!-----------------------------------------------------------------------
!  @ pipeline-report.cal  Keyword OutputFile
!    /REFDATE RefDate   (YYYYMMDD)
!    /LOGFILE InputFile
!    /INDEX IndexFile
!    /CONTINUUM YES|NO
!
! Keyword should be "online" or "offline". This will go in the report
! header.
!
!  Generate a summary of DONE, FAILED, EMPTY and NONE calibrations into
! the OutputFile. This is based on the calibration status found in the
! index file, passed with option /INDEX (default "raw-mrtcal/index.mrt").
!
!  The summary is performed for UNKNOWN CALIBRATE TRACKED and
! ONTHEFLYMAP scans. If /CONTINUUM is YES (default NO), POINTING and
! FOCUS are also shown.
!
!  By default, the summary is performed for Yesterday calibrations. This
! can be enlarged to "RefDate to Yesterday" with the option /REFDATE.
!
!  The reasons of failures are not known in the index file. However, if
! a log file (mrtcal pipeline session) is provided, it will be parsed
! to extract the relevant information regarding all FAILED calibrations.
!-----------------------------------------------------------------------

begin procedure report-init

  ! Restart from a new log file
  sic delete 'report%outfile'

  ! Create a file with a single blank line
  sic output 'report%outfile'".blank"
  say ""
  sic output

  if (report%logfile.ne." ") then
    ! Check if Mrtcal logfile is empty. This may indicate it was not
    ! restarted since a long time.
    define integer nl
    compute nl lines 'report%logfile'
    let report%logfileempty nl.le.0
  endif

  ! Compute number of entries per calibration status
  @ report-stat

end procedure report-init

begin procedure report-mfind
  if pro%narg.lt.1 then
    message e report-mfind "At least 1 calibration status needed"
    exit
  endif
  !
  ! Make the report with only a subset of observation types.
  define char*1024 cline
  let cline "mfind"
  ! /DATE option
  let cline 'cline'" /date "'report%refdate'" to yesterday"
  ! /OBSTYPE option
  let cline 'cline'" /obstype UNKNOWN CALIBRATE TRACKED ONTHEFLYMAP"
  if report%continuum then
    let cline 'cline'" POINTING FOCUS"
  endif
  ! /CALIBRATED option
  let cline 'cline'" /calibrated"
  for iarg 1 to pro%narg
    let cline 'cline'" "'pro%arg[iarg]'
  next
  !
  sic\execute 'cline'
  !
end procedure report-mfind

begin procedure report-mlist-toc
  ! MLIST /TOC
  mlist /toc calib obstype switchmode backend -
        /file 'report%outfile'".list"
  sic append 'report%outfile'".blank" 'report%outfile'
  sic append 'report%outfile'".list" 'report%outfile'
end procedure report-mlist-toc
!
begin procedure report-mlist-column
  ! MLIST /COLUMN
  mlist /column observed ut scan projid source obstype switchmode backend comple calib -
        /file 'report%outfile'".list"
  sic append 'report%outfile'".blank" 'report%outfile'
  sic append 'report%outfile'".list" 'report%outfile'
end procedure report-mlist-column
!
begin procedure report-stat

  index open 'report%imbfits' /file 'report%index'

  @ report-mfind *
  let report%mftot mfound

  @ report-mfind none empty skipped
  let report%mfnone mfound

  @ report-mfind failed
  let report%mffail mfound

  @ report-mfind done
  let report%mfdone mfound

end procedure report-stat
!
begin procedure report-header

  ! Put a header. This header is used as subject when sending the report
  ! by email (must be first)
  sic output 'report%outfile' append
  define char stat*128
  let stat " ("'report%mfdone'" DONE, "'report%mffail'" FAILED, "'report%mfnone'" NONE/EMPTY/SKIPPED)"

  define char*11 refdate yesterday
  datetime /from report%refdate YYYYMMDD /to refdate   DD-MMM-YYYY
  datetime /from previous       DAY      /to yesterday DD-MMM-YYYY
  if refdate.eq.yesterday then
    say "Mrtcal "'report%kind'" pipeline report for "'yesterday''stat'
  else
    say "Mrtcal "'report%kind'" pipeline report for "'refdate'" to "'yesterday''stat'
  endif
  sic output

end procedure report-header
!
begin procedure report-rate

  ! Put the rate (files/sec)
  ! Assume the rate is found in the last line of the log file
  !
  if (report%logfile.eq." ")  return
  !
  ! ZZZ Debug this
  ! ZZZ Make it compatible with online logs?
  ! sic\system "tail -1 "'report%logfile'" >> "'report%outfile'

