#!/usr/bin/ksh
#
# ucla_rmd2all_regen.ksh - modified slightly to send output to my folder for testing DON'T RUN THIS ONE
#
# Run this script manually to regenerate UCLA GMAG data:
#
#	When UCLA Ground Magnetometer (GMAG) raw data (RMD) have been
# rsync-mirrored from the UCLA or UCalgary server, this script 
# will initiate the sequence of file format conversions:
#
#			RMD->FF->Merged_FF->CDF->ASCII
#
#			Author: Tim Quinn
# 		Created: June 2007
#
#			$1 = site
#			$2 = start date (yyyymmdd)
#					 Input zero (0) if start date should be 20040101.
#			$3 = end date (yyyymmdd)
#					 If no date given, current date will be used.
#
#			Updates:
#
## Set the environment variables

  if [[ -z $THMSOC ]]
  then
    THMSOC=/disks/socware/thmsoc_dp_current
    export THMSOC
  fi
#
  . $THMSOC/src/config/ucla_rmd2all.config
  . $THMSOC/src/config/soc_it_to_me.config

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

	loopsite=$1
	startdate=$2
	enddate=$3

## Check for lock file
#
  if [[ -a /var/tmp/ucla_rmd2all_regen_lock ]] ; then
    echo ":ucla_rmd2all_regen:$$:" 
    echo ":ucla_rmd2all_regen:$$: A Mirror Process is currently active " 
    echo ":ucla_rmd2all_regen:$$: Quittin..................." 
    exit 0
  fi
  echo $$ > /var/tmp/ucla_rmd2all_regen_lock

echo ":ucla_rmd2all_regen:$$:ucla_rmd2all:" 
echo ":ucla_rmd2all_regen:$$:ucla_rmd2all:Starting ucla_rmd2all at $(date)" 

