source: trunk/python/asapmath.py @ 299

Last change on this file since 299 was 296, checked in by kil064, 19 years ago

add arg. 'tsys' to simple_math and scale (optioanl application to Tsys)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.1 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    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
43def 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
59def 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
77def 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
103def 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       
126def 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
151def 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       
203def 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       
225def 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       
249def 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
266def average_pol(scan, mask=None, insitu=None):
267    """
268    Average the Polarisations together.
269    Parameters:
270        scan:        The scantable
271        mask:        An optional mask defining the region, where the
272                     averaging will be applied. The output will have all
273                     specified points masked.
274        insitu:      if False a new scantable is returned.
275                     Otherwise, the scaling is done in-situ
276                     The default is taken from .asaprc (False)
277    Example:
278        polav = average_pols(myscan)
279    """
280    if mask is None:
281        mask = ()
282    if insitu is None: insitu = rcParams['insitu']
283    if not insitu:
284        from asap._asap import averagepol as _avpol
285        return scantable(_avpol(scan, mask))
286    else:
287        from asap._asap import averagepol_insitu as _avpol
288        _avpol(scan, mask)
289        return
290   
291def smooth(scan, kernel="hanning", width=5.0, insitu=None, allaxes=None):
292    """
293    Smooth the spectrum by the specified kernel (conserving flux).
294    Parameters:
295        scan:       The input scan
296        kernel:     The type of smoothing kernel. Select from
297                    'hanning' (default), 'gaussian' and 'boxcar'.
298                    The first three characters are sufficient.
299        width:      The width of the kernel in pixels. For hanning this is
300                    ignored otherwise it defauls to 5 pixels.
301                    For 'gaussian' it is the Full Width Half
302                    Maximum. For 'boxcar' it is the full width.
303        insitu:     if False a new scantable is returned.
304                    Otherwise, the scaling is done in-situ
305                    The default is taken from .asaprc (False)
306        allaxes:    If True (default) apply to all spectra. Otherwise
307                    apply only to the selected (beam/pol/if)spectra only
308                    The default is taken from .asaprc (True)
309    Example:
310         none
311    """
312    if allaxes is None: allaxes = rcParams['scantable.allaxes']
313    if insitu is None: insitu = rcParams['insitu']
314    if not insitu:
315        from asap._asap import smooth as _smooth
316        return scantable(_smooth(scan,kernel,width,allaxes))
317    else:
318        from asap._asap import smooth_insitu as _smooth
319        _smooth(scan,kernel,width,allaxes)
320        return
321   
322def poly_baseline(scan, mask=None, order=0, insitu=None):
323    """
324    Return a scan which has been baselined (all rows) by a polynomial.
325    Parameters:
326        scan:    a scantable
327        mask:    an optional mask
328        order:   the order of the polynomial (default is 0)
329        insitu:      if False a new scantable is returned.
330                     Otherwise, the scaling is done in-situ
331                     The default is taken from .asaprc (False)
332    Example:
333        # return a scan baselined by a third order polynomial,
334        # not using a mask
335        bscan = poly_baseline(scan, order=3)
336    """
337    from asap.asapfitter import fitter
338    if mask is None:
339        from numarray import ones
340        mask = tuple(ones(scan.nchan()))
341    f = fitter()
342    f._verbose(True)
343    f.set_scan(scan, mask)
344    f.set_function(poly=order)   
345    sf = f.auto_fit(insitu)
346    return sf
Note: See TracBrowser for help on using the repository browser.