def test_fmt_angle(): assert fmt_angle(1.9999, pre=3, trunc=True) == '+01 59 59.639' assert fmt_angle(1.9999, pre=0, trunc=True) == '+01 59 59' assert fmt_angle(12.348978659, pre=4, trunc=True) == '+12 20 56.3231' assert fmt_angle(12.348978659, pre=5) == '+12 20 56.32317' assert fmt_angle(12.348978659, s1='HH ', s2='MM ', s3='SS', pre=5) == '+12HH 20MM 56.32317SS' x = 23+59/60.0+59.99999/3600.0 assert fmt_angle(x) == '+24 00 00.000' assert fmt_angle(x, lower=0, upper=24, upper_trim=True) == '+00 00 00.000' assert fmt_angle(x, pre=5) == '+23 59 59.99999' assert fmt_angle(-x, lower=0, upper=24, upper_trim=True) == '+00 00 00.000' assert fmt_angle(-x) == '-24 00 00.000'
def test_fmt_angle(): assert fmt_angle(1.9999, pre=3, trunc=True) == '+01 59 59.639' assert fmt_angle(1.9999, pre=0, trunc=True) == '+01 59 59' assert fmt_angle(12.348978659, pre=4, trunc=True) == '+12 20 56.3231' assert fmt_angle(12.348978659, pre=5) == '+12 20 56.32317' assert fmt_angle(12.348978659, s1='HH ', s2='MM ', s3='SS', pre=5) == '+12HH 20MM 56.32317SS' x = 23 + 59 / 60.0 + 59.99999 / 3600.0 assert fmt_angle(x) == '+24 00 00.000' assert fmt_angle(x, lower=0, upper=24, upper_trim=True) == '+00 00 00.000' assert fmt_angle(x, pre=5) == '+23 59 59.99999' assert fmt_angle(-x, lower=0, upper=24, upper_trim=True) == '+00 00 00.000' assert fmt_angle(-x) == '-24 00 00.000'
def write_ascii_file(self, dirname, outname): """ Write ascii file containing astronomy data """ # need the current time to update coordiantes now = self.utc if self.writecount > 0: print("File %4d: %s (%d)" % (self.writecount, outname, self.count)) fullname = dirname + outname outfile = open(fullname, 'w') outfile.write('# File: ' + outname + '\n') self.noteA = self.noteA.replace('\n', '') self.noteA = self.noteA.strip() outline = '# NOTEA = ' + self.noteA + '\n' outfile.write(outline) self.noteB = self.noteB.replace('\n', '') self.noteB = self.noteB.strip() outline = '# NOTEB = ' + self.noteB + '\n' outfile.write(outline) self.observer = self.observer.replace('\n', '') self.observer = self.observer.strip() outline = '# OBSERVER = ' + self.observer + '\n' outfile.write(outline) self.device = self.device.replace('\n', '') self.device = self.device.strip() outline = '# DEVICE = ' + self.device + '\n' outfile.write(outline) self.datadir = self.datadir.replace('\n', '') self.datadir = self.datadir.strip() outline = '# DATADIR = ' + self.datadir + '\n' outfile.write(outline) self.site = self.site.replace('\n', '') self.site = self.site.strip() outline = '# SITE = ' + self.site + '\n' outfile.write(outline) self.city = self.city.replace('\n', '') self.city = self.city.strip() outline = '# CITY = ' + self.city + '\n' outfile.write(outline) self.region = self.region.replace('\n', '') self.region = self.region.strip() outline = '# REGION = ' + self.region + '\n' outfile.write(outline) self.country = self.country.replace('\n', '') self.country = self.country.strip() outline = '# COUNTRY = ' + self.country + '\n' outfile.write(outline) self.telType = self.telType.replace('\n', '') self.telType = self.telType.strip() outline = '# TELTYPE = ' + self.telType + '\n' outfile.write(outline) self.frame = self.frame.replace('\n', '') self.frame = self.frame.strip() outline = '# FRAME = ' + self.frame + '\n' outfile.write(outline) ngains = len(self.gains) if ngains > 0: outline = '# GAIN1 = ' + str(self.gains[0]) + '\n' outfile.write(outline) if ngains > 1: outline = '# GAIN2 = ' + str(self.gains[1]) + '\n' outfile.write(outline) if ngains > 2: outline = '# GAIN3 = ' + str(self.gains[2]) + '\n' outfile.write(outline) if ngains > 3: outline = '# GAIN4 = ' + str(self.gains[3]) + '\n' outfile.write(outline) outline = '# Count = ' + str(self.count) + '\n' outfile.write(outline) outline = '# CenterFreq= ' + str(self.centerFreqHz) + '\n' outfile.write(outline) outline = '# Bandwidth = ' + str(self.bandwidthHz) + '\n' outfile.write(outline) outline = '# Duration = ' + str(self.durationSec) + '\n' outfile.write(outline) outline = '# DeltaX = ' + str(self.deltaFreq) + '\n' outfile.write(outline) outline = '# TSYS = ' + str(self.tSys) + '\n' outfile.write(outline) outline = '# TRX = ' + str(self.tRx) + '\n' outfile.write(outline) outline = '# TRMS = ' + str(self.tRms) + '\n' outfile.write(outline) outline = '# TINT = ' + str(self.tint) + '\n' outfile.write(outline) outline = '# KPERC = ' + str(self.KperC) + '\n' outfile.write(outline) outline = '# GAINFACT = ' + str(self.gainFactor) + '\n' outfile.write(outline) outline = '# BUNIT = ' + str(self.bunit).strip() + '\n' outfile.write(outline) outline = '# NCHAN = ' + str(self.nChan) + '\n' outfile.write(outline) outline = '# NSPEC = ' + str(self.nSpec) + '\n' outfile.write(outline) outline = '# NTIME = ' + str(self.nTime) + '\n' outfile.write(outline) outline = '# NSAMPLES = ' + str(self.nSamples) + '\n' outfile.write(outline) outline = '# EPEAK = ' + str(self.epeak) + '\n' outfile.write(outline) outline = '# ERMS = ' + str(self.erms) + '\n' outfile.write(outline) outline = '# EMJD = %18.12f \n' % (self.emjd) outfile.write(outline) outline = '# REFCHAN = ' + str(self.refChan) + '\n' outfile.write(outline) outline = '# REFSAMPL = ' + str(self.refSample) + '\n' outfile.write(outline) nave = self.nave outline = '# NAVE = ' + str(nave) + '\n' outfile.write(outline) nmedian = self.nmedian outline = '# NMEDIAN = ' + str(nmedian) + '\n' outfile.write(outline) outline = '# Fft_rate = ' + str(self.fft_rate) + '\n' outfile.write(outline) strnow = now.isoformat() dates = strnow.split('T') datestr = dates[0] + ' ' + dates[1] outline = '# UTC = ' + datestr + '\n' outfile.write(outline) outline = '# SECONDS = %18.10f \n' % (self.seconds) outfile.write(outline) lststr = angles.fmt_angle(self.lst / 15., s1=":", s2=":", pre=3) # convert to hours outline = '# LST = ' + lststr[1:] + '\n' outfile.write(outline) outline = '# AZ = ' + str(self.telaz) + '\n' outfile.write(outline) outline = '# EL = ' + str(self.telel) + '\n' outfile.write(outline) anglestr = angles.fmt_angle(float(self.tellon), s1=":", s2=":") outline = '# TELLON = ' + anglestr + '\n' outfile.write(outline) anglestr = angles.fmt_angle(float(self.tellat), s1=":", s2=":") outline = '# TELLAT = ' + anglestr + '\n' outfile.write(outline) rastr = angles.fmt_angle(self.ra / 15., s1=":", s2=":", pre=3) # convert to hours outline = '# RA = ' + rastr[1:] + '\n' outfile.write(outline) decstr = angles.fmt_angle(self.dec, s1=":", s2=":") outline = '# DEC = ' + decstr + '\n' outfile.write(outline) lonstr = angles.fmt_angle(self.gallon, s1=":", s2=":", pre=2) outline = '# GALLON = ' + lonstr[1:] + '\n' outfile.write(outline) latstr = angles.fmt_angle(self.gallat, s1=":", s2=":", pre=2) outline = '# GALLAT = ' + latstr + '\n' outfile.write(outline) altstr = angles.fmt_angle(self.altsun, s1=":", s2=":", pre=1) outline = '# ALT_SUN = ' + altstr + '\n' outfile.write(outline) az_str = angles.fmt_angle(self.az_sun, s1=":", s2=":", pre=1) outline = '# AZ_SUN = ' + az_str + '\n' outfile.write(outline) outline = '# ETAA = ' + str(self.etaA) + '\n' outfile.write(outline) outline = '# ETAB = ' + str(self.etaB) + '\n' outfile.write(outline) outline = '# POLANGLE = ' + str(self.polAngle) + '\n' outfile.write(outline) outline = '# TELSIZEAM = ' + str(self.telSizeAm) + '\n' outfile.write(outline) outline = '# TELSIZEBM = ' + str(self.telSizeBm) + '\n' outfile.write(outline) outline = '# AST_VERS = ' + str("05.01") + '\n' outfile.write(outline) if self.nTime > 0: # if an event self.nSpec = 0 # then not a spectrum # if a spectrum in this data stream if self.nSpec > 0: dx = self.bandwidthHz / float(self.nChan) x = self.centerFreqHz - (self.bandwidthHz / 2.) + (dx / 2.) leny = len(self.ydataA) if self.nSpec > 1: pformat = "%04d %s %.4f %.4f\n" for i in range(min(self.nChan, leny)): outline = pformat % (i, str( int(x)), self.ydataA[i], self.ydataB[i]) outfile.write(outline) x = x + dx else: pformat = "%04d %s %.4f\n" for i in range(min(self.nChan, leny)): # outline = str(i).zfill(4) + ' ' + str(long(x)) + ' ' + str(self.ydataA[i]) + '\n' outline = pformat % (i, str(int(x)), self.ydataA[i]) outline = outline.replace(' ', ' ') outfile.write(outline) x = x + dx del outline if self.nTime > 0: dt = 1. / self.bandwidthHz # sample rate is inverse bandwidth t = -dt * self.refSample # time tag relative to event sample leny = len(self.ydataA) self.nChan = 0 # cannot be both samples and spectra if leny > self.nSamples: self.nSamples = leny print("Y array length and N Sample miss match:", leny, self.nSamples) if TIMEPARTS == 2: # if not writing time outline = "# I Q\n" outfile.write(outline) pformat = "%.5f %.5f\n" if self.nSamples < 2: print("Very small number of samples: ", self.nSamples) print("N Chan: %5d; N x: %5d " % (self.nChan, len(self.xdata))) print("N y1 : %5d; N y2: %5d " % (len(self.ydataA), len(self.ydataB))) for i in range(self.nSamples): outline = pformat % (self.ydataA[i], self.ydataB[i]) outline = outline.replace(' 0.', ' .') outline = outline.replace('-0.', '-.') outfile.write(outline) else: # else writing sample #, time, I and Q outline = "# dt I Q\n" outfile.write(outline) pformat = "%04d %11.9f %7.5f %7.5f\n" for i in range(self.nSamples): outline = pformat % (i, t, self.ydataA[i], self.ydataB[i]) outline = outline.replace(' 0.', ' .') outline = outline.replace('-0.', '-.') outfile.write(outline) t = t + dt del outline outfile.close()
def write_ascii_file(self, dirname, outname): """ Write ascii file containing astronomy data """ # need the current time to update coordiantes now = self.utc print "File %4d: %s (%d)" % (self.writecount, outname, self.count) fullname = dirname + outname outfile = open(fullname, 'w') outfile.write('# File: ' + outname + '\n') gainstr = '' ngains = len(self.gains) for iii in range(ngains-1): gainstr = gainstr + str(self.gains[iii]) + '; ' gainstr = gainstr + str(self.gains[ngains-1]) self.noteA = self.noteA.replace('\n', '') self.noteA = self.noteA.strip() outline = '# NOTEA = ' + self.noteA + '\n' outfile.write(outline) self.noteB = self.noteB.replace('\n', '') self.noteB = self.noteB.strip() outline = '# NOTEB = ' + self.noteB + '\n' outfile.write(outline) self.observer = self.observer.replace('\n', '') self.observer = self.observer.strip() outline = '# OBSERVER = ' + self.observer + '\n' outfile.write(outline) self.device = self.device.replace('\n', '') self.device = self.device.strip() outline = '# DEVICE = ' + self.device + '\n' outfile.write(outline) self.datadir = self.datadir.replace('\n', '') self.datadir = self.datadir.strip() outline = '# DATADIR = ' + self.datadir + '\n' outfile.write(outline) self.site = self.site.replace('\n', '') self.site = self.site.strip() outline = '# SITE = ' + self.site + '\n' outfile.write(outline) self.city = self.city.replace('\n', '') self.city = self.city.strip() outline = '# CITY = ' + self.city + '\n' outfile.write(outline) self.region = self.region.replace('\n', '') self.region = self.region.strip() outline = '# REGION = ' + self.region + '\n' outfile.write(outline) self.country = self.country.replace('\n', '') self.country = self.country.strip() outline = '# COUNTRY = ' + self.country + '\n' outfile.write(outline) self.telType = self.telType.replace('\n', '') self.telType = self.telType.strip() outline = '# TELTYPE = ' + self.telType + '\n' outfile.write(outline) self.frame = self.frame.replace('\n', '') self.frame = self.frame.strip() outline = '# FRAME = ' + self.frame + '\n' outfile.write(outline) outline = '# GAINS = ' + gainstr + '\n' outfile.write(outline) ngains = len(self.gains) if ngains > 0: outline = '# GAIN1 = ' + str(self.gains[0]) + '\n' outfile.write(outline) if ngains > 1: outline = '# GAIN2 = ' + str(self.gains[1]) + '\n' outfile.write(outline) if ngains > 2: outline = '# GAIN3 = ' + str(self.gains[2]) + '\n' outfile.write(outline) if ngains > 3: outline = '# GAIN4 = ' + str(self.gains[3]) + '\n' outfile.write(outline) outline = '# Count = ' + str(self.count) + '\n' outfile.write(outline) outline = '# CenterFreq= ' + str(self.centerFreqHz) + '\n' outfile.write(outline) outline = '# Bandwidth = ' + str(self.bandwidthHz) + '\n' outfile.write(outline) outline = '# Duration = ' + str(self.durationSec) + '\n' outfile.write(outline) outline = '# DeltaX = ' + str(self.deltaFreq) + '\n' outfile.write(outline) outline = '# BUNIT = ' + str(self.bunit).strip() + '\n' outfile.write(outline) nChan = len(self.ydataA) outline = '# NCHAN = ' + str(nChan) + '\n' outfile.write(outline) nSpec = self.nSpec outline = '# NSPEC = ' + str(nSpec) + '\n' outfile.write(outline) nTime = self.nTime outline = '# NTIME = ' + str(nTime) + '\n' outfile.write(outline) nSamples = self.nSamples outline = '# NSAMPLES = ' + str(nSamples) + '\n' outfile.write(outline) outline = '# EPEAK = ' + str(self.epeak) + '\n' outfile.write(outline) outline = '# ERMS = ' + str(self.erms) + '\n' outfile.write(outline) outline = '# REFCHAN = ' + str(self.refChan) + '\n' outfile.write(outline) outline = '# REFSAMPL = ' + str(self.refSample) + '\n' outfile.write(outline) nave = self.nave outline = '# NAVE = ' + str(nave) + '\n' outfile.write(outline) nmedian = self.nmedian outline = '# NMEDIAN = ' + str(nmedian) + '\n' outfile.write(outline) outline = '# Fft_rate = ' + str(self.fft_rate) + '\n' outfile.write(outline) strnow = now.isoformat() dates = strnow.split('T') datestr = dates[0] + ' ' + dates[1] outline = '# UTC = ' + datestr + '\n' outfile.write(outline) lststr = angles.fmt_angle(self.lst/15., s1=":", s2=":", pre=3) # convert to hours outline = '# LST = ' + lststr[1:] + '\n' outfile.write(outline) outline = '# AZ = ' + str(self.telaz) + '\n' outfile.write(outline) outline = '# EL = ' + str(self.telel) + '\n' outfile.write(outline) anglestr = angles.fmt_angle(float(self.tellon), s1=":", s2=":") outline = '# TELLON = ' + anglestr + '\n' outfile.write(outline) anglestr = angles.fmt_angle(float(self.tellat), s1=":", s2=":") outline = '# TELLAT = ' + anglestr + '\n' outfile.write(outline) rastr = angles.fmt_angle(self.ra/15., s1=":", s2=":", pre=3) # convert to hours outline = '# RA = ' + rastr[1:] + '\n' outfile.write(outline) decstr = angles.fmt_angle(self.dec, s1=":", s2=":") outline = '# DEC = ' + decstr + '\n' outfile.write(outline) lonstr = angles.fmt_angle(self.gallon, s1=":", s2=":", pre=2) outline = '# GALLON = ' + lonstr[1:] + '\n' outfile.write(outline) latstr = angles.fmt_angle(self.gallat, s1=":", s2=":", pre=2) outline = '# GALLAT = ' + latstr + '\n' outfile.write(outline) altstr = angles.fmt_angle(self.altsun, s1=":", s2=":", pre=1) outline = '# ALT_SUN = ' + altstr + '\n' outfile.write(outline) az_str = angles.fmt_angle(self.az_sun, s1=":", s2=":", pre=1) outline = '# AZ_SUN = ' + az_str + '\n' outfile.write(outline) outline = '# ETAA = ' + str(self.etaA) + '\n' outfile.write(outline) outline = '# ETAB = ' + str(self.etaB) + '\n' outfile.write(outline) outline = '# POLANGLE = ' + str(self.polAngle) + '\n' outfile.write(outline) outline = '# TELSIZEAM = ' + str(self.telSizeAm) + '\n' outfile.write(outline) outline = '# TELSIZEBM = ' + str(self.telSizeBm) + '\n' outfile.write(outline) outline = '# AST_VERS = ' + str("05.01") + '\n' outfile.write(outline) if self.nTime > 0: # if an event self.nSpec = 0 # then not a spectrum # if a spectrum in this data stream if self.nSpec > 0: dx = self.bandwidthHz/float(self.nChan) x = self.centerFreqHz - (self.bandwidthHz/2.) + (dx/2.) yv = self.ydataA leny = len(yv) for i in range(min(self.nChan, leny)): outline = str(i).zfill(4) + ' ' + str(long(x)) + ' ' + str(yv[i]) + '\n' outfile.write(outline) x = x + dx del outline if self.nTime > 0: dt = 1./self.bandwidthHz # sample rate is inverse bandwidth t = -dt * self.refSample # time tag relative to event sample yvI = self.ydataA # time samples are I/Q (complex) values yvQ = self.ydataB # time samples are I/Q (complex) values leny = len(yvI) if TIMEPARTS == 2: # if not writing time outline = "# I Q\n" outfile.write(outline) pformat = "%.5f %.5f\n" for i in range(min(self.nSamples, leny)): outline = pformat % (yvI[i], yvQ[i]) outline = outline.replace(' 0.', ' .') outline = outline.replace('-0.', '-.') outfile.write(outline) else: # else writing sample #, time, I and Q outline = "# dt I Q\n" outfile.write(outline) pformat = "%04d %11.9f %7.5f %7.5f\n" for i in range(min(self.nSamples, leny)): outline = pformat % (i, t, yvI[i], yvQ[i]) outline = outline.replace(' 0.', ' .') outline = outline.replace('-0.', '-.') outfile.write(outline) t = t + dt del outline outfile.close()
def copy_leda_orig_into_leda(db): cursor = db.cursor() # add the column definition for kleda to kcolumns cursor.execute('delete from kcolumns where dbtable like "%kleda";') db.commit() cursor.execute('delete from ktables where dbtable like "%kleda";') db.commit() define_leda_columns(db) # retrive the list of columns just added using a query cursor.execute( 'select tabcolumn from kcolumns where dbtable like "%kleda";') results = cursor.fetchall() # turn into array columns = [x[0].lstrip().rstrip() for x in results] # turn into a string separated by comma columns = ",".join(columns) print "Creating a new kleda from kleda_orig using columns: " + columns cursor.execute("drop table if exists kleda;") db.commit() cursor.execute("Create table kleda select " + columns + " from kleda_orig;") db.commit() # add primary key cursor.execute("alter table kleda add primary key (pgc);") db.commit() # make sure we don't display kleda_orig cursor.execute('delete from kcolumns where dbtable like "%kleda_orig%";') db.commit() cursor.execute('delete from ktables where dbtable like "%kleda_orig%";') db.commit() # make sure we display kleda cursor.execute('INSERT into ktables values\ ("kleda","Redshift Catalogs","NULL","LEDA","LEDA","LEDA database","NULL","NULL");' ) db.commit() # calculate RA and DEC from al2000 de2000 print "reading ra and dec..." cursor.execute("Select al2000,de2000 from kleda;") results = cursor.fetchall() cursor.close() cursor = db.cursor() print "transforming into new format..." ra = [ "" if x[0] is None else str( angles.fmt_angle(float(x[0]), lower=0, upper=24, pre=1)).replace( "+", "").replace(" ", "") for x in results ] dec = [ "" if x[1] is None else str( angles.fmt_angle(float(x[1]), lower=-90, upper=90, pre=1)).replace( " ", "") for x in results ] #dec = ["" if x is "" else x[:7] for x in dec] print "reading kleda ..." cursor.execute("select * from kleda;") results = cursor.fetchall() results = [list(x) for x in results] # add the RA2000 and DEC2000 columns cursor.execute("ALTER TABLE kleda ADD COLUMN RA2000 varchar(20);") db.commit() cursor.execute("ALTER TABLE kleda ADD COLUMN DEC2000 varchar(20);") db.commit() cursor.execute( "INSERT INTO kcolumns(dbtable,tabcolumn,units,description,ucd,justification,format) VALUES ('kleda','RA2000','h','Right Ascension (J2000)','','r','%12s');" ) db.commit() cursor.execute( "INSERT INTO kcolumns(dbtable,tabcolumn,units,description,ucd,justification,format) VALUES ('kleda','DEC2000','deg','Declination (J2000)','','r','%12s');" ) db.commit() print "adding RA and DEC..." for k in range(len(results)): results[k].append(ra[k]) results[k].append(dec[k]) cursor.execute("Delete from kleda;") db.commit() cursor.close() cursor = db.cursor() format = "%s," * len(results[0]) format = format[:-1] print "lines to add to leda: " + str(len(results)) print "now inserting into kleda with new coordinates..." print results[0] for data in results: try: cursor.execute("Insert into kleda values(" + format + ");", data) except: print "Error: cannot add the data:" print data db.commit() cursor.close()
def copy_leda_orig_into_leda(db): cursor=db.cursor() # add the column definition for kleda to kcolumns cursor.execute('delete from kcolumns where dbtable like "%kleda";') db.commit() cursor.execute('delete from ktables where dbtable like "%kleda";') db.commit() define_leda_columns(db) # retrive the list of columns just added using a query cursor.execute('select tabcolumn from kcolumns where dbtable like "%kleda";') results=cursor.fetchall() # turn into array columns=[x[0].lstrip().rstrip() for x in results] # turn into a string separated by comma columns=",".join(columns) print "Creating a new kleda from kleda_orig using columns: "+columns cursor.execute("drop table if exists kleda;") db.commit() cursor.execute("Create table kleda select "+columns+" from kleda_orig;") db.commit() # add primary key cursor.execute("alter table kleda add primary key (pgc);") db.commit() # make sure we don't display kleda_orig cursor.execute('delete from kcolumns where dbtable like "%kleda_orig%";') db.commit() cursor.execute('delete from ktables where dbtable like "%kleda_orig%";') db.commit() # make sure we display kleda cursor.execute('INSERT into ktables values\ ("kleda","Redshift Catalogs","NULL","LEDA","LEDA","LEDA database","NULL","NULL");') db.commit() # calculate RA and DEC from al2000 de2000 print "reading ra and dec..." cursor.execute("Select al2000,de2000 from kleda;") results=cursor.fetchall() cursor.close() cursor=db.cursor() print "transforming into new format..." ra = ["" if x[0] is None else str(angles.fmt_angle(float(x[0]),lower=0,upper=24,pre=1)).replace("+","").replace(" ","") for x in results] dec = ["" if x[1] is None else str(angles.fmt_angle(float(x[1]),lower=-90,upper=90,pre=1)).replace(" ","") for x in results] #dec = ["" if x is "" else x[:7] for x in dec] print "reading kleda ..." cursor.execute("select * from kleda;") results=cursor.fetchall() results=[list(x) for x in results] # add the RA2000 and DEC2000 columns cursor.execute("ALTER TABLE kleda ADD COLUMN RA2000 varchar(20);") db.commit() cursor.execute("ALTER TABLE kleda ADD COLUMN DEC2000 varchar(20);") db.commit() cursor.execute("INSERT INTO kcolumns(dbtable,tabcolumn,units,description,ucd,justification,format) VALUES ('kleda','RA2000','h','Right Ascension (J2000)','','r','%12s');") db.commit() cursor.execute("INSERT INTO kcolumns(dbtable,tabcolumn,units,description,ucd,justification,format) VALUES ('kleda','DEC2000','deg','Declination (J2000)','','r','%12s');") db.commit() print "adding RA and DEC..." for k in range(len(results)): results[k].append(ra[k]) results[k].append(dec[k]) cursor.execute("Delete from kleda;") db.commit() cursor.close() cursor=db.cursor() format = "%s,"*len(results[0]) format = format[:-1] print "lines to add to leda: "+str(len(results)) print "now inserting into kleda with new coordinates..." print results[0] for data in results: try: cursor.execute("Insert into kleda values("+format+");",data) except: print "Error: cannot add the data:" print data db.commit() cursor.close()
def dec_str(self): return angles.fmt_angle(self.dec_deg, ":", ":")
def ra_str(self): return angles.fmt_angle(self.ra_hrs, ":", ":").lstrip('+-')
def format_hours(value): string_value = angles.fmt_angle(value, 'h', 'm', 's') if string_value[0] == '+': string_value = string_value[1:] return string_value
def format_degrees(value): string_value = angles.fmt_angle(value, ':', ':', '') if string_value[0] == '+': string_value = string_value[1:] return string_value
def write_ascii_file(self, dirname, outname): """ Write ascii file containing astronomy data """ # need the current time to update coordiantes now = self.utc print "File %4d: %s (%d)" % (self.writecount, outname, self.count) fullname = dirname + outname outfile = open(fullname, 'w') outfile.write('# File: ' + outname + '\n') gainstr = '' ngains = len(self.gains) for iii in range(ngains-1): gainstr = gainstr + str(self.gains[iii]) + '; ' gainstr = gainstr + str(self.gains[ngains-1]) self.noteA = self.noteA.replace('\n', '') self.noteA = self.noteA.strip() outline = '# NOTEA = ' + self.noteA + '\n' outfile.write(outline) self.noteB = self.noteB.replace('\n', '') self.noteB = self.noteB.strip() outline = '# NOTEB = ' + self.noteB + '\n' outfile.write(outline) self.observer = self.observer.replace('\n', '') self.observer = self.observer.strip() outline = '# OBSERVER = ' + self.observer + '\n' outfile.write(outline) self.device = self.device.replace('\n', '') self.device = self.device.strip() outline = '# DEVICE = ' + self.device + '\n' outfile.write(outline) self.datadir = self.datadir.replace('\n', '') self.datadir = self.datadir.strip() outline = '# DATADIR = ' + self.datadir + '\n' outfile.write(outline) self.site = self.site.replace('\n', '') self.site = self.site.strip() outline = '# SITE = ' + self.site + '\n' outfile.write(outline) self.city = self.city.replace('\n', '') self.city = self.city.strip() outline = '# CITY = ' + self.city + '\n' outfile.write(outline) self.region = self.region.replace('\n', '') self.region = self.region.strip() outline = '# REGION = ' + self.region + '\n' outfile.write(outline) self.country = self.country.replace('\n', '') self.country = self.country.strip() outline = '# COUNTRY = ' + self.country + '\n' outfile.write(outline) self.telType = self.telType.replace('\n', '') self.telType = self.telType.strip() outline = '# TELTYPE = ' + self.telType + '\n' outfile.write(outline) self.frame = self.frame.replace('\n', '') self.frame = self.frame.strip() outline = '# FRAME = ' + self.frame + '\n' outfile.write(outline) outline = '# GAINS = ' + gainstr + '\n' outfile.write(outline) ngains = len(self.gains) if ngains > 0: outline = '# GAIN1 = ' + str(self.gains[0]) + '\n' outfile.write(outline) if ngains > 1: outline = '# GAIN2 = ' + str(self.gains[1]) + '\n' outfile.write(outline) if ngains > 2: outline = '# GAIN3 = ' + str(self.gains[2]) + '\n' outfile.write(outline) if ngains > 3: outline = '# GAIN4 = ' + str(self.gains[3]) + '\n' outfile.write(outline) outline = '# Count = ' + str(self.count) + '\n' outfile.write(outline) outline = '# CenterFreq= ' + str(self.centerFreqHz) + '\n' outfile.write(outline) outline = '# Bandwidth = ' + str(self.bandwidthHz) + '\n' outfile.write(outline) outline = '# Duration = ' + str(self.durationSec) + '\n' outfile.write(outline) outline = '# DeltaX = ' + str(self.deltaFreq) + '\n' outfile.write(outline) outline = '# BUNIT = ' + str(self.bunit).strip() + '\n' outfile.write(outline) nChan = len(self.ydataA) outline = '# NCHAN = ' + str(nChan) + '\n' outfile.write(outline) nSpec = self.nSpec outline = '# NSPEC = ' + str(nSpec) + '\n' outfile.write(outline) nave = self.nave outline = '# NAVE = ' + str(nave) + '\n' outfile.write(outline) nmedian = self.nmedian outline = '# NMEDIAN = ' + str(nmedian) + '\n' outfile.write(outline) outline = '# Fft_rate = ' + str(self.fft_rate) + '\n' outfile.write(outline) strnow = now.isoformat() dates = strnow.split('T') datestr = dates[0] + ' ' + dates[1] outline = '# UTC = ' + datestr + '\n' outfile.write(outline) lststr = angles.fmt_angle(self.lst/15., s1=":", s2=":", pre=3) # convert to hours outline = '# LST = ' + lststr[1:] + '\n' outfile.write(outline) outline = '# AZ = ' + str(self.telaz) + '\n' outfile.write(outline) outline = '# EL = ' + str(self.telel) + '\n' outfile.write(outline) anglestr = angles.fmt_angle(float(self.tellon), s1=":", s2=":") outline = '# TELLON = ' + anglestr + '\n' outfile.write(outline) anglestr = angles.fmt_angle(float(self.tellat), s1=":", s2=":") outline = '# TELLAT = ' + anglestr + '\n' outfile.write(outline) rastr = angles.fmt_angle(self.ra/15., s1=":", s2=":", pre=3) # convert to hours outline = '# RA = ' + rastr[1:] + '\n' outfile.write(outline) decstr = angles.fmt_angle(self.dec, s1=":", s2=":") outline = '# DEC = ' + decstr + '\n' outfile.write(outline) lonstr = angles.fmt_angle(self.gallon, s1=":", s2=":", pre=2) outline = '# GALLON = ' + lonstr[1:] + '\n' outfile.write(outline) latstr = angles.fmt_angle(self.gallat, s1=":", s2=":", pre=2) outline = '# GALLAT = ' + latstr + '\n' outfile.write(outline) altstr = angles.fmt_angle(self.altsun, s1=":", s2=":", pre=1) outline = '# ALT_SUN = ' + altstr + '\n' outfile.write(outline) az_str = angles.fmt_angle(self.az_sun, s1=":", s2=":", pre=1) outline = '# AZ_SUN = ' + az_str + '\n' outfile.write(outline) outline = '# ETAA = ' + str(self.etaA) + '\n' outfile.write(outline) outline = '# ETAB = ' + str(self.etaB) + '\n' outfile.write(outline) outline = '# POLANGLE = ' + str(self.polAngle) + '\n' outfile.write(outline) outline = '# TELSIZEAM = ' + str(self.telSizeAm) + '\n' outfile.write(outline) outline = '# TELSIZEBM = ' + str(self.telSizeBm) + '\n' outfile.write(outline) outline = '# AST_VERS = ' + str("04.02") + '\n' outfile.write(outline) dx = self.bandwidthHz/float(self.nChan) x = self.centerFreqHz - (self.bandwidthHz/2.) + (dx/2.) yv = self.ydataA leny = len(yv) for i in range(min(self.nChan,leny)): outline = str(i).zfill(4) + ' ' + str(long(x)) + ' ' + str(yv[i]) + '\n' outfile.write(outline) x = x + dx del outline outfile.close()
def _write_fil(self, ibeam): """ Write calibrated data to .fil files. Method produces a separate .fil file for each beam index in `beams`. Resulting files contain header and observations data of shape (`self.nsamples`, `self.nbands`) Parameters ---------- ibeam : int Beam index """ header = { 'az_start': self.az, 'data_type': 1, 'fch1': self.fbands[-1], 'foff': -self.wbands[-1], 'ibeam': ibeam, 'machine_id': 10000, 'nbeams': self.data.shape[1], 'nbits': 32, 'nchans': self.data.shape[2] - 1, # Excluding sum of channels 'nifs': 1, 'nsamples': self.data.shape[0], 'rawdatafile': self.filename, # Declination for central frequency channel 'src_dej': fmt_angle(DeltaAngle(r=np.median(self.dej[ibeam])).d, s1='', s2='', s3='', pre=2), # RA at the beginning of the observation, central freq channel 'src_raj': fmt_angle(AlphaAngle(r=np.median(self.ra[0, ibeam])).h, s1='', s2='', s3='', pre=2), 'za_start': np.median(self.za[ibeam]), 'telescope_id': 10000, 'tsamp': self.resolution, 'tstart': self.mjd[0] } path = self.PATH_OUTPUT \ + f"{self.filename.split('.')[0]}beam{ibeam}.fil" beam = self.data[:, ibeam, :-1] logger.info(f'Writing {beam.shape[1]} frequency bands ' f'of {beam.shape[0]} samples to {path}') sigproc_write(path, header, np.flip(beam, 1))
def write_ascii_file(self, dirname, outname): """ Write ascii file containing astronomy data """ # need the current time to update coordiantes now = self.utc print "File %4d: %s (%d)" % (self.writecount, outname, self.count) fullname = dirname + outname outfile = open(fullname, 'w') outfile.write('# File: ' + outname + '\n') if len(str(self.noteA)) > 1: outfile.write('# ' + str(self.noteA) + '\n') if len(str(self.noteB)) > 1: outfile.write('# ' + str(self.noteB) + '\n') gainstr = '' ngains = len(self.gains) for iii in range(ngains-1): gainstr = gainstr + str(self.gains[iii]) + '; ' gainstr = gainstr + str(self.gains[ngains-1]) outline = '# GAINS = ' + gainstr + '\n' outfile.write(outline) outline = '# Count = ' + str(self.count) + '\n' outfile.write(outline) outline = '# CenterFreq= ' + str(self.centerFreqHz) + '\n' outfile.write(outline) outline = '# Bandwidth = ' + str(self.bandwidthHz) + '\n' outfile.write(outline) outline = '# Duration = ' + str(self.durationSec) + '\n' outfile.write(outline) outline = '# DeltaX = ' + str(self.deltaFreq) + '\n' outfile.write(outline) nChan = len(self.ydataA) outline = '# NCHAN = ' + str(nChan) + '\n' outfile.write(outline) strnow = now.isoformat() dates = strnow.split('T') datestr = dates[0] + ' ' + dates[1] outline = '# UTC = ' + datestr + '\n' outfile.write(outline) lststr = angles.fmt_angle(self.lst/15., s1=":", s2=":", pre=3) # convert to hours outline = '# LST = ' + lststr[1:] + '\n' outfile.write(outline) outline = '# AZ = ' + str(self.telaz) + '\n' outfile.write(outline) outline = '# EL = ' + str(self.telel) + '\n' outfile.write(outline) anglestr = angles.fmt_angle(float(self.tellon), s1=":", s2=":") outline = '# TELLON = ' + anglestr + '\n' outfile.write(outline) anglestr = angles.fmt_angle(float(self.tellat), s1=":", s2=":") outline = '# TELLAT = ' + anglestr + '\n' outfile.write(outline) rastr = angles.fmt_angle(self.ra/15., s1=":", s2=":", pre=3) # convert to hours outline = '# RA = ' + rastr[1:] + '\n' outfile.write(outline) decstr = angles.fmt_angle(self.dec, s1=":", s2=":") outline = '# DEC = ' + decstr + '\n' outfile.write(outline) lonstr = angles.fmt_angle(self.gallon, s1=":", s2=":", pre=2) outline = '# GALLON = ' + lonstr[1:] + '\n' outfile.write(outline) latstr = angles.fmt_angle(self.gallat, s1=":", s2=":", pre=2) outline = '# GALLAT = ' + latstr + '\n' outfile.write(outline) altstr = angles.fmt_angle(self.altsun, s1=":", s2=":", pre=1) outline = '# ALT_SUN = ' + altstr + '\n' outfile.write(outline) az_str = angles.fmt_angle(self.az_sun, s1=":", s2=":", pre=1) outline = '# AZ_SUN = ' + az_str + '\n' outfile.write(outline) outline = '# AST_VERS = ' + str("02.01") + '\n' outfile.write(outline) dx = self.bandwidthHz/float(self.nChan) x = self.centerFreqHz - (self.bandwidthHz/2.) + (dx/2.) yv = self.ydataA leny = len(yv) for i in range(min(self.nChan,leny)): outline = str(i).zfill(4) + ' ' + str(long(x)) + ' ' + str(yv[i]) + '\n' outfile.write(outline) x = x + dx del outline outfile.close()