#! /bin/bash
#
# run_cips_processing.sh
#
# Lon Riesberg
#
# Date: 6-1-07
#
# Notes: 
#   1. provided day is expected to be an AIM mission day number
#   2. nominally, the CIPS Observation Catalog is only updated
#      when the production processing is run from the cron.  to
#      update the Observation Catalog from here, set_production_flag
#      needs to be called in the IDL part of run_cips_processing.sh
#   3. similarly, calibration diagnostic files are only saved 
#      when the production processing is run from the cron.  to
#      save calibration diagnostic files from here, add the following
#      to the IDL part of run_cips_processing.sh:
#         set_debug_level, 6
#      note that if this code is run over a time range that the 
#      calibration diagnostic files use considerably more space
#      than standard 1A files and it's very easy to overfill the
#      the disk.
#

# make sure that mission day is provided
if [ ${#} -lt 1 ]
    then
    echo "Usage: run_cips_processing [ aim_day ] [ debug_level ] [ production_flag ]"
    echo
    exit 1
fi

aim_day=`printf "%04d" ${1}`

# debug_level
if [ ${#2} -gt 0 ]
   then
   debug_level=${2}
else
   debug_level=0
fi

# production_flag
if [ ${#3} -gt 0 ]
   then
   production_flag=${3}
else
   production_flag=0
fi
echo ${debug_level}
echo ${production_flag}

# create cips data files and send PAN's to PDC
function processCipsData
{
idl <<EOF 2>&1 | tee ${logFile}
catch, err
set_cips_production_vars
start_time = ad2usec(${aim_day})
stop_time = ad2usec(${aim_day} + 1)
print, 'start time: ', start_time
print, 'stop time: ', stop_time
set_debug_level, ${debug_level}
print, "Setting production flag to ", ${production_flag}
set_production_flag, flag=${production_flag}
print, "run_cips_production_processing", start_time, stop_time
run_cips_production_processing, start_time, stop_time
EOF
}

# if user is aimsds, create log file
date=`date '+%Y%m%d-%H_%M_%S'`
userName=`whoami`
if [ ${userName}  == "aimpro" ]
    then
    if [ ${production_flag}  > 0 ]
    then
        logFile=/aim/logs/cips/production/cips_day_${aim_day}_${date}.txt
    else
        logFile=/aim/logs/cips/preliminary/cips_day_${aim_day}_${date}.txt
    fi
    processCipsData 2>&1 | tee ${logFile}  
else
    processCipsData
fi

exit 0
# Done
