#!/bin/csh 
#set echo
ml cwutil

# DOWNLOAD COASTWATCH FILES
#cwdownload --region=sl -t --age 148 vmcwsst3.nesdis.noaa.gov
cwdownload --ssl  --region=sl  --age 48 vmcwsst3.nesdis.noaa.gov

# DEFINE DIRECTORIES TO COMPUTE AND STORE FILES
setenv COMPDIR /uufs/chpc.utah.edu/common/home/u0198116/COASTWATCH/7_DAY_AVERAGE
setenv COMPDIR2 /uufs/chpc.utah.edu/common/home/u0198116/COASTWATCH/2_DAY_AVERAGE
setenv STOR    /uufs/chpc.utah.edu/common/home/u0198116/COASTWATCH/FILES

# COPY FILES TO 7_DAY_AVERAGE
cp *.hdf $COMPDIR
cp *.hdf $STOR
cp *.hdf /uufs/chpc.utah.edu/common/home/u0198116/COASTWATCH/2_DAY_AVERAGE
mv *.hdf /uufs/chpc.utah.edu/common/home/u0198116/COASTWATCH/7_DAY_AVERAGE

# MOVE TO WORKING SPACE TO PROCESS WEEKLY FILES
cd $COMPDIR

set year = ` date -u +%C%y `
set mn = ` date -u +%m `
set dy = ` date -u +%d `
set hr = ` date -u +%H `
set julday = ` date -u +%j `

echo $year
echo $mn
echo $julday


set test = `echo $julday | cut -c3`
set test2 = `echo $julday | cut -c1`
if ( $test != '' ) then
  #if true then julday is 3 digits
  if ( $test2 == '0' ) then
    #if true then julday is <100 and has leading zero
    #so cut it off here
    set julday = `echo $julday | cut -c2-3`
 endif
endif




set day = $julday

#LOOP TO MAKE SURE ONE WEEK OLD FILE IS DELETED
set test = `echo $day | cut -c3`
set test2 = `echo $day | cut -c1`
if ( $test != '' ) then
#  if true then day is 3 digits
 if ( $test2 == '0' ) then
#    if true then $day is <100 and has leading zero
#    so cut it off here
    set day = `echo $day | cut -c2-3`
 endif
endif

echo $day

@ day2 = $day - 7
echo $day2

# IN CASE SCRIPT DIDN'T RUN

@ day3 = $day - 8 
echo $day3

rm *_0$day2*.hdf
rm *_0$day3*.hdf
rm *_$day2*.hdf
rm *_$day3*.hdf
rm 7dayaverage*.hdf

# THIS WAS TO REMOVE HIGH ZENITH ANGLES BUT NOW NOAA 19 HAS THESE TIMES
rm *_09??_*.hdf 
rm *_08??_*.hdf


# MAIN BODY OF CODE TO PROCESS LST SATELLITE-DERIVED 
foreach file ( `ls *.hdf` )
echo 'running'
set fyear = `echo $file | cut -c1-4`
set fjulday = `echo $file | cut -c6-8`
#set fmndy = `jultocal $fjulday`
set fhr = `echo $file | cut -c10-11`
echo $fhr

if($fhr > 12)then
#daylight cloud mask different than night
# daytime 01101111
#bits 8-1
# change Feb 4th 01001101 (drop FMFT and reflectance uniformity test)
# change March 28th to add back FMFT 01101101
# COMPOSITE CLOUD TEST IMAGE

cwmath -v --units "temp_deg_c" --longname "sea surface temperature" --expr 'sst_cloud_masked = mask (sst, cloud, or (hex ("0x6D"),1))' $file
echo 'day'

else
#night cloud mask option
#night 00011101
#bits 8-1
#change Feb4th to 00010101 (drop FMFT)
#change March 16 to 00011001
# GENERAL CLOUD MASK PRODUCT
cwmath -v --units "temp_deg_c" --longname "sea surface temperature" --expr 'sst_cloud_masked = mask (sst, cloud, or (hex ("0x19"),1))' $file
echo 'night'

endif
end

cwcomposite -v --match sst_cloud_masked *.hdf 7dayaverage{$julday}{$hr}_n17_sl.hdf

cwrender --verbose --magnify 41.18,-112.5,5 --palette HSL256 --enhance sst_cloud_masked --range -2,32 --coast black,white  7dayaverage{$julday}{$hr}_n17_sl.hdf AVHRR${fyear}${mn}${dy}.gif

cp AVHRR${fyear}${mn}${dy}.gif mostrecent7day.gif




#CREATE STATISTICS ON BOXED AREAS IN THE NORTH AND SOUTH ARMS

cwstats --region=41.05/-112.4/6 7dayaverage{$julday}{$hr}_n17_sl.hdf > 7dayaverage{$julday}_south_arm_stats.txt

cwstats --region=41.3/-112.6667/6 7dayaverage{$julday}{$hr}_n17_sl.hdf > 7dayaverage{$julday}_north_arm_stats.txt

cp 7dayaverage{$julday}_south_arm_stats.txt 7dayaverage{$julday}_north_arm_stats.txt /uufs/chpc.utah.edu/common/home/u0198116/public_html

