source: trunk/python/asapmath.py @ 246

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

in 'quotient' describe that ref can have one row

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