source: trunk/python/asapmath.py@ 508

Last change on this file since 508 was 506, checked in by kil064, 20 years ago

add function rotate_linpolphase

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 19.3 KB
RevLine 
[101]1from scantable import scantable
[258]2from asap import rcParams
[101]3
[143]4def average_time(*args, **kwargs):
[101]5 """
[113]6 Return the (time) average of a scan or list of scans. [in channels only]
[305]7 The cursor of the output scan is set to 0
[113]8 Parameters:
9 one scan or comma separated scans
[143]10 mask: an optional mask (only used for 'var' and 'tsys' weighting)
[424]11 scanav: True (default) averages each scan separately
12 False averages all scans together,
[143]13 weight: Weighting scheme. 'none' (default), 'var' (variance
14 weighted), 'tsys'
[113]15 Example:
16 # return a time averaged scan from scana and scanb
17 # without using a mask
[129]18 scanav = average_time(scana,scanb)
[113]19 # return the (time) averaged scan, i.e. the average of
20 # all correlator cycles
21 scanav = average_time(scan)
[143]22
[101]23 """
[424]24 scanAv = True
[143]25 if kwargs.has_key('scanav'):
26 scanAv = kwargs.get('scanav')
27 weight = 'none'
28 if kwargs.has_key('weight'):
29 weight = kwargs.get('weight')
30 mask = ()
31 if kwargs.has_key('mask'):
32 mask = kwargs.get('mask')
[489]33 varlist = vars()
[143]34 lst = tuple(args)
[489]35 del varlist["kwargs"]
36 varlist["args"] = "%d scantables" % len(lst)
37 # need special formatting her for history...
38
[143]39 from asap._asap import average as _av
[113]40 for s in lst:
[101]41 if not isinstance(s,scantable):
42 print "Please give a list of scantables"
43 return
[489]44 s = scantable(_av(lst, mask, scanAv, weight))
45 s._add_history("average_time",varlist)
46 return s
[101]47
[245]48def quotient(source, reference, preserve=True):
[101]49 """
[246]50 Return the quotient of a 'source' (signal) scan and a 'reference' scan.
51 The reference can have just one row, even if the signal has many. Otherwise
52 they must have the same number of rows.
[305]53 The cursor of the output scan is set to 0
[101]54 Parameters:
55 source: the 'on' scan
56 reference: the 'off' scan
[245]57 preserve: you can preserve (default) the continuum or
58 remove it. The equations used are
59 preserve - Output = Tref * (sig/ref) - Tref
60 remove - Output = Tref * (sig/ref) - Tsig
[101]61 """
62 from asap._asap import quotient as _quot
[245]63 return scantable(_quot(source, reference, preserve))
[101]64
[296]65def simple_math(left, right, op='add', tsys=True):
[242]66 """
67 Apply simple mathematical binary operations to two
68 scan tables, returning the result in a new scan table.
69 The operation is applied to both the correlations and the TSys data
[305]70 The cursor of the output scan is set to 0
[242]71 Parameters:
72 left: the 'left' scan
73 right: the 'right' scan
74 op: the operation: 'add' (default), 'sub', 'mul', 'div'
[296]75 tsys: if True (default) then apply the operation to Tsys
76 as well as the data
[242]77 """
[489]78 varlist = vars()
[258]79 if not isinstance(left,scantable) and not isinstance(right,scantable):
80 print "Please provide two scantables as input"
81 return
[242]82 from asap._asap import b_operate as _bop
[489]83 s = scantable(_bop(left, right, op, tsys))
84 s._add_history("simple_math", varlist)
85 return s
[242]86
[296]87def scale(scan, factor, insitu=None, allaxes=None, tsys=True):
[101]88 """
89 Return a scan where all spectra are scaled by the give 'factor'
90 Parameters:
91 scan: a scantable
[113]92 factor: the scaling factor
[258]93 insitu: if False a new scantable is returned.
[150]94 Otherwise, the scaling is done in-situ
[258]95 The default is taken from .asaprc (False)
96 allaxes: if True apply to all spectra. Otherwise
97 apply only to the selected (beam/pol/if)spectra only.
[435]98 The default is taken from .asaprc (True if none)
[296]99 tsys: if True (default) then apply the operation to Tsys
100 as well as the data
[101]101 """
[258]102 if allaxes is None: allaxes = rcParams['scantable.allaxes']
103 if insitu is None: insitu = rcParams['insitu']
[489]104 varlist = vars()
[141]105 if not insitu:
106 from asap._asap import scale as _scale
[489]107 s = scantable(_scale(scan, factor, allaxes, tsys))
108 s._add_history("scale",varlist)
109 return s
[141]110 else:
111 from asap._asap import scale_insitu as _scale
[296]112 _scale(scan, factor, allaxes, tsys)
[489]113 scan._add_history("scale",varlist)
[141]114 return
115
[258]116def add(scan, offset, insitu=None, allaxes=None):
[113]117 """
[150]118 Return a scan where all spectra have the offset added
[113]119 Parameters:
120 scan: a scantable
[150]121 offset: the offset
[258]122 insitu: if False a new scantable is returned.
123 Otherwise, the scaling is done in-situ
124 The default is taken from .asaprc (False)
125 allaxes: if True apply to all spectra. Otherwise
[150]126 apply only to the selected (beam/pol/if)spectra only
[435]127 The default is taken from .asaprc (True if none)
[113]128 """
[258]129 if allaxes is None: allaxes = rcParams['scantable.allaxes']
130 if insitu is None: insitu = rcParams['insitu']
[150]131 if not insitu:
132 from asap._asap import add as _add
[258]133 return scantable(_add(scan, offset, allaxes))
[150]134 else:
135 from asap._asap import add_insitu as _add
[258]136 _add(scan, offset, allaxes)
[150]137 return
138
[489]139def convert_flux(scan, jyperk=None, eta=None, d=None, insitu=None,
140 allaxes=None):
[224]141 """
142 Return a scan where all spectra are converted to either Jansky or Kelvin
[359]143 depending upon the flux units of the scan table. By default the
144 function tries to look the values up internally. If it can't find
145 them (or if you want to over-ride), you must specify EITHER jyperk
146 OR eta (and D which it will try to look up also if you don't
147 set it). jyperk takes precedence if you set both.
[224]148 Parameters:
149 scan: a scantable
[359]150 jyperk: the Jy / K conversion factor
151 eta: the aperture efficiency
152 d: the geomtric diameter (metres)
[258]153 insitu: if False a new scantable is returned.
154 Otherwise, the scaling is done in-situ
155 The default is taken from .asaprc (False)
156 allaxes: if True apply to all spectra. Otherwise
[224]157 apply only to the selected (beam/pol/if)spectra only
[435]158 The default is taken from .asaprc (True if none)
[224]159 """
[258]160 if allaxes is None: allaxes = rcParams['scantable.allaxes']
161 if insitu is None: insitu = rcParams['insitu']
[489]162 varlist = vars()
[359]163 if jyperk is None: jyperk = -1.0
164 if d is None: d = -1.0
165 if eta is None: eta = -1.0
[224]166 if not insitu:
167 from asap._asap import convertflux as _convert
[489]168 s = scantable(_convert(scan, d, eta, jyperk, allaxes))
169 s._add_history("convert_flux", varlist)
170 return s
[224]171 else:
172 from asap._asap import convertflux_insitu as _convert
[359]173 _convert(scan, d, eta, jyperk, allaxes)
[489]174 scan._add_history("convert_flux", varlist)
[224]175 return
[229]176
[411]177def gain_el(scan, poly=None, filename="", method="linear",
178 insitu=None, allaxes=None):
[229]179 """
[242]180 Return a scan after applying a gain-elevation correction. The correction
181 can be made via either a polynomial or a table-based interpolation
182 (and extrapolation if necessary).
183 You specify polynomial coefficients, an ascii table or neither.
184 If you specify neither, then a polynomial correction will be made
185 with built in coefficients known for certain telescopes (an error will
[481]186 occur if the instrument is not known). The data and Tsys are *divided*
[479]187 by the scaling factors.
[229]188 Parameters:
189 scan: a scantable
[258]190 poly: Polynomial coefficients (default None) to compute a
191 gain-elevation correction as a function of
192 elevation (in degrees).
[242]193 filename: The name of an ascii file holding correction factors.
194 The first row of the ascii file must give the column
195 names and these MUST include columns
[258]196 "ELEVATION" (degrees) and "FACTOR" (multiply data by this)
197 somewhere.
198 The second row must give the data type of the column. Use
199 'R' for Real and 'I' for Integer. An example file
[479]200 would be (actual factors are arbitrary) :
[229]201
[242]202 TIME ELEVATION FACTOR
203 R R R
[479]204 0.1 0 0.8
205 0.2 20 0.85
206 0.3 40 0.9
207 0.4 60 0.85
208 0.5 80 0.8
209 0.6 90 0.75
[242]210 method: Interpolation method when correcting from a table. Values
211 are "nearest", "linear" (default), "cubic" and "spline"
[258]212 insitu: if False a new scantable is returned.
213 Otherwise, the scaling is done in-situ
214 The default is taken from .asaprc (False)
215 allaxes: if True apply to all spectra. Otherwise
216 apply only to the selected (beam/pol/if) spectra only
[435]217 The default is taken from .asaprc (True if none)
[229]218 """
[258]219 if allaxes is None: allaxes = rcParams['scantable.allaxes']
[489]220 if insitu is None: insitu = rcParams['insitu']
221 varlist = vars()
[242]222 if poly is None:
223 poly = ()
[411]224 from os.path import expandvars
225 filename = expandvars(filename)
[229]226 if not insitu:
227 from asap._asap import gainel as _gainEl
[489]228 s = scantable(_gainEl(scan, poly, filename, method, allaxes))
229 s._add_history("gain_el", varlist)
230 return s
[229]231 else:
232 from asap._asap import gainel_insitu as _gainEl
[258]233 _gainEl(scan, poly, filename, method, allaxes)
[489]234 scan._add_history("gain_el", varlist)
[229]235 return
[224]236
[399]237def freq_align(scan, reftime=None, method='cubic', perif=False, insitu=None):
[269]238 """
[313]239 Return a scan where all rows have been aligned in frequency. The
240 alignment frequency frame (e.g. LSRK) is that set by function
241 set_freqframe.
[269]242 scan: a scantable
[273]243 reftime: reference time to align at. By default, the time of
244 the first row of data is used.
[319]245 method: Interpolation method for regridding the spectra. Choose
246 from "nearest", "linear", "cubic" (default) and "spline"
[399]247 perif: Generate aligners per freqID (no doppler tracking) or
248 per IF (scan-based doppler tracking)
[269]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 """
[489]253 if insitu is None: insitu = rcParams['insitu']
254 varlist = vars()
[273]255 if reftime is None: reftime = ''
[399]256 perfreqid = not perif
[269]257 if not insitu:
[313]258 from asap._asap import freq_align as _align
[489]259 s = scantable(_align(scan, reftime, method, perfreqid))
260 s._add_history("freq_align", varlist)
261 return s
[269]262 else:
[313]263 from asap._asap import freq_align_insitu as _align
[399]264 _align(scan, reftime, method, perfreqid)
[489]265 scan._add_history("freq_align", varlist)
[269]266 return
267
[258]268def opacity(scan, tau, insitu=None, allaxes=None):
[242]269 """
[481]270 Return a scan after applying an opacity correction. The data
271 and Tsys are multiplied by the correction factor.
[242]272 Parameters:
273 scan: a scantable
274 tau: Opacity from which the correction factor is exp(tau*ZD)
275 where ZD is the zenith-distance
[258]276 insitu: if False a new scantable is returned.
277 Otherwise, the scaling is done in-situ
278 The default is taken from .asaprc (False)
279 allaxes: if True apply to all spectra. Otherwise
[242]280 apply only to the selected (beam/pol/if)spectra only
[435]281 The default is taken from .asaprc (True if none)
[242]282 """
[258]283 if allaxes is None: allaxes = rcParams['scantable.allaxes']
284 if insitu is None: insitu = rcParams['insitu']
[489]285 varlist = vars()
[242]286 if not insitu:
287 from asap._asap import opacity as _opacity
[489]288 s = scantable(_opacity(scan, tau, allaxes))
289 s._add_history("opacity", varlist)
290 return s
[242]291 else:
292 from asap._asap import opacity_insitu as _opacity
[258]293 _opacity(scan, tau, allaxes)
[489]294 scan._add_history("opacity", varlist)
[242]295 return
296
[258]297def bin(scan, width=5, insitu=None):
[101]298 """
[167]299 Return a scan where all spectra have been binned up
[172]300 width: The bin width (default=5) in pixels
[258]301 insitu: if False a new scantable is returned.
302 Otherwise, the scaling is done in-situ
303 The default is taken from .asaprc (False)
[101]304 """
[258]305 if insitu is None: insitu = rcParams['insitu']
[489]306 varlist = vars()
[167]307 if not insitu:
308 from asap._asap import bin as _bin
[489]309 s = scantable(_bin(scan, width))
310 s._add_history("bin",varlist)
311 return s
[167]312 else:
313 from asap._asap import bin_insitu as _bin
314 _bin(scan, width)
[489]315 scan._add_history("bin",varlist)
[167]316 return
[113]317
[301]318def resample(scan, width=5, method='cubic', insitu=None):
319 """
320 Return a scan where all spectra have been binned up
321 width: The bin width (default=5) in pixels
322 method: Interpolation method when correcting from a table. Values
323 are "nearest", "linear", "cubic" (default) and "spline"
324 insitu: if False a new scantable is returned.
325 Otherwise, the scaling is done in-situ
326 The default is taken from .asaprc (False)
327 """
328 if insitu is None: insitu = rcParams['insitu']
[489]329 varlist = vars()
[301]330 if not insitu:
331 from asap._asap import resample as _resample
[489]332 s = scantable(_resample(scan, method, width))
333 s._add_history("resample",varlist)
334 return s
[301]335 else:
336 from asap._asap import resample_insitu as _resample
337 _resample(scan, method, width)
[489]338 scan._add_history("resample",varlist)
[301]339 return
340
[316]341def average_pol(scan, mask=None, weight='none', insitu=None):
[113]342 """
343 Average the Polarisations together.
[305]344 The polarisation cursor of the output scan is set to 0
[113]345 Parameters:
[172]346 scan: The scantable
347 mask: An optional mask defining the region, where the
348 averaging will be applied. The output will have all
349 specified points masked.
[316]350 weight: Weighting scheme. 'none' (default), or 'var' (variance
351 weighted)
[258]352 insitu: if False a new scantable is returned.
353 Otherwise, the scaling is done in-situ
354 The default is taken from .asaprc (False)
[113]355 Example:
356 polav = average_pols(myscan)
357 """
[489]358 if insitu is None: insitu = rcParams['insitu']
359 varlist = vars()
360
[113]361 if mask is None:
[166]362 mask = ()
363 if not insitu:
364 from asap._asap import averagepol as _avpol
[489]365 s = scantable(_avpol(scan, mask, weight))
366 s._add_history("average_pol",varlist)
367 return s
[166]368 else:
369 from asap._asap import averagepol_insitu as _avpol
[316]370 _avpol(scan, mask, weight)
[489]371 scan._add_history("average_pol",varlist)
[166]372 return
[113]373
[258]374def smooth(scan, kernel="hanning", width=5.0, insitu=None, allaxes=None):
[113]375 """
[180]376 Smooth the spectrum by the specified kernel (conserving flux).
[113]377 Parameters:
[172]378 scan: The input scan
[180]379 kernel: The type of smoothing kernel. Select from
380 'hanning' (default), 'gaussian' and 'boxcar'.
381 The first three characters are sufficient.
382 width: The width of the kernel in pixels. For hanning this is
383 ignored otherwise it defauls to 5 pixels.
384 For 'gaussian' it is the Full Width Half
385 Maximum. For 'boxcar' it is the full width.
[258]386 insitu: if False a new scantable is returned.
[172]387 Otherwise, the scaling is done in-situ
[258]388 The default is taken from .asaprc (False)
389 allaxes: If True (default) apply to all spectra. Otherwise
[180]390 apply only to the selected (beam/pol/if)spectra only
[435]391 The default is taken from .asaprc (True if none)
[113]392 Example:
393 none
394 """
[258]395 if allaxes is None: allaxes = rcParams['scantable.allaxes']
396 if insitu is None: insitu = rcParams['insitu']
[489]397 varlist = vars()
[172]398 if not insitu:
[180]399 from asap._asap import smooth as _smooth
[489]400 s = scantable(_smooth(scan,kernel,width,allaxes))
401 s._add_history("smooth", varlist)
402 return s
[172]403 else:
[180]404 from asap._asap import smooth_insitu as _smooth
[258]405 _smooth(scan,kernel,width,allaxes)
[489]406 scan._add_history("smooth", varlist)
[172]407 return
[113]408
[258]409def poly_baseline(scan, mask=None, order=0, insitu=None):
[113]410 """
[160]411 Return a scan which has been baselined (all rows) by a polynomial.
[113]412 Parameters:
413 scan: a scantable
414 mask: an optional mask
415 order: the order of the polynomial (default is 0)
[258]416 insitu: if False a new scantable is returned.
417 Otherwise, the scaling is done in-situ
418 The default is taken from .asaprc (False)
[113]419 Example:
420 # return a scan baselined by a third order polynomial,
421 # not using a mask
422 bscan = poly_baseline(scan, order=3)
423 """
[489]424 if insitu is None: insitu = rcParams['insitu']
425 varlist = vars()
[113]426 from asap.asapfitter import fitter
427 if mask is None:
428 from numarray import ones
429 mask = tuple(ones(scan.nchan()))
430 f = fitter()
431 f._verbose(True)
432 f.set_scan(scan, mask)
433 f.set_function(poly=order)
[258]434 sf = f.auto_fit(insitu)
[489]435 sf._add_history("poly_baseline", varlist)
[113]436 return sf
[458]437
[489]438def rotate_xyphase(scan, angle, allaxes=None):
[458]439 """
440 Rotate the phase of the XY correlation. This is always done in situ
441 in the data. So if you call this function more than once
442 then each call rotates the phase further.
443 Parameters:
444 angle: The angle (degrees) to rotate (add) by.
445 allaxes: If True apply to all spectra. Otherwise
446 apply only to the selected (beam/pol/if)spectra only.
447 The default is taken from .asaprc (True if none)
448 Examples:
449 rotate_xyphase(scan, 2.3)
450 """
451 if allaxes is None: allaxes = rcParams['scantable.allaxes']
[489]452 varlist = vars()
[506]453 from asap._asap import _rotate_xyphase as _rotate
454 _rotate(scan, angle, allaxes)
455 scan._add_history("rotate_xyphase", varlist)
[458]456 return
[506]457
458def rotate_linpolphase(scan, angle, allaxes=None):
459 """
460 Rotate the phase of the complex polarization O=Q+iU correlation.
461 This is always done in situ in the raw data. So if you call this
462 function more than once then each call rotates the phase further.
463 Parameters:
464 angle: The angle (degrees) to rotate (add) by.
465 allaxes: If True apply to all spectra. Otherwise
466 apply only to the selected (beam/pol/if)spectra only.
467 The default is taken from .asaprc (True if none)
468 Examples:
469 rotate_linpolphase(scan, 2.3)
470 """
471 if allaxes is None: allaxes = rcParams['scantable.allaxes']
472 varlist = vars()
473 from asap._asap import _rotate_linpolphase as _rotate
474 _rotate(scan, angle, allaxes)
475 scan._add_history("rotate_linpolphase", varlist)
476 return
477
Note: See TracBrowser for help on using the repository browser.