#!/usr/bin/ksh
#
# maccs_ascii2all.ksh 
#
# This script generates lines for a IDL batch
# file which creates gmag cdfs for MACCS data files. 
#
# Arguments:
#	
#		$1=site ; nain, cdrt, rbay, etc.
#		$2=yyyymmdd ; start date
#   $3=yyyymmdd ; end date
#
#
#			Author: Tim Quinn
# 		Created: April 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
#	. $THMSOC/src/config/maccs_ascii2all.config
. /home/thmsoc/astraea2_test/maccs_ascii2all_test.config

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

	function run_idl
	{
		origFilename=${fileSite}${startDate}_l0_sec.sec
		newFilename=${site}_${startDate}_maccs.dat

		cp ${UCB_MIRROR_HOME}/${fileSite}/${yyyy}/$origFilename ./$newFilename
		rm -f *.bm
		echo "thm_maccs_gmag_l2gen,'$newFilename'" >> ${newFilename}.bm
		echo exit >> ${newFilename}.bm
		processTime=$(date -u '+%Y-%m-%d %T')
		idl ${newFilename}.bm 1>${IDLOUT}_${newFilename} 2>&1
		idlStatus=$?
		if (( $idlStatus != 0 ))
		then
			echo ":$$:maccs_ascii2all: ERROR in IDL Process for $newFilename" >> $LOGFILE
			echo ":$$:maccs_ascii2all: Check ${IDLOUT}_${newFilename}" >> $LOGFILE
			echo ":$$:maccs_ascii2all:" >> $LOGFILE
			printf "%s\n" "script: $0" \
			"Warning: idl ${processDir}/${newFilename}.bm finished with an error." \
			"Warning: Check ${IDLOUT}_${newFilename}" |
			mailx -s "THEMIS Processing Problem - MACCS Conversion to CDF" $SOC_EMAIL
		fi

		## The IDL run should have produced a cdf and txt file - check
		#
		cdffile=$(ls thg*cdf) 2>/dev/null
		txtfile=$(ls thg*txt) 2>/dev/null

		if [[ -z $cdffile || -z $txtfile ]]
		then
			echo ":$$:maccs_ascii2all:" >> $LOGFILE
			echo ":$$:maccs_ascii2all: ERROR: The IDL run failed to produce both the cdf and txt!!" >> $LOGFILE
			echo ":$$:maccs_ascii2all: Check ${IDLOUT}_${newFilename}" >> $LOGFILE
			echo ":$$:maccs_ascii2all:" >> $LOGFILE
			printf "%s\n" "script: $0" \
			"Warning: idl ${processDir}/${newFilename}.bm did not produce txt and/or cdf." \
			"Warning: Check ${IDLOUT}_${newFilename}" |
			mailx -s "THEMIS Processing Problem - MACCS Conversion to CDF" $SOC_EMAIL
		else
			## Now move files to the appropriate directory
			#
			year=$(echo $cdffile | awk -F_ '{print substr($5,1,4)}')
			site=$(echo $cdffile | awk -F_ '{print $4}')
			if [[ $os = "SunOS" ]]
			then
				ucsite=$(echo $site | nawk '{print toupper($1)}')
			else
				ucsite=$(echo $site | awk '{print toupper($1)}')
			fi
			month=$(echo $cdffile | awk -F_ '{print substr($5,5,2)}')
			day=$(echo $cdffile | awk -F_ '{print substr($5,7,2)}')
			cdf_archive_dir=${L2MAGDIR}/${site}/${year}
			ascii_archive_dir=${ASCIIMAGDIR}/${site}/${year}
			mkdir -p $cdf_archive_dir
			mv $cdffile $cdf_archive_dir

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

			mkdir -p $ascii_archive_dir
			mv $txtfile $ascii_archive_dir
	
			## Update checksum file
			#
			${KSH_SCRIPTS}/fileChkSum.ksh ${ascii_archive_dir}/${txtfile}

			## Create file for stack plot production
			#
			echo ${year}-${month}-${day} > ${WORKMAGSTACKS}/stack_${year}${month}${day}_$$
			echo ":$$:maccs_ascii2all:" >> $LOGFILE
			echo ":$$:maccs_ascii2all: files moved to $cdf_archive_dir." >> $LOGFILE

			## Database update
			#
			dataTime=$(echo $year $month $day | awk '{printf("%s-%02s-%02s 00:00:00",$1,$2,$3)}')
			mysql_cdftable_name=gbo_${ucsite}_cdffiles
			mysql_asciitable_name=gbo_${ucsite}_asciifiles
			cdffileSize=$(ls -l ${cdf_archive_dir}/${cdffile} |awk '{print $5}')
			txtfileSize=$(ls -l ${ascii_archive_dir}/${txtfile} |awk '{print $5}')
			/home/thmsoc/astraea2_test/php/gmag_cdf_test.php $mysql_cdftable_name $dataTime $cdffile $processTime $cdffileSize
			/home/thmsoc/astraea2_test/php/gmag_ascii_test.php $mysql_asciitable_name $dataTime $txtfile $processTime $cdffileSize
		fi
	}			

	function dateBreak 
	{
		yy=$(echo $startDate | awk '{print substr($1,3,2)}')
		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)}')
		doy=$(date2doy ${yyyy}-${mm}-${dd} | awk '{printf("%03d",$1)}')
	}			

#
##
## End Function Declaration
##
#

#
##
## Begin Work
##
#
	echo ":$$:maccs_ascii2all:" >> $LOGFILE
	echo ":$$:maccs_ascii2all:Starting maccs_ascii2all at $(date)" >> $LOGFILE
	echo ":$$:maccs_ascii2all:" >> $LOGFILE

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

#
## Set OS variable
#
	os=$(uname -s)

#
## Translate site name for building file name
## example - nain=NAN
#
	fileSite=$(grep $site ${CONFIGDIR}/maccs_site_list.txt | awk -F_ '{print $2}')

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

	echo ":$$:maccs_ascii2all:" >> $LOGFILE
	echo ":$$:maccs_ascii2all: processing directory created ....." >> $LOGFILE

#
## Copy over the master CDF
#
	mkdir mastercdf
	cp ${CDFMASTERS}/*${site}* ./mastercdf

#
## Start date loop
#

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

#
## Cleanup
#
	exit 0
