Changes in trunk/python/scantable.py [1846:1948]
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/python/scantable.py
r1846 r1948 2 2 3 3 import os 4 import numpy 4 5 try: 5 6 from functools import wraps as wraps_dec … … 11 12 from asap._asap import filler 12 13 from asap.parameters import rcParams 13 from asap.logging import asaplog, print_log, print_log_dec14 from asap.logging import asaplog, asaplog_post_dec 14 15 from asap.selector import selector 15 16 from asap.linecatalog import linecatalog 16 17 from asap.coordinate import coordinate 17 from asap.utils import _n_bools, mask_not, mask_and, mask_or 18 from asap.utils import _n_bools, mask_not, mask_and, mask_or, page 19 from asap.asapfitter import fitter 18 20 19 21 … … 22 24 def wrap(obj, *args, **kw): 23 25 basesel = obj.get_selection() 24 val = func(obj, *args, **kw) 25 obj.set_selection(basesel) 26 try: 27 val = func(obj, *args, **kw) 28 finally: 29 obj.set_selection(basesel) 26 30 return val 27 31 return wrap … … 35 39 36 40 """ 37 return (os.path.isdir(filename) 38 and not os.path.exists(filename+'/table.f1') 39 and os.path.exists(filename+'/table.info')) 40 41 41 if ( os.path.isdir(filename) 42 and os.path.exists(filename+'/table.info') 43 and os.path.exists(filename+'/table.dat') ): 44 f=open(filename+'/table.info') 45 l=f.readline() 46 f.close() 47 #if ( l.find('Scantable') != -1 ): 48 if ( l.find('Measurement Set') == -1 ): 49 return True 50 else: 51 return False 52 else: 53 return False 54 ## return (os.path.isdir(filename) 55 ## and not os.path.exists(filename+'/table.f1') 56 ## and os.path.exists(filename+'/table.info')) 57 58 def is_ms(filename): 59 """Is the given file a MeasurementSet? 60 61 Parameters: 62 63 filename: the name of the file/directory to test 64 65 """ 66 if ( os.path.isdir(filename) 67 and os.path.exists(filename+'/table.info') 68 and os.path.exists(filename+'/table.dat') ): 69 f=open(filename+'/table.info') 70 l=f.readline() 71 f.close() 72 if ( l.find('Measurement Set') != -1 ): 73 return True 74 else: 75 return False 76 else: 77 return False 78 42 79 class scantable(Scantable): 43 80 """\ … … 45 82 """ 46 83 47 @print_log_dec 48 def __init__(self, filename, average=None, unit=None, getpt=None, 49 antenna=None, parallactify=None): 84 @asaplog_post_dec 85 #def __init__(self, filename, average=None, unit=None, getpt=None, 86 # antenna=None, parallactify=None): 87 def __init__(self, filename, average=None, unit=None, parallactify=None, **args): 50 88 """\ 51 89 Create a scantable from a saved one or make a reference … … 73 111 the MS data faster in some cases. 74 112 75 antenna: Antenna selection. integer (id) or string (name or id). 113 antenna: for MeasurementSet input data only: 114 Antenna selection. integer (id) or string (name or id). 76 115 77 116 parallactify: Indicate that the data had been parallatified. Default … … 81 120 if average is None: 82 121 average = rcParams['scantable.autoaverage'] 83 if getpt is None: 84 getpt = True 85 if antenna is not None: 86 asaplog.push("Antenna selection currently unsupported." 87 "Using '0'") 88 print_log('WARN') 89 if antenna is None: 90 antenna = '' 91 elif type(antenna) == int: 92 antenna = '%s' % antenna 93 elif type(antenna) == list: 94 tmpstr = '' 95 for i in range( len(antenna) ): 96 if type(antenna[i]) == int: 97 tmpstr = tmpstr + ('%s,'%(antenna[i])) 98 elif type(antenna[i]) == str: 99 tmpstr=tmpstr+antenna[i]+',' 100 else: 101 asaplog.push('Bad antenna selection.') 102 print_log('ERROR') 103 return 104 antenna = tmpstr.rstrip(',') 122 #if getpt is None: 123 # getpt = True 124 #if antenna is not None: 125 # asaplog.push("Antenna selection currently unsupported." 126 # "Using ''") 127 # asaplog.post('WARN') 128 #if antenna is None: 129 # antenna = '' 130 #elif type(antenna) == int: 131 # antenna = '%s' % antenna 132 #elif type(antenna) == list: 133 # tmpstr = '' 134 # for i in range( len(antenna) ): 135 # if type(antenna[i]) == int: 136 # tmpstr = tmpstr + ('%s,'%(antenna[i])) 137 # elif type(antenna[i]) == str: 138 # tmpstr=tmpstr+antenna[i]+',' 139 # else: 140 # raise TypeError('Bad antenna selection.') 141 # antenna = tmpstr.rstrip(',') 105 142 parallactify = parallactify or rcParams['scantable.parallactify'] 106 143 varlist = vars() … … 115 152 if not os.path.exists(filename): 116 153 s = "File '%s' not found." % (filename) 117 if rcParams['verbose']:118 asaplog.push(s)119 print_log('ERROR')120 return121 154 raise IOError(s) 122 155 if is_scantable(filename): … … 127 160 # do not reset to the default freqframe 128 161 #self.set_freqframe(rcParams['scantable.freqframe']) 129 elif os.path.isdir(filename) \ 130 and not os.path.exists(filename+'/table.f1'): 162 #elif os.path.isdir(filename) \ 163 # and not os.path.exists(filename+'/table.f1'): 164 elif is_ms(filename): 165 # Measurement Set 166 opts={'ms': {}} 167 mskeys=['getpt','antenna'] 168 for key in mskeys: 169 if key in args.keys(): 170 opts['ms'][key] = args[key] 171 #self._fill([filename], unit, average, getpt, antenna) 172 self._fill([filename], unit, average, opts) 173 elif os.path.isfile(filename): 174 #self._fill([filename], unit, average, getpt, antenna) 175 self._fill([filename], unit, average) 176 else: 131 177 msg = "The given file '%s'is not a valid " \ 132 178 "asap table." % (filename) 133 if rcParams['verbose']: 134 #print msg 135 asaplog.push( msg ) 136 print_log( 'ERROR' ) 137 return 138 else: 139 raise IOError(msg) 140 else: 141 self._fill([filename], unit, average, getpt, antenna) 179 raise IOError(msg) 142 180 elif (isinstance(filename, list) or isinstance(filename, tuple)) \ 143 181 and isinstance(filename[-1], str): 144 self._fill(filename, unit, average, getpt, antenna) 182 #self._fill(filename, unit, average, getpt, antenna) 183 self._fill(filename, unit, average) 145 184 self.parallactify(parallactify) 146 185 self._add_history("scantable", varlist) 147 print_log() 148 149 @print_log_dec 186 187 @asaplog_post_dec 150 188 def save(self, name=None, format=None, overwrite=False): 151 189 """\ … … 158 196 this is the root file name (data in 'name'.txt 159 197 and header in 'name'_header.txt) 198 160 199 format: an optional file format. Default is ASAP. 161 Allowed are - 'ASAP' (save as ASAP [aips++] Table), 162 'SDFITS' (save as SDFITS file) 163 'ASCII' (saves as ascii text file) 164 'MS2' (saves as an aips++ 165 MeasurementSet V2) 166 'FITS' (save as image FITS - not 167 readable by class) 168 'CLASS' (save as FITS readable by CLASS) 200 Allowed are: 201 202 * 'ASAP' (save as ASAP [aips++] Table), 203 * 'SDFITS' (save as SDFITS file) 204 * 'ASCII' (saves as ascii text file) 205 * 'MS2' (saves as an casacore MeasurementSet V2) 206 * 'FITS' (save as image FITS - not readable by class) 207 * 'CLASS' (save as FITS readable by CLASS) 208 169 209 overwrite: If the file should be overwritten if it exists. 170 210 The default False is to return with warning 171 211 without writing the output. USE WITH CARE. 212 172 213 Example:: 173 214 … … 187 228 if not overwrite: 188 229 msg = "File %s exists." % name 189 if rcParams['verbose']: 190 #print msg 191 asaplog.push( msg ) 192 print_log( 'ERROR' ) 193 return 194 else: 195 raise IOError(msg) 230 raise IOError(msg) 196 231 format2 = format.upper() 197 232 if format2 == 'ASAP': … … 201 236 writer = stw(format2) 202 237 writer.write(self, name) 203 print_log()204 238 return 205 239 … … 233 267 from asap import unique 234 268 if not _is_valid(scanid): 235 if rcParams['verbose']: 236 #print "Please specify a scanno to drop from the scantable" 237 asaplog.push( 'Please specify a scanno to drop from the scantable' ) 238 print_log( 'ERROR' ) 239 return 240 else: 241 raise RuntimeError("No scan given") 242 try: 243 scanid = _to_list(scanid) 244 allscans = unique([ self.getscan(i) for i in range(self.nrow())]) 245 for sid in scanid: allscans.remove(sid) 246 if len(allscans) == 0: 247 raise ValueError("Can't remove all scans") 248 except ValueError: 249 if rcParams['verbose']: 250 #print "Couldn't find any match." 251 print_log() 252 asaplog.push( "Couldn't find any match." ) 253 print_log( 'ERROR' ) 254 return 255 else: raise 256 try: 257 sel = selector(scans=allscans) 258 return self._select_copy(sel) 259 except RuntimeError: 260 if rcParams['verbose']: 261 #print "Couldn't find any match." 262 print_log() 263 asaplog.push( "Couldn't find any match." ) 264 print_log( 'ERROR' ) 265 else: 266 raise 269 raise RuntimeError( 'Please specify a scanno to drop from the scantable' ) 270 scanid = _to_list(scanid) 271 allscans = unique([ self.getscan(i) for i in range(self.nrow())]) 272 for sid in scanid: allscans.remove(sid) 273 if len(allscans) == 0: 274 raise ValueError("Can't remove all scans") 275 sel = selector(scans=allscans) 276 return self._select_copy(sel) 267 277 268 278 def _select_copy(self, selection): … … 274 284 275 285 def get_scan(self, scanid=None): 276 """ 286 """\ 277 287 Return a specific scan (by scanno) or collection of scans (by 278 288 source name) in a new scantable. … … 299 309 """ 300 310 if scanid is None: 301 if rcParams['verbose']: 302 #print "Please specify a scan no or name to " \ 303 # "retrieve from the scantable" 304 asaplog.push( 'Please specify a scan no or name to retrieve' 305 ' from the scantable' ) 306 print_log( 'ERROR' ) 307 return 308 else: 309 raise RuntimeError("No scan given") 310 311 raise RuntimeError( 'Please specify a scan no or name to ' 312 'retrieve from the scantable' ) 311 313 try: 312 314 bsel = self.get_selection() … … 323 325 else: 324 326 msg = "Illegal scanid type, use 'int' or 'list' if ints." 325 if rcParams['verbose']: 326 #print msg 327 asaplog.push( msg ) 328 print_log( 'ERROR' ) 329 else: 330 raise TypeError(msg) 327 raise TypeError(msg) 331 328 except RuntimeError: 332 if rcParams['verbose']: 333 #print "Couldn't find any match." 334 print_log() 335 asaplog.push( "Couldn't find any match." ) 336 print_log( 'ERROR' ) 337 else: raise 329 raise 338 330 339 331 def __str__(self): … … 362 354 else: 363 355 msg = "Illegal file name '%s'." % (filename) 364 if rcParams['verbose']: 365 #print msg 366 asaplog.push( msg ) 367 print_log( 'ERROR' ) 368 else: 369 raise IOError(msg) 370 if rcParams['verbose']: 371 try: 372 from IPython.genutils import page as pager 373 except ImportError: 374 from pydoc import pager 375 pager(info) 376 else: 377 return info 356 raise IOError(msg) 357 return page(info) 378 358 379 359 def get_spectrum(self, rowno): … … 398 378 399 379 def set_spectrum(self, spec, rowno): 400 """Return the spectrum for the current row in the scantable as a list. 401 402 Parameters: 403 404 spec: the spectrum 405 rowno: the row number to set the spectrum for 380 """Set the spectrum for the current row in the scantable. 381 382 Parameters: 383 384 spec: the new spectrum 385 386 rowno: the row number to set the spectrum for 406 387 407 388 """ … … 517 498 return workscan 518 499 519 #def stats(self, stat='stddev', mask=None):520 def stats(self, stat='stddev', mask=None, form='3.3f' ):500 @asaplog_post_dec 501 def stats(self, stat='stddev', mask=None, form='3.3f', row=None): 521 502 """\ 522 503 Determine the specified statistic of the current beam/if/pol … … 528 509 stat: 'min', 'max', 'min_abc', 'max_abc', 'sumsq', 'sum', 529 510 'mean', 'var', 'stddev', 'avdev', 'rms', 'median' 511 530 512 mask: an optional mask specifying where the statistic 531 513 should be determined. 514 532 515 form: format string to print statistic values 533 516 534 Example:: 535 517 row: row number of spectrum to process. 518 (default is None: for all rows) 519 520 Example: 536 521 scan.set_unit('channel') 537 522 msk = scan.create_mask([100, 200], [500, 600]) … … 551 536 getchan = True 552 537 statvals = [] 553 if not rtnabc: statvals = self._math._stats(self, mask, stat) 538 if not rtnabc: 539 if row == None: 540 statvals = self._math._stats(self, mask, stat) 541 else: 542 statvals = self._math._statsrow(self, mask, stat, int(row)) 554 543 555 544 #def cb(i): … … 563 552 #outvec = [] 564 553 sep = '-'*50 565 for i in range(self.nrow()): 554 555 if row == None: 556 rows = xrange(self.nrow()) 557 elif isinstance(row, int): 558 rows = [ row ] 559 560 for i in rows: 566 561 refstr = '' 567 562 statunit= '' … … 579 574 out += 'Scan[%d] (%s) ' % (self.getscan(i), src) 580 575 out += 'Time[%s]:\n' % (tm) 581 if self.nbeam(-1) > 1: 582 out += ' Beam[%d] ' % (self.getbeam(i)) 583 if self.nif(-1) > 1: out += ' IF[%d] ' % (self.getif(i)) 584 if self.npol(-1) > 1: out += ' Pol[%d] ' % (self.getpol(i)) 576 if self.nbeam(-1) > 1: out += ' Beam[%d] ' % (self.getbeam(i)) 577 if self.nif(-1) > 1: out += ' IF[%d] ' % (self.getif(i)) 578 if self.npol(-1) > 1: out += ' Pol[%d] ' % (self.getpol(i)) 585 579 #outvec.append(callback(i)) 586 #out += ('= %'+form) % (outvec[i]) +' '+refstr+'\n' 587 out += ('= %'+form) % (statvals[i]) +' '+refstr+'\n' 580 if len(rows) > 1: 581 # out += ('= %'+form) % (outvec[i]) +' '+refstr+'\n' 582 out += ('= %'+form) % (statvals[i]) +' '+refstr+'\n' 583 else: 584 # out += ('= %'+form) % (outvec[0]) +' '+refstr+'\n' 585 out += ('= %'+form) % (statvals[0]) +' '+refstr+'\n' 588 586 out += sep+"\n" 589 587 590 if rcParams['verbose']: 591 import os 592 if os.environ.has_key( 'USER' ): 593 usr=os.environ['USER'] 594 else: 595 import commands 596 usr=commands.getoutput( 'whoami' ) 597 tmpfile='/tmp/tmp_'+usr+'_casapy_asap_scantable_stats' 598 f=open(tmpfile,'w') 599 print >> f, sep 600 print >> f, ' %s %s' % (label, statunit) 601 print >> f, sep 602 print >> f, out 603 f.close() 604 f=open(tmpfile,'r') 605 x=f.readlines() 606 f.close() 607 blanc='' 608 asaplog.push(blanc.join(x), False) 609 #for xx in x: 610 # asaplog.push( xx, False ) 611 print_log() 588 import os 589 if os.environ.has_key( 'USER' ): 590 usr = os.environ['USER'] 591 else: 592 import commands 593 usr = commands.getoutput( 'whoami' ) 594 tmpfile = '/tmp/tmp_'+usr+'_casapy_asap_scantable_stats' 595 f = open(tmpfile,'w') 596 print >> f, sep 597 print >> f, ' %s %s' % (label, statunit) 598 print >> f, sep 599 print >> f, out 600 f.close() 601 f = open(tmpfile,'r') 602 x = f.readlines() 603 f.close() 604 asaplog.push(''.join(x), False) 605 612 606 return statvals 613 607 … … 621 615 rowno: a row number in the scantable. Default is the 622 616 first row, i.e. rowno=0 617 623 618 chan: a channel in the scantable. Default is the first 624 619 channel, i.e. pos=0 … … 723 718 out += '= %3.3f\n' % (outvec[i]) 724 719 out += sep+'\n' 725 if rcParams['verbose']: 726 727 728 729 730 print_log()720 721 asaplog.push(sep) 722 asaplog.push(" %s" % (label)) 723 asaplog.push(sep) 724 asaplog.push(out) 725 asaplog.post() 731 726 return outvec 732 727 733 def _get_column(self, callback, row=-1 ):728 def _get_column(self, callback, row=-1, *args): 734 729 """ 735 730 """ 736 731 if row == -1: 737 return [callback(i ) for i in range(self.nrow())]732 return [callback(i, *args) for i in range(self.nrow())] 738 733 else: 739 734 if 0 <= row < self.nrow(): 740 return callback(row )741 742 743 def get_time(self, row=-1, asdatetime=False ):735 return callback(row, *args) 736 737 738 def get_time(self, row=-1, asdatetime=False, prec=-1): 744 739 """\ 745 740 Get a list of time stamps for the observations. 746 Return a datetime object for each integration time stamp in the scantable. 741 Return a datetime object or a string (default) for each 742 integration time stamp in the scantable. 747 743 748 744 Parameters: 749 745 750 746 row: row no of integration. Default -1 return all rows 747 751 748 asdatetime: return values as datetime objects rather than strings 752 749 753 """ 754 from time import strptime 750 prec: number of digits shown. Default -1 to automatic calculation. 751 Note this number is equals to the digits of MVTime, 752 i.e., 0<prec<3: dates with hh:: only, 753 <5: with hh:mm:, <7 or 0: with hh:mm:ss, 754 and 6> : with hh:mm:ss.tt... (prec-6 t's added) 755 756 """ 755 757 from datetime import datetime 756 times = self._get_column(self._gettime, row) 758 if prec < 0: 759 # automagically set necessary precision +1 760 prec = 7 - numpy.floor(numpy.log10(min(self.get_inttime()))) 761 prec = max(6, int(prec)) 762 else: 763 prec = max(0, prec) 764 if asdatetime: 765 #precision can be 1 millisecond at max 766 prec = min(12, prec) 767 768 times = self._get_column(self._gettime, row, prec) 757 769 if not asdatetime: 758 770 return times 759 format = "%Y/%m/%d/%H:%M:%S" 771 format = "%Y/%m/%d/%H:%M:%S.%f" 772 if prec < 7: 773 nsub = 1 + (((6-prec)/2) % 3) 774 substr = [".%f","%S","%M"] 775 for i in range(nsub): 776 format = format.replace(substr[i],"") 760 777 if isinstance(times, list): 761 return [datetime (*strptime(i, format)[:6]) for i in times]762 else: 763 return datetime (*strptime(times, format)[:6])778 return [datetime.strptime(i, format) for i in times] 779 else: 780 return datetime.strptime(times, format) 764 781 765 782 … … 827 844 Get a list of Positions on the sky (direction) for the observations. 828 845 Return a string for each integration in the scantable. 829 Parameters: 846 847 Parameters: 848 830 849 row: row no of integration. Default -1 return all rows 831 Example: 832 none 850 833 851 """ 834 852 return self._get_column(self._getdirection, row) … … 846 864 return self._get_column(self._getdirectionvec, row) 847 865 848 @ print_log_dec866 @asaplog_post_dec 849 867 def set_unit(self, unit='channel'): 850 868 """\ … … 865 883 self._add_history("set_unit", varlist) 866 884 867 @ print_log_dec885 @asaplog_post_dec 868 886 def set_instrument(self, instr): 869 887 """\ … … 878 896 self._setInstrument(instr) 879 897 self._add_history("set_instument", vars()) 880 print_log() 881 882 @print_log_dec 898 899 @asaplog_post_dec 883 900 def set_feedtype(self, feedtype): 884 901 """\ … … 892 909 self._setfeedtype(feedtype) 893 910 self._add_history("set_feedtype", vars()) 894 print_log() 895 896 @print_log_dec 911 912 @asaplog_post_dec 897 913 def set_doppler(self, doppler='RADIO'): 898 914 """\ … … 909 925 self._setcoordinfo(inf) 910 926 self._add_history("set_doppler", vars()) 911 print_log() 912 913 @print_log_dec 927 928 @asaplog_post_dec 914 929 def set_freqframe(self, frame=None): 915 930 """\ … … 942 957 else: 943 958 msg = "Please specify a valid freq type. Valid types are:\n", valid 944 if rcParams['verbose']: 945 #print msg 946 asaplog.push( msg ) 947 print_log( 'ERROR' ) 948 else: 949 raise TypeError(msg) 950 print_log() 951 959 raise TypeError(msg) 960 961 @asaplog_post_dec 952 962 def set_dirframe(self, frame=""): 953 963 """\ … … 965 975 """ 966 976 varlist = vars() 967 try: 968 Scantable.set_dirframe(self, frame) 969 except RuntimeError, msg: 970 if rcParams['verbose']: 971 #print msg 972 print_log() 973 asaplog.push( str(msg) ) 974 print_log( 'ERROR' ) 975 else: 976 raise 977 Scantable.set_dirframe(self, frame) 977 978 self._add_history("set_dirframe", varlist) 978 979 … … 991 992 return unit 992 993 994 @asaplog_post_dec 993 995 def get_abcissa(self, rowno=0): 994 996 """\ … … 1008 1010 abc = self._getabcissa(rowno) 1009 1011 lbl = self._getabcissalabel(rowno) 1010 print_log()1011 1012 return abc, lbl 1012 1013 1014 @asaplog_post_dec 1013 1015 def flag(self, mask=None, unflag=False): 1014 1016 """\ … … 1019 1021 mask: an optional channel mask, created with create_mask. Default 1020 1022 (no mask) is all channels. 1023 1021 1024 unflag: if True, unflag the data 1022 1025 … … 1024 1027 varlist = vars() 1025 1028 mask = mask or [] 1026 try: 1027 self._flag(mask, unflag) 1028 except RuntimeError, msg: 1029 if rcParams['verbose']: 1030 #print msg 1031 print_log() 1032 asaplog.push( str(msg) ) 1033 print_log( 'ERROR' ) 1034 return 1035 else: raise 1029 self._flag(mask, unflag) 1036 1030 self._add_history("flag", varlist) 1037 1031 1032 @asaplog_post_dec 1038 1033 def flag_row(self, rows=[], unflag=False): 1039 1034 """\ … … 1044 1039 rows: list of row numbers to be flagged. Default is no row 1045 1040 (must be explicitly specified to execute row-based flagging). 1041 1046 1042 unflag: if True, unflag the data. 1047 1043 1048 1044 """ 1049 1045 varlist = vars() 1050 try: 1051 self._flag_row(rows, unflag) 1052 except RuntimeError, msg: 1053 if rcParams['verbose']: 1054 print_log() 1055 asaplog.push( str(msg) ) 1056 print_log('ERROR') 1057 return 1058 else: raise 1046 self._flag_row(rows, unflag) 1059 1047 self._add_history("flag_row", varlist) 1060 1048 1049 @asaplog_post_dec 1061 1050 def clip(self, uthres=None, dthres=None, clipoutside=True, unflag=False): 1062 1051 """\ … … 1066 1055 1067 1056 uthres: upper threshold. 1057 1068 1058 dthres: lower threshold 1069 1059 1070 1060 clipoutside: True for flagging data outside the range [dthres:uthres]. 1071 False for glagging data inside the range. 1061 False for flagging data inside the range. 1062 1072 1063 unflag: if True, unflag the data. 1073 1064 1074 1065 """ 1075 1066 varlist = vars() 1076 try: 1077 self._clip(uthres, dthres, clipoutside, unflag) 1078 except RuntimeError, msg: 1079 if rcParams['verbose']: 1080 print_log() 1081 asaplog.push(str(msg)) 1082 print_log('ERROR') 1083 return 1084 else: raise 1067 self._clip(uthres, dthres, clipoutside, unflag) 1085 1068 self._add_history("clip", varlist) 1086 1069 1087 @ print_log_dec1070 @asaplog_post_dec 1088 1071 def lag_flag(self, start, end, unit="MHz", insitu=None): 1089 1072 """\ … … 1096 1079 start: the start frequency (really a period within the 1097 1080 bandwidth) or period to remove 1081 1098 1082 end: the end frequency or period to remove 1083 1099 1084 unit: the frequency unit (default "MHz") or "" for 1100 1085 explicit lag channels … … 1112 1097 if not (unit == "" or base.has_key(unit)): 1113 1098 raise ValueError("%s is not a valid unit." % unit) 1114 try: 1115 if unit == "": 1116 s = scantable(self._math._lag_flag(self, start, end, "lags")) 1117 else: 1118 s = scantable(self._math._lag_flag(self, start*base[unit], 1119 end*base[unit], "frequency")) 1120 except RuntimeError, msg: 1121 if rcParams['verbose']: 1122 #print msg 1123 print_log() 1124 asaplog.push( str(msg) ) 1125 print_log( 'ERROR' ) 1126 return 1127 else: raise 1099 if unit == "": 1100 s = scantable(self._math._lag_flag(self, start, end, "lags")) 1101 else: 1102 s = scantable(self._math._lag_flag(self, start*base[unit], 1103 end*base[unit], "frequency")) 1128 1104 s._add_history("lag_flag", varlist) 1129 print_log()1130 1105 if insitu: 1131 1106 self._assign(s) … … 1133 1108 return s 1134 1109 1135 @ print_log_dec1110 @asaplog_post_dec 1136 1111 def create_mask(self, *args, **kwargs): 1137 1112 """\ … … 1145 1120 Pairs of start/end points (inclusive)specifying the regions 1146 1121 to be masked 1122 1147 1123 invert: optional argument. If specified as True, 1148 1124 return an inverted mask, i.e. the regions 1149 1125 specified are EXCLUDED 1126 1150 1127 row: create the mask using the specified row for 1151 1128 unit conversions, default is row=0 … … 1173 1150 data = self._getabcissa(row) 1174 1151 u = self._getcoordinfo()[0] 1175 if rcParams['verbose']:1176 if u == "":u = "channel"1177 1178 1179 1180 1181 1152 if u == "": 1153 u = "channel" 1154 msg = "The current mask window unit is %s" % u 1155 i = self._check_ifs() 1156 if not i: 1157 msg += "\nThis mask is only valid for IF=%d" % (self.getif(i)) 1158 asaplog.push(msg) 1182 1159 n = self.nchan() 1183 1160 msk = _n_bools(n, False) … … 1201 1178 if kwargs.get('invert'): 1202 1179 msk = mask_not(msk) 1203 print_log()1204 1180 return msk 1205 1181 1206 def get_masklist(self, mask=None, row=0 ):1182 def get_masklist(self, mask=None, row=0, silent=False): 1207 1183 """\ 1208 1184 Compute and return a list of mask windows, [min, max]. … … 1211 1187 1212 1188 mask: channel mask, created with create_mask. 1189 1213 1190 row: calcutate the masklist using the specified row 1214 1191 for unit conversions, default is row=0 … … 1231 1208 data = self._getabcissa(row) 1232 1209 u = self._getcoordinfo()[0] 1233 if rcParams['verbose']: 1234 if u == "": u = "channel" 1235 msg = "The current mask window unit is %s" % u 1236 i = self._check_ifs() 1237 if not i: 1238 msg += "\nThis mask is only valid for IF=%d" % (self.getif(i)) 1210 if u == "": 1211 u = "channel" 1212 msg = "The current mask window unit is %s" % u 1213 i = self._check_ifs() 1214 if not i: 1215 msg += "\nThis mask is only valid for IF=%d" % (self.getif(i)) 1216 if not silent: 1239 1217 asaplog.push(msg) 1240 1218 masklist=[] … … 1251 1229 """\ 1252 1230 Compute and Return lists of mask start indices and mask end indices. 1253 Parameters: 1231 1232 Parameters: 1233 1254 1234 mask: channel mask, created with create_mask. 1255 1235 … … 1326 1306 """\ 1327 1307 Set or replace the restfrequency specified and 1328 If the 'freqs' argument holds a scalar,1308 if the 'freqs' argument holds a scalar, 1329 1309 then that rest frequency will be applied to all the selected 1330 1310 data. If the 'freqs' argument holds … … 1342 1322 1343 1323 freqs: list of rest frequency values or string idenitfiers 1324 1344 1325 unit: unit for rest frequency (default 'Hz') 1345 1326 … … 1452 1433 Scantable.shift_refpix(self, delta) 1453 1434 1435 @asaplog_post_dec 1454 1436 def history(self, filename=None): 1455 1437 """\ … … 1458 1440 Parameters: 1459 1441 1460 filename: The name 1442 filename: The name of the file to save the history to. 1461 1443 1462 1444 """ … … 1465 1447 for h in hist: 1466 1448 if h.startswith("---"): 1467 out += "\n"+h1449 out = "\n".join([out, h]) 1468 1450 else: 1469 1451 items = h.split("##") … … 1474 1456 out += "Function: %s\n Parameters:" % (func) 1475 1457 for i in items: 1458 if i == '': 1459 continue 1476 1460 s = i.split("=") 1477 1461 out += "\n %s = %s" % (s[0], s[1]) 1478 out += "\n"+"-"*801462 out = "\n".join([out, "-"*80]) 1479 1463 if filename is not None: 1480 1464 if filename is "": … … 1488 1472 else: 1489 1473 msg = "Illegal file name '%s'." % (filename) 1490 if rcParams['verbose']: 1491 #print msg 1492 asaplog.push( msg ) 1493 print_log( 'ERROR' ) 1494 else: 1495 raise IOError(msg) 1496 if rcParams['verbose']: 1497 try: 1498 from IPython.genutils import page as pager 1499 except ImportError: 1500 from pydoc import pager 1501 pager(out) 1502 else: 1503 return out 1504 return 1474 raise IOError(msg) 1475 return page(out) 1505 1476 # 1506 1477 # Maths business 1507 1478 # 1508 @ print_log_dec1479 @asaplog_post_dec 1509 1480 def average_time(self, mask=None, scanav=False, weight='tint', align=False): 1510 1481 """\ … … 1519 1490 mask: an optional mask (only used for 'var' and 'tsys' 1520 1491 weighting) 1492 1521 1493 scanav: True averages each scan separately 1522 1494 False (default) averages all scans together, 1495 1523 1496 weight: Weighting scheme. 1524 1497 'none' (mean no weight) … … 1529 1502 'median' ( median averaging) 1530 1503 The default is 'tint' 1504 1531 1505 align: align the spectra in velocity before averaging. It takes 1532 1506 the time of the first spectrum as reference time. … … 1543 1517 scanav = (scanav and 'SCAN') or 'NONE' 1544 1518 scan = (self, ) 1545 try: 1546 if align: 1547 scan = (self.freq_align(insitu=False), ) 1548 s = None 1549 if weight.upper() == 'MEDIAN': 1550 s = scantable(self._math._averagechannel(scan[0], 'MEDIAN', 1551 scanav)) 1552 else: 1553 s = scantable(self._math._average(scan, mask, weight.upper(), 1554 scanav)) 1555 except RuntimeError, msg: 1556 if rcParams['verbose']: 1557 #print msg 1558 print_log() 1559 asaplog.push( str(msg) ) 1560 print_log( 'ERROR' ) 1561 return 1562 else: raise 1519 1520 if align: 1521 scan = (self.freq_align(insitu=False), ) 1522 s = None 1523 if weight.upper() == 'MEDIAN': 1524 s = scantable(self._math._averagechannel(scan[0], 'MEDIAN', 1525 scanav)) 1526 else: 1527 s = scantable(self._math._average(scan, mask, weight.upper(), 1528 scanav)) 1563 1529 s._add_history("average_time", varlist) 1564 print_log()1565 1530 return s 1566 1531 1567 @ print_log_dec1532 @asaplog_post_dec 1568 1533 def convert_flux(self, jyperk=None, eta=None, d=None, insitu=None): 1569 1534 """\ … … 1578 1543 1579 1544 jyperk: the Jy / K conversion factor 1545 1580 1546 eta: the aperture efficiency 1581 d: the geomtric diameter (metres) 1547 1548 d: the geometric diameter (metres) 1549 1582 1550 insitu: if False a new scantable is returned. 1583 1551 Otherwise, the scaling is done in-situ … … 1593 1561 s = scantable(self._math._convertflux(self, d, eta, jyperk)) 1594 1562 s._add_history("convert_flux", varlist) 1595 print_log()1596 1563 if insitu: self._assign(s) 1597 1564 else: return s 1598 1565 1599 @ print_log_dec1566 @asaplog_post_dec 1600 1567 def gain_el(self, poly=None, filename="", method="linear", insitu=None): 1601 1568 """\ … … 1614 1581 gain-elevation correction as a function of 1615 1582 elevation (in degrees). 1583 1616 1584 filename: The name of an ascii file holding correction factors. 1617 1585 The first row of the ascii file must give the column … … 1632 1600 0.5 80 0.8 1633 1601 0.6 90 0.75 1602 1634 1603 method: Interpolation method when correcting from a table. 1635 1604 Values are "nearest", "linear" (default), "cubic" 1636 1605 and "spline" 1606 1637 1607 insitu: if False a new scantable is returned. 1638 1608 Otherwise, the scaling is done in-situ … … 1649 1619 s = scantable(self._math._gainel(self, poly, filename, method)) 1650 1620 s._add_history("gain_el", varlist) 1651 print_log()1652 1621 if insitu: 1653 1622 self._assign(s) … … 1655 1624 return s 1656 1625 1657 @ print_log_dec1626 @asaplog_post_dec 1658 1627 def freq_align(self, reftime=None, method='cubic', insitu=None): 1659 1628 """\ … … 1663 1632 1664 1633 Parameters: 1634 1665 1635 reftime: reference time to align at. By default, the time of 1666 1636 the first row of data is used. 1637 1667 1638 method: Interpolation method for regridding the spectra. 1668 1639 Choose from "nearest", "linear", "cubic" (default) 1669 1640 and "spline" 1641 1670 1642 insitu: if False a new scantable is returned. 1671 1643 Otherwise, the scaling is done in-situ … … 1679 1651 s = scantable(self._math._freq_align(self, reftime, method)) 1680 1652 s._add_history("freq_align", varlist) 1681 print_log()1682 1653 if insitu: self._assign(s) 1683 1654 else: return s 1684 1655 1685 @ print_log_dec1656 @asaplog_post_dec 1686 1657 def opacity(self, tau=None, insitu=None): 1687 1658 """\ … … 1690 1661 1691 1662 Parameters: 1663 1692 1664 tau: (list of) opacity from which the correction factor is 1693 1665 exp(tau*ZD) … … 1698 1670 if tau is `None` the opacities are determined from a 1699 1671 model. 1672 1700 1673 insitu: if False a new scantable is returned. 1701 1674 Otherwise, the scaling is done in-situ … … 1710 1683 s = scantable(self._math._opacity(self, tau)) 1711 1684 s._add_history("opacity", varlist) 1712 print_log()1713 1685 if insitu: self._assign(s) 1714 1686 else: return s 1715 1687 1716 @ print_log_dec1688 @asaplog_post_dec 1717 1689 def bin(self, width=5, insitu=None): 1718 1690 """\ … … 1722 1694 1723 1695 width: The bin width (default=5) in pixels 1696 1724 1697 insitu: if False a new scantable is returned. 1725 1698 Otherwise, the scaling is done in-situ … … 1732 1705 s = scantable(self._math._bin(self, width)) 1733 1706 s._add_history("bin", varlist) 1734 print_log()1735 1707 if insitu: 1736 1708 self._assign(s) … … 1738 1710 return s 1739 1711 1740 @ print_log_dec1712 @asaplog_post_dec 1741 1713 def resample(self, width=5, method='cubic', insitu=None): 1742 1714 """\ … … 1746 1718 1747 1719 width: The bin width (default=5) in pixels 1720 1748 1721 method: Interpolation method when correcting from a table. 1749 1722 Values are "nearest", "linear", "cubic" (default) 1750 1723 and "spline" 1724 1751 1725 insitu: if False a new scantable is returned. 1752 1726 Otherwise, the scaling is done in-situ … … 1759 1733 s = scantable(self._math._resample(self, method, width)) 1760 1734 s._add_history("resample", varlist) 1761 print_log()1762 1735 if insitu: self._assign(s) 1763 1736 else: return s 1764 1737 1765 @ print_log_dec1738 @asaplog_post_dec 1766 1739 def average_pol(self, mask=None, weight='none'): 1767 1740 """\ … … 1773 1746 averaging will be applied. The output will have all 1774 1747 specified points masked. 1748 1775 1749 weight: Weighting scheme. 'none' (default), 'var' (1/var(spec) 1776 1750 weighted), or 'tsys' (1/Tsys**2 weighted) … … 1781 1755 s = scantable(self._math._averagepol(self, mask, weight.upper())) 1782 1756 s._add_history("average_pol", varlist) 1783 print_log()1784 1757 return s 1785 1758 1786 @ print_log_dec1759 @asaplog_post_dec 1787 1760 def average_beam(self, mask=None, weight='none'): 1788 1761 """\ … … 1793 1766 averaging will be applied. The output will have all 1794 1767 specified points masked. 1768 1795 1769 weight: Weighting scheme. 'none' (default), 'var' (1/var(spec) 1796 1770 weighted), or 'tsys' (1/Tsys**2 weighted) … … 1801 1775 s = scantable(self._math._averagebeams(self, mask, weight.upper())) 1802 1776 s._add_history("average_beam", varlist) 1803 print_log()1804 1777 return s 1805 1778 … … 1810 1783 1811 1784 Parameters: 1785 1812 1786 pflag: Bool indicating whether to turn this on (True) or 1813 1787 off (False) … … 1818 1792 self._add_history("parallactify", varlist) 1819 1793 1820 @ print_log_dec1794 @asaplog_post_dec 1821 1795 def convert_pol(self, poltype=None): 1822 1796 """\ … … 1825 1799 1826 1800 Parameters: 1801 1827 1802 poltype: The new polarisation type. Valid types are: 1828 1803 "linear", "circular", "stokes" and "linpol" … … 1830 1805 """ 1831 1806 varlist = vars() 1832 try: 1833 s = scantable(self._math._convertpol(self, poltype)) 1834 except RuntimeError, msg: 1835 if rcParams['verbose']: 1836 #print msg 1837 print_log() 1838 asaplog.push( str(msg) ) 1839 print_log( 'ERROR' ) 1840 return 1841 else: 1842 raise 1807 s = scantable(self._math._convertpol(self, poltype)) 1843 1808 s._add_history("convert_pol", varlist) 1844 print_log()1845 1809 return s 1846 1810 1847 @ print_log_dec1811 @asaplog_post_dec 1848 1812 def smooth(self, kernel="hanning", width=5.0, order=2, plot=False, insitu=None): 1849 1813 """\ … … 1855 1819 'hanning' (default), 'gaussian', 'boxcar', 'rmedian' 1856 1820 or 'poly' 1821 1857 1822 width: The width of the kernel in pixels. For hanning this is 1858 1823 ignored otherwise it defauls to 5 pixels. … … 1860 1825 Maximum. For 'boxcar' it is the full width. 1861 1826 For 'rmedian' and 'poly' it is the half width. 1827 1862 1828 order: Optional parameter for 'poly' kernel (default is 2), to 1863 1829 specify the order of the polnomial. Ignored by all other 1864 1830 kernels. 1831 1865 1832 plot: plot the original and the smoothed spectra. 1866 1833 In this each indivual fit has to be approved, by 1867 1834 typing 'y' or 'n' 1835 1868 1836 insitu: if False a new scantable is returned. 1869 1837 Otherwise, the scaling is done in-situ … … 1916 1884 del orgscan 1917 1885 1918 print_log()1919 1886 if insitu: self._assign(s) 1920 1887 else: return s 1921 1888 1922 @print_log_dec 1923 def poly_baseline(self, mask=None, order=0, plot=False, uselin=False, 1924 insitu=None): 1889 @asaplog_post_dec 1890 def old_poly_baseline(self, mask=None, order=0, plot=False, uselin=False, insitu=None, rows=None): 1925 1891 """\ 1926 1892 Return a scan which has been baselined (all rows) by a polynomial. 1927 1893 1928 1894 Parameters: 1929 1895 1930 1896 mask: an optional mask 1897 1931 1898 order: the order of the polynomial (default is 0) 1899 1932 1900 plot: plot the fit and the residual. In this each 1933 1901 indivual fit has to be approved, by typing 'y' 1934 1902 or 'n' 1903 1935 1904 uselin: use linear polynomial fit 1905 1936 1906 insitu: if False a new scantable is returned. 1937 1907 Otherwise, the scaling is done in-situ 1938 1908 The default is taken from .asaprc (False) 1939 1909 1940 Example:: 1941 1910 rows: row numbers of spectra to be processed. 1911 (default is None: for all rows) 1912 1913 Example: 1942 1914 # return a scan baselined by a third order polynomial, 1943 1915 # not using a mask … … 1952 1924 varlist = vars() 1953 1925 if mask is None: 1954 mask = [True for i in xrange(self.nchan(-1))] 1955 1956 from asap.asapfitter import fitter 1926 mask = [True for i in xrange(self.nchan())] 1927 1957 1928 try: 1958 1929 f = fitter() … … 1962 1933 f.set_function(poly=order) 1963 1934 1964 rows = range(workscan.nrow()) 1935 if rows == None: 1936 rows = xrange(workscan.nrow()) 1937 elif isinstance(rows, int): 1938 rows = [ rows ] 1939 1965 1940 if len(rows) > 0: 1966 1941 self.blpars = [] 1967 1942 self.masklists = [] 1943 self.actualmask = [] 1944 1968 1945 for r in rows: 1969 # take into account flagtra info (CAS-1434)1970 flagtra = workscan._getmask(r)1971 actualmask = mask[:]1972 if len(actualmask) == 0:1973 actualmask = list(flagtra[:])1974 else:1975 if len(actualmask) != len(flagtra):1976 raise RuntimeError, "Mask and flagtra have different length"1977 else:1978 for i in range(0, len(actualmask)):1979 actualmask[i] = actualmask[i] and flagtra[i]1980 f.set_scan(workscan, actualmask)1981 1946 f.x = workscan._getabcissa(r) 1982 1947 f.y = workscan._getspectrum(r) 1948 f.mask = mask_and(mask, workscan._getmask(r)) # (CAS-1434) 1983 1949 f.data = None 1984 1950 f.fit() … … 1988 1954 if x.upper() == 'N': 1989 1955 self.blpars.append(None) 1956 self.masklists.append(None) 1957 self.actualmask.append(None) 1990 1958 continue 1991 1959 workscan._setspectrum(f.fitter.getresidual(), r) 1992 1960 self.blpars.append(f.get_parameters()) 1961 self.masklists.append(workscan.get_masklist(f.mask, row=r, silent=True)) 1962 self.actualmask.append(f.mask) 1993 1963 1994 1964 if plot: … … 1996 1966 f._p = None 1997 1967 workscan._add_history("poly_baseline", varlist) 1998 print_log() 1999 if insitu: self._assign(workscan) 2000 else: return workscan 1968 if insitu: 1969 self._assign(workscan) 1970 else: 1971 return workscan 2001 1972 except RuntimeError: 2002 1973 msg = "The fit failed, possibly because it didn't converge." 2003 if rcParams['verbose']: 2004 #print msg 2005 print_log() 2006 asaplog.push( str(msg) ) 2007 print_log( 'ERROR' ) 1974 raise RuntimeError(msg) 1975 1976 @asaplog_post_dec 1977 def poly_baseline(self, mask=None, order=0, plot=False, batch=False, insitu=None, rows=None): 1978 """\ 1979 Return a scan which has been baselined (all rows) by a polynomial. 1980 Parameters: 1981 mask: an optional mask 1982 order: the order of the polynomial (default is 0) 1983 plot: plot the fit and the residual. In this each 1984 indivual fit has to be approved, by typing 'y' 1985 or 'n'. Ignored if batch = True. 1986 batch: if True a faster algorithm is used and logs 1987 including the fit results are not output 1988 (default is False) 1989 insitu: if False a new scantable is returned. 1990 Otherwise, the scaling is done in-situ 1991 The default is taken from .asaprc (False) 1992 rows: row numbers of spectra to be baselined. 1993 (default is None: for all rows) 1994 Example: 1995 # return a scan baselined by a third order polynomial, 1996 # not using a mask 1997 bscan = scan.poly_baseline(order=3) 1998 """ 1999 2000 varlist = vars() 2001 2002 if insitu is None: insitu = rcParams["insitu"] 2003 if insitu: 2004 workscan = self 2005 else: 2006 workscan = self.copy() 2007 2008 nchan = workscan.nchan() 2009 2010 if mask is None: 2011 mask = [True for i in xrange(nchan)] 2012 2013 try: 2014 if rows == None: 2015 rows = xrange(workscan.nrow()) 2016 elif isinstance(rows, int): 2017 rows = [ rows ] 2018 2019 if len(rows) > 0: 2020 workscan.blpars = [] 2021 workscan.masklists = [] 2022 workscan.actualmask = [] 2023 2024 if batch: 2025 workscan._poly_baseline_batch(mask, order) 2026 elif plot: 2027 f = fitter() 2028 f.set_function(lpoly=order) 2029 for r in rows: 2030 f.x = workscan._getabcissa(r) 2031 f.y = workscan._getspectrum(r) 2032 f.mask = mask_and(mask, workscan._getmask(r)) # (CAS-1434) 2033 f.data = None 2034 f.fit() 2035 2036 f.plot(residual=True) 2037 accept_fit = raw_input("Accept fit ( [y]/n ): ") 2038 if accept_fit.upper() == "N": 2039 self.blpars.append(None) 2040 self.masklists.append(None) 2041 self.actualmask.append(None) 2042 continue 2043 workscan._setspectrum(f.fitter.getresidual(), r) 2044 workscan.blpars.append(f.get_parameters()) 2045 workscan.masklists.append(workscan.get_masklist(f.mask, row=r)) 2046 workscan.actualmask.append(f.mask) 2047 2048 f._p.unmap() 2049 f._p = None 2050 else: 2051 for r in rows: 2052 fitparams = workscan._poly_baseline(mask, order, r) 2053 params = fitparams.getparameters() 2054 fmtd = ", ".join(["p%d = %3.6f" % (i, v) for i, v in enumerate(params)]) 2055 errors = fitparams.geterrors() 2056 fmask = mask_and(mask, workscan._getmask(r)) 2057 2058 workscan.blpars.append({"params":params, 2059 "fixed": fitparams.getfixedparameters(), 2060 "formatted":fmtd, "errors":errors}) 2061 workscan.masklists.append(workscan.get_masklist(fmask, r, silent=True)) 2062 workscan.actualmask.append(fmask) 2063 2064 asaplog.push(fmtd) 2065 2066 workscan._add_history("poly_baseline", varlist) 2067 2068 if insitu: 2069 self._assign(workscan) 2070 else: 2071 return workscan 2072 2073 except RuntimeError, e: 2074 msg = "The fit failed, possibly because it didn't converge." 2075 if rcParams["verbose"]: 2076 asaplog.push(str(e)) 2077 asaplog.push(str(msg)) 2008 2078 return 2009 2079 else: 2010 raise RuntimeError( msg)2011 2012 2013 def auto_poly_baseline(self, mask= [], edge=(0, 0), order=0,2080 raise RuntimeError(str(e)+'\n'+msg) 2081 2082 2083 def auto_poly_baseline(self, mask=None, edge=(0, 0), order=0, 2014 2084 threshold=3, chan_avg_limit=1, plot=False, 2015 insitu=None ):2085 insitu=None, rows=None): 2016 2086 """\ 2017 2087 Return a scan which has been baselined (all rows) by a polynomial. … … 2054 2124 Otherwise, the scaling is done in-situ 2055 2125 The default is taken from .asaprc (False) 2126 rows: row numbers of spectra to be processed. 2127 (default is None: for all rows) 2056 2128 2057 2129 … … 2063 2135 if insitu is None: insitu = rcParams['insitu'] 2064 2136 varlist = vars() 2065 from asap.asapfitter import fitter2066 2137 from asap.asaplinefind import linefinder 2067 2138 from asap import _is_sequence_or_number as _is_valid … … 2087 2158 curedge = edge; 2088 2159 2160 if not insitu: 2161 workscan = self.copy() 2162 else: 2163 workscan = self 2164 2089 2165 # setup fitter 2090 2166 f = fitter() 2091 f.set_function( poly=order)2167 f.set_function(lpoly=order) 2092 2168 2093 2169 # setup line finder … … 2095 2171 fl.set_options(threshold=threshold,avg_limit=chan_avg_limit) 2096 2172 2097 if not insitu:2098 workscan = self.copy()2099 else:2100 workscan = self2101 2102 2173 fl.set_scan(workscan) 2103 2174 2104 rows = range(workscan.nrow()) 2175 if mask is None: 2176 mask = _n_bools(workscan.nchan(), True) 2177 2178 if rows is None: 2179 rows = xrange(workscan.nrow()) 2180 elif isinstance(rows, int): 2181 rows = [ rows ] 2182 2105 2183 # Save parameters of baseline fits & masklists as a class attribute. 2106 2184 # NOTICE: It does not reflect changes in scantable! … … 2108 2186 self.blpars=[] 2109 2187 self.masklists=[] 2188 self.actualmask=[] 2110 2189 asaplog.push("Processing:") 2111 2190 for r in rows: … … 2122 2201 curedge = edge[workscan.getif(r)] 2123 2202 2124 # take into account flagtra info (CAS-1434) 2125 flagtra = workscan._getmask(r) 2126 actualmask = mask[:] 2127 if len(actualmask) == 0: 2128 actualmask = list(flagtra[:]) 2129 else: 2130 if len(actualmask) != len(flagtra): 2131 raise RuntimeError, "Mask and flagtra have different length" 2132 else: 2133 for i in range(0, len(actualmask)): 2134 actualmask[i] = actualmask[i] and flagtra[i] 2203 actualmask = mask_and(mask, workscan._getmask(r)) # (CAS-1434) 2135 2204 2136 2205 # setup line finder 2137 2206 fl.find_lines(r, actualmask, curedge) 2138 outmask=fl.get_mask() 2139 f.set_scan(workscan, fl.get_mask()) 2207 2140 2208 f.x = workscan._getabcissa(r) 2141 2209 f.y = workscan._getspectrum(r) 2210 f.mask = fl.get_mask() 2142 2211 f.data = None 2143 2212 f.fit() 2144 2213 2145 2214 # Show mask list 2146 masklist=workscan.get_masklist(f l.get_mask(),row=r)2215 masklist=workscan.get_masklist(f.mask, row=r, silent=True) 2147 2216 msg = "mask range: "+str(masklist) 2148 2217 asaplog.push(msg, False) … … 2154 2223 self.blpars.append(None) 2155 2224 self.masklists.append(None) 2225 self.actualmask.append(None) 2156 2226 continue 2157 2227 … … 2159 2229 self.blpars.append(f.get_parameters()) 2160 2230 self.masklists.append(masklist) 2231 self.actualmask.append(f.mask) 2161 2232 if plot: 2162 2233 f._p.unmap() … … 2168 2239 return workscan 2169 2240 2170 @ print_log_dec2241 @asaplog_post_dec 2171 2242 def rotate_linpolphase(self, angle): 2172 2243 """\ … … 2187 2258 self._math._rotate_linpolphase(self, angle) 2188 2259 self._add_history("rotate_linpolphase", varlist) 2189 print_log()2190 2260 return 2191 2261 2192 @ print_log_dec2262 @asaplog_post_dec 2193 2263 def rotate_xyphase(self, angle): 2194 2264 """\ … … 2209 2279 self._math._rotate_xyphase(self, angle) 2210 2280 self._add_history("rotate_xyphase", varlist) 2211 print_log()2212 2281 return 2213 2282 2214 @ print_log_dec2283 @asaplog_post_dec 2215 2284 def swap_linears(self): 2216 2285 """\ … … 2221 2290 self._math._swap_linears(self) 2222 2291 self._add_history("swap_linears", varlist) 2223 print_log()2224 2292 return 2225 2293 2226 @ print_log_dec2294 @asaplog_post_dec 2227 2295 def invert_phase(self): 2228 2296 """\ … … 2234 2302 return 2235 2303 2236 @ print_log_dec2304 @asaplog_post_dec 2237 2305 def add(self, offset, insitu=None): 2238 2306 """\ … … 2242 2310 2243 2311 offset: the offset 2312 2244 2313 insitu: if False a new scantable is returned. 2245 2314 Otherwise, the scaling is done in-situ … … 2257 2326 return s 2258 2327 2259 @ print_log_dec2328 @asaplog_post_dec 2260 2329 def scale(self, factor, tsys=True, insitu=None): 2261 2330 """\ 2262 2331 2263 Return a scan where all spectra are scaled by the give 'factor'2332 Return a scan where all spectra are scaled by the given 'factor' 2264 2333 2265 2334 Parameters: 2266 2335 2267 2336 factor: the scaling factor (float or 1D float list) 2337 2268 2338 insitu: if False a new scantable is returned. 2269 2339 Otherwise, the scaling is done in-situ 2270 2340 The default is taken from .asaprc (False) 2341 2271 2342 tsys: if True (default) then apply the operation to Tsys 2272 2343 as well as the data … … 2287 2358 s = scantable(self._math._unaryop(self.copy(), factor, "MUL", tsys)) 2288 2359 s._add_history("scale", varlist) 2289 print_log()2290 2360 if insitu: 2291 2361 self._assign(s) … … 2302 2372 2303 2373 match: a Unix style pattern, regular expression or selector 2374 2304 2375 matchtype: 'pattern' (default) UNIX style pattern or 2305 2376 'regex' regular expression 2377 2306 2378 sourcetype: the type of the source to use (source/reference) 2307 2379 … … 2332 2404 self._add_history("set_sourcetype", varlist) 2333 2405 2334 @print_log_dec 2406 @asaplog_post_dec 2407 @preserve_selection 2335 2408 def auto_quotient(self, preserve=True, mode='paired', verify=False): 2336 2409 """\ … … 2343 2416 preserve: you can preserve (default) the continuum or 2344 2417 remove it. The equations used are 2418 2345 2419 preserve: Output = Toff * (on/off) - Toff 2420 2346 2421 remove: Output = Toff * (on/off) - Ton 2422 2347 2423 mode: the on/off detection mode 2348 2424 'paired' (default) … … 2354 2430 finds the closest off in time 2355 2431 2356 """ 2432 .. todo:: verify argument is not implemented 2433 2434 """ 2435 varlist = vars() 2357 2436 modes = ["time", "paired"] 2358 2437 if not mode in modes: 2359 2438 msg = "please provide valid mode. Valid modes are %s" % (modes) 2360 2439 raise ValueError(msg) 2361 varlist = vars()2362 2440 s = None 2363 2441 if mode.lower() == "paired": 2364 basesel = self.get_selection() 2365 sel = selector()+basesel 2366 sel.set_query("SRCTYPE==1") 2442 sel = self.get_selection() 2443 sel.set_query("SRCTYPE==psoff") 2367 2444 self.set_selection(sel) 2368 2445 offs = self.copy() 2369 sel.set_query("SRCTYPE== 0")2446 sel.set_query("SRCTYPE==pson") 2370 2447 self.set_selection(sel) 2371 2448 ons = self.copy() 2372 2449 s = scantable(self._math._quotient(ons, offs, preserve)) 2373 self.set_selection(basesel)2374 2450 elif mode.lower() == "time": 2375 2451 s = scantable(self._math._auto_quotient(self, mode, preserve)) 2376 2452 s._add_history("auto_quotient", varlist) 2377 print_log()2378 2453 return s 2379 2454 2380 @ print_log_dec2455 @asaplog_post_dec 2381 2456 def mx_quotient(self, mask = None, weight='median', preserve=True): 2382 2457 """\ … … 2386 2461 2387 2462 mask: an optional mask to be used when weight == 'stddev' 2463 2388 2464 weight: How to average the off beams. Default is 'median'. 2465 2389 2466 preserve: you can preserve (default) the continuum or 2390 remove it. The equations used are 2391 preserve: Output = Toff * (on/off) - Toff 2392 remove: Output = Toff * (on/off) - Ton 2467 remove it. The equations used are: 2468 2469 preserve: Output = Toff * (on/off) - Toff 2470 2471 remove: Output = Toff * (on/off) - Ton 2393 2472 2394 2473 """ … … 2401 2480 q = quotient(on, off, preserve) 2402 2481 q._add_history("mx_quotient", varlist) 2403 print_log()2404 2482 return q 2405 2483 2406 @ print_log_dec2484 @asaplog_post_dec 2407 2485 def freq_switch(self, insitu=None): 2408 2486 """\ … … 2421 2499 s = scantable(self._math._freqswitch(self)) 2422 2500 s._add_history("freq_switch", varlist) 2423 print_log() 2424 if insitu: self._assign(s) 2425 else: return s 2426 2427 @print_log_dec 2501 if insitu: 2502 self._assign(s) 2503 else: 2504 return s 2505 2506 @asaplog_post_dec 2428 2507 def recalc_azel(self): 2429 2508 """Recalculate the azimuth and elevation for each position.""" … … 2431 2510 self._recalcazel() 2432 2511 self._add_history("recalc_azel", varlist) 2433 print_log()2434 2512 return 2435 2513 2436 @ print_log_dec2514 @asaplog_post_dec 2437 2515 def __add__(self, other): 2438 2516 varlist = vars() … … 2447 2525 return s 2448 2526 2449 @ print_log_dec2527 @asaplog_post_dec 2450 2528 def __sub__(self, other): 2451 2529 """ … … 2463 2541 return s 2464 2542 2465 @ print_log_dec2543 @asaplog_post_dec 2466 2544 def __mul__(self, other): 2467 2545 """ … … 2480 2558 2481 2559 2482 @ print_log_dec2560 @asaplog_post_dec 2483 2561 def __div__(self, other): 2484 2562 """ … … 2498 2576 return s 2499 2577 2578 @asaplog_post_dec 2500 2579 def get_fit(self, row=0): 2501 2580 """\ … … 2511 2590 from asap.asapfit import asapfit 2512 2591 fit = asapfit(self._getfit(row)) 2513 if rcParams['verbose']: 2514 #print fit 2515 asaplog.push( '%s' %(fit) ) 2516 print_log() 2517 return 2518 else: 2519 return fit.as_dict() 2592 asaplog.push( '%s' %(fit) ) 2593 return fit.as_dict() 2520 2594 2521 2595 def flag_nans(self): … … 2611 2685 return (sum(nchans)/len(nchans) == nchans[0]) 2612 2686 2613 def _fill(self, names, unit, average, getpt, antenna): 2687 @asaplog_post_dec 2688 #def _fill(self, names, unit, average, getpt, antenna): 2689 def _fill(self, names, unit, average, opts={}): 2614 2690 first = True 2615 2691 fullnames = [] … … 2619 2695 if not os.path.exists(name): 2620 2696 msg = "File '%s' does not exists" % (name) 2621 if rcParams['verbose']:2622 asaplog.push(msg)2623 print_log( 'ERROR' )2624 return2625 2697 raise IOError(msg) 2626 2698 fullnames.append(name) … … 2635 2707 msg = "Importing %s..." % (name) 2636 2708 asaplog.push(msg, False) 2637 print_log()2638 r.open(name )# antenna, -1, -1, getpt)2709 #opts = {'ms': {'antenna' : antenna, 'getpt': getpt} } 2710 r.open(name, opts)# antenna, -1, -1, getpt) 2639 2711 r.fill() 2640 2712 if average: … … 2646 2718 del r, tbl 2647 2719 first = False 2720 #flush log 2721 asaplog.post() 2648 2722 if unit is not None: 2649 2723 self.set_fluxunit(unit)
Note:
See TracChangeset
for help on using the changeset viewer.