~pconv-team/polconvertsd/trunk-1

« back to all changes in this revision

Viewing changes to EU-VGOS/EUVGOS_CASA/POL_CALIBRATE.py

  • Committer: I. Marti-Vidal
  • Date: 2021-04-20 10:41:25 UTC
  • Revision ID: i.marti-vidal@uv.es-20210420104125-2tdcc4rm3gb108nm
Fixed Chi2 code when scans were not given in time order.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
## EU-VGOS POLCONVERT HELPER SCRIPT.
 
2
#
 
3
# Copyright (c) Ivan Marti-Vidal 2018-2021 
 
4
#               Yebes Observatory (IGN, Spain)
 
5
#               Universitat de Valencia (Spain)
 
6
#
 
7
# This program is free software; you can redistribute it and/or modify
 
8
# it under the terms of the GNU General Public License as published by
 
9
# the Free Software Foundation; either version 2 of the License, or
 
10
# (at your option) any later version.
 
11
#
 
12
# This program is distributed in the hope that it will be useful,
 
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
# GNU General Public License for more details.
 
16
#
 
17
# You should have received a copy of the GNU General Public License
 
18
# along with this program; if not, see <http://www.gnu.org/licenses/>,
 
19
# or write to the Free Software Foundation, Inc., 
 
20
# 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
21
#
 
22
#
 
23
# Redistribution and use in source and binary forms, with or without
 
24
# modification, are permitted provided that the following conditions are
 
25
# met:
 
26
#
 
27
# a. Redistributions of source code must retain the above copyright
 
28
#    notice, this list of conditions and the following disclaimer.
 
29
# b. Redistributions in binary form must reproduce the above copyright
 
30
#    notice, this list of conditions and the following disclaimer in the
 
31
#    documentation and/or other materials provided with the
 
32
#    distribution.
 
33
# c. Neither the name of the author nor the names of contributors may 
 
34
#    be used to endorse or promote products derived from this software 
 
35
#    without specific prior written permission.
 
36
#
 
37
#
 
38
#THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 
39
#"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 
40
#LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 
41
#A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 
42
#OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 
43
#SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 
44
#LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 
45
#DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 
46
#THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
47
#(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 
48
#OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
49
#
 
50
#
 
51
#
 
52
 
 
53
import pickle as pk
 
54
import os, sys
 
55
from polconvert_cli import polconvert_cli as polconvert
 
56
 
 
57
 
 
58
 
 
59
if __name__=='__main__':
 
60
 EXPNAME = 'vgt274'
 
61
 DIFX_DIR = 'DiFX'
 
62
 DOSCAN = 52
 
63
 NIF = 32
 
64
 CHANSOL = 32
 
65
 
 
66
 DO_RAW = False
 
67
 DO_WPCAL = True
 
68
 DO_FINAL = False
 
69
 
 
70
 
 
71
 
 
72
########################
 
73
# MODEL OF X-Y DIFFERENCE WITH A MB-DELAY:
 
74
 DO_WITH_DELAYS = False
 
75
 Delays = [[-1.095e-10, 9.40041e9], [-5.9e-11, -0.20843e9], [5.229e-10, 5.69512e9]]
 
76
 Phases= [0.,0.,0.]
 
77
########################
 
78
 
 
79
 
 
80
 
 
81
 
 
82
#################################
 
83
# COMMENT OUT THIS LINE WHEN DEBUGGING WITH execfile(...)
 
84
def POL_CALIBRATE(EXPNAME='',DIFX_DIR = '', DOSCAN=-1,CHANSOL=32,USE_PCAL=True,EXCLUDE_BASELINES=[],DOIF=[],DOAMP=True):
 
85
 """ Estimates cross-polarization gains using a scan in a SWIN directory.
 
86
  The channel resolution is set to CHANSOL. Saves the gains in a dictionary
 
87
  that can ge used by PolConvert."""
 
88
#################################
 
89
 
 
90
# try:
 
