source: trunk/python/scantable.py@ 2968

Last change on this file since 2968 was 2966, checked in by WataruKawasaki, 10 years ago

New Development: No

JIRA Issue: Yes CAS-6598

Ready for Test: Yes

Interface Changes: Yes

What Interface Changed: method parameter (removed)

Test Programs: test_sdscale

Put in Release Notes: No

Module(s): sd

Description: removed a parameter of sd.scantable.scale(), skip_flaggedrow, and also changed the default behaviour of the method to skip scaling for row-flagged spectra.


  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 208.2 KB
RevLine 
[1846]1"""This module defines the scantable class."""
2
[1697]3import os
[2751]4import re
[2315]5import tempfile
[1948]6import numpy
[1691]7try:
8 from functools import wraps as wraps_dec
9except ImportError:
10 from asap.compatibility import wraps as wraps_dec
11
[1824]12from asap.env import is_casapy
[876]13from asap._asap import Scantable
[2004]14from asap._asap import filler, msfiller
[1824]15from asap.parameters import rcParams
[1862]16from asap.logging import asaplog, asaplog_post_dec
[1824]17from asap.selector import selector
18from asap.linecatalog import linecatalog
[1600]19from asap.coordinate import coordinate
[1859]20from asap.utils import _n_bools, mask_not, mask_and, mask_or, page
[1907]21from asap.asapfitter import fitter
[102]22
[1689]23def preserve_selection(func):
[1691]24 @wraps_dec(func)
[1689]25 def wrap(obj, *args, **kw):
26 basesel = obj.get_selection()
[1857]27 try:
28 val = func(obj, *args, **kw)
29 finally:
30 obj.set_selection(basesel)
[1689]31 return val
32 return wrap
33
[1846]34def is_scantable(filename):
35 """Is the given file a scantable?
[1689]36
[1846]37 Parameters:
38
39 filename: the name of the file/directory to test
40
41 """
[1883]42 if ( os.path.isdir(filename)
43 and os.path.exists(filename+'/table.info')
44 and os.path.exists(filename+'/table.dat') ):
45 f=open(filename+'/table.info')
46 l=f.readline()
47 f.close()
[2753]48 match_pattern = '^Type = (Scantable)? *$'
[2751]49 if re.match(match_pattern,l):
[1883]50 return True
51 else:
52 return False
53 else:
54 return False
55## return (os.path.isdir(filename)
56## and not os.path.exists(filename+'/table.f1')
57## and os.path.exists(filename+'/table.info'))
[1697]58
[1883]59def is_ms(filename):
60 """Is the given file a MeasurementSet?
[1697]61
[1883]62 Parameters:
63
64 filename: the name of the file/directory to test
65
66 """
67 if ( os.path.isdir(filename)
68 and os.path.exists(filename+'/table.info')
69 and os.path.exists(filename+'/table.dat') ):
70 f=open(filename+'/table.info')
71 l=f.readline()
72 f.close()
73 if ( l.find('Measurement Set') != -1 ):
74 return True
75 else:
76 return False
77 else:
78 return False
[2186]79
80def normalise_edge_param(edge):
81 """\
82 Convert a given edge value to a one-dimensional array that can be
83 given to baseline-fitting/subtraction functions.
84 The length of the output value will be an even because values for
85 the both sides of spectra are to be contained for each IF. When
86 the length is 2, the values will be applied to all IFs. If the length
87 is larger than 2, it will be 2*ifnos().
88 Accepted format of edge include:
89 * an integer - will be used for both sides of spectra of all IFs.
90 e.g. 10 is converted to [10,10]
[2277]91 * an empty list/tuple [] - converted to [0, 0] and used for all IFs.
[2186]92 * a list/tuple containing an integer - same as the above case.
93 e.g. [10] is converted to [10,10]
94 * a list/tuple containing two integers - will be used for all IFs.
95 e.g. [5,10] is output as it is. no need to convert.
96 * a list/tuple of lists/tuples containing TWO integers -
97 each element of edge will be used for each IF.
[2277]98 e.g. [[5,10],[15,20]] - [5,10] for IF[0] and [15,20] for IF[1].
99
100 If an element contains the same integer values, the input 'edge'
101 parameter can be given in a simpler shape in the following cases:
[2186]102 ** when len(edge)!=2
[2277]103 any elements containing the same values can be replaced
104 to single integers.
105 e.g. [[15,15]] can be simplified to [15] (or [15,15] or 15 also).
106 e.g. [[1,1],[2,2],[3,3]] can be simplified to [1,2,3].
[2186]107 ** when len(edge)=2
108 care is needed for this case: ONLY ONE of the
109 elements can be a single integer,
110 e.g. [[5,5],[10,10]] can be simplified to [5,[10,10]]
[2277]111 or [[5,5],10], but can NOT be simplified to [5,10].
[2186]112 when [5,10] given, it is interpreted as
[2277]113 [[5,10],[5,10],[5,10],...] instead, as shown before.
[2186]114 """
115 from asap import _is_sequence_or_number as _is_valid
116 if isinstance(edge, list) or isinstance(edge, tuple):
117 for edgepar in edge:
118 if not _is_valid(edgepar, int):
119 raise ValueError, "Each element of the 'edge' tuple has \
120 to be a pair of integers or an integer."
121 if isinstance(edgepar, list) or isinstance(edgepar, tuple):
122 if len(edgepar) != 2:
123 raise ValueError, "Each element of the 'edge' tuple has \
124 to be a pair of integers or an integer."
125 else:
126 if not _is_valid(edge, int):
127 raise ValueError, "Parameter 'edge' has to be an integer or a \
128 pair of integers specified as a tuple. \
129 Nested tuples are allowed \
130 to make individual selection for different IFs."
131
132
133 if isinstance(edge, int):
134 edge = [ edge, edge ] # e.g. 3 => [3,3]
135 elif isinstance(edge, list) or isinstance(edge, tuple):
136 if len(edge) == 0:
137 edge = [0, 0] # e.g. [] => [0,0]
138 elif len(edge) == 1:
139 if isinstance(edge[0], int):
140 edge = [ edge[0], edge[0] ] # e.g. [1] => [1,1]
141
142 commonedge = True
143 if len(edge) > 2: commonedge = False
144 else:
145 for edgepar in edge:
146 if isinstance(edgepar, list) or isinstance(edgepar, tuple):
147 commonedge = False
148 break
149
150 if commonedge:
151 if len(edge) > 1:
152 norm_edge = edge
153 else:
154 norm_edge = edge + edge
155 else:
156 norm_edge = []
157 for edgepar in edge:
158 if isinstance(edgepar, int):
159 norm_edge += [edgepar, edgepar]
160 else:
161 norm_edge += edgepar
162
163 return norm_edge
164
165def raise_fitting_failure_exception(e):
166 msg = "The fit failed, possibly because it didn't converge."
167 if rcParams["verbose"]:
168 asaplog.push(str(e))
169 asaplog.push(str(msg))
170 else:
171 raise RuntimeError(str(e)+'\n'+msg)
172
[2189]173def pack_progress_params(showprogress, minnrow):
174 return str(showprogress).lower() + ',' + str(minnrow)
175
[2767]176def pack_blinfo(blinfo, maxirow):
177 """\
178 convert a dictionary or a list of dictionaries of baseline info
179 into a list of comma-separated strings.
180 """
181 if isinstance(blinfo, dict):
182 res = do_pack_blinfo(blinfo, maxirow)
183 return [res] if res != '' else []
184 elif isinstance(blinfo, list) or isinstance(blinfo, tuple):
185 res = []
186 for i in xrange(len(blinfo)):
187 resi = do_pack_blinfo(blinfo[i], maxirow)
188 if resi != '':
189 res.append(resi)
190 return res
191
192def do_pack_blinfo(blinfo, maxirow):
193 """\
194 convert a dictionary of baseline info for a spectrum into
195 a comma-separated string.
196 """
197 dinfo = {}
198 for key in ['row', 'blfunc', 'masklist']:
199 if blinfo.has_key(key):
200 val = blinfo[key]
201 if key == 'row':
202 irow = val
203 if isinstance(val, list) or isinstance(val, tuple):
204 slval = []
205 for i in xrange(len(val)):
206 if isinstance(val[i], list) or isinstance(val[i], tuple):
207 for j in xrange(len(val[i])):
208 slval.append(str(val[i][j]))
209 else:
210 slval.append(str(val[i]))
211 sval = ",".join(slval)
212 else:
213 sval = str(val)
214
215 dinfo[key] = sval
216 else:
217 raise ValueError("'"+key+"' is missing in blinfo.")
218
219 if irow >= maxirow: return ''
220
221 for key in ['order', 'npiece', 'nwave']:
222 if blinfo.has_key(key):
223 val = blinfo[key]
224 if isinstance(val, list) or isinstance(val, tuple):
225 slval = []
226 for i in xrange(len(val)):
227 slval.append(str(val[i]))
228 sval = ",".join(slval)
229 else:
230 sval = str(val)
231 dinfo[key] = sval
232
233 blfunc = dinfo['blfunc']
234 fspec_keys = {'poly': 'order', 'chebyshev': 'order', 'cspline': 'npiece', 'sinusoid': 'nwave'}
235
236 fspec_key = fspec_keys[blfunc]
237 if not blinfo.has_key(fspec_key):
238 raise ValueError("'"+fspec_key+"' is missing in blinfo.")
239
240 clip_params_n = 0
241 for key in ['clipthresh', 'clipniter']:
242 if blinfo.has_key(key):
243 clip_params_n += 1
244 dinfo[key] = str(blinfo[key])
245
246 if clip_params_n == 0:
247 dinfo['clipthresh'] = '0.0'
248 dinfo['clipniter'] = '0'
249 elif clip_params_n != 2:
250 raise ValueError("both 'clipthresh' and 'clipniter' must be given for n-sigma clipping.")
251
252 lf_params_n = 0
253 for key in ['thresh', 'edge', 'chan_avg_limit']:
254 if blinfo.has_key(key):
255 lf_params_n += 1
256 val = blinfo[key]
257 if isinstance(val, list) or isinstance(val, tuple):
258 slval = []
259 for i in xrange(len(val)):
260 slval.append(str(val[i]))
261 sval = ",".join(slval)
262 else:
263 sval = str(val)
264 dinfo[key] = sval
265
266 if lf_params_n == 3:
267 dinfo['use_linefinder'] = 'true'
[2810]268 elif lf_params_n == 0:
[2767]269 dinfo['use_linefinder'] = 'false'
270 dinfo['thresh'] = ''
271 dinfo['edge'] = ''
272 dinfo['chan_avg_limit'] = ''
273 else:
274 raise ValueError("all of 'thresh', 'edge' and 'chan_avg_limit' must be given to use linefinder.")
275
276 slblinfo = [dinfo['row'], blfunc, dinfo[fspec_key], dinfo['masklist'], \
277 dinfo['clipthresh'], dinfo['clipniter'], \
278 dinfo['use_linefinder'], dinfo['thresh'], dinfo['edge'], dinfo['chan_avg_limit']]
279
280 return ":".join(slblinfo)
281
282def parse_fitresult(sres):
283 """\
284 Parse the returned value of apply_bltable() or sub_baseline() and
285 extract row number, the best-fit coefficients and rms, then pack
286 them into a dictionary.
287 The input value is generated by Scantable::packFittingResults() and
288 formatted as 'row:coeff[0],coeff[1],..,coeff[n-1]:rms'.
289 """
290 res = []
291 for i in xrange(len(sres)):
292 (srow, scoeff, srms) = sres[i].split(":")
293 row = int(srow)
294 rms = float(srms)
295 lscoeff = scoeff.split(",")
296 coeff = []
297 for j in xrange(len(lscoeff)):
298 coeff.append(float(lscoeff[j]))
299 res.append({'row': row, 'coeff': coeff, 'rms': rms})
300
301 return res
302
[2882]303def is_number(s):
304 s = s.strip()
305 res = True
306 try:
307 a = float(s)
308 res = True
309 except:
310 res = False
311 finally:
312 return res
313
314def is_frequency(s):
315 s = s.strip()
316 return (s[-2:].lower() == "hz")
317
[2884]318def get_freq_by_string(s1, s2):
319 if not (is_number(s1) and is_frequency(s2)):
[2882]320 raise RuntimeError("Invalid input string.")
321
322 prefix_list = ["a", "f", "p", "n", "u", "m", ".", "k", "M", "G", "T", "P", "E"]
323 factor_list = [1e-18, 1e-15, 1e-12, 1e-9, 1e-6, 1e-3, 1.0, 1e+3, 1e+6, 1e+9, 1e+12, 1e+15, 1e+18]
324
[2884]325 s1 = s1.strip()
326 s2 = s2.strip()
[2882]327
[2884]328 prefix = s2[-3:-2]
[2882]329 if is_number(prefix):
[2884]330 res1 = float(s1)
331 res2 = float(s2[:-2])
[2882]332 else:
[2884]333 factor = factor_list[prefix_list.index(prefix)]
334 res1 = float(s1) * factor
335 res2 = float(s2[:-3]) * factor
[2882]336
[2884]337 return (res1, res2)
[2882]338
339def is_velocity(s):
340 s = s.strip()
341 return (s[-3:].lower() == "m/s")
342
[2884]343def get_velocity_by_string(s1, s2):
344 if not (is_number(s1) and is_velocity(s2)):
[2882]345 raise RuntimeError("Invalid input string.")
346
[2884]347 # note that the default velocity unit is km/s
[2882]348 prefix_list = [".", "k"]
349 factor_list = [1e-3, 1.0]
350
[2884]351 s1 = s1.strip()
352 s2 = s2.strip()
[2882]353
[2884]354 prefix = s2[-4:-3]
355 if is_number(prefix): # in case velocity unit m/s
356 res1 = float(s1) * 1e-3
357 res2 = float(s2[:-3]) * 1e-3
[2882]358 else:
[2884]359 factor = factor_list[prefix_list.index(prefix)]
360 res1 = float(s1) * factor
361 res2 = float(s2[:-4]) * factor
[2882]362
[2884]363 return (res1, res2)
[2882]364
[2889]365def get_frequency_by_velocity(restfreq, vel, doppler):
[2882]366 # vel is in unit of km/s
367
368 # speed of light
369 vel_c = 299792.458
370
371 import math
372 r = vel / vel_c
373
[2889]374 if doppler.lower() == 'radio':
375 return restfreq * (1.0 - r)
376 if doppler.lower() == 'optical':
377 return restfreq / (1.0 + r)
378 else:
379 return restfreq * math.sqrt((1.0 - r) / (1.0 + r))
[2882]380
[2891]381def get_restfreq_in_Hz(s_restfreq):
382 value = 0.0
383 unit = ""
384 s = s_restfreq.replace(" ","")
[2889]385
[2891]386 for i in range(len(s))[::-1]:
387 if s[i].isalpha():
388 unit = s[i] + unit
389 else:
390 value = float(s[0:i+1])
391 break
392
393 if (unit == "") or (unit.lower() == "hz"):
394 return value
395 elif (len(unit) == 3) and (unit[1:3].lower() == "hz"):
396 unitprefix = unit[0]
397 factor = 1.0
398
399 prefix_list = ["a", "f", "p", "n", "u", "m", ".", "k", "M", "G", "T", "P", "E"]
400 factor_list = [1e-18, 1e-15, 1e-12, 1e-9, 1e-6, 1e-3, 1.0, 1e+3, 1e+6, 1e+9, 1e+12, 1e+15, 1e+18]
401 factor = factor_list[prefix_list.index(unitprefix)]
402 """
403 if (unitprefix == 'a'):
404 factor = 1.0e-18
405 elif (unitprefix == 'f'):
406 factor = 1.0e-15
407 elif (unitprefix == 'p'):
408 factor = 1.0e-12
409 elif (unitprefix == 'n'):
410 factor = 1.0e-9
411 elif (unitprefix == 'u'):
412 factor = 1.0e-6
413 elif (unitprefix == 'm'):
414 factor = 1.0e-3
415 elif (unitprefix == 'k'):
416 factor = 1.0e+3
417 elif (unitprefix == 'M'):
418 factor = 1.0e+6
419 elif (unitprefix == 'G'):
420 factor = 1.0e+9
421 elif (unitprefix == 'T'):
422 factor = 1.0e+12
423 elif (unitprefix == 'P'):
424 factor = 1.0e+15
425 elif (unitprefix == 'E'):
426 factor = 1.0e+18
427 """
428 return value*factor
429 else:
430 mesg = "wrong unit of restfreq."
431 raise Exception, mesg
432
433def normalise_restfreq(in_restfreq):
434 if isinstance(in_restfreq, float):
435 return in_restfreq
436 elif isinstance(in_restfreq, int) or isinstance(in_restfreq, long):
437 return float(in_restfreq)
438 elif isinstance(in_restfreq, str):
439 return get_restfreq_in_Hz(in_restfreq)
440 elif isinstance(in_restfreq, list) or isinstance(in_restfreq, numpy.ndarray):
441 if isinstance(in_restfreq, numpy.ndarray):
442 if len(in_restfreq.shape) > 1:
443 mesg = "given in numpy.ndarray, in_restfreq must be 1-D."
444 raise Exception, mesg
445
446 res = []
447 for i in xrange(len(in_restfreq)):
448 elem = in_restfreq[i]
449 if isinstance(elem, float):
450 res.append(elem)
451 elif isinstance(elem, int) or isinstance(elem, long):
452 res.append(float(elem))
453 elif isinstance(elem, str):
454 res.append(get_restfreq_in_Hz(elem))
455 elif isinstance(elem, dict):
456 if isinstance(elem["value"], float):
457 res.append(elem)
458 elif isinstance(elem["value"], int):
459 dictelem = {}
460 dictelem["name"] = elem["name"]
461 dictelem["value"] = float(elem["value"])
462 res.append(dictelem)
463 elif isinstance(elem["value"], str):
464 dictelem = {}
465 dictelem["name"] = elem["name"]
466 dictelem["value"] = get_restfreq_in_Hz(elem["value"])
467 res.append(dictelem)
468 else:
469 mesg = "restfreq elements must be float, int, or string."
470 raise Exception, mesg
471 return res
472 else:
473 mesg = "wrong type of restfreq given."
474 raise Exception, mesg
475
476def set_restfreq(s, restfreq):
477 rfset = (restfreq != '') and (restfreq != [])
478 if rfset:
479 s.set_restfreqs(normalise_restfreq(restfreq))
480
[876]481class scantable(Scantable):
[1846]482 """\
483 The ASAP container for scans (single-dish data).
[102]484 """
[1819]485
[1862]486 @asaplog_post_dec
[2315]487 def __init__(self, filename, average=None, unit=None, parallactify=None,
488 **args):
[1846]489 """\
[102]490 Create a scantable from a saved one or make a reference
[1846]491
[102]492 Parameters:
[1846]493
494 filename: the name of an asap table on disk
495 or
496 the name of a rpfits/sdfits/ms file
497 (integrations within scans are auto averaged
498 and the whole file is read) or
499 [advanced] a reference to an existing scantable
500
501 average: average all integrations withinb a scan on read.
502 The default (True) is taken from .asaprc.
503
[484]504 unit: brightness unit; must be consistent with K or Jy.
[1846]505 Over-rides the default selected by the filler
506 (input rpfits/sdfits/ms) or replaces the value
507 in existing scantables
508
[1920]509 antenna: for MeasurementSet input data only:
[2349]510 Antenna selection. integer (id) or string
511 (name or id).
[1846]512
[2349]513 parallactify: Indicate that the data had been parallactified.
514 Default (false) is taken from rc file.
[1846]515
[2754]516 getpt: Whether to import direction from MS/POINTING
517 table properly or not.
518 This is effective only when filename is MS.
519 The default (True) is to import direction
520 from MS/POINTING.
[710]521 """
[976]522 if average is None:
[710]523 average = rcParams['scantable.autoaverage']
[1593]524 parallactify = parallactify or rcParams['scantable.parallactify']
[1259]525 varlist = vars()
[876]526 from asap._asap import stmath
[1819]527 self._math = stmath( rcParams['insitu'] )
[876]528 if isinstance(filename, Scantable):
529 Scantable.__init__(self, filename)
[181]530 else:
[1697]531 if isinstance(filename, str):
[976]532 filename = os.path.expandvars(filename)
533 filename = os.path.expanduser(filename)
534 if not os.path.exists(filename):
535 s = "File '%s' not found." % (filename)
536 raise IOError(s)
[1697]537 if is_scantable(filename):
538 ondisk = rcParams['scantable.storage'] == 'disk'
539 Scantable.__init__(self, filename, ondisk)
540 if unit is not None:
541 self.set_fluxunit(unit)
[2008]542 if average:
543 self._assign( self.average_time( scanav=True ) )
[1819]544 # do not reset to the default freqframe
545 #self.set_freqframe(rcParams['scantable.freqframe'])
[1883]546 elif is_ms(filename):
[1916]547 # Measurement Set
548 opts={'ms': {}}
[2844]549 mskeys=['getpt','antenna']
[1916]550 for key in mskeys:
551 if key in args.keys():
552 opts['ms'][key] = args[key]
553 self._fill([filename], unit, average, opts)
[1893]554 elif os.path.isfile(filename):
[2761]555 opts={'nro': {}}
556 nrokeys=['freqref']
557 for key in nrokeys:
558 if key in args.keys():
559 opts['nro'][key] = args[key]
560 self._fill([filename], unit, average, opts)
[2350]561 # only apply to new data not "copy constructor"
562 self.parallactify(parallactify)
[1883]563 else:
[1819]564 msg = "The given file '%s'is not a valid " \
565 "asap table." % (filename)
[1859]566 raise IOError(msg)
[1118]567 elif (isinstance(filename, list) or isinstance(filename, tuple)) \
[976]568 and isinstance(filename[-1], str):
[1916]569 self._fill(filename, unit, average)
[1586]570 self.parallactify(parallactify)
[1259]571 self._add_history("scantable", varlist)
[102]572
[1862]573 @asaplog_post_dec
[876]574 def save(self, name=None, format=None, overwrite=False):
[1846]575 """\
[1280]576 Store the scantable on disk. This can be an asap (aips++) Table,
577 SDFITS or MS2 format.
[1846]578
[116]579 Parameters:
[1846]580
[2431]581 name: the name of the outputfile. For format 'ASCII'
[1093]582 this is the root file name (data in 'name'.txt
[497]583 and header in 'name'_header.txt)
[1855]584
[116]585 format: an optional file format. Default is ASAP.
[1855]586 Allowed are:
587
588 * 'ASAP' (save as ASAP [aips++] Table),
589 * 'SDFITS' (save as SDFITS file)
590 * 'ASCII' (saves as ascii text file)
591 * 'MS2' (saves as an casacore MeasurementSet V2)
[2315]592 * 'FITS' (save as image FITS - not readable by
593 class)
[1855]594 * 'CLASS' (save as FITS readable by CLASS)
595
[411]596 overwrite: If the file should be overwritten if it exists.
[256]597 The default False is to return with warning
[411]598 without writing the output. USE WITH CARE.
[1855]599
[1846]600 Example::
601
[116]602 scan.save('myscan.asap')
[1118]603 scan.save('myscan.sdfits', 'SDFITS')
[1846]604
[116]605 """
[411]606 from os import path
[1593]607 format = format or rcParams['scantable.save']
[256]608 suffix = '.'+format.lower()
[1118]609 if name is None or name == "":
[256]610 name = 'scantable'+suffix
[718]611 msg = "No filename given. Using default name %s..." % name
612 asaplog.push(msg)
[411]613 name = path.expandvars(name)
[256]614 if path.isfile(name) or path.isdir(name):
615 if not overwrite:
[718]616 msg = "File %s exists." % name
[1859]617 raise IOError(msg)
[451]618 format2 = format.upper()
619 if format2 == 'ASAP':
[116]620 self._save(name)
[2029]621 elif format2 == 'MS2':
622 msopt = {'ms': {'overwrite': overwrite } }
623 from asap._asap import mswriter
624 writer = mswriter( self )
625 writer.write( name, msopt )
[116]626 else:
[989]627 from asap._asap import stwriter as stw
[1118]628 writer = stw(format2)
629 writer.write(self, name)
[116]630 return
631
[102]632 def copy(self):
[1846]633 """Return a copy of this scantable.
634
635 *Note*:
636
[1348]637 This makes a full (deep) copy. scan2 = scan1 makes a reference.
[1846]638
639 Example::
640
[102]641 copiedscan = scan.copy()
[1846]642
[102]643 """
[876]644 sd = scantable(Scantable._copy(self))
[113]645 return sd
646
[1093]647 def drop_scan(self, scanid=None):
[1846]648 """\
[1093]649 Return a new scantable where the specified scan number(s) has(have)
650 been dropped.
[1846]651
[1093]652 Parameters:
[1846]653
[1093]654 scanid: a (list of) scan number(s)
[1846]655
[1093]656 """
657 from asap import _is_sequence_or_number as _is_valid
658 from asap import _to_list
659 from asap import unique
660 if not _is_valid(scanid):
[2315]661 raise RuntimeError( 'Please specify a scanno to drop from the'
662 ' scantable' )
[1859]663 scanid = _to_list(scanid)
664 allscans = unique([ self.getscan(i) for i in range(self.nrow())])
665 for sid in scanid: allscans.remove(sid)
666 if len(allscans) == 0:
667 raise ValueError("Can't remove all scans")
668 sel = selector(scans=allscans)
669 return self._select_copy(sel)
[1093]670
[1594]671 def _select_copy(self, selection):
672 orig = self.get_selection()
673 self.set_selection(orig+selection)
674 cp = self.copy()
675 self.set_selection(orig)
676 return cp
677
[102]678 def get_scan(self, scanid=None):
[1855]679 """\
[102]680 Return a specific scan (by scanno) or collection of scans (by
681 source name) in a new scantable.
[1846]682
683 *Note*:
684
[1348]685 See scantable.drop_scan() for the inverse operation.
[1846]686
[102]687 Parameters:
[1846]688
[513]689 scanid: a (list of) scanno or a source name, unix-style
690 patterns are accepted for source name matching, e.g.
691 '*_R' gets all 'ref scans
[1846]692
693 Example::
694
[513]695 # get all scans containing the source '323p459'
696 newscan = scan.get_scan('323p459')
697 # get all 'off' scans
698 refscans = scan.get_scan('*_R')
699 # get a susbset of scans by scanno (as listed in scan.summary())
[1118]700 newscan = scan.get_scan([0, 2, 7, 10])
[1846]701
[102]702 """
703 if scanid is None:
[1859]704 raise RuntimeError( 'Please specify a scan no or name to '
705 'retrieve from the scantable' )
[102]706 try:
[946]707 bsel = self.get_selection()
708 sel = selector()
[102]709 if type(scanid) is str:
[946]710 sel.set_name(scanid)
[1594]711 return self._select_copy(sel)
[102]712 elif type(scanid) is int:
[946]713 sel.set_scans([scanid])
[1594]714 return self._select_copy(sel)
[381]715 elif type(scanid) is list:
[946]716 sel.set_scans(scanid)
[1594]717 return self._select_copy(sel)
[381]718 else:
[718]719 msg = "Illegal scanid type, use 'int' or 'list' if ints."
[1859]720 raise TypeError(msg)
[102]721 except RuntimeError:
[1859]722 raise
[102]723
724 def __str__(self):
[2315]725 tempFile = tempfile.NamedTemporaryFile()
726 Scantable._summary(self, tempFile.name)
727 tempFile.seek(0)
728 asaplog.clear()
729 return tempFile.file.read()
[102]730
[2315]731 @asaplog_post_dec
[976]732 def summary(self, filename=None):
[1846]733 """\
[102]734 Print a summary of the contents of this scantable.
[1846]735
[102]736 Parameters:
[1846]737
[1931]738 filename: the name of a file to write the putput to
[102]739 Default - no file output
[1846]740
[102]741 """
742 if filename is not None:
[256]743 if filename is "":
744 filename = 'scantable_summary.txt'
[415]745 from os.path import expandvars, isdir
[411]746 filename = expandvars(filename)
[2286]747 if isdir(filename):
[718]748 msg = "Illegal file name '%s'." % (filename)
[1859]749 raise IOError(msg)
[2286]750 else:
751 filename = ""
752 Scantable._summary(self, filename)
[710]753
[1512]754 def get_spectrum(self, rowno):
[1471]755 """Return the spectrum for the current row in the scantable as a list.
[1846]756
[1471]757 Parameters:
[1846]758
[1573]759 rowno: the row number to retrieve the spectrum from
[1846]760
[1471]761 """
762 return self._getspectrum(rowno)
[946]763
[1471]764 def get_mask(self, rowno):
765 """Return the mask for the current row in the scantable as a list.
[1846]766
[1471]767 Parameters:
[1846]768
[1573]769 rowno: the row number to retrieve the mask from
[1846]770
[1471]771 """
772 return self._getmask(rowno)
773
774 def set_spectrum(self, spec, rowno):
[1938]775 """Set the spectrum for the current row in the scantable.
[1846]776
[1471]777 Parameters:
[1846]778
[1855]779 spec: the new spectrum
[1846]780
[1855]781 rowno: the row number to set the spectrum for
782
[1471]783 """
[2348]784 assert(len(spec) == self.nchan(self.getif(rowno)))
[1471]785 return self._setspectrum(spec, rowno)
786
[1600]787 def get_coordinate(self, rowno):
788 """Return the (spectral) coordinate for a a given 'rowno'.
[1846]789
790 *Note*:
791
[1600]792 * This coordinate is only valid until a scantable method modifies
793 the frequency axis.
794 * This coordinate does contain the original frequency set-up
795 NOT the new frame. The conversions however are done using the user
796 specified frame (e.g. LSRK/TOPO). To get the 'real' coordinate,
797 use scantable.freq_align first. Without it there is no closure,
[1846]798 i.e.::
[1600]799
[1846]800 c = myscan.get_coordinate(0)
801 c.to_frequency(c.get_reference_pixel()) != c.get_reference_value()
802
[1600]803 Parameters:
[1846]804
[1600]805 rowno: the row number for the spectral coordinate
806
807 """
808 return coordinate(Scantable.get_coordinate(self, rowno))
809
[946]810 def get_selection(self):
[1846]811 """\
[1005]812 Get the selection object currently set on this scantable.
[1846]813
814 Example::
815
[1005]816 sel = scan.get_selection()
817 sel.set_ifs(0) # select IF 0
818 scan.set_selection(sel) # apply modified selection
[1846]819
[946]820 """
821 return selector(self._getselection())
822
[1576]823 def set_selection(self, selection=None, **kw):
[1846]824 """\
[1005]825 Select a subset of the data. All following operations on this scantable
826 are only applied to thi selection.
[1846]827
[1005]828 Parameters:
[1697]829
[1846]830 selection: a selector object (default unset the selection), or
[2431]831 any combination of 'pols', 'ifs', 'beams', 'scans',
832 'cycles', 'name', 'query'
[1697]833
[1846]834 Examples::
[1697]835
[1005]836 sel = selector() # create a selection object
[1118]837 self.set_scans([0, 3]) # select SCANNO 0 and 3
[1005]838 scan.set_selection(sel) # set the selection
839 scan.summary() # will only print summary of scanno 0 an 3
840 scan.set_selection() # unset the selection
[1697]841 # or the equivalent
842 scan.set_selection(scans=[0,3])
843 scan.summary() # will only print summary of scanno 0 an 3
844 scan.set_selection() # unset the selection
[1846]845
[946]846 """
[1576]847 if selection is None:
848 # reset
849 if len(kw) == 0:
850 selection = selector()
851 else:
852 # try keywords
853 for k in kw:
854 if k not in selector.fields:
[2320]855 raise KeyError("Invalid selection key '%s', "
856 "valid keys are %s" % (k,
857 selector.fields))
[1576]858 selection = selector(**kw)
[946]859 self._setselection(selection)
860
[1819]861 def get_row(self, row=0, insitu=None):
[1846]862 """\
[1819]863 Select a row in the scantable.
864 Return a scantable with single row.
[1846]865
[1819]866 Parameters:
[1846]867
868 row: row no of integration, default is 0.
869 insitu: if False a new scantable is returned. Otherwise, the
870 scaling is done in-situ. The default is taken from .asaprc
871 (False)
872
[1819]873 """
[2349]874 if insitu is None:
875 insitu = rcParams['insitu']
[1819]876 if not insitu:
877 workscan = self.copy()
878 else:
879 workscan = self
880 # Select a row
[2349]881 sel = selector()
[1992]882 sel.set_rows([row])
[1819]883 workscan.set_selection(sel)
884 if not workscan.nrow() == 1:
[2349]885 msg = "Could not identify single row. %d rows selected." \
886 % (workscan.nrow())
[1819]887 raise RuntimeError(msg)
888 if insitu:
889 self._assign(workscan)
890 else:
891 return workscan
892
[1862]893 @asaplog_post_dec
[2957]894 def stats(self, stat='stddev', mask=None, form='3.3f', row=None, skip_flaggedrow=False):
[1846]895 """\
[135]896 Determine the specified statistic of the current beam/if/pol
[102]897 Takes a 'mask' as an optional parameter to specify which
898 channels should be excluded.
[1846]899
[102]900 Parameters:
[1846]901
[1819]902 stat: 'min', 'max', 'min_abc', 'max_abc', 'sumsq', 'sum',
903 'mean', 'var', 'stddev', 'avdev', 'rms', 'median'
[1855]904
[135]905 mask: an optional mask specifying where the statistic
[102]906 should be determined.
[1855]907
[1819]908 form: format string to print statistic values
[1846]909
[1907]910 row: row number of spectrum to process.
911 (default is None: for all rows)
[1846]912
[2957]913 skip_flaggedrow: if True, skip outputting text for flagged
914 spectra. default is False.
915
[1907]916 Example:
[113]917 scan.set_unit('channel')
[1118]918 msk = scan.create_mask([100, 200], [500, 600])
[135]919 scan.stats(stat='mean', mask=m)
[1846]920
[102]921 """
[1593]922 mask = mask or []
[876]923 if not self._check_ifs():
[1118]924 raise ValueError("Cannot apply mask as the IFs have different "
925 "number of channels. Please use setselection() "
926 "to select individual IFs")
[1819]927 getchan = False
928 if stat.lower().startswith('min') or stat.lower().startswith('max'):
929 chan = self._math._minmaxchan(self, mask, stat)
930 getchan = True
931 statvals = []
[2957]932
933 rtnabc = False
934 if stat.lower().endswith('_abc'):
935 rtnabc = True
936 else:
[1907]937 if row == None:
938 statvals = self._math._stats(self, mask, stat)
939 else:
940 statvals = self._math._statsrow(self, mask, stat, int(row))
[256]941
[1819]942 #def cb(i):
943 # return statvals[i]
[256]944
[1819]945 #return self._row_callback(cb, stat)
[102]946
[1819]947 label=stat
948 #callback=cb
949 out = ""
950 #outvec = []
951 sep = '-'*50
[1907]952
953 if row == None:
954 rows = xrange(self.nrow())
955 elif isinstance(row, int):
956 rows = [ row ]
957
958 for i in rows:
[1819]959 refstr = ''
960 statunit= ''
961 if getchan:
962 qx, qy = self.chan2data(rowno=i, chan=chan[i])
963 if rtnabc:
964 statvals.append(qx['value'])
965 refstr = ('(value: %'+form) % (qy['value'])+' ['+qy['unit']+'])'
966 statunit= '['+qx['unit']+']'
967 else:
968 refstr = ('(@ %'+form) % (qx['value'])+' ['+qx['unit']+'])'
969
[2957]970 if skip_flaggedrow and self._getflagrow(i): continue
971
[1819]972 tm = self._gettime(i)
973 src = self._getsourcename(i)
974 out += 'Scan[%d] (%s) ' % (self.getscan(i), src)
975 out += 'Time[%s]:\n' % (tm)
[1907]976 if self.nbeam(-1) > 1: out += ' Beam[%d] ' % (self.getbeam(i))
977 if self.nif(-1) > 1: out += ' IF[%d] ' % (self.getif(i))
978 if self.npol(-1) > 1: out += ' Pol[%d] ' % (self.getpol(i))
[1819]979 #outvec.append(callback(i))
[1907]980 if len(rows) > 1:
981 # out += ('= %'+form) % (outvec[i]) +' '+refstr+'\n'
982 out += ('= %'+form) % (statvals[i]) +' '+refstr+'\n'
983 else:
984 # out += ('= %'+form) % (outvec[0]) +' '+refstr+'\n'
985 out += ('= %'+form) % (statvals[0]) +' '+refstr+'\n'
[1819]986 out += sep+"\n"
987
[1859]988 import os
989 if os.environ.has_key( 'USER' ):
990 usr = os.environ['USER']
991 else:
992 import commands
993 usr = commands.getoutput( 'whoami' )
994 tmpfile = '/tmp/tmp_'+usr+'_casapy_asap_scantable_stats'
995 f = open(tmpfile,'w')
996 print >> f, sep
997 print >> f, ' %s %s' % (label, statunit)
998 print >> f, sep
999 print >> f, out
1000 f.close()
1001 f = open(tmpfile,'r')
1002 x = f.readlines()
1003 f.close()
1004 asaplog.push(''.join(x), False)
1005
[1819]1006 return statvals
1007
1008 def chan2data(self, rowno=0, chan=0):
[1846]1009 """\
[1819]1010 Returns channel/frequency/velocity and spectral value
1011 at an arbitrary row and channel in the scantable.
[1846]1012
[1819]1013 Parameters:
[1846]1014
[1819]1015 rowno: a row number in the scantable. Default is the
1016 first row, i.e. rowno=0
[1855]1017
[1819]1018 chan: a channel in the scantable. Default is the first
1019 channel, i.e. pos=0
[1846]1020
[1819]1021 """
1022 if isinstance(rowno, int) and isinstance(chan, int):
1023 qx = {'unit': self.get_unit(),
1024 'value': self._getabcissa(rowno)[chan]}
1025 qy = {'unit': self.get_fluxunit(),
1026 'value': self._getspectrum(rowno)[chan]}
1027 return qx, qy
1028
[1118]1029 def stddev(self, mask=None):
[1846]1030 """\
[135]1031 Determine the standard deviation of the current beam/if/pol
1032 Takes a 'mask' as an optional parameter to specify which
1033 channels should be excluded.
[1846]1034
[135]1035 Parameters:
[1846]1036
[135]1037 mask: an optional mask specifying where the standard
1038 deviation should be determined.
1039
[1846]1040 Example::
1041
[135]1042 scan.set_unit('channel')
[1118]1043 msk = scan.create_mask([100, 200], [500, 600])
[135]1044 scan.stddev(mask=m)
[1846]1045
[135]1046 """
[1118]1047 return self.stats(stat='stddev', mask=mask);
[135]1048
[1003]1049
[1259]1050 def get_column_names(self):
[1846]1051 """\
[1003]1052 Return a list of column names, which can be used for selection.
1053 """
[1259]1054 return list(Scantable.get_column_names(self))
[1003]1055
[1730]1056 def get_tsys(self, row=-1):
[1846]1057 """\
[113]1058 Return the System temperatures.
[1846]1059
1060 Parameters:
1061
1062 row: the rowno to get the information for. (default all rows)
1063
[113]1064 Returns:
[1846]1065
[876]1066 a list of Tsys values for the current selection
[1846]1067
[113]1068 """
[1730]1069 if row > -1:
1070 return self._get_column(self._gettsys, row)
[876]1071 return self._row_callback(self._gettsys, "Tsys")
[256]1072
[2406]1073 def get_tsysspectrum(self, row=-1):
1074 """\
1075 Return the channel dependent system temperatures.
[1730]1076
[2406]1077 Parameters:
1078
1079 row: the rowno to get the information for. (default all rows)
1080
1081 Returns:
1082
1083 a list of Tsys values for the current selection
1084
1085 """
1086 return self._get_column( self._gettsysspectrum, row )
1087
[2791]1088 def set_tsys(self, values, row=-1):
1089 """\
1090 Set the Tsys value(s) of the given 'row' or the whole scantable
1091 (selection).
1092
1093 Parameters:
1094
1095 values: a scalar or list (if Tsys is a vector) of Tsys value(s)
1096 row: the row number to apply Tsys values to.
1097 (default all rows)
1098
1099 """
1100
1101 if not hasattr(values, "__len__"):
1102 values = [values]
1103 self._settsys(values, row)
1104
[1730]1105 def get_weather(self, row=-1):
[1846]1106 """\
[2930]1107 Return the weather information.
[1846]1108
1109 Parameters:
1110
1111 row: the rowno to get the information for. (default all rows)
1112
1113 Returns:
1114
1115 a dict or list of of dicts of values for the current selection
1116
1117 """
[2930]1118 if row >= len(self):
1119 raise IndexError("row out of range")
[1730]1120 values = self._get_column(self._get_weather, row)
1121 if row > -1:
1122 return {'temperature': values[0],
1123 'pressure': values[1], 'humidity' : values[2],
1124 'windspeed' : values[3], 'windaz' : values[4]
1125 }
1126 else:
1127 out = []
1128 for r in values:
1129 out.append({'temperature': r[0],
1130 'pressure': r[1], 'humidity' : r[2],
1131 'windspeed' : r[3], 'windaz' : r[4]
1132 })
1133 return out
1134
[876]1135 def _row_callback(self, callback, label):
1136 out = ""
[1118]1137 outvec = []
[1590]1138 sep = '-'*50
[876]1139 for i in range(self.nrow()):
1140 tm = self._gettime(i)
1141 src = self._getsourcename(i)
[1590]1142 out += 'Scan[%d] (%s) ' % (self.getscan(i), src)
[876]1143 out += 'Time[%s]:\n' % (tm)
[1590]1144 if self.nbeam(-1) > 1:
1145 out += ' Beam[%d] ' % (self.getbeam(i))
1146 if self.nif(-1) > 1: out += ' IF[%d] ' % (self.getif(i))
1147 if self.npol(-1) > 1: out += ' Pol[%d] ' % (self.getpol(i))
[876]1148 outvec.append(callback(i))
1149 out += '= %3.3f\n' % (outvec[i])
[1590]1150 out += sep+'\n'
[1859]1151
1152 asaplog.push(sep)
1153 asaplog.push(" %s" % (label))
1154 asaplog.push(sep)
1155 asaplog.push(out)
[1861]1156 asaplog.post()
[1175]1157 return outvec
[256]1158
[1947]1159 def _get_column(self, callback, row=-1, *args):
[1070]1160 """
1161 """
1162 if row == -1:
[1947]1163 return [callback(i, *args) for i in range(self.nrow())]
[1070]1164 else:
[1819]1165 if 0 <= row < self.nrow():
[1947]1166 return callback(row, *args)
[256]1167
[1070]1168
[1948]1169 def get_time(self, row=-1, asdatetime=False, prec=-1):
[1846]1170 """\
[113]1171 Get a list of time stamps for the observations.
[1938]1172 Return a datetime object or a string (default) for each
1173 integration time stamp in the scantable.
[1846]1174
[113]1175 Parameters:
[1846]1176
[1348]1177 row: row no of integration. Default -1 return all rows
[1855]1178
[1348]1179 asdatetime: return values as datetime objects rather than strings
[1846]1180
[2349]1181 prec: number of digits shown. Default -1 to automatic
1182 calculation.
[1948]1183 Note this number is equals to the digits of MVTime,
1184 i.e., 0<prec<3: dates with hh:: only,
1185 <5: with hh:mm:, <7 or 0: with hh:mm:ss,
[1947]1186 and 6> : with hh:mm:ss.tt... (prec-6 t's added)
1187
[113]1188 """
[1175]1189 from datetime import datetime
[1948]1190 if prec < 0:
1191 # automagically set necessary precision +1
[2349]1192 prec = 7 - \
1193 numpy.floor(numpy.log10(numpy.min(self.get_inttime(row))))
[1948]1194 prec = max(6, int(prec))
1195 else:
1196 prec = max(0, prec)
1197 if asdatetime:
1198 #precision can be 1 millisecond at max
1199 prec = min(12, prec)
1200
[1947]1201 times = self._get_column(self._gettime, row, prec)
[1348]1202 if not asdatetime:
[1392]1203 return times
[1947]1204 format = "%Y/%m/%d/%H:%M:%S.%f"
1205 if prec < 7:
1206 nsub = 1 + (((6-prec)/2) % 3)
1207 substr = [".%f","%S","%M"]
1208 for i in range(nsub):
1209 format = format.replace(substr[i],"")
[1175]1210 if isinstance(times, list):
[1947]1211 return [datetime.strptime(i, format) for i in times]
[1175]1212 else:
[1947]1213 return datetime.strptime(times, format)
[102]1214
[1348]1215
1216 def get_inttime(self, row=-1):
[1846]1217 """\
[1348]1218 Get a list of integration times for the observations.
1219 Return a time in seconds for each integration in the scantable.
[1846]1220
[1348]1221 Parameters:
[1846]1222
[1348]1223 row: row no of integration. Default -1 return all rows.
[1846]1224
[1348]1225 """
[1573]1226 return self._get_column(self._getinttime, row)
[1348]1227
[1573]1228
[714]1229 def get_sourcename(self, row=-1):
[1846]1230 """\
[794]1231 Get a list source names for the observations.
[714]1232 Return a string for each integration in the scantable.
1233 Parameters:
[1846]1234
[1348]1235 row: row no of integration. Default -1 return all rows.
[1846]1236
[714]1237 """
[1070]1238 return self._get_column(self._getsourcename, row)
[714]1239
[794]1240 def get_elevation(self, row=-1):
[1846]1241 """\
[794]1242 Get a list of elevations for the observations.
1243 Return a float for each integration in the scantable.
[1846]1244
[794]1245 Parameters:
[1846]1246
[1348]1247 row: row no of integration. Default -1 return all rows.
[1846]1248
[794]1249 """
[1070]1250 return self._get_column(self._getelevation, row)
[794]1251
1252 def get_azimuth(self, row=-1):
[1846]1253 """\
[794]1254 Get a list of azimuths for the observations.
1255 Return a float for each integration in the scantable.
[1846]1256
[794]1257 Parameters:
[1348]1258 row: row no of integration. Default -1 return all rows.
[1846]1259
[794]1260 """
[1070]1261 return self._get_column(self._getazimuth, row)
[794]1262
1263 def get_parangle(self, row=-1):
[1846]1264 """\
[794]1265 Get a list of parallactic angles for the observations.
1266 Return a float for each integration in the scantable.
[1846]1267
[794]1268 Parameters:
[1846]1269
[1348]1270 row: row no of integration. Default -1 return all rows.
[1846]1271
[794]1272 """
[1070]1273 return self._get_column(self._getparangle, row)
[794]1274
[1070]1275 def get_direction(self, row=-1):
1276 """
1277 Get a list of Positions on the sky (direction) for the observations.
[1594]1278 Return a string for each integration in the scantable.
[1855]1279
[1070]1280 Parameters:
[1855]1281
[1070]1282 row: row no of integration. Default -1 return all rows
[1855]1283
[1070]1284 """
1285 return self._get_column(self._getdirection, row)
1286
[1391]1287 def get_directionval(self, row=-1):
[1846]1288 """\
[1391]1289 Get a list of Positions on the sky (direction) for the observations.
1290 Return a float for each integration in the scantable.
[1846]1291
[1391]1292 Parameters:
[1846]1293
[1391]1294 row: row no of integration. Default -1 return all rows
[1846]1295
[1391]1296 """
1297 return self._get_column(self._getdirectionvec, row)
1298
[1862]1299 @asaplog_post_dec
[102]1300 def set_unit(self, unit='channel'):
[1846]1301 """\
[102]1302 Set the unit for all following operations on this scantable
[1846]1303
[102]1304 Parameters:
[1846]1305
1306 unit: optional unit, default is 'channel'. Use one of '*Hz',
1307 'km/s', 'channel' or equivalent ''
1308
[102]1309 """
[484]1310 varlist = vars()
[1118]1311 if unit in ['', 'pixel', 'channel']:
[113]1312 unit = ''
1313 inf = list(self._getcoordinfo())
1314 inf[0] = unit
1315 self._setcoordinfo(inf)
[1118]1316 self._add_history("set_unit", varlist)
[113]1317
[1862]1318 @asaplog_post_dec
[484]1319 def set_instrument(self, instr):
[1846]1320 """\
[1348]1321 Set the instrument for subsequent processing.
[1846]1322
[358]1323 Parameters:
[1846]1324
[710]1325 instr: Select from 'ATPKSMB', 'ATPKSHOH', 'ATMOPRA',
[407]1326 'DSS-43' (Tid), 'CEDUNA', and 'HOBART'
[1846]1327
[358]1328 """
1329 self._setInstrument(instr)
[1118]1330 self._add_history("set_instument", vars())
[358]1331
[1862]1332 @asaplog_post_dec
[1190]1333 def set_feedtype(self, feedtype):
[1846]1334 """\
[1190]1335 Overwrite the feed type, which might not be set correctly.
[1846]1336
[1190]1337 Parameters:
[1846]1338
[1190]1339 feedtype: 'linear' or 'circular'
[1846]1340
[1190]1341 """
1342 self._setfeedtype(feedtype)
1343 self._add_history("set_feedtype", vars())
1344
[1862]1345 @asaplog_post_dec
[2897]1346 def get_doppler(self):
1347 """\
1348 Get the doppler.
1349 """
1350 return self._getcoordinfo()[2]
1351
1352 @asaplog_post_dec
[276]1353 def set_doppler(self, doppler='RADIO'):
[1846]1354 """\
[276]1355 Set the doppler for all following operations on this scantable.
[1846]1356
[276]1357 Parameters:
[1846]1358
[276]1359 doppler: One of 'RADIO', 'OPTICAL', 'Z', 'BETA', 'GAMMA'
[1846]1360
[276]1361 """
[484]1362 varlist = vars()
[276]1363 inf = list(self._getcoordinfo())
1364 inf[2] = doppler
1365 self._setcoordinfo(inf)
[1118]1366 self._add_history("set_doppler", vars())
[710]1367
[1862]1368 @asaplog_post_dec
[226]1369 def set_freqframe(self, frame=None):
[1846]1370 """\
[113]1371 Set the frame type of the Spectral Axis.
[1846]1372
[113]1373 Parameters:
[1846]1374
[591]1375 frame: an optional frame type, default 'LSRK'. Valid frames are:
[1819]1376 'TOPO', 'LSRD', 'LSRK', 'BARY',
[1118]1377 'GEO', 'GALACTO', 'LGROUP', 'CMB'
[1846]1378
1379 Example::
1380
[113]1381 scan.set_freqframe('BARY')
[1846]1382
[113]1383 """
[1593]1384 frame = frame or rcParams['scantable.freqframe']
[484]1385 varlist = vars()
[1819]1386 # "REST" is not implemented in casacore
1387 #valid = ['REST', 'TOPO', 'LSRD', 'LSRK', 'BARY', \
1388 # 'GEO', 'GALACTO', 'LGROUP', 'CMB']
1389 valid = ['TOPO', 'LSRD', 'LSRK', 'BARY', \
[1118]1390 'GEO', 'GALACTO', 'LGROUP', 'CMB']
[591]1391
[989]1392 if frame in valid:
[113]1393 inf = list(self._getcoordinfo())
1394 inf[1] = frame
1395 self._setcoordinfo(inf)
[1118]1396 self._add_history("set_freqframe", varlist)
[102]1397 else:
[1118]1398 msg = "Please specify a valid freq type. Valid types are:\n", valid
[1859]1399 raise TypeError(msg)
[710]1400
[1862]1401 @asaplog_post_dec
[989]1402 def set_dirframe(self, frame=""):
[1846]1403 """\
[989]1404 Set the frame type of the Direction on the sky.
[1846]1405
[989]1406 Parameters:
[1846]1407
[989]1408 frame: an optional frame type, default ''. Valid frames are:
1409 'J2000', 'B1950', 'GALACTIC'
[1846]1410
1411 Example:
1412
[989]1413 scan.set_dirframe('GALACTIC')
[1846]1414
[989]1415 """
1416 varlist = vars()
[1859]1417 Scantable.set_dirframe(self, frame)
[1118]1418 self._add_history("set_dirframe", varlist)
[989]1419
[113]1420 def get_unit(self):
[1846]1421 """\
[113]1422 Get the default unit set in this scantable
[1846]1423
[113]1424 Returns:
[1846]1425
[113]1426 A unit string
[1846]1427
[113]1428 """
1429 inf = self._getcoordinfo()
1430 unit = inf[0]
1431 if unit == '': unit = 'channel'
1432 return unit
[102]1433
[1862]1434 @asaplog_post_dec
[158]1435 def get_abcissa(self, rowno=0):
[1846]1436 """\
[158]1437 Get the abcissa in the current coordinate setup for the currently
[113]1438 selected Beam/IF/Pol
[1846]1439
[113]1440 Parameters:
[1846]1441
[226]1442 rowno: an optional row number in the scantable. Default is the
1443 first row, i.e. rowno=0
[1846]1444
[113]1445 Returns:
[1846]1446
[1348]1447 The abcissa values and the format string (as a dictionary)
[1846]1448
[113]1449 """
[256]1450 abc = self._getabcissa(rowno)
[710]1451 lbl = self._getabcissalabel(rowno)
[158]1452 return abc, lbl
[113]1453
[1862]1454 @asaplog_post_dec
[2322]1455 def flag(self, mask=None, unflag=False, row=-1):
[1846]1456 """\
[1001]1457 Flag the selected data using an optional channel mask.
[1846]1458
[1001]1459 Parameters:
[1846]1460
[1001]1461 mask: an optional channel mask, created with create_mask. Default
1462 (no mask) is all channels.
[1855]1463
[1819]1464 unflag: if True, unflag the data
[1846]1465
[2322]1466 row: an optional row number in the scantable.
1467 Default -1 flags all rows
1468
[1001]1469 """
1470 varlist = vars()
[1593]1471 mask = mask or []
[1994]1472 self._flag(row, mask, unflag)
[1001]1473 self._add_history("flag", varlist)
1474
[1862]1475 @asaplog_post_dec
[2322]1476 def flag_row(self, rows=None, unflag=False):
[1846]1477 """\
[1819]1478 Flag the selected data in row-based manner.
[1846]1479
[1819]1480 Parameters:
[1846]1481
[1843]1482 rows: list of row numbers to be flagged. Default is no row
[2322]1483 (must be explicitly specified to execute row-based
1484 flagging).
[1855]1485
[1819]1486 unflag: if True, unflag the data.
[1846]1487
[1819]1488 """
1489 varlist = vars()
[2322]1490 if rows is None:
1491 rows = []
[1859]1492 self._flag_row(rows, unflag)
[1819]1493 self._add_history("flag_row", varlist)
1494
[1862]1495 @asaplog_post_dec
[1819]1496 def clip(self, uthres=None, dthres=None, clipoutside=True, unflag=False):
[1846]1497 """\
[1819]1498 Flag the selected data outside a specified range (in channel-base)
[1846]1499
[1819]1500 Parameters:
[1846]1501
[1819]1502 uthres: upper threshold.
[1855]1503
[1819]1504 dthres: lower threshold
[1846]1505
[2322]1506 clipoutside: True for flagging data outside the range
1507 [dthres:uthres].
[1928]1508 False for flagging data inside the range.
[1855]1509
[1846]1510 unflag: if True, unflag the data.
1511
[1819]1512 """
1513 varlist = vars()
[1859]1514 self._clip(uthres, dthres, clipoutside, unflag)
[1819]1515 self._add_history("clip", varlist)
1516
[1862]1517 @asaplog_post_dec
[1584]1518 def lag_flag(self, start, end, unit="MHz", insitu=None):
[1846]1519 """\
[1192]1520 Flag the data in 'lag' space by providing a frequency to remove.
[2177]1521 Flagged data in the scantable get interpolated over the region.
[1192]1522 No taper is applied.
[1846]1523
[1192]1524 Parameters:
[1846]1525
[1579]1526 start: the start frequency (really a period within the
1527 bandwidth) or period to remove
[1855]1528
[1579]1529 end: the end frequency or period to remove
[1855]1530
[2431]1531 unit: the frequency unit (default 'MHz') or '' for
[1579]1532 explicit lag channels
[1846]1533
1534 *Notes*:
1535
[1579]1536 It is recommended to flag edges of the band or strong
[1348]1537 signals beforehand.
[1846]1538
[1192]1539 """
1540 if insitu is None: insitu = rcParams['insitu']
1541 self._math._setinsitu(insitu)
1542 varlist = vars()
[1579]1543 base = { "GHz": 1000000000., "MHz": 1000000., "kHz": 1000., "Hz": 1.}
1544 if not (unit == "" or base.has_key(unit)):
[1192]1545 raise ValueError("%s is not a valid unit." % unit)
[1859]1546 if unit == "":
1547 s = scantable(self._math._lag_flag(self, start, end, "lags"))
1548 else:
1549 s = scantable(self._math._lag_flag(self, start*base[unit],
1550 end*base[unit], "frequency"))
[1192]1551 s._add_history("lag_flag", varlist)
1552 if insitu:
1553 self._assign(s)
1554 else:
1555 return s
[1001]1556
[1862]1557 @asaplog_post_dec
[2349]1558 def fft(self, rowno=None, mask=None, getrealimag=False):
[2177]1559 """\
1560 Apply FFT to the spectra.
1561 Flagged data in the scantable get interpolated over the region.
1562
1563 Parameters:
[2186]1564
1565 rowno: The row number(s) to be processed. int, list
[2349]1566 and tuple are accepted. By default (None), FFT
[2186]1567 is applied to the whole data.
1568
1569 mask: Auxiliary channel mask(s). Given as a boolean
1570 list, it is applied to all specified rows.
1571 A list of boolean lists can also be used to
1572 apply different masks. In the latter case, the
1573 length of 'mask' must be the same as that of
[2349]1574 'rowno'. The default is None.
[2177]1575
1576 getrealimag: If True, returns the real and imaginary part
1577 values of the complex results.
1578 If False (the default), returns the amplitude
1579 (absolute value) normalised with Ndata/2 and
1580 phase (argument, in unit of radian).
1581
1582 Returns:
1583
[2186]1584 A list of dictionaries containing the results for each spectrum.
1585 Each dictionary contains two values, the real and the imaginary
1586 parts when getrealimag = True, or the amplitude(absolute value)
1587 and the phase(argument) when getrealimag = False. The key for
1588 these values are 'real' and 'imag', or 'ampl' and 'phase',
[2177]1589 respectively.
1590 """
[2349]1591 if rowno is None:
1592 rowno = []
[2177]1593 if isinstance(rowno, int):
1594 rowno = [rowno]
1595 elif not (isinstance(rowno, list) or isinstance(rowno, tuple)):
[2186]1596 raise TypeError("The row number(s) must be int, list or tuple.")
1597 if len(rowno) == 0: rowno = [i for i in xrange(self.nrow())]
1598
[2411]1599 usecommonmask = True
1600
1601 if mask is None:
1602 mask = []
1603 if isinstance(mask, list) or isinstance(mask, tuple):
1604 if len(mask) == 0:
1605 mask = [[]]
1606 else:
1607 if isinstance(mask[0], bool):
1608 if len(mask) != self.nchan(self.getif(rowno[0])):
1609 raise ValueError("The spectra and the mask have "
1610 "different length.")
1611 mask = [mask]
1612 elif isinstance(mask[0], list) or isinstance(mask[0], tuple):
1613 usecommonmask = False
1614 if len(mask) != len(rowno):
1615 raise ValueError("When specifying masks for each "
1616 "spectrum, the numbers of them "
1617 "must be identical.")
1618 for i in xrange(mask):
1619 if len(mask[i]) != self.nchan(self.getif(rowno[i])):
1620 raise ValueError("The spectra and the mask have "
1621 "different length.")
1622 else:
1623 raise TypeError("The mask must be a boolean list or "
1624 "a list of boolean list.")
1625 else:
[2349]1626 raise TypeError("The mask must be a boolean list or a list of "
1627 "boolean list.")
[2186]1628
1629 res = []
1630
1631 imask = 0
1632 for whichrow in rowno:
1633 fspec = self._fft(whichrow, mask[imask], getrealimag)
1634 nspec = len(fspec)
[2177]1635
[2349]1636 i = 0
1637 v1 = []
1638 v2 = []
1639 reselem = {"real":[],"imag":[]} if getrealimag \
1640 else {"ampl":[],"phase":[]}
[2177]1641
[2186]1642 while (i < nspec):
1643 v1.append(fspec[i])
1644 v2.append(fspec[i+1])
[2349]1645 i += 2
[2186]1646
[2177]1647 if getrealimag:
[2186]1648 reselem["real"] += v1
1649 reselem["imag"] += v2
[2177]1650 else:
[2186]1651 reselem["ampl"] += v1
1652 reselem["phase"] += v2
[2177]1653
[2186]1654 res.append(reselem)
1655
[2349]1656 if not usecommonmask:
1657 imask += 1
[2186]1658
[2177]1659 return res
1660
1661 @asaplog_post_dec
[113]1662 def create_mask(self, *args, **kwargs):
[1846]1663 """\
[1118]1664 Compute and return a mask based on [min, max] windows.
[189]1665 The specified windows are to be INCLUDED, when the mask is
[113]1666 applied.
[1846]1667
[102]1668 Parameters:
[1846]1669
[1118]1670 [min, max], [min2, max2], ...
[1024]1671 Pairs of start/end points (inclusive)specifying the regions
[102]1672 to be masked
[1855]1673
[189]1674 invert: optional argument. If specified as True,
1675 return an inverted mask, i.e. the regions
1676 specified are EXCLUDED
[1855]1677
[513]1678 row: create the mask using the specified row for
1679 unit conversions, default is row=0
1680 only necessary if frequency varies over rows.
[1846]1681
1682 Examples::
1683
[113]1684 scan.set_unit('channel')
[1846]1685 # a)
[1118]1686 msk = scan.create_mask([400, 500], [800, 900])
[189]1687 # masks everything outside 400 and 500
[113]1688 # and 800 and 900 in the unit 'channel'
1689
[1846]1690 # b)
[1118]1691 msk = scan.create_mask([400, 500], [800, 900], invert=True)
[189]1692 # masks the regions between 400 and 500
[113]1693 # and 800 and 900 in the unit 'channel'
[1846]1694
1695 # c)
1696 #mask only channel 400
[1554]1697 msk = scan.create_mask([400])
[1846]1698
[102]1699 """
[1554]1700 row = kwargs.get("row", 0)
[513]1701 data = self._getabcissa(row)
[113]1702 u = self._getcoordinfo()[0]
[1859]1703 if u == "":
1704 u = "channel"
1705 msg = "The current mask window unit is %s" % u
1706 i = self._check_ifs()
1707 if not i:
1708 msg += "\nThis mask is only valid for IF=%d" % (self.getif(i))
1709 asaplog.push(msg)
[2348]1710 n = len(data)
[1295]1711 msk = _n_bools(n, False)
[710]1712 # test if args is a 'list' or a 'normal *args - UGLY!!!
1713
[2349]1714 ws = (isinstance(args[-1][-1], int)
1715 or isinstance(args[-1][-1], float)) and args or args[0]
[710]1716 for window in ws:
[1554]1717 if len(window) == 1:
1718 window = [window[0], window[0]]
1719 if len(window) == 0 or len(window) > 2:
[2349]1720 raise ValueError("A window needs to be defined as "
1721 "[start(, end)]")
[1545]1722 if window[0] > window[1]:
1723 tmp = window[0]
1724 window[0] = window[1]
1725 window[1] = tmp
[102]1726 for i in range(n):
[1024]1727 if data[i] >= window[0] and data[i] <= window[1]:
[1295]1728 msk[i] = True
[113]1729 if kwargs.has_key('invert'):
1730 if kwargs.get('invert'):
[1295]1731 msk = mask_not(msk)
[102]1732 return msk
[710]1733
[1931]1734 def get_masklist(self, mask=None, row=0, silent=False):
[1846]1735 """\
[1819]1736 Compute and return a list of mask windows, [min, max].
[1846]1737
[1819]1738 Parameters:
[1846]1739
[1819]1740 mask: channel mask, created with create_mask.
[1855]1741
[1819]1742 row: calcutate the masklist using the specified row
1743 for unit conversions, default is row=0
1744 only necessary if frequency varies over rows.
[1846]1745
[1819]1746 Returns:
[1846]1747
[1819]1748 [min, max], [min2, max2], ...
1749 Pairs of start/end points (inclusive)specifying
1750 the masked regions
[1846]1751
[1819]1752 """
1753 if not (isinstance(mask,list) or isinstance(mask, tuple)):
1754 raise TypeError("The mask should be list or tuple.")
[2427]1755 if len(mask) <= 0:
1756 raise TypeError("The mask elements should be > 0")
[2348]1757 data = self._getabcissa(row)
1758 if len(data) != len(mask):
[1819]1759 msg = "Number of channels in scantable != number of mask elements"
1760 raise TypeError(msg)
1761 u = self._getcoordinfo()[0]
[1859]1762 if u == "":
1763 u = "channel"
1764 msg = "The current mask window unit is %s" % u
1765 i = self._check_ifs()
1766 if not i:
1767 msg += "\nThis mask is only valid for IF=%d" % (self.getif(i))
[1931]1768 if not silent:
1769 asaplog.push(msg)
[2349]1770 masklist = []
[1819]1771 ist, ien = None, None
1772 ist, ien=self.get_mask_indices(mask)
1773 if ist is not None and ien is not None:
1774 for i in xrange(len(ist)):
1775 range=[data[ist[i]],data[ien[i]]]
1776 range.sort()
1777 masklist.append([range[0],range[1]])
1778 return masklist
1779
1780 def get_mask_indices(self, mask=None):
[1846]1781 """\
[1819]1782 Compute and Return lists of mask start indices and mask end indices.
[1855]1783
1784 Parameters:
1785
[1819]1786 mask: channel mask, created with create_mask.
[1846]1787
[1819]1788 Returns:
[1846]1789
[1819]1790 List of mask start indices and that of mask end indices,
1791 i.e., [istart1,istart2,....], [iend1,iend2,....].
[1846]1792
[1819]1793 """
1794 if not (isinstance(mask,list) or isinstance(mask, tuple)):
1795 raise TypeError("The mask should be list or tuple.")
[2427]1796 if len(mask) <= 0:
1797 raise TypeError("The mask elements should be > 0")
[2349]1798 istart = []
1799 iend = []
1800 if mask[0]:
1801 istart.append(0)
[1819]1802 for i in range(len(mask)-1):
1803 if not mask[i] and mask[i+1]:
1804 istart.append(i+1)
1805 elif mask[i] and not mask[i+1]:
1806 iend.append(i)
[2349]1807 if mask[len(mask)-1]:
1808 iend.append(len(mask)-1)
[1819]1809 if len(istart) != len(iend):
1810 raise RuntimeError("Numbers of mask start != mask end.")
1811 for i in range(len(istart)):
1812 if istart[i] > iend[i]:
1813 raise RuntimeError("Mask start index > mask end index")
1814 break
1815 return istart,iend
1816
[2013]1817 @asaplog_post_dec
[2882]1818 def parse_spw_selection(self, selectstring, restfreq=None, frame=None, doppler=None):
1819 """
1820 Parse MS type spw/channel selection syntax.
1821
1822 Parameters:
1823 selectstring : A string expression of spw and channel selection.
1824 Comma-separated expressions mean different spw -
1825 channel combinations. Spws and channel selections
1826 are partitioned by a colon ':'. In a single
1827 selection expression, you can put multiple values
1828 separated by semicolons ';'. Both for spw and
1829 channel selection, allowed cases include single
1830 value, blank('') or asterisk('*') to specify all
1831 available values, two values connected with a
1832 tilde ('~') to specify an inclusive range. Unit
1833 strings for frequency or velocity can be added to
1834 the tilde-connected values. For channel selection
1835 expression, placing a '<' or a '>' is possible to
1836 specify a semi-infinite interval as well.
1837
1838 examples:
1839 '' or '*' = all spws (all channels)
1840 '<2,4~6,9' = Spws 0,1,4,5,6,9 (all channels)
1841 '3:3~45;60' = channels 3 to 45 and 60 in spw 3
1842 '0~1:2~6,8' = channels 2 to 6 in spws 0,1, and
1843 all channels in spw8
[2936]1844 '1.3~1.5GHz' = all spws whose central frequency
1845 falls in frequency range between
1846 1.3GHz and 1.5GHz.
1847 '1.3~1.5GHz:1.3~1.5GHz' = channels which fall
[2884]1848 between the specified
1849 frequency range in spws
[2936]1850 whose central frequency
1851 falls in the specified
1852 frequency range.
[2884]1853 '1:-200~250km/s' = channels that fall between the
1854 specified velocity range in
1855 spw 1.
[2897]1856 restfreq: the rest frequency.
1857 examples: '115.2712GHz', 115271201800.0
1858 frame: an optional frame type, default 'LSRK'. Valid frames are:
1859 'TOPO', 'LSRD', 'LSRK', 'BARY',
1860 'GEO', 'GALACTO', 'LGROUP', 'CMB'
1861 doppler: one of 'RADIO', 'OPTICAL', 'Z', 'BETA', 'GAMMA'
[2882]1862 Returns:
1863 A dictionary of selected (valid) spw and masklist pairs,
1864 e.g. {'0': [[50,250],[350,462]], '2': [[100,400],[550,974]]}
1865 """
1866 if not isinstance(selectstring, str):
1867 asaplog.post()
1868 asaplog.push("Expression of spw/channel selection must be a string.")
1869 asaplog.post("ERROR")
1870
1871 orig_unit = self.get_unit()
1872 self.set_unit('channel')
1873
[2891]1874 if restfreq is not None:
[2892]1875 orig_molids = self._getmolidcol_list()
1876 set_restfreq(self, restfreq)
[2882]1877
[2897]1878 orig_coord = self._getcoordinfo()
[2892]1879
1880 if frame is not None:
1881 orig_frame = orig_coord[1]
1882 self.set_freqframe(frame)
1883
1884 if doppler is not None:
1885 orig_doppler = orig_coord[2]
1886 self.set_doppler(doppler)
[2882]1887
1888 valid_ifs = self.getifnos()
1889
1890 comma_sep = selectstring.split(",")
1891 res = {}
1892
1893 for cms_elem in comma_sep:
1894 colon_sep = cms_elem.split(":")
1895
1896 if (len(colon_sep) > 2):
1897 raise RuntimeError("Invalid selection expression: more than two colons!")
1898
1899 # parse spw expression and store result in spw_list.
1900 # allowed cases include '', '*', 'a', '<a', '>a', 'a~b',
[2884]1901 # 'a~b*Hz' (where * can be '', 'k', 'M', 'G' etc.),
1902 # 'a~b*m/s' (where * can be '' or 'k') and also
[2882]1903 # several of the above expressions connected with ';'.
1904
1905 spw_list = []
1906
1907 semicolon_sep = colon_sep[0].split(";")
1908
1909 for scs_elem in semicolon_sep:
1910 scs_elem = scs_elem.strip()
1911
1912 lt_sep = scs_elem.split("<")
1913 gt_sep = scs_elem.split(">")
1914 ti_sep = scs_elem.split("~")
1915
1916 lt_sep_length = len(lt_sep)
1917 gt_sep_length = len(gt_sep)
1918 ti_sep_length = len(ti_sep)
1919
1920 len_product = lt_sep_length * gt_sep_length * ti_sep_length
1921
1922 if (len_product > 2):
1923 # '<', '>' and '~' must not coexist in a single spw expression
1924
1925 raise RuntimeError("Invalid spw selection.")
1926
1927 elif (len_product == 1):
1928 # '', '*', or single spw number.
1929
1930 if (scs_elem == "") or (scs_elem == "*"):
1931 spw_list = valid_ifs[:] # deep copy
1932
1933 else: # single number
[2887]1934 expr = int(scs_elem)
1935 spw_list.append(expr)
1936 if expr not in valid_ifs:
1937 asaplog.push("Invalid spw given. Ignored.")
1938
[2882]1939 else: # (len_product == 2)
[2887]1940 # namely, one of '<', '>' or '~' appears just once.
[2882]1941
1942 if (lt_sep_length == 2): # '<a'
1943 if is_number(lt_sep[1]):
[2886]1944 no_valid_spw = True
[2882]1945 for i in valid_ifs:
1946 if (i < float(lt_sep[1])):
1947 spw_list.append(i)
[2886]1948 no_valid_spw = False
1949
1950 if no_valid_spw:
1951 raise ValueError("Invalid spw selection ('<" + str(lt_sep[1]) + "').")
[2882]1952
1953 else:
[2886]1954 raise RuntimeError("Invalid spw selection.")
[2882]1955
1956 elif (gt_sep_length == 2): # '>a'
1957 if is_number(gt_sep[1]):
[2886]1958 no_valid_spw = True
[2882]1959 for i in valid_ifs:
1960 if (i > float(gt_sep[1])):
1961 spw_list.append(i)
[2886]1962 no_valid_spw = False
1963
1964 if no_valid_spw:
1965 raise ValueError("Invalid spw selection ('>" + str(gt_sep[1]) + "').")
[2882]1966
1967 else:
[2886]1968 raise RuntimeError("Invalid spw selection.")
[2882]1969
1970 else: # (ti_sep_length == 2) where both boundaries inclusive
1971 expr0 = ti_sep[0].strip()
1972 expr1 = ti_sep[1].strip()
1973
1974 if is_number(expr0) and is_number(expr1):
1975 # 'a~b'
1976 expr_pmin = min(float(expr0), float(expr1))
1977 expr_pmax = max(float(expr0), float(expr1))
[2887]1978 has_invalid_spw = False
[2886]1979 no_valid_spw = True
1980
[2882]1981 for i in valid_ifs:
1982 if (expr_pmin <= i) and (i <= expr_pmax):
1983 spw_list.append(i)
[2886]1984 no_valid_spw = False
[2887]1985 else:
1986 has_invalid_spw = True
[2886]1987
[2887]1988 if has_invalid_spw:
1989 msg = "Invalid spw is given. Ignored."
1990 asaplog.push(msg)
1991 asaplog.post()
1992
[2886]1993 if no_valid_spw:
1994 raise ValueError("No valid spw in range ('" + str(expr_pmin) + "~" + str(expr_pmax) + "').")
[2887]1995
[2884]1996 elif is_number(expr0) and is_frequency(expr1):
1997 # 'a~b*Hz'
1998 (expr_f0, expr_f1) = get_freq_by_string(expr0, expr1)
[2936]1999 expr_fmin = min(expr_f0, expr_f1)
2000 expr_fmax = max(expr_f0, expr_f1)
[2886]2001 no_valid_spw = True
2002
[2882]2003 for coord in self._get_coordinate_list():
[2936]2004 spw = coord['if']
2005
2006 """
[2882]2007 expr_p0 = coord['coord'].to_pixel(expr_f0)
2008 expr_p1 = coord['coord'].to_pixel(expr_f1)
2009 expr_pmin = min(expr_p0, expr_p1)
2010 expr_pmax = max(expr_p0, expr_p1)
2011
2012 pmin = 0.0
2013 pmax = float(self.nchan(spw) - 1)
[2936]2014
[2882]2015 if ((expr_pmax - pmin)*(expr_pmin - pmax) <= 0.0):
2016 spw_list.append(spw)
[2886]2017 no_valid_spw = False
[2936]2018 """
[2886]2019
[2937]2020 crd = coord['coord']
2021 fhead = crd.to_frequency(0)
2022 ftail = crd.to_frequency(self.nchan(spw) - 1)
[2936]2023 fcen = (fhead + ftail) / 2.0
2024
2025 if ((expr_fmin <= fcen) and (fcen <= expr_fmax)):
2026 spw_list.append(spw)
2027 no_valid_spw = False
2028
[2886]2029 if no_valid_spw:
2030 raise ValueError("No valid spw in range ('" + str(expr0) + "~" + str(expr1) + "').")
[2882]2031
[2884]2032 elif is_number(expr0) and is_velocity(expr1):
2033 # 'a~b*m/s'
2034 (expr_v0, expr_v1) = get_velocity_by_string(expr0, expr1)
[2882]2035 expr_vmin = min(expr_v0, expr_v1)
2036 expr_vmax = max(expr_v0, expr_v1)
[2886]2037 no_valid_spw = True
2038
[2882]2039 for coord in self._get_coordinate_list():
2040 spw = coord['if']
[2936]2041
2042 """
[2882]2043 pmin = 0.0
2044 pmax = float(self.nchan(spw) - 1)
2045
2046 vel0 = coord['coord'].to_velocity(pmin)
2047 vel1 = coord['coord'].to_velocity(pmax)
2048
2049 vmin = min(vel0, vel1)
2050 vmax = max(vel0, vel1)
2051
2052 if ((expr_vmax - vmin)*(expr_vmin - vmax) <= 0.0):
2053 spw_list.append(spw)
[2886]2054 no_valid_spw = False
[2936]2055 """
[2886]2056
[2937]2057 crd = coord['coord']
2058 fhead = crd.to_frequency(0)
2059 ftail = crd.to_frequency(self.nchan(spw) - 1)
2060 fcen = (fhead + ftail) / 2.0
2061 vcen = crd.to_velocity(crd.to_pixel(fcen))
[2936]2062
2063 if ((expr_vmin <= vcen) and (vcen <= expr_vmax)):
2064 spw_list.append(spw)
2065 no_valid_spw = False
2066
[2886]2067 if no_valid_spw:
2068 raise ValueError("No valid spw in range ('" + str(expr0) + "~" + str(expr1) + "').")
[2882]2069
2070 else:
2071 # cases such as 'aGHz~bkm/s' are not allowed now
2072 raise RuntimeError("Invalid spw selection.")
2073
[2887]2074 # check spw list and remove invalid ones.
2075 # if no valid spw left, emit ValueError.
2076 if len(spw_list) == 0:
2077 raise ValueError("No valid spw in given range.")
2078
[2882]2079 # parse channel expression and store the result in crange_list.
2080 # allowed cases include '', 'a~b', 'a*Hz~b*Hz' (where * can be
2081 # '', 'k', 'M', 'G' etc.), 'a*m/s~b*m/s' (where * can be '' or 'k')
2082 # and also several of the above expressions connected with ';'.
2083
2084 for spw in spw_list:
2085 pmin = 0.0
2086 pmax = float(self.nchan(spw) - 1)
[2909]2087
2088 molid = self._getmolidcol_list()[self.get_first_rowno_by_if(spw)]
[2882]2089
2090 if (len(colon_sep) == 1):
2091 # no expression for channel selection,
2092 # which means all channels are to be selected.
2093 crange_list = [[pmin, pmax]]
2094
2095 else: # (len(colon_sep) == 2)
2096 crange_list = []
2097
2098 found = False
2099 for i in self._get_coordinate_list():
2100 if (i['if'] == spw):
2101 coord = i['coord']
2102 found = True
2103 break
2104
[2887]2105 if found:
2106 semicolon_sep = colon_sep[1].split(";")
2107 for scs_elem in semicolon_sep:
2108 scs_elem = scs_elem.strip()
[2882]2109
[2887]2110 ti_sep = scs_elem.split("~")
2111 ti_sep_length = len(ti_sep)
[2882]2112
[2887]2113 if (ti_sep_length > 2):
2114 raise RuntimeError("Invalid channel selection.")
[2882]2115
[2887]2116 elif (ti_sep_length == 1):
2117 if (scs_elem == "") or (scs_elem == "*"):
2118 # '' and '*' for all channels
2119 crange_list = [[pmin, pmax]]
2120 break
2121 elif (is_number(scs_elem)):
2122 # single channel given
2123 crange_list.append([float(scs_elem), float(scs_elem)])
2124 else:
2125 raise RuntimeError("Invalid channel selection.")
[2882]2126
[2887]2127 else: #(ti_sep_length == 2)
2128 expr0 = ti_sep[0].strip()
2129 expr1 = ti_sep[1].strip()
[2882]2130
[2887]2131 if is_number(expr0) and is_number(expr1):
2132 # 'a~b'
2133 expr_pmin = min(float(expr0), float(expr1))
2134 expr_pmax = max(float(expr0), float(expr1))
[2882]2135
[2887]2136 elif is_number(expr0) and is_frequency(expr1):
2137 # 'a~b*Hz'
2138 (expr_f0, expr_f1) = get_freq_by_string(expr0, expr1)
2139 expr_p0 = coord.to_pixel(expr_f0)
2140 expr_p1 = coord.to_pixel(expr_f1)
2141 expr_pmin = min(expr_p0, expr_p1)
2142 expr_pmax = max(expr_p0, expr_p1)
[2882]2143
[2887]2144 elif is_number(expr0) and is_velocity(expr1):
2145 # 'a~b*m/s'
[2909]2146 restf = self.get_restfreqs()[molid][0]
[2887]2147 (expr_v0, expr_v1) = get_velocity_by_string(expr0, expr1)
[2897]2148 dppl = self.get_doppler()
2149 expr_f0 = get_frequency_by_velocity(restf, expr_v0, dppl)
2150 expr_f1 = get_frequency_by_velocity(restf, expr_v1, dppl)
[2887]2151 expr_p0 = coord.to_pixel(expr_f0)
2152 expr_p1 = coord.to_pixel(expr_f1)
2153 expr_pmin = min(expr_p0, expr_p1)
2154 expr_pmax = max(expr_p0, expr_p1)
[2882]2155
[2887]2156 else:
2157 # cases such as 'aGHz~bkm/s' are not allowed now
2158 raise RuntimeError("Invalid channel selection.")
[2882]2159
[2887]2160 cmin = max(pmin, expr_pmin)
2161 cmax = min(pmax, expr_pmax)
2162 # if the given range of channel selection has overwrap with
2163 # that of current spw, output the overwrap area.
2164 if (cmin <= cmax):
2165 cmin = float(int(cmin + 0.5))
2166 cmax = float(int(cmax + 0.5))
2167 crange_list.append([cmin, cmax])
[2882]2168
2169 if (len(crange_list) == 0):
2170 crange_list.append([])
2171
[2910]2172 if (len(crange_list[0]) > 0):
2173 if res.has_key(spw):
2174 res[spw].extend(crange_list)
2175 else:
2176 res[spw] = crange_list
[2882]2177
[2887]2178 for spw in res.keys():
[2932]2179 if spw in valid_ifs:
[2933]2180 # remove duplicated channel ranges
[2932]2181 for i in reversed(xrange(len(res[spw]))):
2182 for j in xrange(i):
[2933]2183 if ((res[spw][i][0]-res[spw][j][1])*(res[spw][i][1]-res[spw][j][0]) <= 0) or \
2184 (min(abs(res[spw][i][0]-res[spw][j][1]),abs(res[spw][j][0]-res[spw][i][1])) == 1):
[2935]2185 asaplog.post()
2186 merge_warn_mesg = "Spw " + str(spw) + ": overwrapping channel ranges are merged."
2187 asaplog.push(merge_warn_mesg)
2188 asaplog.post('WARN')
[2932]2189 res[spw][j][0] = min(res[spw][i][0], res[spw][j][0])
2190 res[spw][j][1] = max(res[spw][i][1], res[spw][j][1])
2191 res[spw].pop(i)
2192 break
2193 else:
[2887]2194 del res[spw]
2195
2196 if len(res) == 0:
2197 raise RuntimeError("No valid spw.")
2198
[2882]2199 # restore original values
[2892]2200 self.set_unit(orig_unit)
[2891]2201 if restfreq is not None:
[2892]2202 self._setmolidcol_list(orig_molids)
2203 if frame is not None:
2204 self.set_freqframe(orig_frame)
2205 if doppler is not None:
2206 self.set_doppler(orig_doppler)
[2882]2207
2208 return res
[2890]2209
[2882]2210 @asaplog_post_dec
2211 def get_first_rowno_by_if(self, ifno):
2212 found = False
2213 for irow in xrange(self.nrow()):
2214 if (self.getif(irow) == ifno):
2215 res = irow
2216 found = True
2217 break
2218
[2926]2219 if not found: raise RuntimeError("No valid spw.")
[2882]2220
2221 return res
2222
2223 @asaplog_post_dec
2224 def _get_coordinate_list(self):
2225 res = []
2226 spws = self.getifnos()
2227 for spw in spws:
2228 elem = {}
2229 elem['if'] = spw
2230 elem['coord'] = self.get_coordinate(self.get_first_rowno_by_if(spw))
2231 res.append(elem)
2232
2233 return res
2234
2235 @asaplog_post_dec
[2349]2236 def parse_maskexpr(self, maskstring):
[2013]2237 """
2238 Parse CASA type mask selection syntax (IF dependent).
2239
2240 Parameters:
2241 maskstring : A string mask selection expression.
2242 A comma separated selections mean different IF -
2243 channel combinations. IFs and channel selections
2244 are partitioned by a colon, ':'.
2245 examples:
[2015]2246 '' = all IFs (all channels)
[2013]2247 '<2,4~6,9' = IFs 0,1,4,5,6,9 (all channels)
2248 '3:3~45;60' = channels 3 to 45 and 60 in IF 3
2249 '0~1:2~6,8' = channels 2 to 6 in IFs 0,1, and
2250 all channels in IF8
2251 Returns:
2252 A dictionary of selected (valid) IF and masklist pairs,
2253 e.g. {'0': [[50,250],[350,462]], '2': [[100,400],[550,974]]}
2254 """
2255 if not isinstance(maskstring,str):
2256 asaplog.post()
[2611]2257 asaplog.push("Mask expression should be a string.")
[2013]2258 asaplog.post("ERROR")
2259
2260 valid_ifs = self.getifnos()
2261 frequnit = self.get_unit()
2262 seldict = {}
[2015]2263 if maskstring == "":
2264 maskstring = str(valid_ifs)[1:-1]
[2611]2265 ## split each selection "IF range[:CHAN range]"
[2867]2266 # split maskstring by "<spaces>,<spaces>"
2267 comma_sep = re.compile('\s*,\s*')
2268 sellist = comma_sep.split(maskstring)
2269 # separator by "<spaces>:<spaces>"
2270 collon_sep = re.compile('\s*:\s*')
[2013]2271 for currselstr in sellist:
[2867]2272 selset = collon_sep.split(currselstr)
[2013]2273 # spw and mask string (may include ~, < or >)
[2349]2274 spwmasklist = self._parse_selection(selset[0], typestr='integer',
[2611]2275 minval=min(valid_ifs),
[2349]2276 maxval=max(valid_ifs))
[2013]2277 for spwlist in spwmasklist:
2278 selspws = []
2279 for ispw in range(spwlist[0],spwlist[1]+1):
2280 # Put into the list only if ispw exists
2281 if valid_ifs.count(ispw):
2282 selspws.append(ispw)
2283 del spwmasklist, spwlist
2284
2285 # parse frequency mask list
2286 if len(selset) > 1:
[2349]2287 freqmasklist = self._parse_selection(selset[1], typestr='float',
2288 offset=0.)
[2013]2289 else:
2290 # want to select the whole spectrum
2291 freqmasklist = [None]
2292
2293 ## define a dictionary of spw - masklist combination
2294 for ispw in selspws:
2295 #print "working on", ispw
2296 spwstr = str(ispw)
2297 if len(selspws) == 0:
2298 # empty spw
2299 continue
2300 else:
2301 ## want to get min and max of the spw and
2302 ## offset to set for '<' and '>'
2303 if frequnit == 'channel':
2304 minfreq = 0
2305 maxfreq = self.nchan(ifno=ispw)
2306 offset = 0.5
2307 else:
2308 ## This is ugly part. need improvement
2309 for ifrow in xrange(self.nrow()):
2310 if self.getif(ifrow) == ispw:
2311 #print "IF",ispw,"found in row =",ifrow
2312 break
2313 freqcoord = self.get_coordinate(ifrow)
2314 freqs = self._getabcissa(ifrow)
2315 minfreq = min(freqs)
2316 maxfreq = max(freqs)
2317 if len(freqs) == 1:
2318 offset = 0.5
2319 elif frequnit.find('Hz') > 0:
[2349]2320 offset = abs(freqcoord.to_frequency(1,
2321 unit=frequnit)
2322 -freqcoord.to_frequency(0,
2323 unit=frequnit)
2324 )*0.5
[2013]2325 elif frequnit.find('m/s') > 0:
[2349]2326 offset = abs(freqcoord.to_velocity(1,
2327 unit=frequnit)
2328 -freqcoord.to_velocity(0,
2329 unit=frequnit)
2330 )*0.5
[2013]2331 else:
2332 asaplog.post()
2333 asaplog.push("Invalid frequency unit")
2334 asaplog.post("ERROR")
2335 del freqs, freqcoord, ifrow
2336 for freq in freqmasklist:
2337 selmask = freq or [minfreq, maxfreq]
2338 if selmask[0] == None:
2339 ## selection was "<freq[1]".
2340 if selmask[1] < minfreq:
2341 ## avoid adding region selection
2342 selmask = None
2343 else:
2344 selmask = [minfreq,selmask[1]-offset]
2345 elif selmask[1] == None:
2346 ## selection was ">freq[0]"
2347 if selmask[0] > maxfreq:
2348 ## avoid adding region selection
2349 selmask = None
2350 else:
2351 selmask = [selmask[0]+offset,maxfreq]
2352 if selmask:
2353 if not seldict.has_key(spwstr):
2354 # new spw selection
2355 seldict[spwstr] = []
2356 seldict[spwstr] += [selmask]
2357 del minfreq,maxfreq,offset,freq,selmask
2358 del spwstr
2359 del freqmasklist
2360 del valid_ifs
2361 if len(seldict) == 0:
2362 asaplog.post()
[2349]2363 asaplog.push("No valid selection in the mask expression: "
2364 +maskstring)
[2013]2365 asaplog.post("WARN")
2366 return None
2367 msg = "Selected masklist:\n"
2368 for sif, lmask in seldict.iteritems():
2369 msg += " IF"+sif+" - "+str(lmask)+"\n"
2370 asaplog.push(msg)
2371 return seldict
2372
[2611]2373 @asaplog_post_dec
2374 def parse_idx_selection(self, mode, selexpr):
2375 """
2376 Parse CASA type mask selection syntax of SCANNO, IFNO, POLNO,
2377 BEAMNO, and row number
2378
2379 Parameters:
2380 mode : which column to select.
2381 ['scan',|'if'|'pol'|'beam'|'row']
2382 selexpr : A comma separated selection expression.
2383 examples:
2384 '' = all (returns [])
2385 '<2,4~6,9' = indices less than 2, 4 to 6 and 9
2386 (returns [0,1,4,5,6,9])
2387 Returns:
2388 A List of selected indices
2389 """
2390 if selexpr == "":
2391 return []
2392 valid_modes = {'s': 'scan', 'i': 'if', 'p': 'pol',
2393 'b': 'beam', 'r': 'row'}
2394 smode = mode.lower()[0]
2395 if not (smode in valid_modes.keys()):
2396 msg = "Invalid mode '%s'. Valid modes are %s" %\
2397 (mode, str(valid_modes.values()))
2398 asaplog.post()
2399 asaplog.push(msg)
2400 asaplog.post("ERROR")
2401 mode = valid_modes[smode]
2402 minidx = None
2403 maxidx = None
2404 if smode == 'r':
2405 minidx = 0
2406 maxidx = self.nrow()
2407 else:
2408 idx = getattr(self,"get"+mode+"nos")()
2409 minidx = min(idx)
2410 maxidx = max(idx)
2411 del idx
[2867]2412 # split selexpr by "<spaces>,<spaces>"
2413 comma_sep = re.compile('\s*,\s*')
2414 sellist = comma_sep.split(selexpr)
[2611]2415 idxlist = []
2416 for currselstr in sellist:
2417 # single range (may include ~, < or >)
2418 currlist = self._parse_selection(currselstr, typestr='integer',
2419 minval=minidx,maxval=maxidx)
2420 for thelist in currlist:
2421 idxlist += range(thelist[0],thelist[1]+1)
[2932]2422 # remove duplicated elements after first ones
2423 for i in reversed(xrange(len(idxlist))):
2424 if idxlist.index(idxlist[i]) < i:
2425 idxlist.pop(i)
[2611]2426 msg = "Selected %s: %s" % (mode.upper()+"NO", str(idxlist))
2427 asaplog.push(msg)
2428 return idxlist
2429
[2349]2430 def _parse_selection(self, selstr, typestr='float', offset=0.,
[2351]2431 minval=None, maxval=None):
[2013]2432 """
2433 Parameters:
2434 selstr : The Selection string, e.g., '<3;5~7;100~103;9'
2435 typestr : The type of the values in returned list
2436 ('integer' or 'float')
2437 offset : The offset value to subtract from or add to
2438 the boundary value if the selection string
[2611]2439 includes '<' or '>' [Valid only for typestr='float']
[2013]2440 minval, maxval : The minimum/maximum values to set if the
2441 selection string includes '<' or '>'.
2442 The list element is filled with None by default.
2443 Returns:
2444 A list of min/max pair of selections.
2445 Example:
[2611]2446 _parse_selection('<3;5~7;9',typestr='int',minval=0)
2447 --> returns [[0,2],[5,7],[9,9]]
2448 _parse_selection('<3;5~7;9',typestr='float',offset=0.5,minval=0)
2449 --> returns [[0.,2.5],[5.0,7.0],[9.,9.]]
[2013]2450 """
[2867]2451 # split selstr by '<spaces>;<spaces>'
2452 semi_sep = re.compile('\s*;\s*')
2453 selgroups = semi_sep.split(selstr)
[2013]2454 sellists = []
2455 if typestr.lower().startswith('int'):
2456 formatfunc = int
[2611]2457 offset = 1
[2013]2458 else:
2459 formatfunc = float
2460
2461 for currsel in selgroups:
[2867]2462 if currsel.strip() == '*' or len(currsel.strip()) == 0:
2463 minsel = minval
2464 maxsel = maxval
[2013]2465 if currsel.find('~') > 0:
[2611]2466 # val0 <= x <= val1
[2013]2467 minsel = formatfunc(currsel.split('~')[0].strip())
[2867]2468 maxsel = formatfunc(currsel.split('~')[1].strip())
[2611]2469 elif currsel.strip().find('<=') > -1:
2470 bound = currsel.split('<=')
2471 try: # try "x <= val"
2472 minsel = minval
2473 maxsel = formatfunc(bound[1].strip())
2474 except ValueError: # now "val <= x"
2475 minsel = formatfunc(bound[0].strip())
2476 maxsel = maxval
2477 elif currsel.strip().find('>=') > -1:
2478 bound = currsel.split('>=')
2479 try: # try "x >= val"
2480 minsel = formatfunc(bound[1].strip())
2481 maxsel = maxval
2482 except ValueError: # now "val >= x"
2483 minsel = minval
2484 maxsel = formatfunc(bound[0].strip())
2485 elif currsel.strip().find('<') > -1:
2486 bound = currsel.split('<')
2487 try: # try "x < val"
2488 minsel = minval
2489 maxsel = formatfunc(bound[1].strip()) \
2490 - formatfunc(offset)
2491 except ValueError: # now "val < x"
2492 minsel = formatfunc(bound[0].strip()) \
[2013]2493 + formatfunc(offset)
[2611]2494 maxsel = maxval
2495 elif currsel.strip().find('>') > -1:
2496 bound = currsel.split('>')
2497 try: # try "x > val"
2498 minsel = formatfunc(bound[1].strip()) \
2499 + formatfunc(offset)
2500 maxsel = maxval
2501 except ValueError: # now "val > x"
2502 minsel = minval
2503 maxsel = formatfunc(bound[0].strip()) \
2504 - formatfunc(offset)
[2013]2505 else:
2506 minsel = formatfunc(currsel)
2507 maxsel = formatfunc(currsel)
2508 sellists.append([minsel,maxsel])
2509 return sellists
2510
[1819]2511# def get_restfreqs(self):
2512# """
2513# Get the restfrequency(s) stored in this scantable.
2514# The return value(s) are always of unit 'Hz'
2515# Parameters:
2516# none
2517# Returns:
2518# a list of doubles
2519# """
2520# return list(self._getrestfreqs())
2521
2522 def get_restfreqs(self, ids=None):
[1846]2523 """\
[256]2524 Get the restfrequency(s) stored in this scantable.
2525 The return value(s) are always of unit 'Hz'
[1846]2526
[256]2527 Parameters:
[1846]2528
[1819]2529 ids: (optional) a list of MOLECULE_ID for that restfrequency(s) to
2530 be retrieved
[1846]2531
[256]2532 Returns:
[1846]2533
[1819]2534 dictionary containing ids and a list of doubles for each id
[1846]2535
[256]2536 """
[1819]2537 if ids is None:
[2349]2538 rfreqs = {}
[1819]2539 idlist = self.getmolnos()
2540 for i in idlist:
[2349]2541 rfreqs[i] = list(self._getrestfreqs(i))
[1819]2542 return rfreqs
2543 else:
[2349]2544 if type(ids) == list or type(ids) == tuple:
2545 rfreqs = {}
[1819]2546 for i in ids:
[2349]2547 rfreqs[i] = list(self._getrestfreqs(i))
[1819]2548 return rfreqs
2549 else:
2550 return list(self._getrestfreqs(ids))
[102]2551
[2349]2552 @asaplog_post_dec
[931]2553 def set_restfreqs(self, freqs=None, unit='Hz'):
[1846]2554 """\
[931]2555 Set or replace the restfrequency specified and
[1938]2556 if the 'freqs' argument holds a scalar,
[931]2557 then that rest frequency will be applied to all the selected
2558 data. If the 'freqs' argument holds
2559 a vector, then it MUST be of equal or smaller length than
2560 the number of IFs (and the available restfrequencies will be
2561 replaced by this vector). In this case, *all* data have
2562 the restfrequency set per IF according
2563 to the corresponding value you give in the 'freqs' vector.
[1118]2564 E.g. 'freqs=[1e9, 2e9]' would mean IF 0 gets restfreq 1e9 and
[931]2565 IF 1 gets restfreq 2e9.
[1846]2566
[1395]2567 You can also specify the frequencies via a linecatalog.
[1153]2568
[931]2569 Parameters:
[1846]2570
[931]2571 freqs: list of rest frequency values or string idenitfiers
[1855]2572
[931]2573 unit: unit for rest frequency (default 'Hz')
[402]2574
[1846]2575
2576 Example::
2577
[1819]2578 # set the given restfrequency for the all currently selected IFs
[931]2579 scan.set_restfreqs(freqs=1.4e9)
[1845]2580 # set restfrequencies for the n IFs (n > 1) in the order of the
2581 # list, i.e
2582 # IF0 -> 1.4e9, IF1 -> 1.41e9, IF3 -> 1.42e9
2583 # len(list_of_restfreqs) == nIF
2584 # for nIF == 1 the following will set multiple restfrequency for
2585 # that IF
[1819]2586 scan.set_restfreqs(freqs=[1.4e9, 1.41e9, 1.42e9])
[1845]2587 # set multiple restfrequencies per IF. as a list of lists where
2588 # the outer list has nIF elements, the inner s arbitrary
2589 scan.set_restfreqs(freqs=[[1.4e9, 1.41e9], [1.67e9]])
[391]2590
[1846]2591 *Note*:
[1845]2592
[931]2593 To do more sophisticate Restfrequency setting, e.g. on a
2594 source and IF basis, use scantable.set_selection() before using
[1846]2595 this function::
[931]2596
[1846]2597 # provided your scantable is called scan
2598 selection = selector()
[2431]2599 selection.set_name('ORION*')
[1846]2600 selection.set_ifs([1])
2601 scan.set_selection(selection)
2602 scan.set_restfreqs(freqs=86.6e9)
2603
[931]2604 """
2605 varlist = vars()
[1157]2606 from asap import linecatalog
2607 # simple value
[1118]2608 if isinstance(freqs, int) or isinstance(freqs, float):
[1845]2609 self._setrestfreqs([freqs], [""], unit)
[1157]2610 # list of values
[1118]2611 elif isinstance(freqs, list) or isinstance(freqs, tuple):
[1157]2612 # list values are scalars
[1118]2613 if isinstance(freqs[-1], int) or isinstance(freqs[-1], float):
[1845]2614 if len(freqs) == 1:
2615 self._setrestfreqs(freqs, [""], unit)
2616 else:
2617 # allow the 'old' mode of setting mulitple IFs
2618 savesel = self._getselection()
[2599]2619 sel = self.get_selection()
[1845]2620 iflist = self.getifnos()
2621 if len(freqs)>len(iflist):
2622 raise ValueError("number of elements in list of list "
2623 "exeeds the current IF selections")
2624 iflist = self.getifnos()
2625 for i, fval in enumerate(freqs):
2626 sel.set_ifs(iflist[i])
2627 self._setselection(sel)
2628 self._setrestfreqs([fval], [""], unit)
2629 self._setselection(savesel)
2630
2631 # list values are dict, {'value'=, 'name'=)
[1157]2632 elif isinstance(freqs[-1], dict):
[1845]2633 values = []
2634 names = []
2635 for d in freqs:
2636 values.append(d["value"])
2637 names.append(d["name"])
2638 self._setrestfreqs(values, names, unit)
[1819]2639 elif isinstance(freqs[-1], list) or isinstance(freqs[-1], tuple):
[1157]2640 savesel = self._getselection()
[2599]2641 sel = self.get_selection()
[1322]2642 iflist = self.getifnos()
[1819]2643 if len(freqs)>len(iflist):
[1845]2644 raise ValueError("number of elements in list of list exeeds"
2645 " the current IF selections")
2646 for i, fval in enumerate(freqs):
[1322]2647 sel.set_ifs(iflist[i])
[1259]2648 self._setselection(sel)
[1845]2649 self._setrestfreqs(fval, [""], unit)
[1157]2650 self._setselection(savesel)
2651 # freqs are to be taken from a linecatalog
[1153]2652 elif isinstance(freqs, linecatalog):
2653 savesel = self._getselection()
[2599]2654 sel = self.get_selection()
[1153]2655 for i in xrange(freqs.nrow()):
[1322]2656 sel.set_ifs(iflist[i])
[1153]2657 self._setselection(sel)
[1845]2658 self._setrestfreqs([freqs.get_frequency(i)],
2659 [freqs.get_name(i)], "MHz")
[1153]2660 # ensure that we are not iterating past nIF
2661 if i == self.nif()-1: break
2662 self._setselection(savesel)
[931]2663 else:
2664 return
2665 self._add_history("set_restfreqs", varlist)
2666
[2349]2667 @asaplog_post_dec
[1360]2668 def shift_refpix(self, delta):
[1846]2669 """\
[1589]2670 Shift the reference pixel of the Spectra Coordinate by an
2671 integer amount.
[1846]2672
[1589]2673 Parameters:
[1846]2674
[1589]2675 delta: the amount to shift by
[1846]2676
2677 *Note*:
2678
[1589]2679 Be careful using this with broadband data.
[1846]2680
[1360]2681 """
[2349]2682 varlist = vars()
[1731]2683 Scantable.shift_refpix(self, delta)
[2349]2684 s._add_history("shift_refpix", varlist)
[931]2685
[1862]2686 @asaplog_post_dec
[2820]2687 def history(self, filename=None, nrows=-1, start=0):
[1846]2688 """\
[1259]2689 Print the history. Optionally to a file.
[1846]2690
[1348]2691 Parameters:
[1846]2692
[1928]2693 filename: The name of the file to save the history to.
[1846]2694
[1259]2695 """
[2820]2696 n = self._historylength()
2697 if nrows == -1:
2698 nrows = n
2699 if start+nrows > n:
2700 nrows = nrows-start
2701 if n > 1000 and nrows == n:
2702 nrows = 1000
2703 start = n-1000
2704 asaplog.push("Warning: History has {0} entries. Displaying last "
2705 "1000".format(n))
2706 hist = list(self._gethistory(nrows, start))
[794]2707 out = "-"*80
[484]2708 for h in hist:
[2820]2709 if not h.strip():
2710 continue
2711 if h.find("---") >-1:
2712 continue
[489]2713 else:
2714 items = h.split("##")
2715 date = items[0]
2716 func = items[1]
2717 items = items[2:]
[794]2718 out += "\n"+date+"\n"
2719 out += "Function: %s\n Parameters:" % (func)
[489]2720 for i in items:
[1938]2721 if i == '':
2722 continue
[489]2723 s = i.split("=")
[1118]2724 out += "\n %s = %s" % (s[0], s[1])
[2820]2725 out = "\n".join([out, "*"*80])
[1259]2726 if filename is not None:
2727 if filename is "":
2728 filename = 'scantable_history.txt'
2729 filename = os.path.expandvars(os.path.expanduser(filename))
2730 if not os.path.isdir(filename):
2731 data = open(filename, 'w')
2732 data.write(out)
2733 data.close()
2734 else:
2735 msg = "Illegal file name '%s'." % (filename)
[1859]2736 raise IOError(msg)
2737 return page(out)
[2349]2738
[513]2739 #
2740 # Maths business
2741 #
[1862]2742 @asaplog_post_dec
[2818]2743 def average_time(self, mask=None, scanav=False, weight='tint', align=False,
2744 avmode="NONE"):
[1846]2745 """\
[2349]2746 Return the (time) weighted average of a scan. Scans will be averaged
2747 only if the source direction (RA/DEC) is within 1' otherwise
[1846]2748
2749 *Note*:
2750
[1070]2751 in channels only - align if necessary
[1846]2752
[513]2753 Parameters:
[1846]2754
[513]2755 mask: an optional mask (only used for 'var' and 'tsys'
2756 weighting)
[1855]2757
[558]2758 scanav: True averages each scan separately
2759 False (default) averages all scans together,
[1855]2760
[1099]2761 weight: Weighting scheme.
2762 'none' (mean no weight)
2763 'var' (1/var(spec) weighted)
2764 'tsys' (1/Tsys**2 weighted)
2765 'tint' (integration time weighted)
2766 'tintsys' (Tint/Tsys**2)
2767 'median' ( median averaging)
[535]2768 The default is 'tint'
[1855]2769
[931]2770 align: align the spectra in velocity before averaging. It takes
2771 the time of the first spectrum as reference time.
[2818]2772 avmode: 'SOURCE' - also select by source name - or
2773 'NONE' (default). Not applicable for scanav=True or
2774 weight=median
[1846]2775
2776 Example::
2777
[513]2778 # time average the scantable without using a mask
[710]2779 newscan = scan.average_time()
[1846]2780
[513]2781 """
2782 varlist = vars()
[1593]2783 weight = weight or 'TINT'
2784 mask = mask or ()
[2818]2785 scanav = (scanav and 'SCAN') or avmode.upper()
[1118]2786 scan = (self, )
[1859]2787
2788 if align:
2789 scan = (self.freq_align(insitu=False), )
[2818]2790 asaplog.push("Note: Alignment is don on a source-by-source basis")
2791 asaplog.push("Note: Averaging (by default) is not")
2792 # we need to set it to SOURCE averaging here
[1859]2793 s = None
2794 if weight.upper() == 'MEDIAN':
2795 s = scantable(self._math._averagechannel(scan[0], 'MEDIAN',
2796 scanav))
2797 else:
2798 s = scantable(self._math._average(scan, mask, weight.upper(),
2799 scanav))
[1099]2800 s._add_history("average_time", varlist)
[513]2801 return s
[710]2802
[1862]2803 @asaplog_post_dec
[876]2804 def convert_flux(self, jyperk=None, eta=None, d=None, insitu=None):
[1846]2805 """\
[513]2806 Return a scan where all spectra are converted to either
2807 Jansky or Kelvin depending upon the flux units of the scan table.
2808 By default the function tries to look the values up internally.
2809 If it can't find them (or if you want to over-ride), you must
2810 specify EITHER jyperk OR eta (and D which it will try to look up
2811 also if you don't set it). jyperk takes precedence if you set both.
[1846]2812
[513]2813 Parameters:
[1846]2814
[513]2815 jyperk: the Jy / K conversion factor
[1855]2816
[513]2817 eta: the aperture efficiency
[1855]2818
[1928]2819 d: the geometric diameter (metres)
[1855]2820
[513]2821 insitu: if False a new scantable is returned.
2822 Otherwise, the scaling is done in-situ
2823 The default is taken from .asaprc (False)
[1846]2824
[513]2825 """
2826 if insitu is None: insitu = rcParams['insitu']
[876]2827 self._math._setinsitu(insitu)
[513]2828 varlist = vars()
[1593]2829 jyperk = jyperk or -1.0
2830 d = d or -1.0
2831 eta = eta or -1.0
[876]2832 s = scantable(self._math._convertflux(self, d, eta, jyperk))
2833 s._add_history("convert_flux", varlist)
2834 if insitu: self._assign(s)
2835 else: return s
[513]2836
[1862]2837 @asaplog_post_dec
[876]2838 def gain_el(self, poly=None, filename="", method="linear", insitu=None):
[1846]2839 """\
[513]2840 Return a scan after applying a gain-elevation correction.
2841 The correction can be made via either a polynomial or a
2842 table-based interpolation (and extrapolation if necessary).
2843 You specify polynomial coefficients, an ascii table or neither.
2844 If you specify neither, then a polynomial correction will be made
2845 with built in coefficients known for certain telescopes (an error
2846 will occur if the instrument is not known).
2847 The data and Tsys are *divided* by the scaling factors.
[1846]2848
[513]2849 Parameters:
[1846]2850
[513]2851 poly: Polynomial coefficients (default None) to compute a
2852 gain-elevation correction as a function of
2853 elevation (in degrees).
[1855]2854
[513]2855 filename: The name of an ascii file holding correction factors.
2856 The first row of the ascii file must give the column
2857 names and these MUST include columns
[2431]2858 'ELEVATION' (degrees) and 'FACTOR' (multiply data
[513]2859 by this) somewhere.
2860 The second row must give the data type of the
2861 column. Use 'R' for Real and 'I' for Integer.
2862 An example file would be
2863 (actual factors are arbitrary) :
2864
2865 TIME ELEVATION FACTOR
2866 R R R
2867 0.1 0 0.8
2868 0.2 20 0.85
2869 0.3 40 0.9
2870 0.4 60 0.85
2871 0.5 80 0.8
2872 0.6 90 0.75
[1855]2873
[513]2874 method: Interpolation method when correcting from a table.
[2431]2875 Values are 'nearest', 'linear' (default), 'cubic'
2876 and 'spline'
[1855]2877
[513]2878 insitu: if False a new scantable is returned.
2879 Otherwise, the scaling is done in-situ
2880 The default is taken from .asaprc (False)
[1846]2881
[513]2882 """
2883
2884 if insitu is None: insitu = rcParams['insitu']
[876]2885 self._math._setinsitu(insitu)
[513]2886 varlist = vars()
[1593]2887 poly = poly or ()
[513]2888 from os.path import expandvars
2889 filename = expandvars(filename)
[876]2890 s = scantable(self._math._gainel(self, poly, filename, method))
2891 s._add_history("gain_el", varlist)
[1593]2892 if insitu:
2893 self._assign(s)
2894 else:
2895 return s
[710]2896
[1862]2897 @asaplog_post_dec
[931]2898 def freq_align(self, reftime=None, method='cubic', insitu=None):
[1846]2899 """\
[513]2900 Return a scan where all rows have been aligned in frequency/velocity.
2901 The alignment frequency frame (e.g. LSRK) is that set by function
2902 set_freqframe.
[1846]2903
[513]2904 Parameters:
[1855]2905
[513]2906 reftime: reference time to align at. By default, the time of
2907 the first row of data is used.
[1855]2908
[513]2909 method: Interpolation method for regridding the spectra.
[2431]2910 Choose from 'nearest', 'linear', 'cubic' (default)
2911 and 'spline'
[1855]2912
[513]2913 insitu: if False a new scantable is returned.
2914 Otherwise, the scaling is done in-situ
2915 The default is taken from .asaprc (False)
[1846]2916
[513]2917 """
[931]2918 if insitu is None: insitu = rcParams["insitu"]
[2429]2919 oldInsitu = self._math._insitu()
[876]2920 self._math._setinsitu(insitu)
[513]2921 varlist = vars()
[1593]2922 reftime = reftime or ""
[931]2923 s = scantable(self._math._freq_align(self, reftime, method))
[876]2924 s._add_history("freq_align", varlist)
[2429]2925 self._math._setinsitu(oldInsitu)
[2349]2926 if insitu:
2927 self._assign(s)
2928 else:
2929 return s
[513]2930
[1862]2931 @asaplog_post_dec
[1725]2932 def opacity(self, tau=None, insitu=None):
[1846]2933 """\
[513]2934 Apply an opacity correction. The data
2935 and Tsys are multiplied by the correction factor.
[1846]2936
[513]2937 Parameters:
[1855]2938
[1689]2939 tau: (list of) opacity from which the correction factor is
[513]2940 exp(tau*ZD)
[1689]2941 where ZD is the zenith-distance.
2942 If a list is provided, it has to be of length nIF,
2943 nIF*nPol or 1 and in order of IF/POL, e.g.
2944 [opif0pol0, opif0pol1, opif1pol0 ...]
[1725]2945 if tau is `None` the opacities are determined from a
2946 model.
[1855]2947
[513]2948 insitu: if False a new scantable is returned.
2949 Otherwise, the scaling is done in-situ
2950 The default is taken from .asaprc (False)
[1846]2951
[513]2952 """
[2349]2953 if insitu is None:
2954 insitu = rcParams['insitu']
[876]2955 self._math._setinsitu(insitu)
[513]2956 varlist = vars()
[1689]2957 if not hasattr(tau, "__len__"):
2958 tau = [tau]
[876]2959 s = scantable(self._math._opacity(self, tau))
2960 s._add_history("opacity", varlist)
[2349]2961 if insitu:
2962 self._assign(s)
2963 else:
2964 return s
[513]2965
[1862]2966 @asaplog_post_dec
[513]2967 def bin(self, width=5, insitu=None):
[1846]2968 """\
[513]2969 Return a scan where all spectra have been binned up.
[1846]2970
[1348]2971 Parameters:
[1846]2972
[513]2973 width: The bin width (default=5) in pixels
[1855]2974
[513]2975 insitu: if False a new scantable is returned.
2976 Otherwise, the scaling is done in-situ
2977 The default is taken from .asaprc (False)
[1846]2978
[513]2979 """
[2349]2980 if insitu is None:
2981 insitu = rcParams['insitu']
[876]2982 self._math._setinsitu(insitu)
[513]2983 varlist = vars()
[876]2984 s = scantable(self._math._bin(self, width))
[1118]2985 s._add_history("bin", varlist)
[1589]2986 if insitu:
2987 self._assign(s)
2988 else:
2989 return s
[513]2990
[1862]2991 @asaplog_post_dec
[2672]2992 def reshape(self, first, last, insitu=None):
2993 """Resize the band by providing first and last channel.
2994 This will cut off all channels outside [first, last].
2995 """
2996 if insitu is None:
2997 insitu = rcParams['insitu']
2998 varlist = vars()
2999 if last < 0:
3000 last = self.nchan()-1 + last
3001 s = None
3002 if insitu:
3003 s = self
3004 else:
3005 s = self.copy()
3006 s._reshape(first,last)
3007 s._add_history("reshape", varlist)
3008 if not insitu:
3009 return s
3010
3011 @asaplog_post_dec
[513]3012 def resample(self, width=5, method='cubic', insitu=None):
[1846]3013 """\
[1348]3014 Return a scan where all spectra have been binned up.
[1573]3015
[1348]3016 Parameters:
[1846]3017
[513]3018 width: The bin width (default=5) in pixels
[1855]3019
[513]3020 method: Interpolation method when correcting from a table.
[2431]3021 Values are 'nearest', 'linear', 'cubic' (default)
3022 and 'spline'
[1855]3023
[513]3024 insitu: if False a new scantable is returned.
3025 Otherwise, the scaling is done in-situ
3026 The default is taken from .asaprc (False)
[1846]3027
[513]3028 """
[2349]3029 if insitu is None:
3030 insitu = rcParams['insitu']
[876]3031 self._math._setinsitu(insitu)
[513]3032 varlist = vars()
[876]3033 s = scantable(self._math._resample(self, method, width))
[1118]3034 s._add_history("resample", varlist)
[2349]3035 if insitu:
3036 self._assign(s)
3037 else:
3038 return s
[513]3039
[1862]3040 @asaplog_post_dec
[946]3041 def average_pol(self, mask=None, weight='none'):
[1846]3042 """\
[946]3043 Average the Polarisations together.
[1846]3044
[946]3045 Parameters:
[1846]3046
[946]3047 mask: An optional mask defining the region, where the
3048 averaging will be applied. The output will have all
3049 specified points masked.
[1855]3050
[946]3051 weight: Weighting scheme. 'none' (default), 'var' (1/var(spec)
3052 weighted), or 'tsys' (1/Tsys**2 weighted)
[1846]3053
[946]3054 """
3055 varlist = vars()
[1593]3056 mask = mask or ()
[1010]3057 s = scantable(self._math._averagepol(self, mask, weight.upper()))
[1118]3058 s._add_history("average_pol", varlist)
[992]3059 return s
[513]3060
[1862]3061 @asaplog_post_dec
[1145]3062 def average_beam(self, mask=None, weight='none'):
[1846]3063 """\
[1145]3064 Average the Beams together.
[1846]3065
[1145]3066 Parameters:
3067 mask: An optional mask defining the region, where the
3068 averaging will be applied. The output will have all
3069 specified points masked.
[1855]3070
[1145]3071 weight: Weighting scheme. 'none' (default), 'var' (1/var(spec)
3072 weighted), or 'tsys' (1/Tsys**2 weighted)
[1846]3073
[1145]3074 """
3075 varlist = vars()
[1593]3076 mask = mask or ()
[1145]3077 s = scantable(self._math._averagebeams(self, mask, weight.upper()))
3078 s._add_history("average_beam", varlist)
3079 return s
3080
[1586]3081 def parallactify(self, pflag):
[1846]3082 """\
[1843]3083 Set a flag to indicate whether this data should be treated as having
[1617]3084 been 'parallactified' (total phase == 0.0)
[1846]3085
[1617]3086 Parameters:
[1855]3087
[1843]3088 pflag: Bool indicating whether to turn this on (True) or
[1617]3089 off (False)
[1846]3090
[1617]3091 """
[1586]3092 varlist = vars()
3093 self._parallactify(pflag)
3094 self._add_history("parallactify", varlist)
3095
[1862]3096 @asaplog_post_dec
[992]3097 def convert_pol(self, poltype=None):
[1846]3098 """\
[992]3099 Convert the data to a different polarisation type.
[1565]3100 Note that you will need cross-polarisation terms for most conversions.
[1846]3101
[992]3102 Parameters:
[1855]3103
[992]3104 poltype: The new polarisation type. Valid types are:
[2431]3105 'linear', 'circular', 'stokes' and 'linpol'
[1846]3106
[992]3107 """
3108 varlist = vars()
[1859]3109 s = scantable(self._math._convertpol(self, poltype))
[1118]3110 s._add_history("convert_pol", varlist)
[992]3111 return s
3112
[1862]3113 @asaplog_post_dec
[2269]3114 def smooth(self, kernel="hanning", width=5.0, order=2, plot=False,
3115 insitu=None):
[1846]3116 """\
[513]3117 Smooth the spectrum by the specified kernel (conserving flux).
[1846]3118
[513]3119 Parameters:
[1846]3120
[513]3121 kernel: The type of smoothing kernel. Select from
[1574]3122 'hanning' (default), 'gaussian', 'boxcar', 'rmedian'
3123 or 'poly'
[1855]3124
[513]3125 width: The width of the kernel in pixels. For hanning this is
3126 ignored otherwise it defauls to 5 pixels.
3127 For 'gaussian' it is the Full Width Half
3128 Maximum. For 'boxcar' it is the full width.
[1574]3129 For 'rmedian' and 'poly' it is the half width.
[1855]3130
[1574]3131 order: Optional parameter for 'poly' kernel (default is 2), to
3132 specify the order of the polnomial. Ignored by all other
3133 kernels.
[1855]3134
[1819]3135 plot: plot the original and the smoothed spectra.
3136 In this each indivual fit has to be approved, by
3137 typing 'y' or 'n'
[1855]3138
[513]3139 insitu: if False a new scantable is returned.
3140 Otherwise, the scaling is done in-situ
3141 The default is taken from .asaprc (False)
[1846]3142
[513]3143 """
3144 if insitu is None: insitu = rcParams['insitu']
[876]3145 self._math._setinsitu(insitu)
[513]3146 varlist = vars()
[1819]3147
3148 if plot: orgscan = self.copy()
3149
[1574]3150 s = scantable(self._math._smooth(self, kernel.lower(), width, order))
[876]3151 s._add_history("smooth", varlist)
[1819]3152
[2610]3153 action = 'H'
[1819]3154 if plot:
[2150]3155 from asap.asapplotter import new_asaplot
3156 theplot = new_asaplot(rcParams['plotter.gui'])
[2535]3157 from matplotlib import rc as rcp
3158 rcp('lines', linewidth=1)
[2150]3159 theplot.set_panels()
[1819]3160 ylab=s._get_ordinate_label()
[2150]3161 #theplot.palette(0,["#777777","red"])
[1819]3162 for r in xrange(s.nrow()):
3163 xsm=s._getabcissa(r)
3164 ysm=s._getspectrum(r)
3165 xorg=orgscan._getabcissa(r)
3166 yorg=orgscan._getspectrum(r)
[2610]3167 if action != "N": #skip plotting if rejecting all
3168 theplot.clear()
3169 theplot.hold()
3170 theplot.set_axes('ylabel',ylab)
3171 theplot.set_axes('xlabel',s._getabcissalabel(r))
3172 theplot.set_axes('title',s._getsourcename(r))
3173 theplot.set_line(label='Original',color="#777777")
3174 theplot.plot(xorg,yorg)
3175 theplot.set_line(label='Smoothed',color="red")
3176 theplot.plot(xsm,ysm)
3177 ### Ugly part for legend
3178 for i in [0,1]:
3179 theplot.subplots[0]['lines'].append(
3180 [theplot.subplots[0]['axes'].lines[i]]
3181 )
3182 theplot.release()
3183 ### Ugly part for legend
3184 theplot.subplots[0]['lines']=[]
3185 res = self._get_verify_action("Accept smoothing?",action)
3186 #print "IF%d, POL%d: got result = %s" %(s.getif(r),s.getpol(r),res)
3187 if r == 0: action = None
3188 #res = raw_input("Accept smoothing ([y]/n): ")
[1819]3189 if res.upper() == 'N':
[2610]3190 # reject for the current rows
[1819]3191 s._setspectrum(yorg, r)
[2610]3192 elif res.upper() == 'R':
3193 # reject all the following rows
3194 action = "N"
3195 s._setspectrum(yorg, r)
3196 elif res.upper() == 'A':
3197 # accept all the following rows
3198 break
[2150]3199 theplot.quit()
3200 del theplot
[1819]3201 del orgscan
3202
[876]3203 if insitu: self._assign(s)
3204 else: return s
[513]3205
[2186]3206 @asaplog_post_dec
[2435]3207 def regrid_channel(self, width=5, plot=False, insitu=None):
3208 """\
3209 Regrid the spectra by the specified channel width
3210
3211 Parameters:
3212
3213 width: The channel width (float) of regridded spectra
3214 in the current spectral unit.
3215
3216 plot: [NOT IMPLEMENTED YET]
3217 plot the original and the regridded spectra.
3218 In this each indivual fit has to be approved, by
3219 typing 'y' or 'n'
3220
3221 insitu: if False a new scantable is returned.
3222 Otherwise, the scaling is done in-situ
3223 The default is taken from .asaprc (False)
3224
3225 """
3226 if insitu is None: insitu = rcParams['insitu']
3227 varlist = vars()
3228
3229 if plot:
3230 asaplog.post()
3231 asaplog.push("Verification plot is not implemtnetd yet.")
3232 asaplog.post("WARN")
3233
3234 s = self.copy()
3235 s._regrid_specchan(width)
3236
3237 s._add_history("regrid_channel", varlist)
3238
3239# if plot:
3240# from asap.asapplotter import new_asaplot
3241# theplot = new_asaplot(rcParams['plotter.gui'])
[2535]3242# from matplotlib import rc as rcp
3243# rcp('lines', linewidth=1)
[2435]3244# theplot.set_panels()
3245# ylab=s._get_ordinate_label()
3246# #theplot.palette(0,["#777777","red"])
3247# for r in xrange(s.nrow()):
3248# xsm=s._getabcissa(r)
3249# ysm=s._getspectrum(r)
3250# xorg=orgscan._getabcissa(r)
3251# yorg=orgscan._getspectrum(r)
3252# theplot.clear()
3253# theplot.hold()
3254# theplot.set_axes('ylabel',ylab)
3255# theplot.set_axes('xlabel',s._getabcissalabel(r))
3256# theplot.set_axes('title',s._getsourcename(r))
3257# theplot.set_line(label='Original',color="#777777")
3258# theplot.plot(xorg,yorg)
3259# theplot.set_line(label='Smoothed',color="red")
3260# theplot.plot(xsm,ysm)
3261# ### Ugly part for legend
3262# for i in [0,1]:
3263# theplot.subplots[0]['lines'].append(
3264# [theplot.subplots[0]['axes'].lines[i]]
3265# )
3266# theplot.release()
3267# ### Ugly part for legend
3268# theplot.subplots[0]['lines']=[]
3269# res = raw_input("Accept smoothing ([y]/n): ")
3270# if res.upper() == 'N':
3271# s._setspectrum(yorg, r)
3272# theplot.quit()
3273# del theplot
3274# del orgscan
3275
3276 if insitu: self._assign(s)
3277 else: return s
3278
3279 @asaplog_post_dec
[2186]3280 def _parse_wn(self, wn):
3281 if isinstance(wn, list) or isinstance(wn, tuple):
3282 return wn
3283 elif isinstance(wn, int):
3284 return [ wn ]
3285 elif isinstance(wn, str):
[2277]3286 if '-' in wn: # case 'a-b' : return [a,a+1,...,b-1,b]
[2186]3287 val = wn.split('-')
3288 val = [int(val[0]), int(val[1])]
3289 val.sort()
3290 res = [i for i in xrange(val[0], val[1]+1)]
[2277]3291 elif wn[:2] == '<=' or wn[:2] == '=<': # cases '<=a','=<a' : return [0,1,...,a-1,a]
[2186]3292 val = int(wn[2:])+1
3293 res = [i for i in xrange(val)]
[2277]3294 elif wn[-2:] == '>=' or wn[-2:] == '=>': # cases 'a>=','a=>' : return [0,1,...,a-1,a]
[2186]3295 val = int(wn[:-2])+1
3296 res = [i for i in xrange(val)]
[2277]3297 elif wn[0] == '<': # case '<a' : return [0,1,...,a-2,a-1]
[2186]3298 val = int(wn[1:])
3299 res = [i for i in xrange(val)]
[2277]3300 elif wn[-1] == '>': # case 'a>' : return [0,1,...,a-2,a-1]
[2186]3301 val = int(wn[:-1])
3302 res = [i for i in xrange(val)]
[2411]3303 elif wn[:2] == '>=' or wn[:2] == '=>': # cases '>=a','=>a' : return [a,-999], which is
3304 # then interpreted in C++
3305 # side as [a,a+1,...,a_nyq]
3306 # (CAS-3759)
[2186]3307 val = int(wn[2:])
[2411]3308 res = [val, -999]
3309 #res = [i for i in xrange(val, self.nchan()/2+1)]
3310 elif wn[-2:] == '<=' or wn[-2:] == '=<': # cases 'a<=','a=<' : return [a,-999], which is
3311 # then interpreted in C++
3312 # side as [a,a+1,...,a_nyq]
3313 # (CAS-3759)
[2186]3314 val = int(wn[:-2])
[2411]3315 res = [val, -999]
3316 #res = [i for i in xrange(val, self.nchan()/2+1)]
3317 elif wn[0] == '>': # case '>a' : return [a+1,-999], which is
3318 # then interpreted in C++
3319 # side as [a+1,a+2,...,a_nyq]
3320 # (CAS-3759)
[2186]3321 val = int(wn[1:])+1
[2411]3322 res = [val, -999]
3323 #res = [i for i in xrange(val, self.nchan()/2+1)]
3324 elif wn[-1] == '<': # case 'a<' : return [a+1,-999], which is
3325 # then interpreted in C++
3326 # side as [a+1,a+2,...,a_nyq]
3327 # (CAS-3759)
[2186]3328 val = int(wn[:-1])+1
[2411]3329 res = [val, -999]
3330 #res = [i for i in xrange(val, self.nchan()/2+1)]
[2012]3331
[2186]3332 return res
3333 else:
3334 msg = 'wrong value given for addwn/rejwn'
3335 raise RuntimeError(msg)
3336
[2713]3337 @asaplog_post_dec
[2810]3338 def apply_bltable(self, insitu=None, retfitres=None, inbltable=None, outbltable=None, overwrite=None):
[2767]3339 """\
3340 Subtract baseline based on parameters written in Baseline Table.
3341
3342 Parameters:
[2809]3343 insitu: if True, baseline fitting/subtraction is done
[2810]3344 in-situ. If False, a new scantable with
3345 baseline subtracted is returned. Actually,
3346 format of the returned value depends on both
3347 insitu and retfitres (see below).
[2767]3348 The default is taken from .asaprc (False)
[2810]3349 retfitres: if True, the results of baseline fitting (i.e.,
3350 coefficients and rms) are returned.
3351 default is False.
3352 The format of the returned value of this
3353 function varies as follows:
3354 (1) in case insitu=True and retfitres=True:
3355 fitting result.
3356 (2) in case insitu=True and retfitres=False:
3357 None.
3358 (3) in case insitu=False and retfitres=True:
3359 a dictionary containing a new scantable
3360 (with baseline subtracted) and the fitting
3361 results.
3362 (4) in case insitu=False and retfitres=False:
3363 a new scantable (with baseline subtracted).
[2767]3364 inbltable: name of input baseline table. The row number of
3365 scantable and that of inbltable must be
3366 identical.
3367 outbltable: name of output baseline table where baseline
3368 parameters and fitting results recorded.
3369 default is ''(no output).
[2809]3370 overwrite: if True when an existing baseline table is
3371 specified for outbltable, overwrites it.
3372 Otherwise there is no harm.
[2767]3373 default is False.
3374 """
3375
3376 try:
3377 varlist = vars()
[2810]3378 if retfitres is None: retfitres = False
[2767]3379 if inbltable is None: raise ValueError("bltable missing.")
3380 if outbltable is None: outbltable = ''
3381 if overwrite is None: overwrite = False
3382
3383 if insitu is None: insitu = rcParams['insitu']
3384 if insitu:
3385 workscan = self
3386 else:
3387 workscan = self.copy()
3388
3389 sres = workscan._apply_bltable(inbltable,
[2810]3390 retfitres,
[2767]3391 outbltable,
3392 os.path.exists(outbltable),
3393 overwrite)
[2810]3394 if retfitres: res = parse_fitresult(sres)
[2767]3395
3396 workscan._add_history('apply_bltable', varlist)
3397
3398 if insitu:
3399 self._assign(workscan)
[2810]3400 if retfitres:
3401 return res
3402 else:
3403 return None
[2767]3404 else:
[2810]3405 if retfitres:
3406 return {'scantable': workscan, 'fitresults': res}
3407 else:
3408 return workscan
[2767]3409
3410 except RuntimeError, e:
3411 raise_fitting_failure_exception(e)
3412
3413 @asaplog_post_dec
[2810]3414 def sub_baseline(self, insitu=None, retfitres=None, blinfo=None, bltable=None, overwrite=None):
[2767]3415 """\
3416 Subtract baseline based on parameters written in the input list.
3417
3418 Parameters:
[2809]3419 insitu: if True, baseline fitting/subtraction is done
[2810]3420 in-situ. If False, a new scantable with
3421 baseline subtracted is returned. Actually,
3422 format of the returned value depends on both
3423 insitu and retfitres (see below).
[2767]3424 The default is taken from .asaprc (False)
[2810]3425 retfitres: if True, the results of baseline fitting (i.e.,
3426 coefficients and rms) are returned.
3427 default is False.
3428 The format of the returned value of this
3429 function varies as follows:
3430 (1) in case insitu=True and retfitres=True:
3431 fitting result.
3432 (2) in case insitu=True and retfitres=False:
3433 None.
3434 (3) in case insitu=False and retfitres=True:
3435 a dictionary containing a new scantable
3436 (with baseline subtracted) and the fitting
3437 results.
3438 (4) in case insitu=False and retfitres=False:
3439 a new scantable (with baseline subtracted).
[2767]3440 blinfo: baseline parameter set stored in a dictionary
3441 or a list of dictionary. Each dictionary
3442 corresponds to each spectrum and must contain
3443 the following keys and values:
3444 'row': row number,
3445 'blfunc': function name. available ones include
3446 'poly', 'chebyshev', 'cspline' and
3447 'sinusoid',
3448 'order': maximum order of polynomial. needed
3449 if blfunc='poly' or 'chebyshev',
3450 'npiece': number or piecewise polynomial.
3451 needed if blfunc='cspline',
3452 'nwave': a list of sinusoidal wave numbers.
3453 needed if blfunc='sinusoid', and
3454 'masklist': min-max windows for channel mask.
3455 the specified ranges will be used
3456 for fitting.
3457 bltable: name of output baseline table where baseline
3458 parameters and fitting results recorded.
3459 default is ''(no output).
[2809]3460 overwrite: if True when an existing baseline table is
3461 specified for bltable, overwrites it.
3462 Otherwise there is no harm.
[2767]3463 default is False.
3464
3465 Example:
3466 sub_baseline(blinfo=[{'row':0, 'blfunc':'poly', 'order':5,
3467 'masklist':[[10,350],[352,510]]},
3468 {'row':1, 'blfunc':'cspline', 'npiece':3,
3469 'masklist':[[3,16],[19,404],[407,511]]}
3470 ])
3471
3472 the first spectrum (row=0) will be fitted with polynomial
3473 of order=5 and the next one (row=1) will be fitted with cubic
3474 spline consisting of 3 pieces.
3475 """
3476
3477 try:
3478 varlist = vars()
[2810]3479 if retfitres is None: retfitres = False
[2767]3480 if blinfo is None: blinfo = []
3481 if bltable is None: bltable = ''
3482 if overwrite is None: overwrite = False
3483
3484 if insitu is None: insitu = rcParams['insitu']
3485 if insitu:
3486 workscan = self
3487 else:
3488 workscan = self.copy()
3489
3490 nrow = workscan.nrow()
3491
3492 in_blinfo = pack_blinfo(blinfo=blinfo, maxirow=nrow)
3493
3494 print "in_blinfo=< "+ str(in_blinfo)+" >"
3495
3496 sres = workscan._sub_baseline(in_blinfo,
[2810]3497 retfitres,
[2767]3498 bltable,
3499 os.path.exists(bltable),
3500 overwrite)
[2810]3501 if retfitres: res = parse_fitresult(sres)
[2767]3502
3503 workscan._add_history('sub_baseline', varlist)
3504
3505 if insitu:
3506 self._assign(workscan)
[2810]3507 if retfitres:
3508 return res
3509 else:
3510 return None
[2767]3511 else:
[2810]3512 if retfitres:
3513 return {'scantable': workscan, 'fitresults': res}
3514 else:
3515 return workscan
[2767]3516
3517 except RuntimeError, e:
3518 raise_fitting_failure_exception(e)
3519
3520 @asaplog_post_dec
[2713]3521 def calc_aic(self, value=None, blfunc=None, order=None, mask=None,
3522 whichrow=None, uselinefinder=None, edge=None,
3523 threshold=None, chan_avg_limit=None):
3524 """\
3525 Calculates and returns model selection criteria for a specified
3526 baseline model and a given spectrum data.
3527 Available values include Akaike Information Criterion (AIC), the
3528 corrected Akaike Information Criterion (AICc) by Sugiura(1978),
3529 Bayesian Information Criterion (BIC) and the Generalised Cross
3530 Validation (GCV).
[2186]3531
[2713]3532 Parameters:
3533 value: name of model selection criteria to calculate.
3534 available ones include 'aic', 'aicc', 'bic' and
3535 'gcv'. default is 'aicc'.
3536 blfunc: baseline function name. available ones include
3537 'chebyshev', 'cspline' and 'sinusoid'.
3538 default is 'chebyshev'.
3539 order: parameter for basline function. actually stands for
3540 order of polynomial (order) for 'chebyshev',
3541 number of spline pieces (npiece) for 'cspline' and
3542 maximum wave number for 'sinusoid', respectively.
3543 default is 5 (which is also the default order value
3544 for [auto_]chebyshev_baseline()).
3545 mask: an optional mask. default is [].
3546 whichrow: row number. default is 0 (the first row)
3547 uselinefinder: use sd.linefinder() to flag out line regions
3548 default is True.
3549 edge: an optional number of channel to drop at
3550 the edge of spectrum. If only one value is
3551 specified, the same number will be dropped
3552 from both sides of the spectrum. Default
3553 is to keep all channels. Nested tuples
3554 represent individual edge selection for
3555 different IFs (a number of spectral channels
3556 can be different)
3557 default is (0, 0).
3558 threshold: the threshold used by line finder. It is
3559 better to keep it large as only strong lines
3560 affect the baseline solution.
3561 default is 3.
3562 chan_avg_limit: a maximum number of consequtive spectral
3563 channels to average during the search of
3564 weak and broad lines. The default is no
3565 averaging (and no search for weak lines).
3566 If such lines can affect the fitted baseline
3567 (e.g. a high order polynomial is fitted),
3568 increase this parameter (usually values up
3569 to 8 are reasonable). Most users of this
3570 method should find the default value sufficient.
3571 default is 1.
3572
3573 Example:
3574 aic = scan.calc_aic(blfunc='chebyshev', order=5, whichrow=0)
3575 """
3576
3577 try:
3578 varlist = vars()
3579
3580 if value is None: value = 'aicc'
3581 if blfunc is None: blfunc = 'chebyshev'
3582 if order is None: order = 5
3583 if mask is None: mask = []
3584 if whichrow is None: whichrow = 0
3585 if uselinefinder is None: uselinefinder = True
3586 if edge is None: edge = (0, 0)
3587 if threshold is None: threshold = 3
3588 if chan_avg_limit is None: chan_avg_limit = 1
3589
3590 return self._calc_aic(value, blfunc, order, mask,
3591 whichrow, uselinefinder, edge,
3592 threshold, chan_avg_limit)
3593
3594 except RuntimeError, e:
3595 raise_fitting_failure_exception(e)
3596
[1862]3597 @asaplog_post_dec
[2771]3598 def sinusoid_baseline(self, mask=None, applyfft=None,
[2269]3599 fftmethod=None, fftthresh=None,
[2771]3600 addwn=None, rejwn=None,
3601 insitu=None,
3602 clipthresh=None, clipniter=None,
3603 plot=None,
3604 getresidual=None,
3605 showprogress=None, minnrow=None,
3606 outlog=None,
[2767]3607 blfile=None, csvformat=None,
3608 bltable=None):
[2047]3609 """\
[2349]3610 Return a scan which has been baselined (all rows) with sinusoidal
3611 functions.
3612
[2047]3613 Parameters:
[2186]3614 mask: an optional mask
3615 applyfft: if True use some method, such as FFT, to find
3616 strongest sinusoidal components in the wavenumber
3617 domain to be used for baseline fitting.
3618 default is True.
3619 fftmethod: method to find the strong sinusoidal components.
3620 now only 'fft' is available and it is the default.
3621 fftthresh: the threshold to select wave numbers to be used for
3622 fitting from the distribution of amplitudes in the
3623 wavenumber domain.
3624 both float and string values accepted.
3625 given a float value, the unit is set to sigma.
3626 for string values, allowed formats include:
[2349]3627 'xsigma' or 'x' (= x-sigma level. e.g.,
3628 '3sigma'), or
[2186]3629 'topx' (= the x strongest ones, e.g. 'top5').
3630 default is 3.0 (unit: sigma).
3631 addwn: the additional wave numbers to be used for fitting.
3632 list or integer value is accepted to specify every
3633 wave numbers. also string value can be used in case
3634 you need to specify wave numbers in a certain range,
3635 e.g., 'a-b' (= a, a+1, a+2, ..., b-1, b),
3636 '<a' (= 0,1,...,a-2,a-1),
3637 '>=a' (= a, a+1, ... up to the maximum wave
3638 number corresponding to the Nyquist
3639 frequency for the case of FFT).
[2411]3640 default is [0].
[2186]3641 rejwn: the wave numbers NOT to be used for fitting.
3642 can be set just as addwn but has higher priority:
3643 wave numbers which are specified both in addwn
3644 and rejwn will NOT be used. default is [].
[2771]3645 insitu: if False a new scantable is returned.
3646 Otherwise, the scaling is done in-situ
3647 The default is taken from .asaprc (False)
[2081]3648 clipthresh: Clipping threshold. (default is 3.0, unit: sigma)
[2349]3649 clipniter: maximum number of iteration of 'clipthresh'-sigma
3650 clipping (default is 0)
[2081]3651 plot: *** CURRENTLY UNAVAILABLE, ALWAYS FALSE ***
3652 plot the fit and the residual. In this each
3653 indivual fit has to be approved, by typing 'y'
3654 or 'n'
3655 getresidual: if False, returns best-fit values instead of
3656 residual. (default is True)
[2189]3657 showprogress: show progress status for large data.
3658 default is True.
3659 minnrow: minimum number of input spectra to show.
3660 default is 1000.
[2081]3661 outlog: Output the coefficients of the best-fit
3662 function to logger (default is False)
3663 blfile: Name of a text file in which the best-fit
3664 parameter values to be written
[2186]3665 (default is '': no file/logger output)
[2641]3666 csvformat: if True blfile is csv-formatted, default is False.
[2767]3667 bltable: name of a baseline table where fitting results
3668 (coefficients, rms, etc.) are to be written.
3669 if given, fitting results will NOT be output to
3670 scantable (insitu=True) or None will be
3671 returned (insitu=False).
3672 (default is "": no table output)
[2047]3673
3674 Example:
[2349]3675 # return a scan baselined by a combination of sinusoidal curves
3676 # having wave numbers in spectral window up to 10,
[2047]3677 # also with 3-sigma clipping, iteration up to 4 times
[2186]3678 bscan = scan.sinusoid_baseline(addwn='<=10',clipthresh=3.0,clipniter=4)
[2081]3679
3680 Note:
3681 The best-fit parameter values output in logger and/or blfile are now
3682 based on specunit of 'channel'.
[2047]3683 """
3684
[2186]3685 try:
3686 varlist = vars()
[2047]3687
[2186]3688 if insitu is None: insitu = rcParams['insitu']
3689 if insitu:
3690 workscan = self
3691 else:
3692 workscan = self.copy()
3693
[2410]3694 if mask is None: mask = []
[2186]3695 if applyfft is None: applyfft = True
3696 if fftmethod is None: fftmethod = 'fft'
3697 if fftthresh is None: fftthresh = 3.0
[2411]3698 if addwn is None: addwn = [0]
[2186]3699 if rejwn is None: rejwn = []
3700 if clipthresh is None: clipthresh = 3.0
3701 if clipniter is None: clipniter = 0
3702 if plot is None: plot = False
3703 if getresidual is None: getresidual = True
[2189]3704 if showprogress is None: showprogress = True
3705 if minnrow is None: minnrow = 1000
[2186]3706 if outlog is None: outlog = False
3707 if blfile is None: blfile = ''
[2641]3708 if csvformat is None: csvformat = False
[2767]3709 if bltable is None: bltable = ''
[2047]3710
[2767]3711 sapplyfft = 'true' if applyfft else 'false'
3712 fftinfo = ','.join([sapplyfft, fftmethod.lower(), str(fftthresh).lower()])
[2641]3713
[2767]3714 scsvformat = 'T' if csvformat else 'F'
3715
[2081]3716 #CURRENTLY, PLOT=true is UNAVAILABLE UNTIL sinusoidal fitting is implemented as a fitter method.
[2767]3717 workscan._sinusoid_baseline(mask,
3718 fftinfo,
3719 #applyfft, fftmethod.lower(),
3720 #str(fftthresh).lower(),
[2349]3721 workscan._parse_wn(addwn),
[2643]3722 workscan._parse_wn(rejwn),
3723 clipthresh, clipniter,
3724 getresidual,
[2349]3725 pack_progress_params(showprogress,
[2641]3726 minnrow),
[2767]3727 outlog, scsvformat+blfile,
3728 bltable)
[2186]3729 workscan._add_history('sinusoid_baseline', varlist)
[2767]3730
3731 if bltable == '':
3732 if insitu:
3733 self._assign(workscan)
3734 else:
3735 return workscan
[2047]3736 else:
[2767]3737 if not insitu:
3738 return None
[2047]3739
3740 except RuntimeError, e:
[2186]3741 raise_fitting_failure_exception(e)
[2047]3742
3743
[2186]3744 @asaplog_post_dec
[2771]3745 def auto_sinusoid_baseline(self, mask=None, applyfft=None,
[2349]3746 fftmethod=None, fftthresh=None,
[2771]3747 addwn=None, rejwn=None,
3748 insitu=None,
3749 clipthresh=None, clipniter=None,
3750 edge=None, threshold=None, chan_avg_limit=None,
3751 plot=None,
3752 getresidual=None,
3753 showprogress=None, minnrow=None,
3754 outlog=None,
[2767]3755 blfile=None, csvformat=None,
3756 bltable=None):
[2047]3757 """\
[2349]3758 Return a scan which has been baselined (all rows) with sinusoidal
3759 functions.
[2047]3760 Spectral lines are detected first using linefinder and masked out
3761 to avoid them affecting the baseline solution.
3762
3763 Parameters:
[2189]3764 mask: an optional mask retreived from scantable
3765 applyfft: if True use some method, such as FFT, to find
3766 strongest sinusoidal components in the wavenumber
3767 domain to be used for baseline fitting.
3768 default is True.
3769 fftmethod: method to find the strong sinusoidal components.
3770 now only 'fft' is available and it is the default.
3771 fftthresh: the threshold to select wave numbers to be used for
3772 fitting from the distribution of amplitudes in the
3773 wavenumber domain.
3774 both float and string values accepted.
3775 given a float value, the unit is set to sigma.
3776 for string values, allowed formats include:
[2349]3777 'xsigma' or 'x' (= x-sigma level. e.g.,
3778 '3sigma'), or
[2189]3779 'topx' (= the x strongest ones, e.g. 'top5').
3780 default is 3.0 (unit: sigma).
3781 addwn: the additional wave numbers to be used for fitting.
3782 list or integer value is accepted to specify every
3783 wave numbers. also string value can be used in case
3784 you need to specify wave numbers in a certain range,
3785 e.g., 'a-b' (= a, a+1, a+2, ..., b-1, b),
3786 '<a' (= 0,1,...,a-2,a-1),
3787 '>=a' (= a, a+1, ... up to the maximum wave
3788 number corresponding to the Nyquist
3789 frequency for the case of FFT).
[2411]3790 default is [0].
[2189]3791 rejwn: the wave numbers NOT to be used for fitting.
3792 can be set just as addwn but has higher priority:
3793 wave numbers which are specified both in addwn
3794 and rejwn will NOT be used. default is [].
[2771]3795 insitu: if False a new scantable is returned.
3796 Otherwise, the scaling is done in-situ
3797 The default is taken from .asaprc (False)
[2189]3798 clipthresh: Clipping threshold. (default is 3.0, unit: sigma)
[2349]3799 clipniter: maximum number of iteration of 'clipthresh'-sigma
3800 clipping (default is 0)
[2189]3801 edge: an optional number of channel to drop at
3802 the edge of spectrum. If only one value is
3803 specified, the same number will be dropped
3804 from both sides of the spectrum. Default
3805 is to keep all channels. Nested tuples
3806 represent individual edge selection for
3807 different IFs (a number of spectral channels
3808 can be different)
3809 threshold: the threshold used by line finder. It is
3810 better to keep it large as only strong lines
3811 affect the baseline solution.
3812 chan_avg_limit: a maximum number of consequtive spectral
3813 channels to average during the search of
3814 weak and broad lines. The default is no
3815 averaging (and no search for weak lines).
3816 If such lines can affect the fitted baseline
3817 (e.g. a high order polynomial is fitted),
3818 increase this parameter (usually values up
3819 to 8 are reasonable). Most users of this
3820 method should find the default value sufficient.
3821 plot: *** CURRENTLY UNAVAILABLE, ALWAYS FALSE ***
3822 plot the fit and the residual. In this each
3823 indivual fit has to be approved, by typing 'y'
3824 or 'n'
3825 getresidual: if False, returns best-fit values instead of
3826 residual. (default is True)
3827 showprogress: show progress status for large data.
3828 default is True.
3829 minnrow: minimum number of input spectra to show.
3830 default is 1000.
3831 outlog: Output the coefficients of the best-fit
3832 function to logger (default is False)
3833 blfile: Name of a text file in which the best-fit
3834 parameter values to be written
3835 (default is "": no file/logger output)
[2641]3836 csvformat: if True blfile is csv-formatted, default is False.
[2767]3837 bltable: name of a baseline table where fitting results
3838 (coefficients, rms, etc.) are to be written.
3839 if given, fitting results will NOT be output to
3840 scantable (insitu=True) or None will be
3841 returned (insitu=False).
3842 (default is "": no table output)
[2047]3843
3844 Example:
[2186]3845 bscan = scan.auto_sinusoid_baseline(addwn='<=10', insitu=False)
[2081]3846
3847 Note:
3848 The best-fit parameter values output in logger and/or blfile are now
3849 based on specunit of 'channel'.
[2047]3850 """
3851
[2186]3852 try:
3853 varlist = vars()
[2047]3854
[2186]3855 if insitu is None: insitu = rcParams['insitu']
3856 if insitu:
3857 workscan = self
[2047]3858 else:
[2186]3859 workscan = self.copy()
3860
[2410]3861 if mask is None: mask = []
[2186]3862 if applyfft is None: applyfft = True
3863 if fftmethod is None: fftmethod = 'fft'
3864 if fftthresh is None: fftthresh = 3.0
[2411]3865 if addwn is None: addwn = [0]
[2186]3866 if rejwn is None: rejwn = []
3867 if clipthresh is None: clipthresh = 3.0
3868 if clipniter is None: clipniter = 0
3869 if edge is None: edge = (0,0)
3870 if threshold is None: threshold = 3
3871 if chan_avg_limit is None: chan_avg_limit = 1
3872 if plot is None: plot = False
3873 if getresidual is None: getresidual = True
[2189]3874 if showprogress is None: showprogress = True
3875 if minnrow is None: minnrow = 1000
[2186]3876 if outlog is None: outlog = False
3877 if blfile is None: blfile = ''
[2641]3878 if csvformat is None: csvformat = False
[2767]3879 if bltable is None: bltable = ''
[2047]3880
[2767]3881 sapplyfft = 'true' if applyfft else 'false'
3882 fftinfo = ','.join([sapplyfft, fftmethod.lower(), str(fftthresh).lower()])
[2641]3883
[2767]3884 scsvformat = 'T' if csvformat else 'F'
3885
[2277]3886 #CURRENTLY, PLOT=true is UNAVAILABLE UNTIL sinusoidal fitting is implemented as a fitter method.
[2767]3887 workscan._auto_sinusoid_baseline(mask,
3888 fftinfo,
[2349]3889 workscan._parse_wn(addwn),
3890 workscan._parse_wn(rejwn),
3891 clipthresh, clipniter,
3892 normalise_edge_param(edge),
3893 threshold, chan_avg_limit,
3894 getresidual,
3895 pack_progress_params(showprogress,
3896 minnrow),
[2767]3897 outlog, scsvformat+blfile, bltable)
[2047]3898 workscan._add_history("auto_sinusoid_baseline", varlist)
[2767]3899
3900 if bltable == '':
3901 if insitu:
3902 self._assign(workscan)
3903 else:
3904 return workscan
[2047]3905 else:
[2767]3906 if not insitu:
3907 return None
[2047]3908
3909 except RuntimeError, e:
[2186]3910 raise_fitting_failure_exception(e)
[2047]3911
3912 @asaplog_post_dec
[2771]3913 def cspline_baseline(self, mask=None, npiece=None, insitu=None,
[2349]3914 clipthresh=None, clipniter=None, plot=None,
3915 getresidual=None, showprogress=None, minnrow=None,
[2767]3916 outlog=None, blfile=None, csvformat=None,
3917 bltable=None):
[1846]3918 """\
[2349]3919 Return a scan which has been baselined (all rows) by cubic spline
3920 function (piecewise cubic polynomial).
3921
[513]3922 Parameters:
[2771]3923 mask: An optional mask
3924 npiece: Number of pieces. (default is 2)
[2189]3925 insitu: If False a new scantable is returned.
3926 Otherwise, the scaling is done in-situ
3927 The default is taken from .asaprc (False)
3928 clipthresh: Clipping threshold. (default is 3.0, unit: sigma)
[2349]3929 clipniter: maximum number of iteration of 'clipthresh'-sigma
3930 clipping (default is 0)
[2189]3931 plot: *** CURRENTLY UNAVAILABLE, ALWAYS FALSE ***
3932 plot the fit and the residual. In this each
3933 indivual fit has to be approved, by typing 'y'
3934 or 'n'
3935 getresidual: if False, returns best-fit values instead of
3936 residual. (default is True)
3937 showprogress: show progress status for large data.
3938 default is True.
3939 minnrow: minimum number of input spectra to show.
3940 default is 1000.
3941 outlog: Output the coefficients of the best-fit
3942 function to logger (default is False)
3943 blfile: Name of a text file in which the best-fit
3944 parameter values to be written
3945 (default is "": no file/logger output)
[2641]3946 csvformat: if True blfile is csv-formatted, default is False.
[2767]3947 bltable: name of a baseline table where fitting results
3948 (coefficients, rms, etc.) are to be written.
3949 if given, fitting results will NOT be output to
3950 scantable (insitu=True) or None will be
3951 returned (insitu=False).
3952 (default is "": no table output)
[1846]3953
[2012]3954 Example:
[2349]3955 # return a scan baselined by a cubic spline consisting of 2 pieces
3956 # (i.e., 1 internal knot),
[2012]3957 # also with 3-sigma clipping, iteration up to 4 times
3958 bscan = scan.cspline_baseline(npiece=2,clipthresh=3.0,clipniter=4)
[2081]3959
3960 Note:
3961 The best-fit parameter values output in logger and/or blfile are now
3962 based on specunit of 'channel'.
[2012]3963 """
3964
[2186]3965 try:
3966 varlist = vars()
3967
3968 if insitu is None: insitu = rcParams['insitu']
3969 if insitu:
3970 workscan = self
3971 else:
3972 workscan = self.copy()
[1855]3973
[2410]3974 if mask is None: mask = []
[2189]3975 if npiece is None: npiece = 2
3976 if clipthresh is None: clipthresh = 3.0
3977 if clipniter is None: clipniter = 0
3978 if plot is None: plot = False
3979 if getresidual is None: getresidual = True
3980 if showprogress is None: showprogress = True
3981 if minnrow is None: minnrow = 1000
3982 if outlog is None: outlog = False
3983 if blfile is None: blfile = ''
[2767]3984 if csvformat is None: csvformat = False
3985 if bltable is None: bltable = ''
[1855]3986
[2767]3987 scsvformat = 'T' if csvformat else 'F'
[2641]3988
[2012]3989 #CURRENTLY, PLOT=true UNAVAILABLE UNTIL cubic spline fitting is implemented as a fitter method.
[2767]3990 workscan._cspline_baseline(mask, npiece,
3991 clipthresh, clipniter,
[2349]3992 getresidual,
3993 pack_progress_params(showprogress,
[2641]3994 minnrow),
[2767]3995 outlog, scsvformat+blfile,
3996 bltable)
[2012]3997 workscan._add_history("cspline_baseline", varlist)
[2767]3998
3999 if bltable == '':
4000 if insitu:
4001 self._assign(workscan)
4002 else:
4003 return workscan
[2012]4004 else:
[2767]4005 if not insitu:
4006 return None
[2012]4007
4008 except RuntimeError, e:
[2186]4009 raise_fitting_failure_exception(e)
[1855]4010
[2186]4011 @asaplog_post_dec
[2771]4012 def auto_cspline_baseline(self, mask=None, npiece=None, insitu=None,
[2349]4013 clipthresh=None, clipniter=None,
4014 edge=None, threshold=None, chan_avg_limit=None,
4015 getresidual=None, plot=None,
4016 showprogress=None, minnrow=None, outlog=None,
[2767]4017 blfile=None, csvformat=None, bltable=None):
[2012]4018 """\
4019 Return a scan which has been baselined (all rows) by cubic spline
4020 function (piecewise cubic polynomial).
4021 Spectral lines are detected first using linefinder and masked out
4022 to avoid them affecting the baseline solution.
4023
4024 Parameters:
[2771]4025 mask: an optional mask retreived from scantable
4026 npiece: Number of pieces. (default is 2)
[2189]4027 insitu: if False a new scantable is returned.
4028 Otherwise, the scaling is done in-situ
4029 The default is taken from .asaprc (False)
4030 clipthresh: Clipping threshold. (default is 3.0, unit: sigma)
[2349]4031 clipniter: maximum number of iteration of 'clipthresh'-sigma
4032 clipping (default is 0)
[2189]4033 edge: an optional number of channel to drop at
4034 the edge of spectrum. If only one value is
4035 specified, the same number will be dropped
4036 from both sides of the spectrum. Default
4037 is to keep all channels. Nested tuples
4038 represent individual edge selection for
4039 different IFs (a number of spectral channels
4040 can be different)
4041 threshold: the threshold used by line finder. It is
4042 better to keep it large as only strong lines
4043 affect the baseline solution.
4044 chan_avg_limit: a maximum number of consequtive spectral
4045 channels to average during the search of
4046 weak and broad lines. The default is no
4047 averaging (and no search for weak lines).
4048 If such lines can affect the fitted baseline
4049 (e.g. a high order polynomial is fitted),
4050 increase this parameter (usually values up
4051 to 8 are reasonable). Most users of this
4052 method should find the default value sufficient.
4053 plot: *** CURRENTLY UNAVAILABLE, ALWAYS FALSE ***
4054 plot the fit and the residual. In this each
4055 indivual fit has to be approved, by typing 'y'
4056 or 'n'
4057 getresidual: if False, returns best-fit values instead of
4058 residual. (default is True)
4059 showprogress: show progress status for large data.
4060 default is True.
4061 minnrow: minimum number of input spectra to show.
4062 default is 1000.
4063 outlog: Output the coefficients of the best-fit
4064 function to logger (default is False)
4065 blfile: Name of a text file in which the best-fit
4066 parameter values to be written
4067 (default is "": no file/logger output)
[2641]4068 csvformat: if True blfile is csv-formatted, default is False.
[2767]4069 bltable: name of a baseline table where fitting results
4070 (coefficients, rms, etc.) are to be written.
4071 if given, fitting results will NOT be output to
4072 scantable (insitu=True) or None will be
4073 returned (insitu=False).
4074 (default is "": no table output)
[1846]4075
[1907]4076 Example:
[2012]4077 bscan = scan.auto_cspline_baseline(npiece=3, insitu=False)
[2081]4078
4079 Note:
4080 The best-fit parameter values output in logger and/or blfile are now
4081 based on specunit of 'channel'.
[2012]4082 """
[1846]4083
[2186]4084 try:
4085 varlist = vars()
[2012]4086
[2186]4087 if insitu is None: insitu = rcParams['insitu']
4088 if insitu:
4089 workscan = self
[1391]4090 else:
[2186]4091 workscan = self.copy()
4092
[2410]4093 #if mask is None: mask = [True for i in xrange(workscan.nchan())]
4094 if mask is None: mask = []
[2186]4095 if npiece is None: npiece = 2
4096 if clipthresh is None: clipthresh = 3.0
4097 if clipniter is None: clipniter = 0
4098 if edge is None: edge = (0, 0)
4099 if threshold is None: threshold = 3
4100 if chan_avg_limit is None: chan_avg_limit = 1
4101 if plot is None: plot = False
4102 if getresidual is None: getresidual = True
[2189]4103 if showprogress is None: showprogress = True
4104 if minnrow is None: minnrow = 1000
[2186]4105 if outlog is None: outlog = False
4106 if blfile is None: blfile = ''
[2641]4107 if csvformat is None: csvformat = False
[2767]4108 if bltable is None: bltable = ''
[1819]4109
[2767]4110 scsvformat = 'T' if csvformat else 'F'
[2641]4111
[2277]4112 #CURRENTLY, PLOT=true UNAVAILABLE UNTIL cubic spline fitting is implemented as a fitter method.
[2767]4113 workscan._auto_cspline_baseline(mask, npiece,
4114 clipthresh, clipniter,
[2269]4115 normalise_edge_param(edge),
4116 threshold,
4117 chan_avg_limit, getresidual,
4118 pack_progress_params(showprogress,
4119 minnrow),
[2767]4120 outlog,
4121 scsvformat+blfile,
4122 bltable)
[2012]4123 workscan._add_history("auto_cspline_baseline", varlist)
[2767]4124
4125 if bltable == '':
4126 if insitu:
4127 self._assign(workscan)
4128 else:
4129 return workscan
[1856]4130 else:
[2767]4131 if not insitu:
4132 return None
[2012]4133
4134 except RuntimeError, e:
[2186]4135 raise_fitting_failure_exception(e)
[513]4136
[1931]4137 @asaplog_post_dec
[2771]4138 def chebyshev_baseline(self, mask=None, order=None, insitu=None,
[2645]4139 clipthresh=None, clipniter=None, plot=None,
4140 getresidual=None, showprogress=None, minnrow=None,
[2767]4141 outlog=None, blfile=None, csvformat=None,
4142 bltable=None):
[2645]4143 """\
4144 Return a scan which has been baselined (all rows) by Chebyshev polynomials.
4145
4146 Parameters:
[2771]4147 mask: An optional mask
4148 order: the maximum order of Chebyshev polynomial (default is 5)
[2645]4149 insitu: If False a new scantable is returned.
4150 Otherwise, the scaling is done in-situ
4151 The default is taken from .asaprc (False)
4152 clipthresh: Clipping threshold. (default is 3.0, unit: sigma)
4153 clipniter: maximum number of iteration of 'clipthresh'-sigma
4154 clipping (default is 0)
4155 plot: *** CURRENTLY UNAVAILABLE, ALWAYS FALSE ***
4156 plot the fit and the residual. In this each
4157 indivual fit has to be approved, by typing 'y'
4158 or 'n'
4159 getresidual: if False, returns best-fit values instead of
4160 residual. (default is True)
4161 showprogress: show progress status for large data.
4162 default is True.
4163 minnrow: minimum number of input spectra to show.
4164 default is 1000.
4165 outlog: Output the coefficients of the best-fit
4166 function to logger (default is False)
4167 blfile: Name of a text file in which the best-fit
4168 parameter values to be written
4169 (default is "": no file/logger output)
4170 csvformat: if True blfile is csv-formatted, default is False.
[2767]4171 bltable: name of a baseline table where fitting results
4172 (coefficients, rms, etc.) are to be written.
4173 if given, fitting results will NOT be output to
4174 scantable (insitu=True) or None will be
4175 returned (insitu=False).
4176 (default is "": no table output)
[2645]4177
4178 Example:
4179 # return a scan baselined by a cubic spline consisting of 2 pieces
4180 # (i.e., 1 internal knot),
4181 # also with 3-sigma clipping, iteration up to 4 times
4182 bscan = scan.cspline_baseline(npiece=2,clipthresh=3.0,clipniter=4)
4183
4184 Note:
4185 The best-fit parameter values output in logger and/or blfile are now
4186 based on specunit of 'channel'.
4187 """
4188
4189 try:
4190 varlist = vars()
4191
4192 if insitu is None: insitu = rcParams['insitu']
4193 if insitu:
4194 workscan = self
4195 else:
4196 workscan = self.copy()
4197
4198 if mask is None: mask = []
4199 if order is None: order = 5
4200 if clipthresh is None: clipthresh = 3.0
4201 if clipniter is None: clipniter = 0
4202 if plot is None: plot = False
4203 if getresidual is None: getresidual = True
4204 if showprogress is None: showprogress = True
4205 if minnrow is None: minnrow = 1000
4206 if outlog is None: outlog = False
4207 if blfile is None: blfile = ''
[2767]4208 if csvformat is None: csvformat = False
4209 if bltable is None: bltable = ''
[2645]4210
[2767]4211 scsvformat = 'T' if csvformat else 'F'
[2645]4212
4213 #CURRENTLY, PLOT=true UNAVAILABLE UNTIL cubic spline fitting is implemented as a fitter method.
[2767]4214 workscan._chebyshev_baseline(mask, order,
4215 clipthresh, clipniter,
[2645]4216 getresidual,
4217 pack_progress_params(showprogress,
4218 minnrow),
[2767]4219 outlog, scsvformat+blfile,
4220 bltable)
[2645]4221 workscan._add_history("chebyshev_baseline", varlist)
[2767]4222
4223 if bltable == '':
4224 if insitu:
4225 self._assign(workscan)
4226 else:
4227 return workscan
[2645]4228 else:
[2767]4229 if not insitu:
4230 return None
[2645]4231
4232 except RuntimeError, e:
4233 raise_fitting_failure_exception(e)
4234
4235 @asaplog_post_dec
[2771]4236 def auto_chebyshev_baseline(self, mask=None, order=None, insitu=None,
[2645]4237 clipthresh=None, clipniter=None,
4238 edge=None, threshold=None, chan_avg_limit=None,
4239 getresidual=None, plot=None,
4240 showprogress=None, minnrow=None, outlog=None,
[2767]4241 blfile=None, csvformat=None, bltable=None):
[2645]4242 """\
4243 Return a scan which has been baselined (all rows) by Chebyshev polynomials.
4244 Spectral lines are detected first using linefinder and masked out
4245 to avoid them affecting the baseline solution.
4246
4247 Parameters:
[2771]4248 mask: an optional mask retreived from scantable
4249 order: the maximum order of Chebyshev polynomial (default is 5)
[2645]4250 insitu: if False a new scantable is returned.
4251 Otherwise, the scaling is done in-situ
4252 The default is taken from .asaprc (False)
4253 clipthresh: Clipping threshold. (default is 3.0, unit: sigma)
4254 clipniter: maximum number of iteration of 'clipthresh'-sigma
4255 clipping (default is 0)
4256 edge: an optional number of channel to drop at
4257 the edge of spectrum. If only one value is
4258 specified, the same number will be dropped
4259 from both sides of the spectrum. Default
4260 is to keep all channels. Nested tuples
4261 represent individual edge selection for
4262 different IFs (a number of spectral channels
4263 can be different)
4264 threshold: the threshold used by line finder. It is
4265 better to keep it large as only strong lines
4266 affect the baseline solution.
4267 chan_avg_limit: a maximum number of consequtive spectral
4268 channels to average during the search of
4269 weak and broad lines. The default is no
4270 averaging (and no search for weak lines).
4271 If such lines can affect the fitted baseline
4272 (e.g. a high order polynomial is fitted),
4273 increase this parameter (usually values up
4274 to 8 are reasonable). Most users of this
4275 method should find the default value sufficient.
4276 plot: *** CURRENTLY UNAVAILABLE, ALWAYS FALSE ***
4277 plot the fit and the residual. In this each
4278 indivual fit has to be approved, by typing 'y'
4279 or 'n'
4280 getresidual: if False, returns best-fit values instead of
4281 residual. (default is True)
4282 showprogress: show progress status for large data.
4283 default is True.
4284 minnrow: minimum number of input spectra to show.
4285 default is 1000.
4286 outlog: Output the coefficients of the best-fit
4287 function to logger (default is False)
4288 blfile: Name of a text file in which the best-fit
4289 parameter values to be written
4290 (default is "": no file/logger output)
4291 csvformat: if True blfile is csv-formatted, default is False.
[2767]4292 bltable: name of a baseline table where fitting results
4293 (coefficients, rms, etc.) are to be written.
4294 if given, fitting results will NOT be output to
4295 scantable (insitu=True) or None will be
4296 returned (insitu=False).
4297 (default is "": no table output)
[2645]4298
4299 Example:
4300 bscan = scan.auto_cspline_baseline(npiece=3, insitu=False)
4301
4302 Note:
4303 The best-fit parameter values output in logger and/or blfile are now
4304 based on specunit of 'channel'.
4305 """
4306
4307 try:
4308 varlist = vars()
4309
4310 if insitu is None: insitu = rcParams['insitu']
4311 if insitu:
4312 workscan = self
4313 else:
4314 workscan = self.copy()
4315
4316 if mask is None: mask = []
4317 if order is None: order = 5
4318 if clipthresh is None: clipthresh = 3.0
4319 if clipniter is None: clipniter = 0
4320 if edge is None: edge = (0, 0)
4321 if threshold is None: threshold = 3
4322 if chan_avg_limit is None: chan_avg_limit = 1
4323 if plot is None: plot = False
4324 if getresidual is None: getresidual = True
4325 if showprogress is None: showprogress = True
4326 if minnrow is None: minnrow = 1000
4327 if outlog is None: outlog = False
4328 if blfile is None: blfile = ''
4329 if csvformat is None: csvformat = False
[2767]4330 if bltable is None: bltable = ''
[2645]4331
[2767]4332 scsvformat = 'T' if csvformat else 'F'
[2645]4333
4334 #CURRENTLY, PLOT=true UNAVAILABLE UNTIL cubic spline fitting is implemented as a fitter method.
[2767]4335 workscan._auto_chebyshev_baseline(mask, order,
4336 clipthresh, clipniter,
[2645]4337 normalise_edge_param(edge),
4338 threshold,
4339 chan_avg_limit, getresidual,
4340 pack_progress_params(showprogress,
4341 minnrow),
[2767]4342 outlog, scsvformat+blfile,
4343 bltable)
[2645]4344 workscan._add_history("auto_chebyshev_baseline", varlist)
[2767]4345
4346 if bltable == '':
4347 if insitu:
4348 self._assign(workscan)
4349 else:
4350 return workscan
[2645]4351 else:
[2767]4352 if not insitu:
4353 return None
[2645]4354
4355 except RuntimeError, e:
4356 raise_fitting_failure_exception(e)
4357
4358 @asaplog_post_dec
[2771]4359 def poly_baseline(self, mask=None, order=None, insitu=None,
[2767]4360 clipthresh=None, clipniter=None, plot=None,
[2269]4361 getresidual=None, showprogress=None, minnrow=None,
[2767]4362 outlog=None, blfile=None, csvformat=None,
4363 bltable=None):
[1907]4364 """\
4365 Return a scan which has been baselined (all rows) by a polynomial.
4366 Parameters:
[2771]4367 mask: an optional mask
4368 order: the order of the polynomial (default is 0)
[2189]4369 insitu: if False a new scantable is returned.
4370 Otherwise, the scaling is done in-situ
4371 The default is taken from .asaprc (False)
[2767]4372 clipthresh: Clipping threshold. (default is 3.0, unit: sigma)
4373 clipniter: maximum number of iteration of 'clipthresh'-sigma
4374 clipping (default is 0)
[2189]4375 plot: plot the fit and the residual. In this each
4376 indivual fit has to be approved, by typing 'y'
4377 or 'n'
4378 getresidual: if False, returns best-fit values instead of
4379 residual. (default is True)
4380 showprogress: show progress status for large data.
4381 default is True.
4382 minnrow: minimum number of input spectra to show.
4383 default is 1000.
4384 outlog: Output the coefficients of the best-fit
4385 function to logger (default is False)
4386 blfile: Name of a text file in which the best-fit
4387 parameter values to be written
4388 (default is "": no file/logger output)
[2641]4389 csvformat: if True blfile is csv-formatted, default is False.
[2767]4390 bltable: name of a baseline table where fitting results
4391 (coefficients, rms, etc.) are to be written.
4392 if given, fitting results will NOT be output to
4393 scantable (insitu=True) or None will be
4394 returned (insitu=False).
4395 (default is "": no table output)
[2012]4396
[1907]4397 Example:
4398 # return a scan baselined by a third order polynomial,
4399 # not using a mask
4400 bscan = scan.poly_baseline(order=3)
4401 """
[1931]4402
[2186]4403 try:
4404 varlist = vars()
[1931]4405
[2269]4406 if insitu is None:
4407 insitu = rcParams["insitu"]
[2186]4408 if insitu:
4409 workscan = self
4410 else:
4411 workscan = self.copy()
[1907]4412
[2410]4413 if mask is None: mask = []
[2189]4414 if order is None: order = 0
[2767]4415 if clipthresh is None: clipthresh = 3.0
4416 if clipniter is None: clipniter = 0
[2189]4417 if plot is None: plot = False
4418 if getresidual is None: getresidual = True
4419 if showprogress is None: showprogress = True
4420 if minnrow is None: minnrow = 1000
4421 if outlog is None: outlog = False
[2767]4422 if blfile is None: blfile = ''
[2641]4423 if csvformat is None: csvformat = False
[2767]4424 if bltable is None: bltable = ''
[1907]4425
[2767]4426 scsvformat = 'T' if csvformat else 'F'
[2641]4427
[2012]4428 if plot:
[2269]4429 outblfile = (blfile != "") and \
[2349]4430 os.path.exists(os.path.expanduser(
4431 os.path.expandvars(blfile))
4432 )
[2269]4433 if outblfile:
4434 blf = open(blfile, "a")
[2012]4435
[1907]4436 f = fitter()
4437 f.set_function(lpoly=order)
[2186]4438
4439 rows = xrange(workscan.nrow())
4440 #if len(rows) > 0: workscan._init_blinfo()
[2610]4441
4442 action = "H"
[1907]4443 for r in rows:
4444 f.x = workscan._getabcissa(r)
4445 f.y = workscan._getspectrum(r)
[2541]4446 if mask:
4447 f.mask = mask_and(mask, workscan._getmask(r)) # (CAS-1434)
4448 else: # mask=None
4449 f.mask = workscan._getmask(r)
4450
[1907]4451 f.data = None
4452 f.fit()
[2541]4453
[2610]4454 if action != "Y": # skip plotting when accepting all
4455 f.plot(residual=True)
4456 #accept_fit = raw_input("Accept fit ( [y]/n ): ")
4457 #if accept_fit.upper() == "N":
4458 # #workscan._append_blinfo(None, None, None)
4459 # continue
4460 accept_fit = self._get_verify_action("Accept fit?",action)
4461 if r == 0: action = None
[1907]4462 if accept_fit.upper() == "N":
4463 continue
[2610]4464 elif accept_fit.upper() == "R":
4465 break
4466 elif accept_fit.upper() == "A":
4467 action = "Y"
[2012]4468
4469 blpars = f.get_parameters()
4470 masklist = workscan.get_masklist(f.mask, row=r, silent=True)
4471 #workscan._append_blinfo(blpars, masklist, f.mask)
[2269]4472 workscan._setspectrum((f.fitter.getresidual()
4473 if getresidual else f.fitter.getfit()), r)
[1907]4474
[2012]4475 if outblfile:
4476 rms = workscan.get_rms(f.mask, r)
[2269]4477 dataout = \
4478 workscan.format_blparams_row(blpars["params"],
4479 blpars["fixed"],
4480 rms, str(masklist),
[2641]4481 r, True, csvformat)
[2012]4482 blf.write(dataout)
4483
[1907]4484 f._p.unmap()
4485 f._p = None
[2012]4486
[2349]4487 if outblfile:
4488 blf.close()
[1907]4489 else:
[2767]4490 workscan._poly_baseline(mask, order,
4491 clipthresh, clipniter, #
4492 getresidual,
[2269]4493 pack_progress_params(showprogress,
4494 minnrow),
[2767]4495 outlog, scsvformat+blfile,
4496 bltable) #
[1907]4497
4498 workscan._add_history("poly_baseline", varlist)
4499
4500 if insitu:
4501 self._assign(workscan)
4502 else:
4503 return workscan
4504
[1919]4505 except RuntimeError, e:
[2186]4506 raise_fitting_failure_exception(e)
[1907]4507
[2186]4508 @asaplog_post_dec
[2771]4509 def auto_poly_baseline(self, mask=None, order=None, insitu=None,
[2767]4510 clipthresh=None, clipniter=None,
4511 edge=None, threshold=None, chan_avg_limit=None,
4512 getresidual=None, plot=None,
4513 showprogress=None, minnrow=None, outlog=None,
4514 blfile=None, csvformat=None, bltable=None):
[1846]4515 """\
[1931]4516 Return a scan which has been baselined (all rows) by a polynomial.
[880]4517 Spectral lines are detected first using linefinder and masked out
4518 to avoid them affecting the baseline solution.
4519
4520 Parameters:
[2771]4521 mask: an optional mask retreived from scantable
4522 order: the order of the polynomial (default is 0)
[2189]4523 insitu: if False a new scantable is returned.
4524 Otherwise, the scaling is done in-situ
4525 The default is taken from .asaprc (False)
[2767]4526 clipthresh: Clipping threshold. (default is 3.0, unit: sigma)
4527 clipniter: maximum number of iteration of 'clipthresh'-sigma
4528 clipping (default is 0)
[2189]4529 edge: an optional number of channel to drop at
4530 the edge of spectrum. If only one value is
4531 specified, the same number will be dropped
4532 from both sides of the spectrum. Default
4533 is to keep all channels. Nested tuples
4534 represent individual edge selection for
4535 different IFs (a number of spectral channels
4536 can be different)
4537 threshold: the threshold used by line finder. It is
4538 better to keep it large as only strong lines
4539 affect the baseline solution.
4540 chan_avg_limit: a maximum number of consequtive spectral
4541 channels to average during the search of
4542 weak and broad lines. The default is no
4543 averaging (and no search for weak lines).
4544 If such lines can affect the fitted baseline
4545 (e.g. a high order polynomial is fitted),
4546 increase this parameter (usually values up
4547 to 8 are reasonable). Most users of this
4548 method should find the default value sufficient.
4549 plot: plot the fit and the residual. In this each
4550 indivual fit has to be approved, by typing 'y'
4551 or 'n'
4552 getresidual: if False, returns best-fit values instead of
4553 residual. (default is True)
4554 showprogress: show progress status for large data.
4555 default is True.
4556 minnrow: minimum number of input spectra to show.
4557 default is 1000.
4558 outlog: Output the coefficients of the best-fit
4559 function to logger (default is False)
4560 blfile: Name of a text file in which the best-fit
4561 parameter values to be written
4562 (default is "": no file/logger output)
[2641]4563 csvformat: if True blfile is csv-formatted, default is False.
[2767]4564 bltable: name of a baseline table where fitting results
4565 (coefficients, rms, etc.) are to be written.
4566 if given, fitting results will NOT be output to
4567 scantable (insitu=True) or None will be
4568 returned (insitu=False).
4569 (default is "": no table output)
[1846]4570
[2012]4571 Example:
4572 bscan = scan.auto_poly_baseline(order=7, insitu=False)
4573 """
[880]4574
[2186]4575 try:
4576 varlist = vars()
[1846]4577
[2269]4578 if insitu is None:
4579 insitu = rcParams['insitu']
[2186]4580 if insitu:
4581 workscan = self
4582 else:
4583 workscan = self.copy()
[1846]4584
[2410]4585 if mask is None: mask = []
[2186]4586 if order is None: order = 0
[2767]4587 if clipthresh is None: clipthresh = 3.0
4588 if clipniter is None: clipniter = 0
[2186]4589 if edge is None: edge = (0, 0)
4590 if threshold is None: threshold = 3
4591 if chan_avg_limit is None: chan_avg_limit = 1
4592 if plot is None: plot = False
4593 if getresidual is None: getresidual = True
[2189]4594 if showprogress is None: showprogress = True
4595 if minnrow is None: minnrow = 1000
[2186]4596 if outlog is None: outlog = False
4597 if blfile is None: blfile = ''
[2641]4598 if csvformat is None: csvformat = False
[2767]4599 if bltable is None: bltable = ''
[1846]4600
[2767]4601 scsvformat = 'T' if csvformat else 'F'
[2641]4602
[2186]4603 edge = normalise_edge_param(edge)
[880]4604
[2012]4605 if plot:
[2269]4606 outblfile = (blfile != "") and \
4607 os.path.exists(os.path.expanduser(os.path.expandvars(blfile)))
[2012]4608 if outblfile: blf = open(blfile, "a")
4609
[2186]4610 from asap.asaplinefind import linefinder
[2012]4611 fl = linefinder()
[2269]4612 fl.set_options(threshold=threshold, avg_limit=chan_avg_limit)
[2012]4613 fl.set_scan(workscan)
[2186]4614
[2012]4615 f = fitter()
4616 f.set_function(lpoly=order)
[880]4617
[2186]4618 rows = xrange(workscan.nrow())
4619 #if len(rows) > 0: workscan._init_blinfo()
[2610]4620
4621 action = "H"
[2012]4622 for r in rows:
[2186]4623 idx = 2*workscan.getif(r)
[2541]4624 if mask:
4625 msk = mask_and(mask, workscan._getmask(r)) # (CAS-1434)
4626 else: # mask=None
4627 msk = workscan._getmask(r)
4628 fl.find_lines(r, msk, edge[idx:idx+2])
[907]4629
[2012]4630 f.x = workscan._getabcissa(r)
4631 f.y = workscan._getspectrum(r)
4632 f.mask = fl.get_mask()
4633 f.data = None
4634 f.fit()
4635
[2610]4636 if action != "Y": # skip plotting when accepting all
4637 f.plot(residual=True)
4638 #accept_fit = raw_input("Accept fit ( [y]/n ): ")
4639 accept_fit = self._get_verify_action("Accept fit?",action)
4640 if r == 0: action = None
[2012]4641 if accept_fit.upper() == "N":
4642 #workscan._append_blinfo(None, None, None)
4643 continue
[2610]4644 elif accept_fit.upper() == "R":
4645 break
4646 elif accept_fit.upper() == "A":
4647 action = "Y"
[2012]4648
4649 blpars = f.get_parameters()
4650 masklist = workscan.get_masklist(f.mask, row=r, silent=True)
4651 #workscan._append_blinfo(blpars, masklist, f.mask)
[2349]4652 workscan._setspectrum(
4653 (f.fitter.getresidual() if getresidual
4654 else f.fitter.getfit()), r
4655 )
[2012]4656
4657 if outblfile:
4658 rms = workscan.get_rms(f.mask, r)
[2269]4659 dataout = \
4660 workscan.format_blparams_row(blpars["params"],
4661 blpars["fixed"],
4662 rms, str(masklist),
[2641]4663 r, True, csvformat)
[2012]4664 blf.write(dataout)
4665
4666 f._p.unmap()
4667 f._p = None
4668
4669 if outblfile: blf.close()
4670 else:
[2767]4671 workscan._auto_poly_baseline(mask, order,
4672 clipthresh, clipniter,
4673 edge, threshold,
[2269]4674 chan_avg_limit, getresidual,
4675 pack_progress_params(showprogress,
4676 minnrow),
[2767]4677 outlog, scsvformat+blfile,
4678 bltable)
4679 workscan._add_history("auto_poly_baseline", varlist)
[2012]4680
[2767]4681 if bltable == '':
4682 if insitu:
4683 self._assign(workscan)
4684 else:
4685 return workscan
[2012]4686 else:
[2767]4687 if not insitu:
4688 return None
[2012]4689
4690 except RuntimeError, e:
[2186]4691 raise_fitting_failure_exception(e)
[2012]4692
4693 def _init_blinfo(self):
4694 """\
4695 Initialise the following three auxiliary members:
4696 blpars : parameters of the best-fit baseline,
4697 masklists : mask data (edge positions of masked channels) and
4698 actualmask : mask data (in boolean list),
4699 to keep for use later (including output to logger/text files).
4700 Used by poly_baseline() and auto_poly_baseline() in case of
4701 'plot=True'.
4702 """
4703 self.blpars = []
4704 self.masklists = []
4705 self.actualmask = []
4706 return
[880]4707
[2012]4708 def _append_blinfo(self, data_blpars, data_masklists, data_actualmask):
4709 """\
4710 Append baseline-fitting related info to blpars, masklist and
4711 actualmask.
4712 """
4713 self.blpars.append(data_blpars)
4714 self.masklists.append(data_masklists)
4715 self.actualmask.append(data_actualmask)
4716 return
4717
[1862]4718 @asaplog_post_dec
[914]4719 def rotate_linpolphase(self, angle):
[1846]4720 """\
[914]4721 Rotate the phase of the complex polarization O=Q+iU correlation.
4722 This is always done in situ in the raw data. So if you call this
4723 function more than once then each call rotates the phase further.
[1846]4724
[914]4725 Parameters:
[1846]4726
[914]4727 angle: The angle (degrees) to rotate (add) by.
[1846]4728
4729 Example::
4730
[914]4731 scan.rotate_linpolphase(2.3)
[1846]4732
[914]4733 """
4734 varlist = vars()
[936]4735 self._math._rotate_linpolphase(self, angle)
[914]4736 self._add_history("rotate_linpolphase", varlist)
4737 return
[710]4738
[1862]4739 @asaplog_post_dec
[914]4740 def rotate_xyphase(self, angle):
[1846]4741 """\
[914]4742 Rotate the phase of the XY correlation. This is always done in situ
4743 in the data. So if you call this function more than once
4744 then each call rotates the phase further.
[1846]4745
[914]4746 Parameters:
[1846]4747
[914]4748 angle: The angle (degrees) to rotate (add) by.
[1846]4749
4750 Example::
4751
[914]4752 scan.rotate_xyphase(2.3)
[1846]4753
[914]4754 """
4755 varlist = vars()
[936]4756 self._math._rotate_xyphase(self, angle)
[914]4757 self._add_history("rotate_xyphase", varlist)
4758 return
4759
[1862]4760 @asaplog_post_dec
[914]4761 def swap_linears(self):
[1846]4762 """\
[1573]4763 Swap the linear polarisations XX and YY, or better the first two
[1348]4764 polarisations as this also works for ciculars.
[914]4765 """
4766 varlist = vars()
[936]4767 self._math._swap_linears(self)
[914]4768 self._add_history("swap_linears", varlist)
4769 return
4770
[1862]4771 @asaplog_post_dec
[914]4772 def invert_phase(self):
[1846]4773 """\
[914]4774 Invert the phase of the complex polarisation
4775 """
4776 varlist = vars()
[936]4777 self._math._invert_phase(self)
[914]4778 self._add_history("invert_phase", varlist)
4779 return
4780
[1862]4781 @asaplog_post_dec
[876]4782 def add(self, offset, insitu=None):
[1846]4783 """\
[513]4784 Return a scan where all spectra have the offset added
[1846]4785
[513]4786 Parameters:
[1846]4787
[513]4788 offset: the offset
[1855]4789
[513]4790 insitu: if False a new scantable is returned.
4791 Otherwise, the scaling is done in-situ
4792 The default is taken from .asaprc (False)
[1846]4793
[513]4794 """
4795 if insitu is None: insitu = rcParams['insitu']
[876]4796 self._math._setinsitu(insitu)
[513]4797 varlist = vars()
[2954]4798 s = scantable(self._math._unaryop(self, offset, "ADD", False, False))
[1118]4799 s._add_history("add", varlist)
[876]4800 if insitu:
4801 self._assign(s)
4802 else:
[513]4803 return s
4804
[1862]4805 @asaplog_post_dec
[2966]4806 def scale(self, factor, tsys=True, insitu=None):
[1846]4807 """\
4808
[1938]4809 Return a scan where all spectra are scaled by the given 'factor'
[1846]4810
[513]4811 Parameters:
[1846]4812
[1819]4813 factor: the scaling factor (float or 1D float list)
[1855]4814
[513]4815 insitu: if False a new scantable is returned.
4816 Otherwise, the scaling is done in-situ
4817 The default is taken from .asaprc (False)
[1855]4818
[513]4819 tsys: if True (default) then apply the operation to Tsys
4820 as well as the data
4821 """
4822 if insitu is None: insitu = rcParams['insitu']
[876]4823 self._math._setinsitu(insitu)
[513]4824 varlist = vars()
[1819]4825 s = None
4826 import numpy
4827 if isinstance(factor, list) or isinstance(factor, numpy.ndarray):
[2320]4828 if isinstance(factor[0], list) or isinstance(factor[0],
4829 numpy.ndarray):
[1819]4830 from asapmath import _array2dOp
[2966]4831 s = _array2dOp( self, factor, "MUL", tsys, insitu, True )
[1819]4832 else:
[2320]4833 s = scantable( self._math._arrayop( self, factor,
[2966]4834 "MUL", tsys, True ) )
[1819]4835 else:
[2966]4836 s = scantable(self._math._unaryop(self, factor, "MUL", tsys, True ))
[1118]4837 s._add_history("scale", varlist)
[876]4838 if insitu:
4839 self._assign(s)
4840 else:
[513]4841 return s
4842
[2349]4843 @preserve_selection
4844 def set_sourcetype(self, match, matchtype="pattern",
[1504]4845 sourcetype="reference"):
[1846]4846 """\
[1502]4847 Set the type of the source to be an source or reference scan
[1846]4848 using the provided pattern.
4849
[1502]4850 Parameters:
[1846]4851
[1504]4852 match: a Unix style pattern, regular expression or selector
[1855]4853
[1504]4854 matchtype: 'pattern' (default) UNIX style pattern or
4855 'regex' regular expression
[1855]4856
[1502]4857 sourcetype: the type of the source to use (source/reference)
[1846]4858
[1502]4859 """
4860 varlist = vars()
4861 stype = -1
[2480]4862 if sourcetype.lower().startswith("r") or sourcetype.lower() == "off":
[1502]4863 stype = 1
[2480]4864 elif sourcetype.lower().startswith("s") or sourcetype.lower() == "on":
[1502]4865 stype = 0
[1504]4866 else:
[2480]4867 raise ValueError("Illegal sourcetype use s(ource)/on or r(eference)/off")
[1504]4868 if matchtype.lower().startswith("p"):
4869 matchtype = "pattern"
4870 elif matchtype.lower().startswith("r"):
4871 matchtype = "regex"
4872 else:
4873 raise ValueError("Illegal matchtype, use p(attern) or r(egex)")
[1502]4874 sel = selector()
4875 if isinstance(match, selector):
4876 sel = match
4877 else:
[2480]4878 sel.set_query("SRCNAME=%s('%s')" % (matchtype, match))
4879 self.set_selection(sel)
[1502]4880 self._setsourcetype(stype)
[1573]4881 self._add_history("set_sourcetype", varlist)
[1502]4882
[2818]4883
4884 def set_sourcename(self, name):
4885 varlist = vars()
4886 self._setsourcename(name)
4887 self._add_history("set_sourcename", varlist)
4888
[1862]4889 @asaplog_post_dec
[1857]4890 @preserve_selection
[1819]4891 def auto_quotient(self, preserve=True, mode='paired', verify=False):
[1846]4892 """\
[670]4893 This function allows to build quotients automatically.
[1819]4894 It assumes the observation to have the same number of
[670]4895 "ons" and "offs"
[1846]4896
[670]4897 Parameters:
[1846]4898
[710]4899 preserve: you can preserve (default) the continuum or
4900 remove it. The equations used are
[1857]4901
[670]4902 preserve: Output = Toff * (on/off) - Toff
[1857]4903
[1070]4904 remove: Output = Toff * (on/off) - Ton
[1855]4905
[1573]4906 mode: the on/off detection mode
[1348]4907 'paired' (default)
4908 identifies 'off' scans by the
4909 trailing '_R' (Mopra/Parkes) or
4910 '_e'/'_w' (Tid) and matches
4911 on/off pairs from the observing pattern
[1502]4912 'time'
4913 finds the closest off in time
[1348]4914
[1857]4915 .. todo:: verify argument is not implemented
4916
[670]4917 """
[1857]4918 varlist = vars()
[1348]4919 modes = ["time", "paired"]
[670]4920 if not mode in modes:
[876]4921 msg = "please provide valid mode. Valid modes are %s" % (modes)
4922 raise ValueError(msg)
[1348]4923 s = None
4924 if mode.lower() == "paired":
[2840]4925 from asap._asap import srctype
[1857]4926 sel = self.get_selection()
[2840]4927 #sel.set_query("SRCTYPE==psoff")
4928 sel.set_types(srctype.psoff)
[1356]4929 self.set_selection(sel)
[1348]4930 offs = self.copy()
[2840]4931 #sel.set_query("SRCTYPE==pson")
4932 sel.set_types(srctype.pson)
[1356]4933 self.set_selection(sel)
[1348]4934 ons = self.copy()
4935 s = scantable(self._math._quotient(ons, offs, preserve))
4936 elif mode.lower() == "time":
4937 s = scantable(self._math._auto_quotient(self, mode, preserve))
[1118]4938 s._add_history("auto_quotient", varlist)
[876]4939 return s
[710]4940
[1862]4941 @asaplog_post_dec
[1145]4942 def mx_quotient(self, mask = None, weight='median', preserve=True):
[1846]4943 """\
[1143]4944 Form a quotient using "off" beams when observing in "MX" mode.
[1846]4945
[1143]4946 Parameters:
[1846]4947
[1145]4948 mask: an optional mask to be used when weight == 'stddev'
[1855]4949
[1143]4950 weight: How to average the off beams. Default is 'median'.
[1855]4951
[1145]4952 preserve: you can preserve (default) the continuum or
[1855]4953 remove it. The equations used are:
[1846]4954
[1855]4955 preserve: Output = Toff * (on/off) - Toff
4956
4957 remove: Output = Toff * (on/off) - Ton
4958
[1217]4959 """
[1593]4960 mask = mask or ()
[1141]4961 varlist = vars()
4962 on = scantable(self._math._mx_extract(self, 'on'))
[1143]4963 preoff = scantable(self._math._mx_extract(self, 'off'))
4964 off = preoff.average_time(mask=mask, weight=weight, scanav=False)
[1217]4965 from asapmath import quotient
[1145]4966 q = quotient(on, off, preserve)
[1143]4967 q._add_history("mx_quotient", varlist)
[1217]4968 return q
[513]4969
[1862]4970 @asaplog_post_dec
[718]4971 def freq_switch(self, insitu=None):
[1846]4972 """\
[718]4973 Apply frequency switching to the data.
[1846]4974
[718]4975 Parameters:
[1846]4976
[718]4977 insitu: if False a new scantable is returned.
4978 Otherwise, the swictching is done in-situ
4979 The default is taken from .asaprc (False)
[1846]4980
[718]4981 """
4982 if insitu is None: insitu = rcParams['insitu']
[876]4983 self._math._setinsitu(insitu)
[718]4984 varlist = vars()
[876]4985 s = scantable(self._math._freqswitch(self))
[1118]4986 s._add_history("freq_switch", varlist)
[1856]4987 if insitu:
4988 self._assign(s)
4989 else:
4990 return s
[718]4991
[1862]4992 @asaplog_post_dec
[780]4993 def recalc_azel(self):
[1846]4994 """Recalculate the azimuth and elevation for each position."""
[780]4995 varlist = vars()
[876]4996 self._recalcazel()
[780]4997 self._add_history("recalc_azel", varlist)
4998 return
4999
[1862]5000 @asaplog_post_dec
[513]5001 def __add__(self, other):
[2574]5002 """
5003 implicit on all axes and on Tsys
5004 """
[513]5005 varlist = vars()
[2574]5006 s = self.__op( other, "ADD" )
[513]5007 s._add_history("operator +", varlist)
5008 return s
5009
[1862]5010 @asaplog_post_dec
[513]5011 def __sub__(self, other):
5012 """
5013 implicit on all axes and on Tsys
5014 """
5015 varlist = vars()
[2574]5016 s = self.__op( other, "SUB" )
[513]5017 s._add_history("operator -", varlist)
5018 return s
[710]5019
[1862]5020 @asaplog_post_dec
[513]5021 def __mul__(self, other):
5022 """
5023 implicit on all axes and on Tsys
5024 """
5025 varlist = vars()
[2574]5026 s = self.__op( other, "MUL" ) ;
[513]5027 s._add_history("operator *", varlist)
5028 return s
5029
[710]5030
[1862]5031 @asaplog_post_dec
[513]5032 def __div__(self, other):
5033 """
5034 implicit on all axes and on Tsys
5035 """
5036 varlist = vars()
[2574]5037 s = self.__op( other, "DIV" )
5038 s._add_history("operator /", varlist)
5039 return s
5040
5041 @asaplog_post_dec
5042 def __op( self, other, mode ):
[513]5043 s = None
5044 if isinstance(other, scantable):
[2574]5045 s = scantable(self._math._binaryop(self, other, mode))
[513]5046 elif isinstance(other, float):
5047 if other == 0.0:
[718]5048 raise ZeroDivisionError("Dividing by zero is not recommended")
[2954]5049 s = scantable(self._math._unaryop(self, other, mode, False, False))
[2144]5050 elif isinstance(other, list) or isinstance(other, numpy.ndarray):
[2349]5051 if isinstance(other[0], list) \
5052 or isinstance(other[0], numpy.ndarray):
[2144]5053 from asapmath import _array2dOp
[2574]5054 s = _array2dOp( self, other, mode, False )
[2144]5055 else:
[2574]5056 s = scantable( self._math._arrayop( self, other,
[2954]5057 mode, False, False ) )
[513]5058 else:
[718]5059 raise TypeError("Other input is not a scantable or float value")
[513]5060 return s
5061
[1862]5062 @asaplog_post_dec
[530]5063 def get_fit(self, row=0):
[1846]5064 """\
[530]5065 Print or return the stored fits for a row in the scantable
[1846]5066
[530]5067 Parameters:
[1846]5068
[530]5069 row: the row which the fit has been applied to.
[1846]5070
[530]5071 """
5072 if row > self.nrow():
5073 return
[976]5074 from asap.asapfit import asapfit
[530]5075 fit = asapfit(self._getfit(row))
[1859]5076 asaplog.push( '%s' %(fit) )
5077 return fit.as_dict()
[530]5078
[2349]5079 @preserve_selection
[1483]5080 def flag_nans(self):
[1846]5081 """\
[1483]5082 Utility function to flag NaN values in the scantable.
5083 """
5084 import numpy
5085 basesel = self.get_selection()
5086 for i in range(self.nrow()):
[1589]5087 sel = self.get_row_selector(i)
5088 self.set_selection(basesel+sel)
[1483]5089 nans = numpy.isnan(self._getspectrum(0))
[2877]5090 if numpy.any(nans):
5091 bnans = [ bool(v) for v in nans]
5092 self.flag(bnans)
5093
5094 self.set_selection(basesel)
[1483]5095
[1588]5096 def get_row_selector(self, rowno):
[1992]5097 return selector(rows=[rowno])
[1573]5098
[484]5099 def _add_history(self, funcname, parameters):
[1435]5100 if not rcParams['scantable.history']:
5101 return
[484]5102 # create date
5103 sep = "##"
5104 from datetime import datetime
5105 dstr = datetime.now().strftime('%Y/%m/%d %H:%M:%S')
5106 hist = dstr+sep
5107 hist += funcname+sep#cdate+sep
[2349]5108 if parameters.has_key('self'):
5109 del parameters['self']
[1118]5110 for k, v in parameters.iteritems():
[484]5111 if type(v) is dict:
[1118]5112 for k2, v2 in v.iteritems():
[484]5113 hist += k2
5114 hist += "="
[1118]5115 if isinstance(v2, scantable):
[484]5116 hist += 'scantable'
5117 elif k2 == 'mask':
[1118]5118 if isinstance(v2, list) or isinstance(v2, tuple):
[513]5119 hist += str(self._zip_mask(v2))
5120 else:
5121 hist += str(v2)
[484]5122 else:
[513]5123 hist += str(v2)
[484]5124 else:
5125 hist += k
5126 hist += "="
[1118]5127 if isinstance(v, scantable):
[484]5128 hist += 'scantable'
5129 elif k == 'mask':
[1118]5130 if isinstance(v, list) or isinstance(v, tuple):
[513]5131 hist += str(self._zip_mask(v))
5132 else:
5133 hist += str(v)
[484]5134 else:
5135 hist += str(v)
5136 hist += sep
5137 hist = hist[:-2] # remove trailing '##'
5138 self._addhistory(hist)
5139
[710]5140
[484]5141 def _zip_mask(self, mask):
5142 mask = list(mask)
5143 i = 0
5144 segments = []
5145 while mask[i:].count(1):
5146 i += mask[i:].index(1)
5147 if mask[i:].count(0):
5148 j = i + mask[i:].index(0)
5149 else:
[710]5150 j = len(mask)
[1118]5151 segments.append([i, j])
[710]5152 i = j
[484]5153 return segments
[714]5154
[626]5155 def _get_ordinate_label(self):
5156 fu = "("+self.get_fluxunit()+")"
5157 import re
5158 lbl = "Intensity"
[1118]5159 if re.match(".K.", fu):
[626]5160 lbl = "Brightness Temperature "+ fu
[1118]5161 elif re.match(".Jy.", fu):
[626]5162 lbl = "Flux density "+ fu
5163 return lbl
[710]5164
[876]5165 def _check_ifs(self):
[2349]5166# return len(set([self.nchan(i) for i in self.getifnos()])) == 1
[1986]5167 nchans = [self.nchan(i) for i in self.getifnos()]
[2004]5168 nchans = filter(lambda t: t > 0, nchans)
[876]5169 return (sum(nchans)/len(nchans) == nchans[0])
[976]5170
[1862]5171 @asaplog_post_dec
[1916]5172 def _fill(self, names, unit, average, opts={}):
[976]5173 first = True
5174 fullnames = []
5175 for name in names:
5176 name = os.path.expandvars(name)
5177 name = os.path.expanduser(name)
5178 if not os.path.exists(name):
5179 msg = "File '%s' does not exists" % (name)
5180 raise IOError(msg)
5181 fullnames.append(name)
5182 if average:
5183 asaplog.push('Auto averaging integrations')
[1079]5184 stype = int(rcParams['scantable.storage'].lower() == 'disk')
[976]5185 for name in fullnames:
[1073]5186 tbl = Scantable(stype)
[2004]5187 if is_ms( name ):
5188 r = msfiller( tbl )
5189 else:
5190 r = filler( tbl )
[976]5191 msg = "Importing %s..." % (name)
[1118]5192 asaplog.push(msg, False)
[2349]5193 r.open(name, opts)
[2480]5194 rx = rcParams['scantable.reference']
5195 r.setreferenceexpr(rx)
[1843]5196 r.fill()
[976]5197 if average:
[1118]5198 tbl = self._math._average((tbl, ), (), 'NONE', 'SCAN')
[976]5199 if not first:
[2902]5200 tbl = self._math._merge([self, tbl])
[976]5201 Scantable.__init__(self, tbl)
[1843]5202 r.close()
[1118]5203 del r, tbl
[976]5204 first = False
[1861]5205 #flush log
5206 asaplog.post()
[976]5207 if unit is not None:
5208 self.set_fluxunit(unit)
[1824]5209 if not is_casapy():
5210 self.set_freqframe(rcParams['scantable.freqframe'])
[976]5211
[2610]5212 def _get_verify_action( self, msg, action=None ):
5213 valid_act = ['Y', 'N', 'A', 'R']
5214 if not action or not isinstance(action, str):
5215 action = raw_input("%s [Y/n/a/r] (h for help): " % msg)
5216 if action == '':
5217 return "Y"
5218 elif (action.upper()[0] in valid_act):
5219 return action.upper()[0]
5220 elif (action.upper()[0] in ['H','?']):
5221 print "Available actions of verification [Y|n|a|r]"
5222 print " Y : Yes for current data (default)"
5223 print " N : No for current data"
5224 print " A : Accept all in the following and exit from verification"
5225 print " R : Reject all in the following and exit from verification"
5226 print " H or ?: help (show this message)"
5227 return self._get_verify_action(msg)
5228 else:
5229 return 'Y'
[2012]5230
[1402]5231 def __getitem__(self, key):
5232 if key < 0:
5233 key += self.nrow()
5234 if key >= self.nrow():
5235 raise IndexError("Row index out of range.")
5236 return self._getspectrum(key)
5237
5238 def __setitem__(self, key, value):
5239 if key < 0:
5240 key += self.nrow()
5241 if key >= self.nrow():
5242 raise IndexError("Row index out of range.")
5243 if not hasattr(value, "__len__") or \
5244 len(value) > self.nchan(self.getif(key)):
5245 raise ValueError("Spectrum length doesn't match.")
5246 return self._setspectrum(value, key)
5247
5248 def __len__(self):
5249 return self.nrow()
5250
5251 def __iter__(self):
5252 for i in range(len(self)):
5253 yield self[i]
Note: See TracBrowser for help on using the repository browser.