#!/bin/csh
#
# This script is an example showing how one can generate MER 
# "true LST midnight UTC" table and "approximate LST midnigt UTC" table
# using CHRONOS utility program. This script produces the first
# kind of table for every standard MER CHRONOS setup in current
# directory (it expects these setup files to have names 
# "chronos.mer*") and the second kind of table for every MER approximate 
# LST setup file in current directory (assumed to have names 
# chronos_hlst.mer*).
#
# To learn more, read the script code -- it is rather simple :)
#
# BVS/NAIF, October 16, 2002
#

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

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

   #
   # Set output table file name, report it and "touch" the table file.
   #
   set TABLE = $SETUP:e.ltst_vs_utc.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 00: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 "sol-"`echo $SOL | awk '{printf "%03d", $1}'`" $UTC" >> $TABLE

      @ SOL++
   end

end

#
# Generate approximate LST vs. UTC table for each individual landing
# site setup file in current directory.
#
foreach SETUP ( chronos_hlst.mer* )

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

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

      @ SOL++
   end

end

exit 0

