#!/usr/bin/ksh
#
# gen_gmag_cdf_list.ksh
#
# This script will generate a file listing what gmag
# 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
  . /home/thmsoc/astraea2_test/soc_it_to_me.config
  #. ${THMSOC}/src/config/ground_mag.config
  . /home/thmsoc/astraea2_test/ground_mag_test.config

#
## Start creation of new listing
#
	printf '\n\t\t\t\t\t\t  >>>>> GMAG CDF Data Availability <<<<<\n\t\t\t\t\t\t\t%s\n\n' "`date`" >> ${LOCAL}/GMAG_CDF_LIST_TMP.txt

## Process sites
#
	currentdate=$(date -u '+%Y%m%d')
	oldmonth=NaN
	for date in $(cat ${CONFIGDIR}/date_list.txt)
	do
		if [[ $date = $currentdate ]]
		then
			## Rename temporary file listing
			#
			mv -f ${LOCAL}/GMAG_CDF_LIST_TMP.txt ${FILELISTS}/gmag_cdf_list.txt
			exit 0
		fi
		newmonth=$(echo $date | awk '{print substr($1,5,2)}')
		if [[ $newmonth != $oldmonth ]]
		then
			printf "\n========================================================================================================================================================================================================================================================================================\n" >> ${LOCAL}/GMAG_CDF_LIST_TMP.txt
			printf "Date    |" >> ${LOCAL}/GMAG_CDF_LIST_TMP.txt
			for site in $(cat ${CONFIGDIR}/lc_site_list.txt)
			do
				printf "%s|" $site >> ${LOCAL}/GMAG_CDF_LIST_TMP.txt
			done
			printf "\n========================================================================================================================================================================================================================================================================================\n" >> ${LOCAL}/GMAG_CDF_LIST_TMP.txt
			oldmonth=$newmonth
		fi
		printf "%s|" $date >> ${LOCAL}/GMAG_CDF_LIST_TMP.txt
		for site in $(cat ${CONFIGDIR}/lc_site_list.txt)
		do
			check=$(grep $date ${FILELISTS}/gmag_cdf_list_${site}.txt)
			if [[ -z $check ]]
			then
				check=
			else
				check=X
			fi
			printf "%4s|" $check >> ${LOCAL}/GMAG_CDF_LIST_TMP.txt
		done
			printf "\n---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n" >> ${LOCAL}/GMAG_CDF_LIST_TMP.txt
	done

#
## Rename temporary file listing
#
	mv -f ${LOCAL}/GMAG_CDF_LIST_TMP.txt ${FILELISTS}/gmag_cdf_list.txt

#
## Cleanup
#
	exit 0
