#!/usr/bin/ksh
#
# gen_asf_inventory_display.ksh
#
# This script will generate an ascii file
# showing a summary of the asf CDF files
# that have been produced.

#
## 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
#. /home/thmsoc/astraea2_test/ground_asi.config


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

#
## Start major loop through dates
#
	rm -f ${LOCAL}/asf_l1_cdf_list.txt
	printf '\n\t\t\t  >>>>> ASF CDF Availability <<<<<\n\t\t\t\t%s\t\n' "`date`" >> ${LOCAL}/asf_l1_cdf_list.txt

	## Start date loop for site
	#
	oldDay=NaN
	oldmonth=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
		#
		newDay=$(echo $date | awk '{print substr($1,7,2)}')
		if [[ $newDay != $oldDay ]]
		then
			printf "\n====================================================================================================================================\n" >> ${LOCAL}/asf_l1_cdf_list.txt
			printf "YYYYMMDD-hh|" >> ${LOCAL}/asf_l1_cdf_list.txt

			for site in $(cat ${CONFIGDIR}/lc_asi_site_list.txt)
			do
				printf "%s|" $site >> ${LOCAL}/asf_l1_cdf_list.txt
			done
			printf "\n====================================================================================================================================\n" >> ${LOCAL}/asf_l1_cdf_list.txt
			oldDay=$newDay
		fi
			
		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

		## 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}/asf_l1_cdf_list.txt
			for site in $(cat ${CONFIGDIR}/lc_asi_site_list.txt)
			do
				pattern=${date}${hr}
				check=$(grep $pattern ${FILELISTS}/asf_l1_cdf_list_${site}_${yyyymm}.txt)
				if [[ -z $check ]]
				then
					check=
				else
					check=X
				fi
				printf "%4s|" $check >> ${LOCAL}/asf_l1_cdf_list.txt
			done
		printf "\n------------------------------------------------------------------------------------------------------------------------------------\n" >> ${LOCAL}/asf_l1_cdf_list.txt
		done
	done
		
	mv -f ${LOCAL}/asf_l1_cdf_list.txt ${FILELISTS}

#
## Cleanup
#
	exit 0
