#
# This script demonstrates using standard and high-precision MRO SCLK 
# kernels to convert HiRISE SCLK tags.
#

#
# SPICE kernels: 
#
#    -- standard SCLK file (1 tick = 1/256 sec; ID is -74);
#
#    -- high precision SCLK file (1 tick = 1/65536 sec; ID is -74999);
#
#    -- LSK file;
#
set STANDARD_SCLK_FILE = "MRO_SCLKSCET.00001.tsc"
set STANDARD_SCLK_ID   = "-74"

set HIGHPREC_SCLK_FILE = "MRO_SCLKSCET.00001.65536.tsc"
set HIGHPREC_SCLK_ID   = "-74999"

set LSK_FILE           = "naif0007.tls"

#
# SCLK strings:
#
#    -- high precision SCLK for HiRISE image;
#
#    -- standard SCLK derived from it (fractional part is replaced 
#       by a number computed by dividing original fractional part by
#       256 and rounding the quotient to the nearest integer)
#
#    -- same SCLK with zero'ed out fractional part;
# 
set SCLK_HIGHPREC      = "881352386:26903"
set SCLK_STANDARD      = "881352386:105"
set SCLK_NOFRACTION    = "881352386:0"

#
# Note that the CHRONOS runs below are equivalent to calling SCS2E, and
# then TIMOUT to get UTC for the given input SCLK string.
#

#
# Convert ``0'' fractional part SCLKs to UTC using standard and
# high-precision SCLK kernels.
#
echo " "
echo "converting $SCLK_NOFRACTION using $STANDARD_SCLK_FILE/$STANDARD_SCLK_ID"

chronos -setup $STANDARD_SCLK_FILE $LSK_FILE \
        -sc $STANDARD_SCLK_ID \
        -from sclk \
        -to utc \
        -format 'YYYY-MM-DDTHR:MN:SC.######' \
        -time $SCLK_NOFRACTION

echo " "
echo "converting $SCLK_NOFRACTION using $HIGHPREC_SCLK_FILE/$HIGHPREC_SCLK_ID"

chronos -setup $HIGHPREC_SCLK_FILE $LSK_FILE \
        -sc $HIGHPREC_SCLK_ID \
        -from sclk \
        -to utc \
        -format 'YYYY-MM-DDTHR:MN:SC.######' \
        -time $SCLK_NOFRACTION

#
# Convert high-precision SCLK with fractional part to UTC using 
# standard SCLK -- THE OUTPUT WILL BE INCORRECT.
#
echo " "
echo "converting $SCLK_STANDARD using $STANDARD_SCLK_FILE/$STANDARD_SCLK_ID"

chronos -setup $STANDARD_SCLK_FILE $LSK_FILE \
        -sc $STANDARD_SCLK_ID \
        -from sclk \
        -to utc \
        -format 'YYYY-MM-DDTHR:MN:SC.######' \
        -time $SCLK_STANDARD
        
#
# Convert high-precision SCLK with fractional part to UTC using 
# high precision SCLK.
#
echo " "
echo "converting $SCLK_HIGHPREC using $HIGHPREC_SCLK_FILE/$HIGHPREC_SCLK_ID"

chronos -setup $HIGHPREC_SCLK_FILE $LSK_FILE \
        -sc $HIGHPREC_SCLK_ID \
        -from sclk \
        -to utc \
        -format 'YYYY-MM-DDTHR:MN:SC.######' \
        -time $SCLK_HIGHPREC

#
# Convert high-precision SCLK with fractional part to UTC using 
# standard SCLK -- THE OUTPUT WILL BE INCORRECT.
#
echo " "
echo "converting $SCLK_HIGHPREC using $STANDARD_SCLK_FILE/$STANDARD_SCLK_ID"

chronos -setup $STANDARD_SCLK_FILE $LSK_FILE \
        -sc $STANDARD_SCLK_ID \
        -from sclk \
        -to utc \
        -format 'YYYY-MM-DDTHR:MN:SC.######' \
        -time $SCLK_HIGHPREC
echo "THIS RESULT IS INCORRECT."

echo " "
       
exit;
