Changeset 1118
- Timestamp:
- 08/09/06 16:54:14 (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/python/scantable.py
r1115 r1118 1 1 from asap._asap import Scantable 2 2 from asap import rcParams 3 from asap import print_log, asaplog 3 from asap import print_log 4 from asap import asaplog 4 5 from asap import selector 5 from numarray import ones ,zeros6 import sys6 from numarray import ones 7 from numarray import zeros 7 8 8 9 class scantable(Scantable): … … 32 33 if average is None: 33 34 average = rcParams['scantable.autoaverage'] 34 varlist = vars()35 #varlist = vars() 35 36 from asap._asap import stmath 36 37 self._math = stmath() … … 38 39 Scantable.__init__(self, filename) 39 40 else: 40 if isinstance(filename, str):41 if isinstance(filename, str): 41 42 import os.path 42 43 filename = os.path.expandvars(filename) … … 50 51 raise IOError(s) 51 52 if os.path.isdir(filename) \ 52 and not os.path.exists(filename+'/table.f1'):53 and not os.path.exists(filename+'/table.f1'): 53 54 # crude check if asap table 54 55 if os.path.exists(filename+'/table.info'): 55 Scantable.__init__(self, filename, rcParams['scantable.storage']=='disk') 56 ondisk = rcParams['scantable.storage'] == 'disk' 57 Scantable.__init__(self, filename, ondisk) 56 58 if unit is not None: 57 59 self.set_fluxunit(unit) 58 60 self.set_freqframe(rcParams['scantable.freqframe']) 59 61 else: 60 msg = "The given file '%s'is not a valid asap table." % (filename) 62 msg = "The given file '%s'is not a valid " \ 63 "asap table." % (filename) 61 64 if rcParams['verbose']: 62 65 print msg … … 65 68 raise IOError(msg) 66 69 else: 67 self._fill([filename], unit, average)68 elif (isinstance(filename, list) or isinstance(filename,tuple)) \70 self._fill([filename], unit, average) 71 elif (isinstance(filename, list) or isinstance(filename, tuple)) \ 69 72 and isinstance(filename[-1], str): 70 73 self._fill(filename, unit, average) … … 90 93 Example: 91 94 scan.save('myscan.asap') 92 scan.save('myscan.sdfits', 'SDFITS')95 scan.save('myscan.sdfits', 'SDFITS') 93 96 """ 94 97 from os import path 95 98 if format is None: format = rcParams['scantable.save'] 96 99 suffix = '.'+format.lower() 97 if name is None or name == "":100 if name is None or name == "": 98 101 name = 'scantable'+suffix 99 from asap import asaplog100 102 msg = "No filename given. Using default name %s..." % name 101 103 asaplog.push(msg) … … 114 116 else: 115 117 from asap._asap import stwriter as stw 116 w = stw(format2)117 w .write(self, name)118 writer = stw(format2) 119 writer.write(self, name) 118 120 print_log() 119 121 return … … 150 152 allscans = unique([ self.getscan(i) for i in range(self.nrow())]) 151 153 for sid in scanid: allscans.remove(sid) 152 if len(allscans) == 0: raise ValueError("Can't remove all scans") 154 if len(allscans) == 0: 155 raise ValueError("Can't remove all scans") 153 156 except ValueError: 154 157 if rcParams['verbose']: … … 165 168 return scantable(scopy) 166 169 except RuntimeError: 167 if rcParams['verbose']: print "Couldn't find any match." 168 else: raise 170 if rcParams['verbose']: 171 print "Couldn't find any match." 172 else: 173 raise 169 174 170 175 … … 183 188 refscans = scan.get_scan('*_R') 184 189 # get a susbset of scans by scanno (as listed in scan.summary()) 185 newscan = scan.get_scan([0, 2,7,10])190 newscan = scan.get_scan([0, 2, 7, 10]) 186 191 """ 187 192 if scanid is None: 188 193 if rcParams['verbose']: 189 print "Please specify a scan no or name to retrieve from the scantable" 194 print "Please specify a scan no or name to " \ 195 "retrieve from the scantable" 190 196 return 191 197 else: … … 224 230 225 231 def __str__(self): 226 return Scantable._summary(self, True)232 return Scantable._summary(self, True) 227 233 228 234 def summary(self, filename=None): … … 282 288 Examples: 283 289 sel = selector() # create a selection object 284 self.set_scans([0, 3]) # select SCANNO 0 and 3290 self.set_scans([0, 3]) # select SCANNO 0 and 3 285 291 scan.set_selection(sel) # set the selection 286 292 scan.summary() # will only print summary of scanno 0 an 3 … … 288 294 """ 289 295 self._setselection(selection) 290 291 def set_cursor(self, beam=0, IF=0, pol=0):292 print "DEPRECATED - use set_selection"293 294 def get_cursor(self):295 print "DEPRECATED - use get_selection"296 296 297 297 def stats(self, stat='stddev', mask=None): … … 307 307 Example: 308 308 scan.set_unit('channel') 309 msk = scan.create_mask([100, 200],[500,600])309 msk = scan.create_mask([100, 200], [500, 600]) 310 310 scan.stats(stat='mean', mask=m) 311 311 """ 312 from numarray import array,zeros,Float313 312 if mask == None: 314 313 mask = [] 315 axes = ['Beam', 'IF','Pol','Time']314 axes = ['Beam', 'IF', 'Pol', 'Time'] 316 315 if not self._check_ifs(): 317 raise ValueError("Cannot apply mask as the IFs have different number of channels" 318 "Please use setselection() to select individual IFs") 316 raise ValueError("Cannot apply mask as the IFs have different " 317 "number of channels. Please use setselection() " 318 "to select individual IFs") 319 319 320 320 statvals = self._math._stats(self, mask, stat) … … 341 341 if rcParams['verbose']: 342 342 print "--------------------------------------------------" 343 print " ", stat343 print " ", stat 344 344 print "--------------------------------------------------" 345 345 print out 346 retval = { 'axesnames': ['scanno', 'beamno','ifno','polno','cycleno'],346 retval = { 'axesnames': ['scanno', 'beamno', 'ifno', 'polno', 'cycleno'], 347 347 'axes' : axes, 348 348 'data': statvals} 349 349 return retval 350 350 351 def stddev(self, mask=None):351 def stddev(self, mask=None): 352 352 """ 353 353 Determine the standard deviation of the current beam/if/pol … … 360 360 Example: 361 361 scan.set_unit('channel') 362 msk = scan.create_mask([100, 200],[500,600])362 msk = scan.create_mask([100, 200], [500, 600]) 363 363 scan.stddev(mask=m) 364 364 """ 365 return self.stats(stat='stddev', mask=mask);365 return self.stats(stat='stddev', mask=mask); 366 366 367 367 … … 385 385 def _row_callback(self, callback, label): 386 386 axes = [] 387 axesnames = ['scanno', 'beamno','ifno','polno','cycleno']387 axesnames = ['scanno', 'beamno', 'ifno', 'polno', 'cycleno'] 388 388 out = "" 389 outvec = []389 outvec = [] 390 390 for i in range(self.nrow()): 391 391 axis = [] … … 495 495 Parameters: 496 496 unit: optional unit, default is 'channel' 497 one of '*Hz', 'km/s','channel', ''498 """ 499 varlist = vars() 500 if unit in ['', 'pixel', 'channel']:497 one of '*Hz', 'km/s', 'channel', '' 498 """ 499 varlist = vars() 500 if unit in ['', 'pixel', 'channel']: 501 501 unit = '' 502 502 inf = list(self._getcoordinfo()) 503 503 inf[0] = unit 504 504 self._setcoordinfo(inf) 505 self._add_history("set_unit", varlist)505 self._add_history("set_unit", varlist) 506 506 507 507 def set_instrument(self, instr): … … 513 513 """ 514 514 self._setInstrument(instr) 515 self._add_history("set_instument", vars())515 self._add_history("set_instument", vars()) 516 516 print_log() 517 517 … … 526 526 inf[2] = doppler 527 527 self._setcoordinfo(inf) 528 self._add_history("set_doppler", vars())528 self._add_history("set_doppler", vars()) 529 529 print_log() 530 530 … … 534 534 Parameters: 535 535 frame: an optional frame type, default 'LSRK'. Valid frames are: 536 'REST', 'TOPO','LSRD','LSRK','BARY',537 'GEO', 'GALACTO','LGROUP','CMB'536 'REST', 'TOPO', 'LSRD', 'LSRK', 'BARY', 537 'GEO', 'GALACTO', 'LGROUP', 'CMB' 538 538 Examples: 539 539 scan.set_freqframe('BARY') … … 541 541 if frame is None: frame = rcParams['scantable.freqframe'] 542 542 varlist = vars() 543 valid = ['REST', 'TOPO','LSRD','LSRK','BARY', \544 'GEO', 'GALACTO','LGROUP','CMB']543 valid = ['REST', 'TOPO', 'LSRD', 'LSRK', 'BARY', \ 544 'GEO', 'GALACTO', 'LGROUP', 'CMB'] 545 545 546 546 if frame in valid: … … 548 548 inf[1] = frame 549 549 self._setcoordinfo(inf) 550 self._add_history("set_freqframe", varlist)550 self._add_history("set_freqframe", varlist) 551 551 else: 552 msg = "Please specify a valid freq type. Valid types are:\n", valid552 msg = "Please specify a valid freq type. Valid types are:\n", valid 553 553 if rcParams['verbose']: 554 554 print msg … … 569 569 try: 570 570 Scantable.set_dirframe(self, frame) 571 except RuntimeError, msg:571 except RuntimeError, msg: 572 572 if rcParams['verbose']: 573 573 print msg 574 574 else: 575 575 raise 576 self._add_history("set_dirframe", varlist)576 self._add_history("set_dirframe", varlist) 577 577 578 578 def get_unit(self): … … 603 603 return abc, lbl 604 604 605 def flag(self, mask= []):605 def flag(self, mask=None): 606 606 """ 607 607 Flag the selected data using an optional channel mask. … … 611 611 """ 612 612 varlist = vars() 613 if mask is None: 614 mask = [] 613 615 try: 614 616 self._flag(mask) 615 except RuntimeError, msg:617 except RuntimeError, msg: 616 618 if rcParams['verbose']: 617 619 print msg … … 623 625 def create_mask(self, *args, **kwargs): 624 626 """ 625 Compute and return a mask based on [min, max] windows.627 Compute and return a mask based on [min, max] windows. 626 628 The specified windows are to be INCLUDED, when the mask is 627 629 applied. 628 630 Parameters: 629 [min, max],[min2,max2],...631 [min, max], [min2, max2], ... 630 632 Pairs of start/end points (inclusive)specifying the regions 631 633 to be masked … … 639 641 scan.set_unit('channel') 640 642 a) 641 msk = scan.create_mask([400, 500],[800,900])643 msk = scan.create_mask([400, 500], [800, 900]) 642 644 # masks everything outside 400 and 500 643 645 # and 800 and 900 in the unit 'channel' 644 646 645 647 b) 646 msk = scan.create_mask([400, 500],[800,900], invert=True)648 msk = scan.create_mask([400, 500], [800, 900], invert=True) 647 649 # masks the regions between 400 and 500 648 650 # and 800 and 900 in the unit 'channel' 649 651 c) 650 652 mask only channel 400 651 msk = scan.create_mask([400, 400])653 msk = scan.create_mask([400, 400]) 652 654 """ 653 655 row = 0 … … 658 660 if rcParams['verbose']: 659 661 if u == "": u = "channel" 660 from asap import asaplog661 662 msg = "The current mask window unit is %s" % u 662 if not self._check_ifs(): 663 i = self._check_ifs() 664 if not i: 663 665 msg += "\nThis mask is only valid for IF=%d" % (self.getif(i)) 664 666 asaplog.push(msg) … … 667 669 # test if args is a 'list' or a 'normal *args - UGLY!!! 668 670 669 ws = (isinstance(args[-1][-1],int) or isinstance(args[-1][-1],float)) and args or args[0] 671 ws = (isinstance(args[-1][-1], int) or isinstance(args[-1][-1], float)) \ 672 and args or args[0] 670 673 for window in ws: 671 674 if (len(window) != 2 or window[0] > window[1] ): 672 raise TypeError("A window needs to be defined as [min, max]")675 raise TypeError("A window needs to be defined as [min, max]") 673 676 for i in range(n): 674 677 if data[i] >= window[0] and data[i] <= window[1]: … … 704 707 the restfrequency set per IF according 705 708 to the corresponding value you give in the 'freqs' vector. 706 E.g. 'freqs=[1e9, 2e9]' would mean IF 0 gets restfreq 1e9 and709 E.g. 'freqs=[1e9, 2e9]' would mean IF 0 gets restfreq 1e9 and 707 710 IF 1 gets restfreq 2e9. 708 711 You can also specify the frequencies via known line names … … 717 720 # If thee number of IFs in the data is >= 2 the IF0 gets the first 718 721 # value IF1 the second... 719 scan.set_restfreqs(freqs=[1.4e9, 1.67e9])722 scan.set_restfreqs(freqs=[1.4e9, 1.67e9]) 720 723 #set the given restfrequency for the whole table (by name) 721 724 scan.set_restfreqs(freqs="OH1667") … … 736 739 737 740 t = type(freqs) 738 if isinstance(freqs, int) or isinstance(freqs, float):739 self._setrestfreqs(freqs, unit)740 elif isinstance(freqs, list) or isinstance(freqs, tuple):741 if isinstance(freqs[-1], int) or isinstance(freqs[-1], float):741 if isinstance(freqs, int) or isinstance(freqs, float): 742 self._setrestfreqs(freqs, unit) 743 elif isinstance(freqs, list) or isinstance(freqs, tuple): 744 if isinstance(freqs[-1], int) or isinstance(freqs[-1], float): 742 745 sel = selector() 743 746 savesel = self._getselection() … … 771 774 for i in items: 772 775 s = i.split("=") 773 out += "\n %s = %s" % (s[0], s[1])776 out += "\n %s = %s" % (s[0], s[1]) 774 777 out += "\n"+"-"*80 775 778 try: … … 814 817 if scanav: scanav = "SCAN" 815 818 else: scanav = "NONE" 816 scan = (self, )819 scan = (self, ) 817 820 try: 818 if align: 819 scan = (self.freq_align(insitu=False),) 820 s = None 821 if weight.upper() == 'MEDIAN': 822 s = scantable(self._math._averagechannel(scan[0], 'MEDIAN', scanav)) 823 else: 824 s = scantable(self._math._average(scan, mask, weight.upper(), 825 scanav)) 826 except RuntimeError,msg: 821 if align: 822 scan = (self.freq_align(insitu=False), ) 823 s = None 824 if weight.upper() == 'MEDIAN': 825 s = scantable(self._math._averagechannel(scan[0], 'MEDIAN', 826 scanav)) 827 else: 828 s = scantable(self._math._average(scan, mask, weight.upper(), 829 scanav)) 830 except RuntimeError, msg: 827 831 if rcParams['verbose']: 828 832 print msg … … 908 912 varlist = vars() 909 913 if poly is None: 910 poly = ()914 poly = () 911 915 from os.path import expandvars 912 916 filename = expandvars(filename) … … 975 979 varlist = vars() 976 980 s = scantable(self._math._bin(self, width)) 977 s._add_history("bin", varlist)981 s._add_history("bin", varlist) 978 982 print_log() 979 983 if insitu: self._assign(s) … … 996 1000 varlist = vars() 997 1001 s = scantable(self._math._resample(self, method, width)) 998 s._add_history("resample", varlist)1002 s._add_history("resample", varlist) 999 1003 print_log() 1000 1004 if insitu: self._assign(s) … … 1016 1020 mask = () 1017 1021 s = scantable(self._math._averagepol(self, mask, weight.upper())) 1018 s._add_history("average_pol", varlist)1022 s._add_history("average_pol", varlist) 1019 1023 print_log() 1020 1024 return s … … 1030 1034 try: 1031 1035 s = scantable(self._math._convertpol(self, poltype)) 1032 except RuntimeError, msg:1036 except RuntimeError, msg: 1033 1037 if rcParams['verbose']: 1034 print msg1035 return1038 print msg 1039 return 1036 1040 else: 1037 1041 raise 1038 s._add_history("convert_pol", varlist)1042 s._add_history("convert_pol", varlist) 1039 1043 print_log() 1040 1044 return s … … 1061 1065 self._math._setinsitu(insitu) 1062 1066 varlist = vars() 1063 s = scantable(self._math._smooth(self, kernel.lower(),width))1067 s = scantable(self._math._smooth(self, kernel.lower(), width)) 1064 1068 s._add_history("smooth", varlist) 1065 1069 print_log() … … 1089 1093 varlist = vars() 1090 1094 if mask is None: 1091 from numarray import ones1092 1095 mask = list(ones(self.nchan(-1))) 1093 1096 from asap.asapfitter import fitter … … 1101 1104 else: return s 1102 1105 1103 def auto_poly_baseline(self, mask=[], edge=(0, 0), order=0,1106 def auto_poly_baseline(self, mask=[], edge=(0, 0), order=0, 1104 1107 threshold=3, plot=False, insitu=None): 1105 1108 """ … … 1138 1141 1139 1142 # check whether edge is set up for each IF individually 1140 individual Edge = False;1141 if len(edge) >1:1142 if isinstance(edge[0],list) or isinstance(edge[0],tuple):1143 individualEdge = True;1144 1145 if not _is_valid(edge, int) and not individual Edge:1143 individualedge = False; 1144 if len(edge) > 1: 1145 if isinstance(edge[0], list) or isinstance(edge[0], tuple): 1146 individualedge = True; 1147 1148 if not _is_valid(edge, int) and not individualedge: 1146 1149 raise ValueError, "Parameter 'edge' has to be an integer or a \ 1147 1150 pair of integers specified as a tuple. Nested tuples are allowed \ 1148 1151 to make individual selection for different IFs." 1149 1152 1150 curedge = (0, 0)1151 if individual Edge:1152 for edge_par in edge:1153 if not _is_valid(edge,int):1154 raise ValueError, "Each element of the 'edge' tuple has \1155 to be a pair of integers or an integer."1153 curedge = (0, 0) 1154 if individualedge: 1155 for edgepar in edge: 1156 if not _is_valid(edgepar, int): 1157 raise ValueError, "Each element of the 'edge' tuple has \ 1158 to be a pair of integers or an integer." 1156 1159 else: 1157 curedge = edge;1160 curedge = edge; 1158 1161 1159 1162 # setup fitter … … 1162 1165 1163 1166 # setup line finder 1164 fl =linefinder()1167 fl = linefinder() 1165 1168 fl.set_options(threshold=threshold) 1166 1169 1167 1170 if not insitu: 1168 workscan =self.copy()1171 workscan = self.copy() 1169 1172 else: 1170 workscan =self1173 workscan = self 1171 1174 1172 1175 fl.set_scan(workscan) 1173 1176 1174 rows=range(workscan.nrow()) 1175 from asap import asaplog 1177 rows = range(workscan.nrow()) 1176 1178 asaplog.push("Processing:") 1177 1179 for r in rows: 1178 msg = " Scan[%d] Beam[%d] IF[%d] Pol[%d] Cycle[%d]" % (workscan.getscan(r),workscan.getbeam(r),workscan.getif(r),workscan.getpol(r), workscan.getcycle(r)) 1180 msg = " Scan[%d] Beam[%d] IF[%d] Pol[%d] Cycle[%d]" % \ 1181 (workscan.getscan(r), workscan.getbeam(r), workscan.getif(r), \ 1182 workscan.getpol(r), workscan.getcycle(r)) 1179 1183 asaplog.push(msg, False) 1180 1184 1181 1185 # figure out edge parameter 1182 if individualEdge: 1183 if len(edge)>=workscan.getif(r): 1184 raise RuntimeError, "Number of edge elements appear to be less than the number of IFs" 1185 curedge = edge[workscan.getif(r)] 1186 if individualedge: 1187 if len(edge) >= workscan.getif(r): 1188 raise RuntimeError, "Number of edge elements appear to " \ 1189 "be less than the number of IFs" 1190 curedge = edge[workscan.getif(r)] 1186 1191 1187 1192 # setup line finder 1188 fl.find_lines(r, mask,curedge)1193 fl.find_lines(r, mask, curedge) 1189 1194 f.set_scan(workscan, fl.get_mask()) 1190 1195 f.x = workscan._getabcissa(r) … … 1274 1279 varlist = vars() 1275 1280 s = scantable(self._math._unaryop(self, offset, "ADD", False)) 1276 s._add_history("add", varlist)1281 s._add_history("add", varlist) 1277 1282 print_log() 1278 1283 if insitu: … … 1281 1286 return s 1282 1287 1283 def scale(self, factor, tsys=True, insitu=None, ):1288 def scale(self, factor, tsys=True, insitu=None, ): 1284 1289 """ 1285 1290 Return a scan where all spectra are scaled by the give 'factor' … … 1296 1301 varlist = vars() 1297 1302 s = scantable(self._math._unaryop(self, factor, "MUL", tsys)) 1298 s._add_history("scale", varlist)1303 s._add_history("scale", varlist) 1299 1304 print_log() 1300 1305 if insitu: … … 1325 1330 varlist = vars() 1326 1331 s = scantable(self._math._auto_quotient(self, mode, preserve)) 1327 s._add_history("auto_quotient", varlist)1332 s._add_history("auto_quotient", varlist) 1328 1333 print_log() 1329 1334 return s … … 1346 1351 varlist = vars() 1347 1352 s = scantable(self._math._freqswitch(self)) 1348 s._add_history("freq_switch", varlist)1353 s._add_history("freq_switch", varlist) 1349 1354 print_log() 1350 1355 if insitu: self._assign(s) … … 1456 1461 hist += funcname+sep#cdate+sep 1457 1462 if parameters.has_key('self'): del parameters['self'] 1458 for k, v in parameters.iteritems():1463 for k, v in parameters.iteritems(): 1459 1464 if type(v) is dict: 1460 for k2, v2 in v.iteritems():1465 for k2, v2 in v.iteritems(): 1461 1466 hist += k2 1462 1467 hist += "=" 1463 if isinstance(v2, scantable):1468 if isinstance(v2, scantable): 1464 1469 hist += 'scantable' 1465 1470 elif k2 == 'mask': 1466 if isinstance(v2, list) or isinstance(v2,tuple):1471 if isinstance(v2, list) or isinstance(v2, tuple): 1467 1472 hist += str(self._zip_mask(v2)) 1468 1473 else: … … 1473 1478 hist += k 1474 1479 hist += "=" 1475 if isinstance(v, scantable):1480 if isinstance(v, scantable): 1476 1481 hist += 'scantable' 1477 1482 elif k == 'mask': 1478 if isinstance(v, list) or isinstance(v,tuple):1483 if isinstance(v, list) or isinstance(v, tuple): 1479 1484 hist += str(self._zip_mask(v)) 1480 1485 else: … … 1497 1502 else: 1498 1503 j = len(mask) 1499 segments.append([i, j])1504 segments.append([i, j]) 1500 1505 i = j 1501 1506 return segments … … 1505 1510 import re 1506 1511 lbl = "Intensity" 1507 if re.match(".K.", fu):1512 if re.match(".K.", fu): 1508 1513 lbl = "Brightness Temperature "+ fu 1509 elif re.match(".Jy.", fu):1514 elif re.match(".Jy.", fu): 1510 1515 lbl = "Flux density "+ fu 1511 1516 return lbl … … 1518 1523 def _fill(self, names, unit, average): 1519 1524 import os 1520 varlist = vars()1521 1525 from asap._asap import stfiller 1522 1526 first = True … … 1540 1544 r = stfiller(tbl) 1541 1545 msg = "Importing %s..." % (name) 1542 asaplog.push(msg, False)1546 asaplog.push(msg, False) 1543 1547 print_log() 1544 r._open(name, -1,-1)1548 r._open(name, -1, -1) 1545 1549 r._read() 1546 1550 #tbl = r._getdata() 1547 1551 if average: 1548 tbl = self._math._average((tbl, ),(),'NONE','SCAN')1552 tbl = self._math._average((tbl, ), (), 'NONE', 'SCAN') 1549 1553 #tbl = tbl2 1550 1554 if not first: … … 1553 1557 Scantable.__init__(self, tbl) 1554 1558 r._close() 1555 del r, tbl1559 del r, tbl 1556 1560 first = False 1557 1561 if unit is not None:
Note:
See TracChangeset
for help on using the changeset viewer.