Changes in trunk/python/asapmath.py [1920:1827]
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/python/asapmath.py
r1920 r1827 1 1 from asap.scantable import scantable 2 2 from asap.parameters import rcParams 3 from asap.logging import asaplog, asaplog_post_dec3 from asap.logging import asaplog, print_log, print_log_dec 4 4 from asap.selector import selector 5 5 from asap import asaplotgui 6 6 7 @ asaplog_post_dec7 @print_log_dec 8 8 def average_time(*args, **kwargs): 9 9 """ … … 67 67 if not isinstance(s,scantable): 68 68 msg = "Please give a list of scantables" 69 raise TypeError(msg) 69 if rcParams['verbose']: 70 #print msg 71 asaplog.push(msg) 72 print_log('ERROR') 73 return 74 else: 75 raise TypeError(msg) 70 76 if scanav: scanav = "SCAN" 71 77 else: scanav = "NONE" … … 90 96 s = scantable(stm._new_average(alignedlst, compel, mask, weight.upper(), scanav)) 91 97 s._add_history("average_time",varlist) 92 98 print_log() 93 99 return s 94 100 95 @asaplog_post_dec96 101 def quotient(source, reference, preserve=True): 97 102 """ … … 114 119 s = scantable(stm._quotient(source, reference, preserve)) 115 120 s._add_history("quotient",varlist) 121 print_log() 116 122 return s 117 123 118 @ asaplog_post_dec124 @print_log_dec 119 125 def dototalpower(calon, caloff, tcalval=0.0): 120 126 """ … … 132 138 s = scantable(stm._dototalpower(calon, caloff, tcalval)) 133 139 s._add_history("dototalpower",varlist) 140 print_log() 134 141 return s 135 142 136 @ asaplog_post_dec143 @print_log_dec 137 144 def dosigref(sig, ref, smooth, tsysval=0.0, tauval=0.0): 138 145 """ … … 152 159 s = scantable(stm._dosigref(sig, ref, smooth, tsysval, tauval)) 153 160 s._add_history("dosigref",varlist) 161 print_log() 154 162 return s 155 163 156 @ asaplog_post_dec164 @print_log_dec 157 165 def calps(scantab, scannos, smooth=1, tsysval=0.0, tauval=0.0, tcalval=0.0, verify=False): 158 166 """ 159 167 Calibrate GBT position switched data 160 168 Adopted from GBTIDL getps 161 Currently calps identify the scans as position switched data if source162 type enum is pson or psoff. The data must contains 'CAL' signal163 on/off in each integration. To identify 'CAL' on state, the source type164 enum of poncal and poffcalneed to be present in the source name field.169 Currently calps identify the scans as position switched data if they 170 contain '_ps' in the source name. The data must contains 'CAL' signal 171 on/off in each integration. To identify 'CAL' on state, the word, 'calon' 172 need to be present in the source name field. 165 173 (GBT MS data reading process to scantable automatically append these 166 174 id names to the source names) … … 176 184 tcalval: optional user specified Tcal (default is 0.0, 177 185 use Tcal value in the data) 178 verify: Verify calibration if true179 186 """ 180 187 varlist = vars() … … 183 190 ## if s is None: 184 191 ## msg = "The input data appear to contain no position-switch mode data." 185 ## raise TypeError(msg) 192 ## if rcParams['verbose']: 193 ## #print msg 194 ## asaplog.push(msg) 195 ## print_log('ERROR') 196 ## return 197 ## else: 198 ## raise TypeError(msg) 186 199 s = scantab.copy() 187 200 from asap._asap import srctype … … 192 205 except Exception, e: 193 206 msg = "The input data appear to contain no position-switch mode data." 194 raise TypeError(msg) 207 if rcParams['verbose']: 208 #print msg 209 asaplog.push(msg) 210 print_log('ERROR') 211 return 212 else: 213 raise TypeError(msg) 195 214 s.set_selection() 196 215 sel.reset() … … 198 217 if ssub is None: 199 218 msg = "No data was found with given scan numbers!" 200 raise TypeError(msg) 219 if rcParams['verbose']: 220 #print msg 221 asaplog.push(msg) 222 print_log('ERROR') 223 return 224 else: 225 raise TypeError(msg) 201 226 #ssubon = ssub.get_scan('*calon') 202 227 #ssuboff = ssub.get_scan('*[^calon]') … … 213 238 if ssubon.nrow() != ssuboff.nrow(): 214 239 msg = "mismatch in numbers of CAL on/off scans. Cannot calibrate. Check the scan numbers." 215 raise TypeError(msg) 240 if rcParams['verbose']: 241 #print msg 242 asaplog.push(msg) 243 print_log('ERROR') 244 return 245 else: 246 raise TypeError(msg) 216 247 cals = dototalpower(ssubon, ssuboff, tcalval) 217 248 #sig = cals.get_scan('*ps') … … 229 260 if sig.nscan() != ref.nscan(): 230 261 msg = "mismatch in numbers of on/off scans. Cannot calibrate. Check the scan numbers." 231 raise TypeError(msg) 262 if rcParams['verbose']: 263 #print msg 264 asaplog.push(msg) 265 print_log('ERROR') 266 return 267 else: 268 raise TypeError(msg) 232 269 233 270 #for user supplied Tsys … … 235 272 if tauval<=0.0: 236 273 msg = "Need to supply a valid tau to use the supplied Tsys" 237 raise TypeError(msg) 274 if rcParams['verbose']: 275 #print msg 276 asaplog.push(msg) 277 print_log('ERROR') 278 return 279 else: 280 raise TypeError(msg) 238 281 else: 239 282 sig.recalc_azel() 240 283 ref.recalc_azel() 241 284 #msg = "Use of user specified Tsys is not fully implemented yet." 242 #raise TypeError(msg) 285 #if rcParams['verbose']: 286 # print msg 287 # return 288 #else: 289 # raise TypeError(msg) 243 290 # use get_elevation to get elevation and 244 291 # calculate a scaling factor using the formula … … 308 355 del sel 309 356 # plot 310 asaplog.post()357 print_log() 311 358 asaplog.push('Plot only first spectrum for each [if,pol] pairs to verify calibration.') 312 asaplog.post('WARN')359 print_log('WARN') 313 360 p=asaplotgui.asaplotgui() 314 361 #nr=min(6,len(ifnos)*len(polnos)) … … 337 384 btics.append(b) 338 385 else: 339 asaplog.post()386 print_log() 340 387 asaplog.push('Only first 6 [if,pol] pairs are plotted.') 341 asaplog.post('WARN')388 print_log('WARN') 342 389 nr=6 343 390 for i in range(2*nr): … … 395 442 ### 396 443 ress._add_history("calps", varlist) 444 print_log() 397 445 return ress 398 446 399 @ asaplog_post_dec447 @print_log_dec 400 448 def calnod(scantab, scannos=[], smooth=1, tsysval=0.0, tauval=0.0, tcalval=0.0, verify=False): 401 449 """ … … 410 458 tauval: optional user specified tau value (not implemented yet) 411 459 tcalval: optional user specified Tcal value 412 verify: Verify calibration if true413 460 """ 414 461 varlist = vars() … … 422 469 ## if s is None: 423 470 ## msg = "The input data appear to contain no Nod observing mode data." 424 ## raise TypeError(msg) 471 ## if rcParams['verbose']: 472 ## #print msg 473 ## asaplog.push(msg) 474 ## print_log('ERROR') 475 ## return 476 ## else: 477 ## raise TypeError(msg) 425 478 s = scantab.copy() 426 479 sel = selector() … … 430 483 except Exception, e: 431 484 msg = "The input data appear to contain no Nod observing mode data." 432 raise TypeError(msg) 485 if rcParams['verbose']: 486 #print msg 487 asaplog.push(msg) 488 print_log('ERROR') 489 return 490 else: 491 raise TypeError(msg) 433 492 sel.reset() 434 493 del sel … … 452 511 #if len(scannos)>2: 453 512 # msg = "calnod can only process a pair of nod scans at time." 454 # raise TypeError(msg) 513 # if rcParams['verbose']: 514 # print msg 515 # return 516 # else: 517 # raise TypeError(msg) 455 518 # 456 519 #if len(scannos)==2: … … 462 525 if tauval<=0.0: 463 526 msg = "Need to supply a valid tau to use the supplied Tsys" 464 raise TypeError(msg) 527 if rcParams['verbose']: 528 #print msg 529 asaplog.push(msg) 530 print_log('ERROR') 531 return 532 else: 533 raise TypeError(msg) 465 534 else: 466 535 scantab.recalc_azel() … … 527 596 del sel 528 597 # plot 529 asaplog.post()598 print_log() 530 599 asaplog.push('Plot only first spectrum for each [if,pol] pairs to verify calibration.') 531 asaplog.post('WARN')600 print_log('WARN') 532 601 p=asaplotgui.asaplotgui() 533 602 #nr=min(6,len(ifnos)*len(polnos)) … … 556 625 btics.append(b) 557 626 else: 558 asaplog.post()627 print_log() 559 628 asaplog.push('Only first 6 [if,pol] pairs are plotted.') 560 asaplog.post('WARN')629 print_log('WARN') 561 630 nr=6 562 631 for i in range(2*nr): … … 614 683 ### 615 684 resspec._add_history("calnod",varlist) 685 print_log() 616 686 return resspec 617 687 618 @ asaplog_post_dec688 @print_log_dec 619 689 def calfs(scantab, scannos=[], smooth=1, tsysval=0.0, tauval=0.0, tcalval=0.0, verify=False): 620 690 """ 621 691 Calibrate GBT frequency switched data. 622 692 Adopted from GBTIDL getfs. 623 Currently calfs identify the scans as frequency switched data if source624 type enum is fson and fsoff. The data must contains 'CAL' signal625 on/off in each integration. To identify 'CAL' on state, the source type626 enum of foncal and foffcalneed to be present in the source name field.693 Currently calfs identify the scans as frequency switched data if they 694 contain '_fs' in the source name. The data must contains 'CAL' signal 695 on/off in each integration. To identify 'CAL' on state, the word, 'calon' 696 need to be present in the source name field. 627 697 (GBT MS data reading via scantable automatically append these 628 698 id names to the source names) … … 636 706 use Tsys in the data) 637 707 tauval: optional user specified Tau 638 verify: Verify calibration if true639 708 """ 640 709 varlist = vars() … … 647 716 # if check is None: 648 717 # msg = "The input data appear to contain no Nod observing mode data." 649 # raise TypeError(msg) 718 # if rcParams['verbose']: 719 # print msg 720 # return 721 # else: 722 # raise TypeError(msg) 650 723 s = scantab.get_scan(scannos) 651 724 del scantab … … 739 812 del sel 740 813 # plot 741 asaplog.post()814 print_log() 742 815 asaplog.push('Plot only first spectrum for each [if,pol] pairs to verify calibration.') 743 asaplog.post('WARN')816 print_log('WARN') 744 817 p=asaplotgui.asaplotgui() 745 818 #nr=min(6,len(ifnos)*len(polnos)) … … 748 821 btics=[] 749 822 if nr>3: 750 asaplog.post()823 print_log() 751 824 asaplog.push('Only first 3 [if,pol] pairs are plotted.') 752 asaplog.post('WARN')825 print_log('WARN') 753 826 nr=3 754 827 p.set_panels(rows=nr,cols=3,nplots=3*nr,ganged=False) … … 818 891 ### 819 892 resspec._add_history("calfs",varlist) 893 print_log() 820 894 return resspec 821 895 822 @ asaplog_post_dec896 @print_log_dec 823 897 def merge(*args): 824 898 """ … … 847 921 if not isinstance(s,scantable): 848 922 msg = "Please give a list of scantables" 849 raise TypeError(msg) 923 if rcParams['verbose']: 924 #print msg 925 asaplog.push(msg) 926 print_log('ERROR') 927 return 928 else: 929 raise TypeError(msg) 850 930 s = scantable(stm._merge(lst)) 851 931 s._add_history("merge", varlist) 932 print_log() 852 933 return s 853 934 854 @asaplog_post_dec855 935 def calibrate( scantab, scannos=[], calmode='none', verify=None ): 856 936 """ … … 866 946 if ( calmode == 'nod' ): 867 947 asaplog.push( 'Calibrating nod data.' ) 948 print_log() 868 949 scal = calnod( scantab, scannos=scannos, verify=verify ) 869 950 elif ( calmode == 'quotient' ): 870 951 asaplog.push( 'Calibrating using quotient.' ) 952 print_log() 871 953 scal = scantab.auto_quotient( verify=verify ) 872 954 elif ( calmode == 'ps' ): 873 955 asaplog.push( 'Calibrating %s position-switched data.' % antname ) 956 print_log() 874 957 if ( antname.find( 'APEX' ) != -1 ): 875 958 scal = apexcal( scantab, scannos, calmode, verify ) … … 880 963 elif ( calmode == 'fs' or calmode == 'fsotf' ): 881 964 asaplog.push( 'Calibrating %s frequency-switched data.' % antname ) 965 print_log() 882 966 if ( antname.find( 'APEX' ) != -1 ): 883 967 scal = apexcal( scantab, scannos, calmode, verify ) … … 888 972 elif ( calmode == 'otf' ): 889 973 asaplog.push( 'Calibrating %s On-The-Fly data.' % antname ) 974 print_log() 890 975 scal = almacal( scantab, scannos, calmode, verify ) 891 976 else: … … 930 1015 return scal 931 1016 932 @asaplog_post_dec933 1017 def splitant(filename, outprefix='',overwrite=False): 934 1018 """ … … 948 1032 """ 949 1033 # Import the table toolkit from CASA 950 import casac 951 from asap.scantable import is_ms 952 tbtool = casac.homefinder.find_home_by_name('tableHome') 953 tb = tbtool.create() 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 954 1056 # Check the input filename 955 1057 if isinstance(filename, str): … … 959 1061 if not os.path.exists(filename): 960 1062 s = "File '%s' not found." % (filename) 1063 if rcParams['verbose']: 1064 print_log() 1065 asaplog.push(s) 1066 print_log('ERROR') 1067 return 961 1068 raise IOError(s) 962 1069 # check if input file is MS 963 #if not os.path.isdir(filename) \ 964 # or not os.path.exists(filename+'/ANTENNA') \ 965 # or not os.path.exists(filename+'/table.f1'): 966 if not is_ms(filename): 1070 if not os.path.isdir(filename) \ 1071 or not os.path.exists(filename+'/ANTENNA') \ 1072 or not os.path.exists(filename+'/table.f1'): 967 1073 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 return 968 1079 raise IOError(s) 969 1080 else: 970 1081 s = "The filename should be string. " 1082 if rcParams['verbose']: 1083 print_log() 1084 asaplog.push(s) 1085 print_log('ERROR') 1086 return 971 1087 raise TypeError(s) 972 1088 # Check out put file name … … 980 1096 nant=tb.nrows() 981 1097 antnames=tb.getcol('NAME',0,nant,1) 1098 antpos=tb.getcol('POSITION',0,nant,1).transpose() 982 1099 tb.close() 983 1100 tb.open(tablename=filename,nomodify=True) 984 1101 ant1=tb.getcol('ANTENNA1',0,-1,1) 985 1102 tb.close() 986 tmpname='asapmath.splitant.tmp'987 1103 for antid in set(ant1): 988 tb.open(tablename=filename,nomodify=True) 989 tbsel=tb.query('ANTENNA1 == %s && ANTENNA2 == %s'%(antid,antid),tmpname) 990 scan=scantable(tmpname,average=False,getpt=True,antenna=int(antid)) 1104 scan=scantable(filename,average=False,getpt=True,antenna=int(antid)) 991 1105 outname=prefix+antnames[antid]+'.asap' 992 1106 scan.save(outname,format='ASAP',overwrite=overwrite) 993 tbsel.close()994 tb.close()995 del tbsel996 1107 del scan 997 1108 outfiles.append(outname) 998 os.system('rm -rf '+tmpname) 999 del tb 1109 del tb, tb2 1000 1110 return outfiles 1001 1111 1002 @asaplog_post_dec1003 1112 def _array2dOp( scan, value, mode="ADD", tsys=False ): 1004 1113 """ … … 1019 1128 del stm 1020 1129 elif len( value ) != nrow: 1021 raise ValueError( 'len(value) must be 1 or conform to scan.nrow()' ) 1130 asaplog.push( 'len(value) must be 1 or conform to scan.nrow()' ) 1131 print_log( 'ERROR' ) 1022 1132 else: 1023 1133 from asap._asap import stmath
Note:
See TracChangeset
for help on using the changeset viewer.