#!/usr/bin/ksh
#
# gen_summaryplot_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/gaia2_test/soc_it_to_me.config

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

#
## Start file creation
#
	rm -f ${LOCAL}/summaryplot_display.txt
	printf '\n\t  >>>>> Summary Plot Inventory<<<<<\n\t\t%s\n\n' "`date`" >> ${LOCAL}/summaryplot_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
			printf "\n=================================================================================================================================================\n" >> ${LOCAL}/summaryplot_display.txt
			printf "YYYYMMDD|PID|" >> ${LOCAL}/summaryplot_display.txt
			for dtype in $(cat ${CONFIGDIR}/all_summary_list.txt)
			do
				printf "%-10s|" $dtype >> ${LOCAL}/summaryplot_display.txt
			done
			printf "\n=================================================================================================================================================\n" >> ${LOCAL}/summaryplot_display.txt
			oldmonth=$newmonth
		fi
				
		#
		## Now go through each date and datatype and 
		## see how many files are available
		#
		for probeid in $(cat ${CONFIGDIR}/probe_id_list.txt)
		do
			printf "%s|%s|" $date $probeid >>${LOCAL}/summaryplot_display.txt

			for dtype in $(cat ${CONFIGDIR}/all_summary_list.txt)
			do
				if [[ $dtype != "gmoms" && $dtype != "moms" && $dtype != "overview" ]]
				then
					printf "----------|" >> ${LOCAL}/summaryplot_display.txt
				else
					num_files=$(grep -c $date ${FILELISTS}/summarylist_${probeid}_${dtype}.txt)
					printf "%10s|" $num_files >> ${LOCAL}/summaryplot_display.txt
				fi
			done

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

		printf "%s|thm|" $date >>${LOCAL}/summaryplot_display.txt

		for dtype in $(cat ${CONFIGDIR}/all_summary_list.txt)
		do
			if [[ $dtype = "gmoms" || $dtype = "moms" || $dtype = "overview" ]]
			then
				printf "----------|" >> ${LOCAL}/summaryplot_display.txt
			elif [[ $dtype = "survey" || $dtype = "burst" ]]
			then
				num_files=$(grep -c $date2 ${FILELISTS}/summarylist_thm_${dtype}.txt)
				printf "%10s|" $num_files >> ${LOCAL}/summaryplot_display.txt
			else
				num_files=$(grep -c $date ${FILELISTS}/summarylist_thm_${dtype}.txt)
				printf "%10s|" $num_files >> ${LOCAL}/summaryplot_display.txt
			fi
		done

		printf "\n-------------------------------------------------------------------------------------------------------------------------------------------------\n" >> ${LOCAL}/summaryplot_display.txt
					
	done	#end date loop
		
#
## Rename temporary file
#
	mv -f ${LOCAL}/summaryplot_display.txt ${FILELISTS}/summaryplot_display.txt

#
## Cleanup
#
	exit 0
