#!/usr/bin/ksh
#
# gen_probe_l0_lists.ksh
#
# This script will generate a file (per probe) 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
  
#
## Cycle through probe names
#
	for probe in $(cat ${CONFIGDIR}/probe_list.txt)
	do
	
	## Start creating new file list
	#
		rm -f ${LOCAL}/probe_${probe}_l0_availability.txt
		printf '\n\t\t\t\t\t\t  >>>>> Probe ${probe} Level 0 Data Availability <<<<<\n\t\t\t\t\t\t\t%s\n\n' "`date`" >> ${LOCAL}/probe_${probe}_l0_availability.txt

	## Process file list
	#
		currentdate=$(date -u '+%Y%m%d')
		oldmonth=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_${probe}_l0_availability.txt ${FILELISTS}/probe_${probe}_l0_availability.txt
				break	
			fi

			newmonth=$(echo $date | awk '{print substr($1,5,2)}')
			if [[ $newmonth != $oldmonth ]]
			then
				printf "\n=========================================================================================================================================================================================\n" >> ${LOCAL}/probe_${probe}_l0_availability.txt
				printf "Date    |PID|" >> ${LOCAL}/probe_${probe}_l0_availability.txt
				for apid in $(cat ${CONFIGDIR}/probe_apid_list.txt)
				do
					printf "%s|" $apid >> ${LOCAL}/probe_${probe}_l0_availability.txt
				done
				printf "\n=========================================================================================================================================================================================\n" >> ${LOCAL}/probe_${probe}_l0_availability.txt
				oldmonth=$newmonth
			fi
			printf "%s|%s|" $date $probe>> ${LOCAL}/probe_${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_${probe}_l0_availability.txt
			done
			printf "\n-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n" >> ${LOCAL}/probe_${probe}_l0_availability.txt
		done

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

	done

#
## Cleanup				
#
	exit 0
