#!/bin/csh
#
source /uufs/chpc.utah.edu/sys/pkg/idl/std/etc/idl.csh


################################################################################
# Set the variables here.
################################################################################

# Note: Facility MUST be different from site name. For example,
# for twpc2, site would be 'twp' and facility would be 'C2'


set site='twp'
set facility='C3'

foreach long_month ('200301')
#'200701'\
#'200501'\
#  '200702'\
#'200502' '200503' '200504' '200505' '200506' '200507' '200508' '200509' '200510' '200511' '200512'\
#'200703' '200704' '200705' '200706' '200707' '200708' '200709' '200710' '200711' '200712'\
#'200601' '200602' '200603' '200604' '200605' '200606' '200607' '200608' '200609' '200610' '200611' '200612'\
#'200801' '200802' '200803' '200804' '200805' '200806' '200807' '200808' '200809' '200810' '200811' '200812') 

#foreach long_month ('200301' '200302' '200303' '200304' '200305' '200306' '200307' '200308' '200309' '200310' '200311' '200312'\
#'200401' '200402' '200403' '200404' '200405' '200406' '200407' '200408' '200409' '200410' '200411' '200412'\
#'200501' '200502' '200503' '200504' '200505' '200506' '200507' '200508' '200509' '200510' '200511' '200512'\
#'200601' '200602' '200603' '200604' '200605' '200606' '200607' '200608' '200609' '200610' '200611' '200612'\
#'200701' '200702' '200703' '200704' '200705' '200706' '200707' '200708' '200709' '200710' '200711' '200712'\
#'200801' '200802' '200803' '200804' '200805' '200806' '200807' '200808' '200809' '200810' '200811' '200812')

#foreach long_month ('200407' '200408' '200409' '200410' '200411' '200412'\
#  '200501' '200502' '200503' '200504' '200505' '200506' '200507' '200508' '200509' '200510' '200511' '200512'\
#  '200701' '200702' '200703' '200704' '200705' '200706' '200707' '200708' '200709' '200710' '200711' '200712'\
#  '200601' '200602' '200603' '200604' '200605' '200606' '200607' '200608' '200609' '200610' '200611' '200612'\
#  '200201' '200202' '200203' '200204' '200205' '200206' '200207' '200208' '200209' '200210' '200211' '200212')

#foreach day ('01' '02' '03' '04' '05' '06' '07' '08' '09' '10' '11' '12' '13' '14' '15'\
#             '16' '17' '18' '19' '20' '21' '22' '23' '24' '25' '26' '27' '28' '29' '30' '31')

foreach day ('01' '03' '06' '10')

rm year.txt
rm month.txt
idl<<EOF
find_month_year,'${long_month}'
EOF

set year = `cat year.txt`
set month = `cat month.txt`

echo $year
echo $month
echo $day
	      
set the_day=${year}${month}${day}

rm prev_day.txt
idl<<EOF
find_prev_day,'${the_day}'
EOF

set prev_day = `cat prev_day.txt`




################################################################################


#raprad_path=/home/mace/anvil_s1/DOE/code/rap_rad/Kato_code_update/rt/rt.rapradL/scenarios/bbflux/Integrated_Column
# Everything related to raprad is ignored for now.

#raprad_script=test_script_clrcldy.sh
#raprad_script=cloudy_script.sh

if ( $site == 'sgp' ) then

 # echo using sgp C1 definitions
  set idlsite=sgp

  set armdir=/uufs/chpc.utah.edu/common/home/mace_grp/data/arm
  #data_path=/home/mace/sgp/sgp.average.5min # This is from the meteo environment
  set data_path=${armdir}/sgp/sgp.average.5min # This is to be used on CHPC
  set data_prefix=sgp.average.5min
  set ciret1_path=/uufs/chpc.utah.edu/common/home/u0536646/cloudret/ci_ret1_fl/mean_aeri_mean_dbze
  set ciret1_prefix=ci_retrieval1
  set ciret2_path=/uufs/chpc.utah.edu/common/home/u0536646/cloudret/sgp/sgpmmcrC1ciret2MaceAvg.c1	
  #; commented to get Min's files on 20050527
  #set ciret2_path=/home/mace/sgp/sgpmmcrC1_MM3
  set ciret2_prefix=sgpmmcrC1ciret2MaceAvg.c1
  #set ciret2_prefix=sgpmmcrC1_MM3
  set stret1_path=/uufs/chpc.utah.edu/common/home/u0536646/cloudret/DsgpstretC1.Dong
  set stret1_prefix=sgpstretC1.Mace
  #set optical_property_file_dir=/home/mace/sgp/sgp.average.5min
  set optical_property_file_dir=/uufs/chpc.utah.edu/common/home/u0536646/cloudret/sgp/sgp.average.5min.ciret4
  set optical_suffix=optical_properties
  #set IC_file_dir=/home/mace/sgp/sgp.average.5min
  set IC_file_dir=/uufs/chpc.utah.edu/common/home/u0536646/cloudret/sgp/sgp.average.5min.ciret4
  set minnis_path=/uufs/chpc.utah.edu/common/home/u0536646/cloudret/sgp/sgpgoes8minnis-acfX1.c1.ciret
  #cp $raprad_path/rt1d.brdf.selection.Kato.sgp $raprad_path/rt1d.brdf.selection

  #echo $site > site
  #echo $facility > facility
  #On CHPC, we will run IDL directly from this shell, and
  #therefore we do not need to echo to a file.

