def channelAverage (out, naver, slop=DEFAULT_SLOP, banner=DEFAULT_BANNER, args=['undefined']): """Read data from the uvdat subsystem and channel average into an output dataset. out: dataset handle; the output dataset to be created naver: int; the number of channels to average together (see task docs) slop: float; tolerance for partially flagged bins (see task docs) banner: string; a message to write into the output's history args: list of strings; command-line arguments to write into the output's history, not including the program name (i.e. no traditional argv[0]) returns: None Contrast with channelAverageWithSetup, which sets up the uvdat subsytem itself rather than assuming that it's been initialized. """ if naver < 1: raise ValueError ('must average at least one channel (got naver=%d)' % naver) if slop < 0 or slop > 1: raise ValueError ('slop must be between 0 and 1 (got slop=%f)' % slop) try: _channelAverage (uvdat.read (), out, naver, slop, banner, args) except _CreateFailedError, e: # Don't delete the existing dataset! raise e.subexc
def process (self): byPol = {} tMin = tMax = None for inp, preamble, data, flags in uvdat.read (): if not flags.any (): continue pol = inp.getPol () variance = inp.getVariance () t = preamble[3] # Separation into intervals -- time to flush? if tMin is None: tMin = tMax = t elif t - tMin > self.interval or tMax - t > self.interval: self.flush (tMin, tMax, byPol) byPol = {} tMin = tMax = t else: tMin = min (tMin, t) tMax = max (tMax, t) # Accumulation if self.doOffset: if self.lmn is None: self.prepOffset (inp, data.size) ph0 = (0-2j) * np.pi * np.dot (self.lmn, preamble[0:3]) data *= np.exp (ph0 * self.phscale) if pol in byPol: ddata, idata = byPol[pol] else: ddata = np.zeros (5, dtype=np.double) idata = np.zeros (1, dtype=np.int) byPol[pol] = ddata, idata w = np.where (flags)[0] ngood = w.size data = data[w] wt = 1 / variance ddata[D_REAL] += data.real.sum () * wt ddata[D_IMAG] += data.imag.sum () * wt ddata[D_VAR] += variance * ngood ddata[D_AMP2] += (data.real**2 + data.imag**2).sum () ddata[D_TOTWT] += wt * ngood idata[I_COUNT] += ngood if tMin is not None: self.flush (tMin, tMax, byPol) byPol = {}
def process(self): byPol = {} tMin = tMax = None for inp, preamble, data, flags in uvdat.read(): if not flags.any(): continue pol = inp.getPol() variance = inp.getVariance() t = preamble[3] # Separation into intervals -- time to flush? if tMin is None: tMin = tMax = t elif t - tMin > self.interval or tMax - t > self.interval: self.flush(tMin, tMax, byPol) byPol = {} tMin = tMax = t else: tMin = min(tMin, t) tMax = max(tMax, t) # Accumulation if self.doOffset: if self.lmn is None: self.prepOffset(inp, data.size) ph0 = (0 - 2j) * np.pi * np.dot(self.lmn, preamble[0:3]) data *= np.exp(ph0 * self.phscale) if pol in byPol: ddata, idata = byPol[pol] else: ddata = np.zeros(5, dtype=np.double) idata = np.zeros(1, dtype=np.int) byPol[pol] = ddata, idata w = np.where(flags)[0] ngood = w.size data = data[w] wt = 1 / variance ddata[D_REAL] += data.real.sum() * wt ddata[D_IMAG] += data.imag.sum() * wt ddata[D_VAR] += variance * ngood ddata[D_AMP2] += (data.real**2 + data.imag**2).sum() ddata[D_TOTWT] += wt * ngood idata[I_COUNT] += ngood if tMin is not None: self.flush(tMin, tMax, byPol) byPol = {}
def readUVDat(self): self._read(uvdat.read())
def readUVDat (self): self._read (uvdat.read ())
def task (args): banner = util.printBannerGit ('atabpass', 'correct for ATA digital filter bandpass', IDENT) ks = keys.KeySpec () ks.keyword ('out', 'f', ' ') ks.uvdat ('ds3', False) opts = ks.process (args) if opts.out == ' ': print >>sys.stderr, 'Error: must give an output filename' sys.exit (1) bpass = getData () # Multifile UV copying algorithm copied from uvdat.for; # implementation copied from fxcal.py dOut = miriad.VisData (opts.out).open ('c') dOut.setPreambleType ('uvw', 'time', 'baseline') first = True curFile = None saveNPol = 0 polsVaried = False windowVars = ['ischan', 'nschan', 'nspect', 'restfreq', 'sdf', 'sfreq', 'systemp', 'xtsys', 'ytsys'] for dIn, preamble, data, flags in uvdat.read (): anyChange = False if dIn is not curFile: # Started reading a new file (or the first file) corrType, corrLen, corrUpd = dIn.probeVar ('corr') if corrType != 'r' and corrType != 'j' and corrType != 'c': raise Exception ('No channels to copy') if first: # This is NOT a close approximation to uvcat.for # We don't use an 'init' var since we assume dochan=True. dOut.setCorrelationType (corrType) dIn.initVarsAsInput (' ') # what does ' ' signify? dOut.initVarsAsOutput (dIn, ' ') uvt = dIn.makeVarTracker () uvt.track (*windowVars) tup = dIn.probeVar ('npol') doPol = tup is not None and (tup[0] == 'i') nPol = 0 doneNPol = False curFile = dIn anyChange = True if first: # If very first file, copy the history entry. dIn.copyItem (dOut, 'history') first = False if nPol == 0: # We're on to a new set of baselines. Get the number # of pols in this next set and remind ourselves to # update the 'npol' variable if necessary. nPol = dIn.getNPol () doneNPol = False if uvt.updated (): nAnts = dIn.getVarInt ('nants') tbl = {} dIn.probeVar ('nspect') nSpec = dIn.getVarInt ('nspect') tbl['nspect'] = ('i', nSpec) for v in ['nschan', 'ischan']: dIn.probeVar (v) tbl[v] = ('i', dIn.getVarInt (v, nSpec)) for v in ['sdf', 'sfreq', 'restfreq']: dIn.probeVar (v) tbl[v] = ('d', dIn.getVarDouble (v, nSpec)) for v in ['systemp', 'xtsys', 'ytsys', 'xyphase']: tup = dIn.probeVar (v) if tup is not None and tup[0] == 'r': tbl[v] = ('r', dIn.getVarFloat (v, tup[1])) for (name, (code, val)) in tbl.iteritems (): if code == 'i': dOut.writeVarInt (name, val) elif code == 'r': dOut.writeVarFloat (name, val) elif code == 'd': dOut.writeVarDouble (name, val) else: assert (False) anyChange = True pol = dIn.getPol () if not doneNPol: # If necessary, write out a new value for the # 'npol' variable. If npol has changed, note # that so that we know not to write a # dataset-wide npol item. if nPol != saveNPol: dOut.writeVarInt ('npol', nPol) polsVaried = polsVaried or saveNPol != 0 saveNPol = nPol doneNPol = True dIn.copyLineVars (dOut) # Hey! We actually have some data processing to do! data /= bpass # That was fast. dOut.writeVarInt ('pol', pol) dOut.write (preamble, data, flags, flags.size) # Count down the number of polarizations left for this baseline. # When we reach zero, we may reset the npol variable. nPol -= 1 if not polsVaried: # Number of pols never varied, so it's valid to write out # a single 'npol' item for the entire dataset. dOut.setScalarItem ('npol', np.int32, saveNPol) # All done. Write history entry and quit. dOut.openHistory () dOut.writeHistory (banner) dOut.logInvocation ('ATABPASS') dOut.closeHistory () dOut.close () return 0