#!/usr/bin/ksh
#
# gima_netcdf2all.ksh - convert UAlaska netCDF mag file to 
# 											cdf, txt, and png/gif
#
#	Usage: gima_netcdf2all.ksh [year] [doy] [station]
#
#	Arguments:
#						$1 = year
#						$2 = doy
#						$3 = station 
#
# Creation Date:
#
#								26 September 2006 TimQuinn
#
#	Updates:
#					11/29/2006 - 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/gima_netcdf2all.config
	. /home/thmsoc/astraea2_test/gima_netcdf2all_test.config

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

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

#
## Assign input to variable names
#
	year=$1
	doy=$(echo $2 | awk  '{printf("%03d",$1)}')
	station=$3
	fileroot=${year}${station}${doy}

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

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

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

#
## Move to processing directory
#
	cd $processDir
	rm -f *.nc
	rm -f *.cdf

#
## Copy desired netCDFs into processing directory
#
	cp ${UCB_MIRROR_HOME}/${station}/${year}/${fileroot}*.nc .

	echo ":$$:gima_netcdf2all:" >> $LOGFILE
	echo ":$$:gima_netcdf2all: copyied nc files to directory ..... " >> $LOGFILE

#
## The conversion program requires a mapping file. Copy
## the latest version to the processing directory
#
	cp ${THMSOC}/src/netcdf2cdf/netcdf_to_cdf_mapping.dat .

#
## Convert the 1 hour netCDFs to CDF format
## (Commented out on 20090306 - Jim McTiernan updated IDL
## to read hourly netcdf directly)
#
	##for name in $(ls *.nc)
	##do
		##${THMSOC}/src/netcdf2cdf/netCDF-to-cdf $name 1>/dev/null 2>&1
	##done

	##echo ":$$:gima_netcdf2all:" >> $LOGFILE
	##echo ":$$:gima_netcdf2all: created 1 hour CDFs ..... " >> $LOGFILE

#
## Remove the netCDFs from processing directory
#
	##rm $processDir/*.nc

#
## Copy master CDF file into process directory
#
	site_desc=$(grep $station $THMSOC/src/config/gima_network.txt |awk -F_ '{print $1}')
	cp $THMSOC/src/mastercdfs/thg/*${site_desc}* .	

#
## Build idl batch file
#
	cp ${THMSOC}/idl/thmsoc/ualaska_netcdf/netcdf2cdf_gmag.bm $processDir
	line="netcdf2cdf_gmag,'$station','$site_desc','$year','$doy'"
	echo $line >> ${processDir}/netcdf2cdf_gmag.bm
	echo exit >> ${processDir}/netcdf2cdf_gmag.bm

#
## Run IDL batch file
#
	idl ${processDir}/netcdf2cdf_gmag.bm > ${GIMALOGDIR}/idl_netcdf2cdf_log$$ 2> /dev/null
	if (( $? != 0 ))
	then
  	echo :$$:gbo_ualaska_nc2cdf: 
  	echo :$$:gbo_ualaska_nc2cdf: ERROR in IDL Process
		printf "%s\n" "Subject: THEMIS SOC Processing Message" \
		"script : $0" \
		"Warning: idl ${processDir}/netcdf2cdf_gmag.bm finished with an error." |
		mail $SOC_EMAIL
	fi
	processTime=$(date -u '+%Y-%m-%d %T')

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

	if [[ -z $cdffile || -z $txtfile ]]
	then
		echo :$$:gbo_ualaska_nc2cdf: >> $LOGFILE
		echo :$$:gbo_ualaska_nc2cdf: 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}/netcdf2cdf_gmag.bm did not produce txt and cdf." |
		mail $SOC_EMAIL
		rm -rf $processDir
		exit 1
	fi

	echo ":$$:gima_netcdf2all:" >> $LOGFILE
	echo ":$$:gima_netcdf2all: final IDL finished. Files created: " >> $LOGFILE
	echo ":$$:gima_netcdf2all:  cdffile = $cdffile " >> $LOGFILE
	echo ":$$:gima_netcdf2all:  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}


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

#
## 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
#
rm -rf $processDir

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

exit 0
