def shift_time(told, target): ''' This is for calculating a time shift for a given schedule, to bring it to the current date. When editing a .scd file with calibrators, enter the first time as a Time() object told, and enter the current date/time as a Time() object target. Returns a new Time() object with the date/time that the schedule should be adjusted to. There is a potential for the time to be off by four minutes (one day's sidereal lag). ''' from astropy.time import TimeDelta dt = TimeDelta((util.lobe(eovsa_lst(target) - eovsa_lst(told)) * 0.158720), format='jd') print dt return target - dt
def rd_calpnt(filename): ''' Read and return contents of output of CALPNT or CALPNT2M observation Note that the "x" offsets are dRA and dAZ. To apply these in subsequent routines, dRA is converted to dHA by inverting the sign, while dAZ is converted to dXEL (angle on the sky) by multiplying by cos(EL). ''' import dbutil f = open(filename, 'r') lines = f.readlines() f.close() ants = lines[0].split('Ant ')[1:] antlist = [] try: for ant in ants: antlist.append(int(ant[:2])) except: print 'Unrecognized format (header line) for', filename return None nants = len(ants) lines = lines[1:] nlines = len(lines) dra = np.zeros((nants, nlines), np.float) ddec = np.zeros((nants, nlines), np.float) ha = [] dec = [] source = [] timstr = [] for i, line in enumerate(lines): if len(line) < 9: dra = dra[:, :i] ddec = ddec[:, :i] break vals = line[9:].split() if len(vals) != nants * 2 + 4: print 'Error reading line', i + 2, 'of', filename dra = dra[:, :i] ddec = ddec[:, :i] break else: try: source.append(line[:8]) timstr.append(vals[0] + ' ' + vals[1]) ha.append(vals[2]) dec.append(vals[3]) dra[:, i] = np.array(vals[4::2]).astype(float) ddec[:, i] = np.array(vals[5::2]).astype(float) except: print 'Error parsing line', i + 2, 'of', filename # Convert HA, Dec from degrees to radians, and then convert HA to RA ha = np.array(ha).astype(float) * np.pi / 180. dec = np.array(dec).astype(float) * np.pi / 180. times = Time(timstr) ra = np.zeros_like(ha) for i in range(len(ha)): ra[i] = eovsa_lst(times[i]) - ha[i] # Read pointing parameters from SQL database at time of first observation params_old = np.zeros((9, 15), int) cursor = dbutil.get_cursor() timestamp = times[0].lv # Read stateframe data at time of first observation D15data = dbutil.get_dbrecs(cursor, dimension=15, timestamp=timestamp, nrecs=1) for p in range(9): params_old[p], = D15data['Ante_Cont_PointingCoefficient' + str(p + 1)] params_old = params_old[:, np.array(antlist) - 1] # Pare down to only antennas in antlist return { 'filename': filename, 'source': source, 'time': times, 'params_old': params_old, 'ra': ra, 'dec': dec, 'ha': ha, 'antlist': antlist, 'dra': dra, 'ddec': ddec }
def idb_readXdata(filename, filter=False): '''This routine reads the data from a single IDBfile. Optional Keywords: filter boolean--if True, returns only non-zero frequencies if False (default), returns all frequencies. This differs from Dale's version in that it includes all correlations, drops the tp_only option, and the outputs that are not in the UDB files. ''' # Open uv file for reading uv = aipy.miriad.UV(filename) nf_orig = len(uv['sfreq']) good_idx = np.arange(nf_orig) if filter: good_idx = [] # Read a bunch of records to get number of good frequencies, # i.e. those with at least some non-zero data. Read 20 # records for baseline 1-2, XX pol uv.select('antennae',0,2,include=True) uv.select('polarization',-5,-5,include=True) for i in range(20): preamble, data = uv.read() idx, = data.nonzero() if len(idx) > len(good_idx): good_idx = copy.copy(idx) uv.select('clear',0,0) uv.rewind() #endif #set up outputs nf = len(good_idx) print 'NF: ', nf freq = uv['sfreq'][good_idx] npol = uv['npol'] polarr = np.array([-5, -6, -7, -8]) nants = uv['nants'] nbl = nants*(nants-1)/2 nblc = nbl+nants # all-correlations, add a mask for the output vis array outx0 = np.zeros((nf, nblc, npol, 600),dtype=np.complex64) omask = np.zeros((nf, nblc, npol, 600),dtype=np.int32) outx = ma.masked_array(outx0, mask = omask) i0array = np.zeros((nblc, 600), dtype = np.int32) j0array = np.zeros((nblc, 600), dtype = np.int32) outpx = np.zeros((nf*nants, 600), dtype=np.float) outpy = np.zeros((nf*nants, 600), dtype=np.float) uvwarray = np.zeros((3, nblc, 600), dtype=np.float) delayarray = np.zeros((nants, 600), dtype=np.float) #lists for time arrays utarray = [] timearray = [] lstarray = [] l = -1 tprev = 0 tsav = 0 # Use antennalist if available if 'antlist' in uv.vartable: ants = strip_non_printable(uv['antlist']) antlist = map(int, ants.split()) else: antlist = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16] #endelse #keep autocorrelations in the array bl2ord = p.bl_list() for ij in range(len(antlist)): bl2ord[ij, ij] = nbl+ij #endfor for preamble, data in uv.all(): uvw, t, (i0,j0) = preamble i = antlist.index(i0+1) j = antlist.index(j0+1) if i > j: # Reverse order of indices j = antlist.index(i0+1) i = antlist.index(j0+1) #endif # Assumes uv['pol'] is one of -5, -6, -7, -8 k = -5 - uv['pol'] if filter: if len(data.nonzero()[0]) == nf: if t != tprev: # New time l += 1 if l == 600: break #endif tprev = t timearray.append(t) utarray.append(uv['ut']) try: lstarray.append(uv['lst']) except: pass #endexcept xdata0 = uv['xsampler'].reshape(nf_orig,nants,3) xdata = xdata0[good_idx, :, 0] outpx[:,l] = xdata.reshape(nf*nants) ydata0 = uv['ysampler'].reshape(nf_orig,nants,3) ydata = ydata0[good_idx, :, 0] outpy[:,l] = ydata.reshape(nf*nants) delayarray[:,l] = uv['delay'] #endif outx[:, bl2ord[i,j],k,l] = data[data.nonzero()] if k == 3: uvwarray[:, bl2ord[i,j],l] = uvw i0array[bl2ord[i,j],l] = i0 j0array[bl2ord[i,j],l] = j0 #endif #endif, for len(data.nonzero()) == nf else: if t != tprev: # New time l += 1 if l == 600: break tprev = t timearray.append(t) utarray.append(uv['ut']) try: lstarray.append(uv['lst']) except: pass #endexcept xdata = uv['xsampler'].reshape(nf,nants,3) outpx[:,l] = xdata[:, :, 0].reshape(nf*nants) ydata = uv['ysampler'].reshape(nf,nants,3) outpy[:,l] = ydata[:, :, 0].reshape(nf*nants) delayarray[:,l] = uv['delay'] #endif outx[:,bl2ord[i,j],k,l] = data if k == 3: uvwarray[:,bl2ord[i,j],l] = uvw i0array[bl2ord[i,j],l] = i0 j0array[bl2ord[i,j],l] = j0 #endif #endelse (not filter) #endfor # Truncate in case of early end of data nt = len(timearray) outpx = outpx[:,:nt] outpy = outpy[:,:nt] outx = outx[:,:,:,:nt] uvwarray = uvwarray[:, :, :nt] delayarray = delayarray[:, :nt] if len(lstarray) != 0: pass else: tarray = Time(timearray,format='jd') for t in tarray: lstarray.append(el.eovsa_lst(t)) #endfor #endelse #i0 and j0 should always be the same i0array = i0array[:,0] j0array = j0array[:,0] #timearray, lstarray and utarray are lists out = {'x':outx,'uvw':uvwarray,'time':np.array(timearray),'px':outpx,'py':outpy,'i0':i0array,'j0':j0array,'lst':np.array(lstarray),'pol':polarr,'delay':delayarray,'ut':np.array(utarray),'file0':filename} return out
def readXdata(filename): '''This routine reads the data from a single UDB file. ''' # Open uv file for reading uv = aipy.miriad.UV(filename) # Read all to get a time array utcount = 0 ut = 0.0 for preamble, data in uv.all(): # Look for time change if preamble[1] != ut: ut = preamble[1] utcount = utcount+1 #endif #endfor uv.rewind() #set up outputs src = uv['source'] sfreq = uv['sfreq'] nf = len(sfreq) sdf = uv['sdf'] npol = uv['npol'] polarr = np.array([-5, -6, -7, -8]) nants = uv['nants'] nbl = nants*(nants-1)/2 nblc = nants+nbl # all-correlations, add a mask for the output vis array outx0 = np.zeros((nf, nblc, npol, utcount),dtype=np.complex64) omask = np.zeros((nf, nblc, npol, utcount),dtype=np.int32) outx = ma.masked_array(outx0, mask = omask) i0array = np.zeros((nblc, utcount), dtype = np.int32) j0array = np.zeros((nblc, utcount), dtype = np.int32) outpx = np.zeros((nants*nf, utcount), dtype=np.float) outpy = np.zeros((nants*nf, utcount), dtype=np.float) uvwarray = np.zeros((3, nblc, utcount), dtype=np.float) delayarray = np.zeros((nants, utcount), dtype=np.float) utarray = [] timearray = [] lstarray = [] l = -1 tprev = 0 tsav = 0 # Use antennalist if available if 'antlist' in uv.vartable: ants = strip_non_printable(uv['antlist']) antlist = map(int, ants.split()) else: antlist = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16] #endelse #keep autocorrelations in the array bl2ord = p.bl_list() for ij in range(len(antlist)): bl2ord[ij, ij] = nbl+ij #endfor xxx = 0 for preamble, data in uv.all(): uvw, t, (i0,j0) = preamble i = antlist.index(i0+1) j = antlist.index(j0+1) if i > j: #this should not happen post-2016 # Reverse order of indices j = antlist.index(i0+1) i = antlist.index(j0+1) # Assumes uv['pol'] is one of -5, -6, -7, -8 # Endif k = -5 - uv['pol'] if t != tprev: # New time l += 1 if l == utcount: break #endif tprev = t try: lstarray.append(uv['lst']) except: pass timearray.append(t) utarray.append(uv['ut']) xdata = uv['xtsys'] ydata = uv['ytsys'] outpx[:, l] = xdata outpy[:, l] = ydata delayarray[:, l] = uv['delay'] #endif outx[:,bl2ord[i,j],k,l] = data #apply mask outx[:,bl2ord[i,j],k,l].mask = data.mask if k == 3: uvwarray[:, bl2ord[i,j],l] = uvw i0array[bl2ord[i,j],l] = i0 j0array[bl2ord[i,j],l] = j0 #endif #endfor if len(lstarray) != 0: pass else: tarray = Time(timearray,format='jd') for t in tarray: lstarray.append(el.eovsa_lst(t)) #endfor #endelse #i0 and j0 should always be the same i0array = i0array[:,0] j0array = j0array[:,0] #timearray, lstarray and utarray are lists out = {'x':outx,'uvw':uvwarray,'time':np.array(timearray),'px':outpx,'py':outpy,'i0':i0array,'j0':j0array,'lst':np.array(lstarray),'pol':polarr,'delay':delayarray,'ut':np.array(utarray),'file0':filename} return out