Changeset 1779 for branches/mergetest/python/scantable.py
- Timestamp:
- 07/29/10 19:13:46 (14 years ago)
- Location:
- branches/mergetest
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/mergetest
- Property svn:mergeinfo changed
-
branches/mergetest/python/scantable.py
r1731 r1779 35 35 The ASAP container for scans 36 36 """ 37 @print_log_dec 38 def __init__(self, filename, average=None, unit=None, parallactify=None): 37 38 #@print_log_dec 39 def __init__(self, filename, average=None, unit=None, getpt=None, antenna=None, parallactify=None): 39 40 """ 40 41 Create a scantable from a saved one or make a reference … … 54 55 (input rpfits/sdfits/ms) or replaces the value 55 56 in existing scantables 57 getpt: for MeasurementSet input data only: 58 If True, all pointing data are filled. 59 The deafult is False, which makes time to load 60 the MS data faster in some cases. 61 antenna: Antenna selection. integer (id) or string (name 62 or id). 56 63 parallactify: Indcicate that the data had been parallatified. 57 64 Default is taken form rc file. … … 59 66 if average is None: 60 67 average = rcParams['scantable.autoaverage'] 68 if getpt is None: 69 getpt = True 70 if antenna is None: 71 antenna = '' 72 elif type(antenna) == int: 73 antenna = '%s'%antenna 74 elif type(antenna) == list: 75 tmpstr = '' 76 for i in range( len(antenna) ): 77 if type(antenna[i]) == int: 78 tmpstr = tmpstr + ('%s,'%(antenna[i])) 79 elif type(antenna[i]) == str: 80 tmpstr=tmpstr+antenna[i]+',' 81 else: 82 asaplog.push('Bad antenna selection.') 83 print_log('ERROR') 84 return 85 antenna = tmpstr.rstrip(',') 61 86 parallactify = parallactify or rcParams['scantable.parallactify'] 62 87 varlist = vars() 63 88 from asap._asap import stmath 64 self._math = stmath( )89 self._math = stmath( rcParams['insitu'] ) 65 90 if isinstance(filename, Scantable): 66 91 Scantable.__init__(self, filename) … … 73 98 if rcParams['verbose']: 74 99 asaplog.push(s) 100 print_log('ERROR') 75 101 return 76 102 raise IOError(s) … … 80 106 if unit is not None: 81 107 self.set_fluxunit(unit) 82 self.set_freqframe(rcParams['scantable.freqframe']) 108 # do not reset to the default freqframe 109 #self.set_freqframe(rcParams['scantable.freqframe']) 110 elif os.path.isdir(filename) \ 111 and not os.path.exists(filename+'/table.f1'): 112 msg = "The given file '%s'is not a valid " \ 113 "asap table." % (filename) 114 if rcParams['verbose']: 115 #print msg 116 asaplog.push( msg ) 117 print_log( 'ERROR' ) 118 return 119 else: 120 raise IOError(msg) 83 121 else: 84 self._fill([filename], unit, average )122 self._fill([filename], unit, average, getpt, antenna) 85 123 elif (isinstance(filename, list) or isinstance(filename, tuple)) \ 86 124 and isinstance(filename[-1], str): 87 self._fill(filename, unit, average )125 self._fill(filename, unit, average, getpt, antenna) 88 126 self.parallactify(parallactify) 89 127 self._add_history("scantable", varlist) 90 91 @print_log_dec 128 print_log() 129 130 #@print_log_dec 92 131 def save(self, name=None, format=None, overwrite=False): 93 132 """ … … 126 165 msg = "File %s exists." % name 127 166 if rcParams['verbose']: 128 print msg 167 #print msg 168 asaplog.push( msg ) 169 print_log( 'ERROR' ) 129 170 return 130 171 else: … … 137 178 writer = stw(format2) 138 179 writer.write(self, name) 180 print_log() 139 181 return 140 182 … … 164 206 if not _is_valid(scanid): 165 207 if rcParams['verbose']: 166 print "Please specify a scanno to drop from the scantable" 208 #print "Please specify a scanno to drop from the scantable" 209 asaplog.push( 'Please specify a scanno to drop from the scantable' ) 210 print_log( 'ERROR' ) 167 211 return 168 212 else: … … 176 220 except ValueError: 177 221 if rcParams['verbose']: 178 print "Couldn't find any match." 222 #print "Couldn't find any match." 223 print_log() 224 asaplog.push( "Couldn't find any match." ) 225 print_log( 'ERROR' ) 179 226 return 180 227 else: raise … … 184 231 except RuntimeError: 185 232 if rcParams['verbose']: 186 print "Couldn't find any match." 233 #print "Couldn't find any match." 234 print_log() 235 asaplog.push( "Couldn't find any match." ) 236 print_log( 'ERROR' ) 187 237 else: 188 238 raise … … 215 265 if scanid is None: 216 266 if rcParams['verbose']: 217 print "Please specify a scan no or name to " \ 218 "retrieve from the scantable" 267 #print "Please specify a scan no or name to " \ 268 # "retrieve from the scantable" 269 asaplog.push( 'Please specify a scan no or name to retrieve from the scantable' ) 270 print_log( 'ERROR' ) 219 271 return 220 272 else: … … 236 288 msg = "Illegal scanid type, use 'int' or 'list' if ints." 237 289 if rcParams['verbose']: 238 print msg 290 #print msg 291 asaplog.push( msg ) 292 print_log( 'ERROR' ) 239 293 else: 240 294 raise TypeError(msg) 241 295 except RuntimeError: 242 if rcParams['verbose']: print "Couldn't find any match." 296 if rcParams['verbose']: 297 #print "Couldn't find any match." 298 print_log() 299 asaplog.push( "Couldn't find any match." ) 300 print_log( 'ERROR' ) 243 301 else: raise 244 302 … … 266 324 msg = "Illegal file name '%s'." % (filename) 267 325 if rcParams['verbose']: 268 print msg 326 #print msg 327 asaplog.push( msg ) 328 print_log( 'ERROR' ) 269 329 else: 270 330 raise IOError(msg) … … 367 427 self._setselection(selection) 368 428 369 def stats(self, stat='stddev', mask=None): 429 def get_row(self, row=0, insitu=None): 430 """ 431 Select a row in the scantable. 432 Return a scantable with single row. 433 Parameters: 434 row: row no of integration, default is 0. 435 insitu: if False a new scantable is returned. 436 Otherwise, the scaling is done in-situ 437 The default is taken from .asaprc (False) 438 """ 439 if insitu is None: insitu = rcParams['insitu'] 440 if not insitu: 441 workscan = self.copy() 442 else: 443 workscan = self 444 # Select a row 445 sel=selector() 446 sel.set_scans([workscan.getscan(row)]) 447 sel.set_cycles([workscan.getcycle(row)]) 448 sel.set_beams([workscan.getbeam(row)]) 449 sel.set_ifs([workscan.getif(row)]) 450 sel.set_polarisations([workscan.getpol(row)]) 451 sel.set_name(workscan._getsourcename(row)) 452 workscan.set_selection(sel) 453 if not workscan.nrow() == 1: 454 msg = "Cloud not identify single row. %d rows selected."%(workscan.nrow()) 455 raise RuntimeError(msg) 456 del sel 457 if insitu: 458 self._assign(workscan) 459 else: 460 return workscan 461 462 #def stats(self, stat='stddev', mask=None): 463 def stats(self, stat='stddev', mask=None, form='3.3f'): 370 464 """ 371 465 Determine the specified statistic of the current beam/if/pol … … 373 467 channels should be excluded. 374 468 Parameters: 375 stat: 'min', 'max', ' sumsq', 'sum', 'mean'376 ' var', 'stddev', 'avdev', 'rms', 'median'469 stat: 'min', 'max', 'min_abc', 'max_abc', 'sumsq', 'sum', 470 'mean', 'var', 'stddev', 'avdev', 'rms', 'median' 377 471 mask: an optional mask specifying where the statistic 378 472 should be determined. 473 form: format string to print statistic values 379 474 Example: 380 475 scan.set_unit('channel') … … 387 482 "number of channels. Please use setselection() " 388 483 "to select individual IFs") 389 390 statvals = self._math._stats(self, mask, stat) 391 def cb(i): 392 return statvals[i] 393 394 return self._row_callback(cb, stat) 484 rtnabc = False 485 if stat.lower().endswith('_abc'): rtnabc = True 486 getchan = False 487 if stat.lower().startswith('min') or stat.lower().startswith('max'): 488 chan = self._math._minmaxchan(self, mask, stat) 489 getchan = True 490 statvals = [] 491 if not rtnabc: statvals = self._math._stats(self, mask, stat) 492 493 #def cb(i): 494 # return statvals[i] 495 496 #return self._row_callback(cb, stat) 497 498 label=stat 499 #callback=cb 500 out = "" 501 #outvec = [] 502 sep = '-'*50 503 for i in range(self.nrow()): 504 refstr = '' 505 statunit= '' 506 if getchan: 507 qx, qy = self.chan2data(rowno=i, chan=chan[i]) 508 if rtnabc: 509 statvals.append(qx['value']) 510 refstr = ('(value: %'+form) % (qy['value'])+' ['+qy['unit']+'])' 511 statunit= '['+qx['unit']+']' 512 else: 513 refstr = ('(@ %'+form) % (qx['value'])+' ['+qx['unit']+'])' 514 515 tm = self._gettime(i) 516 src = self._getsourcename(i) 517 out += 'Scan[%d] (%s) ' % (self.getscan(i), src) 518 out += 'Time[%s]:\n' % (tm) 519 if self.nbeam(-1) > 1: 520 out += ' Beam[%d] ' % (self.getbeam(i)) 521 if self.nif(-1) > 1: out += ' IF[%d] ' % (self.getif(i)) 522 if self.npol(-1) > 1: out += ' Pol[%d] ' % (self.getpol(i)) 523 #outvec.append(callback(i)) 524 #out += ('= %'+form) % (outvec[i]) +' '+refstr+'\n' 525 out += ('= %'+form) % (statvals[i]) +' '+refstr+'\n' 526 out += sep+"\n" 527 528 if rcParams['verbose']: 529 import os 530 if os.environ.has_key( 'USER' ): 531 usr=os.environ['USER'] 532 else: 533 import commands 534 usr=commands.getoutput( 'whoami' ) 535 tmpfile='/tmp/tmp_'+usr+'_casapy_asap_scantable_stats' 536 f=open(tmpfile,'w') 537 print >> f, sep 538 print >> f, ' %s %s' % (label, statunit) 539 print >> f, sep 540 print >> f, out 541 f.close() 542 f=open(tmpfile,'r') 543 x=f.readlines() 544 f.close() 545 blanc='' 546 asaplog.push(blanc.join(x), False) 547 #for xx in x: 548 # asaplog.push( xx, False ) 549 print_log() 550 return statvals 551 552 def chan2data(self, rowno=0, chan=0): 553 """ 554 Returns channel/frequency/velocity and spectral value 555 at an arbitrary row and channel in the scantable. 556 Parameters: 557 rowno: a row number in the scantable. Default is the 558 first row, i.e. rowno=0 559 chan: a channel in the scantable. Default is the first 560 channel, i.e. pos=0 561 """ 562 if isinstance(rowno, int) and isinstance(chan, int): 563 qx = {'unit': self.get_unit(), 564 'value': self._getabcissa(rowno)[chan]} 565 qy = {'unit': self.get_fluxunit(), 566 'value': self._getspectrum(rowno)[chan]} 567 return qx, qy 395 568 396 569 def stddev(self, mask=None): … … 462 635 out += sep+'\n' 463 636 if rcParams['verbose']: 464 print sep 465 print " %s" % (label) 466 print sep 467 print out 637 asaplog.push(sep) 638 asaplog.push(" %s" % (label)) 639 asaplog.push(sep) 640 asaplog.push(out) 641 print_log() 468 642 return outvec 469 643 … … 474 648 return [callback(i) for i in range(self.nrow())] 475 649 else: 476 if 0 <= row < self.nrow():650 if 0 <= row < self.nrow(): 477 651 return callback(row) 478 652 … … 578 752 return self._get_column(self._getdirectionvec, row) 579 753 580 @print_log_dec754 #@print_log_dec 581 755 def set_unit(self, unit='channel'): 582 756 """ … … 594 768 self._add_history("set_unit", varlist) 595 769 596 @print_log_dec770 #@print_log_dec 597 771 def set_instrument(self, instr): 598 772 """ … … 604 778 self._setInstrument(instr) 605 779 self._add_history("set_instument", vars()) 606 607 @print_log_dec 780 print_log() 781 782 #@print_log_dec 608 783 def set_feedtype(self, feedtype): 609 784 """ … … 614 789 self._setfeedtype(feedtype) 615 790 self._add_history("set_feedtype", vars()) 616 617 @print_log_dec 791 print_log() 792 793 #@print_log_dec 618 794 def set_doppler(self, doppler='RADIO'): 619 795 """ … … 627 803 self._setcoordinfo(inf) 628 804 self._add_history("set_doppler", vars()) 629 630 @print_log_dec 805 print_log() 806 807 #@print_log_dec 631 808 def set_freqframe(self, frame=None): 632 809 """ … … 634 811 Parameters: 635 812 frame: an optional frame type, default 'LSRK'. Valid frames are: 636 ' REST', 'TOPO', 'LSRD', 'LSRK', 'BARY',813 'TOPO', 'LSRD', 'LSRK', 'BARY', 637 814 'GEO', 'GALACTO', 'LGROUP', 'CMB' 638 815 Examples: … … 641 818 frame = frame or rcParams['scantable.freqframe'] 642 819 varlist = vars() 643 valid = ['REST', 'TOPO', 'LSRD', 'LSRK', 'BARY', \ 820 # "REST" is not implemented in casacore 821 #valid = ['REST', 'TOPO', 'LSRD', 'LSRK', 'BARY', \ 822 # 'GEO', 'GALACTO', 'LGROUP', 'CMB'] 823 valid = ['TOPO', 'LSRD', 'LSRK', 'BARY', \ 644 824 'GEO', 'GALACTO', 'LGROUP', 'CMB'] 645 825 … … 652 832 msg = "Please specify a valid freq type. Valid types are:\n", valid 653 833 if rcParams['verbose']: 654 print msg 834 #print msg 835 asaplog.push( msg ) 836 print_log( 'ERROR' ) 655 837 else: 656 838 raise TypeError(msg) 839 print_log() 657 840 658 841 def set_dirframe(self, frame=""): … … 670 853 except RuntimeError, msg: 671 854 if rcParams['verbose']: 672 print msg 855 #print msg 856 print_log() 857 asaplog.push( str(msg) ) 858 print_log( 'ERROR' ) 673 859 else: 674 860 raise … … 698 884 abc = self._getabcissa(rowno) 699 885 lbl = self._getabcissalabel(rowno) 886 print_log() 700 887 return abc, lbl 701 888 702 def flag(self, mask=None ):889 def flag(self, mask=None, unflag=False): 703 890 """ 704 891 Flag the selected data using an optional channel mask. … … 706 893 mask: an optional channel mask, created with create_mask. Default 707 894 (no mask) is all channels. 895 unflag: if True, unflag the data 708 896 """ 709 897 varlist = vars() 710 898 mask = mask or [] 711 899 try: 712 self._flag(mask )900 self._flag(mask, unflag) 713 901 except RuntimeError, msg: 714 902 if rcParams['verbose']: 715 print msg 903 #print msg 904 print_log() 905 asaplog.push( str(msg) ) 906 print_log( 'ERROR' ) 716 907 return 717 908 else: raise 718 909 self._add_history("flag", varlist) 719 910 720 @print_log_dec 911 def flag_row(self, rows=[], unflag=False): 912 """ 913 Flag the selected data in row-based manner. 914 Parameters: 915 rows: list of row numbers to be flagged. Default is no row (must be explicitly specified to execute row-based flagging). 916 unflag: if True, unflag the data. 917 """ 918 varlist = vars() 919 try: 920 self._flag_row(rows, unflag) 921 except RuntimeError, msg: 922 if rcParams['verbose']: 923 print_log() 924 asaplog.push( str(msg) ) 925 print_log('ERROR') 926 return 927 else: raise 928 self._add_history("flag_row", varlist) 929 930 def clip(self, uthres=None, dthres=None, clipoutside=True, unflag=False): 931 """ 932 Flag the selected data outside a specified range (in channel-base) 933 Parameters: 934 uthres: upper threshold. 935 dthres: lower threshold 936 clipoutside: True for flagging data outside the range [dthres:uthres]. 937 False for glagging data inside the range. 938 unflag : if True, unflag the data. 939 """ 940 varlist = vars() 941 try: 942 self._clip(uthres, dthres, clipoutside, unflag) 943 except RuntimeError, msg: 944 if rcParams['verbose']: 945 print_log() 946 asaplog.push(str(msg)) 947 print_log('ERROR') 948 return 949 else: raise 950 self._add_history("clip", varlist) 951 952 #@print_log_dec 721 953 def lag_flag(self, start, end, unit="MHz", insitu=None): 954 #def lag_flag(self, frequency, width=0.0, unit="GHz", insitu=None): 722 955 """ 723 956 Flag the data in 'lag' space by providing a frequency to remove. … … 748 981 except RuntimeError, msg: 749 982 if rcParams['verbose']: 750 print msg 983 #print msg 984 print_log() 985 asaplog.push( str(msg) ) 986 print_log( 'ERROR' ) 751 987 return 752 988 else: raise 753 989 s._add_history("lag_flag", varlist) 990 print_log() 754 991 if insitu: 755 992 self._assign(s) … … 757 994 return s 758 995 759 @print_log_dec996 #@print_log_dec 760 997 def create_mask(self, *args, **kwargs): 761 998 """ … … 819 1056 if kwargs.get('invert'): 820 1057 msk = mask_not(msk) 1058 print_log() 821 1059 return msk 822 1060 823 def get_restfreqs(self): 1061 def get_masklist(self, mask=None, row=0): 1062 """ 1063 Compute and return a list of mask windows, [min, max]. 1064 Parameters: 1065 mask: channel mask, created with create_mask. 1066 row: calcutate the masklist using the specified row 1067 for unit conversions, default is row=0 1068 only necessary if frequency varies over rows. 1069 Returns: 1070 [min, max], [min2, max2], ... 1071 Pairs of start/end points (inclusive)specifying 1072 the masked regions 1073 """ 1074 if not (isinstance(mask,list) or isinstance(mask, tuple)): 1075 raise TypeError("The mask should be list or tuple.") 1076 if len(mask) < 2: 1077 raise TypeError("The mask elements should be > 1") 1078 if self.nchan() != len(mask): 1079 msg = "Number of channels in scantable != number of mask elements" 1080 raise TypeError(msg) 1081 data = self._getabcissa(row) 1082 u = self._getcoordinfo()[0] 1083 if rcParams['verbose']: 1084 if u == "": u = "channel" 1085 msg = "The current mask window unit is %s" % u 1086 i = self._check_ifs() 1087 if not i: 1088 msg += "\nThis mask is only valid for IF=%d" % (self.getif(i)) 1089 asaplog.push(msg) 1090 masklist=[] 1091 ist, ien = None, None 1092 ist, ien=self.get_mask_indices(mask) 1093 if ist is not None and ien is not None: 1094 for i in xrange(len(ist)): 1095 range=[data[ist[i]],data[ien[i]]] 1096 range.sort() 1097 masklist.append([range[0],range[1]]) 1098 return masklist 1099 1100 def get_mask_indices(self, mask=None): 1101 """ 1102 Compute and Return lists of mask start indices and mask end indices. 1103 Parameters: 1104 mask: channel mask, created with create_mask. 1105 Returns: 1106 List of mask start indices and that of mask end indices, 1107 i.e., [istart1,istart2,....], [iend1,iend2,....]. 1108 """ 1109 if not (isinstance(mask,list) or isinstance(mask, tuple)): 1110 raise TypeError("The mask should be list or tuple.") 1111 if len(mask) < 2: 1112 raise TypeError("The mask elements should be > 1") 1113 istart=[] 1114 iend=[] 1115 if mask[0]: istart.append(0) 1116 for i in range(len(mask)-1): 1117 if not mask[i] and mask[i+1]: 1118 istart.append(i+1) 1119 elif mask[i] and not mask[i+1]: 1120 iend.append(i) 1121 if mask[len(mask)-1]: iend.append(len(mask)-1) 1122 if len(istart) != len(iend): 1123 raise RuntimeError("Numbers of mask start != mask end.") 1124 for i in range(len(istart)): 1125 if istart[i] > iend[i]: 1126 raise RuntimeError("Mask start index > mask end index") 1127 break 1128 return istart,iend 1129 1130 # def get_restfreqs(self): 1131 # """ 1132 # Get the restfrequency(s) stored in this scantable. 1133 # The return value(s) are always of unit 'Hz' 1134 # Parameters: 1135 # none 1136 # Returns: 1137 # a list of doubles 1138 # """ 1139 # return list(self._getrestfreqs()) 1140 1141 def get_restfreqs(self, ids=None): 824 1142 """ 825 1143 Get the restfrequency(s) stored in this scantable. 826 1144 The return value(s) are always of unit 'Hz' 827 1145 Parameters: 828 none 1146 ids: (optional) a list of MOLECULE_ID for that restfrequency(s) to 1147 be retrieved 829 1148 Returns: 830 a list of doubles 831 """ 832 return list(self._getrestfreqs()) 833 1149 dictionary containing ids and a list of doubles for each id 1150 """ 1151 if ids is None: 1152 rfreqs={} 1153 idlist = self.getmolnos() 1154 for i in idlist: 1155 rfreqs[i]=list(self._getrestfreqs(i)) 1156 return rfreqs 1157 else: 1158 if type(ids)==list or type(ids)==tuple: 1159 rfreqs={} 1160 for i in ids: 1161 rfreqs[i]=list(self._getrestfreqs(i)) 1162 return rfreqs 1163 else: 1164 return list(self._getrestfreqs(ids)) 1165 #return list(self._getrestfreqs(ids)) 834 1166 835 1167 def set_restfreqs(self, freqs=None, unit='Hz'): 836 1168 """ 1169 ********NEED TO BE UPDATED begin************ 837 1170 Set or replace the restfrequency specified and 838 1171 If the 'freqs' argument holds a scalar, … … 846 1179 E.g. 'freqs=[1e9, 2e9]' would mean IF 0 gets restfreq 1e9 and 847 1180 IF 1 gets restfreq 2e9. 1181 ********NEED TO BE UPDATED end************ 848 1182 You can also specify the frequencies via a linecatalog. 849 1183 … … 853 1187 854 1188 Example: 855 # set the given restfrequency for the whole table1189 # set the given restfrequency for the all currently selected IFs 856 1190 scan.set_restfreqs(freqs=1.4e9) 857 # If thee number of IFs in the data is >= 2 IF0 gets the first 858 # value IF1 the second... 859 scan.set_restfreqs(freqs=[1.4e9, 1.67e9]) 1191 # set multiple restfrequencies to all the selected data 1192 scan.set_restfreqs(freqs=[1.4e9, 1.41e9, 1.42e9]) 1193 # If the number of IFs in the data is >= 2 the IF0 gets the first 1194 # value IF1 the second... NOTE that freqs needs to be 1195 # specified in list of list (e.g. [[],[],...] ). 1196 scan.set_restfreqs(freqs=[[1.4e9],[1.67e9]]) 860 1197 #set the given restfrequency for the whole table (by name) 861 1198 scan.set_restfreqs(freqs="OH1667") … … 877 1214 # simple value 878 1215 if isinstance(freqs, int) or isinstance(freqs, float): 879 self._setrestfreqs(freqs, "",unit) 1216 # TT mod 1217 #self._setrestfreqs(freqs, "",unit) 1218 self._setrestfreqs([freqs], [""],unit) 880 1219 # list of values 881 1220 elif isinstance(freqs, list) or isinstance(freqs, tuple): 882 1221 # list values are scalars 883 1222 if isinstance(freqs[-1], int) or isinstance(freqs[-1], float): 1223 self._setrestfreqs(freqs, [""],unit) 1224 # list values are tuples, (value, name) 1225 elif isinstance(freqs[-1], dict): 1226 #sel = selector() 1227 #savesel = self._getselection() 1228 #iflist = self.getifnos() 1229 #for i in xrange(len(freqs)): 1230 # sel.set_ifs(iflist[i]) 1231 # self._setselection(sel) 1232 # self._setrestfreqs(freqs[i], "",unit) 1233 #self._setselection(savesel) 1234 self._setrestfreqs(freqs["value"], 1235 freqs["name"], "MHz") 1236 elif isinstance(freqs[-1], list) or isinstance(freqs[-1], tuple): 884 1237 sel = selector() 885 1238 savesel = self._getselection() 886 1239 iflist = self.getifnos() 887 for i in xrange(len(freqs)): 888 sel.set_ifs(iflist[i]) 889 self._setselection(sel) 890 self._setrestfreqs(freqs[i], "",unit) 891 self._setselection(savesel) 892 # list values are tuples, (value, name) 893 elif isinstance(freqs[-1], dict): 894 sel = selector() 895 savesel = self._getselection() 896 iflist = self.getifnos() 1240 if len(freqs)>len(iflist): 1241 raise ValueError("number of elements in list of list exeeds the current IF selections") 897 1242 for i in xrange(len(freqs)): 898 1243 sel.set_ifs(iflist[i]) … … 905 1250 sel = selector() 906 1251 savesel = self._getselection() 907 iflist = self.getifnos()908 1252 for i in xrange(freqs.nrow()): 909 1253 sel.set_ifs(iflist[i]) … … 963 1307 msg = "Illegal file name '%s'." % (filename) 964 1308 if rcParams['verbose']: 965 print msg 1309 #print msg 1310 asaplog.push( msg ) 1311 print_log( 'ERROR' ) 966 1312 else: 967 1313 raise IOError(msg) … … 978 1324 # Maths business 979 1325 # 980 @print_log_dec1326 #@print_log_dec 981 1327 def average_time(self, mask=None, scanav=False, weight='tint', align=False): 982 1328 """ … … 1020 1366 except RuntimeError, msg: 1021 1367 if rcParams['verbose']: 1022 print msg 1368 #print msg 1369 print_log() 1370 asaplog.push( str(msg) ) 1371 print_log( 'ERROR' ) 1023 1372 return 1024 1373 else: raise 1025 1374 s._add_history("average_time", varlist) 1375 print_log() 1026 1376 return s 1027 1377 1028 @print_log_dec1378 #@print_log_dec 1029 1379 def convert_flux(self, jyperk=None, eta=None, d=None, insitu=None): 1030 1380 """ … … 1051 1401 s = scantable(self._math._convertflux(self, d, eta, jyperk)) 1052 1402 s._add_history("convert_flux", varlist) 1403 print_log() 1053 1404 if insitu: self._assign(s) 1054 1405 else: return s 1055 1406 1056 @print_log_dec1407 #@print_log_dec 1057 1408 def gain_el(self, poly=None, filename="", method="linear", insitu=None): 1058 1409 """ … … 1103 1454 s = scantable(self._math._gainel(self, poly, filename, method)) 1104 1455 s._add_history("gain_el", varlist) 1456 print_log() 1105 1457 if insitu: 1106 1458 self._assign(s) … … 1108 1460 return s 1109 1461 1110 @print_log_dec1462 #@print_log_dec 1111 1463 def freq_align(self, reftime=None, method='cubic', insitu=None): 1112 1464 """ … … 1130 1482 s = scantable(self._math._freq_align(self, reftime, method)) 1131 1483 s._add_history("freq_align", varlist) 1484 print_log() 1132 1485 if insitu: self._assign(s) 1133 1486 else: return s 1134 1487 1135 @print_log_dec1488 #@print_log_dec 1136 1489 def opacity(self, tau=None, insitu=None): 1137 1490 """ … … 1158 1511 s = scantable(self._math._opacity(self, tau)) 1159 1512 s._add_history("opacity", varlist) 1513 print_log() 1160 1514 if insitu: self._assign(s) 1161 1515 else: return s 1162 1516 1163 @print_log_dec1517 #@print_log_dec 1164 1518 def bin(self, width=5, insitu=None): 1165 1519 """ … … 1176 1530 s = scantable(self._math._bin(self, width)) 1177 1531 s._add_history("bin", varlist) 1532 print_log() 1178 1533 if insitu: 1179 1534 self._assign(s) … … 1181 1536 return s 1182 1537 1183 @print_log_dec1538 #@print_log_dec 1184 1539 def resample(self, width=5, method='cubic', insitu=None): 1185 1540 """ 1186 1541 Return a scan where all spectra have been binned up. 1187 1542 1188 1543 Parameters: 1189 1544 width: The bin width (default=5) in pixels … … 1200 1555 s = scantable(self._math._resample(self, method, width)) 1201 1556 s._add_history("resample", varlist) 1557 print_log() 1202 1558 if insitu: self._assign(s) 1203 1559 else: return s 1204 1560 1205 @print_log_dec1561 #@print_log_dec 1206 1562 def average_pol(self, mask=None, weight='none'): 1207 1563 """ … … 1218 1574 s = scantable(self._math._averagepol(self, mask, weight.upper())) 1219 1575 s._add_history("average_pol", varlist) 1576 print_log() 1220 1577 return s 1221 1578 1222 @print_log_dec1579 #@print_log_dec 1223 1580 def average_beam(self, mask=None, weight='none'): 1224 1581 """ … … 1235 1592 s = scantable(self._math._averagebeams(self, mask, weight.upper())) 1236 1593 s._add_history("average_beam", varlist) 1594 print_log() 1237 1595 return s 1238 1596 … … 1249 1607 self._add_history("parallactify", varlist) 1250 1608 1251 @print_log_dec1609 #@print_log_dec 1252 1610 def convert_pol(self, poltype=None): 1253 1611 """ … … 1263 1621 except RuntimeError, msg: 1264 1622 if rcParams['verbose']: 1265 print msg 1623 #print msg 1624 print_log() 1625 asaplog.push( str(msg) ) 1626 print_log( 'ERROR' ) 1266 1627 return 1267 1628 else: 1268 1629 raise 1269 1630 s._add_history("convert_pol", varlist) 1631 print_log() 1270 1632 return s 1271 1633 1272 @print_log_dec1273 def smooth(self, kernel="hanning", width=5.0, order=2, insitu=None):1634 #@print_log_dec 1635 def smooth(self, kernel="hanning", width=5.0, order=2, plot=False, insitu=None): 1274 1636 """ 1275 1637 Smooth the spectrum by the specified kernel (conserving flux). … … 1286 1648 specify the order of the polnomial. Ignored by all other 1287 1649 kernels. 1650 plot: plot the original and the smoothed spectra. 1651 In this each indivual fit has to be approved, by 1652 typing 'y' or 'n' 1288 1653 insitu: if False a new scantable is returned. 1289 1654 Otherwise, the scaling is done in-situ … … 1295 1660 self._math._setinsitu(insitu) 1296 1661 varlist = vars() 1662 1663 if plot: orgscan = self.copy() 1664 1297 1665 s = scantable(self._math._smooth(self, kernel.lower(), width, order)) 1298 1666 s._add_history("smooth", varlist) 1667 1668 if plot: 1669 if rcParams['plotter.gui']: 1670 from asap.asaplotgui import asaplotgui as asaplot 1671 else: 1672 from asap.asaplot import asaplot 1673 self._p=asaplot() 1674 self._p.set_panels() 1675 ylab=s._get_ordinate_label() 1676 #self._p.palette(0,["#777777","red"]) 1677 for r in xrange(s.nrow()): 1678 xsm=s._getabcissa(r) 1679 ysm=s._getspectrum(r) 1680 xorg=orgscan._getabcissa(r) 1681 yorg=orgscan._getspectrum(r) 1682 self._p.clear() 1683 self._p.hold() 1684 self._p.set_axes('ylabel',ylab) 1685 self._p.set_axes('xlabel',s._getabcissalabel(r)) 1686 self._p.set_axes('title',s._getsourcename(r)) 1687 self._p.set_line(label='Original',color="#777777") 1688 self._p.plot(xorg,yorg) 1689 self._p.set_line(label='Smoothed',color="red") 1690 self._p.plot(xsm,ysm) 1691 ### Ugly part for legend 1692 for i in [0,1]: 1693 self._p.subplots[0]['lines'].append([self._p.subplots[0]['axes'].lines[i]]) 1694 self._p.release() 1695 ### Ugly part for legend 1696 self._p.subplots[0]['lines']=[] 1697 res = raw_input("Accept smoothing ([y]/n): ") 1698 if res.upper() == 'N': 1699 s._setspectrum(yorg, r) 1700 self._p.unmap() 1701 self._p = None 1702 del orgscan 1703 1704 print_log() 1299 1705 if insitu: self._assign(s) 1300 1706 else: return s 1301 1707 1302 @print_log_dec1708 #@print_log_dec 1303 1709 def poly_baseline(self, mask=None, order=0, plot=False, uselin=False, 1304 1710 insitu=None): … … 1321 1727 """ 1322 1728 if insitu is None: insitu = rcParams['insitu'] 1729 if not insitu: 1730 workscan = self.copy() 1731 else: 1732 workscan = self 1323 1733 varlist = vars() 1324 1734 if mask is None: 1325 1735 mask = [True for i in xrange(self.nchan(-1))] 1736 1326 1737 from asap.asapfitter import fitter 1327 1738 try: 1328 1739 f = fitter() 1329 f.set_scan(self, mask)1330 #f.set_function(poly=order)1331 1740 if uselin: 1332 1741 f.set_function(lpoly=order) 1333 1742 else: 1334 1743 f.set_function(poly=order) 1335 s = f.auto_fit(insitu, plot=plot) 1336 s._add_history("poly_baseline", varlist) 1337 if insitu: self._assign(s) 1338 else: return s 1744 1745 rows = range(workscan.nrow()) 1746 if len(rows) > 0: 1747 self.blpars = [] 1748 1749 for r in rows: 1750 # take into account flagtra info (CAS-1434) 1751 flagtra = workscan._getmask(r) 1752 actualmask = mask[:] 1753 if len(actualmask) == 0: 1754 actualmask = list(flagtra[:]) 1755 else: 1756 if len(actualmask) != len(flagtra): 1757 raise RuntimeError, "Mask and flagtra have different length" 1758 else: 1759 for i in range(0, len(actualmask)): 1760 actualmask[i] = actualmask[i] and flagtra[i] 1761 f.set_scan(workscan, actualmask) 1762 f.x = workscan._getabcissa(r) 1763 f.y = workscan._getspectrum(r) 1764 f.data = None 1765 f.fit() 1766 if plot: 1767 f.plot(residual=True) 1768 x = raw_input("Accept fit ( [y]/n ): ") 1769 if x.upper() == 'N': 1770 self.blpars.append(None) 1771 continue 1772 workscan._setspectrum(f.fitter.getresidual(), r) 1773 self.blpars.append(f.get_parameters()) 1774 1775 if plot: 1776 f._p.unmap() 1777 f._p = None 1778 workscan._add_history("poly_baseline", varlist) 1779 print_log() 1780 if insitu: self._assign(workscan) 1781 else: return workscan 1339 1782 except RuntimeError: 1340 1783 msg = "The fit failed, possibly because it didn't converge." 1341 1784 if rcParams['verbose']: 1342 print msg 1785 #print msg 1786 print_log() 1787 asaplog.push( str(msg) ) 1788 print_log( 'ERROR' ) 1343 1789 return 1344 1790 else: 1345 1791 raise RuntimeError(msg) 1792 1346 1793 1347 1794 def auto_poly_baseline(self, mask=[], edge=(0, 0), order=0, … … 1427 1874 1428 1875 rows = range(workscan.nrow()) 1876 # Save parameters of baseline fits & masklists as a class attribute. 1877 # NOTICE: It does not reflect changes in scantable! 1878 if len(rows) > 0: 1879 self.blpars=[] 1880 self.masklists=[] 1429 1881 asaplog.push("Processing:") 1430 1882 for r in rows: … … 1441 1893 curedge = edge[workscan.getif(r)] 1442 1894 1895 # take into account flagtra info (CAS-1434) 1896 flagtra = workscan._getmask(r) 1897 actualmask = mask[:] 1898 if len(actualmask) == 0: 1899 actualmask = list(flagtra[:]) 1900 else: 1901 if len(actualmask) != len(flagtra): 1902 raise RuntimeError, "Mask and flagtra have different length" 1903 else: 1904 for i in range(0, len(actualmask)): 1905 actualmask[i] = actualmask[i] and flagtra[i] 1906 1443 1907 # setup line finder 1444 fl.find_lines(r, mask, curedge) 1445 f.set_data(workscan._getabcissa(r), workscan._getspectrum(r), 1446 mask_and(workscan._getmask(r), fl.get_mask())) 1908 fl.find_lines(r, actualmask, curedge) 1909 outmask=fl.get_mask() 1910 f.set_scan(workscan, fl.get_mask()) 1911 f.x = workscan._getabcissa(r) 1912 f.y = workscan._getspectrum(r) 1913 f.data = None 1447 1914 f.fit() 1448 x = f.get_parameters() 1915 1916 # Show mask list 1917 masklist=workscan.get_masklist(fl.get_mask(),row=r) 1918 msg = "mask range: "+str(masklist) 1919 asaplog.push(msg, False) 1920 1449 1921 if plot: 1450 1922 f.plot(residual=True) 1451 1923 x = raw_input("Accept fit ( [y]/n ): ") 1452 1924 if x.upper() == 'N': 1925 self.blpars.append(None) 1926 self.masklists.append(None) 1453 1927 continue 1928 1454 1929 workscan._setspectrum(f.fitter.getresidual(), r) 1930 self.blpars.append(f.get_parameters()) 1931 self.masklists.append(masklist) 1455 1932 if plot: 1456 1933 f._p.unmap() … … 1462 1939 return workscan 1463 1940 1464 @print_log_dec1941 #@print_log_dec 1465 1942 def rotate_linpolphase(self, angle): 1466 1943 """ … … 1476 1953 self._math._rotate_linpolphase(self, angle) 1477 1954 self._add_history("rotate_linpolphase", varlist) 1955 print_log() 1478 1956 return 1479 1957 1480 @print_log_dec1958 #@print_log_dec 1481 1959 def rotate_xyphase(self, angle): 1482 1960 """ … … 1492 1970 self._math._rotate_xyphase(self, angle) 1493 1971 self._add_history("rotate_xyphase", varlist) 1972 print_log() 1494 1973 return 1495 1974 1496 @print_log_dec1975 #@print_log_dec 1497 1976 def swap_linears(self): 1498 1977 """ … … 1503 1982 self._math._swap_linears(self) 1504 1983 self._add_history("swap_linears", varlist) 1984 print_log() 1505 1985 return 1506 1986 1507 @print_log_dec1987 #@print_log_dec 1508 1988 def invert_phase(self): 1509 1989 """ … … 1513 1993 self._math._invert_phase(self) 1514 1994 self._add_history("invert_phase", varlist) 1995 print_log() 1515 1996 return 1516 1997 1517 @print_log_dec1998 #@print_log_dec 1518 1999 def add(self, offset, insitu=None): 1519 2000 """ … … 1530 2011 s = scantable(self._math._unaryop(self, offset, "ADD", False)) 1531 2012 s._add_history("add", varlist) 2013 print_log() 1532 2014 if insitu: 1533 2015 self._assign(s) … … 1535 2017 return s 1536 2018 1537 @print_log_dec2019 #@print_log_dec 1538 2020 def scale(self, factor, tsys=True, insitu=None): 1539 2021 """ 1540 2022 Return a scan where all spectra are scaled by the give 'factor' 1541 2023 Parameters: 1542 factor: the scaling factor 2024 factor: the scaling factor (float or 1D float list) 1543 2025 insitu: if False a new scantable is returned. 1544 2026 Otherwise, the scaling is done in-situ … … 1550 2032 self._math._setinsitu(insitu) 1551 2033 varlist = vars() 1552 s = scantable(self._math._unaryop(self, factor, "MUL", tsys)) 2034 s = None 2035 import numpy 2036 if isinstance(factor, list) or isinstance(factor, numpy.ndarray): 2037 if isinstance(factor[0], list) or isinstance(factor[0], numpy.ndarray): 2038 from asapmath import _array2dOp 2039 s = _array2dOp( self.copy(), factor, "MUL", tsys ) 2040 else: 2041 s = scantable( self._math._arrayop( self.copy(), factor, "MUL", tsys ) ) 2042 else: 2043 s = scantable(self._math._unaryop(self.copy(), factor, "MUL", tsys)) 1553 2044 s._add_history("scale", varlist) 2045 print_log() 1554 2046 if insitu: 1555 2047 self._assign(s) … … 1593 2085 self._add_history("set_sourcetype", varlist) 1594 2086 1595 @print_log_dec1596 def auto_quotient(self, preserve=True, mode='paired' ):2087 #@print_log_dec 2088 def auto_quotient(self, preserve=True, mode='paired', verify=False): 1597 2089 """ 1598 2090 This function allows to build quotients automatically. 1599 It assumes the observation to have the same num er of2091 It assumes the observation to have the same number of 1600 2092 "ons" and "offs" 1601 2093 Parameters: … … 1634 2126 s = scantable(self._math._auto_quotient(self, mode, preserve)) 1635 2127 s._add_history("auto_quotient", varlist) 2128 print_log() 1636 2129 return s 1637 2130 1638 @print_log_dec2131 #@print_log_dec 1639 2132 def mx_quotient(self, mask = None, weight='median', preserve=True): 1640 2133 """ … … 1656 2149 q = quotient(on, off, preserve) 1657 2150 q._add_history("mx_quotient", varlist) 2151 print_log() 1658 2152 return q 1659 2153 1660 @print_log_dec2154 #@print_log_dec 1661 2155 def freq_switch(self, insitu=None): 1662 2156 """ … … 1674 2168 s = scantable(self._math._freqswitch(self)) 1675 2169 s._add_history("freq_switch", varlist) 2170 print_log() 1676 2171 if insitu: self._assign(s) 1677 2172 else: return s 1678 2173 1679 @print_log_dec2174 #@print_log_dec 1680 2175 def recalc_azel(self): 1681 2176 """ … … 1688 2183 self._recalcazel() 1689 2184 self._add_history("recalc_azel", varlist) 2185 print_log() 1690 2186 return 1691 2187 1692 @print_log_dec2188 #@print_log_dec 1693 2189 def __add__(self, other): 1694 varlist = vars() 1695 s = None 1696 if isinstance(other, scantable): 1697 s = scantable(self._math._binaryop(self, other, "ADD")) 1698 elif isinstance(other, float): 1699 s = scantable(self._math._unaryop(self, other, "ADD", False)) 1700 else: 1701 raise TypeError("Other input is not a scantable or float value") 1702 s._add_history("operator +", varlist) 1703 return s 1704 1705 @print_log_dec 2190 """ 2191 implicit on all axes and on Tsys 2192 """ 2193 return self._operation( other, "ADD" ) 2194 2195 #@print_log_dec 1706 2196 def __sub__(self, other): 1707 2197 """ 1708 2198 implicit on all axes and on Tsys 1709 2199 """ 1710 varlist = vars() 1711 s = None 1712 if isinstance(other, scantable): 1713 s = scantable(self._math._binaryop(self, other, "SUB")) 1714 elif isinstance(other, float): 1715 s = scantable(self._math._unaryop(self, other, "SUB", False)) 1716 else: 1717 raise TypeError("Other input is not a scantable or float value") 1718 s._add_history("operator -", varlist) 1719 return s 1720 1721 @print_log_dec 2200 return self._operation( other, 'SUB' ) 2201 2202 #@print_log_dec 1722 2203 def __mul__(self, other): 1723 2204 """ 1724 2205 implicit on all axes and on Tsys 1725 2206 """ 1726 varlist = vars() 1727 s = None 1728 if isinstance(other, scantable): 1729 s = scantable(self._math._binaryop(self, other, "MUL")) 1730 elif isinstance(other, float): 1731 s = scantable(self._math._unaryop(self, other, "MUL", False)) 1732 else: 1733 raise TypeError("Other input is not a scantable or float value") 1734 s._add_history("operator *", varlist) 1735 return s 1736 1737 1738 @print_log_dec 2207 return self._operation( other, 'MUL' ) 2208 2209 #@print_log_dec 1739 2210 def __div__(self, other): 1740 2211 """ 1741 2212 implicit on all axes and on Tsys 1742 2213 """ 1743 varlist = vars() 1744 s = None 1745 if isinstance(other, scantable): 1746 s = scantable(self._math._binaryop(self, other, "DIV")) 1747 elif isinstance(other, float): 1748 if other == 0.0: 1749 raise ZeroDivisionError("Dividing by zero is not recommended") 1750 s = scantable(self._math._unaryop(self, other, "DIV", False)) 1751 else: 1752 raise TypeError("Other input is not a scantable or float value") 1753 s._add_history("operator /", varlist) 1754 return s 2214 return self._operation( other, 'DIV' ) 1755 2215 1756 2216 def get_fit(self, row=0): … … 1765 2225 fit = asapfit(self._getfit(row)) 1766 2226 if rcParams['verbose']: 1767 print fit 2227 #print fit 2228 asaplog.push( '%s' %(fit) ) 2229 print_log() 1768 2230 return 1769 2231 else: … … 1862 2324 return (sum(nchans)/len(nchans) == nchans[0]) 1863 2325 1864 def _fill(self, names, unit, average ):2326 def _fill(self, names, unit, average, getpt, antenna): 1865 2327 import os 1866 2328 from asap._asap import stfiller … … 1874 2336 if rcParams['verbose']: 1875 2337 asaplog.push(msg) 1876 print asaplog.pop().strip() 2338 #print asaplog.pop().strip() 2339 print_log( 'ERROR' ) 1877 2340 return 1878 2341 raise IOError(msg) … … 1889 2352 asaplog.push(msg, False) 1890 2353 print_log() 1891 r._open(name, -1, -1)2354 r._open(name, antenna, -1, -1, getpt) 1892 2355 r._read() 1893 2356 if average: … … 1901 2364 if unit is not None: 1902 2365 self.set_fluxunit(unit) 1903 self.set_freqframe(rcParams['scantable.freqframe'])2366 #self.set_freqframe(rcParams['scantable.freqframe']) 1904 2367 1905 2368 def __getitem__(self, key): … … 1926 2389 for i in range(len(self)): 1927 2390 yield self[i] 2391 2392 def _operation(self, other, opmode): 2393 varlist = vars() 2394 s = None 2395 import numpy 2396 if isinstance(other, scantable): 2397 s = scantable(self._math._binaryop(self.copy(), other, opmode)) 2398 elif isinstance(other, float) or isinstance(other, int): 2399 if opmode == 'DIV' and float(other) == 0.0: 2400 raise ZeroDivisionError("Dividing by zero is not recommended") 2401 s = scantable(self._math._unaryop(self.copy(), other, opmode, False)) 2402 elif isinstance(other, list) or isinstance(other, numpy.ndarray): 2403 if isinstance(other[0], list) or isinstance(other[0], numpy.ndarray): 2404 from asapmath import _array2dOp 2405 s = _array2dOp( self.copy(), other, opmode, False ) 2406 else: 2407 s = scantable(self._math._arrayop(self.copy(), other, opmode, False)) 2408 else: 2409 raise TypeError("Other input is not a scantable or float value or float list") 2410 opdic = {} 2411 opdic['ADD'] = '+' 2412 opdic['SUB'] = '-' 2413 opdic['MUL'] = '*' 2414 opdic['DIV'] = '/' 2415 s._add_history("operator %s" % opdic[opmode], varlist) 2416 print_log() 2417 return s 2418 2419
Note: See TracChangeset
for help on using the changeset viewer.