source: trunk/python/asapmath.py@ 243

Last change on this file since 243 was 242, checked in by kil064, 20 years ago

add b_operate

gain_el (polynomials)
opacity

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.5 KB
RevLine 
[101]1from scantable import scantable
2
[143]3def average_time(*args, **kwargs):
[101]4 """
[113]5 Return the (time) average of a scan or list of scans. [in channels only]
6 Parameters:
7 one scan or comma separated scans
[143]8 mask: an optional mask (only used for 'var' and 'tsys' weighting)
9 scanav: False (default) averages all scans together,
10 True averages each scan separately
11 weight: Weighting scheme. 'none' (default), 'var' (variance
12 weighted), 'tsys'
[113]13 Example:
14 # return a time averaged scan from scana and scanb
15 # without using a mask
[129]16 scanav = average_time(scana,scanb)
[113]17 # return the (time) averaged scan, i.e. the average of
18 # all correlator cycles
19 scanav = average_time(scan)
[143]20
[101]21 """
[143]22 scanAv = False
23 if kwargs.has_key('scanav'):
24 scanAv = kwargs.get('scanav')
25#
26 weight = 'none'
27 if kwargs.has_key('weight'):
28 weight = kwargs.get('weight')
29#
30 mask = ()
31 if kwargs.has_key('mask'):
32 mask = kwargs.get('mask')
33#
34 lst = tuple(args)
35 from asap._asap import average as _av
[113]36 for s in lst:
[101]37 if not isinstance(s,scantable):
38 print "Please give a list of scantables"
39 return
[143]40 return scantable(_av(lst, mask, scanAv, weight))
[101]41
42def quotient(source, reference):
43 """
44 Return the quotient of a 'source' scan and a 'reference' scan
45 Parameters:
46 source: the 'on' scan
47 reference: the 'off' scan
48 """
49 from asap._asap import quotient as _quot
50 return scantable(_quot(source, reference))
51
[242]52def b_operate(left, right, op='add'):
53 """
54 Apply simple mathematical binary operations to two
55 scan tables, returning the result in a new scan table.
56 The operation is applied to both the correlations and the TSys data
57 Parameters:
58 left: the 'left' scan
59 right: the 'right' scan
60 op: the operation: 'add' (default), 'sub', 'mul', 'div'
61 """
62 from asap._asap import b_operate as _bop
63 return scantable(_bop(left, right, op))
64
[150]65def scale(scan, factor, insitu=False, all=True):
[101]66 """
67 Return a scan where all spectra are scaled by the give 'factor'
68 Parameters:
69 scan: a scantable
[113]70 factor: the scaling factor
[150]71 insitu: if False (default) a new scantable is returned.
72 Otherwise, the scaling is done in-situ
73 all: if True (default) apply to all spectra. Otherwise
74 apply only to the selected (beam/pol/if)spectra only
[101]75 """
[141]76 if not insitu:
77 from asap._asap import scale as _scale
[150]78 return scantable(_scale(scan, factor, all))
[141]79 else:
80 from asap._asap import scale_insitu as _scale
[150]81 _scale(scan, factor, all)
[141]82 return
83
[101]84
[150]85def add(scan, offset, insitu=False, all=True):
[113]86 """
[150]87 Return a scan where all spectra have the offset added
[113]88 Parameters:
89 scan: a scantable
[150]90 offset: the offset
91 insitu: if False (default) a new scantable is returned.
92 Otherwise, the addition is done in-situ
93 all: if True (default) apply to all spectra. Otherwise
94 apply only to the selected (beam/pol/if)spectra only
[113]95 """
[150]96 if not insitu:
97 from asap._asap import add as _add
98 return scantable(_add(scan, offset, all))
99 else:
100 from asap._asap import add_insitu as _add
101 _add(scan, offset, all)
102 return
103
[225]104def convert_flux(scan, area, eta=1.0, insitu=False, all=True):
[224]105 """
106 Return a scan where all spectra are converted to either Jansky or Kelvin
107 depending upon the flux units of the scan table.
108 Parameters:
109 scan: a scantable
110 area: the illuminated area of the telescope (m**2)
111 eta: The efficiency of the telescope (default 1.0)
112 insitu: if False (default) a new scantable is returned.
113 Otherwise, the conversion is done in-situ
114 all: if True (default) apply to all spectra. Otherwise
115 apply only to the selected (beam/pol/if)spectra only
116 """
117 if not insitu:
118 from asap._asap import convertflux as _convert
119 return scantable(_convert(scan, area, eta, all))
120 else:
121 from asap._asap import convertflux_insitu as _convert
122 _convert(scan, area, eta, all)
123 return
[229]124
[242]125def gain_el(scan, poly=None, filename="", method="linear", insitu=False, all=True):
[229]126 """
[242]127 Return a scan after applying a gain-elevation correction. The correction
128 can be made via either a polynomial or a table-based interpolation
129 (and extrapolation if necessary).
130 You specify polynomial coefficients, an ascii table or neither.
131 If you specify neither, then a polynomial correction will be made
132 with built in coefficients known for certain telescopes (an error will
133 occur if the instrument is not known).
[229]134 Parameters:
135 scan: a scantable
[242]136 poly: Polynomial coefficients (default None) to compute a gain-elevation
137 correction as a function of elevation (in degrees).
138 filename: The name of an ascii file holding correction factors.
139 The first row of the ascii file must give the column
140 names and these MUST include columns
141 "ELEVATION" (degrees) and "FACTOR" (multiply data by this) somewhere.
[229]142 The second row must give the data type of the column. Use 'R' for
143 Real and 'I' for Integer. An example file would be:
144
[242]145 TIME ELEVATION FACTOR
146 R R R
147 0.1 0 1.5
148 0.2 20 1.4
149 0.3 40 1.3
150 0.4 60 1.2
151 0.5 80 1.1
152 0.6 90 1.0
153 method: Interpolation method when correcting from a table. Values
154 are "nearest", "linear" (default), "cubic" and "spline"
[229]155 insitu: if False (default) a new scantable is returned.
156 Otherwise, the conversion is done in-situ
157 all: if True (default) apply to all spectra. Otherwise
158 apply only to the selected (beam/pol/if)spectra only
159 """
[242]160 if poly is None:
161 poly = ()
[229]162 if not insitu:
163 from asap._asap import gainel as _gainEl
[242]164 return scantable(_gainEl(scan, poly, filename, method, all))
[229]165 else:
166 from asap._asap import gainel_insitu as _gainEl
[242]167 _gainEl(scan, poly, filename, method, all)
[229]168 return
[224]169
[242]170def opacity(scan, tau, insitu=False, all=True):
171 """
172 Return a scan after applying an opacity correction.
173 Parameters:
174 scan: a scantable
175 tau: Opacity from which the correction factor is exp(tau*ZD)
176 where ZD is the zenith-distance
177 insitu: if False (default) a new scantable is returned.
178 Otherwise, the conversion is done in-situ
179 all: if True (default) apply to all spectra. Otherwise
180 apply only to the selected (beam/pol/if)spectra only
181 """
182 if not insitu:
183 from asap._asap import opacity as _opacity
184 return scantable(_opacity(scan, tau, all))
185 else:
186 from asap._asap import opacity_insitu as _opacity
187 _opacity(scan, tau, all)
188 return
189
[167]190def bin(scan, width=5, insitu=False):
[101]191 """
[167]192 Return a scan where all spectra have been binned up
[172]193 width: The bin width (default=5) in pixels
[167]194 insitu: if False (default) a new scantable is returned.
195 Otherwise, the addition is done in-situ
[101]196 """
[167]197 if not insitu:
198 from asap._asap import bin as _bin
199 return scantable(_bin(scan, width))
200 else:
201 from asap._asap import bin_insitu as _bin
202 _bin(scan, width)
203 return
[113]204
[166]205def average_pol(scan, mask=None, insitu=False):
[113]206 """
207 Average the Polarisations together.
208 Parameters:
[172]209 scan: The scantable
210 mask: An optional mask defining the region, where the
211 averaging will be applied. The output will have all
212 specified points masked.
213 insitu: If False (default) a new scantable is returned.
[166]214 Otherwise, the averaging is done in-situ
[113]215 Example:
216 polav = average_pols(myscan)
217 """
218 if mask is None:
[166]219 mask = ()
220 if not insitu:
221 from asap._asap import averagepol as _avpol
222 return scantable(_avpol(scan, mask))
223 else:
224 from asap._asap import averagepol_insitu as _avpol
225 _avpol(scan, mask)
226 return
[113]227
[180]228def smooth(scan, kernel="hanning", width=5.0, insitu=False, all=True):
[113]229 """
[180]230 Smooth the spectrum by the specified kernel (conserving flux).
[113]231 Parameters:
[172]232 scan: The input scan
[180]233 kernel: The type of smoothing kernel. Select from
234 'hanning' (default), 'gaussian' and 'boxcar'.
235 The first three characters are sufficient.
236 width: The width of the kernel in pixels. For hanning this is
237 ignored otherwise it defauls to 5 pixels.
238 For 'gaussian' it is the Full Width Half
239 Maximum. For 'boxcar' it is the full width.
[172]240 insitu: If False (default) a new scantable is returned.
241 Otherwise, the scaling is done in-situ
[180]242 all: If True (default) apply to all spectra. Otherwise
243 apply only to the selected (beam/pol/if)spectra only
[113]244 Example:
245 none
246 """
[172]247 if not insitu:
[180]248 from asap._asap import smooth as _smooth
249 return scantable(_smooth(scan,kernel,width,all))
[172]250 else:
[180]251 from asap._asap import smooth_insitu as _smooth
252 _smooth(scan,kernel,width,all)
[172]253 return
[113]254
255def poly_baseline(scan, mask=None, order=0):
256 """
[160]257 Return a scan which has been baselined (all rows) by a polynomial.
[113]258 Parameters:
259 scan: a scantable
260 mask: an optional mask
261 order: the order of the polynomial (default is 0)
262 Example:
263 # return a scan baselined by a third order polynomial,
264 # not using a mask
265 bscan = poly_baseline(scan, order=3)
266 """
267 from asap.asapfitter import fitter
268 if mask is None:
269 from numarray import ones
270 mask = tuple(ones(scan.nchan()))
271 f = fitter()
272 f._verbose(True)
273 f.set_scan(scan, mask)
274 f.set_function(poly=order)
275 sf = f.auto_fit()
276 return sf
Note: See TracBrowser for help on using the repository browser.