[102] | 1 | from asap._asap import sdtable
|
---|
| 2 | from numarray import ones
|
---|
| 3 | import sys
|
---|
| 4 |
|
---|
| 5 | class scantable(sdtable):
|
---|
| 6 | """
|
---|
| 7 | The ASAP container for scans
|
---|
| 8 | """
|
---|
| 9 | def functions(self):
|
---|
| 10 | return ['copy','get_scan','summary','rms','set_unit','create_mask']
|
---|
| 11 |
|
---|
| 12 | def __init__(self, filename):
|
---|
| 13 | """
|
---|
| 14 | Create a scantable from a saved one or make a reference
|
---|
| 15 | Parameters:
|
---|
| 16 | filename: the name of an asap table on disk, or
|
---|
| 17 | [advanced] a refernce to an existing
|
---|
| 18 | scantable
|
---|
| 19 | """
|
---|
| 20 | sdtable.__init__(self, filename)
|
---|
| 21 | self._unit = 'channel'
|
---|
| 22 |
|
---|
| 23 | def copy(self):
|
---|
| 24 | """
|
---|
| 25 | Return a copy of this scantable.
|
---|
| 26 | Parameters:
|
---|
| 27 |
|
---|
| 28 | Example:
|
---|
| 29 | copiedscan = scan.copy()
|
---|
| 30 | """
|
---|
| 31 | sd = sdtable._copy(self)
|
---|
| 32 | return scantable(sd)
|
---|
| 33 | def get_scan(self, scanid=None):
|
---|
| 34 | """
|
---|
| 35 | Return a specific scan (by scanno) or collection of scans (by
|
---|
| 36 | source name) in a new scantable.
|
---|
| 37 | Parameters:
|
---|
| 38 | scanid: a scanno or a source name
|
---|
| 39 | Example:
|
---|
| 40 | scan.getscan('323p459')
|
---|
| 41 | # gets all scans containing the source '323p459'
|
---|
| 42 | """
|
---|
| 43 | if scanid is None:
|
---|
| 44 | print "Please specify a scan no or name to retrieve from the scantable"
|
---|
| 45 | try:
|
---|
| 46 | if type(scanid) is str:
|
---|
| 47 | s = sdtable.getsource(self,scanid)
|
---|
| 48 | return scantable(s)
|
---|
| 49 | elif type(scanid) is int:
|
---|
| 50 | s = sdtable.getscan(self,scanid)
|
---|
| 51 | return scantable(s)
|
---|
| 52 | except RuntimeError:
|
---|
| 53 | print "Couldn't find any match."
|
---|
| 54 |
|
---|
| 55 | def __str__(self):
|
---|
| 56 | return sdtable.summary(self)
|
---|
| 57 |
|
---|
| 58 | def summary(self,filename=None):
|
---|
| 59 | """
|
---|
| 60 | Print a summary of the contents of this scantable.
|
---|
| 61 | Parameters:
|
---|
| 62 | filename: the name of a file to write the putput to
|
---|
| 63 | Default - no file output
|
---|
| 64 | """
|
---|
| 65 | info = sdtable.summary(self)
|
---|
| 66 | if filename is not None:
|
---|
| 67 | data = open(filename, 'w')
|
---|
| 68 | data.write(info)
|
---|
| 69 | data.close()
|
---|
| 70 | print info
|
---|
| 71 |
|
---|
| 72 | def set_selection(self, thebeam=0,theif=0,thepol=0):
|
---|
| 73 | """
|
---|
| 74 | Set the spectrum for individual operations.
|
---|
| 75 | Parameters:
|
---|
| 76 | thebeam,theif,thepol: a number
|
---|
| 77 | Example:
|
---|
| 78 | scan.set_selection(0,0,1)
|
---|
| 79 | pol1rms = scan.rms(all=False) # returns rms for beam=0
|
---|
| 80 | # if=0, pol=1
|
---|
| 81 | """
|
---|
| 82 | self.setbeam(thebeam)
|
---|
| 83 | self.setpol(thepol)
|
---|
| 84 | self.setif(theif)
|
---|
| 85 | return
|
---|
| 86 |
|
---|
| 87 | def get_selection(self):
|
---|
| 88 | i = self.getbeam()
|
---|
| 89 | j = self.getif()
|
---|
| 90 | k = self.getpol()
|
---|
| 91 | out = 'Beam=%d IF=%d Pol=%d '% (i,j,k)
|
---|
| 92 | print out
|
---|
| 93 | return i,j,k
|
---|
| 94 |
|
---|
| 95 | def rms(self,mask=None, all=True):
|
---|
| 96 | """
|
---|
| 97 | Determine the root mean square of the current beam/if/pol
|
---|
| 98 | Takes a 'mask' as an optional parameter to specify which
|
---|
| 99 | channels should be excluded.
|
---|
| 100 | Parameters:
|
---|
| 101 | mask: an optional mask specifying where the rms
|
---|
| 102 | should be determined.
|
---|
| 103 | all: optional flag to show all or a selected
|
---|
| 104 | spectrum of Beam/IF/Pol
|
---|
| 105 |
|
---|
| 106 | Example:
|
---|
| 107 | scan.setuint('channel')
|
---|
| 108 | msk = scan.create_mask([100,200],[500,600])
|
---|
| 109 | scan.rms(mask=m)
|
---|
| 110 | """
|
---|
| 111 | from asap._asap import rms as _rms
|
---|
| 112 | if mask == None:
|
---|
| 113 | mask = ones(self.nchan())
|
---|
| 114 | if all:
|
---|
| 115 | out = ''
|
---|
| 116 | tmp = []
|
---|
| 117 | for i in range(self.nbeam()):
|
---|
| 118 | self.setbeam(i)
|
---|
| 119 | for j in range(self.nif()):
|
---|
| 120 | self.setif(j)
|
---|
| 121 | for k in range(self.npol()):
|
---|
| 122 | self.setpol(k)
|
---|
| 123 | rmsval = _rms(self,mask)
|
---|
| 124 | tmp.append(rmsval)
|
---|
| 125 | out += 'Beam[%d], IF[%d], Pol[%d] = %3.3f\n' % (i,j,k,rmsval)
|
---|
| 126 | print out
|
---|
| 127 | return tmp
|
---|
| 128 |
|
---|
| 129 | else:
|
---|
| 130 | i = self.getbeam()
|
---|
| 131 | j = self.getif()
|
---|
| 132 | k = self.getpol()
|
---|
| 133 | rmsval = _rms(self,mask)
|
---|
| 134 | out = 'Beam[%d], IF[%d], Pol[%d] = %3.3f' % (i,j,k,rmsval)
|
---|
| 135 | print out
|
---|
| 136 | return rmsval
|
---|
| 137 |
|
---|
| 138 | def get_tsys(self, all=True):
|
---|
| 139 | if all:
|
---|
| 140 | tmp = []
|
---|
| 141 | out = ''
|
---|
| 142 | for i in range(self.nbeam()):
|
---|
| 143 | self.setbeam(i)
|
---|
| 144 | for j in range(self.nif()):
|
---|
| 145 | self.setif(j)
|
---|
| 146 | for k in range(self.npol()):
|
---|
| 147 | self.setpol(k)
|
---|
| 148 | ts = self.gettsys()
|
---|
| 149 | tmp.append(ts)
|
---|
| 150 | out += 'TSys: Beam[%d], IF[%d], Pol[%d] = %3.3f\n' % (i,j,k,ts)
|
---|
| 151 | print out
|
---|
| 152 | return tmp
|
---|
| 153 | else:
|
---|
| 154 | i = self.getbeam()
|
---|
| 155 | j = self.getif()
|
---|
| 156 | k = self.getpol()
|
---|
| 157 | ts = self.gettsys()
|
---|
| 158 | out = 'TSys: Beam[%d], IF[%d], Pol[%d] = %3.3f' % (i,j,k,ts)
|
---|
| 159 | print out
|
---|
| 160 | return ts
|
---|
| 161 |
|
---|
| 162 | def set_unit(self, unit='channel'):
|
---|
| 163 | """
|
---|
| 164 | Set the unit for all following operations on this scantable
|
---|
| 165 | Parameters:
|
---|
| 166 | unit: optional unit, default is 'channel'
|
---|
| 167 | one of 'GHz','MHz','km/s','channel'
|
---|
| 168 | """
|
---|
| 169 | units = ['GHz','MHz','km/s','channel','pixel','']
|
---|
| 170 | if unit in units:
|
---|
| 171 | if unit in ['','pixel']:
|
---|
| 172 | unit = 'channel'
|
---|
| 173 | self._unit = unit
|
---|
| 174 | else:
|
---|
| 175 | print "Invalid unit given, please use one of:"
|
---|
| 176 | print units
|
---|
| 177 |
|
---|
| 178 | def create_mask(self, *args):
|
---|
| 179 | """
|
---|
| 180 | Compute and return a mask based on [min,max] windows.
|
---|
| 181 | Parameters:
|
---|
| 182 | [min,max],[min2,max2],...
|
---|
| 183 | Pairs of start/end points specifying the regions
|
---|
| 184 | to be masked
|
---|
| 185 | Example:
|
---|
| 186 | scan.setunit('channel')
|
---|
| 187 | msk = scan.set_mask([400,500],[800,900])
|
---|
| 188 | masks the regions between 400 and 500
|
---|
| 189 | and 800 and 900 in the unit 'channel'
|
---|
| 190 | """
|
---|
| 191 | print "The current mask window unit is", self._unit
|
---|
| 192 | n = self.nchan()
|
---|
| 193 | if self._unit == 'channel':
|
---|
| 194 | data = range(n)
|
---|
| 195 | else:
|
---|
| 196 | data = self.getabscissa(unit=self._unit)
|
---|
| 197 | msk = ones(n)
|
---|
| 198 | for window in args:
|
---|
| 199 | if (len(window) != 2 or window[0] > window[1] ):
|
---|
| 200 | print "A window needs to be defined as [min,max]"
|
---|
| 201 | return
|
---|
| 202 | for i in range(n):
|
---|
| 203 | if data[i] > window[0] and data[i] < window[1]:
|
---|
| 204 | msk[i] = 0
|
---|
| 205 | return msk
|
---|
| 206 | def set_restfrequencies(self, freqs, unit='Hz'):
|
---|
| 207 | """
|
---|
| 208 | Set the restfrequency(s) for this scantable.
|
---|
| 209 | Parameters:
|
---|
| 210 | freqs: one or more frequencies
|
---|
| 211 | unit: optional 'unit', default 'Hz'
|
---|
| 212 | Example:
|
---|
| 213 | scan.set_restfrequencies([1000000000.0])
|
---|
| 214 | """
|
---|
| 215 | self.setrestfreqs(freqs, unit)
|
---|
| 216 | return
|
---|
| 217 |
|
---|
| 218 | def flag_spectrum(self, thebeam, theif, thepol):
|
---|
| 219 | """
|
---|
| 220 | This flags a selected spectrum in the scan 'for good'.
|
---|
| 221 | USE WITH CARE - not reversible.
|
---|
| 222 | Use masks for non-permanent exclusion of channels.
|
---|
| 223 | Parameters:
|
---|
| 224 | thebeam,theif,thepol: all have to be explicitly
|
---|
| 225 | specified
|
---|
| 226 | Example:
|
---|
| 227 | scan.flag_spectrum(0,0,1)
|
---|
| 228 | flags the spectrum for Beam=0, IF=0, Pol=1
|
---|
| 229 | """
|
---|
| 230 | if (thebeam < self.nbeam() and theif < self.nif() and thepol < self.npol()):
|
---|
| 231 | stable.setbeam(thebeam)
|
---|
| 232 | stable.setif(theif)
|
---|
| 233 | stable.setpol(thepol)
|
---|
| 234 | stable._flag(self)
|
---|
| 235 | else:
|
---|
| 236 | print "Please specify a valid (Beam/IF/Pol)"
|
---|
| 237 | return
|
---|