| 1 | from scantable import scantable
 | 
|---|
| 2 | 
 | 
|---|
| 3 | def 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 | 
 | 
|---|
| 42 | def 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 | 
 | 
|---|
| 52 | def scale(scan, factor, insitu=False, all=True):
 | 
|---|
| 53 |     """
 | 
|---|
| 54 |     Return a scan where all spectra are scaled by the give 'factor'
 | 
|---|
| 55 |     Parameters:
 | 
|---|
| 56 |         scan:        a scantable
 | 
|---|
| 57 |         factor:      the scaling factor
 | 
|---|
| 58 |         insitu:      if False (default) a new scantable is returned.
 | 
|---|
| 59 |                      Otherwise, the scaling is done in-situ
 | 
|---|
| 60 |         all:         if True (default) apply to all spectra. Otherwise
 | 
|---|
| 61 |                      apply only to the selected (beam/pol/if)spectra only
 | 
|---|
| 62 |     """
 | 
|---|
| 63 |     if not insitu:
 | 
|---|
| 64 |         from asap._asap import scale as _scale
 | 
|---|
| 65 |         return scantable(_scale(scan, factor, all))
 | 
|---|
| 66 |     else:
 | 
|---|
| 67 |         from asap._asap import scale_insitu as _scale
 | 
|---|
| 68 |         _scale(scan, factor, all)
 | 
|---|
| 69 |         return
 | 
|---|
| 70 |         
 | 
|---|
| 71 | 
 | 
|---|
| 72 | def add(scan, offset, insitu=False, all=True):
 | 
|---|
| 73 |     """
 | 
|---|
| 74 |     Return a scan where all spectra have the offset added
 | 
|---|
| 75 |     Parameters:
 | 
|---|
| 76 |         scan:        a scantable
 | 
|---|
| 77 |         offset:      the offset
 | 
|---|
| 78 |         insitu:      if False (default) a new scantable is returned.
 | 
|---|
| 79 |                      Otherwise, the addition is done in-situ
 | 
|---|
| 80 |         all:         if True (default) apply to all spectra. Otherwise
 | 
|---|
| 81 |                      apply only to the selected (beam/pol/if)spectra only
 | 
|---|
| 82 |     """
 | 
|---|
| 83 |     if not insitu:
 | 
|---|
| 84 |         from asap._asap import add as _add
 | 
|---|
| 85 |         return scantable(_add(scan, offset, all))
 | 
|---|
| 86 |     else:
 | 
|---|
| 87 |         from asap._asap import add_insitu as _add
 | 
|---|
| 88 |         _add(scan, offset, all)
 | 
|---|
| 89 |         return
 | 
|---|
| 90 |         
 | 
|---|
| 91 | def convert_flux(scan, area, eta=1.0, insitu=False, all=True):
 | 
|---|
| 92 |     """
 | 
|---|
| 93 |     Return a scan where all spectra are converted to either Jansky or Kelvin
 | 
|---|
| 94 |         depending upon the flux units of the scan table.
 | 
|---|
| 95 |     Parameters:
 | 
|---|
| 96 |         scan:        a scantable
 | 
|---|
| 97 |         area:        the illuminated area of the telescope (m**2)
 | 
|---|
| 98 |         eta:         The efficiency of the telescope (default 1.0)        
 | 
|---|
| 99 |         insitu:      if False (default) a new scantable is returned.
 | 
|---|
| 100 |                      Otherwise, the conversion is done in-situ
 | 
|---|
| 101 |         all:         if True (default) apply to all spectra. Otherwise
 | 
|---|
| 102 |                      apply only to the selected (beam/pol/if)spectra only
 | 
|---|
| 103 |     """
 | 
|---|
| 104 |     if not insitu:
 | 
|---|
| 105 |         from asap._asap import convertflux as _convert
 | 
|---|
| 106 |         return scantable(_convert(scan, area, eta, all))
 | 
|---|
| 107 |     else:
 | 
|---|
| 108 |         from asap._asap import convertflux_insitu as _convert
 | 
|---|
| 109 |         _convert(scan, area, eta, all)
 | 
|---|
| 110 |         return
 | 
|---|
| 111 |         
 | 
|---|
| 112 | def bin(scan, width=5, insitu=False):
 | 
|---|
| 113 |     """
 | 
|---|
| 114 |     Return a scan where all spectra have been binned up
 | 
|---|
| 115 |         width:       The bin width (default=5) in pixels
 | 
|---|
| 116 |         insitu:      if False (default) a new scantable is returned.
 | 
|---|
| 117 |                      Otherwise, the addition is done in-situ
 | 
|---|
| 118 |     """
 | 
|---|
| 119 |     if not insitu:
 | 
|---|
| 120 |         from asap._asap import bin as _bin
 | 
|---|
| 121 |         return scantable(_bin(scan, width))
 | 
|---|
| 122 |     else:
 | 
|---|
| 123 |         from asap._asap import bin_insitu as _bin
 | 
