#!/usr/bin/ksh
#
# ucla_flat2cdf_test.ksh - convert UCLA flat files to CDF
# format 
#
#	Usage: ucla_flat2cdf.ksh [filename] 
#
#	Arguments:
#						$1 = filename (extensions have to be added)
#						$2 = EPO/GBO
#
# Creation Date:
#
#								09 August 2006 TimQuinn
#
#	Updates:
#
#					10/09/06	updated to all new IDL procedures
#					10/23/06  new path for archiving cdf:
#										{root}/thg/[l0|l1|l2]/mag/yyyy/mm/dd
#         11/29/06  Added line to put cdf date in file for stack plot
#                   creation process to use
#	

## Set the environment variables
#
if [[ -z $THMSOC ]]
  then
    THMSOC=/home/thmsoc/clrussells_stuff/vldr_test
    export THMSOC
  fi
	. $THMSOC/config/setup_themis_UCLA.bourne
	. $THMSOC/config/ucla_rmd2all_vldr_test.config
	. $THMSOC/config/soc_it_to_me_vldr_test.config

## Create variables derived from the input arguments
#
	os=$(uname -s)
	fileroot=$1
	year=$(echo $fileroot |awk '{print "20" substr($1,2,2)}')
	ucschool=$(echo $fileroot | awk '{print substr($1,8,4)}')
echo $fileroot
echo $year
echo $ucschool
	if [[ $os = "SunOS" ]]
	then
		lcschool=$(echo $ucschool | nawk '{print tolower($1)}')
		grdnet=$(echo $2 | nawk '{print tolower($1)}')
	else
		lcschool=$(echo $ucschool | awk '{print tolower($1)}')
		grdnet=$(echo $2 | awk '{print tolower($1)}')
	fi
	yydoy=$(echo $fileroot | awk '{print substr($1,2,5)}')
echo $yydoy
## Where is the flat file located
#
	schdir=/home/thmsoc/clrussells_stuff/vldr_test/data/thg/l1/mag/${lcschool}/${year}
	echo :$$:ucla_flat2cdf: 
	echo :$$:ucla_flat2cdf: Using Flat File:
	echo :$$:ucla_flat2cdf: ${UCB_FLAT_HOME}/${lcschool}/${year}/$fileroot 
	
## Create a directory where processing will occur
#
	processDir=${L2MAGDIR}/garage/tmp_${yydoy}
	mkdir -p $processDir

## Now copy all of the necessary files to the processing directory
#
echo ${processDir}
echo ${fileroot}
	cp ${schdir}/${fileroot}.ffd ${processDir}/${fileroot}.DAT
	cp ${schdir}/${fileroot}.ffh ${processDir}/${fileroot}.ABS
	cp $/disks/socware/thmsoc_dp_current/src/config/default.DES ${processDir}/${fileroot}.DES

## Create HED file used by IDL routine
#
	HEDstring=0123456789012345678901234567890123456789012345678901234567890123456789012345678901
	HEDint1=4
	filesize=$(ls -l $processDir/${fileroot}.DAT |awk '{print $5}')
	HEDint2=$(( $filesize/20 ))
	printf "%82s%10i%10i" $HEDstring $HEDint1 $HEDint2 > ${processDir}/${fileroot}.HED

## Build IDL batch file
#
	#cp $THMSOC/idl/thmsoc/ucla_ff/uff2cdf.bm $processDir
	cp /disks/socware/thmsoc_dp_current/idl/thmsoc/ucla_ff/uff2cdf.bm $processDir
	line="uff2cdf,'$ucschool','$yydoy'"
	echo $line >> ${processDir}/uff2cdf.bm
	echo exit >> ${processDir}/uff2cdf.bm
echo $line
## Now run idl with batch file
#
	echo :$$:ucla_flat2cdf: 
	echo :$$:ucla_flat2cdf: Starting FLAT to CDF conversion for $fileroot at $(date)
	cd ${L2MAGDIR}/garage
echo $L2MAGDIR
	mkdir mastercdfs
	cp ${CDFMASTERS}/thg_l2_mag_${lcschool}* mastercdfs
echo $CDFMASTERS
temp = 'entering idl'
echo $temp
	#idl ${processDir}/uff2cdf.bm 1>> $LOGDIR/IDLOUT_FF2CDF_${fileroot} 2>/dev/null
	idl ${processDir}/uff2cdf.bm 
	if (( $? != 0 ))
	then
		echo :$$:ucla_flat2cdf: ERROR in IDL Process
		echo :$$:ucla_flat2cdf: Notifying OPS Personnel
	fi
	processTime=$(date -u '+%Y-%m-%d %T')
temp = "done with idl2cdf"
echo $temp
## Create subdirectory for archiving the cdf file
#
	year=$(echo $yydoy | awk '{print "20"substr($1,1,2)}')
	doy=$(echo $yydoy | awk '{print substr($1,3,3)}')
	date=$(/home/thmsoc/software_dev/ksh/doy2date $year $doy)
	month=$(echo $date |awk -F/ '{printf("%02d",$2)}')
	day=$(echo $date |awk -F/ '{printf("%02d",$3)}')
	#archive_dir=${L2MAGDIR}/${lcschool}/${year}
	archive_dir=/home/thmsoc/clrussells_stuff/cdfdir/${lcschool}/${year}
	mkdir -p $archive_dir

## Create cdf file name and move to archive directory
#
	idlcdffile=thg_l2_mag_${lcschool}_${year}${month}${day}_v01.cdf

	echo :$$:ucla_flat2cdf: cdf file $idlcdffile created at $(date)
	echo :$$:ucla_flat2cdf: moved file to $archive_dir

	mv ${L2MAGDIR}/garage/${idlcdffile} $archive_dir
	rm -rf $processDir
	rm -rf ${L2MAGDIR}/garage/mastercdfs

## Update checksum file
#
	${KSH_SCRIPTS}/fileChkSum.ksh ${archive_dir}/${idlcdffile}

## Create file for stack plot production
#
#	echo ${year}-${month}-${day} > ${WORKMAGSTACKS}/stack_${year}${month}${day}_$$

## Process information into database
#
#	mysql_table_name=${grdnet}_${ucschool}_cdffiles
#	dataTime=$(echo $year $month $day | awk '{printf("%s-%02s-%02s 00:00:00",$1,$2,$3)}')
#	fileSize=$(ls -l ${archive_dir}/${idlcdffile} |awk '{print $5}')
#	echo :$$:ucla_flat2cdf:
#	echo :$$:ucla_flat2cdf: gmag_cdf.php $mysql_table_name $dataTime $idlcdffile $processTime $fileSize
#	gmag_cdf.php $mysql_table_name $dataTime $idlcdffile $processTime $fileSize


exit 0