#!/usr/bin/ksh

## 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

## The following variables are used to pick out
## site name (which is variable) from the filename
#
	integer end_length=8

## Process sites
#
	for line in $(cat ${CONFIGDIR}/ualaska_netcdf_site_list.txt)
	do
		ucsite=$(echo $line | awk -F_ '{print $1}')
		lcsite=$(echo $line | awk -F_ '{print $2}')

		find ${UALA_MIRROR_HOME}/${ucsite}/ -name \*${ucsite}\* -exec /bin/basename {} \; | sort >> /tmp/gmag_raw_alk_${lcsite}.txt

		for file in $(cat /tmp/gmag_raw_alk_${lcsite}.txt)
		do
			year=$(echo $file | awk '{print substr($1,1,4)}')
			integer filename_length=${#file}
			start=$(( $filename_length - $end_length ))
			doy=$(echo $file $start | awk '{print substr($1,$2,3)}')
			finddate=$(/home/thmsoc/software_dev/ksh/doy2date $year $doy)
			month=$(echo $finddate | awk -F/ '{printf("%02d",$2)}')
			day=$(echo $finddate | awk -F/ '{printf("%02d",$3)}')
			echo ${year}${month}${day} >> /tmp/gmag_raw_alk_${lcsite}2.txt
		done

		previous=NaN
		for line in $(cat /tmp/gmag_raw_alk_${lcsite}2.txt)
		do
			if [[ $line != $previous ]]
			then
				echo $line >> ${LOCAL}/gmag_raw_alk_${lcsite}.txt
				previous=${line}
			fi
		done
		rm -f /tmp/gmag_raw_alk_${lcsite}*.txt
		mv -f ${LOCAL}/gmag_raw_alk_${lcsite}.txt ${FILELISTS}/gmag_raw_alk_${lcsite}.txt
	done

#
## Cleanup
#
	exit 0
