!-----------------------------------------------------------------------
!   This procedure "simulates" online observations at the telescope.
! From the list of IMB-FITS files in the current working directory,
! it creates symlinks of the original IMB-FITS files to a target
! directory. These symlinks appear one after the other. There are 2
! modes in this context:
! - Interactive mode (default): user is prompted interactively to create
!          the next symlink.
! - Automatic mode (/AUTO): symlinks are created automatically,
!          respecting the time delays between each! Note that the time
!          may be speed up with the appropriate argument.
!
!  This procedure is intended to be executed in a dedicated process,
! while another calibration process tries to calibrate the files as
! they appear. In order to help the calibration process to know what
! is the "current simulated date-time", a file named
! "pipeline-simulator.datetime" is created in the target directory. It
! contains the "current simulated date-time" under the MJD format.
!
! Calling sequence:
!  @ pipeline-simulator  TargetDir   ! Target directory for symlinks to IMB-FITS
!       /AUTO SpeedFactor            ! Time speed up factor, optional (default 1, i.e. no speed up)
!       /DATE YYYYMMDD               ! Process only this date (default all dates)
!-----------------------------------------------------------------------
!
begin procedure pipeline-simulator-init
  !
  if .not.exist(pipe) then
    define structure pipe         /global
    define double    pipe%speed   /global
    define char*512  pipe%target  /global
    define char*8    pipe%date    /global
  endif
  !
end procedure pipeline-simulator-init
!
@ pipeline-simulator-init
!
! --- Argument parsing ---
parse 1 /auto 0 1 /date 1
!
let pipe%target 'pro%parse%command%arg[1]'"/"
sic mkdir 'pipe%target'
!
if pro%parse%auto%narg.eq.1 then
  let pipe%speed 'pro%parse%auto%arg[1]'
else if pro%parse%auto%narg.eq.1 then
  let pipe%speed 1
else
  let pipe%speed -1  ! Negative means interactive mode
endif
!
if pro%parse%date%narg.gt.0 then
  let pipe%date 'pro%parse%date%arg[1]'
else
  let pipe%date "*"
endif

! --- Files to simulate ---
index build
mfind /date 'pipe%date'
mlist

! Retrieve the date-time of each file
define double mjd[mfound]
variable
datetime /from mdx%dobs GAG_DATE mdx%ut RADIAN /to mjd MJD

! Define the starting point
define double startmjd curmjd waittime
define char*32 curiso
let startmjd mjd[1]  ! MFIND guarantees the file are cleverly sorted

let curmjd startmjd
for ifile 1 to mfound
  say ""
  !
  if pipe%speed.gt.0 then
    let waittime (mjd[ifile]-curmjd)*86400/pipe%speed
    say "Waiting "'waittime'" seconds"
    sic wait waittime
  else
    pause "Type C for next file"
  endif
  !
  if mjd[ifile].eq.mjd[ifile] then
    ! Protection againt NaNs coming from mdx%ut for unreadable files. If
    ! unreadable, reuse the previous MJD to avoid troubles.
    let curmjd mjd[ifile]
  endif
  datetime /from curmjd MJD /to curiso iso
  say "Date-time is now "'curiso'
  sic out 'pipe%target'"/pipeline-simulator.datetime"
  say 'curmjd'
  sic out
  !
  say "Creating link "'pipe%target'"/"'mdx%filename[ifile]'
  system "ln -s "'sic%directory'"/"'mdx%filename[ifile]'" "'pipe%target'
  !
next ifile
say "All files created"
