source: trunk/python/asapmath.py@ 182

Last change on this file since 182 was 180, checked in by kil064, 20 years ago

add function 'smooth' and remove function 'hanning'

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.1 KB
RevLine 
[101]1from scantable import scantable
2
[143]3def average_time(*args, **kwargs):
[101]4 """
[113]5 Return the (time) average of a scan or list of scans. [in channels only]
6 Parameters:
7 one scan or comma separated scans
[143]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'
[113]13 Example:
14 # return a time averaged scan from scana and scanb
15 # without using a mask
[129]16 scanav = average_time(scana,scanb)
[113]17 # return the (time) averaged scan, i.e. the average of
18 # all correlator cycles
19 scanav = average_time(scan)
[143]20
[101]21 """
[143]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
[113]36 for s in lst:
[101]37 if not isinstance(s,scantable):
38 print "Please give a list of scantables"
39 return
[143]40 return scantable(_av(lst, mask, scanAv, weight))
[101]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
[150]52def scale(scan, factor, insitu=False, all=True):
[101]53 """
54 Return a scan where all spectra are scaled by the give 'factor'
55 Parameters:
56 scan: a scantable
[113]57 factor: the scaling factor
[150]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
[101]62 """
[141]63 if not insitu:
64 from asap._asap import scale as _scale
[150]65 return scantable(_scale(scan, factor, all))
[141]66 else:
67 from asap._asap import scale_insitu as _scale
[150]68 _scale(scan, factor, all)
[141]69 return
70
[101]71
[150]72def add(scan, offset, insitu=False, all=True):
[113]73 """
[150]74 Return a scan where all spectra have the offset added
[113]75 Parameters:
76 scan: a scantable
[150]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
[113]82 """
[150]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
[167]91def bin(scan, width=5, insitu=False):
[101]92 """
[167]93 Return a scan where all spectra have been binned up
[172]94 width: The bin width (default=5) in pixels
[167]95 insitu: if False (default) a new scantable is returned.
96 Otherwise, the addition is done in-situ
[101]97 """
[167]98 if not insitu:
99 from asap._asap import bin as _bin
100 return scantable(_bin(scan, width))
101 else:
102 from asap._asap import bin_insitu as _bin
103 _bin(scan, width)
104 return
[113]105
[166]106def average_pol(scan, mask=None, insitu=False):
[113]107 """
108 Average the Polarisations together.
109 Parameters:
[172]110 scan: The scantable
111 mask: An optional mask defining the region, where the
112 averaging will be applied. The output will have all
113 specified points masked.
114 insitu: If False (default) a new scantable is returned.
[166]115 Otherwise, the averaging is done in-situ
[113]116 Example:
117 polav = average_pols(myscan)
118 """
119 if mask is None:
[166]120 mask = ()
121 if not insitu:
122 from asap._asap import averagepol as _avpol
123 return scantable(_avpol(scan, mask))
124 else:
125 from asap._asap import averagepol_insitu as _avpol
126 _avpol(scan, mask)
127 return
[113]128
[180]129def smooth(scan, kernel="hanning", width=5.0, insitu=False, all=True):
[113]130 """
[180]131 Smooth the spectrum by the specified kernel (conserving flux).
[113]132 Parameters:
[172]133 scan: The input scan
[180]134 kernel: The type of smoothing kernel. Select from
135 'hanning' (default), 'gaussian' and 'boxcar'.
136 The first three characters are sufficient.
137 width: The width of the kernel in pixels. For hanning this is
138 ignored otherwise it defauls to 5 pixels.
139 For 'gaussian' it is the Full Width Half
140 Maximum. For 'boxcar' it is the full width.
[172]141 insitu: If False (default) a new scantable is returned.
142 Otherwise, the scaling is done in-situ
[180]143 all: If True (default) apply to all spectra. Otherwise
144 apply only to the selected (beam/pol/if)spectra only
[113]145 Example:
146 none
147 """
[172]148 if not insitu:
[180]149 from asap._asap import smooth as _smooth
150 return scantable(_smooth(scan,kernel,width,all))
[172]151 else:
[180]152 from asap._asap import smooth_insitu as _smooth
153 _smooth(scan,kernel,width,all)
[172]154 return
[113]155
156def poly_baseline(scan, mask=None, order=0):
157 """
[160]158 Return a scan which has been baselined (all rows) by a polynomial.
[113]159 Parameters:
160 scan: a scantable
161 mask: an optional mask
162 order: the order of the polynomial (default is 0)
163 Example:
164 # return a scan baselined by a third order polynomial,
165 # not using a mask
166 bscan = poly_baseline(scan, order=3)
167 """
168 from asap.asapfitter import fitter
169 if mask is None:
170 from numarray import ones
171 mask = tuple(ones(scan.nchan()))
172 f = fitter()
173 f._verbose(True)
174 f.set_scan(scan, mask)
175 f.set_function(poly=order)
176 sf = f.auto_fit()
177 return sf
Note: See TracBrowser for help on using the repository browser.