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