## Process any RMD directories that were produced as part of the 
## rsync-mirror process
#
		if [[ $os = "SunOS" ]]
		then
			ucsite=$(echo $loopsite | nawk '{print toupper($1)}')
		else
			ucsite=$(echo $loopsite | awk '{print toupper($1)}')
		fi
		
		chk_site=$(grep $loopsite $CONFIGDIR/lc_site_list_ucla_epo.txt)
		if [[ -n $chk_site ]]
		then
			gbo_or_epo=EPO
		else
			gbo_or_epo=GBO
		fi

		## First: Check if calibration file exists
		#
		if [[ ! -e ${TABLE_FILES_DIR}/${ucsite}.txt ]]
		then
			# Calibration file does not exist. Go to next dir
			echo ":ucla_rmd2all_regen:$$:ucla_rmd2all:" 
			echo ":ucla_rmd2all_regen:$$:ucla_rmd2all:WARNING: Cal file ${loopsite}.txt does not exist." 
			echo ":ucla_rmd2all_regen:$$:ucla_rmd2all:.........trying next directory." 
			continue
		fi
		
		if [[ -z $enddate ]]
		then	
			enddate=$(date -u '+%Y%m%d')
		fi

		for date in $(cat $CONFIGDIR/date_list.txt 2>/dev/null)
		do
			if [[ $date = $enddate ]]
			then
				rm -f /var/tmp/ucla_rmd2all_regen_lock
				exit 0
			fi
			if [[ $date < $startdate ]]
			then
				continue
			fi
			year=$(echo $date | awk '{print substr($1,1,4)}')
			month=$(echo $date | awk '{print substr($1,5,2)}')
			day=$(echo $date | awk '{print substr($1,7,2)}')

			## create a directory name that includes siteyyyymmdd_rmd in 
			## garage subdir of epo ff storage dir
			#
			tmpdir=${loopsite}${date}_rmd
			mkdir -p ${UCB_FLAT_HOME}/process_rmd2ff/${tmpdir}
		
			## Copy all rmd files from RMD dir to temp dir
			#
			if [[ $gbo_or_epo = "EPO" ]]
			then
				sitename=$(grep $loopsite ${EPO_MIRROR_HOME}/site_list.txt |awk -F_ '{print $1}')
				path=${EPO_MIRROR_HOME}/${sitename}/${year}/${month}/${day}
				if [[ -d $path ]]
				then
					cp -r ${path}/*RMD ${UCB_FLAT_HOME}/process_rmd2ff/${tmpdir}
				else
					echo Does Not Exist - $path
					continue
				fi
			else
				path=${GBO_MIRROR_HOME}/${year}/${month}/${day}
				if [[ -d $path ]]
				then	
					cp -r ${path}/${ucsite}*RMD ${UCB_FLAT_HOME}/process_rmd2ff/${tmpdir}
				else
					continue
				fi			
			fi

			## Change directories to temp dir and run rmd2ff on each RMD file
			#
			cd ${UCB_FLAT_HOME}/process_rmd2ff/${tmpdir}
			echo ":ucla_rmd2all_regen:$$:ucla_rmd2all:" 
			echo ":ucla_rmd2all_regen:$$:ucla_rmd2all:	Running rmd2ff on files in $(pwd)......." 
			for file in $(ls *RMD)
			do
				fileroot=$(echo $file | awk -F. '{print $1}')
				site=$(echo $fileroot |awk '{print substr($1,1,4)}')
				rm -f $LOGDIR/rmd2ff_${fileroot}.log
				##${RMD2FFPATH}/RMD2FF -batch -in_file $file -table ${TABLE_FILES_DIR}/${site}.txt -outfile $fileroot >$LOGDIR/rmd2ff_${fileroot}.log
				${RMD2FFPATH}/RMD2FF -ctime -batch -in_file $file -table ${TABLE_FILES_DIR}/${site}.txt -outfile $fileroot 1>/dev/null 2>${LOGDIR}/RMD2FF_BAD$$
				if [[ -s ${LOGDIR}/RMD2FF_BAD$$ ]]
				then
					echo ":ucla_rmd2all_regen:$$:ucla_rmd2all:"
					echo ":ucla_rmd2all_regen:$$:ucla_rmd2all: Problem in RMD2FF run. Check ${LOGDIR}/RMD2FF_BAD$$"
					printf "%s\n" "Subject: THEMIS SOC Processing Problem" \
					"script: $0" \
					"Warning: RMD2FF Problem with $file" |
					mailx -s "THEMIS RMD2FF Processing Message" $SOC_EMAIL
				else
					rm -f ${LOGDIR}/RMD2FF_BAD$$
				fi
			done
			echo ":ucla_rmd2all_regen:$$:ucla_rmd2all:	rmd2ff complete. "

			## Now merge the flat files into one 24 hour file
			#
			for file in $(ls *ffd)
			do
				fileroot=$(echo $file | awk -F. '{print $1}')
				echo $fileroot >> filelist.txt
			done
			newfilename=$(echo $fileroot |awk '{print substr($1,1,10)}')
			echo filelist.txt >> ffmerge.input
			echo "2 3 4" >> ffmerge.input
			echo x >> ffmerge.input
			echo first >> ffmerge.input
			echo last >> ffmerge.input
			echo $newfilename >> ffmerge.input
			echo n >> ffmerge.input
			echo y >> ffmerge.input
			rm -f $LOGDIR/ffmerge_${newfilename}
			echo ":ucla_rmd2all_regen:$$:ucla_rmd2all:" 
			echo ":ucla_rmd2all_regen:$$:ucla_rmd2all:	Running ffmerge on files ......" 
			#${FFMERGEPATH}/ffmergesungnucmp < ffmerge.input > $LOGDIR/ffmerge_${newfilename} 
			${FFMERGEPATH}/ffmergesungnucmp < ffmerge.input 1> /dev/null
			#echo ":$$:ucla_rmd2all:	ffmerge complete. Check log ffmerge_${newfilename}." 
			echo ":ucla_rmd2all_regen:$$:ucla_rmd2all:	ffmerge complete. "
	
			# Now a single flat file exists for that day in the form
			# schlyymmdd.ffd, schlyymmdd.ffh and must be changed to
			# Byydoy_schl
			#
			fullyear=$(echo $newfilename |awk '{print "20" substr($1,5,2)}')
			partyear=$(echo $newfilename |awk '{print  substr($1,5,2)}')
			month=$(echo $newfilename |awk '{print substr($1,7,2)}')
			day=$(echo $newfilename |awk '{print substr($1,9,2)}')
			sitedir=$(echo $site | nawk '{print tolower($1)}')
			doy=$(${KSHSCRIPTS}/date2doy ${fullyear}-${month}-${day} | awk '{printf("%03d",$1)}')
			finalfileroot=B${partyear}${doy}_${site}
			cdftxtgifroot=thg_l2_mag_${sitedir}_${fullyear}${month}${day}_v01
			echo ":ucla_rmd2all_regen:$$:ucla_rmd2all:	New files: ${newfilename}.ffd and ${newfilename}.ffh." 

			#mkdir -p ${UCB_FLAT_HOME}/${sitedir}/${fullyear}
			#mv ${newfilename}.ffd ${UCB_FLAT_HOME}/${sitedir}/${fullyear}/${finalfileroot}.ffd
			#mv ${newfilename}.ffh ${UCB_FLAT_HOME}/${sitedir}/${fullyear}/${finalfileroot}.ffh
			mkdir -p /home/thmsoc/lphilpotts_stuff/${sitedir}/${fulldir}
			mv ${newfilename}.ffd /home/thmsoc/lphilpotts_stuff/${sitedir}/${fullyear}/${finalfileroot}.ffd
			mv ${newfilename}.ffh /home/thmsoc/lphilpotts_stuff/${sitedir}/${fullyear}/${finalfileroot}.ffh
			echo ":ucla_rmd2all_regen:$$:ucla_rmd2all:	New 24 hour flat file = ${finalfileroot}" 

			# TBD: Update database with merged FF stats 
			# TBD: insert php script that was used to update db as part of rsync process

			#/home/thmsoc/software_dev/ksh/ucla_flat2cdf.ksh $finalfileroot $gbo_or_epo 
			# TBD: Update database with merged CDF stats 

			## Make ASCII data
			# TBD: Update database with merged ASCII stats 
			#/home/thmsoc/software_dev/ksh/ucla_flat2ascii.ksh $finalfileroot $gbo_or_epo 

			## Delete temp dir
			#
			cd ${UCB_FLAT_HOME}
			rm -rf ${UCB_FLAT_HOME}/process_rmd2ff/${tmpdir}

		done
		

rm -f /var/tmp/ucla_rmd2all_regen_lock

exit 0