#!/usr/bin/ksh
#
# gen_lowrate_asi_display_summary.ksh
#
# This script will generate display files
# for each GBO/ASI, listing a summary of 
# how many files were collected each day for
# each type.


#
## 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
#  . ${THMSOC}/src/config/ground_asi.config

. /home/thmsoc/astraea2_test/soc_it_to_me.config
. /home/thmsoc/astraea2_test/ground_asi.config


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

#
## Start looping through GBO sites
#
	for site in $(cat ${CONFIGDIR}/lc_asi_site_list.txt)
	do
		rm -f ${LOCAL}/asi_lowrate_display_${site}_sum.txt
		printf '\n\t\t  >>>>> ASI Low Rate Data Availability for %s <<<<<\n\t\t\t\t%s\n\n' "$site" "`date`" >> ${LOCAL}/asi_lowrate_display_${site}_sum.txt
	
		#
		## Start date loop for site
		#
		oldmonth=NaN
		for date in $(cat ${CONFIGDIR}/date_list.txt)
		do

			## Set a start date - else 20050101 used
			#
			if (( $date < 20070101 ))
			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)}')
				printf "\n============================================================================================================================\n" >> ${LOCAL}/asi_lowrate_display_${site}_sum.txt
				printf "YYYYMMDD|" >> ${LOCAL}/asi_lowrate_display_${site}_sum.txt
				for dtype in $(cat ${CONFIGDIR}/asi_dtype_list.txt)
				do
					printf "%-20s|" $dtype >> ${LOCAL}/asi_lowrate_display_${site}_sum.txt
				done
				printf "\n============================================================================================================================\n" >> ${LOCAL}/asi_lowrate_display_${site}_sum.txt
				oldmonth=$newmonth
			fi
				
			#
			## Now go through each date and datatype and 
			## see how many files are available
			#
			printf "%s|" $date >>${LOCAL}/asi_lowrate_display_${site}_sum.txt
			for dtype in $(cat ${CONFIGDIR}/asi_dtype_list.txt)
			do
				num_files=$(grep -c $date ${FILELISTS}/asi_lowrate_list_${site}_${dtype}_${yyyymm}.txt)
				printf "%20s|" $num_files >> ${LOCAL}/asi_lowrate_display_${site}_sum.txt
			done
			printf "\n----------------------------------------------------------------------------------------------------------------------------\n" >> ${LOCAL}/asi_lowrate_display_${site}_sum.txt
		done
		
		#
		## Rename temporary file
		#
		mv -f ${LOCAL}/asi_lowrate_display_${site}_sum.txt ${FILELISTS}/asi_lowrate_display_${site}_sum.txt

	done

#
## Cleanup
#

exit 0
