#!/usr/bin/ksh
#
# make_mag_stack_plots.ksh - Just like it says!
#
#	Usage: make_mag_stack_plots.ksh yyyy-mm-dd 
#
#	Arguments:
#						$1 = yyyy-mm-dd
#
# Creation Date:
#
#								13 November 2006 TimQuinn
#
#	Updates:
#					Matt changed thg_mag_stackplot.pro to thm_gmag_stackplot.pro 
#					in the repository
#

#
## 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
  . /home/thmsoc/astraea2_test/soc_it_to_me.config
. $THMSOC/src/config/setup_themis.bourne
	#. $THMSOC/src/config/ground_mag.config
. /home/thmsoc/astraea2_test/ground_mag_test.config

#
## Create variables derived from the input fileroot variable
#
	date=$1
	if [[ -z $date ]]
	then
		echo ":$$:make_mag_stack_plots:" >> $LOGFILE
		echo ":$$:make_mag_stack_plots: Date input argument not given. Quitting....." >> $LOGFILE
		echo ":$$:make_mag_stack_plots:" 
		echo ":$$:make_mag_stack_plots: Date input argument not given. Quitting....." 
		exit 0
	fi
	year=$(echo $date | awk -F- '{print $1}')
	mkdir -p ${STACKPLOTS}/$year
	month=$(echo $date | awk -F- '{print $2}')
	day=$(echo $date | awk -F- '{print $3}')
	workdir=${STACKPLOTS}/tmp/stack_4_${year}${month}${day}_$$
	mkdir -p $workdir
	mysql_table_name=stackplot_file_list

## Build IDL batch file
#
cp ${THMSOC}/idl/thmsoc/summary_plots/thm_gmag_stackplot.bm $workdir
line="thm_gmag_stackplot,'$date',1,/make_png,/no_expose"
echo $line >> ${workdir}/thm_gmag_stackplot.bm
echo exit >> ${workdir}/thm_gmag_stackplot.bm

## Now run idl with batch file
#
echo :$$:make_mag_stack_plots: >> $LOGFILE
echo :$$:make_mag_stack_plots: Starting CDF to PNG conversion for ${fileroot} at $(date)>> $LOGFILE
cd $workdir
#idl start3dp thg_mag_stackplot.bm 1>> ${STACKLOGDIR}/IDLOUT_STACK_${date} 2>/dev/null
idl thm_gmag_stackplot.bm 1>> $LOGDIR/IDLOUT_STACK_${date}_$$ 2>/dev/null
#idl thg_mag_stackplot.bm 

## Check IDL run for problems
#
if (( $? != 0 ))
then
	echo :$$:make_mag_stack_plots: >> $LOGFILE
	echo :$$:make_mag_stack_plots: ERROR in IDL process: Notifying OPS Personnel>> $LOGFILE
	printf "%s\n" "script: $0" \
	"Warning: Stack Plot IDL run for $date finished with an error" |
	mailx -s "THEMIS Processing Problem - Stack Plots" $SOC_EMAIL
	cd
	rm -rf $workdir
	exit 1
fi

processTime=$(date -u '+%Y-%m-%d %T')

## Check to make sure png files were created
#
if [[ -z $(ls -1 *png 2>/dev/null) ]]
then
	echo :$$:make_mag_stack_plots: >> $LOGFILE
	echo :$$:make_mag_stack_plots: ERROR in IDL process: Notifying OPS Personnel>> $LOGFILE

	printf "%s\n" "Subject: THEMIS SOC Processing Problem" \
	"script: $0" \
	"Warning: Stack Plot IDL run for $date did not produce PNG files" |
	mailx -s "THEMIS Processing Problem - Stack Plots" $SOC_EMAIL

	cd
	rm -rf $workdir
	exit 1
fi

echo :$$:make_mag_stack_plots: >> $LOGFILE
for pngfile in $(ls -1 *png)
do
	echo :$$:make_mag_stack_plots: png file $pngfile created >> $LOGFILE
	dataTime=$(echo $year $month $day | awk '{printf("%s-%02s-%02s 00:00:00",$1,$2,$3)}')
	fileSize=$(ls -l $pngfile |awk '{print $5}')
	echo :$$:make_mag_stack_plots: moving $pngfile to ${STACKPLOTS}/${year} >> $LOGFILE
	mv $pngfile ${STACKPLOTS}/${year}
	/home/thmsoc/astraea2_test/php/gmag_stackplots_test.php $mysql_table_name $dataTime $pngfile $processTime $fileSize
done

## Cleanup directory
#
	cd
	rm -rf $workdir
	rm -rf $LOGDIR/IDLOUT_STACK_${date}_$$

	exit 0
