source: trunk/python/asapmath.py @ 481

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

docuiment change in gain/el and opaicty corrections

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 17.0 KB
RevLine 
[101]1from scantable import scantable
[258]2from asap import rcParams
[101]3
[143]4def average_time(*args, **kwargs):
[101]5    """
[113]6    Return the (time) average of a scan or list of scans. [in channels only]
[305]7    The cursor of the output scan is set to 0
[113]8    Parameters:
9        one scan or comma separated  scans
[143]10        mask:     an optional mask (only used for 'var' and 'tsys' weighting)
[424]11        scanav:   True (default) averages each scan separately
12                  False averages all scans together,
[143]13        weight:   Weighting scheme. 'none' (default), 'var' (variance
14                  weighted), 'tsys'
[113]15    Example:
16        # return a time averaged scan from scana and scanb
17        # without using a mask
[129]18        scanav = average_time(scana,scanb)
[113]19        # return the (time) averaged scan, i.e. the average of
20        # all correlator cycles
21        scanav = average_time(scan)
[143]22
[101]23    """
[424]24    scanAv = True
[143]25    if kwargs.has_key('scanav'):
26       scanAv = kwargs.get('scanav')
27    weight = 'none'
28    if kwargs.has_key('weight'):
29       weight = kwargs.get('weight')
30    mask = ()
31    if kwargs.has_key('mask'):
32        mask = kwargs.get('mask')
33    lst = tuple(args)
34    from asap._asap import average as _av
[113]35    for s in lst:
[101]36        if not isinstance(s,scantable):
37            print "Please give a list of scantables"
38            return
[143]39    return scantable(_av(lst, mask, scanAv, weight))
[101]40
[245]41def quotient(source, reference, preserve=True):
[101]42    """
[246]43    Return the quotient of a 'source' (signal) scan and a 'reference' scan.
44    The reference can have just one row, even if the signal has many. Otherwise
45    they must have the same number of rows.
[305]46    The cursor of the output scan is set to 0
[101]47    Parameters:
48        source:        the 'on' scan
49        reference:     the 'off' scan
[245]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
[101]54    """
55    from asap._asap import quotient as _quot
[245]56    return scantable(_quot(source, reference, preserve))
[101]57
[296]58def simple_math(left, right, op='add', tsys=True):
[242]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
[305]63    The cursor of the output scan is set to 0
[242]64    Parameters:
65        left:          the 'left' scan
66        right:         the 'right' scan
67        op:            the operation: 'add' (default), 'sub', 'mul', 'div'
[296]68        tsys:          if True (default) then apply the operation to Tsys
69                       as well as the data
[242]70    """
[258]71    if not isinstance(left,scantable) and not isinstance(right,scantable):
72        print "Please provide two scantables as input"
73        return
[242]74    from asap._asap import b_operate as _bop
[296]75    return scantable(_bop(left, right, op, tsys))
[242]76
[296]77def scale(scan, factor, insitu=None, allaxes=None, tsys=True):
[101]78    """
79    Return a scan where all spectra are scaled by the give 'factor'
80    Parameters:
81        scan:        a scantable
[113]82        factor:      the scaling factor
[258]83        insitu:      if False a new scantable is returned.
[150]84                     Otherwise, the scaling is done in-situ
[258]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.
[435]88                     The default is taken from .asaprc (True if none)
[296]89        tsys:        if True (default) then apply the operation to Tsys
90                     as well as the data
[101]91    """
[258]92    if allaxes is None: allaxes = rcParams['scantable.allaxes']
93    if insitu is None: insitu = rcParams['insitu']
[141]94    if not insitu:
95        from asap._asap import scale as _scale
[296]96        return scantable(_scale(scan, factor, allaxes, tsys))
[141]97    else:
98        from asap._asap import scale_insitu as _scale
[296]99        _scale(scan, factor, allaxes, tsys)
[141]100        return
101       
[101]102
[258]103def add(scan, offset, insitu=None, allaxes=None):
[113]104    """
[150]105    Return a scan where all spectra have the offset added
[113]106    Parameters:
107        scan:        a scantable
[150]108        offset:      the offset
[258]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
[150]113                     apply only to the selected (beam/pol/if)spectra only
[435]114                     The default is taken from .asaprc (True if none)
[113]115    """
[258]116    if allaxes is None: allaxes = rcParams['scantable.allaxes']
117    if insitu is None: insitu = rcParams['insitu']
[150]118    if not insitu:
119        from asap._asap import add as _add
[258]120        return scantable(_add(scan, offset, allaxes))
[150]121    else:
122        from asap._asap import add_insitu as _add
[258]123        _add(scan, offset, allaxes)
[150]124        return
125       
[359]126def convert_flux(scan, jyperk=None, eta=None, d=None, insitu=None, allaxes=None):
[224]127    """
128    Return a scan where all spectra are converted to either Jansky or Kelvin
[359]129        depending upon the flux units of the scan table.  By default the
130        function tries to look the values up internally. If it can't find
131        them (or if you want to over-ride), you must specify EITHER jyperk
132        OR eta (and D which it will try to look up also if you don't
133        set it).  jyperk takes precedence if you set both.
[224]134    Parameters:
135        scan:        a scantable
[359]136        jyperk:      the Jy / K conversion factor
137        eta:         the aperture efficiency 
138        d:           the geomtric diameter (metres)
[258]139        insitu:      if False a new scantable is returned.
140                     Otherwise, the scaling is done in-situ
141                     The default is taken from .asaprc (False)
142        allaxes:         if True apply to all spectra. Otherwise
[224]143                     apply only to the selected (beam/pol/if)spectra only
[435]144                     The default is taken from .asaprc (True if none)
[224]145    """
[258]146    if allaxes is None: allaxes = rcParams['scantable.allaxes']
147    if insitu is None: insitu = rcParams['insitu']
[359]148    if jyperk is None: jyperk = -1.0
149    if d is None: d = -1.0
150    if eta is None: eta = -1.0
[224]151    if not insitu:
152        from asap._asap import convertflux as _convert
[359]153        return scantable(_convert(scan, d, eta, jyperk, allaxes))
[224]154    else:
155        from asap._asap import convertflux_insitu as _convert
[359]156        _convert(scan, d, eta, jyperk, allaxes)
[224]157        return
[229]158
[411]159def gain_el(scan, poly=None, filename="", method="linear",
160            insitu=None, allaxes=None):
[229]161    """
[242]162    Return a scan after applying a gain-elevation correction. The correction
163    can be made via either a polynomial or a table-based interpolation
164    (and extrapolation if necessary).
165    You specify polynomial coefficients, an ascii table or neither.
166    If you specify neither, then a polynomial correction will be made
167    with built in coefficients known for certain telescopes (an error will
[481]168    occur if the instrument is not known).   The data and Tsys are *divided*
[479]169    by the scaling factors.
[229]170    Parameters:
171        scan:        a scantable
[258]172        poly:        Polynomial coefficients (default None) to compute a
173                     gain-elevation correction as a function of
174                     elevation (in degrees).
[242]175        filename:    The name of an ascii file holding correction factors.
176                     The first row of the ascii file must give the column
177                     names and these MUST include columns
[258]178                     "ELEVATION" (degrees) and "FACTOR" (multiply data by this)
179                     somewhere.
180                     The second row must give the data type of the column. Use
181                     'R' for Real and 'I' for Integer.  An example file
[479]182                     would be (actual factors are arbitrary) :
[229]183
[242]184                     TIME ELEVATION FACTOR
185                     R R R
[479]186                     0.1 0 0.8
187                     0.2 20 0.85
188                     0.3 40 0.9
189                     0.4 60 0.85
190                     0.5 80 0.8
191                     0.6 90 0.75
[242]192        method:      Interpolation method when correcting from a table. Values
193                     are  "nearest", "linear" (default), "cubic" and "spline"
[258]194        insitu:      if False a new scantable is returned.
195                     Otherwise, the scaling is done in-situ
196                     The default is taken from .asaprc (False)
197        allaxes:         if True apply to all spectra. Otherwise
198                     apply only to the selected (beam/pol/if) spectra only
[435]199                     The default is taken from .asaprc (True if none)
[229]200    """
[258]201    if allaxes is None: allaxes = rcParams['scantable.allaxes']
[242]202    if poly is None:
203       poly = ()
[258]204    if insitu is None: insitu = rcParams['insitu']
[411]205    from os.path import expandvars
206    filename = expandvars(filename)
[229]207    if not insitu:
208        from asap._asap import gainel as _gainEl
[258]209        return scantable(_gainEl(scan, poly, filename, method, allaxes))
[229]210    else:
211        from asap._asap import gainel_insitu as _gainEl
[258]212        _gainEl(scan, poly, filename, method, allaxes)
[229]213        return
[224]214       
[399]215def freq_align(scan, reftime=None, method='cubic', perif=False, insitu=None):
[269]216    """
[313]217        Return a scan where all rows have been aligned in frequency. The
218        alignment frequency frame (e.g. LSRK) is that set by function
219        set_freqframe. 
[269]220        scan:        a scantable
[273]221        reftime:     reference time to align at. By default, the time of
222                     the first row of data is used. 
[319]223        method:      Interpolation method for regridding the spectra. Choose
224                     from "nearest", "linear", "cubic" (default) and "spline"
[399]225        perif:       Generate aligners per freqID (no doppler tracking) or
226                     per IF (scan-based doppler tracking)
[269]227        insitu:      if False a new scantable is returned.
228                     Otherwise, the scaling is done in-situ
229                     The default is taken from .asaprc (False)
230    """
[273]231    if reftime is None: reftime = ''
[269]232    if insitu is None: insitu = rcParams['insitu']
[399]233    perfreqid = not perif
[269]234    if not insitu:
[313]235        from asap._asap import freq_align as _align
[399]236        return scantable(_align(scan, reftime, method, perfreqid))
[269]237    else:
[313]238        from asap._asap import freq_align_insitu as _align
[399]239        _align(scan, reftime, method, perfreqid)
[269]240        return
241       
[258]242def opacity(scan, tau, insitu=None, allaxes=None):
[242]243    """
[481]244    Return a scan after applying an opacity correction. The data
245    and Tsys are multiplied by the correction factor.
[242]246    Parameters:
247        scan:        a scantable
248        tau:         Opacity from which the correction factor is exp(tau*ZD)
249                     where ZD is the zenith-distance
[258]250        insitu:      if False a new scantable is returned.
251                     Otherwise, the scaling is done in-situ
252                     The default is taken from .asaprc (False)
253        allaxes:     if True apply to all spectra. Otherwise
[242]254                     apply only to the selected (beam/pol/if)spectra only
[435]255                     The default is taken from .asaprc (True if none)
[242]256    """
[258]257    if allaxes is None: allaxes = rcParams['scantable.allaxes']
258    if insitu is None: insitu = rcParams['insitu']
[242]259    if not insitu:
260        from asap._asap import opacity as _opacity
[258]261        return scantable(_opacity(scan, tau, allaxes))
[242]262    else:
263        from asap._asap import opacity_insitu as _opacity
[258]264        _opacity(scan, tau, allaxes)
[242]265        return
266       
[258]267def bin(scan, width=5, insitu=None):
[101]268    """
[167]269    Return a scan where all spectra have been binned up
[172]270        width:       The bin width (default=5) in pixels
[258]271        insitu:      if False a new scantable is returned.
272                     Otherwise, the scaling is done in-situ
273                     The default is taken from .asaprc (False)
[101]274    """
[258]275    if insitu is None: insitu = rcParams['insitu']
[167]276    if not insitu:
277        from asap._asap import bin as _bin
278        return scantable(_bin(scan, width))
279    else:
280        from asap._asap import bin_insitu as _bin
281        _bin(scan, width)
282        return
[113]283
[301]284def resample(scan, width=5, method='cubic', insitu=None):
285    """
286    Return a scan where all spectra have been binned up
287        width:       The bin width (default=5) in pixels
288        method:      Interpolation method when correcting from a table. Values
289                     are  "nearest", "linear", "cubic" (default) and "spline"
290        insitu:      if False a new scantable is returned.
291                     Otherwise, the scaling is done in-situ
292                     The default is taken from .asaprc (False)
293    """
294    if insitu is None: insitu = rcParams['insitu']
295    if not insitu:
296        from asap._asap import resample as _resample
297        return scantable(_resample(scan, method, width))
298    else:
299        from asap._asap import resample_insitu as _resample
300        _resample(scan, method, width)
301        return
302
[316]303def average_pol(scan, mask=None, weight='none', insitu=None):
[113]304    """
305    Average the Polarisations together.
[305]306    The polarisation cursor of the output scan is set to 0
[113]307    Parameters:
[172]308        scan:        The scantable
309        mask:        An optional mask defining the region, where the
310                     averaging will be applied. The output will have all
311                     specified points masked.
[316]312        weight:      Weighting scheme. 'none' (default), or 'var' (variance
313                     weighted)
[258]314        insitu:      if False a new scantable is returned.
315                     Otherwise, the scaling is done in-situ
316                     The default is taken from .asaprc (False)
[113]317    Example:
318        polav = average_pols(myscan)
319    """
320    if mask is None:
[166]321        mask = ()
[258]322    if insitu is None: insitu = rcParams['insitu']
[166]323    if not insitu:
324        from asap._asap import averagepol as _avpol
[316]325        return scantable(_avpol(scan, mask, weight))
[166]326    else:
327        from asap._asap import averagepol_insitu as _avpol
[316]328        _avpol(scan, mask, weight)
[166]329        return
[113]330   
[258]331def smooth(scan, kernel="hanning", width=5.0, insitu=None, allaxes=None):
[113]332    """
[180]333    Smooth the spectrum by the specified kernel (conserving flux).
[113]334    Parameters:
[172]335        scan:       The input scan
[180]336        kernel:     The type of smoothing kernel. Select from
337                    'hanning' (default), 'gaussian' and 'boxcar'.
338                    The first three characters are sufficient.
339        width:      The width of the kernel in pixels. For hanning this is
340                    ignored otherwise it defauls to 5 pixels.
341                    For 'gaussian' it is the Full Width Half
342                    Maximum. For 'boxcar' it is the full width.
[258]343        insitu:     if False a new scantable is returned.
[172]344                    Otherwise, the scaling is done in-situ
[258]345                    The default is taken from .asaprc (False)
346        allaxes:    If True (default) apply to all spectra. Otherwise
[180]347                    apply only to the selected (beam/pol/if)spectra only
[435]348                    The default is taken from .asaprc (True if none)
[113]349    Example:
350         none
351    """
[258]352    if allaxes is None: allaxes = rcParams['scantable.allaxes']
353    if insitu is None: insitu = rcParams['insitu']
[172]354    if not insitu:
[180]355        from asap._asap import smooth as _smooth
[258]356        return scantable(_smooth(scan,kernel,width,allaxes))
[172]357    else:
[180]358        from asap._asap import smooth_insitu as _smooth
[258]359        _smooth(scan,kernel,width,allaxes)
[172]360        return
[113]361   
[258]362def poly_baseline(scan, mask=None, order=0, insitu=None):
[113]363    """
[160]364    Return a scan which has been baselined (all rows) by a polynomial.
[113]365    Parameters:
366        scan:    a scantable
367        mask:    an optional mask
368        order:   the order of the polynomial (default is 0)
[258]369        insitu:      if False a new scantable is returned.
370                     Otherwise, the scaling is done in-situ
371                     The default is taken from .asaprc (False)
[113]372    Example:
373        # return a scan baselined by a third order polynomial,
374        # not using a mask
375        bscan = poly_baseline(scan, order=3)
376    """
377    from asap.asapfitter import fitter
378    if mask is None:
379        from numarray import ones
380        mask = tuple(ones(scan.nchan()))
381    f = fitter()
382    f._verbose(True)
383    f.set_scan(scan, mask)
384    f.set_function(poly=order)   
[258]385    sf = f.auto_fit(insitu)
[113]386    return sf
[458]387
388def rotate_xyphase (scan, angle, allaxes=None):
389    """
390    Rotate the phase of the XY correlation.  This is always done in situ
391    in the data.  So if you call this function more than once
392    then each call rotates the phase further.
393    Parameters:
394        angle:   The angle (degrees) to rotate (add) by.
395        allaxes: If True apply to all spectra. Otherwise
396                 apply only to the selected (beam/pol/if)spectra only.
397                 The default is taken from .asaprc (True if none)
398    Examples:
399        rotate_xyphase(scan, 2.3)
400    """
401    if allaxes is None: allaxes = rcParams['scantable.allaxes']
402    from asap._asap import rotate_xyphase as _rotate_xyphase
403    _rotate_xyphase(scan, angle, allaxes)
404    return
405
Note: See TracBrowser for help on using the repository browser.