def load_canvas(file_canvas,zoomfactor=1.,Debug=False): f0 = fits.open(file_canvas) im_canvas0 = f0[0].data hdr_canvas0= f0[0].header #Vtools.View(f0) hdr_canvas = deepcopy(hdr_canvas0) hdr_canvas['CDELT1']*=zoomfactor hdr_canvas['CDELT2']*=zoomfactor f1=gridding(f0,hdr_canvas,ReturnHDUList=True) im_canvas = f1[0].data pixscale=hdr_canvas['CDELT2']*3600. (ny,nx) = im_canvas.shape x=np.arange(0,nx) y=np.arange(0,ny) X, Y = np.meshgrid(x, y) X0 = np.floor(nx/2) Y0 = np.floor(ny/2) dxxs = -pixscale *(X-X0) dyys = pixscale *(Y-Y0) rrs = np.sqrt( (dxxs)**2 + (dyys)**2) hdr_canvas['PIXSCALE'] =pixscale return rrs,hdr_canvas,hdr_canvas0
def punch_skymap(im_sky,hdr_canvas,hdr_canvas0,PA,fileouttag,fileout_basename='H_top_sky.fits'): rotangle= -PA im_sky_rot = ndimage.rotate(im_sky, rotangle, reshape=False) hdu = fits.PrimaryHDU() hdu.header=hdr_canvas hdu.data=im_sky_rot hdu0=gridding(hdu,hdr_canvas0,ReturnHDUList=True) fileout=fileouttag+fileout_basename hdu0.writeto(fileout, overwrite=True) return hdu0
def View(indata,cmap='RdBu_r',AllContours=False): #help(indata) #indatatype=type(indata) global hdu AddContours=False if isinstance(indata,list): # astropy.io.fits.hdu.hdulist.HDUList): #print("This is an HDU List") hdu=indata[0] if isinstance(hdu,astropy.io.fits.hdu.hdulist.HDUList): hdu=hdu[0] elif isinstance(indata,np.ndarray): hdu = fits.PrimaryHDU() hdu.data = indata hdr=hdu.header hdr['CDELT1']=1. hdr['CDELT2']=1. (nx,ny)=indata.shape hdr['CRPIX1']=int(nx/2.) hdr['CRPIX2']=int(ny/2.) hdr['CTYPE1']='pixel' hdr['CTYPE2']='pixel' if (len(indata) > 1): hducontours=indata[1] AddContours=True if isinstance(hducontours,astropy.io.fits.hdu.hdulist.HDUList): hducontours=hducontours[0] elif isinstance(hducontours,np.ndarray): imcont=indata[1] hducontours = fits.PrimaryHDU() hducontours.data = imcont hdr=hducontours.header hdrcontours['CDELT1']=1. hdrcontours['CDELT2']=1. (nx,ny)=imcont.shape hdrcontours['CRPIX1']=int(nx/2.) hdrcontours['CRPIX2']=int(ny/2.) hdrcontours['CTYPE1']='pixel' hdrcontours['CTYPE2']='pixel' else: # isinstance(indata,astropy.io.fits.hdu.hdulist): #print("This is an HDU") hdu=indata if isinstance(hdu,astropy.io.fits.hdu.hdulist.HDUList): hdu=hdu[0] elif isinstance(indata,np.ndarray): hdu = fits.PrimaryHDU() hdu.data = indata hdr=hdu.header hdr['CDELT1']=1. hdr['CDELT2']=1. (nx,ny)=indata.shape hdr['CRPIX1']=int(nx/2.) hdr['CRPIX2']=int(ny/2.) hdr['CTYPE1']='pixel' hdr['CTYPE2']='pixel' im=hdu.data hdr=hdu.header if (not 'CTYPE1' in hdr.keys()): hdr['CTYPE1']='pixel' hdr['CTYPE2']='pixel' (nx,ny)=im.shape hdr['CRPIX1']=int(nx/2.) hdr['CRPIX2']=int(ny/2.) # mpl.use('TkAgg') global fig1 global ax1 fig1,ax1=plt.subplots() (d0,a0)=pix2wcs_0CRVAL(0.,0.) (d1,a1)=pix2wcs_0CRVAL(hdr['NAXIS2']-1,hdr['NAXIS1']-1) #(j0,i0)=wcs2pix_0CRVAL(d0,a0) #(j1,i1)=wcs2pix_0CRVAL(d1,a1) #print("a0 d0 a1 d1", a0,d0,a1,d1) #print("i0 j0 i1 j1", i0,j0,i1,j1) range2=np.max(im[np.where(np.isfinite(im))]) range1=np.min(im[np.where(np.isfinite(im))]) MedianvalRange=False if MedianvalRange: typicalvalue=np.median(im) medrms=np.sqrt(np.median( (im - typicalvalue)**2)) print("typical value ",typicalvalue," rms ",rms,"medrms",medrms) range1=typicalvalue-3.*medrms range2=typicalvalue+3.*medrms global theimage theimage=ax1.imshow(im, origin='lower', cmap=cmap, #norm=norm, extent=[a0,a1,d0,d1], vmin=range1, vmax=range2, interpolation='nearest') #'nearest' 'bicubic' colorbar(theimage) if AllContours: levels=np.array([0.2,0.4,0.6,0.8])*np.max(im) #ax1.contour(im, origin='lower', extent=[a0,a1,d0,d1], levels=levels,colors='green',linewidths=2.0,alpha=0.5) ax1.contour(im, origin='lower', extent=[a0,a1,d0,d1], levels=levels, cmap=cmap) if AddContours: hducontours_matched=gridding(hducontours,hdr,ReturnHDU=True) imcontours=hducontours_matched.data levels=np.array([0.2,0.4,0.6,0.8])*np.max(imcontours) if AllContours: ax1.contour(imcontours,origin='lower', colors='green', extent=[a0,a1,d0,d1], levels=levels,linewidths=2.0,alpha=0.5) else: ax1.contour(imcontours,origin='lower', cmap=cmap, extent=[a0,a1,d0,d1], levels=levels) #help(theimage) global ClearAll ClearAll=False toggle_selector.RS = RectangleSelector(ax1, line_select_callback, drawtype='box', useblit=True, button=[1, 3], # don't use middle button minspanx=5, minspany=5, spancoords='pixels', interactive=True) #help(toggle_selector.RS) #print("Pretty printing togle_selector") #pp(toggle_selector.RS) toggle_selector.RS.set_active(False) plt.connect('key_press_event', toggle_selector) plt.show()
def gen_surface(file_canvas, file_psiprofile=False, PA=0., inc=0., fileouttag='H', ForceTop=False, ncores=4, Verbose=False, RunTransforms=False, nrmesh=36, z0=0.4, r0=1., q=1., r1=2., r2=4, zoomfactor=1., DoConicPolar=False, DoOffsetPolar=True): global Debug Debug = Verbose global ConicPolar global OffsetPolar ConicPolar = DoConicPolar OffsetPolar = DoOffsetPolar if ConicPolar: OffsetPolar = False if file_psiprofile: (rregions, psis, psi_ds, psi_us) = np.loadtxt(file_psiprofile, unpack=True) Hs = rregions * np.tan(psis * np.pi / 180.) tanpsis = np.tan(psis * np.pi / 180.) rmax0 = np.max(rregions) if (ForceTop): Hs = np.fabs(Hs) tanpsis = np.fabs(tanpsis) f0 = fits.open(file_canvas) im_canvas0 = f0[0].data hdr_canvas0 = f0[0].header #Vtools.View(f0) hdr_canvas = deepcopy(hdr_canvas0) hdr_canvas['CDELT1'] *= zoomfactor hdr_canvas['CDELT2'] *= zoomfactor f1 = gridding(f0, hdr_canvas, ReturnHDUList=True) im_canvas = f1[0].data #Vtools.View(im_canvas) pixscale = hdr_canvas['CDELT2'] * 3600. (ny, nx) = im_canvas.shape x = np.arange(1, nx + 1) y = np.arange(1, ny + 1) X, Y = np.meshgrid(x, y) X0 = np.floor(nx / 2) + 1 Y0 = np.floor(ny / 2) + 1 dxxs = -pixscale * (X - X0) dyys = pixscale * (Y - Y0) rrs = np.sqrt((dxxs)**2 + (dyys)**2) rmax = np.max(rrs) if Debug: print("rmax", rmax) if file_psiprofile: if (zoomfactor > 1.): rregions = np.append([0.], rregions) rregions = np.append(rregions, [rmax / 2., rmax]) Hs = np.append(0., Hs) Hs = np.append(Hs, [0., 0.]) tanpsis = np.append(0., tanpsis) tanpsis = np.append(tanpsis, [0., 0.]) else: rregions = np.append([0.], rregions) rregions = np.append(rregions, rmax) Hs = np.append(0., Hs) Hs = np.append(Hs, 0.) tanpsis = np.append(0., tanpsis) tanpsis = np.append(tanpsis, 0.) if Debug: print("rregions", rregions) print("Hs", Hs) #fH = interp1d(rregions, Hs, kind='cubic') #ftanpsi = interp1d(rregions, tanpsis, kind='cubic') ##finvH = interp1d(Hs, rregions, kind='cubic') popt, pcov = curve_fit(z_func, rregions, tanpsis, p0=[0., 0.5, 1., 1.], bounds=[[-10., 0., 0., 0.1], [10., 10., 10., 4.]]) obsprof = np.zeros((len(rregions), 2)) modprof = np.zeros((len(rregions), 2)) obsprof[:, 0] = rregions obsprof[:, 1] = tanpsis z0 = popt[0] r0 = popt[1] r1 = popt[2] q = popt[3] if Debug: print("z0 ", z0, "r0 ", r0, "r1 ", r1, "q", q) Retro = False #if z0 < 0.: # Hsign=-1 # Retro=True modprof[:, 0] = rregions modprof[:, 1] = z_func(rregions, z0, r0, r1, q) if Debug: Vtools.Spec([obsprof, modprof]) #HHs=fH(rrs) #HHs=z_func(rrs,z0,r0,r1,q) #r2=4. #r1=2. HHs = z_func_gap(rrs, z0, r0, q, r1, r2) master_Hsign = np.sign(z0) fileout = fileouttag + 'H_faceon.fits' fits.writeto(fileout, HHs, hdr_canvas, overwrite=True) ######################################################################## ## default expansion # #M.workdir='polarmaps_default/' # directory for products #M.prep_files() #M.polar_expansions() nphis = HHs.shape[0] if Debug: print("nphis", nphis) nrs = HHs.shape[1] if Debug: print("nrs", nrs) HHs_polar = carttopolar(HHs, 0.) hdupolar = fits.PrimaryHDU() hdrpolar = hdupolar.header hdrpolar['NAXIS1'] = nrs hdrpolar['NAXIS2'] = nphis hdrpolar['CRPIX1'] = 1 hdrpolar['CRVAL1'] = 0. hdrpolar['CDELT1'] = 2. * np.pi / nphis hdrpolar['CRPIX2'] = 1 hdrpolar['CRVAL2'] = 0. hdrpolar['CDELT2'] = hdr_canvas['CDELT2'] hdupolar.header = hdrpolar rs = 3600. * (np.arange(hdrpolar['NAXIS2']) - hdrpolar['CRPIX2'] + 1.0) * hdrpolar['CDELT2'] + hdrpolar['CRVAL2'] if Debug and not file_psiprofile: modprof = np.zeros((len(rs), 2)) modprof[:, 0] = rs modprof[:, 1] = z_func_gap(rs, z0, r0, q, r1, r2) #modprof[:,1]=z_func(rs,z0,r0,r1,q) print("model H(R)") Vtools.Spec([modprof]) #modprof[:,1]=ftanpsi(rs,z0,r0,r1,q) #modprof[:,1]=ftanpsi_gap(rs,z0,r0,q,r1,r2) modprof[:, 1] = z_func_gap(rs, z0, r0, q, r1, r2) / rs print("model h(R)") Vtools.Spec([ modprof, ]) #modprof[:,1]=np.arctan(ftanpsi(rs,z0,r0,r1,q))*180./np.pi modprof[:, 1] = np.arctan( z_func_gap(rs, z0, r0, q, r1, r2) / rs) * 180. / np.pi print("model psi(R)") Vtools.Spec([ modprof, ]) rrs_polar = np.zeros(HHs_polar.shape) print("nphis", nphis, "nx", nx) x = np.arange(1, nx + 1) y = np.arange(1, ny + 1) X, Y = np.meshgrid(x, y) rrs_polar = 3600. * (Y - hdrpolar['CRPIX2'] + 1.0) * hdrpolar['CDELT2'] + hdrpolar['CRVAL2'] phis_polar = (X - hdrpolar['CRPIX1'] + 1.0) * hdrpolar['CDELT1'] + hdrpolar['CRVAL1'] if Debug: Vtools.View(phis_polar) zero_offset = hdr_canvas['CDELT2'] * 3600. rmesh = zero_offset + (np.arange(nrmesh) / (nrmesh - 1)) * rmax tasks = [] for iregion in list(range(nrmesh - 1)): Rmesh1 = rmesh[iregion] Rmesh2 = rmesh[iregion + 1] tanpsi = z_func_gap(Rmesh1, z0, r0, q, r1, r2) / Rmesh1 psi_deg_mod = np.fabs(np.arctan(tanpsi)) * 180. / np.pi inc_deg_mod = inc * 180. / np.pi if (inc_deg_mod > 90.): inc_deg_mod = 180. - inc_deg if Debug: print("Rmesh1: ", Rmesh1, "Rmesh2: ", Rmesh2) print("tanpsi", tanpsi, "psi_deg_mod", psi_deg_mod, "inc_deg_mod", inc_deg_mod) if (ConicPolar & (psi_deg_mod > (90. - inc_deg_mod))): print("psi_deg_mod", psi_deg_mod, "inc_deg_mod", inc_deg_mod) sys.exit( "opening angle too steep, no solutions via single-valued conic transforms -> develop bi-valued transforms" ) regionparams = { 'Rmesh1': Rmesh1, 'Rmesh2': Rmesh2, 'inc': inc, 'tanpsi': tanpsi, 'rrs_polar': rrs_polar, 'HHs_polar': HHs_polar, 'phis_polar': phis_polar, 'pixscale': pixscale } tasks.append(regionparams) domain_polar = np.zeros(rrs_polar.shape) datafile = fileouttag + 'binfile_Pooloutput.npy' if RunTransforms: with Pool(ncores) as pool: Pooloutput = list( tqdm(pool.imap(proc_1region, tasks), total=len(tasks))) pool.close() pool.join() np.save(datafile, Pooloutput) else: Pooloutput = np.load(datafile, allow_pickle=True) HHs_sky_top = np.zeros(HHs.shape) HHs_sky_bottom = np.zeros(HHs.shape) phis_sky_top = np.zeros(HHs.shape) phis_sky_bottom = np.zeros(HHs.shape) rrs_sky_top = np.zeros(HHs.shape) rrs_sky_bottom = np.zeros(HHs.shape) for iregion, aregion in enumerate(Pooloutput): regionparams = tasks[iregion] Rmesh1 = regionparams['Rmesh1'] Rmesh2 = regionparams['Rmesh2'] HHs_sky_domain_top = aregion['HHs_sky_domain_top'] HHs_sky_domain_bottom = aregion['HHs_sky_domain_bottom'] rrs_sky_domain_top = aregion['rrs_sky_domain_top'] rrs_sky_domain_bottom = aregion['rrs_sky_domain_bottom'] phis_sky_domain_top = aregion['phis_sky_domain_top'] phis_sky_domain_bottom = aregion['phis_sky_domain_bottom'] inc_deg = inc * 180. / np.pi tanpsi = tasks[iregion]['tanpsi'] psi_deg = np.arctan(tanpsi) * 180. / np.pi if Debug: print("region tanpsi", tanpsi, "psi_deg", psi_deg, "inc_deg", inc_deg) maskN = (((rrs_sky_domain_top >= Rmesh1) | (master_Hsign * np.sign(HHs_sky_top) <= 0.)) & (rrs_sky_domain_top < Rmesh2) & (phis_sky_domain_top > np.pi)) HHs_sky_top[maskN] = HHs_sky_domain_top[maskN] phis_sky_top[maskN] = phis_sky_domain_top[maskN] rrs_sky_top[maskN] = rrs_sky_domain_top[maskN] maskF = (((rrs_sky_domain_top >= Rmesh1) | (master_Hsign * np.sign(HHs_sky_top) <= 0.)) & (rrs_sky_domain_top < Rmesh2) & (phis_sky_domain_top <= np.pi) & (master_Hsign * np.sign(HHs_sky_top) <= 0.)) #maskF=( ((rrs_sky_domain_top >= Rmesh1) ) & (rrs_sky_domain_top < Rmesh2) & (phis_sky_domain_top <= np.pi) & (master_Hsign*np.sign(HHs_sky_top) <= 0.)) HHs_sky_top[maskF] = HHs_sky_domain_top[maskF] phis_sky_top[maskF] = phis_sky_domain_top[maskF] rrs_sky_top[maskF] = rrs_sky_domain_top[maskF] if ((Rmesh1 > r1) & Debug): Vtools.View(phis_sky_domain_top) print("Mask N") arrmaskN = np.zeros(rrs_sky_top.shape) arrmaskN[maskN] = 1. Vtools.View(arrmaskN) print("Mask F") arrmaskF = np.zeros(rrs_sky_top.shape) arrmaskF[maskF] = 1. Vtools.View(arrmaskF) print("combine H") Vtools.View(HHs_sky_top) maskN = (((rrs_sky_domain_bottom >= Rmesh1) | (-master_Hsign * np.sign(HHs_sky_bottom) <= 0.)) & (rrs_sky_domain_bottom < Rmesh2) & (phis_sky_domain_bottom > np.pi)) HHs_sky_bottom[maskN] = HHs_sky_domain_bottom[maskN] phis_sky_bottom[maskN] = phis_sky_domain_bottom[maskN] rrs_sky_bottom[maskN] = rrs_sky_domain_bottom[maskN] maskF = (((rrs_sky_domain_bottom >= Rmesh1) | (-master_Hsign * np.sign(HHs_sky_bottom) <= 0.)) & (rrs_sky_domain_bottom < Rmesh2) & (phis_sky_domain_bottom <= np.pi) & (-master_Hsign * np.sign(HHs_sky_bottom) <= 0.)) HHs_sky_bottom[maskF] = HHs_sky_domain_bottom[maskF] phis_sky_bottom[maskF] = phis_sky_domain_bottom[maskF] rrs_sky_bottom[maskF] = rrs_sky_domain_bottom[maskF] hdu_H_top_sky = punch_skymap(HHs_sky_top, hdr_canvas, hdr_canvas0, PA, fileouttag, fileout_basename='H_top_sky.fits') hdu_phis_top_sky = punch_skymap(phis_sky_top, hdr_canvas, hdr_canvas0, PA, fileouttag, fileout_basename='phis_top_sky.fits') hdu_rrs_top_sky = punch_skymap(rrs_sky_top, hdr_canvas, hdr_canvas0, PA, fileouttag, fileout_basename='rrs_top_sky.fits') hdu_H_bottom_sky = punch_skymap(HHs_sky_bottom, hdr_canvas, hdr_canvas0, PA, fileouttag, fileout_basename='H_bottom_sky.fits') hdu_phis_bottom_sky = punch_skymap(phis_sky_bottom, hdr_canvas, hdr_canvas0, PA, fileouttag, fileout_basename='phis_bottom_sky.fits') hdu_rrs_bottom_sky = punch_skymap(rrs_sky_bottom, hdr_canvas, hdr_canvas0, PA, fileouttag, fileout_basename='rrs_bottom_sky.fits') #punch_skymap(-HHs_sky_top,hdr_canvas,hdr_canvas0,180.,fileouttag,fileout_basename='H_top_xcheck_180degrot.fits') return