#!/bin/sh
#

if [ $# -eq 5 ]
then


the_day=$1
num_days=$2
site=$3
facility=$4
year=$5

raprad_path=/home/mace/anvil_s1/DOE/code/rap_rad/Kato_code_update/rt/rt.rapradL/scenarios/bbflux/Integrated_Column

raprad_script=test_script_icemicro.sh
#raprad_script=cloudy_script.sh

if [ $site = 'sgp' ]
then

echo using sgp C1 definitions

data_path=/home/mace/sgp/sgp.average.5min
data_prefix=sgp.average.5min
ciret1_path=/home/mace/sgp_dir/ci_ret1_fl/mean_aeri_mean_dbze
ciret1_prefix=ci_retrieval1
ciret2_path=/home/mace/sgp/sgpmmcrC1ciret2Mace.c1
ciret2_prefix=sgpmmcrC1ciret2Mace.c1
stret1_path=/home/mace/sgp/DsgpstretC1.Dong
stret1_prefix=sgpstretC1.Mace
optical_property_file_dir=/home/mace/sgp/sgp.average.5min
optical_suffix=optical_properties
IC_file_dir=/home/mace/sgp/sgp.average.5min

cp $raprad_path/rt1d.brdf.selection.Kato.sgp $raprad_path/rt1d.brdf.selection

echo $site > site
echo $facility > facility

fi
if [ $site = 'twp' -a $facility = 'C2' ]
then

echo using twp C2 definitions

path=/home/mace/twp2
data_path=$path/twpc2.average.5min
data_prefix=twpc2.average.5min
ciret1_path=/home/mace/sgp_dir/ci_ret1_twp2/mean_aeri_mean_dbze
ciret1_prefix=ci_retrieval1
ciret2_path=$path/twpmmcrC2ciret2Mace.c1
ciret2_prefix=twpmmcrC2ciret2Mace.c1
stret1_path=$path/DtwpstretC2.Dong
stret1_prefix=twpstretC2.Mace
optical_property_file_dir=$path/twpc2.average.5min
optical_suffix=optical_properties
IC_file_dir=$path/twpc2.average.5min

cp $raprad_path/rt1d.brdf.selection.Kato.twp $raprad_path/rt1d.brdf.selection


echo $site > site
echo $facility > facility

fi

incre=1



while [ $incre -le $num_days ]
do

# see if there is a five minute file for this day

gunzip -f "$data_path"/*"$data_prefix"*"$the_day"*.cdf.gz 2>> /dev/null
echo "$data_path"/*"$data_prefix"*"$the_day".000000.cdf
ls "$data_path"/*"$data_prefix"*"$the_day".000000.cdf > IntCol_files.dat
gunzip -f "$data_path"/*"$data_prefix"*"$the_day"*.cdf.gz 2>> /dev/null

../../code_linux/incre_day_2k $the_day -1
prev_day=`cat prev_day`

gunzip -f "$ciret2_path"/*"$ciret2_prefix"*"$prev_day"*.cdf.gz 2>> /dev/null
gunzip -f "$ciret2_path"/*"$ciret2_prefix"*"$the_day"*.cdf.gz 2>> /dev/null
ls "$ciret2_path"/*"$ciret2_prefix"*"$prev_day"*.cdf > ciret2_files.dat
ls "$ciret2_path"/*"$ciret2_prefix"*"$the_day"*.cdf >> ciret2_files.dat
check=`cat IntCol_files.dat | wc -l`
check22=`cat ciret2_files.dat | wc -l`
#if [ $check22 -ne 0 ]
#then
if [ $check -ne 0 ]
then


gunzip -f "$ciret1_path"/"$the_day"*"$ciret1_prefix".cdf.gz 2>> /dev/null
ls "$ciret1_path"/"$the_day"*"$ciret1_prefix".cdf > ciret1_files.dat
gunzip -f "$stret1_path"/*"$stret1_prefix"*"$the_day"*.cdf.gz 2>> /dev/null
ls "$stret1_path"/*"$stret1_prefix"*"$the_day"*.cdf > stret1_files.dat

# now run the idl code to generate the optical property file and the appended 5 minute file

/usr/local/idl/idl/bin/idl run_idl_IC_ice

# now check to see if the optical property file was created.  If so, run fluxes.

ls "$optical_property_file_dir"/*"$data_prefix"*"$the_day"*"$optical_suffix"*.cdf > IntCol_files.dat

check2=`cat IntCol_files.dat | wc -l`

	if [ $check2 -ne 0 ]
	then

#		now run raprad

		cur_dir=`pwd`
		cd $raprad_path
echo $raprad_script $year $the_day 1 $site $facility
		$raprad_script $year $the_day 1 $site $facility

		cd $cur_dir
#		gzip -f "$optical_property_file_dir"/*"$data_prefix"*"$the_day"*"$optical_suffix"*.cdf &
	fi



fi	# if [ $check -ne 0 ]
#fi

gzip -f "$data_path"/*"$the_day"*.cdf 
gzip -f "$IC_file_dir"/*"$the_day"*.cdf
gzip -f "$optical_property_file_dir"/*"$the_day"*.cdf
gzip -f "$ciret1_path"/*"$the_day"*.cdf
gzip -f "$ciret2_path"/*"$the_day"*.cdf
gzip -f "$stret1_path"/*"$the_day"*.cdf


		../../code_linux/incre_day_2k $the_day
		the_day=`cat next_day`




	incre=`expr $incre + 1`


done


else

	echo Supply argument YYYYMMDD, num, site, facility, and year where YYYY is the year, MM is the month
	echo numer and DD is the day of the month and num is the number of days to process.
	echo site is the ARM site and facility is C1 or C2.
fi
