#!/usr/bin/ksh
#
# gen_probe_l1_lists.ksh
#
# This script will generate a file (per probe) listing what l1
# 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/gaia2_test/soc_it_to_me.config
  
#
## Start creating file list
#
	for probe in $(cat ${CONFIGDIR}/probe_list.txt)
	do
		rm -f ${LOCAL}/probe_${probe}_l1_availability.txt
		printf '\n\t\t\t\t\t\t  >>>>> Probe Level 1 Data Availability <<<<<\n\t\t\t\t\t\t\t%s\n\n' "`date`" >> ${LOCAL}/probe_${probe}_l1_availability.txt

		## Process sites
		#
		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
				#
				mv -f ${LOCAL}/probe_${probe}_l1_availability.txt ${FILELISTS}/probe_${probe}_l1_availability.txt
				break
			fi

			newmonth=$(echo $date | awk '{print substr($1,5,2)}')
			if [[ $newmonth != $oldmonth ]]
			then
				printf "\n=================================================================================================================================================================================================================\n" >> ${LOCAL}/probe_${probe}_l1_availability.txt
				printf "Date    |PID|" >> ${LOCAL}/probe_${probe}_l1_availability.txt
				for dtype in $(cat ${CONFIGDIR}/probe_l1_list.txt)
				do
					printf "%-6s|" $dtype >> ${LOCAL}/probe_${probe}_l1_availability.txt
				done
				printf "\n=================================================================================================================================================================================================================\n" >> ${LOCAL}/probe_${probe}_l1_availability.txt
				oldmonth=$newmonth
			fi
			printf "%s|%s|" $date $probe>> ${LOCAL}/probe_${probe}_l1_availability.txt
			for dtype in $(cat ${CONFIGDIR}/probe_l1_list.txt)
			do
				check=$(grep $date ${FILELISTS}/${probe}_l1_list_${dtype}.txt)
				if [[ -z $check ]]
				then
					check=
				else
					check=X
				fi
				printf "%6s|" $check >> ${LOCAL}/probe_${probe}_l1_availability.txt
			done
			printf "\n-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n" >> ${LOCAL}/probe_${probe}_l1_availability.txt
		done

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

	done

#
## Cleanup
#				
	exit 0
