#!/bin/csh

set rdir = "/home/coolwx/public_html/record/archive"

cd ${rdir}

set months = "01 02 03 04 05 06 07 08 09 10 11 12"

set curyear = `date -u +%Y`

set day7 = `date -u +%Y%m%d --date="7 days ago"`
set day6 = `date -u +%Y%m%d --date="6 days ago"`
set day5 = `date -u +%Y%m%d --date="5 days ago"`
set day4 = `date -u +%Y%m%d --date="4 days ago"`
set day3 = `date -u +%Y%m%d --date="3 days ago"`
set day2 = `date -u +%Y%m%d --date="2 days ago"`
set day1 = `date -u +%Y%m%d --date="1 days ago"`
set day0 = `date -u +%Y%m%d`

rm -f ${rdir}/counts.high.new
touch ${rdir}/counts.high.new
rm -f ${rdir}/counts.low.new
touch ${rdir}/counts.low.new
rm -f ${rdir}/counts.lowhigh.new
touch ${rdir}/counts.lowhigh.new

set year = $curyear
foreach month ($months)
   echo $month $year
   if (-e ${year}${month}) then
      cd ${year}${month}
      set ffs = `ls -1 *.data | egrep -v "0229[0-9][0-9]"`
      foreach ff ($ffs)
         set lowcount = `grep -c "BROKEN LOW" $ff`
         set highcount = `grep -c "BROKEN HIGH" $ff`
         set lowhighcount = `egrep -c "(BROKEN HIGH|BROKEN LOW)" $ff`
         if ($lowcount > 0) then
            echo  ${year}`echo $ff | cut -d"." -f1` $lowcount >> ${rdir}/counts.low.new
         endif
         if ($highcount > 0) then
            echo  ${year}`echo $ff | cut -d"." -f1` $highcount >> ${rdir}/counts.high.new
         endif
         if ($lowhighcount > 0) then
            echo  ${year}`echo $ff | cut -d"." -f1` $lowhighcount >> ${rdir}/counts.lowhigh.new
         endif
      end
      cd ..
   endif
end

cat counts.low.old counts.low.new | sort -r -n -k 2   > counts.low
cat counts.high.old counts.high.new | sort -r -n -k 2 > counts.high
cat counts.lowhigh.old counts.lowhigh.new | sort -r -n -k 2 > counts.lowhigh

set months = "Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec"

rm -f counts.temp
set dtgs = `head -1000 counts.low | cut -c1-8 | sort | uniq`
foreach dtg ($dtgs)
   grep "^${dtg}" counts.low | sort -r -n -k 2 | head --lines=1 >> counts.temp
end

sort -r -n -k 2 counts.temp | head --lines=15 | cat -n |  sed 's/     /    0/g'| sed 's/^/<BR>\&nbsp\&nbsp\&nbsp#/g' | sed 's/\t/: /g'| awk '{print $1$2" "$3" (" $4 ")"}' > counts.low.trim
set dtgs = `cut -d" " -f2 counts.low.trim`
rm -f counts.temp
foreach dtg ($dtgs)
   set sdtg = `echo $dtg | cut -c1-8`
   set year = `echo $dtg | cut -c1-4`
   set month = `echo $dtg | cut -c5-6`
   set month2 = `echo $months | cut -d" " -f${month}`
   set day = `echo $dtg | cut -c7-8`
   set hr = `echo $dtg | cut -c9-10`
   set dtg2 = "${hr}Z ${day} ${month2} ${year}"
   if ($sdtg == $day0 | $sdtg == $day1 | $sdtg == $day2 | $sdtg == $day3 | $sdtg == $day4 | $sdtg == $day5 | $sdtg == $day6 | $sdtg == $day7) then
      grep $dtg counts.low.trim  | sed "s/$dtg/\<font color=#ff00ff\>$dtg2\<\/font\>/g" >> counts.temp
   else
      grep $dtg counts.low.trim  | sed "s/$dtg/$dtg2/g" >> counts.temp
   endif
end

mv counts.temp counts.low.trim.latest

rm -f counts.temp
set dtgs = `head -1000 counts.high | cut -c1-8 | sort | uniq`
foreach dtg ($dtgs)
   grep "^${dtg}" counts.high| sort -r -n -k 2 | head --lines=1 >> counts.temp
end

sort -r -n -k 2 counts.temp | head --lines=15 | cat -n |  sed 's/     /    0/g'| sed 's/^/<BR>\&nbsp\&nbsp\&nbsp#/g' | sed 's/\t/: /g'| awk '{print $1$2" "$3" (" $4 ")"}' > counts.high.trim
set dtgs = `cut -d" " -f2 counts.high.trim`
rm -f counts.temp
foreach dtg ($dtgs)
   set sdtg = `echo $dtg | cut -c1-8`
   set year = `echo $dtg | cut -c1-4`
   set month = `echo $dtg | cut -c5-6`
   set month2 = `echo $months | cut -d" " -f${month}`
   set day = `echo $dtg | cut -c7-8`
   set hr = `echo $dtg | cut -c9-10`
   set dtg2 = "${hr}Z ${day} ${month2} ${year}"
   if ($sdtg == $day0 | $sdtg == $day1 | $sdtg == $day2 | $sdtg == $day3 | $sdtg == $day4 | $sdtg == $day5 | $sdtg == $day6 | $sdtg == $day7) then
      grep $dtg counts.high.trim  | sed "s/$dtg/\<font color=#ff00ff\>$dtg2\<\/font\>/g" >> counts.temp
   else
      grep $dtg counts.high.trim  | sed "s/$dtg/$dtg2/g" >> counts.temp
   endif
end
mv counts.temp counts.high.trim.latest


rm -f counts.temp
set dtgs = `head -1000 counts.lowhigh | cut -c1-8 | sort | uniq`
foreach dtg ($dtgs)
   grep "^${dtg}" counts.lowhigh| sort -r -n -k 2 | head --lines=1 >> counts.temp
end

sort -r -n -k 2 counts.temp | head --lines=15 | cat -n |  sed 's/     /    0/g'| sed 's/^/<BR>\&nbsp\&nbsp\&nbsp#/g' | sed 's/\t/: /g'| awk '{print $1$2" "$3" (" $4 ")"}' > counts.lowhigh.trim
set dtgs = `cut -d" " -f2 counts.lowhigh.trim`
rm -f counts.temp
foreach dtg ($dtgs)
   set sdtg = `echo $dtg | cut -c1-8`
   set year = `echo $dtg | cut -c1-4`
   set month = `echo $dtg | cut -c5-6`
   set month2 = `echo $months | cut -d" " -f${month}`
   set day = `echo $dtg | cut -c7-8`
   set hr = `echo $dtg | cut -c9-10`
   set dtg2 = "${hr}Z ${day} ${month2} ${year}"
   if ($sdtg == $day0 | $sdtg == $day1 | $sdtg == $day2 | $sdtg == $day3 | $sdtg == $day4 | $sdtg == $day5 | $sdtg == $day6 | $sdtg == $day7) then
      grep $dtg counts.lowhigh.trim  | sed "s/$dtg/\<font color=#ff00ff\>$dtg2\<\/font\>/g" >> counts.temp
   else
      grep $dtg counts.lowhigh.trim  | sed "s/$dtg/$dtg2/g" >> counts.temp
   endif
end
mv counts.temp counts.lowhigh.trim.latest
