!-----------------------------------------------------------------------
!
! This procedure reprocesses the IMB-FITS calibrations for a given
! account. Input IMB-FITS files are found in the project account (i.e.
! NOT in the main mrt account with all FITS mixed). Only Class files are
! generated (i.e. plots are not redone). This procedure must run with
! the "mrt" account. Usage is:
!
! MRTCAL> @ pipeline-reprocess.cal
!
! with the following selectors:
!
! /PROJECT t06-16 (or any other). As of today, a project name must be
!       given. Looping on all projects is not (yet) possible.
!
! /DATE 16-JAN-2017, or
! /DATE 16-JAN-2017 28-FEB-2017: reprocess this date or range of
!       dates. BEWARE, default is all dates found in
!       /mrt-lx3/vis/t06-16/observationData/imbfits/ !
!
! /SCAN 152, or
! /SCAN 152 153: reprocess this scan or range of scans. Default is all
!       scans.
!
! /BACKEND VESPA: reprocess the named backend. Default is all backends.
!
! For example:
! MRTCAL> @ pipeline-reprocess.cal /PROJECT t06-16 /DATE 16-FEB-2017
! will reprocess all scans for that date and for the named project.
!
! As a result, the procedure will generate:
! - an output directory named:
!   /mrt-lx3/vis/t06-16/observationData/mrtcal/reprocessed/
!   I choose a subdirectory so that it is clear for the user that
!   something important is present in the default "mrtcal" directory.
! - an "index.mrt" file in the output directory. We can not use the
!   online pipeline indexes as we can not interfere with the current odp
!   processing.
! - one or more Class files of the form "VESPAOdp20170209.30m" (same as
!   usual, i.e. one file per date and per backend). Note that the files
!   are created if missing. Then they are appended and never overwritten.
!   (i.e. beware in case of multiple reprocessings). We could add an
!   /OVERWRITE option in the future.
!
!-----------------------------------------------------------------------
!
begin procedure pipeline-reprocess-setup
  !
  ! MSET tunings
  mset  bookkeeping  space        1024
  !
  mset  calibration  bad          no
  mset  calibration  bandwidth    20.0
  mset  calibration  chopper      strict
  mset  calibration  feedback     set
  mset  calibration  interval     30.0
  mset  calibration  match        0.040
  mset  calibration  mjdinter     yes
  mset  calibration  off          linear  ! For OTF PSW
  mset  calibration  products     linear
  mset  calibration  scan         nearest
  mset  calibration  water        element  ! 1 pwv per calib bandwidth
  !
  mset  output       calibration  associated
  mset  output       chunk        set
  mset  output       integration  *
  mset  output       spectra      yes
  mset  output       usersection  no
  mset  output       vodir        " "  ! Unused
  mset  output       voxml        no
  mset  output       weight       no
  !
end procedure pipeline-reprocess-setup
!
@ pipeline-reprocess-setup
@ pipeline-tools
@ pipeline-structure
! Only a few elements from pipe% structure are used here.
!
! --- Command line parsing ---
parse /project 1 /date 1 2 /scan 1 2 /backend 1
! /PROJECT ?
define char*16 projid
if pro%parse%project%narg.le.0 then
  message e pipeline-reprocess "Reprocessing all projects at once is not implemented"
  message e pipeline-reprocess "Use option /PROJECT NNN-YY"
  return base
else
  let projid 'pro%parse%project%arg[1]'
endif
! /BACKEND ?
define char*64 optback
if pro%parse%backend%narg.gt.0 then
  let optback " /BACKEND "'pro%parse%backend%arg[1]'
else
  let optback " "
endif
! /DATE ?
define char*64 optdate
if pro%parse%date%narg.eq.2 then
  let optdate " /DATE "'pro%parse%date%arg[1]'" TO "'pro%parse%date%arg[2]'
else if pro%parse%date%narg.eq.1 then
  let optdate " /DATE "'pro%parse%date%arg[1]'
else
  let optdate " "
endif
! /SCAN ?
define char*64 optscan
if pro%parse%scan%narg.eq.2 then
  let optscan " /SCAN "'pro%parse%scan%arg[1]'" TO "'pro%parse%scan%arg[2]'
else if pro%parse%scan%narg.eq.1 then
  let optscan " /SCAN "'pro%parse%scan%arg[1]'
else
  let optscan " "
endif
! --- End of command line parsing ---
!
! Set up environment
define char*256 projdir
let projdir        "/mrt-lx3/vis/"'projid'"/observationData"
let pipe%projfits  'projdir'"/imbfits"
let pipe%classdir  'projdir'"/mrtcal/reprocessed"
sic mkdir 'pipe%classdir'
!
! Index build (for this project only!)
say "Indexing IMB-FITS file"
index build 'pipe%projfits' /file 'pipe%classdir'"/index.mrt"
!
! Find
define char*256 commandline
let commandline "MFIND"'optdate''optscan''optback'
execute 'commandline'
!
! Calibrate
mlist /toc observed backend
for isetup 1 to mtoc%nsetup
  ! Switch to appropriate Class file
  @ pipeline-fileout 'mtoc%setup[isetup,2]' 'mtoc%setup[isetup,1]'
  ! Selection (add project selector if we process many projects!)
  let commandline "MFIND /DATE "'mtoc%setup[isetup,1]'" /BACKEND "'mtoc%setup[isetup,2]''optscan'
  execute 'commandline'
  ! Process data
  pipeline
next isetup
