#!/usr/bin/ksh
#
# make_ae_index.ksh 
#
# This script generates lines for a IDL batch
# file which creates AE Index cdfs from the gmag cdfs
#
# Arguments:
#	
#		$1=yyyymmdd ; start date
#   $2=yyyymmdd ; end date
#
#
#			Author: Tim Quinn
# 		Created: July 2009
#

#
## 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

#
##
## Function Declaration
##
#
	autoload pn_month
	FPATH=${THMSOC}/src/ksh/funcs

	function run_idl
	{
		rm -f *.bm
		## Prepare date argument for IDL routine
		yyyy=$(echo $startDate | awk '{print substr($1,1,4)}')
		mm=$(echo $startDate | awk '{print substr($1,5,2)}')
		dd=$(echo $startDate | awk '{print substr($1,7,2)}')
		date="${yyyy}-${mm}-${dd}"
		idloutLog=idlout_${yyyy}${mm}${dd}
		location=${workstation}:${processDir}
		## Build batch file content
		echo "thm_l1gen_idx,date='$date',mastercdf='$mastercdf',temp_dir='$processDir'" >> make_ae_index.bm
		echo exit >> make_ae_index.bm
		processTime=$(date -u '+%Y-%m-%d %T')
		idl make_ae_index.bm 1>$idloutLog 2>&1
		idlStatus=$?
		if (( $idlStatus != 0 ))
		then
			echo ":$$:make_ae_index: ERROR in IDL Process for $date" >> $LOGFILE
			echo ":$$:make_ae_index: Check ${location}/${idloutLog}" >> $LOGFILE
			echo ":$$:make_ae_index:" >> $LOGFILE
			printf "%s\n" "script: $0" \
			"Warning: idl ${location}/make_ae_index.bm finished with an error." \
			"Warning: Check ${location}/${idloutLog}" |
			mailx -s "THEMIS Processing Problem - AE Index Creation" $SOC_EMAIL
		fi

		## The IDL run should have produced a cdf file - check
		## File naming convention is thg_l1_idx_yyyymmdd_v01.cdf
		#
		cdffile=$(ls thg*cdf) 2>/dev/null

		if [[ -z $cdffile ]]
		then
			echo ":$$:make_ae_index:" >> $LOGFILE
			echo ":$$:make_ae_index: ERROR: The IDL run failed to produce a cdf file!!" >> $LOGFILE
			echo ":$$:make_ae_index: Check ${location}/${idloutLog}">> $LOGFILE
			echo ":$$:make_ae_index:" >> $LOGFILE
			printf "%s\n" "script: $0" \
			"Warning: idl ${location}/make_ae_index.bm did not produce a cdf file." \
			"Warning: Check ${location}/${idloutLog}" |
			mailx -s "THEMIS Processing Problem - AE Index Creation" $SOC_EMAIL
		else
			## Now move files to the appropriate directory
			#
			year=$(echo $cdffile | awk -F_ '{print substr($4,1,4)}')
			cdf_archive_dir=${THEMISDATA}/thg/l1/mag/idx/${year}
			mkdir -p $cdf_archive_dir
			mv -f $cdffile $cdf_archive_dir

			echo ":$$:make_ae_index:" >> $LOGFILE
			echo ":$$:make_ae_index: files moved to $cdf_archive_dir." >> $LOGFILE

			## Update checksum file
			#
			${KSH_SCRIPTS}/fileChkSum.ksh ${cdf_archive_dir}/${cdffile}

			## Placeholder for trigger to make a plot of the AE Index
			#
			####echo ${year}-${month}-${day} > ${WORKMAGSTACKS}/stack_${year}${month}${day}_$$

			## Database update
			#
			dataTime=$(echo $date | awk '{printf("%s 00:00:00",$1)}')
			cdffileSize=$(ls -l ${cdf_archive_dir}/${cdffile} |awk '{print $5}')
			gmag_cdf_test.php gbo_aeIndex_cdffiles $dataTime $cdffile $processTime $cdffileSize
		fi
	}			

#
##
## End Function Declaration
##
#

#
##
## Begin Work
##
#

#
## Check if LOCAL working directory exists
#
	workstation=$(uname -n)
	if [[ ! -d  /mydisks/home/thmsoc ]]
	then
		echo "/mydisks/home/thmsoc does not exist on $workstation"
		echo "exiting procedure $0......................"
		printf "%s\n" "script: $0" \
		"Warning: /mydisks/home/thmsoc does not exist on" \
		"Warning: workstation $workstation" |
		mailx -s "THEMIS Data Processing Problem - AE Index" $SOC_EMAIL
		exit 1
	fi	

#
## Grab input variables
#
	startDate=$1
	endDate=$2

	if [[ -z $startDate && -z $endDate ]]
	then
		startDate=$(date -u '+%Y%m%d')
	fi

#
## Create scratch directory for this run
#
	thisDir=AEIndex_${startDate}_${endDate}_$$
	processDir=${LOCAL}/${thisDir}/
	rm -rf $processDir
	mkdir -p $processDir
	cd $processDir

#
## Create general log file
#
    processTime=$(date -u '+%Y%m%d')
    LOGFILE=$LOGDIR/ae_index/make_ae_index_${processTime}_$$

	echo ":$$:make_ae_index:" >> $LOGFILE
	echo ":$$:make_ae_index:Starting make_ae_index at $(date)" >> $LOGFILE
	echo ":$$:make_ae_index:" >> $LOGFILE
	echo ":$$:make_ae_index: processing directory created:" >> $LOGFILE
	echo ":$$:make_ae_index: $processDir" >> $LOGFILE

#
## Copy over the master CDF
#
	echo ":$$:make_ae_index:" >> $LOGFILE
	echo ":$$:make_ae_index: Copying over master CDF........" >> $LOGFILE
	mkdir -p mastercdf
	cp ${IDL_THMSOC_DIR}/l1gen/mastercdfs/thg_l1_idx_00000000_v01.cdf ./mastercdf
	mastercdf=${processDir}mastercdf/thg_l1_idx_00000000_v01.cdf

#
## Start date loop
#

	if [[ -z $endDate ]]
	then
		run_idl
	else
		while [[ $startDate != $endDate ]]
		do
			run_idl
			startDate=$(pn_day $startDate 1)
		done
		run_idl
	fi

#
## Cleanup
#
	exit 0