|---|
| 124 |         _bin(scan, width)
 | 
|---|
| 125 |         return
 | 
|---|
| 126 | 
 | 
|---|
| 127 | def average_pol(scan, mask=None, insitu=False):
 | 
|---|
| 128 |     """
 | 
|---|
| 129 |     Average the Polarisations together.
 | 
|---|
| 130 |     Parameters:
 | 
|---|
| 131 |         scan:        The scantable
 | 
|---|
| 132 |         mask:        An optional mask defining the region, where the
 | 
|---|
| 133 |                      averaging will be applied. The output will have all 
 | 
|---|
| 134 |                      specified points masked. 
 | 
|---|
| 135 |         insitu:      If False (default) a new scantable is returned.
 | 
|---|
| 136 |                      Otherwise, the averaging is done in-situ
 | 
|---|
| 137 |     Example:
 | 
|---|
| 138 |         polav = average_pols(myscan)
 | 
|---|
| 139 |     """
 | 
|---|
| 140 |     if mask is None:
 | 
|---|
| 141 |         mask = ()
 | 
|---|
| 142 |     if not insitu:
 | 
|---|
| 143 |         from asap._asap import averagepol as _avpol
 | 
|---|
| 144 |         return scantable(_avpol(scan, mask))
 | 
|---|
| 145 |     else:
 | 
|---|
| 146 |         from asap._asap import averagepol_insitu as _avpol
 | 
|---|
| 147 |         _avpol(scan, mask)
 | 
|---|
| 148 |         return
 | 
|---|
| 149 |     
 | 
|---|
| 150 | def smooth(scan, kernel="hanning", width=5.0, insitu=False, all=True):
 | 
|---|
| 151 |     """
 | 
|---|
| 152 |     Smooth the spectrum by the specified kernel (conserving flux).
 | 
|---|
| 153 |     Parameters:
 | 
|---|
| 154 |         scan:       The input scan
 | 
|---|
| 155 |         kernel:     The type of smoothing kernel. Select from
 | 
|---|
| 156 |                     'hanning' (default), 'gaussian' and 'boxcar'.
 | 
|---|
| 157 |                     The first three characters are sufficient.
 | 
|---|
| 158 |         width:      The width of the kernel in pixels. For hanning this is
 | 
|---|
| 159 |                     ignored otherwise it defauls to 5 pixels.
 | 
|---|
| 160 |                     For 'gaussian' it is the Full Width Half
 | 
|---|
| 161 |                     Maximum. For 'boxcar' it is the full width.
 | 
|---|
| 162 |         insitu:     If False (default) a new scantable is returned.
 | 
|---|
| 163 |                     Otherwise, the scaling is done in-situ
 | 
|---|
| 164 |         all:        If True (default) apply to all spectra. Otherwise
 | 
|---|
| 165 |                     apply only to the selected (beam/pol/if)spectra only
 | 
|---|
| 166 |     Example:
 | 
|---|
| 167 |          none
 | 
|---|
| 168 |     """
 | 
|---|
| 169 |     if not insitu:
 | 
|---|
| 170 |         from asap._asap import smooth as _smooth
 | 
|---|
| 171 |         return scantable(_smooth(scan,kernel,width,all))
 | 
|---|
| 172 |     else:
 | 
|---|
| 173 |         from asap._asap import smooth_insitu as _smooth
 | 
|---|
| 174 |         _smooth(scan,kernel,width,all)
 | 
|---|
| 175 |         return
 | 
|---|
| 176 |     
 | 
|---|
| 177 | def poly_baseline(scan, mask=None, order=0):
 | 
|---|
| 178 |     """
 | 
|---|
| 179 |     Return a scan which has been baselined (all rows) by a polynomial. 
 | 
|---|
| 180 |     Parameters:
 | 
|---|
| 181 |         scan:    a scantable
 | 
|---|
| 182 |         mask:    an optional mask
 | 
|---|
| 183 |         order:   the order of the polynomial (default is 0)
 | 
|---|
| 184 |     Example:
 | 
|---|
| 185 |         # return a scan baselined by a third order polynomial,
 | 
|---|
| 186 |         # not using a mask
 | 
|---|
| 187 |         bscan = poly_baseline(scan, order=3)
 | 
|---|
| 188 |     """
 | 
|---|
| 189 |     from asap.asapfitter import fitter
 | 
|---|
| 190 |     if mask is None:
 | 
|---|
| 191 |         from numarray import ones
 | 
|---|
| 192 |         mask = tuple(ones(scan.nchan()))
 | 
|---|
| 193 |     f = fitter()
 | 
|---|
| 194 |     f._verbose(True)
 | 
|---|
| 195 |     f.set_scan(scan, mask)
 | 
|---|
| 196 |     f.set_function(poly=order)    
 | 
|---|
| 197 |     sf = f.auto_fit()
 | 
|---|
| 198 |     return sf
 | 
|---|