#!/usr/bin/ksh
#
# gen_asi_site_summary_display.ksh
#
# This script will generate an ascii display
# file which lists what summary plots have 
# been made

#
## 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 file creation
#
	rm -f ${LOCAL}/asi_site_summary_display.txt
	printf '\n\t  >>>>> ASI Site Summary Plot Inventory<<<<<\n\t\t%s\n\n' "`date`" >> ${LOCAL}/asi_site_summary_display.txt
	
	#
	## Start date loop 
	#
	oldmonth=NaN
	for date in $(cat ${CONFIGDIR}/date_list.txt)
	do
		## Set a start date - else 20050101 used
		#
		if (( $date < 20070215 ))
		then
			continue
		fi

		#
		## Finish loop when current date reached
		#
		if [[ $date = $currentdate ]]
		then
			break
		fi

		#
		## Create alternate date format - yyyy-mm-dd
		#
		date2=$(echo $date |awk '{print substr($1,1,4) "." substr($1,5,2) "." substr($1,7,2)}')

		#
		## 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)}')
			printf "\n=========================================================================================================================================================================================================================\n" >> ${LOCAL}/asi_site_summary_display.txt
			printf "YYYYMMDD|" >> ${LOCAL}/asi_site_summary_display.txt
			for site in $(cat ${CONFIGDIR}/uc_asi_site_list.txt)
			do
				printf "%-7s|" $site >> ${LOCAL}/asi_site_summary_display.txt
			done
			printf "\n=========================================================================================================================================================================================================================\n" >> ${LOCAL}/asi_site_summary_display.txt
			oldmonth=$newmonth
		fi
				
		#
		## Now go through each date and datatype and 
		## see how many files are available
		#
		printf "%s|" $date >>${LOCAL}/asi_site_summary_display.txt
		for site in $(cat ${CONFIGDIR}/uc_asi_site_list.txt)
		do
			num_files=$(grep -c $date2 ${FILELISTS}/asi_summary_list_${site}_${yyyymm}.txt)
			printf "%7s|" $num_files >> ${LOCAL}/asi_site_summary_display.txt
		done

			printf "\n-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n" >> ${LOCAL}/asi_site_summary_display.txt

	done	#end date loop
		
#
## Rename temporary file
#
	mv -f ${LOCAL}/asi_site_summary_display.txt ${FILELISTS}/asi_site_summary_display.txt

#
## Cleanup
#
	exit 0