def read_cold(names, ave_cold, minel, minGLat, maxGlat): """ read_cold() reads all files and averages selected files with high elevation and galactic Latitude. Inputs: minel minimum elevation to accept for cold load minGLat minimum galactic latitude maxGlat maximum galactic latitude """ # flag starting a new sum of cold (high elevation and galactic latitude) obs ncold = 0 rs = radioastronomy.Spectrum() # now average coldest data for calibration for filename in names: rs.read_spec_ast(filename) rs.azel2radec() # compute ra,dec from az,el if rs.telel < lowel: #if elevation too low for a cold load obs continue # note this test excludes low galactic latitude ranges if rs.gallat > maxGlat or rs.gallat < minGlat: if ncold == 0: firstutc = rs.utc lastutc = rs.utc ave_cold, rs, ncold, firstutc, lastutc = \ average_spec( ave_cold, rs, ncold, firstutc, lastutc) # end of all files to average if ncold < 1: print("No high galactic latitude data: can not calibrate") exit() else: ave_cold, ncold = normalize_spec(ave_cold, ncold, firstutc, lastutc) if flagRfi: yv = ave_cold.ydataA xv = ave_cold.xdata * 1.E-6 cv = interpolate.lines(linelist, linewidth, xv, yv) # interpolate rfi ave_cold.ydataA = cv if doFold: ave_cold.foldfrequency() print("Found %3d High Galactic Latitude spectra" % (ncold)) return ave_cold, ncold
def read_hot(names, ave_hot): """ read_hot() reads in all files in the names list and averages hot load observations. The hot load file is returned. While reading the files, the minmum elevation and galactic latitudes are recorded """ rs = radioastronomy.Spectrum() nhot = 0 # init count of hot files minGlat = +90. maxGlat = -90. minel = 200. maxel = -200. # now run through and find hot and cold loads obs for filename in names: parts = filename.split('/') nparts = len(parts) if nparts == 1: aname = parts[0] else: aname = parts[nparts - 1] parts = aname.split('.') nparts = len(parts) if nparts < 2: print('File is not an astronomy file: ', filename) continue else: extension = parts[nparts - 1] extension = extension.upper() if (extension != 'HOT') and (extension != 'AST') and (extension != 'CLD'): print('Extension not recognized : ', parts[nparts - 1]) continue rs.read_spec_ast(filename) rs.azel2radec() # compute ra,dec from az,el # if a hot load observation if rs.telel < 0: if nhot == 0: firstutc = rs.utc lastutc = rs.utc # accumulate spectra ave_hot, rs, nhot, firstutc, lastutc = \ average_spec( ave_hot, rs, nhot, firstutc, lastutc) else: # else above horizon, find min, max galactic latitudes if rs.gallat > maxGlat: maxGlat = rs.gallat if rs.gallat < minGlat: minGlat = rs.gallat if minel > rs.telel: minel = rs.telel if maxel < rs.telel: maxel = rs.telel # end for all files if nhot > 0: print("Found %3d Hot load observations" % (nhot)) ave_hot, nhot = normalize_spec(ave_hot, nhot, firstutc, lastutc) else: print("No Hot load data, can not calibrate") exit() # do more cleanup on spectra for RFI if flagRfi: yv = copy.deepcopy(ave_hot.ydataA) xv = ave_hot.xdata * 1.E-6 hv = interpolate.lines(linelist, linewidth, xv, yv) # interpolate rfi ave_hot.ydataA = hv # now obsolete option to fold spectra, when a problem with SDR I-Q balance if doFold: ave_hot.foldfrequency() return ave_hot, minel, maxel, minGlat, maxGlat
def findzero( names, gain, vel, azoffset, eloffset, avetimesec): lastgain = 0. global nprint # flag start of search for a latitude zero crossing ncrossings = -1 crossingsum = 0. lastgallat = -100. lastsum = 0. nData = len(vel) minGlat = +90. maxGlat = -90. nhot = 0 # number of obs with el < 0 ncold = 0 nhigh = 0 # highest galactic latitude lastfreq = 0. lastbw = 0. lastgain = 0. lastel = 0. lastaz = 0. firstrun = True avetime = datetime.timedelta(seconds=avetimesec) firstgallon = -1. ncrossings = 0 nread = 0 # now read through all data and average cold sky obs for filename in names: parts = filename.split('/') nparts = len(parts) aname = parts[nparts-1] parts = aname.split('.') aname = parts[0] extension = parts[1] parts = aname.split('T') date = parts[0] time = parts[1] time = time.replace('_', ':') # put time back in normal hh:mm:ss format # exclude hot load data for averaging if extension == 'hot': continue rs = radioastronomy.Spectrum() # print filename rs.read_spec_ast(filename) rs.telaz = rs.telaz + azoffset rs.telel = rs.telel + eloffset rs.azel2radec() # compute ra,dec from az,el # if a sky observation and close to galactic plane if rs.telel > 0. and (rs.gallat > -5.0 and rs.gallat < 5.0) : # if first time reading data, set obs parameters if lastfreq == 0.: lastfreq = rs.centerFreqHz lastbw = rs.bandwidthHz lastgain = rs.gains[0] lastaz = rs.telaz lastel = rs.telel cold = copy.deepcopy( rs) ncold = 0 timesum = 0. firstutc = cold.utc lastutc = cold.utc newAzEl = (lastaz != rs.telaz) or (lastel != rs.telel) if newAzEl and nprint < 5: print 'Must keep az,el constant to find pointing offsets!' print 'Last %7.2f,%7.2f; New: %7.2f,%7.2f' % (lastaz, lastel, rs.telaz, rs.telel) lastaz = rs.telaz lastel = rs.telel nprint = nprint + 1 break if ncold > 1: # time difference is between mid-points of integrations dt = rs.utc - cold.utc # add the time since midpoint of latests dt = dt + datetime.timedelta(seconds=rs.durationSec) lastutc = rs.utc # if time to average (or end of all files) if (dt > avetime) or (filename == sys.argv[nargs-1]): cold.ydataA = cold.ydataA/float(timesum) # have complicated steps to simple get the average time. deltatime = endtime - starttime aveutc,duration = radioastronomy.aveutcs( firstutc, lastutc) cold.utc = aveutc cold.azel2radec() # recompute coordinates for average time ra = cold.ra dec = cold.dec gallat = cold.gallat gallon = cold.gallon if ncrossings < 0: lastgallat = gallat ncrossings = 0 crossingsum = 0. xv = cold.xdata * 1.E-6 yv = cold.ydataA * scalefactor yv = interpolate.lines( linelist, linewidth, xv, yv) # interpolate rfi xmin = min(xv) xmax = max(xv) count = cold.count note = cold.noteA #print('%s' % note) ncolor = min(nmax-1, nplot) tsky = np.zeros(nData) # initialize arrays for jjj in range (0, nData): tsky[jjj] = yv[jjj]/gain[jjj] imin, imax = velocity_to_indicies( vel, minvel, maxvel) ymed = np.median(tsky[imin:imax]) ya = np.median(tsky[(imin-10):(imin+10)]) yb = np.median(tsky[(imax-10):(imax+10)]) slope = (yb-ya)/(imax-imin) nFit = 20 baseline = fit_baseline( vel, tsky, imin, imax, nFit) tsky = tsky - baseline ymin = min(tsky[imin:imax]) ymax = max(tsky[imin:imax]) thesum = compute_sum( minvel, maxvel, ra, dec, gallon, gallat, vel, tsky, ncold) # finallly if this is a latitude crossing sum. if ((lastgallat < 0. and gallat > 0.) or (lastgallat > 0. and gallat < 0.)) and lastgallat > -100.: if abs(gallat) < 5.: # if first crossing, init sum if ncrossings == 0: ncrossings = 1 crossingsum = thesum # print 'Zero Latitude: %7.1f %7.1f: %10.0f, %10.0f' % (lastgallat, gallat, lastsum, thesum) else: # else average crossings ncrossings = ncrossings + 1 crossingsum = crossingsum + thesum if abs(lastgallat) < 5.: if ncrossings == 0: ncrossings = 1 crossingsum = lastsum # print 'Zero Latitude: %7.1f %7.1f: %10.0f, %10.0f' % (lastgallat, gallat, lastsum, thesum) else: # else average crossings ncrossings = ncrossings + 1 crossingsum = crossingsum + lastsum lastgallat = gallat lastsum = thesum # computation done, start sum over gain ncold = 0 # if this was a new obs; restart the sums if ncold == 0: cold = copy.deepcopy(rs) # initial spectrum is one just read starttime = cold.utc endtime = starttime ncold = 1 # sums are weighted by durations crossZero = False crossZeroLat = False crossZeroRa = False firstlon = rs.gallon # try to keep track of crossing zero in angular coordinates firstra = rs.ra # cold.ydataA = rs.ydataA * cold.durationSec # keep track of observing time for weighted sum timesum = rs.durationSec else: # else not enough time yet, average cold data # fix wrap of longitudes cold.count = cold.count + rs.count ncold = ncold + 1 cold.ydataA = cold.ydataA + (rs.ydataA * rs.durationSec) cold.ra = cold.ra + (rs.ra * cold.durationSec) cold.dec = cold.dec + (rs.dec * cold.durationSec) cold.gallon = cold.gallon + (rs.gallon * rs.durationSec) cold.gallat = cold.gallat + (rs.gallat * rs.durationSec) # keep track of observing time for weighted sum endtime = rs.utc timesum = timesum + rs.durationSec # end if not a enough time # end if a cold file #end for all files to sum if ncrossings > 0: crossingsum = crossingsum/float(ncrossings) return crossingsum
if (dt > avetime) or newObs or allFiles: if doDebug: medianData = np.median(ave_spec.ydataA[n6:n56]) print("Average duration: %7.1f, Median: %8.3f" % (ave_spec.durationSec, medianData)) # not calibrating hot load observations. if ave_spec.telel < 0.: # Reset the ncold count and restart sum nave = 0 continue ave_spec, nave = normalize_spec(ave_spec, nave, firstutc, lastutc) xv = ave_spec.xdata * 1.E-6 # covert to MHz yv = ave_spec.ydataA if flagRfi: yv = interpolate.lines(linelist, linewidth, xv, yv) # interpolate rfi xmin = min(xv) xmax = max(xv) xallmin = min(xmin, xallmin) xallmax = max(xmax, xallmax) count = ave_spec.count note = ave_spec.noteA #print('%s' % note) ncolor = min(nmax - 1, nplot) tsky, vel = compute_tsky_hotcold(xv, yv, hv, cv, thot, tcold) # get tsys from averages of ends of spectra tSys = np.median(tsky[n6:n56]) tStdA = np.std(tsky[n6:n26]) tStdB = np.std(tsky[n46:n56])
def findhotgain( names, avetimesec): lastgain = 0. # flag start of search for a latitude zero crossing ncrossings = -1 crossingsum = 0. lastgallat = -100. lastsum = 0. minGlat = +90. maxGlat = -90. nhot = 0 # number of obs with el < 0 ncold = 0 nhigh = 0 # highest galactic latitude lastfreq = 0. lastbw = 0. lastgain = 0. lastel = 0. lastaz = 0. firstrun = True for filename in names: rs = radioastronomy.Spectrum() rs.read_spec_ast(filename) rs.telel = rs.telel + eloffset rs.telaz = rs.telaz + azoffset rs.azel2radec() # compute ra,dec from az,el if rs.gains[0] != lastgain: if lastgain != 0: print 'Gain Change: ', lastgain, ' -> ', rs.gains[0] lastgain = rs.gains[0] if rs.telel < 0: if nhot == 0: hot = copy.deepcopy( rs) nhot = 1 else: hot.ydataA = hot.ydataA + rs.ydataA hot.count = hot.count + rs.count nhot = nhot + 1 else: # else above horizon, find min, max galactic latitudes if rs.gallat > maxGlat: maxGlat = rs.gallat if rs.gallat < minGlat: minGlat = rs.gallat if nhot > 0: hot.ydataA = scalefactor * hot.ydataA / float(nhot) print "Found %3d hot load obs" % nhot else: print "No hot load data, can not calibrate" exit() xv = hot.xdata * 1.E-6 yv = hot.ydataA nData = len(xv) hv = interpolate.lines( linelist, linewidth, xv, yv) # interpolate rfi print 'Min,Max Galactic Latitudes %7.1f,%7.1f (d)' % (minGlat, maxGlat) # assume only a limited range of galactic latitudes are available # not range about +/-60. use60Range = False # all galactic latitudes above +/-60d can be used if minGlat < -60. or maxGlat > 60.: minGlat = -60. maxGlat = 60. else: # else no high galactic latitude data # use highest galactic latitudes - +/-5.degrees if -minGlat > maxGlat: # if negative latitudes higher minGlat = minGlat + 5. maxGlat = 90. else: # else positive latitudes higher maxGlat = maxGlat - 5. minGlat = -90. outnames = names nout = 0 # now average coldest data for calibration for filename in names: rs = radioastronomy.Spectrum() rs.read_spec_ast(filename) rs.telaz = rs.telaz + azoffset rs.telel = rs.telel + eloffset rs.azel2radec() # compute ra,dec from az,el if rs.telel < 0: continue if rs.gallat > maxGlat or rs.gallat < minGlat: if nhigh == 0: high = copy.deepcopy( rs) high.ydataA = rs.ydataA nhigh = 1 else: high.ydataA = high.ydataA + rs.ydataA high.count = high.count + rs.count nhigh = nhigh + 1 else: # if here, then only these names will be used to find zero crossings outnames[nout] = filename nout = nout + 1 if nhigh < 1.: print "No high galactic latitude data: can not calibrate" exit() else: high.ydataA = scalefactor * high.ydataA/nhigh print "Found %d High Galactic Latidue spectra" % (nhigh) yv = high.ydataA cv = interpolate.lines( linelist, linewidth, xv, yv) # interpolate rfi # finally compute gain on a channel by chanel basis gain = np.zeros(nData) vel = np.zeros(nData) for iii in range(nData): gain[iii] = (hv[iii] - cv[iii])/(thot - tcold) vel[iii] = c * (nuh1 - xv[iii])/nuh1 # now interpolate over galactic velocities gain = interpolate_range( minvel, maxvel, vel, gain) if nout > 0: outnames = outnames[0:(nout-1)] else: print 'No low latitude file names left!' exit() return gain, vel, outnames