;--------------------------------------------------------------------------------------------------
; SAMPLE MIR CALIBRATION SCRIPT.
;
; Substitute experiment specific names or numbers for place holders:
;  passband_cal, ancillary_1, gain_calibrator_1, target_1, int_1, planet_or_satellite,...
;--------------------------------------------------------------------------------------------------


;--------------------------------------------------------------------------------------------------
; Preliminary commands, then READ IN DATA.  On R&G public computers:
;--------------------------------------------------------------------------------------------------
source /opt/mir/setup
source /opt/mir/linux/MIRRC.linux
idl
readdata, dir='/2009/science/mir_data/track_time'


;--------------------------------------------------------------------------------------------------
; INSPECT DATA and select reference antenna (refant).
;
; Identify:
; 1) Gain calibrator(s) and target source(s), at least one of each.  May be more than one group.
;    Gain calibrators and target sources are interleaved in time (gain, target, gain,...).
; 2) Passband calibrator(s).  Usually a strong quasar, e.g., 3c454.3, sometimes a strong planet, e.g., Mars.  
;    A strong gain calibrator can double up as a passband calibrator.
; 3) Flux calibrator: a planet or satellite.  If more than one, can use all or just the best.  
;    In an experiment with no obvious problems, e.g., deteriorating phases, best to worst are:
;    Titan, Ganymede/Callisto, Uranus, Neptune, Mars, Ceres,...
;--------------------------------------------------------------------------------------------------

select, /pos_wt, /reset
plot_continuum

select, /pos_wt, /reset
plot_continuum, x='int'

select


;--------------------------------------------------------------------------------------------------
; To ZOOM IN on integration number range (int):
;--------------------------------------------------------------------------------------------------
select, /pos_wt, /reset, int=[int_1,int_2]
plot_continuum, x='int'


;--------------------------------------------------------------------------------------------------
; IDENTIFY RECEIVER.  Usually it's the first or the second.
;--------------------------------------------------------------------------------------------------
select, rx='230', /reset
select, rx='345', /reset
select, rx='400', /reset
select, rx='690', /reset


;--------------------------------------------------------------------------------------------------
; FLAG POINTING data.
;--------------------------------------------------------------------------------------------------
result=dat_filter(s_f,'"integ" lt "9."',/reset)
flag, /flag


;--------------------------------------------------------------------------------------------------
; INSPECT AND APPLY SYSTEM TEMPERATURES.
;--------------------------------------------------------------------------------------------------
select, /pos_wt, /reset, band='c1'
plot_var, x='int', frames=9

select, /pos_wt, /reset
apply_tsys


;--------------------------------------------------------------------------------------------------
; REGENERATE CONTINUUM to flag out bad chunk/scan combinations.
;--------------------------------------------------------------------------------------------------
select, /pos_wt, /reset
uti_avgband


;--------------------------------------------------------------------------------------------------
; INSPECT SPECTRA for problems, e.g., amplitude spikes, and determine band resolutions:
;--------------------------------------------------------------------------------------------------
plot_spectra, /norm, source=['passband_cal']

select, /pos_wt, /reset, source='passband_cal', band='s01'
plot_spectra


;--------------------------------------------------------------------------------------------------
; VIEW CHUNK TO CHUNK PHASE OFFSETS.
;--------------------------------------------------------------------------------------------------
select, source='passband_cal', sideband='l', /pos_wt, /reset
plot_spectra, x_var='fsky', color='band', frames_per_page='2'


;--------------------------------------------------------------------------------------------------
; TO REMOVE CHUNK TO CHUNK PHASE OFFSETS, do phase and amplitude passband calibrations separately.
;--------------------------------------------------------------------------------------------------


;--------------------------------------------------------------------------------------------------
; 1) PHASE ONLY PASSBAND CALIBRATION, antenna based.  
;--------------------------------------------------------------------------------------------------
select, /pos_wt, /reset, band=['s13']
pass_cal, cal_type='pha', tel_bsl='telescope', refant=5, preavg=4, ntrim=0, smooth=1
all no
passband_cal yes

