#!/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_clrcldy.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/sgpmmcrC1ciret2MaceAvg.c1	
#; commented to get Min's files on 20050527
#ciret2_path=/home/mace/sgp/sgpmmcrC1_MM3
ciret2_prefix=sgpmmcrC1ciret2MaceAvg.c1
#ciret2_prefix=sgpmmcrC1_MM3
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
minnis_path=/home/mace/sgp/sgpgoes8minnis-acfX1.c1.ciret

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

echo $site > site
echo $facility > facility

fi
if [ $site = 's2p' ]
then

echo using sgp C1 definitions

data_path=/home/mace/sgp/sgp.average.2min
data_prefix=sgp.average.2min
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.2min
optical_suffix=optical_properties
IC_file_dir=/home/mace/sgp/sgp.average.2min

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

if [ $site = 'nsa' ]
then

echo using nsa definitions

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

cp $raprad_path/rt1d.brdf.selection.Kato.sgp $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".000000.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

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

if [ $check -ne 0 ]
then

echo at the incre day $the_day
../../code_linux/incre_day_2k $the_day -1
prev_day=`cat prev_day`
echo aftr the incre day

gunzip -f "$ciret1_path"/"$the_day"*"$ciret1_prefix".cdf.gz 2>> /dev/null
gunzip -f "$minnis_path"/*"$the_day"*.cdf.gz 2>> /dev/null
ls "$ciret1_path"/"$the_day"*"$ciret1_prefix".cdf > ciret1_files.dat
gunzip -f "$ciret2_path"/*"$ciret2_prefix"*"$prev_day"*.cdf.gz 2>> /dev/null  
#commented 20050527 for getting Min's files
#gunzip -f "$ciret2_path"/*"$ciret2_prefix"*"$the_day"*exp.cdf.gz 2>> /dev/null
ls "$ciret2_path"/*"$ciret2_prefix"*"$prev_day"*.cdf > ciret2_files.dat
gunzip -f "$ciret2_path"/*"$ciret2_prefix"*"$the_day"*.cdf.gz 2>> /dev/null  
ls "$ciret2_path"/*"$ciret2_prefix"*"$the_day"*.cdf >> ciret2_files.dat
  
#commented 20050527 
#ls "$ciret2_path"/*"$ciret2_prefix"*"$the_day"*exp.cdf > ciret2_files.dat
# removed >> in the previous line - 20050527	
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
if [ $site = 'sgp' ]
then
#/usr/local/idl/idl/bin/idl run_idl_IC
/usr/local/idl/idl/bin/idl run_idl_IC_acft	
#; this runs the lwp/acft parameterization derived from 3/2000 data
fi
if [ $site = 'nsa' ]
then
/usr/local/idl/idl/bin/idl run_idl_IC_acft_nsa
fi

# 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
echo !!!!!!!!!!!!!!!!!  RAPRAD TURNED OFF !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#		$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 ]

#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
