#!/bin/csh
#
# This script is an example showing how one can generate INSIGHT "ltst
# noon UTC" table, "lmst noon UTC" table, and "ltst noon
# lmst" table using CHRONOS utility program. This script
# produces the first kind of table for every standard INSIGHT CHRONOS setup
# in current directory (it expects these setup files to have names
# "chronos.insight*") and the second kind of table for every INSIGHT
# lmst setup file in current directory (assumed to have
# names chronos_lmst.insight*). To generate the third type of table the
# script requires that a pair of setups -- standard and hybrid --
# with the same extension to be present.
#
# To learn more, read the script code -- it is rather simple :)
#
# BVS/NAIF, August 24, 2015
#
 

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

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

   #
   # Set output table file name, report it and "touch" the table file.
   #
   set TABLE = $SETUP:e.ltst_vs_utc.noon.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 LST to UTC
      #
      set UTC = `chronos -setup $SETUP -from LST -to UTC -time sol $SOL 12:00:00 -format 'YYYY-MM-DDTHR:MN:SC.###' -nolabel` 

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

      @ SOL++
   end

end

#
# Generate lmst vs. UTC table for each individual landing
# site setup file in current directory.
#
foreach SETUP ( chronos_lmst.insight* )

   #
   # Set output table file name, report it and "touch" the table file.
   #
   set TABLE = $SETUP:e.lmst_vs_utc.noon.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 SCLK (!!! this because LMST fits
      # nicely into SPICE SCLK subsystem :) to UTC.
      #
      set UTC = `chronos -setup $SETUP -from SCLK -to UTC -time $SOL\:12:00:00 -format 'YYYY-MM-DDTHR:MN:SC.###' -nolabel` 

      #
      # Print result pair in the format Paul wants to see :)
      #
      echo "LMST-SOL-"`echo $SOL | awk '{printf "%04d", $1}'`"-12:00:00 $UTC" >> $TABLE

      @ SOL++
   end

end

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

   #
   # Set output table file name, report it and "touch" the table file.
   #
   set TABLE = $SETUP:e.ltst_vs_lmst.noon.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 LST to ET seconds.
      #
      set ET = `chronos -setup $SETUP -from LST -to ET -totype SECONDS -time sol $SOL 12:00:00 -nolabel` 

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

      #
      # Print result pair in the format Paul wants to see :)
      # (awk is used to pad SOL number with zeros)
      #
      echo "LTST-SOL-"`echo $SOL | awk '{printf "%04d", $1}'`"-12:00:00 "`echo $LMST | sed "s/1\/\0\{1,5\}//g" | sed "s/\:/ /" | sed 's/:.....$//' | awk '{printf "LMST-SOL-%04d-%8s", $1, $2, $3}'` >> $TABLE

      @ SOL++
   end

end

exit 0

