source: trunk/python/asapmath.py @ 313

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

replace velocity alignment bvy fredquemncy alignment function

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 15.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)
11        scanav:   False (default) averages all scans together,
12                  True averages each scan separately
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    """
[143]24    scanAv = False
25    if kwargs.has_key('scanav'):
26       scanAv = kwargs.get('scanav')
[258]27
[143]28    weight = 'none'
29    if kwargs.has_key('weight'):
30       weight = kwargs.get('weight')
[258]31
[143]32    mask = ()
33    if kwargs.has_key('mask'):
34        mask = kwargs.get('mask')
[258]35
[143]36    lst = tuple(args)
37    from asap._asap import average as _av
[113]38    for s in lst:
[101]39        if not isinstance(s,scantable):
40            print "Please give a list of scantables"
41            return
[143]42    return scantable(_av(lst, mask, scanAv, weight))
[101]43
[245]44def quotient(source, reference, preserve=True):
[101]45    """
[246]46    Return the quotient of a 'source' (signal) scan and a 'reference' scan.
47    The reference can have just one row, even if the signal has many. Otherwise
48    they must have the same number of rows.
[305]49    The cursor of the output scan is set to 0
[101]50    Parameters:
51        source:        the 'on' scan
52        reference:     the 'off' scan
[245]53        preserve:      you can preserve (default) the continuum or
54                       remove it.  The equations used are
55                          preserve - Output = Tref * (sig/ref) - Tref
56                          remove   - Output = Tref * (sig/ref) - Tsig
[101]57    """
58    from asap._asap import quotient as _quot
[245]59    return scantable(_quot(source, reference, preserve))
[101]60
[296]61def simple_math(left, right, op='add', tsys=True):
[242]62    """
63    Apply simple mathematical binary operations to two
64    scan tables,  returning the result in a new scan table.
65    The operation is applied to both the correlations and the TSys data
[305]66    The cursor of the output scan is set to 0
[242]67    Parameters:
68        left:          the 'left' scan
69        right:         the 'right' scan
70        op:            the operation: 'add' (default), 'sub', 'mul', 'div'
[296]71        tsys:          if True (default) then apply the operation to Tsys
72                       as well as the data
[242]73    """
[258]74    if not isinstance(left,scantable) and not isinstance(right,scantable):
75        print "Please provide two scantables as input"
76        return
[242]77    from asap._asap import b_operate as _bop
[296]78    return scantable(_bop(left, right, op, tsys))
[242]79
[296]80def scale(scan, factor, insitu=None, allaxes=None, tsys=True):
[101]81    """
82    Return a scan where all spectra are scaled by the give 'factor'
83    Parameters:
84        scan:        a scantable
[113]85        factor:      the scaling factor
[258]86        insitu:      if False a new scantable is returned.
[150]87                     Otherwise, the scaling is done in-situ
[258]88                     The default is taken from .asaprc (False)
89        allaxes:     if True apply to all spectra. Otherwise
90                     apply only to the selected (beam/pol/if)spectra only.
91                     The default is taken from .asaprc (True)
[296]92        tsys:        if True (default) then apply the operation to Tsys
93                     as well as the data
[101]94    """
[258]95    if allaxes is None: allaxes = rcParams['scantable.allaxes']
96    if insitu is None: insitu = rcParams['insitu']
[141]97    if not insitu:
98        from asap._asap import scale as _scale
[296]99        return scantable(_scale(scan, factor, allaxes, tsys))
[141]100    else:
101        from asap._asap import scale_insitu as _scale
[296]102        _scale(scan, factor, allaxes, tsys)
[141]103        return
104       
[101]105
[258]106def add(scan, offset, insitu=None, allaxes=None):
[113]107    """
[150]108    Return a scan where all spectra have the offset added
[113]109    Parameters:
110        scan:        a scantable
[150]111        offset:      the offset
[258]112        insitu:      if False a new scantable is returned.
113                     Otherwise, the scaling is done in-situ
114                     The default is taken from .asaprc (False)
115        allaxes:     if True apply to all spectra. Otherwise
[150]116                     apply only to the selected (beam/pol/if)spectra only
[258]117                     The default is taken from .asaprc (True)
[113]118    """
[258]119    if allaxes is None: allaxes = rcParams['scantable.allaxes']
120    if insitu is None: insitu = rcParams['insitu']
[150]121    if not insitu:
122        from asap._asap import add as _add
[258]123        return scantable(_add(scan, offset, allaxes))
[150]124    else:
125        from asap._asap import add_insitu as _add
[258]126        _add(scan, offset, allaxes)
[150]127        return
128       
[258]129def convert_flux(scan, area, eta=1.0, insitu=None, allaxes=None):
[224]130    """
131    Return a scan where all spectra are converted to either Jansky or Kelvin
132        depending upon the flux units of the scan table.
133    Parameters:
134        scan:        a scantable
135        area:        the illuminated area of the telescope (m**2)
136        eta:         The efficiency of the telescope (default 1.0)       
[258]137        insitu:      if False a new scantable is returned.
138                     Otherwise, the scaling is done in-situ
139                     The default is taken from .asaprc (False)
140        allaxes:         if True apply to all spectra. Otherwise
[224]141                     apply only to the selected (beam/pol/if)spectra only
[258]142                     The default is taken from .asaprc (True)
[224]143    """
[258]144    if allaxes is None: allaxes = rcParams['scantable.allaxes']
145    if insitu is None: insitu = rcParams['insitu']
[224]146    if not insitu:
147        from asap._asap import convertflux as _convert
[258]148        return scantable(_convert(scan, area, eta, allaxes))
[224]149    else:
150        from asap._asap import convertflux_insitu as _convert
[258]151        _convert(scan, area, eta, allaxes)
[224]152        return
[229]153
[258]154def gain_el(scan, poly=None, filename="", method="linear", insitu=None, allaxes=None):
[229]155    """
[242]156    Return a scan after applying a gain-elevation correction. The correction
157    can be made via either a polynomial or a table-based interpolation
158    (and extrapolation if necessary).
159    You specify polynomial coefficients, an ascii table or neither.
160    If you specify neither, then a polynomial correction will be made
161    with built in coefficients known for certain telescopes (an error will
162    occur if the instrument is not known).
[229]163    Parameters:
164        scan:        a scantable
[258]165        poly:        Polynomial coefficients (default None) to compute a
166                     gain-elevation correction as a function of
167                     elevation (in degrees).
[242]168        filename:    The name of an ascii file holding correction factors.
169                     The first row of the ascii file must give the column
170                     names and these MUST include columns
[258]171                     "ELEVATION" (degrees) and "FACTOR" (multiply data by this)
172                     somewhere.
173                     The second row must give the data type of the column. Use
174                     'R' for Real and 'I' for Integer.  An example file
175                     would be:
[229]176
[242]177                     TIME ELEVATION FACTOR
178                     R R R
179                     0.1 0 1.5
180                     0.2 20 1.4
181                     0.3 40 1.3
182                     0.4 60 1.2
183                     0.5 80 1.1
184                     0.6 90 1.0
185        method:      Interpolation method when correcting from a table. Values
186                     are  "nearest", "linear" (default), "cubic" and "spline"
[258]187        insitu:      if False a new scantable is returned.
188                     Otherwise, the scaling is done in-situ
189                     The default is taken from .asaprc (False)
190        allaxes:         if True apply to all spectra. Otherwise
191                     apply only to the selected (beam/pol/if) spectra only
192                     The default is taken from .asaprc (True)
[229]193    """
[258]194    if allaxes is None: allaxes = rcParams['scantable.allaxes']
[242]195    if poly is None:
196       poly = ()
[258]197    if insitu is None: insitu = rcParams['insitu']
[229]198    if not insitu:
199        from asap._asap import gainel as _gainEl
[258]200        return scantable(_gainEl(scan, poly, filename, method, allaxes))
[229]201    else:
202        from asap._asap import gainel_insitu as _gainEl
[258]203        _gainEl(scan, poly, filename, method, allaxes)
[229]204        return
[224]205       
[313]206def freq_align(scan, reftime=None, insitu=None):
[269]207    """
[313]208        Return a scan where all rows have been aligned in frequency. The
209        alignment frequency frame (e.g. LSRK) is that set by function
210        set_freqframe. 
[269]211        scan:        a scantable
[273]212        reftime:     reference time to align at. By default, the time of
213                     the first row of data is used. 
[269]214        insitu:      if False a new scantable is returned.
215                     Otherwise, the scaling is done in-situ
216                     The default is taken from .asaprc (False)
217    """
[273]218    if reftime is None: reftime = ''
[269]219    if insitu is None: insitu = rcParams['insitu']
220    if not insitu:
[313]221        from asap._asap import freq_align as _align
[273]222        return scantable(_align(scan, reftime))
[269]223    else:
[313]224        from asap._asap import freq_align_insitu as _align
[273]225        _align(scan, reftime)
[269]226        return
227       
[258]228def opacity(scan, tau, insitu=None, allaxes=None):
[242]229    """
230    Return a scan after applying an opacity correction.
231    Parameters:
232        scan:        a scantable
233        tau:         Opacity from which the correction factor is exp(tau*ZD)
234                     where ZD is the zenith-distance
[258]235        insitu:      if False a new scantable is returned.
236                     Otherwise, the scaling is done in-situ
237                     The default is taken from .asaprc (False)
238        allaxes:     if True apply to all spectra. Otherwise
[242]239                     apply only to the selected (beam/pol/if)spectra only
[258]240                     The default is taken from .asaprc (True)
[242]241    """
[258]242    if allaxes is None: allaxes = rcParams['scantable.allaxes']
243    if insitu is None: insitu = rcParams['insitu']
[242]244    if not insitu:
245        from asap._asap import opacity as _opacity
[258]246        return scantable(_opacity(scan, tau, allaxes))
[242]247    else:
248        from asap._asap import opacity_insitu as _opacity
[258]249        _opacity(scan, tau, allaxes)
[242]250        return
251       
[258]252def bin(scan, width=5, insitu=None):
[101]253    """
[167]254    Return a scan where all spectra have been binned up
[172]255        width:       The bin width (default=5) in pixels
[258]256        insitu:      if False a new scantable is returned.
257                     Otherwise, the scaling is done in-situ
258                     The default is taken from .asaprc (False)
[101]259    """
[258]260    if insitu is None: insitu = rcParams['insitu']
[167]261    if not insitu:
262        from asap._asap import bin as _bin
263        return scantable(_bin(scan, width))
264    else:
265        from asap._asap import bin_insitu as _bin
266        _bin(scan, width)
267        return
[113]268
[301]269def resample(scan, width=5, method='cubic', insitu=None):
270    """
271    Return a scan where all spectra have been binned up
272        width:       The bin width (default=5) in pixels
273        method:      Interpolation method when correcting from a table. Values
274                     are  "nearest", "linear", "cubic" (default) and "spline"
275        insitu:      if False a new scantable is returned.
276                     Otherwise, the scaling is done in-situ
277                     The default is taken from .asaprc (False)
278    """
279    if insitu is None: insitu = rcParams['insitu']
280    if not insitu:
281        from asap._asap import resample as _resample
282        return scantable(_resample(scan, method, width))
283    else:
284        from asap._asap import resample_insitu as _resample
285        _resample(scan, method, width)
286        return
287
[258]288def average_pol(scan, mask=None, insitu=None):
[113]289    """
290    Average the Polarisations together.
[305]291    The polarisation cursor of the output scan is set to 0
[113]292    Parameters:
[172]293        scan:        The scantable
294        mask:        An optional mask defining the region, where the
295                     averaging will be applied. The output will have all
296                     specified points masked.
[258]297        insitu:      if False a new scantable is returned.
298                     Otherwise, the scaling is done in-situ
299                     The default is taken from .asaprc (False)
[113]300    Example:
301        polav = average_pols(myscan)
302    """
303    if mask is None:
[166]304        mask = ()
[258]305    if insitu is None: insitu = rcParams['insitu']
[166]306    if not insitu:
307        from asap._asap import averagepol as _avpol
308        return scantable(_avpol(scan, mask))
309    else:
310        from asap._asap import averagepol_insitu as _avpol
311        _avpol(scan, mask)
312        return
[113]313   
[258]314def smooth(scan, kernel="hanning", width=5.0, insitu=None, allaxes=None):
[113]315    """
[180]316    Smooth the spectrum by the specified kernel (conserving flux).
[113]317    Parameters:
[172]318        scan:       The input scan
[180]319        kernel:     The type of smoothing kernel. Select from
320                    'hanning' (default), 'gaussian' and 'boxcar'.
321                    The first three characters are sufficient.
322        width:      The width of the kernel in pixels. For hanning this is
323                    ignored otherwise it defauls to 5 pixels.
324                    For 'gaussian' it is the Full Width Half
325                    Maximum. For 'boxcar' it is the full width.
[258]326        insitu:     if False a new scantable is returned.
[172]327                    Otherwise, the scaling is done in-situ
[258]328                    The default is taken from .asaprc (False)
329        allaxes:    If True (default) apply to all spectra. Otherwise
[180]330                    apply only to the selected (beam/pol/if)spectra only
[258]331                    The default is taken from .asaprc (True)
[113]332    Example:
333         none
334    """
[258]335    if allaxes is None: allaxes = rcParams['scantable.allaxes']
336    if insitu is None: insitu = rcParams['insitu']
[172]337    if not insitu:
[180]338        from asap._asap import smooth as _smooth
[258]339        return scantable(_smooth(scan,kernel,width,allaxes))
[172]340    else:
[180]341        from asap._asap import smooth_insitu as _smooth
[258]342        _smooth(scan,kernel,width,allaxes)
[172]343        return
[113]344   
[258]345def poly_baseline(scan, mask=None, order=0, insitu=None):
[113]346    """
[160]347    Return a scan which has been baselined (all rows) by a polynomial.
[113]348    Parameters:
349        scan:    a scantable
350        mask:    an optional mask
351        order:   the order of the polynomial (default is 0)
[258]352        insitu:      if False a new scantable is returned.
353                     Otherwise, the scaling is done in-situ
354                     The default is taken from .asaprc (False)
[113]355    Example:
356        # return a scan baselined by a third order polynomial,
357        # not using a mask
358        bscan = poly_baseline(scan, order=3)
359    """
360    from asap.asapfitter import fitter
361    if mask is None:
362        from numarray import ones
363        mask = tuple(ones(scan.nchan()))
364    f = fitter()
365    f._verbose(True)
366    f.set_scan(scan, mask)
367    f.set_function(poly=order)   
[258]368    sf = f.auto_fit(insitu)
[113]369    return sf
Note: See TracBrowser for help on using the repository browser.