endif

if ( $site == 's2p' ) then

  echo using sgp C1 definitions
  set idlsite=s2p

  set data_path=/uufs/chpc.utah.edu/common/home/u0536646/cloudret/sgp/sgp.average.2min
  set data_prefix=sgp.average.2min
  set ciret1_path=/uufs/chpc.utah.edu/common/home/u0536646/cloudret/ci_ret1_fl/mean_aeri_mean_dbze
  set ciret1_prefix=ci_retrieval1
  set ciret2_path=/uufs/chpc.utah.edu/common/home/u0536646/cloudret/sgp/sgpmmcrC1ciret2Mace.c1
  set ciret2_prefix=sgpmmcrC1ciret2Mace.c1
  set stret1_path=/uufs/chpc.utah.edu/common/home/u0536646/cloudret/sgp/DsgpstretC1.Dong
  set stret1_prefix=sgpstretC1.Mace
  set optical_property_file_dir=/uufs/chpc.utah.edu/common/home/u0536646/cloudret/sgp/sgp.average.2min
  set optical_suffix=optical_properties
  set IC_file_dir=/uufs/chpc.utah.edu/common/home/u0536646/cloudret/sgp/sgp.average.2min

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

  #echo $site > site
  #echo $facility > facility
  #not used on new version.

endif

if ( $site == 'twp' && $facility == 'C2' ) then

  echo using twp C2 definitions
  set idlsite=twpc2

  set path=/uufs/chpc.utah.edu/common/home/u0536646/cloudret/twpc2
  set data_path=${path}/twpc2.average.5min
  set data_prefix=twpc2.average.5min
  set ciret1_path=/uufs/chpc.utah.edu/common/home/u0536646/cloudret/ci_ret1_twp2/mean_aeri_mean_dbze
  set ciret1_prefix=ci_retrieval1
  set ciret2_path=${path}/twpmmcrC2ciret2Mace.c1
  set ciret2_prefix=twpmmcrC2ciret2Mace.c1
  set stret1_path=${path}/DtwpstretC2.Dong
  set stret1_prefix=twpstretC2.Mace
  set optical_property_file_dir=$path/twpc2.average.5min
  set optical_suffix=optical_properties
  set 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

endif

if ( $site == 'nsa' ) then

  echo using nsa definitions
  set idlsite=nsa

  set data_path=/uufs/chpc.utah.edu/common/home/u0536646/cloudret/nsa/nsa.average.5min
  set data_prefix=nsa.average.5min
  set ciret1_path=/uufs/chpc.utah.edu/common/home/u0536646/cloudret/ci_ret1_fl/mean_aeri_mean_dbze
  set ciret1_prefix=ci_retrieval1
  set ciret2_path=/uufs/chpc.utah.edu/common/home/u0536646/cloudret/nsa/nsammcrC1ciret2MaceAvg.c1
  set ciret2_prefix=nsammcrC1ciret2MaceAvg.c1
  #set stret1_path=/home/mace/sgp/DsgpstretC1.Dong
  #set stret1_prefix=sgpstretC1.Mace
  set optical_property_file_dir=/uufs/chpc.utah.edu/common/home/u0536646/cloudret/nsa/nsa.average.5min
  set optical_suffix=optical_properties
  set IC_file_dir=/uufs/chpc.utah.edu/common/home/u0536646/cloudret/nsa/nsa.average.5min

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

  #echo $site > site
  #echo $facility > facility

endif

if ( $site == 'twp' && $facility == 'C3' ) then

  echo using twp C3 definitions
  set idlsite=twpc3

  set armdir=/uufs/chpc.utah.edu/common/home/mace_grp/data/arm
  #data_path=/home/mace/sgp/sgp.average.5min # This is from the meteo environment
  set data_path=${armdir}/twpc3/twpc3.average.5min # This is to be used on CHPC
  set data_prefix=twpc3.average.5min
  set ciret1_path=/uufs/chpc.utah.edu/common/home/u0536646/cloudret/ci_ret1_fl/mean_aeri_mean_dbze
  set ciret1_prefix=ci_retrieval1
  set ciret2_path=/uufs/chpc.utah.edu/common/home/u0536646/cloudret/twpc3/sgpmmcrC1ciret2MaceAvg.c1	
  #; commented to get Min's files on 20050527
  #set ciret2_path=/home/mace/sgp/sgpmmcrC1_MM3
  set ciret2_prefix=twpmmcrC3ciret2MaceAvg.c1
  #set ciret2_prefix=sgpmmcrC1_MM3
  set stret1_path=/uufs/chpc.utah.edu/common/home/u0536646/cloudret/DtwpstretC3.Dong
  set stret1_prefix=twpstretC3.Mace
  #set optical_property_file_dir=/home/mace/sgp/sgp.average.5min
  set optical_property_file_dir=/uufs/chpc.utah.edu/common/home/u0536646/cloudret/twpc3/twpc3.average.5min.ciret4
  set optical_suffix=optical_properties
  #set IC_file_dir=/home/mace/sgp/sgp.average.5min
  set IC_file_dir=/uufs/chpc.utah.edu/common/home/u0536646/cloudret/twpc3/twpc3.average.5min.ciret4
  set minnis_path=/uufs/chpc.utah.edu/common/home/u0536646/cloudret/twpc3/twpc3goes8minnis-acfX1.c1.ciret
  #cp $raprad_path/rt1d.brdf.selection.Kato.twp $raprad_path/rt1d.brdf.selection


  #echo $site > site
  #echo $facility > facility

