def WriteFitsThisDir(self,iDir,Weight=False): """ Store the dynamic spectrum in a FITS file """ ra,dec=self.DynSpecMS.PosArray.ra[iDir],self.DynSpecMS.PosArray.dec[iDir] strRA=rad2hmsdms(ra,Type="ra").replace(" ",":") strDEC=rad2hmsdms(dec,Type="dec").replace(" ",":") fitsname = "%s/%s/%s_%s_%s.fits"%(self.DIRNAME,self.GiveSubDir(self.DynSpecMS.PosArray.Type[iDir]),self.DynSpecMS.OutName, strRA, strDEC) if Weight: fitsname = "%s/%s.fits"%(self.DIRNAME,"Weights") print(iDir,self.DynSpecMS.PosArray.Type[iDir],fitsname) # Create the fits file prihdr = fits.Header() prihdr.set('CTYPE1', 'Time', 'Time') prihdr.set('CRPIX1', 1., 'Reference') prihdr.set('CRVAL1', 0., 'Time at the reference pixel (sec since OBS-STAR)') deltaT = (Time(self.DynSpecMS.times[1]/(24*3600.), format='mjd', scale='utc') - Time(self.DynSpecMS.times[0]/(24*3600.), format='mjd', scale='utc')).sec prihdr.set('CDELT1', deltaT, 'Delta time (sec)') prihdr.set('CUNIT1', 'Time', 'unit') prihdr.set('CTYPE2', 'Frequency', 'Frequency') prihdr.set('CRPIX2', 1., 'Reference') prihdr.set('CRVAL2', self.DynSpecMS.fMin*1e-6, 'Frequency at the reference pixel (MHz)') prihdr.set('CDELT2', self.DynSpecMS.ChanWidth*1e-6, 'Delta freq (MHz)') prihdr.set('CUNIT2', 'MHz', 'unit') prihdr.set('CTYPE3', 'Stokes parameter', '1=I, 2=Q, 3=U, 4=V') prihdr.set('CRPIX3', 1., 'Reference') prihdr.set('CRVAL3', 1., 'frequence at the reference pixel') prihdr.set('CDELT3', 1., 'Delta stokes') prihdr.set('CUNIT3', '', 'unit') prihdr.set('DATE-CRE', Time.now().iso.split()[0], 'Date of file generation') prihdr.set('OBSID', self.DynSpecMS.OutName, 'LOFAR Observation ID') prihdr.set('CHAN-WID', self.DynSpecMS.ChanWidth, 'Frequency channel width') prihdr.set('FRQ-MIN', self.DynSpecMS.fMin, 'Minimal frequency') prihdr.set('FRQ-MAX', self.DynSpecMS.fMax, 'Maximal frequency') prihdr.set('OBS-STAR', self.DynSpecMS.tStart, 'Observation start date') prihdr.set('OBS-STOP', self.DynSpecMS.tStop, 'Observation end date') prihdr.set('RA_RAD', ra, 'Pixel right ascension') prihdr.set('DEC_RAD', dec, 'Pixel declination') name=self.DynSpecMS.PosArray.Name[iDir] if not isinstance(name,str): # it must be a byte string, this must be Python 3, act accordingly name=name.decode('utf-8') prihdr.set('NAME', name, 'Name of the source in the source list') prihdr.set('ORIGIN', 'DynSpecMS '+version(),'Created by') if Weight: Gn = self.DynSpecMS.DicoGrids["GridWeight"][iDir,:, :, :].real # dir, time, freq, pol else: Gn = self.DynSpecMS.GOut[iDir,:, :, :].real hdu = fits.PrimaryHDU(np.rollaxis(Gn, 2), header=prihdr) hdu.writeto(fitsname, overwrite=True)
def __init__(self, ListMSName=None, ColName="DATA", ModelName="PREDICT_KMS", UVRange=[1., 1000.], ColWeights=None, SolsName=None, FileCoords="Transient_LOTTS.csv", Radius=3., NOff=-1, ImageI=None, ImageV=None, SolsDir=None, NCPU=1, BaseDirSpecs=None, BeamModel=None, BeamNBand=1): self.ColName = ColName self.ModelName = ModelName self.ColWeights = ColWeights self.BeamNBand = BeamNBand self.UVRange = UVRange self.Mode = "Spec" self.BaseDirSpecs = BaseDirSpecs self.NOff = NOff self.SolsName = SolsName self.NCPU = NCPU self.BeamModel = BeamModel if ListMSName is None: print(ModColor.Str("WORKING IN REPLOT MODE"), file=log) self.Mode = "Plot" self.Radius = Radius self.ImageI = ImageI self.ImageV = ImageV self.SolsDir = SolsDir #self.PosArray=np.genfromtxt(FileCoords,dtype=[('Name','S200'),("ra",np.float64),("dec",np.float64),('Type','S200')],delimiter="\t") # identify version in logs print("DynSpecMS version %s starting up" % version(), file=log) self.FileCoords = FileCoords if self.Mode == "Spec": self.ListMSName = sorted(ListMSName) #[0:2] self.nMS = len(self.ListMSName) self.OutName = self.ListMSName[0].split("/")[-1].split("_")[0] self.ReadMSInfos() self.InitFromCatalog() elif self.Mode == "Plot": self.OutName = self.BaseDirSpecs.split("_")[-1] self.InitFromSpecs()
#!/usr/bin/env python __author__ = "Cyril Tasse, and Alan Loh" #__copyright__ = "Copyright 2007, The Cogent Project" __credits__ = ["Cyril Tasse", "Alan Loh"] #__license__ = "GPL" from dynspecms_version import version __version__ = version() #__maintainer__ = "Rob Knight" #__email__ = "*****@*****.**" #__status__ = "Production" SaveFile = "last_dynspec.obj" """ ========================================================================= DESCRIPTION Blablabla Modif Example: python ms2dynspec.py --msfile /usr/data/ --data CORRECTED --model PREDICT --sols killms.npz --srclist SRCPOS.txt --rad 10 ------------------------------------------------------------------------- TO DO - convertSrclist: only keep (RA, Dec) wich are within the field -- DONE - convertSrclist: add some other ~random positions on wich to compute dynamic spectra for comparison - stokes computation: CHECK correct I Q U V computation! -- DONE ========================================================================= """ import sys import os
def __init__(self, ListMSName, ColName="DATA", ModelName="PREDICT_KMS", UVRange=[1., 1000.], SolsName=None, FileCoords=None, Radius=3., NOff=-1, Image=None, SolsDir=None, NCPU=1): self.ListMSName = sorted(ListMSName) #[0:2] self.nMS = len(self.ListMSName) self.ColName = ColName self.ModelName = ModelName self.OutName = self.ListMSName[0].split("/")[-1].split("_")[0] self.UVRange = UVRange self.ReadMSInfos() self.Radius = Radius self.Image = Image self.SolsDir = SolsDir #self.PosArray=np.genfromtxt(FileCoords,dtype=[('Name','S200'),("ra",np.float64),("dec",np.float64),('Type','S200')],delimiter="\t") # identify version in logs print >> log, "DynSpecMS version %s starting up" % version() # should we use the surveys DB? if 'DDF_PIPELINE_DATABASE' in os.environ: print >> log, "Using the surveys database" from surveys_db import SurveysDB with SurveysDB() as sdb: sdb.cur.execute('select * from transients') result = sdb.cur.fetchall() # convert to a list, then to ndarray, then to recarray l = [] for r in result: l.append((r['id'], r['ra'], r['decl'], r['type'])) self.PosArray = np.asarray(l, dtype=[('Name', 'S200'), ("ra", np.float64), ("dec", np.float64), ('Type', 'S200')]) print >> log, "Created an array with %i records" % len(result) else: if FileCoords is None: FileCoords = "Transient_LOTTS.csv" if not os.path.isfile(FileCoords): ssExec = "wget -q --user=anonymous ftp://ftp.strw.leidenuniv.nl/pub/tasse/%s -O %s" % ( FileCoords, FileCoords) print >> log, "Downloading %s" % FileCoords print >> log, " Executing: %s" % ssExec os.system(ssExec) self.PosArray = np.genfromtxt(FileCoords, dtype=[('Name', 'S200'), ("ra", np.float64), ("dec", np.float64), ('Type', 'S200')], delimiter=",")[()] self.PosArray = self.PosArray.view(np.recarray) self.PosArray.ra *= np.pi / 180. self.PosArray.dec *= np.pi / 180. NOrig = self.PosArray.shape[0] Dist = AngDist(self.ra0, self.PosArray.ra, self.dec0, self.PosArray.dec) ind = np.where(Dist < Radius * np.pi / 180)[0] self.PosArray = self.PosArray[ind] self.NDirSelected = self.PosArray.shape[0] print >> log, "Selected %i target [out of the %i in the original list]" % ( self.NDirSelected, NOrig) if self.NDirSelected == 0: print >> log, ModColor.Str(" Have found no sources - returning") self.killWorkers() return if NOff == -1: NOff = self.PosArray.shape[0] * 2 if NOff is not None: print >> log, "Including %i off targets" % (NOff) self.PosArray = np.concatenate( [self.PosArray, self.GiveOffPosArray(NOff)]) self.PosArray = self.PosArray.view(np.recarray) self.NDir = self.PosArray.shape[0] print >> log, "For a total of %i targets" % (self.NDir) self.DicoDATA = shared_dict.create("DATA") self.DicoGrids = shared_dict.create("Grids") self.DicoGrids["GridLinPol"] = np.zeros( (self.NDir, self.NChan, self.NTimes, 4), np.complex128) self.DicoGrids["GridWeight"] = np.zeros( (self.NDir, self.NChan, self.NTimes, 4), np.complex128) self.SolsName = SolsName self.DoJonesCorr = False if self.SolsName: self.DoJonesCorr = True self.DicoJones = shared_dict.create("DicoJones") APP.registerJobHandlers(self) AsyncProcessPool.init(ncpu=NCPU, affinity=0) APP.startWorkers()