def getVis(sbmodelloc, visdataloc): #print(sbmodelloc, visdataloc) # read in the surface brightness map of the model modelimage = fits.getdata(sbmodelloc) modelheader = fits.getheader(sbmodelloc) # load the uv data, including the phase center of the data uu, vv, ww = uvutil.uvload(visdataloc) # load the uv data, including the phase center of the data pcd = uvutil.pcdload(visdataloc) # sample the uv visfile[0].data using the model uushape = uu.shape if len(uushape) == 2: npol = uushape[0] nrow = uushape[1] uushape = (npol, 1, nrow) uu = uu.flatten() vv = vv.flatten() model_complex = sample_vis.uvmodel(modelimage, modelheader, \ uu, vv, pcd) vis_complex = model_complex.reshape(uushape) return vis_complex
def replace_visibilities_galario(visdataloc,sbmodelloc,modelvisloc): #this needs to be open, channel by channel modelimage_cube = fits.getdata(sbmodelloc) #this can be the same throughout modelheader = fits.getheader(sbmodelloc) #these need to be peeled, channel by channel for the following uu_cube, vv_cube, ww_cube = uvutil.uvload(visdataloc) vis_complex_cube, wgt_cube = uvutil.visload(visdataloc) #this can be the same throughout pcd = uvutil.pcdload(visdataloc) #all the following is done channel by channel for chan in range(modelimage_cube.shape[0]): uu=np.ones((uu_cube.shape[0],uu_cube.shape[1],1,uu_cube.shape[3])) vv=np.ones((vv_cube.shape[0],vv_cube.shape[1],1,vv_cube.shape[3])) uu[:,:,0,:]=uu_cube[:,:,chan,:] vv[:,:,0,:]=vv_cube[:,:,chan,:] modelimage=modelimage_cube[chan] uushape = uu.shape if len(uushape) == 2: npol = uushape[0] nrow = uushape[1] uushape = (npol, 1, nrow) uu = uu.flatten() vv = vv.flatten() uu=uu.copy(order='C') vv=vv.copy(order='C') modelimage=np.roll(np.flip(modelimage,axis=0),1,axis=0).copy(order='C').byteswap().newbyteorder() model_complex = sampleImage(modelimage, np.absolute(modelheader['CDELT1'])/180*np.pi, uu, vv) # sample_vis.uvmodel(modelimage, modelheader, uu, vv, pcd) vis_complex = model_complex.reshape(uushape) vis_complex_cube[:,:,chan,:]=vis_complex[:,:,0,:] if chan%10==0: print chan #modelvisloc='../replaced_visib.uvfits' os.system('rm -rf ' + modelvisloc[:-7]+'*') cmd = 'cp ' + visdataloc + ' ' + modelvisloc os.system(cmd) real = np.real(vis_complex_cube) imag = np.imag(vis_complex_cube) visfile = fits.open(modelvisloc, mode='update') visibilities = visfile[0].data visheader = visfile[0].header if visheader['NAXIS'] == 7: visibilities['DATA'][:, 0, 0, :, :, :, 0] = real visibilities['DATA'][:, 0, 0, :, :, :, 1] = imag elif visheader['NAXIS'] == 6: visibilities['DATA'][:, 0, 0, :, :, 0] = real visibilities['DATA'][:, 0, 0, :, :, 1] = imag else: print("Visibility dataset has >7 or <6 axes. I can't read this.") #visibilities['DATA'][:, 0, 0, :, :, 2] = wgt visfile[0].data = visibilities visfile.flush() visfile.close()
def __init__(self,xs,ys,vs,cellsize,dv,UVFITSfile,cube_template_file): """ This function initializes the modeling object. Parameters ---------- xs: float X-axis size of the model image (in arcsec) ys: float Y-axis size of the model image (in arcsec) vs: float Velocity-axis size of the model image (in km/s) cellsize: float Arcseconds in a pixel dv: float Km/s in a channel UVFITSfile: string Filename of the UVFITS visibility file cube_template_file: string Filename of the image cube to be used as a model template (the dimensions and coordinates are the only thing that's used). It's best obtained by imaging the visibilities themselves, using mode='channel'. This ensures that the cell size is sufficient to suitable for the UV coverage. Returns ------- """ self.xs=xs self.ys=ys #y-axis size of the model image (in arcsec) self.vs=vs self.cellsize=cellsize self.dv=dv self.UVFITSfile=UVFITSfile #'.uvfits' self.cube_template_file=cube_template_file #'.fits' self.sbrad=np.arange(0,2,.01) #radius vector defining the light profile, sbprof is the light intensity evaluated at these radial values self.velrad=self.sbrad #use the same radius vector for the velocity definition, used by velprof to define the velocity profile self.nsamps=1e5 #number of clouds, should be sufficient self.vis_complex_data, self.wgt_data = uvutil.visload(self.UVFITSfile) #load visibility and weights self.vis_complex_model_template=np.copy(self.vis_complex_data) self.wgt_data=self.wgt_data.flatten() self.good_vis=self.wgt_data>0 self.wgt_data=self.wgt_data[self.good_vis] #get rid of flagged data self.vis_complex_data=self.vis_complex_data.flatten()[self.good_vis] self.modelheader = fits.getheader(self.cube_template_file) self.uu_cube, self.vv_cube, self.ww_cube = uvutil.uvload(self.UVFITSfile) self.pcd = uvutil.pcdload(self.UVFITSfile) self.Nxpix=self.modelheader['NAXIS1'] self.Nypix=self.modelheader['NAXIS2'] self.Nchan=self.modelheader['NAXIS3'] self.Nxpix_small=int(self.xs/self.cellsize) self.Nypix_small=int(self.ys/self.cellsize) self.Nchan_small=int(self.vs/self.dv) #In this code we assume the continuum emission can be modelled as a 2D Gaussian, with known parameters. This is often the case and the continuum parameters are often precisely known. If they are not, include the continuum parameters as fitting parameters and make sure the visibilities cover a good range of continuum-only channels. self.y,self.x,self.zz=np.mgrid[:self.Nypix_small,:self.Nxpix_small,:self.Nchan_small] #define a coordinate cube of the same size as the KinMS model, to make the continuum model, to be added in. self.offset_lnlike=0 self.xpos_center_padded,self.ypos_center_padded= int(self.Nxpix/2),int(self.Nypix/2)
def replace(sbmodelloc, visdataloc): # read in the surface brightness map of the model modelimage = fits.getdata(sbmodelloc) modelheader = fits.getheader(sbmodelloc) # read in the uvfits data visfile = fits.open(visdataloc) # load the uv data, including the phase center of the data uu, vv = uvutil.uvload(visfile) # load the uv data, including the phase center of the data pcd = uvutil.pcdload(visfile) # get the number of visfile[0].data, spws, frequencies, polarizations if uu.ndim == 4: nvis = uu[:, 0, 0, 0].size nspw = uu[0, :, 0, 0].size nfreq = uu[0, 0, :, 0].size npol = uu[0, 0, 0, :].size if uu.ndim == 3: nvis = uu[:, 0, 0].size nspw = 0 nfreq = uu[0, :, 0].size npol = uu[0, 0, :].size # sample the uv visfile[0].data using the model uu = uu.flatten() vv = vv.flatten() model_complex = sample_vis.uvmodel(modelimage, modelheader, \ uu, vv, pcd) if nspw > 0: # deflatten the real and imaginary components real = numpy.real(model_complex).reshape(nvis, nspw, nfreq, npol) imag = numpy.imag(model_complex).reshape(nvis, nspw, nfreq, npol) # replace data visfile[0].data with model visfile[0].data visfile[0].data['DATA'][:, 0, 0, :, :, :, 0] = real visfile[0].data['DATA'][:, 0, 0, :, :, :, 1] = imag else: # deflatten the real and imaginary components real = numpy.real(model_complex).reshape(nvis, nfreq, npol) imag = numpy.imag(model_complex).reshape(nvis, nfreq, npol) # replace data visfile[0].data with model visfile[0].data visfile[0].data['DATA'][:, 0, 0, :, :, 0] = real visfile[0].data['DATA'][:, 0, 0, :, :, 1] = imag print "Exiting replace" #visfile[0].data = vismodel return visfile
def plot(cleanup=True, configloc='sandbox.yaml', interactive=True, threshold=1.2, plot=True, tag='sandbox'): ''' Parameters ---------- threshold: float in mJy, cleaning threshold ''' # read the input parameters configfile = open(configloc, 'r') config = yaml.load(configfile) paramSetup = setuputil.loadParams(config) fixindx = setuputil.fixParams(paramSetup) testfit = paramSetup['p_l'] visfile = config['UVData'] uuu, vvv, www = uvutil.uvload(visfile) pcd = uvutil.pcdload(visfile) vis_complex, wgt = uvutil.visload(visfile) # remove the data points with zero or negative weight positive_definite = wgt > 0 vis_complex = vis_complex[positive_definite] wgt = wgt[positive_definite] uuu = uuu[positive_definite] vvv = vvv[positive_definite] testlnprob, testmu = lnprob(testfit, vis_complex, wgt, uuu, vvv, pcd, fixindx, paramSetup, computeamp=True) # prepend 1 dummy value to represent lnprob testfit = numpy.append(testlnprob, testfit) # append nmu dummy values that represent the magnification factors nlensedsource = paramSetup['nlensedsource'] nlensedregions = paramSetup['nlensedregions'] nmu = 2 * (numpy.array(nlensedsource).sum() + nlensedregions) for i in range(nmu): testfit = numpy.append(testfit, 0) print("lnprob: %f" %testlnprob) print("Using the following model parameters:") for k, v in zip(paramSetup['pname'], testfit[1:-4]): print("%s : %.4f" %(k,v)) if plot: visualutil.plotFit(config, testfit, threshold, tag=tag, cleanup=cleanup, interactive=interactive) return testlnprob
def plot(cleanup=True, configloc='sandbox.yaml', interactive=True): # read the input parameters configfile = open(configloc, 'r') config = yaml.load(configfile) paramSetup = setuputil.loadParams(config) fixindx = setuputil.fixParams(paramSetup) testfit = paramSetup['p_l'] visfile = config['UVData'] uuu, vvv, www = uvutil.uvload(visfile) pcd = uvutil.pcdload(visfile) vis_complex, wgt = uvutil.visload(visfile) # remove the data points with zero or negative weight positive_definite = wgt > 0 vis_complex = vis_complex[positive_definite] wgt = wgt[positive_definite] uuu = uuu[positive_definite] vvv = vvv[positive_definite] testlnprob, testmu = lnprob(testfit, vis_complex, wgt, uuu, vvv, pcd, fixindx, paramSetup, computeamp=True) # prepend 1 dummy value to represent lnprob testfit = numpy.append(testlnprob, testfit) # append nmu dummy values that represent the magnification factors nlensedsource = paramSetup['nlensedsource'] nlensedregions = paramSetup['nlensedregions'] nmu = 2 * (numpy.array(nlensedsource).sum() + nlensedregions) for i in range(nmu): testfit = numpy.append(testfit, 0) tag = 'sandbox' print("lnprob: %f" % testlnprob) print("Using the following model parameters:") for k, v in zip(paramSetup['pname'], testfit[1:-4]): print("%s : %.4f" % (k, v)) visualutil.plotFit(config, testfit, tag=tag, cleanup=cleanup, interactive=interactive)
fitsfiles = config.FitsFiles nfiles = len(fitsfiles) nvis = [] # read in the observed visibilities uuu = [] vvv = [] real = [] imag = [] wgt = [] for file in fitsfiles: print file vis_data = fits.open(file) uu, vv = uvutil.uvload(vis_data) pcd = uvutil.pcdload(vis_data) real_raw, imag_raw, wgt_raw = uvutil.visload(vis_data) uuu.extend(uu) vvv.extend(vv) real.extend(real_raw) imag.extend(imag_raw) wgt.extend(wgt_raw) # convert the list to an array real = numpy.array(real) imag = numpy.array(imag) wgt = numpy.array(wgt) uuu = numpy.array(uuu) vvv = numpy.array(vvv) #www = numpy.array(www)
uvutil.scalewt(visfile, newvisfile) visfile = newvisfile else: miriad = False else: miriad = False # attempt to process multiple visibility files. This won't work if miriad=True try: filetype = visfile[-6:] if filetype == 'uvfits': uvfits = True else: uvfits = False uuu, vvv, www = uvutil.uvload(visfile) pcd = uvutil.pcdload(visfile) vis_complex, wgt = uvutil.visload(visfile) except: try: for i, ivisfile in enumerate(visfile): filetype = ivisfile[-6:] if filetype == 'uvfits': uvfits = True else: uvfits = False iuuu, ivvv, iwww = uvutil.uvload(ivisfile) ipcd = uvutil.pcdload(ivisfile) ivis_complex, iwgt = uvutil.visload(ivisfile) if i == 0: uuu = iuuu vvv = ivvv