def getresult( self ): """ Get result as scantable instance. Returned scantable is copy of input scantable except for that some data are marked as OFF as a result of edge detection and marking. """ return scantable( self.marker._get() )
def splitant(filename, outprefix='',overwrite=False, getpt=True): """ Split Measurement set by antenna name, save data as a scantables, and return a list of filename. Note that frequency reference frame is imported as it is in Measurement set. Notice this method can only be available from CASA. Prameter filename: the name of Measurement set to be read. outprefix: the prefix of output scantable name. the names of output scantable will be outprefix.antenna1, outprefix.antenna2, .... If not specified, outprefix = filename is assumed. overwrite If the file should be overwritten if it exists. The default False is to return with warning without writing the output. USE WITH CARE. getpt Whether to import direction from MS/POINTING table or not. Default is True (import direction). """ # Import the table toolkit from CASA from taskinit import gentools from asap.scantable import is_ms tb = gentools(['tb'])[0] # Check the input filename if isinstance(filename, str): import os.path filename = os.path.expandvars(filename) filename = os.path.expanduser(filename) if not os.path.exists(filename): s = "File '%s' not found." % (filename) raise IOError(s) # check if input file is MS if not is_ms(filename): s = "File '%s' is not a Measurement set." % (filename) raise IOError(s) else: s = "The filename should be string. " raise TypeError(s) # Check out put file name outname='' if len(outprefix) > 0: prefix=outprefix+'.' else: prefix=filename.rstrip('/') # Now do the actual splitting. outfiles=[] tb.open(tablename=filename,nomodify=True) ant1=tb.getcol('ANTENNA1',0,-1,1) anttab=tb.getkeyword('ANTENNA').lstrip('Table: ') tb.close() tb.open(tablename=anttab,nomodify=True) nant=tb.nrows() antnames=tb.getcol('NAME',0,nant,1) tb.close() for antid in set(ant1): scan=scantable(filename,average=False,antenna=int(antid),getpt=getpt) outname=prefix+antnames[antid]+'.asap' scan.save(outname,format='ASAP',overwrite=overwrite) del scan outfiles.append(outname) return outfiles
def _import_data(data): if not isinstance(data, (list,tuple)): if isinstance(data, scantable): return data elif isinstance(data, str): return scantable(data) tables = [] for d in data: if isinstance(d, scantable): tables.append(d) elif isinstance(d, str): if os.path.exists(d): tables.append(scantable(d)) else: raise IOError("Data file doesn't exists") else: raise TypeError("data is not a scantable or valid file") return merge(tables)
def _import_data(data): if not isinstance(data, (list, tuple)): if isinstance(data, scantable): return data elif isinstance(data, str): return scantable(data) tables = [] for d in data: if isinstance(d, scantable): tables.append(d) elif isinstance(d, str): if os.path.exists(d): tables.append(scantable(d)) else: raise IOError("Data file doesn't exists") else: raise TypeError("data is not a scantable or valid file") return merge(tables)
def __chanIndex(self, idx): s = scantable(self.outfile, average=False) nrow = s.nrow() spectra = numpy.zeros((self.npol, nrow / self.npol), dtype=float) irow = 0 sp = [0 for i in xrange(self.nchan)] for i in xrange(nrow / self.npol): for ip in xrange(self.npol): sp = s._getspectrum(irow) spectra[ip, i] = sp[idx] irow += 1 return spectra
def __chanIndex( self, idx ): s = scantable( self.outfile, average=False ) nrow = s.nrow() spectra = numpy.zeros( (self.npol,nrow/self.npol), dtype=float ) irow = 0 sp = [0 for i in xrange(self.nchan)] for i in xrange(nrow/self.npol): for ip in xrange(self.npol): sp = s._getspectrum( irow ) spectra[ip,i] = sp[idx] irow += 1 return spectra
def createTableIn(self, tab): del self.tablein self.tablein = scantable(tab, average=False) if self.ifno < 0: ifno = self.tablein.getif(0) #print 'ifno=',ifno else: ifno = self.ifno sel = selector() sel.set_ifs(ifno) self.tablein.set_selection(sel) self.nchan = len(self.tablein._getspectrum(0)) self.nrow = self.tablein.nrow() del sel
def _array2dOp(scan, value, mode="ADD", tsys=False, insitu=None, skip_flaggedrow=False): """ This function is workaround on the basic operation of scantable with 2 dimensional float list. scan: scantable operand value: float list operand mode: operation mode (ADD, SUB, MUL, DIV) tsys: if True, operate tsys as well insitu: if False, a new scantable is returned. Otherwise, the array operation is done in-sitsu. skip_flaggedrow: skip operation for row-flagged spectra. """ if insitu is None: insitu = rcParams['insitu'] nrow = scan.nrow() s = None from asap._asap import stmath stm = stmath() stm._setinsitu(insitu) if len(value) == 1: s = scantable(stm._arrayop(scan, value[0], mode, tsys, skip_flaggedrow)) elif len(value) != nrow: raise ValueError('len(value) must be 1 or conform to scan.nrow()') else: from asap._asap import stmath if not insitu: s = scan.copy() else: s = scan # insitu must be True as we go row by row on the same data stm._setinsitu(True) basesel = s.get_selection() # generate a new selector object based on basesel sel = selector(basesel) for irow in range(nrow): sel.set_rows(irow) s.set_selection(sel) if len(value[irow]) == 1: stm._unaryop(s, value[irow][0], mode, tsys, skip_flaggedrow) else: #stm._arrayop( s, value[irow], mode, tsys, 'channel' ) stm._arrayop(s, value[irow], mode, tsys, skip_flaggedrow) s.set_selection(basesel) return s
def createTableIn( self, tab ): del self.tablein self.tablein = scantable( tab, average=False ) if self.ifno < 0: ifno = self.tablein.getif(0) #print 'ifno=',ifno else: ifno = self.ifno sel = selector() sel.set_ifs( ifno ) self.tablein.set_selection( sel ) self.nchan = len(self.tablein._getspectrum(0)) self.nrow = self.tablein.nrow() del sel
def dototalpower(calon, caloff, tcalval=0.0): """ Do calibration for CAL on,off signals. Adopted from GBTIDL dototalpower Parameters: calon: the 'cal on' subintegration caloff: the 'cal off' subintegration tcalval: user supplied Tcal value """ varlist = vars() from asap._asap import stmath stm = stmath() stm._setinsitu(False) s = scantable(stm._dototalpower(calon, caloff, tcalval)) s._add_history("dototalpower",varlist) return s
def dototalpower(calon, caloff, tcalval=0.0): """ Do calibration for CAL on,off signals. Adopted from GBTIDL dototalpower Parameters: calon: the 'cal on' subintegration caloff: the 'cal off' subintegration tcalval: user supplied Tcal value """ varlist = vars() from asap._asap import stmath stm = stmath() stm._setinsitu(False) s = scantable(stm._dototalpower(calon, caloff, tcalval)) s._add_history("dototalpower", varlist) return s
def __chanAverage( self, start=-1, end=-1 ): s = scantable( self.outfile, average=False ) nrow = s.nrow() spectra = numpy.zeros( (self.npol,nrow/self.npol), dtype=float ) irow = 0 sp = [0 for i in xrange(self.nchan)] if start < 0: start = 0 if end < 0: end = self.nchan for i in xrange(nrow/self.npol): for ip in xrange(self.npol): sp = s._getspectrum( irow )[start:end] spectra[ip,i] = numpy.mean( sp ) irow += 1 return spectra
def __chanAverage(self, start=-1, end=-1): s = scantable(self.outfile, average=False) nrow = s.nrow() spectra = numpy.zeros((self.npol, nrow / self.npol), dtype=float) irow = 0 sp = [0 for i in xrange(self.nchan)] if start < 0: start = 0 if end < 0: end = self.nchan for i in xrange(nrow / self.npol): for ip in xrange(self.npol): sp = s._getspectrum(irow)[start:end] spectra[ip, i] = numpy.mean(sp) irow += 1 return spectra
def dosigref(sig, ref, smooth, tsysval=0.0, tauval=0.0): """ Calculate a quotient (sig-ref/ref * Tsys) Adopted from GBTIDL dosigref Parameters: sig: on source data ref: reference data smooth: width of box car smoothing for reference tsysval: user specified Tsys (scalar only) tauval: user specified Tau (required if tsysval is set) """ varlist = vars() from asap._asap import stmath stm = stmath() stm._setinsitu(False) s = scantable(stm._dosigref(sig, ref, smooth, tsysval, tauval)) s._add_history("dosigref",varlist) return s
def dosigref(sig, ref, smooth, tsysval=0.0, tauval=0.0): """ Calculate a quotient (sig-ref/ref * Tsys) Adopted from GBTIDL dosigref Parameters: sig: on source data ref: reference data smooth: width of box car smoothing for reference tsysval: user specified Tsys (scalar only) tauval: user specified Tau (required if tsysval is set) """ varlist = vars() from asap._asap import stmath stm = stmath() stm._setinsitu(False) s = scantable(stm._dosigref(sig, ref, smooth, tsysval, tauval)) s._add_history("dosigref", varlist) return s
def _array2dOp( scan, value, mode="ADD", tsys=False, insitu=None, skip_flaggedrow=False): """ This function is workaround on the basic operation of scantable with 2 dimensional float list. scan: scantable operand value: float list operand mode: operation mode (ADD, SUB, MUL, DIV) tsys: if True, operate tsys as well insitu: if False, a new scantable is returned. Otherwise, the array operation is done in-sitsu. skip_flaggedrow: skip operation for row-flagged spectra. """ if insitu is None: insitu = rcParams['insitu'] nrow = scan.nrow() s = None from asap._asap import stmath stm = stmath() stm._setinsitu(insitu) if len( value ) == 1: s = scantable( stm._arrayop( scan, value[0], mode, tsys, skip_flaggedrow ) ) elif len( value ) != nrow: raise ValueError( 'len(value) must be 1 or conform to scan.nrow()' ) else: from asap._asap import stmath if not insitu: s = scan.copy() else: s = scan # insitu must be True as we go row by row on the same data stm._setinsitu( True ) basesel = s.get_selection() # generate a new selector object based on basesel sel = selector(basesel) for irow in range( nrow ): sel.set_rows( irow ) s.set_selection( sel ) if len( value[irow] ) == 1: stm._unaryop( s, value[irow][0], mode, tsys, skip_flaggedrow ) else: #stm._arrayop( s, value[irow], mode, tsys, 'channel' ) stm._arrayop( s, value[irow], mode, tsys, skip_flaggedrow ) s.set_selection(basesel) return s
def merge(*args, **kwargs): """ Merge a list of scanatables, or comma-sperated scantables into one scnatble. Parameters: A list [scan1, scan2] or scan1, scan2. freq_tol: frequency tolerance for merging IFs. numeric values in units of Hz (1.0e6 -> 1MHz) and string ('1MHz') is allowed. Example: myscans = [scan1, scan2] allscans = merge(myscans) # or equivalent sameallscans = merge(scan1, scan2) # with freqtol allscans = merge(scan1, scan2, freq_tol=1.0e6) # or equivalently allscans = merge(scan1, scan2, freq_tol='1MHz') """ varlist = vars() if isinstance(args[0],list): lst = tuple(args[0]) elif isinstance(args[0],tuple): lst = args[0] else: lst = tuple(args) if kwargs.has_key('freq_tol'): freq_tol = str(kwargs['freq_tol']) if len(freq_tol) > 0 and re.match('.+[GMk]Hz$', freq_tol) is None: freq_tol += 'Hz' else: freq_tol = '' varlist["args"] = "%d scantables" % len(lst) # need special formatting her for history... from asap._asap import stmath stm = stmath() for s in lst: if not isinstance(s,scantable): msg = "Please give a list of scantables" raise TypeError(msg) s = scantable(stm._merge(lst, freq_tol)) s._add_history("merge", varlist) return s
def merge(*args, **kwargs): """ Merge a list of scanatables, or comma-sperated scantables into one scnatble. Parameters: A list [scan1, scan2] or scan1, scan2. freq_tol: frequency tolerance for merging IFs. numeric values in units of Hz (1.0e6 -> 1MHz) and string ('1MHz') is allowed. Example: myscans = [scan1, scan2] allscans = merge(myscans) # or equivalent sameallscans = merge(scan1, scan2) # with freqtol allscans = merge(scan1, scan2, freq_tol=1.0e6) # or equivalently allscans = merge(scan1, scan2, freq_tol='1MHz') """ varlist = vars() if isinstance(args[0], list): lst = tuple(args[0]) elif isinstance(args[0], tuple): lst = args[0] else: lst = tuple(args) if kwargs.has_key('freq_tol'): freq_tol = str(kwargs['freq_tol']) if len(freq_tol) > 0 and re.match('.+[GMk]Hz$', freq_tol) is None: freq_tol += 'Hz' else: freq_tol = '' varlist["args"] = "%d scantables" % len(lst) # need special formatting her for history... from asap._asap import stmath stm = stmath() for s in lst: if not isinstance(s, scantable): msg = "Please give a list of scantables" raise TypeError(msg) s = scantable(stm._merge(lst, freq_tol)) s._add_history("merge", varlist) return s
def get( self ): s = scantable( self.outfile, average=False ) self.nchan = len(s._getspectrum(0)) nrow = s.nrow() pols = numpy.ones( nrow, dtype=int ) for i in xrange(nrow): pols[i] = s.getpol(i) self.pollist, indices = numpy.unique( pols, return_inverse=True ) self.npol = len(self.pollist) self.pollist = self.pollist[indices[:self.npol]] #print 'pollist=',self.pollist #print 'npol=',self.npol #print 'nrow=',nrow if self.nx <= 0 or self.ny <= 0: idx = 1 d0 = s.get_direction( 0 ).split()[-2] d = s.get_direction(self.npol*idx) while( d is not None \ and d.split()[-2] != d0): idx += 1 d = s.get_direction(self.npol*idx) self.nx = idx self.ny = nrow / (self.npol * idx ) #print 'nx,ny=',self.nx,self.ny self.blc = s.get_directionval( 0 ) self.trc = s.get_directionval( nrow-self.npol ) #print self.blc #print self.trc if self.cellx <= 0.0 or self.celly <= 0.0: if nrow > 1: incrx = s.get_directionval( self.npol ) incry = s.get_directionval( self.nx*self.npol ) else: incrx = [0.0,0.0] incry = [0.0,0.0] self.cellx = abs( self.blc[0] - incrx[0] ) self.celly = abs( self.blc[1] - incry[1] )
def get(self): s = scantable(self.outfile, average=False) self.nchan = len(s._getspectrum(0)) nrow = s.nrow() pols = numpy.ones(nrow, dtype=int) for i in xrange(nrow): pols[i] = s.getpol(i) self.pollist, indices = numpy.unique(pols, return_inverse=True) self.npol = len(self.pollist) self.pollist = self.pollist[indices[:self.npol]] #print 'pollist=',self.pollist #print 'npol=',self.npol #print 'nrow=',nrow if self.nx <= 0 or self.ny <= 0: idx = 1 d0 = s.get_direction(0).split()[-2] d = s.get_direction(self.npol * idx) while( d is not None \ and d.split()[-2] != d0): idx += 1 d = s.get_direction(self.npol * idx) self.nx = idx self.ny = nrow / (self.npol * idx) #print 'nx,ny=',self.nx,self.ny self.blc = s.get_directionval(0) self.trc = s.get_directionval(nrow - self.npol) #print self.blc #print self.trc if self.cellx <= 0.0 or self.celly <= 0.0: if nrow > 1: incrx = s.get_directionval(self.npol) incry = s.get_directionval(self.nx * self.npol) else: incrx = [0.0, 0.0] incry = [0.0, 0.0] self.cellx = abs(self.blc[0] - incrx[0]) self.celly = abs(self.blc[1] - incry[1])
def quotient(source, reference, preserve=True): """ Return the quotient of a 'source' (signal) scan and a 'reference' scan. The reference can have just one scan, even if the signal has many. Otherwise they must have the same number of scans. The cursor of the output scan is set to 0 Parameters: source: the 'on' scan reference: the 'off' scan preserve: you can preserve (default) the continuum or remove it. The equations used are preserve: Output = Toff * (on/off) - Toff remove: Output = Toff * (on/off) - Ton """ varlist = vars() from asap._asap import stmath stm = stmath() stm._setinsitu(False) s = scantable(stm._quotient(source, reference, preserve)) s._add_history("quotient",varlist) return s
def quotient(source, reference, preserve=True): """ Return the quotient of a 'source' (signal) scan and a 'reference' scan. The reference can have just one scan, even if the signal has many. Otherwise they must have the same number of scans. The cursor of the output scan is set to 0 Parameters: source: the 'on' scan reference: the 'off' scan preserve: you can preserve (default) the continuum or remove it. The equations used are preserve: Output = Toff * (on/off) - Toff remove: Output = Toff * (on/off) - Ton """ varlist = vars() from asap._asap import stmath stm = stmath() stm._setinsitu(False) s = scantable(stm._quotient(source, reference, preserve)) s._add_history("quotient", varlist) return s
def almacal( scantab, scannos=[], calmode='none', verify=False ): """ Calibrate ALMA data Parameters: scantab: scantable scannos: list of scan number calmode: calibration mode verify: verify calibration """ from asap._asap import stmath stm = stmath() selection=selector() selection.set_scans(scannos) orig = scantab.get_selection() scantab.set_selection(orig+selection) ## ssub = scantab.get_scan( scannos ) ## scal = scantable( stm.almacal( ssub, calmode ) ) scal = scantable( stm.almacal( scantab, calmode ) ) scantab.set_selection(orig) return scal
def almacal(scantab, scannos=[], calmode='none', verify=False): """ Calibrate ALMA data Parameters: scantab: scantable scannos: list of scan number calmode: calibration mode verify: verify calibration """ from asap._asap import stmath stm = stmath() selection = selector() selection.set_scans(scannos) orig = scantab.get_selection() scantab.set_selection(orig + selection) ## ssub = scantab.get_scan( scannos ) ## scal = scantable( stm.almacal( ssub, calmode ) ) scal = scantable(stm.almacal(scantab, calmode)) scantab.set_selection(orig) return scal
def calfs(scantab, scannos=[], smooth=1, tsysval=0.0, tauval=0.0, tcalval=0.0, verify=False): """ Calibrate GBT frequency switched data. Adopted from GBTIDL getfs. Currently calfs identify the scans as frequency switched data if source type enum is fson and fsoff. The data must contains 'CAL' signal on/off in each integration. To identify 'CAL' on state, the source type enum of foncal and foffcal need to be present. Parameters: scantab: scantable scannos: list of scan numbers smooth: optional box smoothing order for the reference (default is 1 = no smoothing) tsysval: optional user specified Tsys (default is 0.0, use Tsys in the data) tauval: optional user specified Tau verify: Verify calibration if true """ varlist = vars() from asap._asap import stmath from asap._asap import srctype stm = stmath() stm._setinsitu(False) # check = scantab.get_scan('*_fs*') # if check is None: # msg = "The input data appear to contain no Nod observing mode data." # raise TypeError(msg) s = scantab.get_scan(scannos) del scantab resspec = scantable(stm._dofs(s, scannos, smooth, tsysval,tauval,tcalval)) ### if verify: # get data ssub = s.get_scan(scannos) #ssubon = ssub.get_scan('*calon') #ssuboff = ssub.get_scan('*[^calon]') sel = selector() sel.set_types( [srctype.foncal,srctype.foffcal] ) ssub.set_selection( sel ) ssubon = ssub.copy() ssub.set_selection() sel.reset() sel.set_types( [srctype.fson,srctype.fsoff] ) ssub.set_selection( sel ) ssuboff = ssub.copy() ssub.set_selection() sel.reset() import numpy precal={} postcal=[] keys=['fs','fs_calon','fsr','fsr_calon'] types=[srctype.fson,srctype.foncal,srctype.fsoff,srctype.foffcal] ifnos=list(ssub.getifnos()) polnos=list(ssub.getpolnos()) for i in range(2): #ss=ssuboff.get_scan('*'+keys[2*i]) ll=[] for j in range(len(ifnos)): for k in range(len(polnos)): sel.set_ifs(ifnos[j]) sel.set_polarizations(polnos[k]) sel.set_types(types[2*i]) try: #ss.set_selection(sel) ssuboff.set_selection(sel) except: continue ll.append(numpy.array(ss._getspectrum(0))) sel.reset() #ss.set_selection() ssuboff.set_selection() precal[keys[2*i]]=ll #del ss #ss=ssubon.get_scan('*'+keys[2*i+1]) ll=[] for j in range(len(ifnos)): for k in range(len(polnos)): sel.set_ifs(ifnos[j]) sel.set_polarizations(polnos[k]) sel.set_types(types[2*i+1]) try: #ss.set_selection(sel) ssubon.set_selection(sel) except: continue ll.append(numpy.array(ss._getspectrum(0))) sel.reset() #ss.set_selection() ssubon.set_selection() precal[keys[2*i+1]]=ll #del ss #sig=resspec.get_scan('*_fs') #ref=resspec.get_scan('*_fsr') sel.set_types( srctype.fson ) resspec.set_selection( sel ) sig=resspec.copy() resspec.set_selection() sel.reset() sel.set_type( srctype.fsoff ) resspec.set_selection( sel ) ref=resspec.copy() resspec.set_selection() sel.reset() for k in range(len(polnos)): for j in range(len(ifnos)): sel.set_ifs(ifnos[j]) sel.set_polarizations(polnos[k]) try: sig.set_selection(sel) postcal.append(numpy.array(sig._getspectrum(0))) except: ref.set_selection(sel) postcal.append(numpy.array(ref._getspectrum(0))) sel.reset() resspec.set_selection() del sel # plot asaplog.post() asaplog.push('Plot only first spectrum for each [if,pol] pairs to verify calibration.') asaplog.post('WARN') p=new_asaplot() rcp('lines', linewidth=1) #nr=min(6,len(ifnos)*len(polnos)) nr=len(ifnos)/2*len(polnos) titles=[] btics=[] if nr>3: asaplog.post() asaplog.push('Only first 3 [if,pol] pairs are plotted.') asaplog.post('WARN') nr=3 p.set_panels(rows=nr,cols=3,nplots=3*nr,ganged=False) for i in range(3*nr): b=False if i >= 3*nr-3: b=True btics.append(b) for i in range(nr): p.subplot(3*i) p.color=0 title='raw data IF%s,%s POL%s' % (ifnos[2*int(i/len(polnos))],ifnos[2*int(i/len(polnos))+1],polnos[i%len(polnos)]) titles.append(title) #p.set_axes('title',title,fontsize=40) ymin=1.0e100 ymax=-1.0e100 nchan=s.nchan(ifnos[2*int(i/len(polnos))]) edge=int(nchan*0.01) for j in range(4): spmin=min(precal[keys[j]][i][edge:nchan-edge]) spmax=max(precal[keys[j]][i][edge:nchan-edge]) ymin=min(ymin,spmin) ymax=max(ymax,spmax) for j in range(4): if i==0: p.set_line(label=keys[j]) else: p.legend() p.plot(precal[keys[j]][i]) p.axes.set_ylim(ymin-0.1*abs(ymin),ymax+0.1*abs(ymax)) if not btics[3*i]: p.axes.set_xticks([]) p.subplot(3*i+1) p.color=0 title='sig data IF%s POL%s' % (ifnos[2*int(i/len(polnos))],polnos[i%len(polnos)]) titles.append(title) #p.set_axes('title',title) p.legend() ymin=postcal[2*i][edge:nchan-edge].min() ymax=postcal[2*i][edge:nchan-edge].max() p.plot(postcal[2*i]) p.axes.set_ylim(ymin-0.1*abs(ymin),ymax+0.1*abs(ymax)) if not btics[3*i+1]: p.axes.set_xticks([]) p.subplot(3*i+2) p.color=0 title='ref data IF%s POL%s' % (ifnos[2*int(i/len(polnos))+1],polnos[i%len(polnos)]) titles.append(title) #p.set_axes('title',title) p.legend() ymin=postcal[2*i+1][edge:nchan-edge].min() ymax=postcal[2*i+1][edge:nchan-edge].max() p.plot(postcal[2*i+1]) p.axes.set_ylim(ymin-0.1*abs(ymin),ymax+0.1*abs(ymax)) if not btics[3*i+2]: p.axes.set_xticks([]) for i in range(3*nr): p.subplot(i) p.set_axes('title',titles[i],fontsize='medium') x=raw_input('Accept calibration ([y]/n): ' ) if x.upper() == 'N': p.quit() del p return scantab p.quit() del p ### resspec._add_history("calfs",varlist) return resspec
#if len(scannos)>2: # msg = "calnod can only process a pair of nod scans at time." # raise TypeError(msg) # #if len(scannos)==2: # scan1no = scannos[0] # scan2no = scannos[1] pairScans = list(scannos) if tsysval>0.0: if tauval<=0.0: msg = "Need to supply a valid tau to use the supplied Tsys" raise TypeError(msg) else: scantab.recalc_azel() resspec = scantable(stm._donod(scantab, pairScans, smooth, tsysval,tauval,tcalval)) ### if verify: # get data import numpy precal={} postcal=[] keys=['','_calon'] types=[srctype.nod,srctype.nodcal] ifnos=list(scantab.getifnos()) polnos=list(scantab.getpolnos()) sel=selector() ss = scantab.copy() for i in range(2): #ss=scantab.get_scan('*'+keys[i]) ll=[]
def average_time(*args, **kwargs): """ Return the (time) average of a scan or list of scans. [in channels only] The cursor of the output scan is set to 0 Parameters: one scan or comma separated scans or a list of scans mask: an optional mask (only used for 'var' and 'tsys' weighting) scanav: True averages each scan separately. False (default) averages all scans together, weight: Weighting scheme. 'none' (mean no weight) 'var' (1/var(spec) weighted) 'tsys' (1/Tsys**2 weighted) 'tint' (integration time weighted) 'tintsys' (Tint/Tsys**2) 'median' ( median averaging) align: align the spectra in velocity before averaging. It takes the time of the first spectrum in the first scantable as reference time. compel: True forces to average overwrapped IFs. Example: # return a time averaged scan from scana and scanb # without using a mask scanav = average_time(scana,scanb) # or equivalent # scanav = average_time([scana, scanb]) # return the (time) averaged scan, i.e. the average of # all correlator cycles scanav = average_time(scan, scanav=True) """ scanav = False if kwargs.has_key('scanav'): scanav = kwargs.get('scanav') weight = 'tint' if kwargs.has_key('weight'): weight = kwargs.get('weight') mask = () if kwargs.has_key('mask'): mask = kwargs.get('mask') align = False if kwargs.has_key('align'): align = kwargs.get('align') compel = False if kwargs.has_key('compel'): compel = kwargs.get('compel') varlist = vars() if isinstance(args[0],list): lst = args[0] elif isinstance(args[0],tuple): lst = list(args[0]) else: lst = list(args) del varlist["kwargs"] varlist["args"] = "%d scantables" % len(lst) # need special formatting here for history... from asap._asap import stmath stm = stmath() for s in lst: if not isinstance(s,scantable): msg = "Please give a list of scantables" raise TypeError(msg) if scanav: scanav = "SCAN" else: scanav = "NONE" alignedlst = [] if align: refepoch = lst[0].get_time(0) for scan in lst: alignedlst.append(scan.freq_align(refepoch,insitu=False)) else: alignedlst = lst if weight.upper() == 'MEDIAN': # median doesn't support list of scantables - merge first merged = None if len(alignedlst) > 1: merged = merge(alignedlst) else: merged = alignedlst[0] s = scantable(stm._averagechannel(merged, 'MEDIAN', scanav)) del merged else: #s = scantable(stm._average(alignedlst, mask, weight.upper(), scanav)) s = scantable(stm._new_average(alignedlst, compel, mask, weight.upper(), scanav)) s._add_history("average_time",varlist) return s
def calfs(scantab, scannos=[], smooth=1, tsysval=0.0, tauval=0.0, tcalval=0.0, verify=False): """ Calibrate GBT frequency switched data. Adopted from GBTIDL getfs. Currently calfs identify the scans as frequency switched data if source type enum is fson and fsoff. The data must contains 'CAL' signal on/off in each integration. To identify 'CAL' on state, the source type enum of foncal and foffcal need to be present. Parameters: scantab: scantable scannos: list of scan numbers smooth: optional box smoothing order for the reference (default is 1 = no smoothing) tsysval: optional user specified Tsys (default is 0.0, use Tsys in the data) tauval: optional user specified Tau verify: Verify calibration if true """ varlist = vars() from asap._asap import stmath from asap._asap import srctype stm = stmath() stm._setinsitu(False) # check = scantab.get_scan('*_fs*') # if check is None: # msg = "The input data appear to contain no Nod observing mode data." # raise TypeError(msg) s = scantab.get_scan(scannos) del scantab resspec = scantable(stm._dofs(s, scannos, smooth, tsysval, tauval, tcalval)) ### if verify: # get data ssub = s.get_scan(scannos) #ssubon = ssub.get_scan('*calon') #ssuboff = ssub.get_scan('*[^calon]') sel = selector() sel.set_types([srctype.foncal, srctype.foffcal]) ssub.set_selection(sel) ssubon = ssub.copy() ssub.set_selection() sel.reset() sel.set_types([srctype.fson, srctype.fsoff]) ssub.set_selection(sel) ssuboff = ssub.copy() ssub.set_selection() sel.reset() import numpy precal = {} postcal = [] keys = ['fs', 'fs_calon', 'fsr', 'fsr_calon'] types = [srctype.fson, srctype.foncal, srctype.fsoff, srctype.foffcal] ifnos = list(ssub.getifnos()) polnos = list(ssub.getpolnos()) for i in range(2): #ss=ssuboff.get_scan('*'+keys[2*i]) ll = [] for j in range(len(ifnos)): for k in range(len(polnos)): sel.set_ifs(ifnos[j]) sel.set_polarizations(polnos[k]) sel.set_types(types[2 * i]) try: #ss.set_selection(sel) ssuboff.set_selection(sel) except: continue ll.append(numpy.array(ss._getspectrum(0))) sel.reset() #ss.set_selection() ssuboff.set_selection() precal[keys[2 * i]] = ll #del ss #ss=ssubon.get_scan('*'+keys[2*i+1]) ll = [] for j in range(len(ifnos)): for k in range(len(polnos)): sel.set_ifs(ifnos[j]) sel.set_polarizations(polnos[k]) sel.set_types(types[2 * i + 1]) try: #ss.set_selection(sel) ssubon.set_selection(sel) except: continue ll.append(numpy.array(ss._getspectrum(0))) sel.reset() #ss.set_selection() ssubon.set_selection() precal[keys[2 * i + 1]] = ll #del ss #sig=resspec.get_scan('*_fs') #ref=resspec.get_scan('*_fsr') sel.set_types(srctype.fson) resspec.set_selection(sel) sig = resspec.copy() resspec.set_selection() sel.reset() sel.set_type(srctype.fsoff) resspec.set_selection(sel) ref = resspec.copy() resspec.set_selection() sel.reset() for k in range(len(polnos)): for j in range(len(ifnos)): sel.set_ifs(ifnos[j]) sel.set_polarizations(polnos[k]) try: sig.set_selection(sel) postcal.append(numpy.array(sig._getspectrum(0))) except: ref.set_selection(sel) postcal.append(numpy.array(ref._getspectrum(0))) sel.reset() resspec.set_selection() del sel # plot asaplog.post() asaplog.push( 'Plot only first spectrum for each [if,pol] pairs to verify calibration.' ) asaplog.post('WARN') p = new_asaplot() rcp('lines', linewidth=1) #nr=min(6,len(ifnos)*len(polnos)) nr = len(ifnos) / 2 * len(polnos) titles = [] btics = [] if nr > 3: asaplog.post() asaplog.push('Only first 3 [if,pol] pairs are plotted.') asaplog.post('WARN') nr = 3 p.set_panels(rows=nr, cols=3, nplots=3 * nr, ganged=False) for i in range(3 * nr): b = False if i >= 3 * nr - 3: b = True btics.append(b) for i in range(nr): p.subplot(3 * i) p.color = 0 title = 'raw data IF%s,%s POL%s' % ( ifnos[2 * int(i / len(polnos))], ifnos[2 * int(i / len(polnos)) + 1], polnos[i % len(polnos)]) titles.append(title) #p.set_axes('title',title,fontsize=40) ymin = 1.0e100 ymax = -1.0e100 nchan = s.nchan(ifnos[2 * int(i / len(polnos))]) edge = int(nchan * 0.01) for j in range(4): spmin = min(precal[keys[j]][i][edge:nchan - edge]) spmax = max(precal[keys[j]][i][edge:nchan - edge]) ymin = min(ymin, spmin) ymax = max(ymax, spmax) for j in range(4): if i == 0: p.set_line(label=keys[j]) else: p.legend() p.plot(precal[keys[j]][i]) p.axes.set_ylim(ymin - 0.1 * abs(ymin), ymax + 0.1 * abs(ymax)) if not btics[3 * i]: p.axes.set_xticks([]) p.subplot(3 * i + 1) p.color = 0 title = 'sig data IF%s POL%s' % (ifnos[2 * int(i / len(polnos))], polnos[i % len(polnos)]) titles.append(title) #p.set_axes('title',title) p.legend() ymin = postcal[2 * i][edge:nchan - edge].min() ymax = postcal[2 * i][edge:nchan - edge].max() p.plot(postcal[2 * i]) p.axes.set_ylim(ymin - 0.1 * abs(ymin), ymax + 0.1 * abs(ymax)) if not btics[3 * i + 1]: p.axes.set_xticks([]) p.subplot(3 * i + 2) p.color = 0 title = 'ref data IF%s POL%s' % (ifnos[2 * int(i / len(polnos)) + 1], polnos[i % len(polnos)]) titles.append(title) #p.set_axes('title',title) p.legend() ymin = postcal[2 * i + 1][edge:nchan - edge].min() ymax = postcal[2 * i + 1][edge:nchan - edge].max() p.plot(postcal[2 * i + 1]) p.axes.set_ylim(ymin - 0.1 * abs(ymin), ymax + 0.1 * abs(ymax)) if not btics[3 * i + 2]: p.axes.set_xticks([]) for i in range(3 * nr): p.subplot(i) p.set_axes('title', titles[i], fontsize='medium') x = raw_input('Accept calibration ([y]/n): ') if x.upper() == 'N': p.quit() del p return scantab p.quit() del p ### resspec._add_history("calfs", varlist) return resspec
def getResult( self ): """ Return gridded data as a scantable. """ tp = 0 if rcParams['scantable.storage']=='memory' else 1 return scantable( self.gridder._get( tp ), average=False )
def getResult(self): """ Return gridded data as a scantable. """ tp = 0 if rcParams['scantable.storage'] == 'memory' else 1 return scantable(self.gridder._get(tp), average=False)
def splitant(filename, outprefix='', overwrite=False, getpt=True): """ Split Measurement set by antenna name, save data as a scantables, and return a list of filename. Note that frequency reference frame is imported as it is in Measurement set. Notice this method can only be available from CASA. Prameter filename: the name of Measurement set to be read. outprefix: the prefix of output scantable name. the names of output scantable will be outprefix.antenna1, outprefix.antenna2, .... If not specified, outprefix = filename is assumed. overwrite If the file should be overwritten if it exists. The default False is to return with warning without writing the output. USE WITH CARE. getpt Whether to import direction from MS/POINTING table or not. Default is True (import direction). """ # Import the table toolkit from CASA from taskinit import gentools from asap.scantable import is_ms tb = gentools(['tb'])[0] # Check the input filename if isinstance(filename, str): import os.path filename = os.path.expandvars(filename) filename = os.path.expanduser(filename) if not os.path.exists(filename): s = "File '%s' not found." % (filename) raise IOError(s) # check if input file is MS if not is_ms(filename): s = "File '%s' is not a Measurement set." % (filename) raise IOError(s) else: s = "The filename should be string. " raise TypeError(s) # Check out put file name outname = '' if len(outprefix) > 0: prefix = outprefix + '.' else: prefix = filename.rstrip('/') # Now do the actual splitting. outfiles = [] tb.open(tablename=filename, nomodify=True) ant1 = tb.getcol('ANTENNA1', 0, -1, 1) anttab = tb.getkeyword('ANTENNA').lstrip('Table: ') tb.close() tb.open(tablename=anttab, nomodify=True) nant = tb.nrows() antnames = tb.getcol('NAME', 0, nant, 1) tb.close() for antid in set(ant1): scan = scantable(filename, average=False, antenna=int(antid), getpt=getpt) outname = prefix + antnames[antid] + '.asap' scan.save(outname, format='ASAP', overwrite=overwrite) del scan outfiles.append(outname) return outfiles
#if len(scannos)>2: # msg = "calnod can only process a pair of nod scans at time." # raise TypeError(msg) # #if len(scannos)==2: # scan1no = scannos[0] # scan2no = scannos[1] pairScans = list(scannos) if tsysval > 0.0: if tauval <= 0.0: msg = "Need to supply a valid tau to use the supplied Tsys" raise TypeError(msg) else: scantab.recalc_azel() resspec = scantable( stm._donod(scantab, pairScans, smooth, tsysval, tauval, tcalval)) ### if verify: # get data import numpy precal = {} postcal = [] keys = ['', '_calon'] types = [srctype.nod, srctype.nodcal] ifnos = list(scantab.getifnos()) polnos = list(scantab.getpolnos()) sel = selector() ss = scantab.copy() for i in range(2): #ss=scantab.get_scan('*'+keys[i]) ll = []
def average_time(*args, **kwargs): """ Return the (time) average of a scan or list of scans. [in channels only] The cursor of the output scan is set to 0 Parameters: one scan or comma separated scans or a list of scans mask: an optional mask (only used for 'var' and 'tsys' weighting) scanav: True averages each scan separately. False (default) averages all scans together, weight: Weighting scheme. 'none' (mean no weight) 'var' (1/var(spec) weighted) 'tsys' (1/Tsys**2 weighted) 'tint' (integration time weighted) 'tintsys' (Tint/Tsys**2) 'median' ( median averaging) align: align the spectra in velocity before averaging. It takes the time of the first spectrum in the first scantable as reference time. compel: True forces to average overwrapped IFs. Example: # return a time averaged scan from scana and scanb # without using a mask scanav = average_time(scana,scanb) # or equivalent # scanav = average_time([scana, scanb]) # return the (time) averaged scan, i.e. the average of # all correlator cycles scanav = average_time(scan, scanav=True) """ scanav = False if kwargs.has_key('scanav'): scanav = kwargs.get('scanav') weight = 'tint' if kwargs.has_key('weight'): weight = kwargs.get('weight') mask = () if kwargs.has_key('mask'): mask = kwargs.get('mask') align = False if kwargs.has_key('align'): align = kwargs.get('align') compel = False if kwargs.has_key('compel'): compel = kwargs.get('compel') varlist = vars() if isinstance(args[0], list): lst = args[0] elif isinstance(args[0], tuple): lst = list(args[0]) else: lst = list(args) del varlist["kwargs"] varlist["args"] = "%d scantables" % len(lst) # need special formatting here for history... from asap._asap import stmath stm = stmath() for s in lst: if not isinstance(s, scantable): msg = "Please give a list of scantables" raise TypeError(msg) if scanav: scanav = "SCAN" else: scanav = "NONE" alignedlst = [] if align: refepoch = lst[0].get_time(0) for scan in lst: alignedlst.append(scan.freq_align(refepoch, insitu=False)) else: alignedlst = lst if weight.upper() == 'MEDIAN': # median doesn't support list of scantables - merge first merged = None if len(alignedlst) > 1: merged = merge(alignedlst) else: merged = alignedlst[0] s = scantable(stm._averagechannel(merged, 'MEDIAN', scanav)) del merged else: #s = scantable(stm._average(alignedlst, mask, weight.upper(), scanav)) s = scantable( stm._new_average(alignedlst, compel, mask, weight.upper(), scanav)) s._add_history("average_time", varlist) return s