endif

if ( $site == 'twp' && $facility == 'C1' ) then

  echo using twp C1 definitions
  set idlsite=twpc1

  set armdir=/uufs/chpc.utah.edu/common/home/mace_grp/data/arm
  #data_path=/home/mace/sgp/sgp.average.5min # This is from the meteo environment
  set data_path=${armdir}/twpc1/twpc1.average.5min # This is to be used on CHPC
  set data_prefix=twpc1.average.5min
  set ciret1_path=/uufs/chpc.utah.edu/common/home/u0536646/cloudret/ci_ret1_fl/mean_aeri_mean_dbze
  set ciret1_prefix=ci_retrieval1
  set ciret2_path=/uufs/chpc.utah.edu/common/home/u0536646/cloudret/twpc1/sgpmmcrC1ciret2MaceAvg.c1	
  #; commented to get Min's files on 20050527
  #set ciret2_path=/home/mace/sgp/sgpmmcrC1_MM3
  set ciret2_prefix=twpmmcrC1ciret2MaceAvg.c1
  #set ciret2_prefix=sgpmmcrC1_MM3
  set stret1_path=/uufs/chpc.utah.edu/common/home/u0536646/cloudret/DtwpstretC1.Dong
  set stret1_prefix=twpstretC1.Mace
  #set optical_property_file_dir=/home/mace/sgp/sgp.average.5min
  set optical_property_file_dir=/uufs/chpc.utah.edu/common/home/u0536646/cloudret/twpc1/twpc1.average.5min.ciret4
  set optical_suffix=optical_properties
  #set IC_file_dir=/home/mace/sgp/sgp.average.5min
  set IC_file_dir=/uufs/chpc.utah.edu/common/home/u0536646/cloudret/twpc1/twpc1.average.5min.ciret4
  set minnis_path=/uufs/chpc.utah.edu/common/home/u0536646/cloudret/twpc1/twpc1goes8minnis-acfX1.c1.ciret
  #cp $raprad_path/rt1d.brdf.selection.Kato.twp $raprad_path/rt1d.brdf.selection


  #echo $site > site
  #echo $facility > facility

endif

set incre=1



# while ( $incre <= $num_days ) ## Replaced by triple for loop
  # 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.gz 2>> /dev/null
  ls "$data_path"/*"$data_prefix"*"$the_day".000000.cdf > IntCol_files.dat
  ls "$data_path"/*"$data_prefix"*"$the_day".000000.temp.cdf > IntCol_files_temp.dat
  ls "$data_path"/*"$data_prefix"*"$the_day".000000.optical_properties.cdf > IntCol_files_optical.dat
  gunzip -f "$data_path"/*"$data_prefix"*"$the_day"*.cdf.gz 2>> /dev/null

  #set check1=`cat IntCol_files.dat | wc -l`
  set check1=1
echo $check1
  if ( $check1 != 0 ) then
    #echo at the incre day $the_day
    # ../../code_linux/incre_day_2k $the_day -1
    # prev_day=`cat prev_day` # This code removed due to hardsetting 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
    #endif
    #if ( $site == 'nsa' ) then
    #/usr/local/idl/idl/bin/idl run_idl_IC_acft_nsa #Changed to a straight IDL call.
    #Replaced with this code:
    #idl<<EOF
    #append_cldmicro_to_5minavg_jk_notype
    #exit
    #EOF

    #set file_error=0

    idl<<EOF
    plot_ciret_5min,'${idlsite}','${the_day}','${prev_day}','${data_path}','${data_prefix}','${ciret2_path}','${ciret2_prefix}','${stret1_path}','${stret1_prefix}'
EOF

   
   
        
    #endif

    # 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

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

    #if ( $check2 != 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 &
  endif



#endif # if ( $check != 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 ## Removed because of the foreach statements.
#@ the_day=`cat next_day`



# @ incre+=1 ## Removed because of the foreach statements.
#incre=`expr $incre + 1`

set prev_day=the_day
end
end
