00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 import time
00013 import os
00014 import pickle
00015
00016
00017
00018
00019
00020
00021
00022 scriptmode = True
00023
00024
00025
00026
00027
00028
00029
00030 pathname=os.environ.get('CASAPATH').split()[0]
00031
00032
00033 prefix = 'polcal_20080224.cband.all'
00034
00035
00036 os.system('rm -rf '+prefix+'*')
00037
00038
00039
00040
00041 importmode = 'vla'
00042
00043
00044
00045
00046
00047
00048
00049
00050 datafile = ['POLCA_20080224_1']
00051
00052
00053 exportproject = 'POLCA'
00054 exportband = 'C'
00055
00056
00057 usespw = ''
00058 usespwlist = ['0','1']
00059
00060
00061 msfile = prefix + '.ms'
00062
00063
00064 gtable = prefix + '.gcal'
00065 ftable = prefix + '.fluxscale'
00066 ptable = prefix + '.pcal'
00067 xtable = prefix + '.polx'
00068
00069
00070 myquackinterval = 14.0
00071
00072
00073 flagants = ''
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091 gaincalfield = ['0137+331','2202+422','1743-038','1924-292','2136+006','2253+161','2355+498','0319+415','0359+509']
00092
00093
00094 targets = []
00095
00096
00097 fieldgain = ''
00098 if ( len(gaincalfield) > 0 ):
00099 for fn in range(len(gaincalfield)):
00100 if ( fn > 0 ):
00101 fieldgain += ','
00102 fieldgain += gaincalfield[fn]
00103
00104 fieldtargets = ''
00105 if ( len(targets) > 0 ):
00106 for fn in range(len(targets)):
00107 if ( fn > 0 ):
00108 fieldtargets += ','
00109 fieldtargets += targets[fn]
00110
00111
00112
00113 srclist = gaincalfield + targets
00114
00115
00116
00117
00118
00119 fluxcaldir = pathname + '/data/nrao/VLA/CalModels/'
00120
00121
00122
00123
00124 fluxcalfield = '0137+331'
00125 fluxcalmodel = '3C48_C.im'
00126 gaincalfield = ''
00127 usegaincurve = False
00128 gainopacity = 0.0
00129 calrefant = 'VA15'
00130 gainsolint = 20.0
00131 polcalfield = '2202+422'
00132 polcalmode = 'D+QU'
00133 polduvrange = ''
00134 setpolmodel = True
00135 polxfield = '0137+331'
00136 polxuvrange = ''
00137
00138 setjymode = 'set'
00139
00140
00141 srcsplitms = prefix + '.split.ms'
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160 clnalg = 'hogbom'
00161 usecsclean = False
00162 clnimsize = 288
00163 clncell = 0.4
00164
00165 clniter = 200
00166
00167
00168
00169
00170 clthreshold = 1.3
00171
00172
00173 clncenter = clnimsize/2
00174 clnblc = clncenter - clnimsize/8
00175 clntrc = clncenter + clnimsize/8
00176
00177 clnblc = clncenter - 10
00178 clntrc = clncenter + 10
00179 centerbox = [clnblc,clnblc,clntrc,clntrc]
00180
00181 myclnbox = centerbox
00182
00183
00184
00185 aipsmodel = {}
00186
00187
00188
00189
00190
00191
00192
00193
00194 fcalmodel = {}
00195
00196
00197 fcalfield = {}
00198
00199
00200 fcalfield['0'] = [5.405,0,0,0]
00201 fcalfield['1'] = [5.458,0,0,0]
00202 fcalmodel['0137+331'] = fcalfield
00203
00204
00205 fcalfield = {}
00206 fcalfield['0'] = [2.465,0,0,0]
00207 fcalfield['1'] = [2.461,0,0,0]
00208 fcalmodel['2202+422'] = fcalfield
00209
00210
00211
00212 pcalmodel = {}
00213
00214
00215 pcalfield = {}
00216
00217
00218
00219 pcalfield['0'] = [5.405,0.041,-148.0]
00220 pcalfield['1'] = [5.458,0.041,-148.0]
00221 pcalmodel['0137+331'] = pcalfield
00222
00223
00224 pcalfield = {}
00225 pcalfield['0'] = [1.0,0.072,-55.00]
00226 pcalfield['1'] = [1.0,0.072,-55.00]
00227 pcalmodel['2202+422'] = pcalfield
00228
00229
00230
00231 print '--Setting up Polarization models--'
00232
00233 polmodel = {}
00234 for field in pcalmodel.keys() :
00235 spwmodel = {}
00236
00237 for spw in usespwlist:
00238 ipol = pcalmodel[field][spw][0]
00239 fpol = pcalmodel[field][spw][1]
00240 rlpd_deg = pcalmodel[field][spw][2]
00241 rlpd = rlpd_deg*pl.pi/180.0
00242 ppol = ipol*fpol
00243 qpol = ppol*cos(rlpd)
00244 upol = ppol*sin(rlpd)
00245 fluxdensity=[ipol,qpol,upol,0.0]
00246
00247 pmodel = {}
00248 pmodel['rlpd_deg'] = rlpd_deg
00249 pmodel['rlpd'] = rlpd
00250 pmodel['fpol'] = fpol
00251
00252 fmodel = {}
00253 fmodel['flux'] = fluxdensity
00254 fmodel['poln'] = pmodel
00255 spwmodel[spw] = fmodel
00256
00257 polmodel[field] = spwmodel
00258
00259 print "Created polmodel dictionary"
00260 print polmodel
00261
00262
00263
00264
00265
00266 if ( importmode == 'vla' ):
00267
00268
00269
00270 print '--ImportVLA--'
00271 default('importvla')
00272
00273 print "Use importvla to read VLA Export and make an MS"
00274
00275 archivefiles = datafile
00276 vis = msfile
00277 bandname = exportband
00278 autocorr = False
00279 antnamescheme = 'new'
00280 project = exportproject
00281
00282 saveinputs('importvla',prefix+'.importvla.saved')
00283 importvla()
00284 elif ( importmode == 'fits' ):
00285
00286
00287
00288 print '--ImportUVFITS--'
00289 default('importuvfits')
00290
00291 print "Use importuvfits to read UVFITS and make an MS"
00292
00293 fitsfile = datafile
00294 vis = msfile
00295 async = False
00296
00297 saveinputs('importuvfits',prefix+'.importuvfits.saved')
00298 importuvfits()
00299 else:
00300
00301
00302
00303 print '--MS Copy--'
00304 print "Copying "+datafile+" to "+msfile
00305
00306 os.system('cp -r '+datafile+' '+msfile)
00307 vis = msfile
00308
00309
00310
00311
00312 print '--Listobs--'
00313
00314 print "List summary of MS"
00315
00316 listobs()
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423 if ( myquackinterval > 0.0 ):
00424
00425
00426
00427 print '--Flagdata--'
00428 default('tflagdata')
00429
00430 print "Quacking scan beginnings using interval "+str(myquackinterval)
00431
00432 vis = msfile
00433 correlation = ''
00434 field = ''
00435 antenna = ''
00436 spw = usespw
00437 mode = 'quack'
00438 quackinterval = myquackinterval
00439
00440 saveinputs('flagdata',prefix+'.flagdata.quack.saved')
00441 tflagdata()
00442
00443
00444
00445
00446 default('flagmanager')
00447
00448 print "Now will use flagmanager to save the flags"
00449
00450 vis = msfile
00451 mode = 'save'
00452 versionname = 'quack'
00453 comment = 'Quack '+str(myquackinterval)
00454 merge = 'replace'
00455
00456 saveinputs('flagmanager',prefix+'.flagmanager.quack.saved')
00457 flagmanager()
00458
00459
00460
00461 if (flagants != '' and not flagants.isspace() ):
00462 print '--Flagdata--'
00463 default('tflagdata')
00464
00465 print "Flag all data to AN "+flagants
00466
00467 vis = msfile
00468 correlation = ''
00469 field = ''
00470 spw = usespw
00471 mode = 'manual'
00472 antenna = flagants
00473
00474 saveinputs('flagdata',prefix+'.flagdata.ants.saved')
00475 tflagdata()
00476
00477
00478
00479
00480 default('flagmanager')
00481
00482 print "Now will use flagmanager to save the flags"
00483
00484 vis = msfile
00485 mode = 'save'
00486 versionname = 'antflags'
00487 comment = 'flag AN '+flagants
00488 merge = 'replace'
00489
00490 saveinputs('flagmanager',prefix+'.flagmanager.ants.saved')
00491 flagmanager()
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501 if ( setjymode == 'flux' ):
00502 print '--Setjy--'
00503 default('setjy')
00504
00505 vis = msfile
00506
00507 print "Use setjy to set flux of "+fluxcalfield+" to point model"
00508 field = fluxcalfield
00509 spw = usespw
00510
00511
00512 modimage = fluxcaldir + fluxcalmodel
00513
00514
00515 for spw in usespwlist:
00516 fluxdensity = fcalmodel[fluxcalfield][spw]
00517 print "Setting SPW "+spw+" to "+str(fluxdensity)
00518 saveinputs('setjy',prefix+'.setjy.'+spw+'.saved')
00519 setjy()
00520
00521 elif ( setjymode == 'ft' ):
00522 print '--FT--'
00523
00524 default('ft')
00525 vis = msfile
00526 field = fluxcalfield
00527
00528 for spw in usespwlist:
00529 model = fluxcaldir + fluxcalmodel+'_'+spw+'_IQUV.model'
00530 print "Use FT to set model"+model
00531 saveinputs('ft',prefix+'.ft.0.saved')
00532 ft()
00533
00534 else:
00535 print '--Setjy--'
00536 default('setjy')
00537
00538 vis = msfile
00539
00540 print "Use setjy to set flux of "+fluxcalfield
00541 field = fluxcalfield
00542 spw = usespw
00543
00544
00545 modimage = fluxcaldir + fluxcalmodel
00546
00547 saveinputs('setjy',prefix+'.setjy.saved')
00548 setjy()
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559 print "Look in logger for the fluxes (should be 5.405 and 5.458 Jy)"
00560
00561
00562
00563
00564
00565 print '--Gaincal--'
00566 default('gaincal')
00567
00568 print "Solve for antenna gains on sources "+gaincalfield
00569 print "We have 2 single-channel continuum spw"
00570
00571 vis = msfile
00572
00573
00574 print "Output gain table name is "+gtable
00575 caltable = gtable
00576
00577
00578
00579
00580
00581 field = fieldgain
00582 print "Calibrating using fields "+field
00583
00584
00585 spw = usespw
00586
00587
00588 gaincurve = usegaincurve
00589 opacity = gainopacity
00590
00591
00592 parang = False
00593
00594
00595 gaintype = 'G'
00596 solint = gainsolint
00597 calmode = 'ap'
00598
00599
00600 refant = calrefant
00601
00602
00603 minsnr = 3
00604
00605 saveinputs('gaincal',prefix+'.gaincal.saved')
00606 gaincal()
00607
00608
00609
00610
00611
00612
00613
00614 print '--Listcal--'
00615
00616 listfile = caltable + '.list'
00617
00618 print "Listing calibration to file "+listfile
00619
00620 listcal()
00621
00622
00623
00624
00625
00626
00627 print '--Fluxscale--'
00628 default('fluxscale')
00629
00630 print "Use fluxscale to rescale gain table to make new one"
00631
00632 vis = msfile
00633
00634
00635 ftable = prefix + '.fluxscale'
00636 fluxtable = ftable
00637
00638 print "Output scaled gain cal table is "+ftable
00639
00640
00641 caltable = gtable
00642
00643
00644 reference = fluxcalfield
00645
00646
00647
00648 transfer = fieldgain
00649
00650 saveinputs('fluxscale',prefix+'.fluxscale.saved')
00651 fluxscale()
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679 print '--Listcal--'
00680
00681 caltable = ftable
00682 listfile = caltable + '.list'
00683
00684 print "Listing calibration to file "+listfile
00685
00686 listcal()
00687
00688
00689
00690
00691
00692 print '--Plotcal--'
00693
00694 iteration = ''
00695 showgui = False
00696
00697 xaxis = 'time'
00698 yaxis = 'amp'
00699 figfile = caltable + '.plot.amp.png'
00700 print "Plotting calibration to file "+figfile
00701 saveinputs('plotcal',prefix+'.plotcal.fluxscale.amp.saved')
00702 plotcal()
00703
00704 xaxis = 'time'
00705 yaxis = 'phase'
00706 figfile = caltable + '.plot.phase.png'
00707 print "Plotting calibration to file "+figfile
00708 saveinputs('plotcal',prefix+'.plotcal.fluxscale.phase.saved')
00709 plotcal()
00710
00711 xaxis = 'antenna'
00712 yaxis = 'amp'
00713 figfile = caltable + '.plot.antamp.png'
00714 print "Plotting calibration to file "+figfile
00715 saveinputs('plotcal',prefix+'.plotcal.fluxscale.antamp.saved')
00716 plotcal()
00717
00718 if ( setpolmodel and polcalmode.count('X') > 0 ):
00719
00720
00721
00722
00723
00724 print '--Setjy--'
00725 default('setjy')
00726
00727 vis = msfile
00728
00729 print "Use setjy to set IQU fluxes of "+polxfield
00730 field = polxfield
00731
00732 for spw in usespwlist:
00733 fluxdensity = polmodel[field][spw]['flux']
00734
00735 saveinputs('setjy',prefix+'.setjy.polspw.'+spw+'.saved')
00736 setjy()
00737
00738
00739
00740
00741
00742
00743 print '--PolCal--'
00744 default('polcal')
00745
00746 print "Polarization D-term Calibration (linear approx) on "+polcalfield
00747
00748 vis = msfile
00749
00750
00751 gaintable = gtable
00752
00753
00754 gaincurve = usegaincurve
00755 opacity = gainopacity
00756
00757
00758 ptable = prefix + '.pcal'
00759 caltable = ptable
00760
00761
00762 field = polcalfield
00763 spw = usespw
00764
00765 selectdata=True
00766 uvrange = polduvrange
00767
00768
00769 poltype = polcalmode
00770
00771
00772 solint = 86400.
00773
00774
00775 refant = calrefant
00776
00777
00778 minsnr = 3
00779
00780 saveinputs('polcal',prefix+'.polcal.saved')
00781 polcal()
00782
00783
00784
00785
00786
00787
00788
00789
00790
00791
00792
00793 print '--Listcal--'
00794
00795 listfile = caltable + '.list'
00796
00797 print "Listing calibration to file "+listfile
00798
00799 listcal()
00800
00801
00802
00803
00804
00805 print '--Plotcal--'
00806
00807 iteration = ''
00808 showgui = False
00809
00810 xaxis = 'real'
00811 yaxis = 'imag'
00812 figfile = caltable + '.plot.reim.png'
00813 print "Plotting calibration to file "+figfile
00814 saveinputs('plotcal',prefix+'.plotcal.polcal.d.reim.saved')
00815 plotcal()
00816
00817 xaxis = 'antenna'
00818 yaxis = 'amp'
00819 figfile = caltable + '.plot.antamp.png'
00820 print "Plotting calibration to file "+figfile
00821 saveinputs('plotcal',prefix+'.plotcal.polcal.d.antamp.saved')
00822 plotcal()
00823
00824 xaxis = 'antenna'
00825 yaxis = 'phase'
00826 figfile = caltable + '.plot.antphase.png'
00827 print "Plotting calibration to file "+figfile
00828 saveinputs('plotcal',prefix+'.plotcal.polcal.d.antphase.saved')
00829 plotcal()
00830
00831 xaxis = 'antenna'
00832 yaxis = 'snr'
00833 figfile = caltable + '.plot.antsnr.png'
00834 print "Plotting calibration to file "+figfile
00835 saveinputs('plotcal',prefix+'.plotcal.polcal.d.antsnr.saved')
00836 plotcal()
00837
00838
00839
00840
00841
00842 dopolx = False
00843 if ( pcalmodel.has_key(polxfield) ):
00844 dopolx = True
00845
00846 if ( setpolmodel and not polcalmode.count('X') > 0 ):
00847
00848
00849
00850
00851
00852
00853 print '--Setjy--'
00854 default('setjy')
00855
00856 vis = msfile
00857
00858 print "Use setjy to set IQU fluxes of "+polxfield
00859 field = polxfield
00860
00861 for spw in usespwlist:
00862 fluxdensity = polmodel[field][spw]['flux']
00863
00864 saveinputs('setjy',prefix+'.setjy.polspw.'+spw+'.saved')
00865 setjy()
00866
00867
00868
00869
00870
00871
00872
00873 print '--PolCal--'
00874 default('polcal')
00875
00876 print "Polarization R-L Phase Calibration (linear approx)"
00877
00878 vis = msfile
00879
00880
00881 gaintable = [gtable,ptable]
00882
00883
00884 gaincurve = usegaincurve
00885 opacity = gainopacity
00886
00887
00888 xtable = prefix + '.polx'
00889 caltable = xtable
00890
00891
00892 field = polxfield
00893 spw = usespw
00894
00895 selectdata=True
00896 uvrange = polxuvrange
00897
00898
00899 poltype = 'X'
00900 solint = 86400.
00901
00902
00903 refant = calrefant
00904
00905
00906 minsnr = 3
00907
00908 saveinputs('polcal',prefix+'.polcal.X.saved')
00909 polcal()
00910
00911
00912
00913
00914
00915
00916
00917
00918
00919
00920
00921
00922
00923
00924
00925
00926
00927
00928
00929
00930
00931
00932
00933 print '--Plotcal--'
00934
00935 xaxis = 'antenna'
00936 yaxis = 'phase'
00937 iteration = ''
00938
00939 showgui = False
00940 figfile = caltable + '.plot.png'
00941
00942 print "Plotting calibration to file "+figfile
00943 saveinputs('plotcal',prefix+'.plotcal.polcal.x.antphase.saved')
00944 plotcal()
00945
00946 else:
00947 if (polxfield != '' and not polxfield.isspace() ):
00948 print "DO NOT HAVE PCALMODEL FOR "+polxfield
00949 print "PCALMODEL = ",pcalmodel
00950
00951
00952
00953
00954
00955
00956
00957
00958
00959 print '--ApplyCal--'
00960 default('applycal')
00961
00962 print "This will apply the calibration to the DATA"
00963 print "Fills CORRECTED_DATA"
00964
00965 vis = msfile
00966
00967
00968 if (dopolx):
00969 gaintable = [ftable,ptable,xtable]
00970 else:
00971 gaintable = [ftable,ptable]
00972
00973
00974 gaincurve = usegaincurve
00975 opacity = gainopacity
00976
00977
00978 spw = usespw
00979 selectdata = False
00980
00981
00982 parang = True
00983
00984
00985 field = fieldgain
00986 gainselect = field
00987 print "Applying calibration to gain calibrators "+field
00988
00989 saveinputs('applycal',prefix+'.applycal.saved')
00990 applycal()
00991
00992 if ( len(targets) > 0 ):
00993
00994
00995
00996
00997 field = fieldtargets
00998 print "Applying calibration to targets "+field
00999
01000 saveinputs('applycal',prefix+'.applycal.targets.saved')
01001 applycal()
01002
01003
01004
01005
01006
01007
01008 print '--Split--'
01009 default('split')
01010
01011 vis = msfile
01012
01013
01014
01015
01016 srcsplitms = prefix + '.split.ms'
01017 outputvis = srcsplitms
01018
01019
01020 field = ''
01021
01022
01023 spw = ''
01024
01025
01026 datacolumn = 'corrected'
01027
01028 print "Split CORRECTED_DATA into DATA in new ms "+srcsplitms
01029
01030 saveinputs('split',prefix+'.split.saved')
01031 split()
01032
01033
01034
01035
01036
01037
01038 print '--Plotxy--'
01039 default('plotxy')
01040
01041 vis = srcsplitms
01042
01043 field = fluxcalfield
01044 spw = ''
01045
01046 selectdata=True
01047
01048 xaxis = 'uvdist'
01049
01050 interactive=False
01051
01052 correlation='RR LL'
01053 yaxis = 'amp'
01054 figfile = prefix+'.split.'+field+'.uvplot.amp.png'
01055 saveinputs('plotxy',prefix+'.plotxy.'+field+'.amp.saved')
01056 plotxy()
01057
01058 correlation='RL LR'
01059 yaxis = 'phase'
01060 figfile = prefix+'.split.'+field+'.uvplot.rlphase.png'
01061 saveinputs('plotxy',prefix+'.plotxy.'+field+'.rlphase.saved')
01062 plotxy()
01063
01064 if ( polcalfield != fluxcalfield ):
01065
01066 field = polcalfield
01067
01068 correlation='RR LL'
01069 yaxis = 'amp'
01070 figfile = prefix+'.split.'+field+'.uvplot.amp.png'
01071 saveinputs('plotxy',prefix+'.plotxy.'+field+'.amp.saved')
01072 plotxy()
01073
01074 correlation='RL LR'
01075 yaxis = 'phase'
01076 figfile = prefix+'.split.'+field+'.uvplot.rlphase.png'
01077 saveinputs('plotxy',prefix+'.plotxy.'+field+'.rlphase.saved')
01078 plotxy()
01079
01080
01081
01082
01083
01084
01085 clnmodel = {}
01086
01087
01088
01089
01090
01091 for src in srclist:
01092
01093 srcmodel = {}
01094
01095 for spwid in usespwlist:
01096
01097 print '-- Clean '+src+' spw '+spwid+' --'
01098 default('clean')
01099
01100 field = src
01101 spw = spwid
01102
01103
01104 vis = srcsplitms
01105
01106
01107 imname1 = prefix + '.' + src + '.' + spwid + '.clean'
01108 imagename = imname1
01109
01110 print " Output images will be prefixed with "+imname1
01111
01112
01113 mode = 'mfs'
01114
01115
01116 stokes = 'IQUV'
01117
01118
01119 psfmode = clnalg
01120 csclean = usecsclean
01121
01122 imsize = [clnimsize,clnimsize]
01123 cell = [clncell,clncell]
01124
01125
01126 gain = 0.1
01127
01128 niter = clniter
01129
01130 threshold = clthreshold
01131
01132
01133
01134 weighting = 'briggs'
01135 robust = 0.5
01136
01137 weighting = 'natural'
01138
01139
01140 mask = myclnbox
01141
01142 saveinputs('clean',prefix+'.clean.'+src+'.'+spwid+'.saved')
01143 clean()
01144
01145
01146 clnimage1 = imname1+'.image'
01147 clnmodel1 = imname1+'.model'
01148 clnresid1 = imname1+'.residual'
01149 clnmask1 = imname1+'.mask'
01150 clnpsf1 = imname1+'.psf'
01151 clnflux1 = imname1+'.flux'
01152
01153
01154
01155
01156
01157
01158 default('imstat')
01159
01160 field = src
01161 spw = spwid
01162
01163
01164 mybox = str(clnblc)+','+str(clnblc)+','+str(clntrc)+','+str(clntrc)
01165
01166 spwmodel = {}
01167
01168 spwstats = {}
01169 spwfluxes = {}
01170 spwsum = {}
01171 spwmod = {}
01172
01173 for stokes in ['I','Q','U','V']:
01174
01175
01176 imagename = clnimage1
01177 box = mybox
01178
01179 saveinputs('imstat',prefix+'.imstat.'+src+'.'+spwid+'.'+stokes+'.saved')
01180 xstat = imstat()
01181
01182 spwstats[stokes] = xstat
01183
01184
01185 xmax = xstat['max'][0]
01186 xmin = xstat['min'][0]
01187 if( abs(xmin) > abs(xmax) ):
01188 xpol = xmin
01189 else:
01190 xpol = xmax
01191
01192 spwfluxes[stokes]= xpol
01193
01194
01195 xsum = xstat['flux'][0]
01196 spwsum[stokes]= xsum
01197
01198
01199 imagename = clnmodel1
01200 box = ''
01201
01202 saveinputs('imstat',prefix+'.imstat.'+src+'.'+spwid+'.'+stokes+'.model.saved')
01203 xstat = imstat()
01204
01205 xmod = xstat['sum'][0]
01206 spwmod[stokes]= xmod
01207
01208
01209
01210 spwmodel['stat'] = spwstats
01211 spwmodel['flux'] = spwfluxes
01212 spwmodel['integ'] = spwsum
01213 spwmodel['model'] = spwmod
01214
01215
01216 imagename = clnimage1
01217
01218 spwref = {}
01219 ia.open(imagename)
01220
01221
01222 ipix = ia.pixelvalue()
01223
01224 xref = ipix['pixel'][0]
01225 yref = ipix['pixel'][1]
01226 iflx = ipix['value']['value']
01227 spwref['I'] = iflx
01228
01229
01230 qpix = ia.pixelvalue([xref,yref,1,0])
01231 qflx = qpix['value']['value']
01232 spwref['Q'] = qflx
01233
01234
01235 upix = ia.pixelvalue([xref,yref,2,0])
01236 uflx = upix['value']['value']
01237 spwref['U'] = uflx
01238
01239
01240 vpix = ia.pixelvalue([xref,yref,3,0])
01241 vflx = vpix['value']['value']
01242 spwref['V'] = vflx
01243
01244
01245 pflx = sqrt( qflx**2 + uflx**2 )
01246 fflx = pflx/iflx
01247 xflx = atan2(uflx,qflx)*180.0/pi
01248 spwref['P'] = pflx
01249 spwref['F'] = fflx
01250 spwref['X'] = xflx
01251 spwref['xref'] = xref
01252 spwref['yref'] = yref
01253
01254
01255
01256 spwmax = {}
01257
01258
01259 xref = spwstats['I']['maxpos'][0]
01260 yref = spwstats['I']['maxpos'][1]
01261
01262
01263 iflx = spwstats['I']['max'][0]
01264 spwmax['I'] = iflx
01265
01266
01267 qpix = ia.pixelvalue([xref,yref,1,0])
01268 qflx = qpix['value']['value']
01269 spwmax['Q'] = qflx
01270
01271
01272 upix = ia.pixelvalue([xref,yref,2,0])
01273 uflx = upix['value']['value']
01274 spwmax['U'] = uflx
01275
01276
01277 vpix = ia.pixelvalue([xref,yref,3,0])
01278 vflx = vpix['value']['value']
01279 spwmax['V'] = vflx
01280
01281 spwmax['xref'] = xref
01282 spwmax['yref'] = yref
01283
01284 ia.close()
01285
01286 spwmodel['refval'] = spwref
01287 spwmodel['maxval'] = spwmax
01288
01289 srcmodel[spwid] = spwmodel
01290
01291
01292
01293 clnmodel[src] = srcmodel
01294
01295
01296
01297
01298
01299
01300
01301 print 'Results for '+prefix+' :'
01302 print ""
01303
01304 import datetime
01305 datestring=datetime.datetime.isoformat(datetime.datetime.today())
01306
01307 outfile = 'out.'+prefix+'.'+datestring+'.log'
01308 logfile=open(outfile,'w')
01309 print >>logfile,'Results for '+prefix+' :'
01310 print >>logfile,""
01311
01312 if ( polmodel.has_key(polxfield) ):
01313
01314 print "R-L phase residual from image of "+polxfield
01315 print ""
01316 print >>logfile,"R-L phase residual from image of "+polxfield+" :"
01317 print >>logfile,""
01318
01319 src = polxfield
01320 rlcor = {}
01321
01322 for spwid in usespwlist:
01323 ipol = clnmodel[src][spwid]['flux']['I']
01324 qpol = clnmodel[src][spwid]['flux']['Q']
01325 upol = clnmodel[src][spwid]['flux']['U']
01326 vpol = clnmodel[src][spwid]['flux']['V']
01327 rlpd = atan2(upol,qpol)
01328 rlpdcal = polmodel[src][spwid]['poln']['rlpd']
01329 rlpcor = rlpdcal - rlpd
01330 scor = sin(rlpcor); ccor = cos(rlpcor); rlpcor = atan2(scor,ccor)
01331 rlcor[spwid] = rlpcor
01332 rlpcor_deg = rlpcor*180.0/pl.pi
01333
01334 print "R-L Phase Correction SPW "+spwid+" = %7.2f deg" % rlpcor_deg
01335 print >>logfile,"R-L Phase Correction SPW "+spwid+" = %7.2f deg" % rlpcor_deg
01336
01337
01338
01339
01340
01341
01342
01343 print ""
01344 print "Final Stats:"
01345 print ""
01346
01347 print >>logfile,""
01348 print >>logfile,"Final Stats:"
01349 print >>logfile,""
01350
01351 for src in srclist:
01352
01353 print "Source "+src+" :"
01354 print >>logfile,"Source "+src+" :"
01355
01356 for spwid in usespwlist:
01357
01358 field = src
01359 spw = spwid
01360
01361
01362
01363 ipol = clnmodel[src][spwid]['flux']['I']
01364 qpol = clnmodel[src][spwid]['flux']['Q']
01365 upol = clnmodel[src][spwid]['flux']['U']
01366 vpol = clnmodel[src][spwid]['flux']['V']
01367
01368
01369
01370 ppol = sqrt(qpol**2 + upol**2)
01371 fpol = ppol/ipol
01372 rlpd = atan2(upol,qpol)
01373 rlpd_deg = rlpd*180.0/pl.pi
01374
01375
01376 print ' spw %s CASA I = %7.3f P = %7.3f F = %7.4f X = %7.2f deg' % (spwid,ipol,ppol,fpol,rlpd_deg)
01377 print >>logfile,' spw %s CASA I = %7.3f P = %7.3f F = %7.4f X = %7.2f deg' % (spwid,ipol,ppol,fpol,rlpd_deg)
01378
01379 if (aipsmodel.has_key(src)):
01380 iflx = aipsmodel[src][spwid][0]/1000.0
01381 fflx = aipsmodel[src][spwid][1]
01382 rlaips_deg = aipsmodel[src][spwid][2]
01383 rlaips = rlaips_deg*pl.pi/180.0
01384
01385 pflx = iflx*fflx
01386 qflx = pflx*cos(rlaips)
01387 uflx = pflx*sin(rlaips)
01388 vflx = 0.0
01389
01390 print ' spw %s AIPS I = %7.3f P = %7.3f F = %7.4f X = %7.2f deg' % (spwid,iflx,pflx,fflx,rlaips_deg)
01391 print >>logfile,' spw %s AIPS I = %7.3f P = %7.3f F = %7.4f X = %7.2f deg' % (spwid,iflx,pflx,fflx,rlaips_deg)
01392
01393
01394
01395 print ""
01396 print >>logfile,""
01397
01398
01399
01400
01401
01402
01403
01404
01405
01406
01407
01408
01409
01410
01411
01412
01413
01414
01415
01416
01417
01418
01419
01420
01421
01422
01423
01424
01425
01426
01427
01428
01429
01430
01431
01432
01433
01434
01435
01436
01437
01438
01439
01440
01441
01442
01443
01444
01445
01446
01447 logfile.close()
01448 print "Results are in "+outfile
01449
01450
01451
01452
01453
01454 pickfile = prefix + '.pickle'
01455 f = open(pickfile,'w')
01456 p = pickle.Pickler(f)
01457 p.dump(clnmodel)
01458 p.dump(polmodel)
01459 f.close()
01460
01461 print ""
01462 print "Dictionaries clnmodel,polmodel saved in "+pickfile
01463 print "Use Pickle to retrieve"
01464 print ""
01465
01466
01467
01468
01469
01470
01471
01472
01473 print ""
01474 print "Completed Processing"