Changes in trunk/python/scantable.py [1948:1846]
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/python/scantable.py
r1948 r1846 2 2 3 3 import os 4 import numpy5 4 try: 6 5 from functools import wraps as wraps_dec … … 12 11 from asap._asap import filler 13 12 from asap.parameters import rcParams 14 from asap.logging import asaplog, asaplog_post_dec13 from asap.logging import asaplog, print_log, print_log_dec 15 14 from asap.selector import selector 16 15 from asap.linecatalog import linecatalog 17 16 from asap.coordinate import coordinate 18 from asap.utils import _n_bools, mask_not, mask_and, mask_or, page 19 from asap.asapfitter import fitter 17 from asap.utils import _n_bools, mask_not, mask_and, mask_or 20 18 21 19 … … 24 22 def wrap(obj, *args, **kw): 25 23 basesel = obj.get_selection() 26 try: 27 val = func(obj, *args, **kw) 28 finally: 29 obj.set_selection(basesel) 24 val = func(obj, *args, **kw) 25 obj.set_selection(basesel) 30 26 return val 31 27 return wrap … … 39 35 40 36 """ 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 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 79 42 class scantable(Scantable): 80 43 """\ … … 82 45 """ 83 46 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): 47 @print_log_dec 48 def __init__(self, filename, average=None, unit=None, getpt=None, 49 antenna=None, parallactify=None): 88 50 """\ 89 51 Create a scantable from a saved one or make a reference … … 111 73 the MS data faster in some cases. 112 74 113 antenna: for MeasurementSet input data only: 114 Antenna selection. integer (id) or string (name or id). 75 antenna: Antenna selection. integer (id) or string (name or id). 115 76 116 77 parallactify: Indicate that the data had been parallatified. Default … … 120 81 if average is None: 121 82 average = rcParams['scantable.autoaverage'] 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(',') 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(',') 142 105 parallactify = parallactify or rcParams['scantable.parallactify'] 143 106 varlist = vars() … … 152 115 if not os.path.exists(filename): 153 116 s = "File '%s' not found." % (filename) 117 if rcParams['verbose']: 118 asaplog.push(s) 119 print_log('ERROR') 120 return 154 121 raise IOError(s) 155 122 if is_scantable(filename): … … 160 127 # do not reset to the default freqframe 161 128 #self.set_freqframe(rcParams['scantable.freqframe']) 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: 129 elif os.path.isdir(filename) \ 130 and not os.path.exists(filename+'/table.f1'): 177 131 msg = "The given file '%s'is not a valid " \ 178 132 "asap table." % (filename) 179 raise IOError(msg) 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) 180 142 elif (isinstance(filename, list) or isinstance(filename, tuple)) \ 181 143 and isinstance(filename[-1], str): 182 #self._fill(filename, unit, average, getpt, antenna) 183 self._fill(filename, unit, average) 144 self._fill(filename, unit, average, getpt, antenna) 184 145 self.parallactify(parallactify) 185 146 self._add_history("scantable", varlist) 186 187 @asaplog_post_dec 147 print_log() 148 149 @print_log_dec 188 150 def save(self, name=None, format=None, overwrite=False): 189 151 """\ … … 196 158 this is the root file name (data in 'name'.txt 197 159 and header in 'name'_header.txt) 198 199 160 format: an optional file format. Default is ASAP. 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 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) 209 169 overwrite: If the file should be overwritten if it exists. 210 170 The default False is to return with warning 211 171 without writing the output. USE WITH CARE. 212 213 172 Example:: 214 173 … … 228 187 if not overwrite: 229 188 msg = "File %s exists." % name 230 raise IOError(msg) 189 if rcParams['verbose']: 190 #print msg 191 asaplog.push( msg ) 192 print_log( 'ERROR' ) 193 return 194 else: 195 raise IOError(msg) 231 196 format2 = format.upper() 232 197 if format2 == 'ASAP': … … 236 201 writer = stw(format2) 237 202 writer.write(self, name) 203 print_log() 238 204 return 239 205 … … 267 233 from asap import unique 268 234 if not _is_valid(scanid): 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) 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 277 267 278 268 def _select_copy(self, selection): … … 284 274 285 275 def get_scan(self, scanid=None): 286 """ \276 """ 287 277 Return a specific scan (by scanno) or collection of scans (by 288 278 source name) in a new scantable. … … 309 299 """ 310 300 if scanid is None: 311 raise RuntimeError( 'Please specify a scan no or name to ' 312 'retrieve from the scantable' ) 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 313 311 try: 314 312 bsel = self.get_selection() … … 325 323 else: 326 324 msg = "Illegal scanid type, use 'int' or 'list' if ints." 327 raise TypeError(msg) 325 if rcParams['verbose']: 326 #print msg 327 asaplog.push( msg ) 328 print_log( 'ERROR' ) 329 else: 330 raise TypeError(msg) 328 331 except RuntimeError: 329 raise 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 330 338 331 339 def __str__(self): … … 354 362 else: 355 363 msg = "Illegal file name '%s'." % (filename) 356 raise IOError(msg) 357 return page(info) 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 358 378 359 379 def get_spectrum(self, rowno): … … 378 398 379 399 def set_spectrum(self, spec, rowno): 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 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 387 406 388 407 """ … … 498 517 return workscan 499 518 500 @asaplog_post_dec501 def stats(self, stat='stddev', mask=None, form='3.3f' , row=None):519 #def stats(self, stat='stddev', mask=None): 520 def stats(self, stat='stddev', mask=None, form='3.3f'): 502 521 """\ 503 522 Determine the specified statistic of the current beam/if/pol … … 509 528 stat: 'min', 'max', 'min_abc', 'max_abc', 'sumsq', 'sum', 510 529 'mean', 'var', 'stddev', 'avdev', 'rms', 'median' 511 512 530 mask: an optional mask specifying where the statistic 513 531 should be determined. 514 515 532 form: format string to print statistic values 516 533 517 row: row number of spectrum to process. 518 (default is None: for all rows) 519 520 Example: 534 Example:: 535 521 536 scan.set_unit('channel') 522 537 msk = scan.create_mask([100, 200], [500, 600]) … … 536 551 getchan = True 537 552 statvals = [] 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)) 553 if not rtnabc: statvals = self._math._stats(self, mask, stat) 543 554 544 555 #def cb(i): … … 552 563 #outvec = [] 553 564 sep = '-'*50 554 555 if row == None: 556 rows = xrange(self.nrow()) 557 elif isinstance(row, int): 558 rows = [ row ] 559 560 for i in rows: 565 for i in range(self.nrow()): 561 566 refstr = '' 562 567 statunit= '' … … 574 579 out += 'Scan[%d] (%s) ' % (self.getscan(i), src) 575 580 out += 'Time[%s]:\n' % (tm) 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)) 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)) 579 585 #outvec.append(callback(i)) 580 if len(rows) > 1: 581 # out += ('= %'+form) % (outvec[i]) +' '+refstr+'\n' 582 out += ('= %'+form) % (statvals[i]) +' '+refstr+'\n' 586 #out += ('= %'+form) % (outvec[i]) +' '+refstr+'\n' 587 out += ('= %'+form) % (statvals[i]) +' '+refstr+'\n' 588 out += sep+"\n" 589 590 if rcParams['verbose']: 591 import os 592 if os.environ.has_key( 'USER' ): 593 usr=os.environ['USER'] 583 594 else: 584 # out += ('= %'+form) % (outvec[0]) +' '+refstr+'\n' 585 out += ('= %'+form) % (statvals[0]) +' '+refstr+'\n' 586 out += sep+"\n" 587 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 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() 606 612 return statvals 607 613 … … 615 621 rowno: a row number in the scantable. Default is the 616 622 first row, i.e. rowno=0 617 618 623 chan: a channel in the scantable. Default is the first 619 624 channel, i.e. pos=0 … … 718 723 out += '= %3.3f\n' % (outvec[i]) 719 724 out += sep+'\n' 720 721 asaplog.push(sep)722 asaplog.push(" %s" % (label))723 asaplog.push(sep)724 asaplog.push(out)725 asaplog.post()725 if rcParams['verbose']: 726 asaplog.push(sep) 727 asaplog.push(" %s" % (label)) 728 asaplog.push(sep) 729 asaplog.push(out) 730 print_log() 726 731 return outvec 727 732 728 def _get_column(self, callback, row=-1 , *args):733 def _get_column(self, callback, row=-1): 729 734 """ 730 735 """ 731 736 if row == -1: 732 return [callback(i , *args) for i in range(self.nrow())]737 return [callback(i) for i in range(self.nrow())] 733 738 else: 734 739 if 0 <= row < self.nrow(): 735 return callback(row , *args)736 737 738 def get_time(self, row=-1, asdatetime=False , prec=-1):740 return callback(row) 741 742 743 def get_time(self, row=-1, asdatetime=False): 739 744 """\ 740 745 Get a list of time stamps for the observations. 741 Return a datetime object or a string (default) for each 742 integration time stamp in the scantable. 746 Return a datetime object for each integration time stamp in the scantable. 743 747 744 748 Parameters: 745 749 746 750 row: row no of integration. Default -1 return all rows 747 748 751 asdatetime: return values as datetime objects rather than strings 749 752 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 """ 753 """ 754 from time import strptime 757 755 from datetime import datetime 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) 756 times = self._get_column(self._gettime, row) 769 757 if not asdatetime: 770 758 return times 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],"") 759 format = "%Y/%m/%d/%H:%M:%S" 777 760 if isinstance(times, list): 778 return [datetime .strptime(i, format) for i in times]779 else: 780 return datetime .strptime(times, format)761 return [datetime(*strptime(i, format)[:6]) for i in times] 762 else: 763 return datetime(*strptime(times, format)[:6]) 781 764 782 765 … … 844 827 Get a list of Positions on the sky (direction) for the observations. 845 828 Return a string for each integration in the scantable. 846 847 Parameters: 848 829 Parameters: 849 830 row: row no of integration. Default -1 return all rows 850 831 Example: 832 none 851 833 """ 852 834 return self._get_column(self._getdirection, row) … … 864 846 return self._get_column(self._getdirectionvec, row) 865 847 866 @ asaplog_post_dec848 @print_log_dec 867 849 def set_unit(self, unit='channel'): 868 850 """\ … … 883 865 self._add_history("set_unit", varlist) 884 866 885 @ asaplog_post_dec867 @print_log_dec 886 868 def set_instrument(self, instr): 887 869 """\ … … 896 878 self._setInstrument(instr) 897 879 self._add_history("set_instument", vars()) 898 899 @asaplog_post_dec 880 print_log() 881 882 @print_log_dec 900 883 def set_feedtype(self, feedtype): 901 884 """\ … … 909 892 self._setfeedtype(feedtype) 910 893 self._add_history("set_feedtype", vars()) 911 912 @asaplog_post_dec 894 print_log() 895 896 @print_log_dec 913 897 def set_doppler(self, doppler='RADIO'): 914 898 """\ … … 925 909 self._setcoordinfo(inf) 926 910 self._add_history("set_doppler", vars()) 927 928 @asaplog_post_dec 911 print_log() 912 913 @print_log_dec 929 914 def set_freqframe(self, frame=None): 930 915 """\ … … 957 942 else: 958 943 msg = "Please specify a valid freq type. Valid types are:\n", valid 959 raise TypeError(msg) 960 961 @asaplog_post_dec 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 962 952 def set_dirframe(self, frame=""): 963 953 """\ … … 975 965 """ 976 966 varlist = vars() 977 Scantable.set_dirframe(self, frame) 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 978 977 self._add_history("set_dirframe", varlist) 979 978 … … 992 991 return unit 993 992 994 @asaplog_post_dec995 993 def get_abcissa(self, rowno=0): 996 994 """\ … … 1010 1008 abc = self._getabcissa(rowno) 1011 1009 lbl = self._getabcissalabel(rowno) 1010 print_log() 1012 1011 return abc, lbl 1013 1012 1014 @asaplog_post_dec1015 1013 def flag(self, mask=None, unflag=False): 1016 1014 """\ … … 1021 1019 mask: an optional channel mask, created with create_mask. Default 1022 1020 (no mask) is all channels. 1023 1024 1021 unflag: if True, unflag the data 1025 1022 … … 1027 1024 varlist = vars() 1028 1025 mask = mask or [] 1029 self._flag(mask, unflag) 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 1030 1036 self._add_history("flag", varlist) 1031 1037 1032 @asaplog_post_dec1033 1038 def flag_row(self, rows=[], unflag=False): 1034 1039 """\ … … 1039 1044 rows: list of row numbers to be flagged. Default is no row 1040 1045 (must be explicitly specified to execute row-based flagging). 1041 1042 1046 unflag: if True, unflag the data. 1043 1047 1044 1048 """ 1045 1049 varlist = vars() 1046 self._flag_row(rows, unflag) 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 1047 1059 self._add_history("flag_row", varlist) 1048 1060 1049 @asaplog_post_dec1050 1061 def clip(self, uthres=None, dthres=None, clipoutside=True, unflag=False): 1051 1062 """\ … … 1055 1066 1056 1067 uthres: upper threshold. 1057 1058 1068 dthres: lower threshold 1059 1069 1060 1070 clipoutside: True for flagging data outside the range [dthres:uthres]. 1061 False for flagging data inside the range. 1062 1071 False for glagging data inside the range. 1063 1072 unflag: if True, unflag the data. 1064 1073 1065 1074 """ 1066 1075 varlist = vars() 1067 self._clip(uthres, dthres, clipoutside, unflag) 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 1068 1085 self._add_history("clip", varlist) 1069 1086 1070 @ asaplog_post_dec1087 @print_log_dec 1071 1088 def lag_flag(self, start, end, unit="MHz", insitu=None): 1072 1089 """\ … … 1079 1096 start: the start frequency (really a period within the 1080 1097 bandwidth) or period to remove 1081 1082 1098 end: the end frequency or period to remove 1083 1084 1099 unit: the frequency unit (default "MHz") or "" for 1085 1100 explicit lag channels … … 1097 1112 if not (unit == "" or base.has_key(unit)): 1098 1113 raise ValueError("%s is not a valid unit." % unit) 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")) 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 1104 1128 s._add_history("lag_flag", varlist) 1129 print_log() 1105 1130 if insitu: 1106 1131 self._assign(s) … … 1108 1133 return s 1109 1134 1110 @ asaplog_post_dec1135 @print_log_dec 1111 1136 def create_mask(self, *args, **kwargs): 1112 1137 """\ … … 1120 1145 Pairs of start/end points (inclusive)specifying the regions 1121 1146 to be masked 1122 1123 1147 invert: optional argument. If specified as True, 1124 1148 return an inverted mask, i.e. the regions 1125 1149 specified are EXCLUDED 1126 1127 1150 row: create the mask using the specified row for 1128 1151 unit conversions, default is row=0 … … 1150 1173 data = self._getabcissa(row) 1151 1174 u = self._getcoordinfo()[0] 1152 if u == "":1153 u = "channel"1154 msg = "The current mask window unit is %s" % u1155 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)1175 if rcParams['verbose']: 1176 if u == "": u = "channel" 1177 msg = "The current mask window unit is %s" % u 1178 i = self._check_ifs() 1179 if not i: 1180 msg += "\nThis mask is only valid for IF=%d" % (self.getif(i)) 1181 asaplog.push(msg) 1159 1182 n = self.nchan() 1160 1183 msk = _n_bools(n, False) … … 1178 1201 if kwargs.get('invert'): 1179 1202 msk = mask_not(msk) 1203 print_log() 1180 1204 return msk 1181 1205 1182 def get_masklist(self, mask=None, row=0 , silent=False):1206 def get_masklist(self, mask=None, row=0): 1183 1207 """\ 1184 1208 Compute and return a list of mask windows, [min, max]. … … 1187 1211 1188 1212 mask: channel mask, created with create_mask. 1189 1190 1213 row: calcutate the masklist using the specified row 1191 1214 for unit conversions, default is row=0 … … 1208 1231 data = self._getabcissa(row) 1209 1232 u = self._getcoordinfo()[0] 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: 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)) 1217 1239 asaplog.push(msg) 1218 1240 masklist=[] … … 1229 1251 """\ 1230 1252 Compute and Return lists of mask start indices and mask end indices. 1231 1232 Parameters: 1233 1253 Parameters: 1234 1254 mask: channel mask, created with create_mask. 1235 1255 … … 1306 1326 """\ 1307 1327 Set or replace the restfrequency specified and 1308 if the 'freqs' argument holds a scalar,1328 If the 'freqs' argument holds a scalar, 1309 1329 then that rest frequency will be applied to all the selected 1310 1330 data. If the 'freqs' argument holds … … 1322 1342 1323 1343 freqs: list of rest frequency values or string idenitfiers 1324 1325 1344 unit: unit for rest frequency (default 'Hz') 1326 1345 … … 1433 1452 Scantable.shift_refpix(self, delta) 1434 1453 1435 @asaplog_post_dec1436 1454 def history(self, filename=None): 1437 1455 """\ … … 1440 1458 Parameters: 1441 1459 1442 filename: The name of the file to save the history to.1460 filename: The name of the file to save the history to. 1443 1461 1444 1462 """ … … 1447 1465 for h in hist: 1448 1466 if h.startswith("---"): 1449 out = "\n".join([out, h])1467 out += "\n"+h 1450 1468 else: 1451 1469 items = h.split("##") … … 1456 1474 out += "Function: %s\n Parameters:" % (func) 1457 1475 for i in items: 1458 if i == '':1459 continue1460 1476 s = i.split("=") 1461 1477 out += "\n %s = %s" % (s[0], s[1]) 1462 out = "\n".join([out, "-"*80])1478 out += "\n"+"-"*80 1463 1479 if filename is not None: 1464 1480 if filename is "": … … 1472 1488 else: 1473 1489 msg = "Illegal file name '%s'." % (filename) 1474 raise IOError(msg) 1475 return page(out) 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 1476 1505 # 1477 1506 # Maths business 1478 1507 # 1479 @ asaplog_post_dec1508 @print_log_dec 1480 1509 def average_time(self, mask=None, scanav=False, weight='tint', align=False): 1481 1510 """\ … … 1490 1519 mask: an optional mask (only used for 'var' and 'tsys' 1491 1520 weighting) 1492 1493 1521 scanav: True averages each scan separately 1494 1522 False (default) averages all scans together, 1495 1496 1523 weight: Weighting scheme. 1497 1524 'none' (mean no weight) … … 1502 1529 'median' ( median averaging) 1503 1530 The default is 'tint' 1504 1505 1531 align: align the spectra in velocity before averaging. It takes 1506 1532 the time of the first spectrum as reference time. … … 1517 1543 scanav = (scanav and 'SCAN') or 'NONE' 1518 1544 scan = (self, ) 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)) 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 1529 1563 s._add_history("average_time", varlist) 1564 print_log() 1530 1565 return s 1531 1566 1532 @ asaplog_post_dec1567 @print_log_dec 1533 1568 def convert_flux(self, jyperk=None, eta=None, d=None, insitu=None): 1534 1569 """\ … … 1543 1578 1544 1579 jyperk: the Jy / K conversion factor 1545 1546 1580 eta: the aperture efficiency 1547 1548 d: the geometric diameter (metres) 1549 1581 d: the geomtric diameter (metres) 1550 1582 insitu: if False a new scantable is returned. 1551 1583 Otherwise, the scaling is done in-situ … … 1561 1593 s = scantable(self._math._convertflux(self, d, eta, jyperk)) 1562 1594 s._add_history("convert_flux", varlist) 1595 print_log() 1563 1596 if insitu: self._assign(s) 1564 1597 else: return s 1565 1598 1566 @ asaplog_post_dec1599 @print_log_dec 1567 1600 def gain_el(self, poly=None, filename="", method="linear", insitu=None): 1568 1601 """\ … … 1581 1614 gain-elevation correction as a function of 1582 1615 elevation (in degrees). 1583 1584 1616 filename: The name of an ascii file holding correction factors. 1585 1617 The first row of the ascii file must give the column … … 1600 1632 0.5 80 0.8 1601 1633 0.6 90 0.75 1602 1603 1634 method: Interpolation method when correcting from a table. 1604 1635 Values are "nearest", "linear" (default), "cubic" 1605 1636 and "spline" 1606 1607 1637 insitu: if False a new scantable is returned. 1608 1638 Otherwise, the scaling is done in-situ … … 1619 1649 s = scantable(self._math._gainel(self, poly, filename, method)) 1620 1650 s._add_history("gain_el", varlist) 1651 print_log() 1621 1652 if insitu: 1622 1653 self._assign(s) … … 1624 1655 return s 1625 1656 1626 @ asaplog_post_dec1657 @print_log_dec 1627 1658 def freq_align(self, reftime=None, method='cubic', insitu=None): 1628 1659 """\ … … 1632 1663 1633 1664 Parameters: 1634 1635 1665 reftime: reference time to align at. By default, the time of 1636 1666 the first row of data is used. 1637 1638 1667 method: Interpolation method for regridding the spectra. 1639 1668 Choose from "nearest", "linear", "cubic" (default) 1640 1669 and "spline" 1641 1642 1670 insitu: if False a new scantable is returned. 1643 1671 Otherwise, the scaling is done in-situ … … 1651 1679 s = scantable(self._math._freq_align(self, reftime, method)) 1652 1680 s._add_history("freq_align", varlist) 1681 print_log() 1653 1682 if insitu: self._assign(s) 1654 1683 else: return s 1655 1684 1656 @ asaplog_post_dec1685 @print_log_dec 1657 1686 def opacity(self, tau=None, insitu=None): 1658 1687 """\ … … 1661 1690 1662 1691 Parameters: 1663 1664 1692 tau: (list of) opacity from which the correction factor is 1665 1693 exp(tau*ZD) … … 1670 1698 if tau is `None` the opacities are determined from a 1671 1699 model. 1672 1673 1700 insitu: if False a new scantable is returned. 1674 1701 Otherwise, the scaling is done in-situ … … 1683 1710 s = scantable(self._math._opacity(self, tau)) 1684 1711 s._add_history("opacity", varlist) 1712 print_log() 1685 1713 if insitu: self._assign(s) 1686 1714 else: return s 1687 1715 1688 @ asaplog_post_dec1716 @print_log_dec 1689 1717 def bin(self, width=5, insitu=None): 1690 1718 """\ … … 1694 1722 1695 1723 width: The bin width (default=5) in pixels 1696 1697 1724 insitu: if False a new scantable is returned. 1698 1725 Otherwise, the scaling is done in-situ … … 1705 1732 s = scantable(self._math._bin(self, width)) 1706 1733 s._add_history("bin", varlist) 1734 print_log() 1707 1735 if insitu: 1708 1736 self._assign(s) … … 1710 1738 return s 1711 1739 1712 @ asaplog_post_dec1740 @print_log_dec 1713 1741 def resample(self, width=5, method='cubic', insitu=None): 1714 1742 """\ … … 1718 1746 1719 1747 width: The bin width (default=5) in pixels 1720 1721 1748 method: Interpolation method when correcting from a table. 1722 1749 Values are "nearest", "linear", "cubic" (default) 1723 1750 and "spline" 1724 1725 1751 insitu: if False a new scantable is returned. 1726 1752 Otherwise, the scaling is done in-situ … … 1733 1759 s = scantable(self._math._resample(self, method, width)) 1734 1760 s._add_history("resample", varlist) 1761 print_log() 1735 1762 if insitu: self._assign(s) 1736 1763 else: return s 1737 1764 1738 @ asaplog_post_dec1765 @print_log_dec 1739 1766 def average_pol(self, mask=None, weight='none'): 1740 1767 """\ … … 1746 1773 averaging will be applied. The output will have all 1747 1774 specified points masked. 1748 1749 1775 weight: Weighting scheme. 'none' (default), 'var' (1/var(spec) 1750 1776 weighted), or 'tsys' (1/Tsys**2 weighted) … … 1755 1781 s = scantable(self._math._averagepol(self, mask, weight.upper())) 1756 1782 s._add_history("average_pol", varlist) 1783 print_log() 1757 1784 return s 1758 1785 1759 @ asaplog_post_dec1786 @print_log_dec 1760 1787 def average_beam(self, mask=None, weight='none'): 1761 1788 """\ … … 1766 1793 averaging will be applied. The output will have all 1767 1794 specified points masked. 1768 1769 1795 weight: Weighting scheme. 'none' (default), 'var' (1/var(spec) 1770 1796 weighted), or 'tsys' (1/Tsys**2 weighted) … … 1775 1801 s = scantable(self._math._averagebeams(self, mask, weight.upper())) 1776 1802 s._add_history("average_beam", varlist) 1803 print_log() 1777 1804 return s 1778 1805 … … 1783 1810 1784 1811 Parameters: 1785 1786 1812 pflag: Bool indicating whether to turn this on (True) or 1787 1813 off (False) … … 1792 1818 self._add_history("parallactify", varlist) 1793 1819 1794 @ asaplog_post_dec1820 @print_log_dec 1795 1821 def convert_pol(self, poltype=None): 1796 1822 """\ … … 1799 1825 1800 1826 Parameters: 1801 1802 1827 poltype: The new polarisation type. Valid types are: 1803 1828 "linear", "circular", "stokes" and "linpol" … … 1805 1830 """ 1806 1831 varlist = vars() 1807 s = scantable(self._math._convertpol(self, poltype)) 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 1808 1843 s._add_history("convert_pol", varlist) 1844 print_log() 1809 1845 return s 1810 1846 1811 @ asaplog_post_dec1847 @print_log_dec 1812 1848 def smooth(self, kernel="hanning", width=5.0, order=2, plot=False, insitu=None): 1813 1849 """\ … … 1819 1855 'hanning' (default), 'gaussian', 'boxcar', 'rmedian' 1820 1856 or 'poly' 1821 1822 1857 width: The width of the kernel in pixels. For hanning this is 1823 1858 ignored otherwise it defauls to 5 pixels. … … 1825 1860 Maximum. For 'boxcar' it is the full width. 1826 1861 For 'rmedian' and 'poly' it is the half width. 1827 1828 1862 order: Optional parameter for 'poly' kernel (default is 2), to 1829 1863 specify the order of the polnomial. Ignored by all other 1830 1864 kernels. 1831 1832 1865 plot: plot the original and the smoothed spectra. 1833 1866 In this each indivual fit has to be approved, by 1834 1867 typing 'y' or 'n' 1835 1836 1868 insitu: if False a new scantable is returned. 1837 1869 Otherwise, the scaling is done in-situ … … 1884 1916 del orgscan 1885 1917 1918 print_log() 1886 1919 if insitu: self._assign(s) 1887 1920 else: return s 1888 1921 1889 @asaplog_post_dec 1890 def old_poly_baseline(self, mask=None, order=0, plot=False, uselin=False, insitu=None, rows=None): 1922 @print_log_dec 1923 def poly_baseline(self, mask=None, order=0, plot=False, uselin=False, 1924 insitu=None): 1891 1925 """\ 1892 1926 Return a scan which has been baselined (all rows) by a polynomial. 1893 1927 1894 1928 Parameters: 1895 1929 1896 1930 mask: an optional mask 1897 1898 1931 order: the order of the polynomial (default is 0) 1899 1900 1932 plot: plot the fit and the residual. In this each 1901 1933 indivual fit has to be approved, by typing 'y' 1902 1934 or 'n' 1903 1904 1935 uselin: use linear polynomial fit 1905 1906 1936 insitu: if False a new scantable is returned. 1907 1937 Otherwise, the scaling is done in-situ 1908 1938 The default is taken from .asaprc (False) 1909 1939 1910 rows: row numbers of spectra to be processed. 1911 (default is None: for all rows) 1912 1913 Example: 1940 Example:: 1941 1914 1942 # return a scan baselined by a third order polynomial, 1915 1943 # not using a mask … … 1924 1952 varlist = vars() 1925 1953 if mask is None: 1926 mask = [True for i in xrange(self.nchan())] 1927 1954 mask = [True for i in xrange(self.nchan(-1))] 1955 1956 from asap.asapfitter import fitter 1928 1957 try: 1929 1958 f = fitter() … … 1933 1962 f.set_function(poly=order) 1934 1963 1935 if rows == None: 1936 rows = xrange(workscan.nrow()) 1937 elif isinstance(rows, int): 1938 rows = [ rows ] 1939 1964 rows = range(workscan.nrow()) 1940 1965 if len(rows) > 0: 1941 1966 self.blpars = [] 1942 self.masklists = [] 1943 self.actualmask = [] 1944 1967 1945 1968 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) 1946 1981 f.x = workscan._getabcissa(r) 1947 1982 f.y = workscan._getspectrum(r) 1948 f.mask = mask_and(mask, workscan._getmask(r)) # (CAS-1434)1949 1983 f.data = None 1950 1984 f.fit() … … 1954 1988 if x.upper() == 'N': 1955 1989 self.blpars.append(None) 1956 self.masklists.append(None)1957 self.actualmask.append(None)1958 1990 continue 1959 1991 workscan._setspectrum(f.fitter.getresidual(), r) 1960 1992 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)1963 1993 1964 1994 if plot: … … 1966 1996 f._p = None 1967 1997 workscan._add_history("poly_baseline", varlist) 1968 if insitu: 1969 self._assign(workscan) 1970 else: 1971 return workscan 1998 print_log() 1999 if insitu: self._assign(workscan) 2000 else: return workscan 1972 2001 except RuntimeError: 1973 2002 msg = "The fit failed, possibly because it didn't converge." 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)) 2003 if rcParams['verbose']: 2004 #print msg 2005 print_log() 2006 asaplog.push( str(msg) ) 2007 print_log( 'ERROR' ) 2078 2008 return 2079 2009 else: 2080 raise RuntimeError( str(e)+'\n'+msg)2081 2082 2083 def auto_poly_baseline(self, mask= None, edge=(0, 0), order=0,2010 raise RuntimeError(msg) 2011 2012 2013 def auto_poly_baseline(self, mask=[], edge=(0, 0), order=0, 2084 2014 threshold=3, chan_avg_limit=1, plot=False, 2085 insitu=None , rows=None):2015 insitu=None): 2086 2016 """\ 2087 2017 Return a scan which has been baselined (all rows) by a polynomial. … … 2124 2054 Otherwise, the scaling is done in-situ 2125 2055 The default is taken from .asaprc (False) 2126 rows: row numbers of spectra to be processed.2127 (default is None: for all rows)2128 2056 2129 2057 … … 2135 2063 if insitu is None: insitu = rcParams['insitu'] 2136 2064 varlist = vars() 2065 from asap.asapfitter import fitter 2137 2066 from asap.asaplinefind import linefinder 2138 2067 from asap import _is_sequence_or_number as _is_valid … … 2158 2087 curedge = edge; 2159 2088 2160 if not insitu:2161 workscan = self.copy()2162 else:2163 workscan = self2164 2165 2089 # setup fitter 2166 2090 f = fitter() 2167 f.set_function( lpoly=order)2091 f.set_function(poly=order) 2168 2092 2169 2093 # setup line finder … … 2171 2095 fl.set_options(threshold=threshold,avg_limit=chan_avg_limit) 2172 2096 2097 if not insitu: 2098 workscan = self.copy() 2099 else: 2100 workscan = self 2101 2173 2102 fl.set_scan(workscan) 2174 2103 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 2104 rows = range(workscan.nrow()) 2183 2105 # Save parameters of baseline fits & masklists as a class attribute. 2184 2106 # NOTICE: It does not reflect changes in scantable! … … 2186 2108 self.blpars=[] 2187 2109 self.masklists=[] 2188 self.actualmask=[]2189 2110 asaplog.push("Processing:") 2190 2111 for r in rows: … … 2201 2122 curedge = edge[workscan.getif(r)] 2202 2123 2203 actualmask = mask_and(mask, workscan._getmask(r)) # (CAS-1434) 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] 2204 2135 2205 2136 # setup line finder 2206 2137 fl.find_lines(r, actualmask, curedge) 2207 2138 outmask=fl.get_mask() 2139 f.set_scan(workscan, fl.get_mask()) 2208 2140 f.x = workscan._getabcissa(r) 2209 2141 f.y = workscan._getspectrum(r) 2210 f.mask = fl.get_mask()2211 2142 f.data = None 2212 2143 f.fit() 2213 2144 2214 2145 # Show mask list 2215 masklist=workscan.get_masklist(f .mask, row=r, silent=True)2146 masklist=workscan.get_masklist(fl.get_mask(),row=r) 2216 2147 msg = "mask range: "+str(masklist) 2217 2148 asaplog.push(msg, False) … … 2223 2154 self.blpars.append(None) 2224 2155 self.masklists.append(None) 2225 self.actualmask.append(None)2226 2156 continue 2227 2157 … … 2229 2159 self.blpars.append(f.get_parameters()) 2230 2160 self.masklists.append(masklist) 2231 self.actualmask.append(f.mask)2232 2161 if plot: 2233 2162 f._p.unmap() … … 2239 2168 return workscan 2240 2169 2241 @ asaplog_post_dec2170 @print_log_dec 2242 2171 def rotate_linpolphase(self, angle): 2243 2172 """\ … … 2258 2187 self._math._rotate_linpolphase(self, angle) 2259 2188 self._add_history("rotate_linpolphase", varlist) 2189 print_log() 2260 2190 return 2261 2191 2262 @ asaplog_post_dec2192 @print_log_dec 2263 2193 def rotate_xyphase(self, angle): 2264 2194 """\ … … 2279 2209 self._math._rotate_xyphase(self, angle) 2280 2210 self._add_history("rotate_xyphase", varlist) 2211 print_log() 2281 2212 return 2282 2213 2283 @ asaplog_post_dec2214 @print_log_dec 2284 2215 def swap_linears(self): 2285 2216 """\ … … 2290 2221 self._math._swap_linears(self) 2291 2222 self._add_history("swap_linears", varlist) 2223 print_log() 2292 2224 return 2293 2225 2294 @ asaplog_post_dec2226 @print_log_dec 2295 2227 def invert_phase(self): 2296 2228 """\ … … 2302 2234 return 2303 2235 2304 @ asaplog_post_dec2236 @print_log_dec 2305 2237 def add(self, offset, insitu=None): 2306 2238 """\ … … 2310 2242 2311 2243 offset: the offset 2312 2313 2244 insitu: if False a new scantable is returned. 2314 2245 Otherwise, the scaling is done in-situ … … 2326 2257 return s 2327 2258 2328 @ asaplog_post_dec2259 @print_log_dec 2329 2260 def scale(self, factor, tsys=True, insitu=None): 2330 2261 """\ 2331 2262 2332 Return a scan where all spectra are scaled by the give n'factor'2263 Return a scan where all spectra are scaled by the give 'factor' 2333 2264 2334 2265 Parameters: 2335 2266 2336 2267 factor: the scaling factor (float or 1D float list) 2337 2338 2268 insitu: if False a new scantable is returned. 2339 2269 Otherwise, the scaling is done in-situ 2340 2270 The default is taken from .asaprc (False) 2341 2342 2271 tsys: if True (default) then apply the operation to Tsys 2343 2272 as well as the data … … 2358 2287 s = scantable(self._math._unaryop(self.copy(), factor, "MUL", tsys)) 2359 2288 s._add_history("scale", varlist) 2289 print_log() 2360 2290 if insitu: 2361 2291 self._assign(s) … … 2372 2302 2373 2303 match: a Unix style pattern, regular expression or selector 2374 2375 2304 matchtype: 'pattern' (default) UNIX style pattern or 2376 2305 'regex' regular expression 2377 2378 2306 sourcetype: the type of the source to use (source/reference) 2379 2307 … … 2404 2332 self._add_history("set_sourcetype", varlist) 2405 2333 2406 @asaplog_post_dec 2407 @preserve_selection 2334 @print_log_dec 2408 2335 def auto_quotient(self, preserve=True, mode='paired', verify=False): 2409 2336 """\ … … 2416 2343 preserve: you can preserve (default) the continuum or 2417 2344 remove it. The equations used are 2418 2419 2345 preserve: Output = Toff * (on/off) - Toff 2420 2421 2346 remove: Output = Toff * (on/off) - Ton 2422 2423 2347 mode: the on/off detection mode 2424 2348 'paired' (default) … … 2430 2354 finds the closest off in time 2431 2355 2432 .. todo:: verify argument is not implemented 2433 2434 """ 2435 varlist = vars() 2356 """ 2436 2357 modes = ["time", "paired"] 2437 2358 if not mode in modes: 2438 2359 msg = "please provide valid mode. Valid modes are %s" % (modes) 2439 2360 raise ValueError(msg) 2361 varlist = vars() 2440 2362 s = None 2441 2363 if mode.lower() == "paired": 2442 sel = self.get_selection() 2443 sel.set_query("SRCTYPE==psoff") 2364 basesel = self.get_selection() 2365 sel = selector()+basesel 2366 sel.set_query("SRCTYPE==1") 2444 2367 self.set_selection(sel) 2445 2368 offs = self.copy() 2446 sel.set_query("SRCTYPE== pson")2369 sel.set_query("SRCTYPE==0") 2447 2370 self.set_selection(sel) 2448 2371 ons = self.copy() 2449 2372 s = scantable(self._math._quotient(ons, offs, preserve)) 2373 self.set_selection(basesel) 2450 2374 elif mode.lower() == "time": 2451 2375 s = scantable(self._math._auto_quotient(self, mode, preserve)) 2452 2376 s._add_history("auto_quotient", varlist) 2377 print_log() 2453 2378 return s 2454 2379 2455 @ asaplog_post_dec2380 @print_log_dec 2456 2381 def mx_quotient(self, mask = None, weight='median', preserve=True): 2457 2382 """\ … … 2461 2386 2462 2387 mask: an optional mask to be used when weight == 'stddev' 2463 2464 2388 weight: How to average the off beams. Default is 'median'. 2465 2466 2389 preserve: you can preserve (default) the continuum or 2467 remove it. The equations used are: 2468 2469 preserve: Output = Toff * (on/off) - Toff 2470 2471 remove: Output = Toff * (on/off) - Ton 2390 remove it. The equations used are 2391 preserve: Output = Toff * (on/off) - Toff 2392 remove: Output = Toff * (on/off) - Ton 2472 2393 2473 2394 """ … … 2480 2401 q = quotient(on, off, preserve) 2481 2402 q._add_history("mx_quotient", varlist) 2403 print_log() 2482 2404 return q 2483 2405 2484 @ asaplog_post_dec2406 @print_log_dec 2485 2407 def freq_switch(self, insitu=None): 2486 2408 """\ … … 2499 2421 s = scantable(self._math._freqswitch(self)) 2500 2422 s._add_history("freq_switch", varlist) 2501 if insitu: 2502 self._assign(s) 2503 else: 2504 return s 2505 2506 @asaplog_post_dec 2423 print_log() 2424 if insitu: self._assign(s) 2425 else: return s 2426 2427 @print_log_dec 2507 2428 def recalc_azel(self): 2508 2429 """Recalculate the azimuth and elevation for each position.""" … … 2510 2431 self._recalcazel() 2511 2432 self._add_history("recalc_azel", varlist) 2433 print_log() 2512 2434 return 2513 2435 2514 @ asaplog_post_dec2436 @print_log_dec 2515 2437 def __add__(self, other): 2516 2438 varlist = vars() … … 2525 2447 return s 2526 2448 2527 @ asaplog_post_dec2449 @print_log_dec 2528 2450 def __sub__(self, other): 2529 2451 """ … … 2541 2463 return s 2542 2464 2543 @ asaplog_post_dec2465 @print_log_dec 2544 2466 def __mul__(self, other): 2545 2467 """ … … 2558 2480 2559 2481 2560 @ asaplog_post_dec2482 @print_log_dec 2561 2483 def __div__(self, other): 2562 2484 """ … … 2576 2498 return s 2577 2499 2578 @asaplog_post_dec2579 2500 def get_fit(self, row=0): 2580 2501 """\ … … 2590 2511 from asap.asapfit import asapfit 2591 2512 fit = asapfit(self._getfit(row)) 2592 asaplog.push( '%s' %(fit) ) 2593 return fit.as_dict() 2513 if rcParams['verbose']: 2514 #print fit 2515 asaplog.push( '%s' %(fit) ) 2516 print_log() 2517 return 2518 else: 2519 return fit.as_dict() 2594 2520 2595 2521 def flag_nans(self): … … 2685 2611 return (sum(nchans)/len(nchans) == nchans[0]) 2686 2612 2687 @asaplog_post_dec 2688 #def _fill(self, names, unit, average, getpt, antenna): 2689 def _fill(self, names, unit, average, opts={}): 2613 def _fill(self, names, unit, average, getpt, antenna): 2690 2614 first = True 2691 2615 fullnames = [] … … 2695 2619 if not os.path.exists(name): 2696 2620 msg = "File '%s' does not exists" % (name) 2621 if rcParams['verbose']: 2622 asaplog.push(msg) 2623 print_log( 'ERROR' ) 2624 return 2697 2625 raise IOError(msg) 2698 2626 fullnames.append(name) … … 2707 2635 msg = "Importing %s..." % (name) 2708 2636 asaplog.push(msg, False) 2709 #opts = {'ms': {'antenna' : antenna, 'getpt': getpt} }2710 r.open(name , opts)# antenna, -1, -1, getpt)2637 print_log() 2638 r.open(name)# antenna, -1, -1, getpt) 2711 2639 r.fill() 2712 2640 if average: … … 2718 2646 del r, tbl 2719 2647 first = False 2720 #flush log2721 asaplog.post()2722 2648 if unit is not None: 2723 2649 self.set_fluxunit(unit)
Note:
See TracChangeset
for help on using the changeset viewer.