#!/usr/bin/ksh
#
# gen_hirate_asi_display_daily.ksh
#
# This script will generate an ascii file
# showing a summary of the high resolution asi
# files that have been collected.

#
## 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

#
## Set current date variable
#
	currentdate=$(date -u '+%Y%m%d')

#
## Start major loop through dates
#
	rm -f ${LOCAL}/asi_hirate_display.txt
	printf '\n\t\t\t  >>>>> ASI High Rate Data Availability <<<<<\n\t\t\t\t%s\t\n' "`date`" >> ${LOCAL}/asi_hirate_display.txt

	## Start date loop for site
	#
	oldmonth=NaN
	oldDay=NaN
	for date in $(cat ${CONFIGDIR}/date_list.txt)
	do
		## Set a start date - else 20050101 used
		#
		if (( $date < 20060101 ))
		then
			continue
		fi

		## Finish loop when current date reached
		#
		if [[ $date = $currentdate ]]
		then
			break
		fi
	
		## Insert column headers every month
		#
		newmonth=$(echo $date | awk '{print substr($1,5,2)}')
		if [[ $newmonth != $oldmonth ]]
		then
			yyyymm=$(echo $date |awk '{print substr($1,1,4) substr($1,5,2)}')
			oldmonth=$newmonth
		fi

		newDay=$(echo $date | awk '{print substr($1,7,2)}')
		if [[ $newDay != $oldDay ]]
		then
			printf "\n====================================================================================================================================\n" >> ${LOCAL}/asi_hirate_display.txt
			printf "YYYYMMDD-hh|" >> ${LOCAL}/asi_hirate_display.txt

			for site in $(cat ${CONFIGDIR}/lc_asi_site_list.txt)
			do
				printf "%s|" $site >> ${LOCAL}/asi_hirate_display.txt
			done
			printf "\n====================================================================================================================================\n" >> ${LOCAL}/asi_hirate_display.txt
			oldDay=$newDay
		fi
			
		## Now go through each hour and site and 
		## see how many files are available
		#
		for hr in $(cat ${CONFIGDIR}/asi_hour_list.txt)
		do
			printf "%s-%s|" $date $hr >>${LOCAL}/asi_hirate_display.txt
			for site in $(cat ${CONFIGDIR}/lc_asi_site_list.txt)
			do
				pattern=${date}_${hr}
				num_files=$(grep -c $pattern ${FILELISTS}/asi_hirate_list_${site}_${yyyymm}.txt)
				printf "%4s|" $num_files >> ${LOCAL}/asi_hirate_display.txt
			done
		printf "\n------------------------------------------------------------------------------------------------------------------------------------\n" >> ${LOCAL}/asi_hirate_display.txt
		done
	done
		
	mv -f ${LOCAL}/asi_hirate_display.txt ${FILELISTS}/asi_hirate_display.txt

#
## Cleanup
#
	exit 0