[101] | 1 | from scantable import scantable
|
---|
| 2 |
|
---|
[143] | 3 | def average_time(*args, **kwargs):
|
---|
[101] | 4 | """
|
---|
[113] | 5 | Return the (time) average of a scan or list of scans. [in channels only]
|
---|
| 6 | Parameters:
|
---|
| 7 | one scan or comma separated scans
|
---|
[143] | 8 | mask: an optional mask (only used for 'var' and 'tsys' weighting)
|
---|
| 9 | scanav: False (default) averages all scans together,
|
---|
| 10 | True averages each scan separately
|
---|
| 11 | weight: Weighting scheme. 'none' (default), 'var' (variance
|
---|
| 12 | weighted), 'tsys'
|
---|
[113] | 13 | Example:
|
---|
| 14 | # return a time averaged scan from scana and scanb
|
---|
| 15 | # without using a mask
|
---|
[129] | 16 | scanav = average_time(scana,scanb)
|
---|
[113] | 17 | # return the (time) averaged scan, i.e. the average of
|
---|
| 18 | # all correlator cycles
|
---|
| 19 | scanav = average_time(scan)
|
---|
[143] | 20 |
|
---|
[101] | 21 | """
|
---|
[143] | 22 | scanAv = False
|
---|
| 23 | if kwargs.has_key('scanav'):
|
---|
| 24 | scanAv = kwargs.get('scanav')
|
---|
| 25 | #
|
---|
| 26 | weight = 'none'
|
---|
| 27 | if kwargs.has_key('weight'):
|
---|
| 28 | weight = kwargs.get('weight')
|
---|
| 29 | #
|
---|
| 30 | mask = ()
|
---|
| 31 | if kwargs.has_key('mask'):
|
---|
| 32 | mask = kwargs.get('mask')
|
---|
| 33 | #
|
---|
| 34 | lst = tuple(args)
|
---|
| 35 | from asap._asap import average as _av
|
---|
[113] | 36 | for s in lst:
|
---|
[101] | 37 | if not isinstance(s,scantable):
|
---|
| 38 | print "Please give a list of scantables"
|
---|
| 39 | return
|
---|
[143] | 40 | return scantable(_av(lst, mask, scanAv, weight))
|
---|
[101] | 41 |
|
---|
[245] | 42 | def quotient(source, reference, preserve=True):
|
---|
[101] | 43 | """
|
---|
[246] | 44 | Return the quotient of a 'source' (signal) scan and a 'reference' scan.
|
---|
| 45 | The reference can have just one row, even if the signal has many. Otherwise
|
---|
| 46 | they must have the same number of rows.
|
---|
[101] | 47 | Parameters:
|
---|
| 48 | source: the 'on' scan
|
---|
| 49 | reference: the 'off' scan
|
---|
[245] | 50 | preserve: you can preserve (default) the continuum or
|
---|
| 51 | remove it. The equations used are
|
---|
| 52 | preserve - Output = Tref * (sig/ref) - Tref
|
---|
| 53 | remove - Output = Tref * (sig/ref) - Tsig
|
---|
[101] | 54 | """
|
---|
| 55 | from asap._asap import quotient as _quot
|
---|
[245] | 56 | return scantable(_quot(source, reference, preserve))
|
---|
[101] | 57 |
|
---|
[242] | 58 | def b_operate(left, right, op='add'):
|
---|
| 59 | """
|
---|
| 60 | Apply simple mathematical binary operations to two
|
---|
| 61 | scan tables, returning the result in a new scan table.
|
---|
| 62 | The operation is applied to both the correlations and the TSys data
|
---|
| 63 | Parameters:
|
---|
| 64 | left: the 'left' scan
|
---|
| 65 | right: the 'right' scan
|
---|
| 66 | op: the operation: 'add' (default), 'sub', 'mul', 'div'
|
---|
| 67 | """
|
---|
| 68 | from asap._asap import b_operate as _bop
|
---|
| 69 | return scantable(_bop(left, right, op))
|
---|
| 70 |
|
---|
[150] | 71 | def scale(scan, factor, insitu=False, all=True):
|
---|
[101] | 72 | """
|
---|
| 73 | Return a scan where all spectra are scaled by the give 'factor'
|
---|
| 74 | Parameters:
|
---|
| 75 | scan: a scantable
|
---|
[113] | 76 | factor: the scaling factor
|
---|
[150] | 77 | insitu: if False (default) a new scantable is returned.
|
---|
| 78 | Otherwise, the scaling is done in-situ
|
---|
| 79 | all: if True (default) apply to all spectra. Otherwise
|
---|
| 80 | apply only to the selected (beam/pol/if)spectra only
|
---|
[101] | 81 | """
|
---|
[141] | 82 | if not insitu:
|
---|
| 83 | from asap._asap import scale as _scale
|
---|
[150] | 84 | return scantable(_scale(scan, factor, all))
|
---|
[141] | 85 | else:
|
---|
| 86 | from asap._asap import scale_insitu as _scale
|
---|
[150] | 87 | _scale(scan, factor, all)
|
---|
[141] | 88 | return
|
---|
| 89 |
|
---|
[101] | 90 |
|
---|
[150] | 91 | def add(scan, offset, insitu=False, all=True):
|
---|
[113] | 92 | """
|
---|
[150] | 93 | Return a scan where all spectra have the offset added
|
---|
[113] | 94 | Parameters:
|
---|
| 95 | scan: a scantable
|
---|
[150] | 96 | offset: the offset
|
---|
| 97 | insitu: if False (default) a new scantable is returned.
|
---|
| 98 | Otherwise, the addition is done in-situ
|
---|
| 99 | all: if True (default) apply to all spectra. Otherwise
|
---|
| 100 | apply only to the selected (beam/pol/if)spectra only
|
---|
[113] | 101 | """
|
---|
[150] | 102 | if not insitu:
|
---|
| 103 | from asap._asap import add as _add
|
---|
| 104 | return scantable(_add(scan, offset, all))
|
---|
| 105 | else:
|
---|
| 106 | from asap._asap import add_insitu as _add
|
---|
| 107 | _add(scan, offset, all)
|
---|
| 108 | return
|
---|
| 109 |
|
---|
[225] | 110 | def convert_flux(scan, area, eta=1.0, insitu=False, all=True):
|
---|
[224] | 111 | """
|
---|
| 112 | Return a scan where all spectra are converted to either Jansky or Kelvin
|
---|
| 113 | depending upon the flux units of the scan table.
|
---|
| 114 | Parameters:
|
---|
| 115 | scan: a scantable
|
---|
| 116 | area: the illuminated area of the telescope (m**2)
|
---|
| 117 | eta: The efficiency of the telescope (default 1.0)
|
---|
| 118 | insitu: if False (default) a new scantable is returned.
|
---|
| 119 | Otherwise, the conversion is done in-situ
|
---|
| 120 | all: if True (default) apply to all spectra. Otherwise
|
---|
| 121 | apply only to the selected (beam/pol/if)spectra only
|
---|
| 122 | """
|
---|
| 123 | if not insitu:
|
---|
| 124 | from asap._asap import convertflux as _convert
|
---|
| 125 | return scantable(_convert(scan, area, eta, all))
|
---|
| 126 | else:
|
---|
| 127 | from asap._asap import convertflux_insitu as _convert
|
---|
| 128 | _convert(scan, area, eta, all)
|
---|
| 129 | return
|
---|
[229] | 130 |
|
---|
[242] | 131 | def gain_el(scan, poly=None, filename="", method="linear", insitu=False, all=True):
|
---|
[229] | 132 | """
|
---|
[242] | 133 | Return a scan after applying a gain-elevation correction. The correction
|
---|
| 134 | can be made via either a polynomial or a table-based interpolation
|
---|
| 135 | (and extrapolation if necessary).
|
---|
| 136 | You specify polynomial coefficients, an ascii table or neither.
|
---|
| 137 | If you specify neither, then a polynomial correction will be made
|
---|
| 138 | with built in coefficients known for certain telescopes (an error will
|
---|
| 139 | occur if the instrument is not known).
|
---|
[229] | 140 | Parameters:
|
---|
| 141 | scan: a scantable
|
---|
[242] | 142 | poly: Polynomial coefficients (default None) to compute a gain-elevation
|
---|
| 143 | correction as a function of elevation (in degrees).
|
---|
| 144 | filename: The name of an ascii file holding correction factors.
|
---|
| 145 | The first row of the ascii file must give the column
|
---|
| 146 | names and these MUST include columns
|
---|
| 147 | "ELEVATION" (degrees) and "FACTOR" (multiply data by this) somewhere.
|
---|
[229] | 148 | The second row must give the data type of the column. Use 'R' for
|
---|
| 149 | Real and 'I' for Integer. An example file would be:
|
---|
| 150 |
|
---|
[242] | 151 | TIME ELEVATION FACTOR
|
---|
| 152 | R R R
|
---|
| 153 | 0.1 0 1.5
|
---|
| 154 | 0.2 20 1.4
|
---|
| 155 | 0.3 40 1.3
|
---|
| 156 | 0.4 60 1.2
|
---|
| 157 | 0.5 80 1.1
|
---|
| 158 | 0.6 90 1.0
|
---|
| 159 | method: Interpolation method when correcting from a table. Values
|
---|
| 160 | are "nearest", "linear" (default), "cubic" and "spline"
|
---|
[229] | 161 | insitu: if False (default) a new scantable is returned.
|
---|
| 162 | Otherwise, the conversion is done in-situ
|
---|
| 163 | all: if True (default) apply to all spectra. Otherwise
|
---|
| 164 | apply only to the selected (beam/pol/if)spectra only
|
---|
| 165 | """
|
---|
[242] | 166 | if poly is None:
|
---|
| 167 | poly = ()
|
---|
[229] | 168 | if not insitu:
|
---|
| 169 | from asap._asap import gainel as _gainEl
|
---|
[242] | 170 | return scantable(_gainEl(scan, poly, filename, method, all))
|
---|
[229] | 171 | else:
|
---|
| 172 | from asap._asap import gainel_insitu as _gainEl
|
---|
[242] | 173 | _gainEl(scan, poly, filename, method, all)
|
---|
[229] | 174 | return
|
---|
[224] | 175 |
|
---|
[242] | 176 | def opacity(scan, tau, insitu=False, all=True):
|
---|
| 177 | """
|
---|
| 178 | Return a scan after applying an opacity correction.
|
---|
| 179 | Parameters:
|
---|
| 180 | scan: a scantable
|
---|
| 181 | tau: Opacity from which the correction factor is exp(tau*ZD)
|
---|
| 182 | where ZD is the zenith-distance
|
---|
| 183 | insitu: if False (default) a new scantable is returned.
|
---|
| 184 | Otherwise, the conversion is done in-situ
|
---|
| 185 | all: if True (default) apply to all spectra. Otherwise
|
---|
| 186 | apply only to the selected (beam/pol/if)spectra only
|
---|
| 187 | """
|
---|
| 188 | if not insitu:
|
---|
| 189 | from asap._asap import opacity as _opacity
|
---|
| 190 | return scantable(_opacity(scan, tau, all))
|
---|
| 191 | else:
|
---|
| 192 | from asap._asap import opacity_insitu as _opacity
|
---|
| 193 | _opacity(scan, tau, all)
|
---|
| 194 | return
|
---|
| 195 |
|
---|
[167] | 196 | def bin(scan, width=5, insitu=False):
|
---|
[101] | 197 | """
|
---|
[167] | 198 | Return a scan where all spectra have been binned up
|
---|
[172] | 199 | width: The bin width (default=5) in pixels
|
---|
[167] | 200 | insitu: if False (default) a new scantable is returned.
|
---|
| 201 | Otherwise, the addition is done in-situ
|
---|
[101] | 202 | """
|
---|
[167] | 203 | if not insitu:
|
---|
| 204 | from asap._asap import bin as _bin
|
---|
| 205 | return scantable(_bin(scan, width))
|
---|
| 206 | else:
|
---|
| 207 | from asap._asap import bin_insitu as _bin
|
---|
| 208 | _bin(scan, width)
|
---|
| 209 | return
|
---|
[113] | 210 |
|
---|
[166] | 211 | def average_pol(scan, mask=None, insitu=False):
|
---|
[113] | 212 | """
|
---|
| 213 | Average the Polarisations together.
|
---|
| 214 | Parameters:
|
---|
[172] | 215 | scan: The scantable
|
---|
| 216 | mask: An optional mask defining the region, where the
|
---|
| 217 | averaging will be applied. The output will have all
|
---|
| 218 | specified points masked.
|
---|
| 219 | insitu: If False (default) a new scantable is returned.
|
---|
[166] | 220 | Otherwise, the averaging is done in-situ
|
---|
[113] | 221 | Example:
|
---|
| 222 | polav = average_pols(myscan)
|
---|
| 223 | """
|
---|
| 224 | if mask is None:
|
---|
[166] | 225 | mask = ()
|
---|
| 226 | if not insitu:
|
---|
| 227 | from asap._asap import averagepol as _avpol
|
---|
| 228 | return scantable(_avpol(scan, mask))
|
---|
| 229 | else:
|
---|
| 230 | from asap._asap import averagepol_insitu as _avpol
|
---|
| 231 | _avpol(scan, mask)
|
---|
| 232 | return
|
---|
[113] | 233 |
|
---|
[180] | 234 | def smooth(scan, kernel="hanning", width=5.0, insitu=False, all=True):
|
---|
[113] | 235 | """
|
---|
[180] | 236 | Smooth the spectrum by the specified kernel (conserving flux).
|
---|
[113] | 237 | Parameters:
|
---|
[172] | 238 | scan: The input scan
|
---|
[180] | 239 | kernel: The type of smoothing kernel. Select from
|
---|
| 240 | 'hanning' (default), 'gaussian' and 'boxcar'.
|
---|
| 241 | The first three characters are sufficient.
|
---|
| 242 | width: The width of the kernel in pixels. For hanning this is
|
---|
| 243 | ignored otherwise it defauls to 5 pixels.
|
---|
| 244 | For 'gaussian' it is the Full Width Half
|
---|
| 245 | Maximum. For 'boxcar' it is the full width.
|
---|
[172] | 246 | insitu: If False (default) a new scantable is returned.
|
---|
| 247 | Otherwise, the scaling is done in-situ
|
---|
[180] | 248 | all: If True (default) apply to all spectra. Otherwise
|
---|
| 249 | apply only to the selected (beam/pol/if)spectra only
|
---|
[113] | 250 | Example:
|
---|
| 251 | none
|
---|
| 252 | """
|
---|
[172] | 253 | if not insitu:
|
---|
[180] | 254 | from asap._asap import smooth as _smooth
|
---|
| 255 | return scantable(_smooth(scan,kernel,width,all))
|
---|
[172] | 256 | else:
|
---|
[180] | 257 | from asap._asap import smooth_insitu as _smooth
|
---|
| 258 | _smooth(scan,kernel,width,all)
|
---|
[172] | 259 | return
|
---|
[113] | 260 |
|
---|
| 261 | def poly_baseline(scan, mask=None, order=0):
|
---|
| 262 | """
|
---|
[160] | 263 | Return a scan which has been baselined (all rows) by a polynomial.
|
---|
[113] | 264 | Parameters:
|
---|
| 265 | scan: a scantable
|
---|
| 266 | mask: an optional mask
|
---|
| 267 | order: the order of the polynomial (default is 0)
|
---|
| 268 | Example:
|
---|
| 269 | # return a scan baselined by a third order polynomial,
|
---|
| 270 | # not using a mask
|
---|
| 271 | bscan = poly_baseline(scan, order=3)
|
---|
| 272 | """
|
---|
| 273 | from asap.asapfitter import fitter
|
---|
| 274 | if mask is None:
|
---|
| 275 | from numarray import ones
|
---|
| 276 | mask = tuple(ones(scan.nchan()))
|
---|
| 277 | f = fitter()
|
---|
| 278 | f._verbose(True)
|
---|
| 279 | f.set_scan(scan, mask)
|
---|
| 280 | f.set_function(poly=order)
|
---|
| 281 | sf = f.auto_fit()
|
---|
| 282 | return sf
|
---|