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