source: trunk/python/asapmath.py@ 353

Last change on this file since 353 was 319, checked in by kil064, 20 years ago

function 'freq_align' now takes interpolation method string

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