00001 ##################################### 00002 # 00003 # ORION-S SDtasks Use Case 00004 # Position-Switched data 00005 # Version WK 2011-06-29 (updated) 00006 # Version TT 2008-10-14 (updated) 00007 # Version STM 2007-03-04 00008 # 00009 # This is a detailed walk-through 00010 # for using the SDtasks on a 00011 # test dataset. 00012 # 00013 ##################################### 00014 import time 00015 import os 00016 00017 # NOTE: you should have already run 00018 # asap_init() 00019 # to import the ASAP tools as sd.<tool> 00020 # and the SDtasks 00021 00022 # 00023 # This is the environment variable 00024 # pointing to the head of the CASA 00025 # tree that you are running 00026 casapath=os.environ['CASAPATH'] 00027 00028 # 00029 # This bit removes old versions of the output files 00030 os.system('rm -rf sdusecase_orions* ') 00031 # 00032 # This is the path to the OrionS GBT ms in the data repository 00033 datapath=casapath.split()[0]+'/data/regression/ATST5/OrionS/OrionS_rawACSmod' 00034 # 00035 # The follwing will remove old versions of the data and 00036 # copy the data from the repository to your 00037 # current directory. Comment this out if you already have it 00038 # and don't want to recopy 00039 os.system('rm -rf OrionS_rawACSmod') 00040 copystring='cp -r '+datapath+' .' 00041 os.system(copystring) 00042 00043 # Now is the time to set some of the more useful 00044 # ASAP environment parameters (the ones that the 00045 # ASAP User Manual claims are in the .asaprc file). 00046 # These are in the Python dictionary sd.rcParams 00047 # You can see whats in it by typing: 00048 #sd.rcParams 00049 # One of them is the 'verbose' parameter which tells 00050 # ASAP whether to spew lots of verbiage during processing 00051 # or to keep quiet. The default is 00052 #sd.rcParams['verbose']=True 00053 # You can make ASAP run quietly (with only task output) with 00054 #sd.rcParams['verbose']=False 00055 00056 # Another key one is to tell ASAP to save memory by 00057 # going off the disk instead. The default is 00058 #sd.rcParams['scantable.storage']='memory' 00059 # but if you are on a machine with small memory, do 00060 #sd.rcParams['scantable.storage']='disk' 00061 00062 # You can reset back to defaults with 00063 #sd.rcdefaults 00064 00065 ########################## 00066 # 00067 # ORION-S HC3N 00068 # Position-Switched data 00069 # 00070 ########################## 00071 startTime=time.time() 00072 startProc=time.clock() 00073 00074 ########################## 00075 # List data 00076 ########################## 00077 # List the contents of the dataset 00078 # First reset parameter defaults (safe) 00079 default('sdlist') 00080 00081 # You can see its inputs with 00082 #inp('sdlist') 00083 # or just 00084 #inp 00085 # now that the defaults('sdlist') set the 00086 # taskname='sdlist' 00087 # 00088 # Set the name of the GBT ms file 00089 sdfile = 'OrionS_rawACSmod' 00090 00091 # Set an output file in case we want to 00092 # refer back to it 00093 outfile = 'sdusecase_orions_summary.txt' 00094 sdlist() 00095 00096 # You could also just type 00097 #go 00098 00099 # You should see something like: 00100 # 00101 #-------------------------------------------------------------------------------- 00102 # Scan Table Summary 00103 #-------------------------------------------------------------------------------- 00104 #Beams: 1 00105 #IFs: 26 00106 #Polarisations: 2 (linear) 00107 #Channels: 8192 00108 # 00109 #Observer: Joseph McMullin 00110 #Obs Date: 2006/01/19/01:45:58 00111 #Project: AGBT06A_018_01 00112 #Obs. Type: OffOn:PSWITCHOFF:TPWCAL 00113 #Antenna Name: GBT 00114 #Flux Unit: Jy 00115 #Rest Freqs: [4.5490258e+10] [Hz] 00116 #Abcissa: Channel 00117 #Selection: none 00118 # 00119 #Scan Source Time Integration 00120 # Beam Position (J2000) 00121 # IF Frame RefVal RefPix Increment 00122 #-------------------------------------------------------------------------------- 00123 # 20 OrionS_psr 01:45:58 4 x 30.0s 00124 # 0 05:15:13.5 -05.24.08.2 00125 # 0 LSRK 4.5489354e+10 4096 6104.233 00126 # 1 LSRK 4.5300785e+10 4096 6104.233 00127 # 2 LSRK 4.4074929e+10 4096 6104.233 00128 # 3 LSRK 4.4166215e+10 4096 6104.233 00129 # 21 OrionS_ps 01:48:38 4 x 30.0s 00130 # 0 05:35:13.5 -05.24.08.2 00131 # 0 LSRK 4.5489354e+10 4096 6104.233 00132 # 1 LSRK 4.5300785e+10 4096 6104.233 00133 # 2 LSRK 4.4074929e+10 4096 6104.233 00134 # 3 LSRK 4.4166215e+10 4096 6104.233 00135 # 22 OrionS_psr 01:51:21 4 x 30.0s 00136 # 0 05:15:13.5 -05.24.08.2 00137 # 0 LSRK 4.5489354e+10 4096 6104.233 00138 # 1 LSRK 4.5300785e+10 4096 6104.233 00139 # 2 LSRK 4.4074929e+10 4096 6104.233 00140 # 3 LSRK 4.4166215e+10 4096 6104.233 00141 # 23 OrionS_ps 01:54:01 4 x 30.0s 00142 # 0 05:35:13.5 -05.24.08.2 00143 # 0 LSRK 4.5489354e+10 4096 6104.233 00144 # 1 LSRK 4.5300785e+10 4096 6104.233 00145 # 2 LSRK 4.4074929e+10 4096 6104.233 00146 # 3 LSRK 4.4166215e+10 4096 6104.233 00147 # 24 OrionS_psr 02:01:47 4 x 30.0s 00148 # 0 05:15:13.5 -05.24.08.2 00149 # 12 LSRK 4.3962126e+10 4096 6104.2336 00150 # 13 LSRK 4.264542e+10 4096 6104.2336 00151 # 14 LSRK 4.159498e+10 4096 6104.2336 00152 # 15 LSRK 4.3422823e+10 4096 6104.2336 00153 # 25 OrionS_ps 02:04:27 4 x 30.0s 00154 # 0 05:35:13.5 -05.24.08.2 00155 # 12 LSRK 4.3962126e+10 4096 6104.2336 00156 # 13 LSRK 4.264542e+10 4096 6104.2336 00157 # 14 LSRK 4.159498e+10 4096 6104.2336 00158 # 15 LSRK 4.3422823e+10 4096 6104.2336 00159 # 26 OrionS_psr 02:07:10 4 x 30.0s 00160 # 0 05:15:13.5 -05.24.08.2 00161 # 12 LSRK 4.3962126e+10 4096 6104.2336 00162 # 13 LSRK 4.264542e+10 4096 6104.2336 00163 # 14 LSRK 4.159498e+10 4096 6104.2336 00164 # 15 LSRK 4.3422823e+10 4096 6104.2336 00165 # 27 OrionS_ps 02:09:51 4 x 30.0s 00166 # 0 05:35:13.5 -05.24.08.2 00167 # 12 LSRK 4.3962126e+10 4096 6104.2336 00168 # 13 LSRK 4.264542e+10 4096 6104.2336 00169 # 14 LSRK 4.159498e+10 4096 6104.2336 00170 # 15 LSRK 4.3422823e+10 4096 6104.2336 00171 00172 # The HC3N and CH3OH lines are in IFs 0 and 2 respectively 00173 # of scans 20,21,22,23. We will pull these out in our 00174 # calibration. 00175 00176 ########################## 00177 # Calibrate data 00178 ########################## 00179 # We will use the sdreduce task to calibrate the data. 00180 # Set the defaults 00181 default('sdreduce') 00182 00183 # You can see the inputs with 00184 #inp 00185 00186 # Set our sdfile (which would have been set from our run of 00187 # sdlist if we were not cautious and reset defaults). 00188 sdfile = 'OrionS_rawACSmod' 00189 00190 # Lets leave the spectral axis in channels for now 00191 specunit = 'channel' 00192 00193 # This is position-switched data so we tell sdreduce this 00194 calmode = 'ps' 00195 00196 # For GBT data, it is safest to not have scantable pre-average 00197 # integrations within scans. 00198 average = True 00199 scanaverage = False 00200 00201 # We do want sdreduce to average up scans and polarization after 00202 # calibration however. 00203 timeaverage = True 00204 tweight='tintsys' 00205 polaverage = True 00206 pweight='tsys' 00207 00208 # Do an atmospheric optical depth (attenuation) correction 00209 # Input the zenith optical depth at 43 GHz 00210 tau = 0.09 00211 00212 # Select our scans and IFs (for HC3N) 00213 scanlist = [20,21,22,23] 00214 iflist = [0] 00215 00216 # We do not require selection by field name (they are all 00217 # the same except for on and off) 00218 field = '' 00219 00220 # We will do some spectral smoothing 00221 # For this demo we will use boxcar smoothing rather than 00222 # the default 00223 #kernel='hanning' 00224 # We will set the width of the kernel to 5 channels 00225 kernel = 'boxcar' 00226 kwidth = 5 00227 00228 # We wish to fit out a baseline from the spectrum 00229 # The GBT has particularly nasty baselines :( 00230 # We will let ASAP use auto_poly_baseline mode 00231 # but tell it to drop the 1000 edge channels from 00232 # the beginning and end of the spectrum. 00233 # A 2nd-order polynomial will suffice for this test. 00234 # You might try higher orders for fun. 00235 blmode = 'auto' 00236 blpoly = 2 00237 edge = [1000] 00238 00239 # We will not give it regions as an input mask 00240 # though you could, with something like 00241 #masklist=[[1000,3000],[5000,7000]] 00242 masklist = [] 00243 00244 # By default, we will not get plots in sdreduce (but 00245 # can make them using sdplot). 00246 plotlevel = 0 00247 # But if you wish to see a final spectrum, set 00248 #plotlevel = 1 00249 # or even 00250 #plotlevel = 2 00251 # to see intermediate plots and baselining output. 00252 00253 # Now we give the name for the output file 00254 outfile = 'sdusecase_orions_hc3n.asap' 00255 00256 # We will write it out in ASAP scantable format 00257 outform = 'asap' 00258 00259 # You can look at the inputs with 00260 #inp 00261 00262 # Before running, lets save the inputs in case we want 00263 # to come back and re-run the calibration. 00264 saveinputs('sdreduce','sdreduce.orions.save') 00265 # These can be recovered by 00266 #execfile 'sdreduce.orions.save' 00267 00268 # We are ready to calibrate 00269 sdreduce() 00270 00271 # Note that after the task ran, it produced a file 00272 # sdreduce.last which contains the inputs from the last 00273 # run of the task (all tasks do this). You can recover 00274 # this (anytime before sdreduce is run again) with 00275 #execfile 'sdreduce.last' 00276 00277 ########################## 00278 # List data 00279 ########################## 00280 # List the contents of the calibrated dataset 00281 # Set the input to the just created file 00282 sdfile = outfile 00283 outfile = '' 00284 sdlist() 00285 00286 # You should see: 00287 # 00288 #-------------------------------------------------------------------------------- 00289 # Scan Table Summary 00290 #-------------------------------------------------------------------------------- 00291 #Beams: 1 00292 #IFs: 26 00293 #Polarisations: 1 (linear) 00294 #Channels: 8192 00295 # 00296 #Observer: Joseph McMullin 00297 #Obs Date: 2006/01/19/01:45:58 00298 #Project: AGBT06A_018_01 00299 #Obs. Type: OffOn:PSWITCHOFF:TPWCAL 00300 #Antenna Name: GBT 00301 #Flux Unit: K 00302 #Rest Freqs: [4.5490258e+10] [Hz] 00303 #Abcissa: Channel 00304 #Selection: none 00305 # 00306 #Scan Source Time Integration 00307 # Beam Position (J2000) 00308 # IF Frame RefVal RefPix Increment 00309 #-------------------------------------------------------------------------------- 00310 # 0 OrionS_ps 01:52:05 1 x 08:00.5 00311 # 0 05:35:13.5 -05.24.08.2 00312 # 0 LSRK 4.5489354e+10 4096 6104.233 00313 # 00314 # Note that our scans are now collapsed (timeaverage=True) but we still have 00315 # our IF 0 00316 00317 ########################## 00318 # Plot data 00319 ########################## 00320 default('sdplot') 00321 00322 # The file we produced after calibration 00323 # (if we hadn't reset defaults it would have 00324 # been set - note that sdplot,sdfit,sdstat use 00325 # sdfile as the input file, which is the output 00326 # file of sdreduce). 00327 sdfile = 'sdusecase_orions_hc3n.asap' 00328 00329 # Lets just go ahead and plot it up as-is 00330 sdplot() 00331 00332 # Looks ok. Plot with x-axis in GHz 00333 specunit='GHz' 00334 sdplot() 00335 00336 # Note that the rest frequency in the scantable 00337 # is set correctly to the HCCCN line at 45.490 GHz. 00338 # So you can plot the spectrum in km/s 00339 specunit='km/s' 00340 sdplot() 00341 00342 # Zoom in 00343 sprange=[-100,50] 00344 sdplot() 00345 00346 # Lets plot up the lines to be sure 00347 # We have to go back to GHz for this 00348 # (known deficiency in ASAP) 00349 specunit='GHz' 00350 sprange=[45.48,45.51] 00351 linecat='all' 00352 sdplot() 00353 00354 # Too many lines! Focus on the HC3N ones 00355 linecat='HCCCN' 00356 sdplot() 00357 00358 # Finally, we can convert from K to Jy 00359 # using the aperture efficiencies we have 00360 # coded into the sdtasks 00361 # For GBT data, do not set telescopeparm 00362 fluxunit='Jy' 00363 telescopeparm='' 00364 sdplot() 00365 00366 # Lets save this plot 00367 outfile='sdusecase_orions_hc3n.eps' 00368 sdplot() 00369 00370 ########################## 00371 # Off-line Statistics 00372 ########################## 00373 # Now do some region statistics 00374 # First the line-free region 00375 # Set parameters 00376 default('sdstat') 00377 sdfile = 'sdusecase_orions_hc3n.asap' 00378 00379 # Keep the default spectrum and flux units 00380 # K and channel 00381 fluxunit = '' 00382 specunit = '' 00383 00384 # Pick out a line-free region 00385 # You can bring up a default sdplot again 00386 # to check this 00387 masklist = [[5000,7000]] 00388 00389 # This is a line-free region so we don't need 00390 # to invert the mask 00391 invertmask = False 00392 00393 # You can check with 00394 #inp 00395 00396 # sdstat returns some results in 00397 # the Python dictionary. You can assign 00398 # this to a variable 00399 off_stat=sdstat() 00400 00401 # and look at it 00402 off_stat 00403 # which should give 00404 # {'eqw': 38.563105620704945, 00405 # 'max': 0.15543246269226074, 00406 # 'mean': -0.0030361821409314871, 00407 # 'median': -0.0032975673675537109, 00408 # 'min': -0.15754437446594238, 00409 # 'rms': 0.047580458223819733, 00410 # 'stddev': 0.047495327889919281, 00411 # 'sum': -6.0754003524780273} 00412 00413 00414 #You see it has some keywords for the various 00415 #stats. We want the standard deviation about 00416 #the mean, or 'stddev' 00417 print "The off-line std. deviation = ",off_stat['stddev'] 00418 # which should give 00419 # The off-line std. deviation = 0.0474953278899 00420 00421 # or better formatted (using Python I/O formatting) 00422 print "The off-line std. deviation = %5.3f K" % (off_stat['stddev']) 00423 # which should give 00424 # The off-line std. deviation = 0.047 K 00425 00426 ########################## 00427 # On-line Statistics 00428 ########################## 00429 # Now do the line region 00430 # Continue setting or resetting parameters 00431 masklist = [[3900,4200]] 00432 00433 line_stat=sdstat() 00434 00435 # look at these 00436 line_stat 00437 # which gives 00438 # {'eqw': 73.335154614280981, 00439 # 'max': 0.92909121513366699, 00440 # 'mean': 0.22636228799819946, 00441 # 'median': 0.10317134857177734, 00442 # 'min': -0.13283586502075195, 00443 # 'rms': 0.35585442185401917, 00444 # 'stddev': 0.27503398060798645, 00445 # 'sum': 68.135047912597656} 00446 00447 # of particular interest are the max value 00448 print "The on-line maximum = %5.3f K" % (line_stat['max']) 00449 # which gives 00450 # The on-line maximum = 0.929 K 00451 00452 # and the estimated equivalent width (in channels) 00453 # which is the sum/max 00454 print "The estimated equivalent width = %5.1f channels" % (line_stat['eqw']) 00455 # which gives 00456 # The estimated equivalent width = 73.3 channels 00457 00458 ########################## 00459 # Line Fitting 00460 ########################## 00461 # Now we are ready to do some line fitting 00462 # Default the parameters 00463 default('sdfit') 00464 00465 # Set our input file 00466 sdfile = 'sdusecase_orions_hc3n.asap' 00467 00468 # Stick to defaults 00469 # fluxunit = 'K', specunit = 'channel' 00470 fluxunit = '' 00471 specunit = '' 00472 00473 # We will try auto-fitting first 00474 fitmode = 'auto' 00475 # A single Gaussian 00476 nfit = [1] 00477 # Leave the auto-parameters to their defaults for 00478 # now, except ignore the edge channels 00479 edge = [1000] 00480 00481 # Lets see a plot while doing this 00482 plotlevel = 1 00483 00484 # Save the fit output in a file 00485 outfile = 'sdusecase_orions_hc3n.fit' 00486 00487 # Go ahead and do the fit 00488 fit_stat=sdfit() 00489 00490 # If you had verbose mode on, you probably saw something 00491 # like: 00492 # 00493 # 0: peak = 0.811 K , centre = 4091.041 channel, FWHM = 72.900 channel 00494 # area = 62.918 K channel 00495 # 00496 00497 # returned dictionary is stored in a variable, fit_stat 00498 fit_stat 00499 # 00500 # {'cent': [[4091.04052734375, 0.72398632764816284]], 00501 # 'fwhm': [[72.899894714355469, 1.7048574686050415]], 00502 # 'nfit': 1, 00503 # 'peak': [[0.81080442667007446, 0.016420882195234299]]} 00504 # 00505 # So you can write them out or test them: 00506 print "The line-fit parameters were:" 00507 print " maximum = %6.3f +/- %6.3f K" % (fit_stat['peak'][0][0],fit_stat['peak'][0][1]) 00508 print " center = %6.1f +/- %6.1f channels" % (fit_stat['cent'][0][0],fit_stat['cent'][0][1]) 00509 print " FWHM = %6.2f +/- %6.2f channels" % (fit_stat['fwhm'][0][0],fit_stat['fwhm'][0][1]) 00510 # 00511 # Which gives: 00512 # The line-fit parameters were: 00513 # maximum = 0.811 +/- 0.016 K 00514 # center = 4091.0 +/- 0.7 channels 00515 # FWHM = 72.90 +/- 1.70 channels 00516 00517 # We can do the fit in km/s also 00518 specunit = 'km/s' 00519 # For some reason we need to help it along with a mask 00520 maskline = [-50,0] 00521 00522 outfile = 'sdusecase_orions_hc3n_kms.fit' 00523 fit_stat_kms=sdfit() 00524 # Should give (if in verbose mode) 00525 # 0: peak = 0.811 K , centre = -27.134 km/s, FWHM = 2.933 km/s 00526 # area = 2.531 K km/s 00527 # 00528 00529 # with 00530 fit_stat_kms 00531 # giving 00532 # {'cent': [[-27.133651733398438, 0.016480101272463799]], 00533 # 'fwhm': [[2.93294358253479, 0.038807671517133713]], 00534 # 'nfit': 1, 00535 # 'peak': [[0.81080895662307739, 0.0092909494414925575]]} 00536 00537 00538 print "The line-fit parameters were:" 00539 print " maximum = %6.3f +/- %6.3f K" % (fit_stat_kms['peak'][0][0],fit_stat_kms['peak'][0][1]) 00540 print " center = %6.2f +/- %6.2f km/s" % (fit_stat_kms['cent'][0][0],fit_stat_kms['cent'][0][1]) 00541 print " FWHM = %6.4f +/- %6.4f km/s" % (fit_stat_kms['fwhm'][0][0],fit_stat_kms['fwhm'][0][1]) 00542 00543 # The line-fit parameters were: 00544 # maximum = 0.811 +/- 0.009 K 00545 # center = -27.13 +/- 0.02 km/s 00546 # FWHM = 2.9329 +/- 0.0388 km/s 00547 00548 ########################## 00549 # 00550 # End ORION-S Use Case 00551 # 00552 ##########################