91
 if True:
 
92
 
 
93
  EXP = EXPNAME
 
94
  if DOSCAN < 0:
 
95
    raise Exception("POL_CALIBRATE ERROR! SCAN(S) NOT SPECIFIED!\n")
 
96
 
 
97
  DIFX = DIFX_DIR
 
98
 
 
99
  if not (type(DOSCAN) is list):
 
100
    DOSCAN = [DOSCAN]
 
101
 
 
102
 
 
103
# Figure out number of antennas, IFs and channels per IF:
 
104
  #temp = open('%s_%03i.input'%(os.path.join(DIFX,EXP),DOSCAN[0]))  
 
105
  temp = open('%s_%s.input'%(os.path.join(DIFX,EXP),DOSCAN[0]))
 
106
  lines = temp.readlines()
 
107
  for li in lines:
 
108
    if li.startswith('TELESCOPE ENTRIES:'):
 
109
      Nants = int(li.split()[-1])
 
110
 
 
111
    if li.startswith('FREQ ENTRIES:'):
 
112
      NIF = int(li.split()[-1])
 
113
    if li.startswith('NUM CHANNELS 0:'):
 
114
      Nchan = int(li.split()[-1])
 
115
 
 
116
  temp.close()
 
117
  del lines
 
118
 
 
119
  if type(USE_PCAL) is bool:
 
120
      temp = bool(USE_PCAL)
 
121
      USE_PCAL = [temp for i in range(Nants)]
 
122
 
 
123
  if len(DOIF)==0:
 
124
    DOIF = range(1,NIF+1)
 
125
 
 
126
 
 
127
############################################################
 
128
# X-Y cross-pol gain estimate (with phasecal correction):
 
129
 
 
130
  os.system('rm -rf %s'%os.path.join(DIFX,'POLCONVERT_CALIB_SCANS'))
 
131
  os.system('mkdir %s'%os.path.join(DIFX,'POLCONVERT_CALIB_SCANS'))
 
132
  for SI in DOSCAN:
 
133
    os.system('cp -r %s %s/.'%('%s_%s.difx'%(os.path.join(DIFX,EXP),SI), os.path.join(DIFX,'POLCONVERT_CALIB_SCANS')))  
 
134
 
 
135
  
 
136
  WITH_PCAL = polconvert(IDI='%s/POLCONVERT_CALIB_SCANS'%DIFX,
 
137
    OUTPUTIDI = '%s_POL_CALIBRATE_BLIND'%(os.path.join(DIFX,EXP)),
 
138
    DiFXinput = '%s_%s.input'%(os.path.join(DIFX,EXP),DOSCAN[0]),
 
139
    DiFXcalc = '%s_%s.calc'%(os.path.join(DIFX,EXP),DOSCAN[0]),
 
140
    doIF = DOIF, plotIF = DOIF,
 
141
    plotRange=[0,0,0,0,2,0,0,0], plotAnt=1,excludeBaselines=EXCLUDE_BASELINES,
 
142
    linAntIdx = range(1,Nants+1),swapXY=[False for i in range(Nants)], 
 
143
    usePcal = USE_PCAL, XYadd={}, XYratio={}, XYdel={},
 
144
# ALMA-specific:
 
145
    Range = [], ALMAant = '',spw=-1,calAPP='',
 
146
    gains = [['NONE'] for i in range(Nants)], 
 
147
    dterms = ['NONE' for i in range(Nants)],
 
148
    gainmode = [[] for i in range(Nants)],XYavgTime=0.0,amp_norm=1.0,
 
149
# Gain-solver configuration:    
 
150
    solveAmp = DOAMP, solveMethod = 'COBYLA', #'Nelder-Mead', #'COBYLA', #"Levenberg-Marquardt",
 
151
    doSolve=0.0,doTest=False, solint = [CHANSOL, 1])
 
152
 
 
153
 # raw_input('HOLD')
 
154
 
 
155
  FPK = 'FRINGE.PEAKS'
 
