#!/usr/bin/ksh
#
# make_asi_products.ksh 
#
# This script initiates processes for creating a
# variety of orbit plots
#
#
#			Author: Tim Quinn
# 		Created: August 2008
#

#
## Set the environment variables
#
  if [[ -z $THMSOC ]]
  then
    THMSOC=/disks/socware/thmsoc_dp_current
    export THMSOC
  fi
	. $THMSOC/src/config/soc_it_to_me.config
	. $THMSOC/src/config/setup_themis_idl71.bourne

#
## Set logfiles
#
	processdate=$(date '+%y%m%d')
	ORBITS_LOGFILE=${LOGDIR}/orbits/idl_orbits_${processdate}
	echo "IDL Orbit output: $ORBITS_LOGFILE"
	touch $ORBITS_LOGFILE  #prevent complaints of appends to non-existent file
	
    LUNAR_ORBITS_LOGFILE=${LOGDIR}/orbits/idl_lunar_orbits_${processdate}
	echo "IDL Lunar Orbit output: $LUNAR_ORBITS_LOGFILE"
	touch $LUNAR_ORBITS_LOGFILE  #prevent complaints of appends to non-existent file
	
    LUNAR_ORBITS_SSE_LOGFILE=${LOGDIR}/orbits/idl_lunar_orbits_sse_${processdate}
	echo "IDL Lunar Orbit output: $LUNAR_ORBITS_SSE_LOGFILE"
	touch $LUNAR_ORBITS_SSE_LOGFILE  #prevent complaints of appends to non-existent file
#
## Start IDL Processes
#
	cd ${SCRATCH}/asi_production
	idl < ${IDL_THMSOC_DIR}/asi/orbit_batchfile >> $ORBITS_LOGFILE 2>> $ORBITS_LOGFILE
	idlcheck=$?
	if (( $idlcheck != 0 ))
	then
		msg="Warning: THEMIS ASI Production Problem"
		thmsoc_dblog.php $(basename $0) 1 "$msg"		### added 2012-06-25 - bsadeghi
	fi
	
	idl < ${IDL_THMSOC_DIR}/asi/lunar_orbit_batchfile >> $LUNAR_ORBITS_LOGFILE 2>> $LUNAR_ORBITS_LOGFILE
	idlcheck=$?
	if (( $idlcheck != 0 ))
	then
		msg="Warning: THEMIS ASI Lunar Orbit Production Problem"
		thmsoc_dblog.php $(basename $0) 1 "$msg"		### added 2012-06-25 - bsadeghi
	fi
	
	
	idl < ${IDL_THMSOC_DIR}/asi/lunar_orbit_sse_batchfile >> $LUNAR_ORBITS_SSE_LOGFILE 2>> $LUNAR_ORBITS_SSE_LOGFILE
	idlcheck=$?
	if (( $idlcheck != 0 ))
	then
		msg="Warning: THEMIS ASI Lunar SSE Orbit Production Problem"
		thmsoc_dblog.php $(basename $0) 1 "$msg"		### added 2012-06-25 - bsadeghi
	fi

#
## Cleanup
#
        # mms ephemeris files have to be downloaded for orbit plots but we don't 
        # want to keep them around
        rm -rf ${TMPDIR}/mms

	exit 0