select, /pos_wt, /reset, band=['-s13']
pass_cal, cal_type='pha', tel_bsl='telescope', refant=5, ntrim=0, smooth=1
all no
passband_cal yes


;--------------------------------------------------------------------------------------------------
; PASSBAND CALIBRATION NOTES:
; 1) Do each resolution separately.  
; 2) If high enough signal-to-noise, average the band down to 32 channels (preavg).
; 3) In the above, s13 has 128 channels, other bands have 32 channels.  "-" means all except.
;--------------------------------------------------------------------------------------------------


;--------------------------------------------------------------------------------------------------
; REGENERATE CONTINUUM again to fix chunk to chunk phase offsets.
;--------------------------------------------------------------------------------------------------
select, /pos_wt, /reset
uti_avgband


;--------------------------------------------------------------------------------------------------
; CHECK to make sure chunk to chunk phase offsets have been removed.
;--------------------------------------------------------------------------------------------------
select, source='passband_cal', sideband='l', /pos_wt, /reset
plot_spectra, x_var='fsky', color='band', frames_per_page='2'


;--------------------------------------------------------------------------------------------------
; 2) AMPLITUDE ONLY PASSBAND CALIBRATION, antenna based.
;--------------------------------------------------------------------------------------------------
select, /pos_wt, /reset, band=['s13']
pass_cal, cal_type='amp', tel_bsl='telescope', refant=5, preavg=4, ntrim=0, smooth=1
all no
passband_cal yes

select, /pos_wt, /reset, band=['-s13']
pass_cal, cal_type='amp', tel_bsl='telescope', refant=5, ntrim=0, smooth=1
all no
passband_cal yes


;--------------------------------------------------------------------------------------------------
; CALIBRATE PHASE GAINS AS A FUNCTION OF TIME.
;--------------------------------------------------------------------------------------------------


;--------------------------------------------------------------------------------------------------
; 1) Ancillary calibration sources (everything except gain calibrators and target sources):
;--------------------------------------------------------------------------------------------------
select, /pos_wt, /reset, source=['ancillary_1','ancillary_2']
gain_cal, cal_type='pha', x_var='int', tel_bsl='telescope', refant=5, /connect, /non_point
all yes 1


;--------------------------------------------------------------------------------------------------
; 2) Gain calibrators and Target sources:
;--------------------------------------------------------------------------------------------------
select, /pos_wt, /reset, source=['gain_calibrator_1','target_1']
gain_cal, cal_type='pha', x_var='hours', tel_bsl='telescope', refant=5, smooth=0.2, /non_point, /preavg
all no
gain_calibrator_1 yes 1


;--------------------------------------------------------------------------------------------------
; FLUX CALIBRATION PRELIMINARIES.
;--------------------------------------------------------------------------------------------------


;--------------------------------------------------------------------------------------------------
; 1) Check for spectral lines in planet/satellite, each sideband.
;--------------------------------------------------------------------------------------------------
select, source='planet_or_satellite', sideband='u', /pos_wt, /reset
plot_spectra, x_var='fsky', frame_var='sb', color='band', ntrim=8


;--------------------------------------------------------------------------------------------------
; 2) Get sky frequency of observations, each sideband:
;--------------------------------------------------------------------------------------------------
select, /pos_wt, /reset, sideband='l', band='s13', baseline='1-2', int=0
print, sp[psf].fsky


;--------------------------------------------------------------------------------------------------
; 3) Get zero-spacing flux of planet/satellite, each sideband:
;  http://sma1.sma.hawaii.edu/planetvis.html
;--------------------------------------------------------------------------------------------------


;--------------------------------------------------------------------------------------------------
; 4) Get flux scale for all antennas, each sideband:
;--------------------------------------------------------------------------------------------------
select, /pos_wt, /reset, sideband='l'
gain_cal, cal_type='amp', x_var='hours', tel_bsl='telescope', refant=5, /non_point, poly=0, /preavg
all no
planet_or_satellite yes flux_l

select, /pos_wt, /reset, sideband='u'
gain_cal, cal_type='amp', x_var='hours', tel_bsl='telescope', refant=5, /non_point, poly=0, /preavg
all no
planet_or_satellite yes flux_u