cp 7dayaverage{$julday}_south_arm_stats.txt mostrecent7dayaverage_south_arm_stats.txt

cp 7dayaverage{$julday}_north_arm_stats.txt mostrecent7dayaverage_north_arm_stats.txt


cp /uufs/chpc.utah.edu/common/home/u0198116/COASTWATCH/7_DAY_AVERAGE/*.gif /uufs/chpc.utah.edu/common/home/u0198116/public_html/AVHRR/IMAGES

cp /uufs/chpc.utah.edu/common/home/u0198116/COASTWATCH/7_DAY_AVERAGE/*.txt /uufs/chpc.utah.edu/common/home/u0198116/public_html/AVHRR/IMAGES



# MOVE TO WORKING SPACE TO PROCESS 2 DAY FILES
cd $COMPDIR2

set year = ` date -u +%C%y `
set mn = ` date -u +%m `
set dy = ` date -u +%d `
set hr = ` date -u +%H `
set julday = ` date -u +%j `

echo $year
echo $mn
echo $julday


set test = `echo $julday | cut -c3`
set test2 = `echo $julday | cut -c1`
if ( $test != '' ) then
  #if true then julday is 3 digits
  if ( $test2 == '0' ) then
    #if true then julday is <100 and has leading zero
    #so cut it off here
    set julday = `echo $julday | cut -c2-3`
 endif
endif

set day = $julday

#LOOP TO MAKE SURE ONE WEEK OLD FILE IS DELETED
set test = `echo $day | cut -c3`
set test2 = `echo $day | cut -c1`
if ( $test != '' ) then
#  if true then day is 3 digits
 if ( $test2 == '0' ) then
#    if true then $day is <100 and has leading zero
#    so cut it off here
    set day = `echo $day | cut -c2-3`
 endif
endif

echo $day

@ day2 = $day - 3
echo $day2

# IN CASE SCRIPT DIDN'T RUN

@ day3 = $day - 4
echo $day3

rm *_0$day2*.hdf
rm *_0$day3*.hdf
rm *_$day2*.hdf
rm *_$day3*.hdf
rm 2dayaverage*.hdf

# THIS WAS TO REMOVE HIGH ZENITH ANGLES BUT NOW NOAA 19 HAS THESE TIMES
rm *_09??_*.hdf
rm *_08??_*.hdf


# MAIN BODY OF CODE TO PROCESS LST SATELLITE-DERIVED 
foreach file ( `ls *.hdf` )
echo 'running'
set fyear = `echo $file | cut -c1-4`
set fjulday = `echo $file | cut -c6-8`
#set fmndy = `jultocal $fjulday`
set fhr = `echo $file | cut -c10-11`
echo $fhr

if($fhr > 12)then
#daylight cloud mask different than night
# daytime 01101111
#bits 8-1
# change Feb 4th 01001101 (drop FMFT and reflectance uniformity test)
# change March 28th to add back FMFT 01101101
# COMPOSITE CLOUD TEST IMAGE

cwmath -v --units "temp_deg_c" --longname "sea surface temperature" --expr 'sst_cloud_masked = mask (sst, cloud, or (hex ("0x6D"),1))' $file
echo 'day'

else
#night cloud mask option
#night 00011101
#bits 8-1
#change Feb4th to 00010101 (drop FMFT)
#change March 16 to 00011001
# GENERAL CLOUD MASK PRODUCT
cwmath -v --units "temp_deg_c" --longname "sea surface temperature" --expr 'sst_cloud_masked = mask (sst, cloud, or (hex ("0x19"),1))' $file
echo 'night'

endif
end


cwcomposite -v --match sst_cloud_masked *.hdf 2dayaverage{$julday}{$hr}_n17_sl.hdf

cwstats --region=41.05/-112.4/6 2dayaverage{$julday}{$hr}_n17_sl.hdf > 2dayaverage{$julday}_south_arm_stats.txt

cwstats --region=41.3/-112.6667/6 2dayaverage{$julday}{$hr}_n17_sl.hdf > 2dayaverage{$julday}_north_arm_stats.txt

cwrender --verbose --magnify 41.18,-112.5,5 --palette HSL256 --enhance sst_cloud_masked --range -2,32 --coast black,white  2dayaverage{$julday}{$hr}_n17_sl.hdf AVHRR2day${fyear}${mn}${dy}.gif



cp AVHRR2day${fyear}${mn}${dy}.gif mostrecent2day.gif

cp /uufs/chpc.utah.edu/common/home/u0198116/COASTWATCH/2_DAY_AVERAGE/*.gif /uufs/chpc.utah.edu/common/home/u0198116/public_html/AVHRR/IMAGES

cp 2dayaverage{$julday}_south_arm_stats.txt mostrecent2dayaverage_south_arm_stats.txt

cp 2dayaverage{$julday}_north_arm_stats.txt mostrecent2dayaverage_north_arm_stats.txt

cp /uufs/chpc.utah.edu/common/home/u0198116/COASTWATCH/2_DAY_AVERAGE/*.txt /uufs/chpc.utah.edu/common/home/u0198116/public_html/AVHRR/IMAGES

echo ALL DONE
