#!/usr/bin/ksh
#
## make_hi_asi_file_list.ksh 
##
## Make lists of high rate asi files in the archive
## directories. The ASI files are of the type:
##
##    [yyyymmdd]_[hhmm]_[site]_themis[nn]_full.pgm.gz
##    example: 20081201_0659_kian_themis22_full.pgm.gz
##
## These files are archived at:
##
## /disks/themisdata/thg/mirrors/asi/hi_res_copy/[yyyy]/[mm]
##
## Inputs:
## $1 = yyyy (4 digit year)
## $2 = mm (2 digit month)
## $3 = site (atha,whit,etc.)
##
#

#
## Set the environment variables
#
	if [[ -z $THMSOC ]]
	then
		THMSOC=/disks/socware/thmsoc_dp_current
		export THMSOC
	fi

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


#	. ${THMSOC}/src/config/soc_it_to_me.config
#  . ${THMSOC}/src/config/ground_asi.config

#
## Check if arguments passed
#
  thisYear=$1
  thisMonth=$2
  oneSite=$3
  if [[ -z $thisYear ]]
  then
    thisYear=$(date -u '+%Y')
    thisMonth=$(date -u '+%m')
  fi

  if [[ -z $thisMonth ]]
  then
    echo Please pas 2 digit month also
    exit 1
  fi
  integer ithisMonth=$thisMonth
  integer ilastMonth=$(( ithisMonth - 1 ))

	if (( $ilastMonth <= 0 ))
	then
		lastMonth=12
		lastYear=$(( $thisYear - 1 ))
	else
		lastYear=$thisYear
		lastMonth=$(echo $ilastMonth | awk '{printf("%02d",$1)}')
	fi

	thisYearMonth=${thisYear}${thisMonth}
	lastYearMonth=${lastYear}${lastMonth}


#
## Generate new site files
#
	for yearMonth in $thisYearMonth $lastYearMonth
	do
		year=$(echo $yearMonth | awk '{print substr($1,1,4)}')
		month=$(echo $yearMonth | awk '{print substr($1,5,2)}')
		if [[ -z $oneSite ]]
		then
			for site in $(cat ${CONFIGDIR}/lc_asi_site_list.txt)
			do
				find ${HIRES_COPY}/${year}/${month}/*/${site}_themis??/ -name \*_${site}_\*_full.pgm.gz  -exec /bin/basename {} \; >> ${LOCAL}/asi_hirate_list_${site}_${yearMonth}.txt
			done
		else
			find ${HIRES_COPY}/${year}/${month}/*/${oneSite}_themis??/ -name \*_${oneSite}_\*_full.pgm.gz  -exec /bin/basename {} \; >> ${LOCAL}/asi_hirate_list_${oneSite}_${yearMonth}.txt
		fi
	done
	mv -f ${LOCAL}/asi_hirate_list_*.txt $FILELISTS

#
## Cleanup
#

	exit 0
