1
## EU-VGOS POLCONVERT HELPER SCRIPT.
3
# Copyright (c) Ivan Marti-Vidal 2018-2021
4
# Yebes Observatory (IGN, Spain)
5
# Universitat de Valencia (Spain)
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.
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.
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
23
# Redistribution and use in source and binary forms, with or without
24
# modification, are permitted provided that the following conditions are
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
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.
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.
55
from polconvert_cli import polconvert_cli as polconvert
59
if __name__=='__main__':
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]]
77
########################
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
#################################
95
raise Exception("POL_CALIBRATE ERROR! SCAN(S) NOT SPECIFIED!\n")
99
if not (type(DOSCAN) is list):
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()
108
if li.startswith('TELESCOPE ENTRIES:'):
109
Nants = int(li.split()[-1])
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])
119
if type(USE_PCAL) is bool:
120
temp = bool(USE_PCAL)
121
USE_PCAL = [temp for i in range(Nants)]
124
DOIF = range(1,NIF+1)
127
############################################################
128
# X-Y cross-pol gain estimate (with phasecal correction):
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'))
133
os.system('cp -r %s %s/.'%('%s_%s.difx'%(os.path.join(DIFX,EXP),SI), os.path.join(DIFX,'POLCONVERT_CALIB_SCANS')))
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={},
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])
157
PCF = 'POLCONVERT.FRINGE'
158
Plot = 'Cross-Gains.png'
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]))
165
os.system('mv %s %s_POLCAL_%s.png'%(Plot, Plot, DOSCAN[0]))
167
OFF=open('POLCAL_OUTPUT_SCAN-%s.dat'%DOSCAN[0],'w')
168
pk.dump(WITH_PCAL,OFF) ; OFF.close()
171
#IFF = open('POLCAL_OUTPUT_SCAN-%03i.dat'%DOSCAN)
172
#WITH_PCAL = pk.load(IFF)
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)],
183
XYadd=WITH_PCAL['XYadd'], XYratio=WITH_PCAL['XYratio'], XYdel={},
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)
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]))
198
if os.path.exists('POL_CALIBRATE.FAILED'):
199
os.system('rm -rf POL_CALIBRATE.FAILED')
204
e = sys.exc_info()[0]
205
OFF = open('POL_CALIBRATE.FAILED','w')