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', tsys=True):
|
---|
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 | tsys: if True (default) then apply the operation to Tsys
|
---|
69 | as well as the data
|
---|
70 | """
|
---|
71 | if not isinstance(left,scantable) and not isinstance(right,scantable):
|
---|
72 | print "Please provide two scantables as input"
|
---|
73 | return
|
---|
74 | from asap._asap import b_operate as _bop
|
---|
75 | return scantable(_bop(left, right, op, tsys))
|
---|
76 |
|
---|
77 | def scale(scan, factor, insitu=None, allaxes=None, tsys=True):
|
---|
78 | """
|
---|
79 | Return a scan where all spectra are scaled by the give 'factor'
|
---|
80 | Parameters:
|
---|
81 | scan: a scantable
|
---|
82 | factor: the scaling factor
|
---|
83 | insitu: if False a new scantable is returned.
|
---|
84 | Otherwise, the scaling is done in-situ
|
---|
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)
|
---|
89 | tsys: if True (default) then apply the operation to Tsys
|
---|
90 | as well as the data
|
---|
91 | """
|
---|
92 | if allaxes is None: allaxes = rcParams['scantable.allaxes']
|
---|
93 | if insitu is None: insitu = rcParams['insitu']
|
---|
94 | if not insitu:
|
---|
95 | from asap._asap import scale as _scale
|
---|
96 | return scantable(_scale(scan, factor, allaxes, tsys))
|
---|
97 | else:
|
---|
98 | from asap._asap import scale_insitu as _scale
|
---|
99 | _scale(scan, factor, allaxes, tsys)
|
---|
100 | return
|
---|
101 |
|
---|
102 |
|
---|
103 | def add(scan, offset, insitu=None, allaxes=None):
|
---|
104 | """
|
---|
105 | Return a scan where all spectra have the offset added
|
---|
106 | Parameters:
|
---|
107 | scan: a scantable
|
---|
108 | offset: the offset
|
---|
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
|
---|
113 | apply only to the selected (beam/pol/if)spectra only
|
---|
114 | The default is taken from .asaprc (True)
|
---|
115 | """
|
---|
116 | if allaxes is None: allaxes = rcParams['scantable.allaxes']
|
---|
117 | if insitu is None: insitu = rcParams['insitu']
|
---|
118 | if not insitu:
|
---|
119 | from asap._asap import add as _add
|
---|
120 | return scantable(_add(scan, offset, allaxes))
|
---|
121 | else:
|
---|
122 | from asap._asap import add_insitu as _add
|
---|
123 | _add(scan, offset, allaxes)
|
---|
124 | return
|
---|
125 |
|
---|
126 | def convert_flux(scan, area, eta=1.0, insitu=None, allaxes=None):
|
---|
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)
|
---|
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
|
---|
138 | apply only to the selected (beam/pol/if)spectra only
|
---|
139 | The default is taken from .asaprc (True)
|
---|
140 | """
|
---|
141 | if allaxes is None: allaxes = rcParams['scantable.allaxes']
|
---|
142 | if insitu is None: insitu = rcParams['insitu']
|
---|
143 | if not insitu:
|
---|
144 | from asap._asap import convertflux as _convert
|
---|
145 | return scantable(_convert(scan, area, eta, allaxes))
|
---|
146 | else:
|
---|
147 | from asap._asap import convertflux_insitu as _convert
|
---|
148 | _convert(scan, area, eta, allaxes)
|
---|
149 | return
|
---|
150 |
|
---|
151 | def gain_el(scan, poly=None, filename="", method="linear", insitu=None, allaxes=None):
|
---|
152 | """
|
---|
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).
|
---|
160 | Parameters:
|
---|
161 | scan: a scantable
|
---|
162 | poly: Polynomial coefficients (default None) to compute a
|
---|
163 | gain-elevation correction as a function of
|
---|
164 | elevation (in degrees).
|
---|
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
|
---|
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:
|
---|
173 |
|
---|
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"
|
---|
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)
|
---|
190 | """
|
---|
191 | if allaxes is None: allaxes = rcParams['scantable.allaxes']
|
---|
192 | if poly is None:
|
---|
193 | poly = ()
|
---|
194 | if insitu is None: insitu = rcParams['insitu']
|
---|
195 | if not insitu:
|
---|
196 | from asap._asap import gainel as _gainEl
|
---|
197 | return scantable(_gainEl(scan, poly, filename, method, allaxes))
|
---|
198 | else:
|
---|
199 | from asap._asap import gainel_insitu as _gainEl
|
---|
200 | _gainEl(scan, poly, filename, method, allaxes)
|
---|
201 | return
|
---|
202 |
|
---|
203 | def align(scan, reftime=None, insitu=None):
|
---|
204 | """
|
---|
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)
|
---|
207 | are those set by functions set_unit and set_freqframe.
|
---|
208 | scan: a scantable
|
---|
209 | reftime: reference time to align at. By default, the time of
|
---|
210 | the first row of data is used.
|
---|
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 | """
|
---|
215 | if reftime is None: reftime = ''
|
---|
216 | if insitu is None: insitu = rcParams['insitu']
|
---|
217 | if not insitu:
|
---|
218 | from asap._asap import align as _align
|
---|
219 | return scantable(_align(scan, reftime))
|
---|
220 | else:
|
---|
221 | from asap._asap import align_insitu as _align
|
---|
222 | _align(scan, reftime)
|
---|
223 | return
|
---|
224 |
|
---|
225 | def opacity(scan, tau, insitu=None, allaxes=None):
|
---|
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
|
---|
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
|
---|
236 | apply only to the selected (beam/pol/if)spectra only
|
---|
237 | The default is taken from .asaprc (True)
|
---|
238 | """
|
---|
239 | if allaxes is None: allaxes = rcParams['scantable.allaxes']
|
---|
240 | if insitu is None: insitu = rcParams['insitu']
|
---|
241 | if not insitu:
|
---|
242 | from asap._asap import opacity as _opacity
|
---|
243 | return scantable(_opacity(scan, tau, allaxes))
|
---|
244 | else:
|
---|
245 | from asap._asap import opacity_insitu as _opacity
|
---|
246 | _opacity(scan, tau, allaxes)
|
---|
247 | return
|
---|
248 |
|
---|
249 | def bin(scan, width=5, insitu=None):
|
---|
250 | """
|
---|
251 | Return a scan where all spectra have been binned up
|
---|
252 | width: The bin width (default=5) in pixels
|
---|
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)
|
---|
256 | """
|
---|
257 | if insitu is None: insitu = rcParams['insitu']
|
---|
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
|
---|
265 |
|
---|
266 | def resample(scan, width=5, method='cubic', insitu=None):
|
---|
267 | """
|
---|
268 | Return a scan where all spectra have been binned up
|
---|
269 | width: The bin width (default=5) in pixels
|
---|
270 | method: Interpolation method when correcting from a table. Values
|
---|
271 | are "nearest", "linear", "cubic" (default) and "spline"
|
---|
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)
|
---|
275 | """
|
---|
276 | if insitu is None: insitu = rcParams['insitu']
|
---|
277 | if not insitu:
|
---|
278 | from asap._asap import resample as _resample
|
---|
279 | return scantable(_resample(scan, method, width))
|
---|
280 | else:
|
---|
281 | from asap._asap import resample_insitu as _resample
|
---|
282 | _resample(scan, method, width)
|
---|
283 | return
|
---|
284 |
|
---|
285 | def average_pol(scan, mask=None, insitu=None):
|
---|
286 | """
|
---|
287 | Average the Polarisations together.
|
---|
288 | Parameters:
|
---|
289 | scan: The scantable
|
---|
290 | mask: An optional mask defining the region, where the
|
---|
291 | averaging will be applied. The output will have all
|
---|
292 | specified points masked.
|
---|
293 | insitu: if False a new scantable is returned.
|
---|
294 | Otherwise, the scaling is done in-situ
|
---|
295 | The default is taken from .asaprc (False)
|
---|
296 | Example:
|
---|
297 | polav = average_pols(myscan)
|
---|
298 | """
|
---|
299 | if mask is None:
|
---|
300 | mask = ()
|
---|
301 | if insitu is None: insitu = rcParams['insitu']
|
---|
302 | if not insitu:
|
---|
303 | from asap._asap import averagepol as _avpol
|
---|
304 | return scantable(_avpol(scan, mask))
|
---|
305 | else:
|
---|
306 | from asap._asap import averagepol_insitu as _avpol
|
---|
307 | _avpol(scan, mask)
|
---|
308 | return
|
---|
309 |
|
---|
310 | def smooth(scan, kernel="hanning", width=5.0, insitu=None, allaxes=None):
|
---|
311 | """
|
---|
312 | Smooth the spectrum by the specified kernel (conserving flux).
|
---|
313 | Parameters:
|
---|
314 | scan: The input scan
|
---|
315 | kernel: The type of smoothing kernel. Select from
|
---|
316 | 'hanning' (default), 'gaussian' and 'boxcar'.
|
---|
317 | The first three characters are sufficient.
|
---|
318 | width: The width of the kernel in pixels. For hanning this is
|
---|
319 | ignored otherwise it defauls to 5 pixels.
|
---|
320 | For 'gaussian' it is the Full Width Half
|
---|
321 | Maximum. For 'boxcar' it is the full width.
|
---|
322 | insitu: if False a new scantable is returned.
|
---|
323 | Otherwise, the scaling is done in-situ
|
---|
324 | The default is taken from .asaprc (False)
|
---|
325 | allaxes: If True (default) apply to all spectra. Otherwise
|
---|
326 | apply only to the selected (beam/pol/if)spectra only
|
---|
327 | The default is taken from .asaprc (True)
|
---|
328 | Example:
|
---|
329 | none
|
---|
330 | """
|
---|
331 | if allaxes is None: allaxes = rcParams['scantable.allaxes']
|
---|
332 | if insitu is None: insitu = rcParams['insitu']
|
---|
333 | if not insitu:
|
---|
334 | from asap._asap import smooth as _smooth
|
---|
335 | return scantable(_smooth(scan,kernel,width,allaxes))
|
---|
336 | else:
|
---|
337 | from asap._asap import smooth_insitu as _smooth
|
---|
338 | _smooth(scan,kernel,width,allaxes)
|
---|
339 | return
|
---|
340 |
|
---|
341 | def poly_baseline(scan, mask=None, order=0, insitu=None):
|
---|
342 | """
|
---|
343 | Return a scan which has been baselined (all rows) by a polynomial.
|
---|
344 | Parameters:
|
---|
345 | scan: a scantable
|
---|
346 | mask: an optional mask
|
---|
347 | order: the order of the polynomial (default is 0)
|
---|
348 | insitu: if False a new scantable is returned.
|
---|
349 | Otherwise, the scaling is done in-situ
|
---|
350 | The default is taken from .asaprc (False)
|
---|
351 | Example:
|
---|
352 | # return a scan baselined by a third order polynomial,
|
---|
353 | # not using a mask
|
---|
354 | bscan = poly_baseline(scan, order=3)
|
---|
355 | """
|
---|
356 | from asap.asapfitter import fitter
|
---|
357 | if mask is None:
|
---|
358 | from numarray import ones
|
---|
359 | mask = tuple(ones(scan.nchan()))
|
---|
360 | f = fitter()
|
---|
361 | f._verbose(True)
|
---|
362 | f.set_scan(scan, mask)
|
---|
363 | f.set_function(poly=order)
|
---|
364 | sf = f.auto_fit(insitu)
|
---|
365 | return sf
|
---|