!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
begin procedure calib-parse-date
  ! *** JP This should be a facility of MRTCAL (maybe it already is!)
  define character date*11 day*2 month*3 year*4 ncsmonth*2
  let date "&1" /upper
  let day   'date[1:2]'
  let month 'date[4:6]'
  let year  'date[8:11]'
  if .not.exist(ncsdate) then
     define character ncsdate*8 /global
  endif
  if (month.eq."JAN") then
     let ncsmonth "01"
  else if (month.eq."FEB") then
     let ncsmonth "02"
  else if (month.eq."MAR") then
     let ncsmonth "03"
  else if (month.eq."APR") then
     let ncsmonth "04"
  else if (month.eq."MAY") then
     let ncsmonth "05"
  else if (month.eq."JUN") then
     let ncsmonth "06"
  else if (month.eq."JUL") then
     let ncsmonth "07"
  else if (month.eq."AUG") then
     let ncsmonth "08"
  else if (month.eq."SEP") then
     let ncsmonth "09"
  else if (month.eq."OCT") then
     let ncsmonth "10"
  else if (month.eq."NOV") then
     let ncsmonth "11"
  else if (month.eq."DEC") then
     let ncsmonth "12"
  else
     message e parse-date "Unknown month "'month'
     return base
  endif
  let ncsdate 'year''ncsmonth''day'
  exa ncsdate
end procedure calib-parse-date
!
begin procedure calib-defaults
  if (.not.exist(pipe)) then
     define structure pipe /global
     define character pipe%imbfits*256 pipe%mrtcal*256 pipe%pattern*256 pipe%index*256 /global
  endif
  !
  let pipe%imbfits "/ncsServer/mrt/ncs/data/imbfits/het/"
  let pipe%mrtcal "raw-mrtcal/"
  sic mkdir 'pipe%mrtcal'
end procedure calib-defaults
!
begin procedure calib-pattern
  sic date
  if ("&1none".eq."none") then
     @ calib-parse-date 'sys_date'  
     let pipe%pattern "iram30m-*-"'ncsdate'"s*-imb.fits"
     let pipe%index 'pipe%mrtcal'"index-"'ncsdate'".mrt"
  else
     let pipe%pattern "iram30m-*-&1s*-imb.fits"
     let pipe%index 'pipe%mrtcal'"index-&1.mrt"
  endif
end procedure calib-pattern
!
begin procedure calib-open-file
  define character filename*256 dirname*256
  let dirname 'pipe%mrtcal'"&1/" 
  sic mkdir 'dirname'
  let filename 'dirname'"spectraOdp"'ncsdate'".30m"
  if .not.file(filename) then
     file out 'filename' single
  else 
     file out 'filename'
  endif
  say " "
  message i open-file "Opened output file "'filename'
  say " "
end procedure calib-open-file
!
begin procedure calib-data
  index update 'pipe%imbfits' /file 'pipe%index' /pattern 'pipe%pattern'
  if ("&2none".eq."none") then 
     mfind /completenes readable /calibrated &1 /date &3
  else
     mfind /completenes readable /calibrated &1 /projid &2 /date &3
  endif
  if (mfound.ne.0) then
     mlist /toc id observed /var mytoc
!     pause
     if (mytoc%nsetup.ge.1) then
        for isetup 1 to mytoc%nsetup
           mfind  /completenes readable /calibrated &1 /projid 'mytoc%setup[isetup,1]' /date 'mytoc%setup[isetup,2]'
           @ calib-parse-date 'mytoc%setup[isetup,2]'
           @ calib-open-file 'mytoc%setup[isetup,1]'
           if (mfound.ne.0) then
              mlist /column observed ut scan source obstype switchmode backend calib
              pipeline /show failed
           endif
        next isetup
     endif
  endif
end procedure calib-data
!
begin procedure calib-init
  mset book space 1024
  mset calib tsys linear
  mset calib off  linear
  @ calib-pattern &1
  @ calib-data none &2
end procedure calib-init
!
begin procedure calib-pipe
  for /while .true.
     index open 'pipe%imbfits' /file 'pipe%index' /pattern 'pipe%pattern'
     sic date
     say 'sys_date'
     ! *** JP The /pattern is needed below. Else "index watch" will try to
     ! *** JP index everything in the imbfits directory, including previous
     ! *** JP days of observations...
     index watch /pattern 'pipe%pattern'
     @ calib-data none
  next
end procedure calib-pipe
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! Example of online use 
@ calib-defaults
@ calib-init
@ calib-pipe
return
!
! Example of offline use
let pipe%imbfits "~/observationdata/imbfits/"
@ calib-init 20140804 022-14
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