;--------------------------------------------------------------------------------------------------
; MEASURE FLUX(ES) OF GAIN CALIBRATOR(S).
;--------------------------------------------------------------------------------------------------
select, /pos_wt, /reset, source=['gain_calibrator_1','planet_or_satellite'], sideband='l', int=[int_1,int_2]
flux_measure
v

select, /pos_wt, /reset, source=['gain_calibrator_1','planet_or_satellite'], sideband='u', int=[int_1,int_2]
flux_measure
v


;--------------------------------------------------------------------------------------------------
; FLUX MEASUREMENT NOTES: 
; 1) If sideband of planet/satellite has spectral line, use gain calibrator measurement in other 
;    sideband (flux proportional to frequency^-0.5) to estimate flux.
; 2) Choose time range free of pointings, if possible.
; 3) "v" means vector averaging, especially important for weaker sources.
;--------------------------------------------------------------------------------------------------


;--------------------------------------------------------------------------------------------------
; CALIBRATE AMPLITUDE GAINS AS A FUNCTION OF TIME, GAIN CALIBRATOR(S) AND TARGET(S):
;--------------------------------------------------------------------------------------------------
select, /pos_wt, /reset, source=['gain_calibrator_1','target_1'], sideband='l'
gain_cal, cal_type='amp', x_var='hours', tel_bsl='telescope', refant=5, /non_point, smooth=1.0, /preavg
all no
gain_calibrator_1 yes measured_l


select, /pos_wt, /reset, source=['gain_calibrator_1','target_1'], sideband='u'
gain_cal, cal_type='amp', x_var='hours', tel_bsl='telescope', refant=5, /non_point, smooth=1.0, /preavg
all no
gain_calibrator_1 yes measured_u


;--------------------------------------------------------------------------------------------------
; FLAG gain calibrator scans with discrepant phase residuals (usually at beginning or end of track) 
; before typing "y" (yes) in calibrations above.
;--------------------------------------------------------------------------------------------------
select, /pos_wt, /reset, source=['gain_calibrator_1'], int=[int_1,int_2]
flag, /flag


;--------------------------------------------------------------------------------------------------
; TO ENSURE PROPER INTERPOLATION, FLAG target scans that are not preceeded AND followed by a 
; gain calibrator.
;--------------------------------------------------------------------------------------------------
select, /pos_wt, /reset, int=[int_1,int_2]
flag, /flag


;--------------------------------------------------------------------------------------------------
; If more than one target source, DOPPLER CORRECT NON-REFERENCE TARGET(S).
;--------------------------------------------------------------------------------------------------
select, /pos_wt, /reset
uti_doppler_fix, reference='reference_target', source='other_target'


;--------------------------------------------------------------------------------------------------
; SAVE fully calibrated file IN MIR FORMAT.
;--------------------------------------------------------------------------------------------------
select, /reset
mir_save, 'track_time.mir'


;--------------------------------------------------------------------------------------------------
; OUTPUT IN MIRIAD FORMAT in preparation for imaging.
;--------------------------------------------------------------------------------------------------
.compile idl2miriad
.compile idl2miriad

select, /pos_wt, /reset
idl2miriad, dir='track_time_target_1_usb', source='target_1', sideband='u'
select, /pos_wt, /reset
idl2miriad, dir='track_time_target_1_lsb', source='target_1', sideband='l'


;--------------------------------------------------------------------------------------------------
; MISCELLANEOUS CHECKS:
;--------------------------------------------------------------------------------------------------


;--------------------------------------------------------------------------------------------------
; 1) Amplitude versus projected baseline of gain calibrator(s).  Should be straight across.
;--------------------------------------------------------------------------------------------------
select, /pos_wt, /reset, source='gain_calibrator_1'
plot_var, x='prbl', y='ampave', frame_var='sb', color='blcd'


;--------------------------------------------------------------------------------------------------
; 2) Spectrum of target source(s), averaged over all baselines, to look for spectral lines.
;--------------------------------------------------------------------------------------------------
select, /pos_wt, /reset, source='target_1', sideband='l'
plot_spectra, x_var='fsky', frame_var='sb', color='band', ntrim=8
Back to RTDC 'Searching for SMA Data'