# The following assumes standard calibration of the amplitude calibrator and
# science target via the script generator.
# Examples of each are provided in:
# Amplitude Calibrator: uid___A002_X86fcfa_X3ae.ms.scriptForSDCalibration.py
# Science target: uid___A002_X86fcfa_X664.ms.scriptForSDCalibration.py
#
# Below is the procedure to achieve a fully calibrated TP Science image,
# in Jansky/beam units, taking as input a set of calibrated Science and
# Amplitude Calibrator datasets (in units of Kelvin).
#
# 1. Image the amplitude calibrator and measure the value of Jy/K from the image
# (values should be obtained for each night data is taken and for each spw and antenna)
#
# 2. Convert the science target units from Kelvin to Jansky
# (treating each night's data and each spw separately)
#
# 3. Image the science target
# (combining data from all antennas)
#
# 4. Optionally subtract a residual background from the image
# (currently, this is likely necessary in most cases; in future, improvements
# in the sdbaseline task should remove the need for this step)
#
# 5. Apply the restoring beam to the science image

### scriptForImagingAmpCalAndDerivingJyPerK.py deals with STEP 2. ###
### Tested in CASA Version 4.3.0                                  ###
# Prior to running this script, scriptForImagingAmpCalAndDerivingJyPerK.py
# must have been run.

### 2. Convert the science target units from Kelvin to Jansky ###
# The script  scriptForImagingAmpCalAndDerivingJyPerK.py must first have been
# run in order to get the list of Jy/K values that are given below
# Ideally these should be written to a file and read by this script
# but for now they are entered manually.

# List the science spws
spwlist = [17, 19, 21, 23]  # the science spws

# List the Jy/K values (corresponding to the list of the science spws above)
# that were the output from scriptForImagingAmpCalAndDerivingJyPerK.py
jyperklist0701 = [41.37, 42.39, 43.45, 42.82]
jyperklist0705 = [40.99, 42.74, 40.08, 42.09]
jyperklist0707 = [39.42, 42.08, 40.18, 40.82]
jyperklist0717 = [43.49, 43.70, 42.01, 43.04]

# Apply calibration tables for Jy/K conversion
print 'Applying the Jy/K conversion factor ...'

# Data taken on 2014-07-01
for name in ['uid___A002_X85c183_X36f', 'uid___A002_X85c183_X60b']:
    os.system('rm -Rf %s.ms.cal.jy' % name)
    os.system('cp -Rf %s.ms.cal %s.ms.cal.jy' % (name, name))
    for (spw, jyperk) in zip(spwlist, jyperklist0701):
        aU.scaleAutocorr(vis='%s.ms.cal.jy' % name, scale=jyperk, spw=spw)

# Data taken on 2014-07-05
for name in ['uid___A002_X8602fa_X2ab', 'uid___A002_X8602fa_X577']:
    os.system('rm -Rf %s.ms.cal.jy' % name)
    os.system('cp -Rf %s.ms.cal %s.ms.cal.jy' % (name, name))
    for (spw, jyperk) in zip(spwlist, jyperklist0705):
        aU.scaleAutocorr(vis='%s.ms.cal.jy' % name, scale=jyperk, spw=spw)

# Data taken on 2014-07-07
for name in ['uid___A002_X864236_X2d4', 'uid___A002_X864236_X693']:
    os.system('rm -Rf %s.ms.cal.jy' % name)
    os.system('cp -Rf %s.ms.cal %s.ms.cal.jy' % (name, name))
    for (spw, jyperk) in zip(spwlist, jyperklist0707):
        aU.scaleAutocorr(vis='%s.ms.cal.jy' % name, scale=jyperk, spw=spw)

# Data taken on 2014-07-17
for name in ['uid___A002_X86fcfa_Xd9', 'uid___A002_X86fcfa_X664',
                 'uid___A002_X86fcfa_X96c']:
    os.system('rm -Rf %s.ms.cal.jy' % name)
    os.system('cp -Rf %s.ms.cal %s.ms.cal.jy' % (name, name))
    for (spw, jyperk) in zip(spwlist, jyperklist0717):
        aU.scaleAutocorr(vis='%s.ms.cal.jy' % name, scale=jyperk, spw=spw)

