source: trunk/python/asapmath.py @ 242

Last change on this file since 242 was 242, checked in by kil064, 19 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
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):
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
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
65def scale(scan, factor, insitu=False, all=True):
66    """
67    Return a scan where all spectra are scaled by the give 'factor'
68    Parameters:
69        scan:        a scantable
70        factor:      the scaling factor
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
75    """
76    if not insitu:
77        from asap._asap import scale as _scale
78        return scantable(_scale(scan, factor, all))
79    else:
80        from asap._asap import scale_insitu as _scale
81        _scale(scan, factor, all)
82        return
83       
84
85def add(scan, offset, insitu=False, all=True):
86    """
87    Return a scan where all spectra have the offset added
88    Parameters:
89        scan:        a scantable
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
95    """
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       
104def convert_flux(scan, area, eta=1.0, insitu=False, all=True):
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
124
125def gain_el(scan, poly=None, filename="", method="linear", insitu=False, all=True):
126    """
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).
134    Parameters:
135        scan:        a scantable
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.
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
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"
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    """
160    if poly is None:
161       poly = ()
162    if not insitu:
163        from asap._asap import gainel as _gainEl
164        return scantable(_gainEl(scan, poly, filename, method, all))
165    else:
166        from asap._asap import gainel_insitu as _gainEl
167        _gainEl(scan, poly, filename, method, all)
168        return
169       
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       
190def bin(scan, width=5, insitu=False):
191    """
192    Return a scan where all spectra have been binned up
193        width:       The bin width (default=5) in pixels
194        insitu:      if False (default) a new scantable is returned.
195                     Otherwise, the addition is done in-situ
196    """
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
204
205def average_pol(scan, mask=None, insitu=False):
206    """
207    Average the Polarisations together.
208    Parameters:
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.
214                     Otherwise, the averaging is done in-situ
215    Example:
216        polav = average_pols(myscan)
217    """
218    if mask is None:
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
227   
228def smooth(scan, kernel="hanning", width=5.0, insitu=False, all=True):
229    """
230    Smooth the spectrum by the specified kernel (conserving flux).
231    Parameters:
232        scan:       The input scan
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.
240        insitu:     If False (default) a new scantable is returned.
241                    Otherwise, the scaling is done in-situ
242        all:        If True (default) apply to all spectra. Otherwise
243                    apply only to the selected (beam/pol/if)spectra only
244    Example:
245         none
246    """
247    if not insitu:
248        from asap._asap import smooth as _smooth
249        return scantable(_smooth(scan,kernel,width,all))
250    else:
251        from asap._asap import smooth_insitu as _smooth
252        _smooth(scan,kernel,width,all)
253        return
254   
255def poly_baseline(scan, mask=None, order=0):
256    """
257    Return a scan which has been baselined (all rows) by a polynomial.
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.