156
  FPL = 'FRINGE.PLOTS'
 
157
  PCF = 'POLCONVERT.FRINGE'
 
158
  Plot = 'Cross-Gains.png'
 
159
 
 
160
  os.system('rm -rf %s_POLCAL_%s %s_POLCAL_%s %s_POLCAL_%s'%(FPK, DOSCAN[0], FPL, DOSCAN[0], PCF, DOSCAN[0]))
 
161
  os.system('mv %s %s_POLCAL_%s'%(FPK, FPK, DOSCAN[0]))
 
162
  os.system('mv %s %s_POLCAL_%s'%(FPL, FPL, DOSCAN[0]))
 
163
  os.system('mv %s %s_POLCAL_%s'%(PCF, PCF, DOSCAN[0]))
 
164
 
 
165
  os.system('mv %s %s_POLCAL_%s.png'%(Plot, Plot, DOSCAN[0]))
 
166
 
 
167
  OFF=open('POLCAL_OUTPUT_SCAN-%s.dat'%DOSCAN[0],'w')
 
168
  pk.dump(WITH_PCAL,OFF) ; OFF.close()
 
169
 
 
170
 
 
171
  #IFF = open('POLCAL_OUTPUT_SCAN-%03i.dat'%DOSCAN)
 
172
  #WITH_PCAL = pk.load(IFF)
 
173
  #IFF.close()
 
174
  
 
175
  FINAL = polconvert(IDI='%s/POLCONVERT_CALIB_SCANS'%DIFX,
 
176
    OUTPUTIDI = '%s_POL_CALIBRATE_RESULTS'%(os.path.join(DIFX,EXP)),          
 
177
    DiFXinput = '%s_%s.input'%(os.path.join(DIFX,EXP),DOSCAN[0]),
 
178
    DiFXcalc = '%s_%s.calc'%(os.path.join(DIFX,EXP),DOSCAN[0]),
 
179
    doIF = DOIF, plotIF = DOIF,
 
180
    plotRange=[0,0,0,0,2,0,0,0], plotAnt=1,excludeBaselines=EXCLUDE_BASELINES,
 
181
    linAntIdx = range(1,Nants+1),swapXY=[False for i in range(Nants)], 
 
182
    usePcal = USE_PCAL, 
 
183
    XYadd=WITH_PCAL['XYadd'], XYratio=WITH_PCAL['XYratio'], XYdel={},
 
184
# ALMA-specific:
 
185
    Range = [], ALMAant = '',spw=-1,calAPP='',
 
186
    gains = [['NONE'] for i in range(Nants)], 
 
187
    dterms = ['NONE' for i in range(Nants)],
 
188
    gainmode = [[] for i in range(Nants)],XYavgTime=0.0,amp_norm=1.0,
 
189
# Gain-solver configuration:    
 
190
    doSolve=-1,doTest=False)
 
191
 
 
192
  os.system('rm -rf %s_CHECK_%s %s_CHECK_%s %s_CHECK_%s'%(FPK, DOSCAN[0], FPL, DOSCAN[0], PCF, DOSCAN[0]))
 
193
  os.system('mv %s %s_CHECK_%s'%(FPK, FPK, DOSCAN[0]))
 
194
  os.system('mv %s %s_CHECK_%s'%(FPL, FPL, DOSCAN[0]))
 
195
  os.system('mv %s %s_CHECK_%s'%(PCF, PCF, DOSCAN[0]))
 
196
 
 
197
 
 
198
  if os.path.exists('POL_CALIBRATE.FAILED'):   
 
199
    os.system('rm -rf POL_CALIBRATE.FAILED')   
 
200
 
 
201
# except:
 
202
 else:
 
203
 
 
204
  e = sys.exc_info()[0]  
 
205
  OFF = open('POL_CALIBRATE.FAILED','w')
 
206
  print >> OFF, e
 
207
  OFF.close()
 
208
 
 
209