#!/usr/bin/ksh
#
# candat_ascii2all.ksh - convert Canadian ascii mag file to 
# 											cdf, ascii, and png
#
#	Usage: candat_ascii2all.ksh [path/filename] 
#
#	Arguments:
#
#					$1 = File to process in the form yyyy/mm/dd/filename
#
# Creation Date:
#
#								17 November 2006 TimQuinn
#
# Updates:
#         11/29/06  Added line to put cdf date in file for stack plot
#                   creation process to use


## Set 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/ucla_rmd2all.config
	. /home/thmsoc/astraea2_test/ucla_rmd2all_test.config
#	. $THMSOC/src/config/candat_ascii2all.config
	. /home/thmsoc/astraea2_test/candat_ascii2all_test.config


## Set local variables
#
  os=$(uname -s)

## Draw out information from input variable
#
	inputdata=$1
	filename=$(basename $inputdata)
	ucschool=$(echo $filename | awk '{print substr($1,9,4)}')
	if [[ $os = "SunOS" ]]
	then
  	lcschool=$(echo $ucschool | nawk '{print tolower($1)}')
	else
  	lcschool=$(echo $ucschool | awk '{print tolower($1)}')
	fi

echo ":$$:candat_ascii2all:" >> $LOGFILE
echo ":$$:candat_ascii2all:Starting candat_ascii2all at $(date)" >> $LOGFILE

echo ":$$:candat_ascii2all:" >> $LOGFILE
echo ":$$:candat_ascii2all:Processing netCDFs for $fileroot:" >> $LOGFILE

## Create a directory where processing will occur
#
dateSubDir=tmp_${filename}
processDir=${L2MAGDIR}/garage/${dateSubDir}
mkdir -p $processDir
cd $processDir

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

## Copy desired Canadian Mag ASCII file into processing directory
## and unzip
#
cp ${UCB_MIRROR_HOME}/${inputdata} .
gunzip -f $filename
unzippedfile=${filename%.gz}

echo ":$$:candat_ascii2all:" >> $LOGFILE
echo ":$$:candat_ascii2all: copied canmag file to directory ..... " >> $LOGFILE

## Copy CDF master and dat file to processing directory
#
mkdir -p mastercdfs
cp ${CDFMASTERS}/*${lcschool}* ./mastercdfs
cp ${CONFIGDIR}/*.dat .

## Build and run IDL batch file
#
cp ${THMSOC}/idl/thmsoc/ualberta_ascii/make_canadian_gbo_cdf2.bm .
line="make_canadian_gbo_cdf2,'$unzippedfile'"
echo $line >> make_canadian_gbo_cdf2.bm
echo exit >> make_canadian_gbo_cdf2.bm
idl make_canadian_gbo_cdf2.bm > ${IDLOUT}_$$ 2> /dev/null
processTime=$(date -u '+%Y-%m-%d %T')

if (( $? != 0 ))
then
  echo ":$$:candat_ascii2all:" >> $LOGFILE
  echo ":$$:candat_ascii2all: ERROR in IDL Process" >> $LOGFILE
	printf "%s\n" "Subject: THEMIS SOC Processing Message" \
	"script : $0" \
	"Warning: idl ${processDir}/make_canadian_gbo_cdf.bm finished with an error." |
	mail $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 :$$:candat_ascii2all: >> $LOGFILE
	echo :$$:candat_ascii2all: ERROR: The IDL run failed to produce both the cdf and txt!! >> $LOGFILE
	printf "%s\n" "Subject: THEMIS SOC Processing Message" \
	"script : $0" \
	"Warning: idl ${processDir}/make_canadian_gbo_cdf2.bm did not produce txt and cdf." |
	mail $SOC_EMAIL
	rm -rf $processDir
	rm -f /var/tmp/candat_ascii2all_lock
	exit 1
fi

echo ":$$:candat_ascii2all:" >> $LOGFILE
echo ":$$:candat_ascii2all: final IDL finished. Files created: " >> $LOGFILE
echo ":$$:candat_ascii2all:  cdffile = $cdffile " >> $LOGFILE
echo ":$$:candat_ascii2all:  txtfile = $txtfile " >> $LOGFILE

#
## 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 ":$$:candat_ascii2all:" >> $LOGFILE
	echo ":$$:candat_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

#
## Cleanup and exit
#
	cd
	rm -rf $processDir

	echo ":$$:candat_ascii2all:" >> $LOGFILE
	echo ":$$:candat_ascii2all:Ending candat_ascii2all at $(date)" >> $LOGFILE

	exit 0