end procedure report-rate
!
begin procedure report-none

  @ report-mfind none empty skipped

  sic output 'report%outfile' append
  say ""
  say "=== NONE/EMPTY/SKIPPED (list) =========================================="
  say ""
  @ report-summary "NONE/EMPTY/SKIPPED"
  sic output

  if (mfound.eq.0)  return

  @ report-mlist-column

end procedure report-none
!
begin procedure report-failed

  @ report-mfind failed

  sic output 'report%outfile' append
  say ""
  say "=== FAILED (list) ======================================================"
  say ""
  @ report-summary "FAILED"
  sic output

  if (mfound.eq.0)  return

  @ report-mlist-column

  if (report%logfile.eq." ")  return

  sic output 'report%outfile' append
  say ""
  say "=== FAILED (details) ==================================================="
  sic output

  if (report%logfileempty) then
    sic output 'report%outfile' append
    say ""
    say "Log file is empty => no details"
    sic output
    return
  endif

  define character*13 ut  ! NB: 13 characters long, but TRUNCATED (not ROUNDED)
                          ! to 11 characters hereafter to match the behaviour
                          ! of the commands LIST and PIPELINE ("Calibrating...")
  define character*9 backend
  define integer iback
  for i 1 to mfound
    let ut mdx%ut[i] /sexa h r
    let iback 1+mdx%backend[i]
    let backend 'mrt%backend[iback]'
    sic\system "echo """" >> "'report%outfile'
    sic\system "echo ""---"" >> "'report%outfile'
    sic\system "awk /""Calibrating.*"'ut[1:11]'".*"'mdx%scan[i]'".*"'backend'"""/,/^"" ""*$/{print} "'report%logfile'" >> "'report%outfile'
  next i

end procedure report-failed
!
begin procedure report-done

  @ report-mfind done

  sic output 'report%outfile' append
  say ""
  say "=== DONE (table of contents) ==========================================="
  say ""
  !
  @ report-summary "DONE"
  sic output

  if (mfound.eq.0)  return

  @ report-mlist-toc

end procedure report-done
!
begin procedure report-summary
  !---------------------------------------------------------------------
  ! SAY one-line summary
  !  pro%arg[1] Kind of entries (DONE, FAILED, etc)
  !---------------------------------------------------------------------
  !
  define char*11 refdate yesterday
  datetime /from report%refdate YYYYMMDD /to refdate   DD-MMM-YYYY
  datetime /from PREVIOUS       DAY      /to yesterday DD-MMM-YYYY
  !
  if refdate.eq.yesterday then
    say 'mfound'/'report%mftot'" "'pro%arg[1]'" entries for "'yesterday'
  else
    say 'mfound'/'report%mftot'" "'pro%arg[1]'" entries for "'refdate'" to "'yesterday'
  endif
  !
end procedure report-summary

if (.not.exist(report)) then
  define structure report                                   /global
  define character*12  report%kind                          /global
  define character*256 report%imbfits report%index          /global
  define character*256 report%outfile report%logfile        /global
  define character*11  report%refdate                       /global
  define integer report%mftot                               /global
  define integer report%mfdone report%mffail report%mfnone  /global
  define logical report%logfileempty report%continuum       /global
endif

! Command line parsing
sic\parse 2 /refdate 1 /logfile 1 /index 1 /continuum 1
! Arguments
let report%kind    'pro%parse%command%arg[1]'
let report%outfile 'pro%parse%command%arg[2]'
if pro%parse%refdate%narg.eq.1 then
  let report%refdate 'pro%parse%refdate%arg[1]'
else
  datetime /from PREVIOUS DAY /to report%refdate YYYYMMDD
endif
if pro%parse%logfile%narg.eq.1 then
  let report%logfile 'pro%parse%logfile%arg[1]'
else
  let report%logfile ""
endif
if pro%parse%index%narg.eq.1 then
  let report%index 'pro%parse%index%arg[1]'
else
  let report%index "raw-mrtcal/index.mrt"
endif
if pro%parse%continuum%narg.eq.1 then
  let report%continuum 'pro%parse%continuum%arg[1]'
else
  let report%continuum no
endif
let report%imbfits "/ncsServer/mrt/ncs/data/imbfits/het/"

@ report-init
@ report-header  ! Put a header
@ report-rate    ! Give the rate

! --- Reconstruct the command line ---
! define character*1024 commandline
! let commandline "@ "'pro%name'
! define integer nc
! for iarg 1 to pro%narg
!   let nc len_trim(commandline)
!   let commandline['nc+2':] 'pro%arg[iarg]'
! next iarg
! !
! ! Save command line in outfile
! sic out 'report%outfile' append
! say ""
! say "Generated with command:"
! say 'commandline'
! sic out
! del /var commandline
! --- Done command line ---

@ report-done
@ report-none
@ report-failed

exit  ! Close Mrtcal (must give hand back to the calling shell script)
