#!/usr/bin/ksh
#
# gen_probe_l0_list.ksh
#
# This script will generate a file listing what l0
# packet files 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/gaia2_test/soc_it_to_me.config

#
## Start creating new file list
#
	rm -f ${LOCAL}/probe_l0_availability.txt
	printf '\n\t\t\t\t\t\t  >>>>> Probe Level 0 Data Availability <<<<<\n\t\t\t\t\t\t\t%s\n\n' "`date`" >> ${LOCAL}/probe_l0_availability.txt

## Process sites
#
	currentdate=$(date -u '+%Y%m%d')
	oldday=NaN
	for date in $(cat ${CONFIGDIR}/date_list.txt)
	do
		if (( $date < 20070201 ))
		then
			continue
		fi

		if [[ $date = $currentdate ]]
		then
			## Rename temporary file list
			#
			mv -f ${LOCAL}/probe_l0_availability.txt ${FILELISTS}/probe_l0_availability.txt
			exit 0
		fi

		newday=$(echo $date | awk '{print substr($1,7,2)}')
		if [[ $newday != $oldday ]]
		then
			printf "\n=========================================================================================================================================================================================\n" >> ${LOCAL}/probe_l0_availability.txt
			printf "Date    |PID|" >> ${LOCAL}/probe_l0_availability.txt
			for apid in $(cat ${CONFIGDIR}/probe_apid_list.txt)
			do
				printf "%s|" $apid >> ${LOCAL}/probe_l0_availability.txt
			done
			printf "\n=========================================================================================================================================================================================\n" >> ${LOCAL}/probe_l0_availability.txt
			oldday=$newday
		fi
		for probe in $(cat ${CONFIGDIR}/probe_list.txt)
		do
			printf "%s|%s|" $date $probe>> ${LOCAL}/probe_l0_availability.txt
			for apid in $(cat ${CONFIGDIR}/probe_apid_list.txt)
			do
				check=$(grep $date ${FILELISTS}/${probe}_l0_list_${apid}.txt)
				if [[ -z $check ]]
				then
					check=
				else
					check=X
				fi
				printf "%3s|" $check >> ${LOCAL}/probe_l0_availability.txt
			done
			printf "\n-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n" >> ${LOCAL}/probe_l0_availability.txt
		done
	done

#
## Rename temporary file list
#
	mv -f ${LOCAL}/probe_l0_availability.txt ${FILELISTS}/probe_l0_availability.txt

#
## Cleanup				
#
	exit 0
