Changeset 1859 for trunk/python
- Timestamp:
- 08/05/10 14:40:38 (14 years ago)
- Location:
- trunk/python
- Files:
-
- 10 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/python/__init__.py
r1835 r1859 16 16 from asap.utils import _n_bools, _is_sequence_or_number, _to_list 17 17 18 19 18 if is_ipython(): 20 19 from ipysupport import * 21 20 22 # use rc parameter to enable/disable logging 23 asaplog.enable(rcParams['verbose']) 21 # Only use rcParams['verbose'] in standard asap cli mode 22 # not in scripts or casapy 23 if not is_asap_cli(): 24 rcParams['verbose'] = False 24 25 25 26 setup_env() -
trunk/python/asapfitter.py
r1826 r1859 49 49 return 50 50 51 @print_log_dec 51 52 def set_scan(self, thescan=None, mask=None): 52 53 """ … … 58 59 if not thescan: 59 60 msg = "Please give a correct scan" 60 if rcParams['verbose']: 61 #print msg 62 asaplog.push(msg) 63 print_log('ERROR') 64 return 65 else: 66 raise TypeError(msg) 61 raise TypeError(msg) 67 62 self.fitted = False 68 63 self.data = thescan … … 74 69 return 75 70 71 @print_log_dec 76 72 def set_function(self, **kwargs): 77 73 """ … … 114 110 else: 115 111 msg = "Invalid function type." 116 if rcParams['verbose']: 117 #print msg 118 asaplog.push(msg) 119 print_log('ERROR') 120 return 121 else: 122 raise TypeError(msg) 112 raise TypeError(msg) 123 113 124 114 self.fitter.setexpression(self.fitfunc,n) … … 146 136 or self.fitfunc is None: 147 137 msg = "Fitter not yet initialised. Please set data & fit function" 148 if rcParams['verbose']: 149 #print msg 150 asaplog.push(msg) 151 print_log('ERROR') 152 return 153 else: 154 raise RuntimeError(msg) 138 raise RuntimeError(msg) 155 139 156 140 else: … … 172 156 if len(ps) == 0 or estimate: 173 157 self.fitter.estimate() 174 try: 175 fxdpar = list(self.fitter.getfixedparameters()) 176 if len(fxdpar) and fxdpar.count(0) == 0: 177 raise RuntimeError,"No point fitting, if all parameters are fixed." 178 if self.uselinear: 179 converged = self.fitter.lfit() 180 else: 181 converged = self.fitter.fit() 182 if not converged: 183 raise RuntimeError,"Fit didn't converge." 184 except RuntimeError, msg: 185 if rcParams['verbose']: 186 #print msg 187 print_log() 188 asaplog.push(str(msg)) 189 print_log('ERROR') 190 else: 191 raise 158 fxdpar = list(self.fitter.getfixedparameters()) 159 if len(fxdpar) and fxdpar.count(0) == 0: 160 raise RuntimeError,"No point fitting, if all parameters are fixed." 161 if self.uselinear: 162 converged = self.fitter.lfit() 163 else: 164 converged = self.fitter.fit() 165 if not converged: 166 raise RuntimeError,"Fit didn't converge." 192 167 self._fittedrow = row 193 168 self.fitted = True 194 print_log()195 169 return 196 170 … … 244 218 if self.fitfunc is None: 245 219 msg = "Please specify a fitting function first." 246 if rcParams['verbose']: 247 #print msg 248 asaplog.push(msg) 249 print_log('ERROR') 250 return 251 else: 252 raise RuntimeError(msg) 220 raise RuntimeError(msg) 253 221 if (self.fitfunc == "gauss" or self.fitfunc == 'lorentz') and component is not None: 254 222 if not self.fitted and sum(self.fitter.getparameters()) == 0: … … 266 234 if fixed is not None: 267 235 self.fitter.setfixedparameters(fixed) 268 print_log()269 236 return 270 237 238 @print_log_dec 271 239 def set_gauss_parameters(self, peak, centre, fwhm, 272 240 peakfixed=0, centrefixed=0, … … 288 256 if self.fitfunc != "gauss": 289 257 msg = "Function only operates on Gaussian components." 290 if rcParams['verbose']: 291 #print msg 292 asaplog.push(msg) 293 print_log('ERROR') 294 return 295 else: 296 raise ValueError(msg) 258 raise ValueError(msg) 297 259 if 0 <= component < len(self.components): 298 260 d = {'params':[peak, centre, fwhm], … … 301 263 else: 302 264 msg = "Please select a valid component." 303 if rcParams['verbose']: 304 #print msg 305 asaplog.push(msg) 306 print_log('ERROR') 307 return 308 else: 309 raise ValueError(msg) 310 265 raise ValueError(msg) 266 267 @print_log_dec 311 268 def set_lorentz_parameters(self, peak, centre, fwhm, 312 269 peakfixed=0, centrefixed=0, … … 328 285 if self.fitfunc != "lorentz": 329 286 msg = "Function only operates on Lorentzian components." 330 if rcParams['verbose']: 331 #print msg 332 asaplog.push(msg) 333 print_log('ERROR') 334 return 335 else: 336 raise ValueError(msg) 287 raise ValueError(msg) 337 288 if 0 <= component < len(self.components): 338 289 d = {'params':[peak, centre, fwhm], … … 341 292 else: 342 293 msg = "Please select a valid component." 343 if rcParams['verbose']: 344 #print msg 345 asaplog.push(msg) 346 print_log('ERROR') 347 return 348 else: 349 raise ValueError(msg) 294 raise ValueError(msg) 350 295 351 296 def get_area(self, component=None): … … 378 323 return sum(areas) 379 324 325 @print_log_dec 380 326 def get_errors(self, component=None): 381 327 """ … … 387 333 if not self.fitted: 388 334 msg = "Not yet fitted." 389 if rcParams['verbose']: 390 #print msg 391 asaplog.push(msg) 392 print_log('ERROR') 393 return 394 else: 395 raise RuntimeError(msg) 335 raise RuntimeError(msg) 396 336 errs = list(self.fitter.geterrors()) 397 337 cerrs = errs … … 403 343 return cerrs 404 344 345 346 @print_log_dec 405 347 def get_parameters(self, component=None, errors=False): 406 348 """ … … 412 354 if not self.fitted: 413 355 msg = "Not yet fitted." 414 if rcParams['verbose']: 415 #print msg 416 asaplog.push(msg) 417 print_log('ERROR') 418 return 419 else: 420 raise RuntimeError(msg) 356 raise RuntimeError(msg) 421 357 pars = list(self.fitter.getparameters()) 422 358 fixed = list(self.fitter.getfixedparameters()) … … 444 380 area += [a for i in range(3)] 445 381 fpars = self._format_pars(cpars, cfixed, errors and cerrs, area) 446 if rcParams['verbose']: 447 #print fpars 448 asaplog.push(fpars) 449 print_log() 382 asaplog.push(fpars) 450 383 return {'params':cpars, 'fixed':cfixed, 'formatted': fpars, 451 384 'errors':cerrs} … … 481 414 return out 482 415 416 417 @print_log_dec 483 418 def get_estimate(self): 484 419 """ … … 487 422 pars = self.fitter.getestimate() 488 423 fixed = self.fitter.getfixedparameters() 489 if rcParams['verbose']: 490 #print self._format_pars(pars,fixed,None) 491 asaplog.push(self._format_pars(pars,fixed,None)) 492 print_log() 424 asaplog.push(self._format_pars(pars,fixed,None)) 493 425 return pars 494 426 427 @print_log_dec 495 428 def get_residual(self): 496 429 """ … … 499 432 if not self.fitted: 500 433 msg = "Not yet fitted." 501 if rcParams['verbose']: 502 #print msg 503 asaplog.push(msg) 504 print_log('ERROR') 505 return 506 else: 507 raise RuntimeError(msg) 434 raise RuntimeError(msg) 508 435 return self.fitter.getresidual() 509 436 437 @print_log_dec 510 438 def get_chi2(self): 511 439 """ … … 514 442 if not self.fitted: 515 443 msg = "Not yet fitted." 516 if rcParams['verbose']: 517 #print msg 518 asaplog.push(msg) 519 print_log('ERROR') 520 return 521 else: 522 raise RuntimeError(msg) 444 raise RuntimeError(msg) 523 445 ch2 = self.fitter.getchi2() 524 if rcParams['verbose']: 525 #print 'Chi^2 = %3.3f' % (ch2) 526 asaplog.push( 'Chi^2 = %3.3f' % (ch2) ) 527 print_log() 446 asaplog.push( 'Chi^2 = %3.3f' % (ch2) ) 528 447 return ch2 529 448 449 @print_log_dec 530 450 def get_fit(self): 531 451 """ … … 534 454 if not self.fitted: 535 455 msg = "Not yet fitted." 536 if rcParams['verbose']: 537 #print msg 538 asaplog.push(msg) 539 print_log('ERROR') 540 return 541 else: 542 raise RuntimeError(msg) 456 raise RuntimeError(msg) 543 457 return self.fitter.getfit() 544 458 … … 550 464 if not self.fitted: 551 465 msg = "Not yet fitted." 552 if rcParams['verbose']: 553 #print msg 554 asaplog.push(msg) 555 print_log('ERROR') 556 return 557 else: 558 raise RuntimeError(msg) 466 raise RuntimeError(msg) 559 467 from asap import scantable 560 468 if not isinstance(self.data, scantable): 561 469 msg = "Not a scantable" 562 if rcParams['verbose']: 563 #print msg 564 asaplog.push(msg) 565 print_log('ERROR') 566 return 567 else: 568 raise TypeError(msg) 470 raise TypeError(msg) 569 471 scan = self.data.copy() 570 472 scan._setspectrum(self.fitter.getresidual()) 571 print_log()572 473 return scan 573 474 … … 671 572 if (not rcParams['plotter.gui']): 672 573 self._p.save(filename) 673 print_log()674 574 675 575 @print_log_dec … … 683 583 if not isinstance(self.data, scantable) : 684 584 msg = "Data is not a scantable" 685 if rcParams['verbose']: 686 #print msg 687 asaplog.push(msg) 688 print_log('ERROR') 689 return 690 else: 691 raise TypeError(msg) 585 raise TypeError(msg) 692 586 if insitu is None: insitu = rcParams['insitu'] 693 587 if not insitu: … … 725 619 self._p.unmap() 726 620 self._p = None 727 print_log()728 621 return scan -
trunk/python/asapmath.py
r1827 r1859 67 67 if not isinstance(s,scantable): 68 68 msg = "Please give a list of scantables" 69 if rcParams['verbose']: 70 #print msg 71 asaplog.push(msg) 72 print_log('ERROR') 73 return 74 else: 75 raise TypeError(msg) 69 raise TypeError(msg) 76 70 if scanav: scanav = "SCAN" 77 71 else: scanav = "NONE" … … 96 90 s = scantable(stm._new_average(alignedlst, compel, mask, weight.upper(), scanav)) 97 91 s._add_history("average_time",varlist) 98 print_log() 92 99 93 return s 100 94 95 @print_log_dec 101 96 def quotient(source, reference, preserve=True): 102 97 """ … … 119 114 s = scantable(stm._quotient(source, reference, preserve)) 120 115 s._add_history("quotient",varlist) 121 print_log()122 116 return s 123 117 … … 138 132 s = scantable(stm._dototalpower(calon, caloff, tcalval)) 139 133 s._add_history("dototalpower",varlist) 140 print_log()141 134 return s 142 135 … … 159 152 s = scantable(stm._dosigref(sig, ref, smooth, tsysval, tauval)) 160 153 s._add_history("dosigref",varlist) 161 print_log()162 154 return s 163 155 … … 190 182 ## if s is None: 191 183 ## msg = "The input data appear to contain no position-switch mode data." 192 ## if rcParams['verbose']: 193 ## #print msg 194 ## asaplog.push(msg) 195 ## print_log('ERROR') 196 ## return 197 ## else: 198 ## raise TypeError(msg) 184 ## raise TypeError(msg) 199 185 s = scantab.copy() 200 186 from asap._asap import srctype … … 205 191 except Exception, e: 206 192 msg = "The input data appear to contain no position-switch mode data." 207 if rcParams['verbose']: 208 #print msg 209 asaplog.push(msg) 210 print_log('ERROR') 211 return 212 else: 213 raise TypeError(msg) 193 raise TypeError(msg) 214 194 s.set_selection() 215 195 sel.reset() … … 217 197 if ssub is None: 218 198 msg = "No data was found with given scan numbers!" 219 if rcParams['verbose']: 220 #print msg 221 asaplog.push(msg) 222 print_log('ERROR') 223 return 224 else: 225 raise TypeError(msg) 199 raise TypeError(msg) 226 200 #ssubon = ssub.get_scan('*calon') 227 201 #ssuboff = ssub.get_scan('*[^calon]') … … 238 212 if ssubon.nrow() != ssuboff.nrow(): 239 213 msg = "mismatch in numbers of CAL on/off scans. Cannot calibrate. Check the scan numbers." 240 if rcParams['verbose']: 241 #print msg 242 asaplog.push(msg) 243 print_log('ERROR') 244 return 245 else: 246 raise TypeError(msg) 214 raise TypeError(msg) 247 215 cals = dototalpower(ssubon, ssuboff, tcalval) 248 216 #sig = cals.get_scan('*ps') … … 260 228 if sig.nscan() != ref.nscan(): 261 229 msg = "mismatch in numbers of on/off scans. Cannot calibrate. Check the scan numbers." 262 if rcParams['verbose']: 263 #print msg 264 asaplog.push(msg) 265 print_log('ERROR') 266 return 267 else: 268 raise TypeError(msg) 230 raise TypeError(msg) 269 231 270 232 #for user supplied Tsys … … 272 234 if tauval<=0.0: 273 235 msg = "Need to supply a valid tau to use the supplied Tsys" 274 if rcParams['verbose']: 275 #print msg 276 asaplog.push(msg) 277 print_log('ERROR') 278 return 279 else: 280 raise TypeError(msg) 236 raise TypeError(msg) 281 237 else: 282 238 sig.recalc_azel() 283 239 ref.recalc_azel() 284 240 #msg = "Use of user specified Tsys is not fully implemented yet." 285 #if rcParams['verbose']: 286 # print msg 287 # return 288 #else: 289 # raise TypeError(msg) 241 #raise TypeError(msg) 290 242 # use get_elevation to get elevation and 291 243 # calculate a scaling factor using the formula … … 442 394 ### 443 395 ress._add_history("calps", varlist) 444 print_log()445 396 return ress 446 397 … … 469 420 ## if s is None: 470 421 ## msg = "The input data appear to contain no Nod observing mode data." 471 ## if rcParams['verbose']: 472 ## #print msg 473 ## asaplog.push(msg) 474 ## print_log('ERROR') 475 ## return 476 ## else: 477 ## raise TypeError(msg) 422 ## raise TypeError(msg) 478 423 s = scantab.copy() 479 424 sel = selector() … … 483 428 except Exception, e: 484 429 msg = "The input data appear to contain no Nod observing mode data." 485 if rcParams['verbose']: 486 #print msg 487 asaplog.push(msg) 488 print_log('ERROR') 489 return 490 else: 491 raise TypeError(msg) 430 raise TypeError(msg) 492 431 sel.reset() 493 432 del sel … … 511 450 #if len(scannos)>2: 512 451 # msg = "calnod can only process a pair of nod scans at time." 513 # if rcParams['verbose']: 514 # print msg 515 # return 516 # else: 517 # raise TypeError(msg) 452 # raise TypeError(msg) 518 453 # 519 454 #if len(scannos)==2: … … 525 460 if tauval<=0.0: 526 461 msg = "Need to supply a valid tau to use the supplied Tsys" 527 if rcParams['verbose']: 528 #print msg 529 asaplog.push(msg) 530 print_log('ERROR') 531 return 532 else: 533 raise TypeError(msg) 462 raise TypeError(msg) 534 463 else: 535 464 scantab.recalc_azel() … … 683 612 ### 684 613 resspec._add_history("calnod",varlist) 685 print_log()686 614 return resspec 687 615 … … 716 644 # if check is None: 717 645 # msg = "The input data appear to contain no Nod observing mode data." 718 # if rcParams['verbose']: 719 # print msg 720 # return 721 # else: 722 # raise TypeError(msg) 646 # raise TypeError(msg) 723 647 s = scantab.get_scan(scannos) 724 648 del scantab … … 891 815 ### 892 816 resspec._add_history("calfs",varlist) 893 print_log()894 817 return resspec 895 818 … … 921 844 if not isinstance(s,scantable): 922 845 msg = "Please give a list of scantables" 923 if rcParams['verbose']: 924 #print msg 925 asaplog.push(msg) 926 print_log('ERROR') 927 return 928 else: 929 raise TypeError(msg) 846 raise TypeError(msg) 930 847 s = scantable(stm._merge(lst)) 931 848 s._add_history("merge", varlist) 932 print_log()933 849 return s 934 850 851 @print_log_dec 935 852 def calibrate( scantab, scannos=[], calmode='none', verify=None ): 936 853 """ … … 946 863 if ( calmode == 'nod' ): 947 864 asaplog.push( 'Calibrating nod data.' ) 948 print_log()949 865 scal = calnod( scantab, scannos=scannos, verify=verify ) 950 866 elif ( calmode == 'quotient' ): 951 867 asaplog.push( 'Calibrating using quotient.' ) 952 print_log()953 868 scal = scantab.auto_quotient( verify=verify ) 954 869 elif ( calmode == 'ps' ): 955 870 asaplog.push( 'Calibrating %s position-switched data.' % antname ) 956 print_log()957 871 if ( antname.find( 'APEX' ) != -1 ): 958 872 scal = apexcal( scantab, scannos, calmode, verify ) … … 963 877 elif ( calmode == 'fs' or calmode == 'fsotf' ): 964 878 asaplog.push( 'Calibrating %s frequency-switched data.' % antname ) 965 print_log()966 879 if ( antname.find( 'APEX' ) != -1 ): 967 880 scal = apexcal( scantab, scannos, calmode, verify ) … … 972 885 elif ( calmode == 'otf' ): 973 886 asaplog.push( 'Calibrating %s On-The-Fly data.' % antname ) 974 print_log()975 887 scal = almacal( scantab, scannos, calmode, verify ) 976 888 else: … … 1015 927 return scal 1016 928 929 @print_log_dec 1017 930 def splitant(filename, outprefix='',overwrite=False): 1018 931 """ … … 1032 945 """ 1033 946 # Import the table toolkit from CASA 1034 try: 1035 import casac 1036 except ImportError: 1037 if rcParams['verbose']: 1038 #print "failed to load casa" 1039 print_log() 1040 asaplog.push("failed to load casa") 1041 print_log('ERROR') 1042 else: raise 1043 return False 1044 try: 1045 tbtool = casac.homefinder.find_home_by_name('tableHome') 1046 tb = tbtool.create() 1047 tb2 = tbtool.create() 1048 except: 1049 if rcParams['verbose']: 1050 #print "failed to load a table tool:\n", e 1051 print_log() 1052 asaplog.push("failed to load table tool") 1053 print_log('ERROR') 1054 else: raise 1055 return False 947 948 import casac 949 tbtool = casac.homefinder.find_home_by_name('tableHome') 950 tb = tbtool.create() 951 tb2 = tbtool.create() 1056 952 # Check the input filename 1057 953 if isinstance(filename, str): … … 1061 957 if not os.path.exists(filename): 1062 958 s = "File '%s' not found." % (filename) 1063 if rcParams['verbose']:1064 print_log()1065 asaplog.push(s)1066 print_log('ERROR')1067 return1068 959 raise IOError(s) 1069 960 # check if input file is MS … … 1072 963 or not os.path.exists(filename+'/table.f1'): 1073 964 s = "File '%s' is not a Measurement set." % (filename) 1074 if rcParams['verbose']:1075 print_log()1076 asaplog.push(s)1077 print_log('ERROR')1078 return1079 965 raise IOError(s) 1080 966 else: 1081 967 s = "The filename should be string. " 1082 if rcParams['verbose']:1083 print_log()1084 asaplog.push(s)1085 print_log('ERROR')1086 return1087 968 raise TypeError(s) 1088 969 # Check out put file name … … 1110 991 return outfiles 1111 992 993 @print_log_dec 1112 994 def _array2dOp( scan, value, mode="ADD", tsys=False ): 1113 995 """ … … 1128 1010 del stm 1129 1011 elif len( value ) != nrow: 1130 asaplog.push( 'len(value) must be 1 or conform to scan.nrow()' ) 1131 print_log( 'ERROR' ) 1012 raise ValueError( 'len(value) must be 1 or conform to scan.nrow()' ) 1132 1013 else: 1133 1014 from asap._asap import stmath -
trunk/python/asapplotter.py
r1858 r1859 102 102 if not self._data and not scan: 103 103 msg = "Input is not a scantable" 104 if rcParams['verbose']:105 #print msg106 asaplog.push( msg )107 print_log( 'ERROR' )108 return109 104 raise TypeError(msg) 110 105 if scan: self.set_data(scan,refresh=False) … … 277 272 # end matplotlib.axes fowarding functions 278 273 274 @print_log_dec 279 275 def set_data(self, scan, refresh=True): 280 276 """ … … 299 295 msg = "A new scantable is set to the plotter. The masks and data selections are reset." 300 296 asaplog.push( msg ) 301 print_log( 'INFO' )302 297 else: 303 298 self._data = scan … … 305 300 else: 306 301 msg = "Input is not a scantable" 307 if rcParams['verbose']:308 #print msg309 asaplog.push( msg )310 print_log( 'ERROR' )311 return312 302 raise TypeError(msg) 313 303 … … 320 310 if refresh: self.plot() 321 311 322 312 @print_log_dec 323 313 def set_mode(self, stacking=None, panelling=None, refresh=True): 324 314 """ … … 343 333 if not self.set_panelling(panelling) or \ 344 334 not self.set_stacking(stacking): 345 if rcParams['verbose']: 346 #print msg 347 asaplog.push( msg ) 348 print_log( 'ERROR' ) 349 return 350 else: 351 raise TypeError(msg) 335 raise TypeError(msg) 352 336 if refresh and self._data: self.plot(self._data) 353 337 return … … 745 729 return 746 730 747 731 @print_log_dec 748 732 def set_mask(self, mask=None, selection=None, refresh=True): 749 733 """ … … 763 747 if not self._data: 764 748 msg = "Can only set mask after a first call to plot()" 765 if rcParams['verbose']: 766 #print msg 767 asaplog.push( msg ) 768 print_log( 'ERROR' ) 769 return 770 else: 771 raise RuntimeError(msg) 749 raise RuntimeError(msg) 772 750 if len(mask): 773 751 if isinstance(mask, list) or isinstance(mask, tuple): … … 1171 1149 # plot total power data 1172 1150 # plotting in time is not yet implemented.. 1151 @print_log_dec 1173 1152 def plottp(self, scan=None, outfile=None): 1174 1153 if self._plotter.is_dead: … … 1182 1161 if not self._data and not scan: 1183 1162 msg = "Input is not a scantable" 1184 if rcParams['verbose']:1185 #print msg1186 asaplog.push( msg )1187 print_log( 'ERROR' )1188 return1189 1163 raise TypeError(msg) 1190 1164 if isinstance(scan, scantable): … … 1216 1190 self._plotter.tidy() 1217 1191 self._plotter.show(hardrefresh=False) 1218 print_log()1219 1192 return 1220 1193 … … 1277 1250 1278 1251 # printing header information 1252 @print_log_dec 1279 1253 def print_header(self, plot=True, fontsize=9, logger=False, selstr='', extrastr=''): 1280 1254 """ … … 1288 1262 extrastr: additional string to print (not verified) 1289 1263 """ 1290 if not plot and not logger: return 1291 if not self._data: raise RuntimeError("No scantable has been set yet.") 1264 if not plot and not logger: 1265 return 1266 if not self._data: 1267 raise RuntimeError("No scantable has been set yet.") 1292 1268 # Now header will be printed on plot and/or logger. 1293 1269 # Get header information and format it. … … 1322 1298 asaplog.push(extrastr) 1323 1299 asaplog.push(ssum[ssum.find('Beams:'):]) 1324 print_log()1325 1300 del ssum -
trunk/python/env.py
r1858 r1859 1 1 """This module has various functions for environment specific setings. 2 2 """ 3 __all__ = ["is_casapy", "is_ipython", "setup_env", "get_revision"] 3 __all__ = ["is_casapy", "is_ipython", "setup_env", "get_revision", 4 "is_asap_cli"] 4 5 5 6 import sys … … 19 20 """Are we running inside IPython?""" 20 21 return 'IPython' in sys.modules.keys() 22 23 def is_asap_cli(): 24 """Are we running inside asap ipython (but not casapy)""" 25 return is_ipython() and not is_casapy() 21 26 22 27 def setup_env(): -
trunk/python/linecatalog.py
r1826 r1859 7 7 __revision__ = "$Revision$" 8 8 from asap._asap import linecatalog as lcbase 9 from asap.parameters import rcParams10 9 from asap.logging import asaplog 11 10 import os … … 30 29 else: 31 30 msg = "File '%s' not found" % fpath 32 if rcParams['verbose']: 33 #print msg 34 asaplog.push( msg ) 35 print_log( 'ERROR' ) 36 return 37 else: 38 raise IOError(msg) 31 raise IOError(msg) 39 32 40 33 def __repr__(self): … … 99 92 if not overwrite: 100 93 msg = "File %s exists." % name 101 if rcParams['verbose']: 102 #print msg 103 asaplog.push( msg ) 104 print_log( 'ERROR' ) 105 return 106 else: 107 raise IOError(msg) 94 raise IOError(msg) 108 95 lcbase.save(self, name) 109 96 -
trunk/python/logging.py
r1858 r1859 19 19 20 20 .. note:: Do instantiate a new one - use the :obj:`asaplog` instead. 21 21 22 22 """ 23 23 def __init__(self): 24 self._enabled = False24 self._enabled = True 25 25 self._log = "" 26 26 if is_casapy(): … … 31 31 set_global_sink(self.logger) 32 32 33 def post(self, level ):33 def post(self, level, origin=""): 34 34 """Post the messages to the logger. This will clear the buffered 35 35 logs. … … 42 42 if not self._enabled: 43 43 return 44 if not rcParams['verbose']:45 return46 44 47 45 logs = self._log.strip() 48 46 if len(logs) > 0: 49 self.logger.post(logs, priority=level)47 self.logger.post(logs, priority=level, origin=origin) 50 48 if isinstance(self.logger, LogSink): 51 49 logs = self.logger.pop().strip() … … 64 62 65 63 """ 66 from asap import rcParams67 64 if self._enabled: 68 if rcParams["verbose"]: 69 sep = "" 70 self._log = sep.join([self._log, msg]) 71 if newline: 72 self._log += "\n" 65 sep = "" 66 self._log = sep.join([self._log, msg]) 67 if newline: 68 self._log += "\n" 73 69 74 70 def enable(self, flag=True): … … 80 76 self._enabled = flag 81 77 78 def is_enabled(self): 79 return self._enabled 80 82 81 asaplog = AsapLogger() 83 82 """Default asap logger""" 84 83 85 def print_log_dec(f , level='INFO'):84 def print_log_dec(f): 86 85 """Decorator which posts log at completion of the wrapped method. 86 87 87 Example:: 88 88 … … 96 96 @wraps_dec(f) 97 97 def wrap_it(*args, **kw): 98 val = f(*args, **kw) 99 print_log(level) 100 return val 98 level = "INFO" 99 try: 100 val = f(*args, **kw) 101 return val 102 except Exception, ex: 103 level = "ERROR" 104 asaplog.push(str(ex)) 105 if rcParams['verbose']: 106 pass 107 else: 108 raise 109 finally: 110 print_log(level, f.func_name) 101 111 return wrap_it 102 112 103 def print_log(level='INFO' ):113 def print_log(level='INFO', origin=""): 104 114 """Alias for asaplog.post(level)""" 105 asaplog.post(level )115 asaplog.post(level, origin) -
trunk/python/opacity.py
r1826 r1859 6 6 from asap.asapfitter import fitter 7 7 from asap.selector import selector 8 from asap.parameters import rcParams9 8 from asap._asap import atmosphere 10 9 … … 152 151 plot: Plot each fit (airmass vs. Tsys). Default is 'False' 153 152 """ 154 rcsave = rcParams['verbose']155 rcParams['verbose'] = False156 153 if plot: 157 154 from matplotlib import pylab … … 222 219 223 220 scan.set_selection(basesel) 224 rcParams['verbose'] = rcsave225 221 if plot: 226 222 pylab.close() -
trunk/python/scantable.py
r1857 r1859 15 15 from asap.linecatalog import linecatalog 16 16 from asap.coordinate import coordinate 17 from asap.utils import _n_bools, mask_not, mask_and, mask_or 17 from asap.utils import _n_bools, mask_not, mask_and, mask_or, page 18 18 19 19 … … 101 101 tmpstr=tmpstr+antenna[i]+',' 102 102 else: 103 asaplog.push('Bad antenna selection.') 104 print_log('ERROR') 105 return 103 raise TypeError('Bad antenna selection.') 106 104 antenna = tmpstr.rstrip(',') 107 105 parallactify = parallactify or rcParams['scantable.parallactify'] … … 117 115 if not os.path.exists(filename): 118 116 s = "File '%s' not found." % (filename) 119 if rcParams['verbose']:120 asaplog.push(s)121 print_log('ERROR')122 return123 117 raise IOError(s) 124 118 if is_scantable(filename): … … 133 127 msg = "The given file '%s'is not a valid " \ 134 128 "asap table." % (filename) 135 if rcParams['verbose']: 136 #print msg 137 asaplog.push( msg ) 138 print_log( 'ERROR' ) 139 return 140 else: 141 raise IOError(msg) 129 raise IOError(msg) 142 130 else: 143 131 self._fill([filename], unit, average, getpt, antenna) … … 191 179 if not overwrite: 192 180 msg = "File %s exists." % name 193 if rcParams['verbose']: 194 #print msg 195 asaplog.push( msg ) 196 print_log( 'ERROR' ) 197 return 198 else: 199 raise IOError(msg) 181 raise IOError(msg) 200 182 format2 = format.upper() 201 183 if format2 == 'ASAP': … … 236 218 from asap import unique 237 219 if not _is_valid(scanid): 238 if rcParams['verbose']: 239 asaplog.push( 'Please specify a scanno to drop from the scantable' ) 240 print_log( 'ERROR' ) 241 return 242 else: 243 raise RuntimeError("No scan given") 244 try: 245 scanid = _to_list(scanid) 246 allscans = unique([ self.getscan(i) for i in range(self.nrow())]) 247 for sid in scanid: allscans.remove(sid) 248 if len(allscans) == 0: 249 raise ValueError("Can't remove all scans") 250 except ValueError: 251 if rcParams['verbose']: 252 print_log() 253 asaplog.push( "Couldn't find any match." ) 254 print_log( 'ERROR' ) 255 return 256 else: raise 257 try: 258 sel = selector(scans=allscans) 259 return self._select_copy(sel) 260 except RuntimeError: 261 if rcParams['verbose']: 262 print_log() 263 asaplog.push( "Couldn't find any match." ) 264 print_log( 'ERROR' ) 265 else: 266 raise 220 raise RuntimeError( 'Please specify a scanno to drop from the scantable' ) 221 scanid = _to_list(scanid) 222 allscans = unique([ self.getscan(i) for i in range(self.nrow())]) 223 for sid in scanid: allscans.remove(sid) 224 if len(allscans) == 0: 225 raise ValueError("Can't remove all scans") 226 sel = selector(scans=allscans) 227 return self._select_copy(sel) 267 228 268 229 def _select_copy(self, selection): … … 299 260 """ 300 261 if scanid is None: 301 if rcParams['verbose']: 302 #print "Please specify a scan no or name to " \ 303 # "retrieve from the scantable" 304 asaplog.push( 'Please specify a scan no or name to retrieve' 305 ' from the scantable' ) 306 print_log( 'ERROR' ) 307 return 308 else: 309 raise RuntimeError("No scan given") 310 262 raise RuntimeError( 'Please specify a scan no or name to ' 263 'retrieve from the scantable' ) 311 264 try: 312 265 bsel = self.get_selection() … … 323 276 else: 324 277 msg = "Illegal scanid type, use 'int' or 'list' if ints." 325 if rcParams['verbose']: 326 #print msg 327 asaplog.push( msg ) 328 print_log( 'ERROR' ) 329 else: 330 raise TypeError(msg) 278 raise TypeError(msg) 331 279 except RuntimeError: 332 if rcParams['verbose']: 333 #print "Couldn't find any match." 334 print_log() 335 asaplog.push( "Couldn't find any match." ) 336 print_log( 'ERROR' ) 337 else: raise 280 raise 338 281 339 282 def __str__(self): … … 362 305 else: 363 306 msg = "Illegal file name '%s'." % (filename) 364 if rcParams['verbose']: 365 #print msg 366 asaplog.push( msg ) 367 print_log( 'ERROR' ) 368 else: 369 raise IOError(msg) 370 if rcParams['verbose']: 371 try: 372 from IPython.genutils import page as pager 373 except ImportError: 374 from pydoc import pager 375 pager(info) 376 else: 377 return info 307 raise IOError(msg) 308 return page(info) 378 309 379 310 def get_spectrum(self, rowno): … … 518 449 return workscan 519 450 520 #def stats(self, stat='stddev', mask=None):451 @print_log_dec 521 452 def stats(self, stat='stddev', mask=None, form='3.3f'): 522 453 """\ … … 591 522 out += sep+"\n" 592 523 593 if rcParams['verbose']: 594 import os 595 if os.environ.has_key( 'USER' ): 596 usr=os.environ['USER'] 597 else: 598 import commands 599 usr=commands.getoutput( 'whoami' ) 600 tmpfile='/tmp/tmp_'+usr+'_casapy_asap_scantable_stats' 601 f=open(tmpfile,'w') 602 print >> f, sep 603 print >> f, ' %s %s' % (label, statunit) 604 print >> f, sep 605 print >> f, out 606 f.close() 607 f=open(tmpfile,'r') 608 x=f.readlines() 609 f.close() 610 blanc='' 611 asaplog.push(blanc.join(x), False) 612 #for xx in x: 613 # asaplog.push( xx, False ) 614 print_log() 524 525 import os 526 if os.environ.has_key( 'USER' ): 527 usr = os.environ['USER'] 528 else: 529 import commands 530 usr = commands.getoutput( 'whoami' ) 531 tmpfile = '/tmp/tmp_'+usr+'_casapy_asap_scantable_stats' 532 f = open(tmpfile,'w') 533 print >> f, sep 534 print >> f, ' %s %s' % (label, statunit) 535 print >> f, sep 536 print >> f, out 537 f.close() 538 f = open(tmpfile,'r') 539 x = f.readlines() 540 f.close() 541 asaplog.push(''.join(x), False) 542 615 543 return statvals 616 544 … … 727 655 out += '= %3.3f\n' % (outvec[i]) 728 656 out += sep+'\n' 729 if rcParams['verbose']: 730 731 732 733 734 657 658 asaplog.push(sep) 659 asaplog.push(" %s" % (label)) 660 asaplog.push(sep) 661 asaplog.push(out) 662 print_log() 735 663 return outvec 736 664 … … 945 873 else: 946 874 msg = "Please specify a valid freq type. Valid types are:\n", valid 947 if rcParams['verbose']: 948 #print msg 949 asaplog.push( msg ) 950 print_log( 'ERROR' ) 951 else: 952 raise TypeError(msg) 953 875 raise TypeError(msg) 876 877 @print_log_dec 954 878 def set_dirframe(self, frame=""): 955 879 """\ … … 967 891 """ 968 892 varlist = vars() 969 try: 970 Scantable.set_dirframe(self, frame) 971 except RuntimeError, msg: 972 if rcParams['verbose']: 973 #print msg 974 print_log() 975 asaplog.push( str(msg) ) 976 print_log( 'ERROR' ) 977 else: 978 raise 893 Scantable.set_dirframe(self, frame) 979 894 self._add_history("set_dirframe", varlist) 980 895 … … 1013 928 return abc, lbl 1014 929 930 @print_log_dec 1015 931 def flag(self, mask=None, unflag=False): 1016 932 """\ … … 1027 943 varlist = vars() 1028 944 mask = mask or [] 1029 try: 1030 self._flag(mask, unflag) 1031 except RuntimeError, msg: 1032 if rcParams['verbose']: 1033 #print msg 1034 print_log() 1035 asaplog.push( str(msg) ) 1036 print_log( 'ERROR' ) 1037 return 1038 else: raise 945 self._flag(mask, unflag) 1039 946 self._add_history("flag", varlist) 1040 947 948 @print_log_dec 1041 949 def flag_row(self, rows=[], unflag=False): 1042 950 """\ … … 1052 960 """ 1053 961 varlist = vars() 1054 try: 1055 self._flag_row(rows, unflag) 1056 except RuntimeError, msg: 1057 if rcParams['verbose']: 1058 print_log() 1059 asaplog.push( str(msg) ) 1060 print_log('ERROR') 1061 return 1062 else: raise 962 self._flag_row(rows, unflag) 1063 963 self._add_history("flag_row", varlist) 1064 964 965 @print_log_dec 1065 966 def clip(self, uthres=None, dthres=None, clipoutside=True, unflag=False): 1066 967 """\ … … 1080 981 """ 1081 982 varlist = vars() 1082 try: 1083 self._clip(uthres, dthres, clipoutside, unflag) 1084 except RuntimeError, msg: 1085 if rcParams['verbose']: 1086 print_log() 1087 asaplog.push(str(msg)) 1088 print_log('ERROR') 1089 return 1090 else: raise 983 self._clip(uthres, dthres, clipoutside, unflag) 1091 984 self._add_history("clip", varlist) 1092 985 … … 1120 1013 if not (unit == "" or base.has_key(unit)): 1121 1014 raise ValueError("%s is not a valid unit." % unit) 1122 try: 1123 if unit == "": 1124 s = scantable(self._math._lag_flag(self, start, end, "lags")) 1125 else: 1126 s = scantable(self._math._lag_flag(self, start*base[unit], 1127 end*base[unit], "frequency")) 1128 except RuntimeError, msg: 1129 if rcParams['verbose']: 1130 #print msg 1131 print_log() 1132 asaplog.push( str(msg) ) 1133 print_log( 'ERROR' ) 1134 return 1135 else: raise 1015 if unit == "": 1016 s = scantable(self._math._lag_flag(self, start, end, "lags")) 1017 else: 1018 s = scantable(self._math._lag_flag(self, start*base[unit], 1019 end*base[unit], "frequency")) 1136 1020 s._add_history("lag_flag", varlist) 1137 1021 if insitu: … … 1182 1066 data = self._getabcissa(row) 1183 1067 u = self._getcoordinfo()[0] 1184 if rcParams['verbose']:1185 if u == "":u = "channel"1186 1187 1188 1189 1190 1068 if u == "": 1069 u = "channel" 1070 msg = "The current mask window unit is %s" % u 1071 i = self._check_ifs() 1072 if not i: 1073 msg += "\nThis mask is only valid for IF=%d" % (self.getif(i)) 1074 asaplog.push(msg) 1191 1075 n = self.nchan() 1192 1076 msk = _n_bools(n, False) … … 1240 1124 data = self._getabcissa(row) 1241 1125 u = self._getcoordinfo()[0] 1242 if rcParams['verbose']:1243 if u == "":u = "channel"1244 1245 1246 1247 1248 1126 if u == "": 1127 u = "channel" 1128 msg = "The current mask window unit is %s" % u 1129 i = self._check_ifs() 1130 if not i: 1131 msg += "\nThis mask is only valid for IF=%d" % (self.getif(i)) 1132 asaplog.push(msg) 1249 1133 masklist=[] 1250 1134 ist, ien = None, None … … 1464 1348 Scantable.shift_refpix(self, delta) 1465 1349 1350 @print_log_dec 1466 1351 def history(self, filename=None): 1467 1352 """\ … … 1500 1385 else: 1501 1386 msg = "Illegal file name '%s'." % (filename) 1502 if rcParams['verbose']: 1503 #print msg 1504 asaplog.push( msg ) 1505 print_log( 'ERROR' ) 1506 else: 1507 raise IOError(msg) 1508 if rcParams['verbose']: 1509 try: 1510 from IPython.genutils import page as pager 1511 except ImportError: 1512 from pydoc import pager 1513 pager(out) 1514 else: 1515 return out 1516 return 1387 raise IOError(msg) 1388 return page(out) 1517 1389 # 1518 1390 # Maths business … … 1558 1430 scanav = (scanav and 'SCAN') or 'NONE' 1559 1431 scan = (self, ) 1560 try: 1561 if align: 1562 scan = (self.freq_align(insitu=False), ) 1563 s = None 1564 if weight.upper() == 'MEDIAN': 1565 s = scantable(self._math._averagechannel(scan[0], 'MEDIAN', 1566 scanav)) 1567 else: 1568 s = scantable(self._math._average(scan, mask, weight.upper(), 1569 scanav)) 1570 except RuntimeError, msg: 1571 if rcParams['verbose']: 1572 #print msg 1573 print_log() 1574 asaplog.push( str(msg) ) 1575 print_log( 'ERROR' ) 1576 return 1577 else: raise 1432 1433 if align: 1434 scan = (self.freq_align(insitu=False), ) 1435 s = None 1436 if weight.upper() == 'MEDIAN': 1437 s = scantable(self._math._averagechannel(scan[0], 'MEDIAN', 1438 scanav)) 1439 else: 1440 s = scantable(self._math._average(scan, mask, weight.upper(), 1441 scanav)) 1578 1442 s._add_history("average_time", varlist) 1579 1443 return s … … 1854 1718 """ 1855 1719 varlist = vars() 1856 try: 1857 s = scantable(self._math._convertpol(self, poltype)) 1858 except RuntimeError, msg: 1859 if rcParams['verbose']: 1860 #print msg 1861 print_log() 1862 asaplog.push( str(msg) ) 1863 print_log( 'ERROR' ) 1864 return 1865 else: 1866 raise 1720 s = scantable(self._math._convertpol(self, poltype)) 1867 1721 s._add_history("convert_pol", varlist) 1868 1722 return s … … 2032 1886 except RuntimeError: 2033 1887 msg = "The fit failed, possibly because it didn't converge." 2034 if rcParams['verbose']: 2035 #print msg 2036 print_log() 2037 asaplog.push( str(msg) ) 2038 print_log( 'ERROR' ) 2039 return 2040 else: 2041 raise RuntimeError(msg) 1888 raise RuntimeError(msg) 2042 1889 2043 1890 … … 2550 2397 from asap.asapfit import asapfit 2551 2398 fit = asapfit(self._getfit(row)) 2552 if rcParams['verbose']: 2553 #print fit 2554 asaplog.push( '%s' %(fit) ) 2555 return 2556 else: 2557 return fit.as_dict() 2399 asaplog.push( '%s' %(fit) ) 2400 return fit.as_dict() 2558 2401 2559 2402 def flag_nans(self): … … 2649 2492 return (sum(nchans)/len(nchans) == nchans[0]) 2650 2493 2494 @print_log_dec 2651 2495 def _fill(self, names, unit, average, getpt, antenna): 2652 2496 first = True … … 2657 2501 if not os.path.exists(name): 2658 2502 msg = "File '%s' does not exists" % (name) 2659 if rcParams['verbose']:2660 asaplog.push(msg)2661 print_log( 'ERROR' )2662 return2663 2503 raise IOError(msg) 2664 2504 fullnames.append(name) … … 2673 2513 msg = "Importing %s..." % (name) 2674 2514 asaplog.push(msg, False) 2675 print_log()2676 2515 r.open(name)# antenna, -1, -1, getpt) 2677 2516 r.fill() -
trunk/python/utils.py
r1824 r1859 66 66 files = [os.path.expanduser(os.path.expandvars(path+"/"+f)) for f in os.listdir(path)] 67 67 return filter(lambda x: x.endswith(suffix),files) 68 69 def page(message): 70 verbose = False 71 try: 72 from asap.parameters import rcParams 73 verbose = rcParams['verbose'] 74 except: 75 pass 76 if verbose: 77 try: 78 from IPython.genutils import page as pager 79 except ImportError: 80 from pydoc import pager 81 pager(message) 82 return None 83 else: 84 return message
Note:
See TracChangeset
for help on using the changeset viewer.