source: trunk/python/asapmath.py@ 421

Last change on this file since 421 was 411, checked in by mar637, 20 years ago

Added handling of environment variables throughout.

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