#!/bin/csh
#
# This script is an example showing how one can generate MSR SRL "ltst
# midnight L_s" table and "lmst midnight L_s" table using CHRONOS 
# utility program. This script produces the first kind of table for 
# every standard MSR SRL CHRONOS setup in current directory (it expects 
# these setup files to have names "chronos.msr_srl*") and the second 
# kind of table for every pair of MSR SRL standard and lmst setup files 
# in current directory (lmst setups are assumed to have names 
# chronos_lmst.msr_srl*).
#
# To learn more, read the script code -- it is rather simple :)
#
# BVS/NAIF, February 26, 2023
#
 

#
# Set maximum number of SOLs in the output table.
#
set MAXSOL = 1000
#set MAXSOL = 10

#
# Generate ltst vs. ls table for each individual landing
# site setup file in current directory.
#
foreach SETUP ( chronos.msr_srl* )

   #
   # Set output table file name, report it and "touch" the table file.
   #
   set TABLE = $SETUP:e.ltst_vs_ls.midnight.tab
   if ( -e $TABLE ) then
      echo " Cannot proceed: file $TABLE already exists."
      exit 1
   endif

   echo "Making $TABLE ..."
   touch $TABLE

   #
   # Run chronos for each SOL starting with SOL 1.
   #
   @ SOL = 1
   while ( $SOL <= $MAXSOL )

      #
      # Run CHRONOS to convert from LTST to L_s.
      #
      set LSUN = `chronos -setup $SETUP -from LST -to LST -totype LSUN -time sol $SOL 00:00:00 -nolabel` 

      #
      # Print result pair in the format Paul wants to see :)
      # (awk is used to pad SOL number with zeros)
      #
      echo $SOL $LSUN | awk '{printf "LTST-SOL-%04d-00:00:00 %8.3f\n", $1, $2}'  >> $TABLE

      @ SOL++
   end

end

#
# Generate lmst vs. ls table for each individual landing
# site setup file in current directory.
#
foreach SETUP ( chronos.msr_srl* )

   #
   # Set output table file name, report it and "touch" the table file.
   #
   set TABLE = $SETUP:e.lmst_vs_ls.midnight.tab
   if ( -e $TABLE ) then
      echo " Cannot proceed: file $TABLE already exists."
      exit 1
   endif

   echo "Making $TABLE ..."
   touch $TABLE

   #
   # Run chronos for each SOL starting with SOL 1.
   #
   @ SOL = 1
   while ( $SOL <= $MAXSOL )

      #
      # Run CHRONOS to convert from LMST to ET seconds.
      #
      set ET = `chronos -setup chronos_lmst.$SETUP:e -from SCLK -to ET -totype SECONDS -time $SOL 00:00:00 -nolabel` 

      #
      # Run CHRONOS to convert from ET seconds to L_s.
      #
      set LSUN = `chronos -setup $SETUP -from ET -fromtype SECONDS -to LST -totype LSUN -time $ET -nolabel` 

      #
      # Print result pair in the format Paul wants to see :)
      # (awk is used to pad SOL number with zeros)
      #
      echo $SOL $LSUN | awk '{printf "LMST-SOL-%04d-00:00:00 %8.3f\n", $1, $2}'  >> $TABLE

      @ SOL++
   end

end

exit 0

