#!/usr/bin/ksh
#
# gen_ast_l1_cdf_list.ksh
#
# This script will generate a file listing what ast
# cdfs 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
#  . ${THMSOC}/src/config/ground_asi.config

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


printf '\n\t\t\t\t\t\t  >>>>> AST L1 CDF Data Availability <<<<<\n\t\t\t\t\t\t\t%s\n\n' "`date`" >> ${LOCAL}/ast_l1_cdf_list.txt

## Process sites
#
	currentdate=$(date -u '+%Y%m%d')
	oldmonth=NaN
	for date in $(cat ${CONFIGDIR}/date_list.txt)
	do
		if [[ $date = $currentdate ]]
		then
			break
		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)}')
			printf "\n=================================================================================================================================\n" >> ${LOCAL}/ast_l1_cdf_list.txt
			printf "Date    |" >> ${LOCAL}/ast_l1_cdf_list.txt
			for site in $(cat ${CONFIGDIR}/lc_asi_site_list.txt)
			do
				printf "%s|" $site >> ${LOCAL}/ast_l1_cdf_list.txt
			done
			printf "\n=================================================================================================================================\n" >> ${LOCAL}/ast_l1_cdf_list.txt
			oldmonth=$newmonth
		fi
		printf "%s|" $date >> ${LOCAL}/ast_l1_cdf_list.txt
		for site in $(cat ${CONFIGDIR}/lc_asi_site_list.txt)
		do
			check=$(grep $date ${FILELISTS}/ast_l1_cdf_list_${site}_${yyyymm}.txt)
			if [[ -z $check ]]
			then
				check=
			else
				check=X
			fi
			printf "%4s|" $check >> ${LOCAL}/ast_l1_cdf_list.txt
		done
		printf "\n---------------------------------------------------------------------------------------------------------------------------------\n" >> ${LOCAL}/ast_l1_cdf_list.txt
	done

## Move file to permanent location
#
	mv -f ${LOCAL}/ast_l1_cdf_list.txt ${FILELISTS}

## Cleanup
#
	exit 0
