#!/usr/bin/perl
#
# This small script illustrates how three subsequent runs of chronos can 
# help to convert ``future'' SCET time to ``today's'' ERT. 
#
# BVS/NAIF, October 29, 1999
#
# CHRONOS and setup file locations. 
#
$chronos_exe = "chronos";
$setup_file  = "chronos.mpl";

#
# Get command line arguments.
#
if ( @ARGV != 2 ){
   print "\n   Usage:                                                   ".
         "\n                                                            ".
         "\n    > scet2stl_ert.pl '<SCET TIME>' <DAYS>                  ".
         "\n                                                            ".
         "\n   Don't forget quotes around SCET! DAYS must be a signed   ".
         "\n   integer (-2 corresponds to the day before yesterday with ".
         "\n   respect to SCET TIME.)                                   ".
         "\n                                                            ".
         "\n                                                            \n";
   exit;
} else {
   $input_scet = $ARGV[0];
   $days = $ARGV[1];
}

#
#  Run chronos to convert ``future'' SCET to ``future'' ERT 
#  and get ERT as ET Julian date past on output. 
#
$future_ert_sec = `$chronos_exe -setup $setup_file -from utc -to et -totype ert -nolabel -format 'JULIAND.##########' -time $input_scet 2>&1`;
$status = ( $? >> 8 );
if ( $status ) {
   die "CHRONOS failed to convert input SCET to ERT Julian Date. Error message was:\n$future_ert_sec\n";
} else {
   chop( $future_ert_sec );
}

#
# Add number of days to the computed Julian Date.
#
$todays_ert_sec = $future_ert_sec + $days;

#
# Run CHRONOS second time to convert ``corrected'' ERT to UTC.
#
$todays_ert = `$chronos_exe -setup $setup_file -from et -to utc -nolabel -time JD $todays_ert_sec 2>&1`;
$status = ( $? >> 8 );
if ( $status ) {
   die "CHRONOS failed to convert corrected ERT Julian date to UTC. Error message was:\n$todays_ert\n";
} else {
   print $todays_ert;
}

#
# All done.
#
exit;

