#! /bin/csh -f
#
## Determine if this is a leap year or not.
# Also, determine if last year was a leap year or not.
# #
setenv S /Net/elninost4/amishra/RSM
setenv HD /Net/water/amishra/RSM_ROMS/MONSOON10km/RCM-CLD2/STAMPEDE/O

@ yr = 1995

while ($yr <= 1995)
mkdir ${HD}/Daily/$yr
mkdir ${HD}/Daily/$yr/traw
cd ${HD}/Daily/$yr/traw
echo 'going to extract Temperature for ' $yr  >> synopsis_$yr
##*******************************
set dayend = 365
echo `date `>> ${yr}.log

echo "$yr / 4" >> tmp
echo "quit" >> tmp
set leap = `bc -l < tmp`
rm -f tmp


set leaptrue = `echo $leap | cut -c5`
if ($leaptrue == 0) then
   set dayend = 366
   echo 'yes, '$yr' is a leap year'
endif

@ pyear = $yr - 1
echo "$pyear / 4" >> leaptmp
echo quit >> leaptmp
set pleap = `bc -l < leaptmp`
rm -f leaptmp
set pleaptrue = `echo $pleap | cut -c5`
   if ($pleaptrue == 0) then
      echo $pyear was a leap year
   endif

set nmon = 1
##set nmon = 3
set nday = 1

##@ loop = 60
@ loop = 1
while ($loop <= $dayend)
   if ($nday > 31 && ($nmon == 1 || $nmon == 3 || $nmon == 5 || $nmon == 7 || $nmon == 8 || $nmon == 10 || $nmon == 12)) then
      @ nday = 1
      @ nmon = $nmon + 1
      if ($nmon > 12) then
         @ nmon = 1
      endif
   else if ($nmon == 2 && $nday > 28 && $leaptrue != 0) then
      @ nday = 1
      @ nmon = $nmon + 1
   else if ($nmon == 2 && $nday > 29 && $leaptrue == 0) then
      @ nday = 1
      @ nmon = $nmon + 1
   else if ($nday > 30 && ($nmon == 4 || $nmon == 6 || $nmon == 9 || $nmon == 11)) then
      @ nday = 1
      @ nmon = $nmon + 1
   endif
   if ($nday < 10) set nday=0$nday
   if ($nmon < 10) set nmon=0$nmon

   set ntim = $nmon$nday
   echo $ntim
##################################################################
echo 'Extracting Temperature Data for  = ' $yr$ntim >> synopsis_${yr}

cat > temp_${ntim}.ncl << EOF
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"

begin
     
     
     cdf_file_01 = addfile("${HD}/${yr}/Daily/ocean_avg.${yr}${ntim}.nc","r")
     sst_01 = cdf_file_01->temp
     filo_01 = "temp-${yr}${ntim}-s.dat"
     fbinrecwrite (filo_01, -1, sst_01 )

end
EOF
###############################################
ncl temp_${ntim}.ncl
wait
#############################################
rm temp_${ntim}.ncl
##################################################################

   set nmontmp = `echo $nmon | cut -c1-1`
   if ($nmontmp < 1) set nmon = `echo $nmon | cut -c2-2`


   if ($pleaptrue != 0) then
      if ($loop > 365) then
         @ looptmp = $loop - 365
      else
         set looptmp = $loop
      endif
   else if ($pleaptrue == 0) then
      if ($loop > 366) then
         @ looptmp = $loop - 366
      else
         set looptmp = $loop
      endif
   endif

   if ($leaptrue != 0) then

      if (($looptmp < 10 && $nmon == 1) || ($looptmp < 41 && $nmon == 2) || ($looptmp < 69 && $nmon == 3) || ($looptmp < 100 && $nmon == 4) || ($looptmp < 130 && $nmon == 5) || ($looptmp < 161 && $nmon == 6) || ($looptmp < 191 && $nmon == 7) || ($looptmp < 222 && $nmon == 8) || ($looptmp < 253 && $nmon == 9) || ($looptmp < 283 && $nmon == 10) || ($looptmp < 314 && $nmon == 11) || ($looptmp < 344 && $nmon == 12)) set nday = `echo $nday | cut -c2-2`

   else if ($leaptrue == 0) then

      if (($looptmp < 10 && $nmon == 1) || ($looptmp < 41 && $nmon == 2) || ($looptmp < 70 && $nmon == 3) || ($looptmp < 101 && $nmon == 4) || ($looptmp < 131 && $nmon == 5) || ($looptmp < 162 && $nmon == 6) || ($looptmp < 192 && $nmon == 7) || ($looptmp < 223 && $nmon == 8) || ($looptmp < 254 && $nmon == 9) || ($looptmp < 284 && $nmon == 10) || ($looptmp < 315 && $nmon == 11) || ($looptmp < 345 && $nmon == 12)) set nday = `echo $nday | cut -c2-2`

   endif


   @ nday = $nday + 1
   @ loop = $loop + 1
end
    @ yr = $yr + 1
end
exit
