#!/bin/csh


# NOTE THIS ONLY NEEDS TO BE RE-RUN WHEN THE YEAR CHANGES.
# gettopnew.csh by default runs for the current year

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

cd ${rdir}/old

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

set curyear = `date -u +%Y`
set curyearm1 = `expr $curyear {-} 1`

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

set year = 2010
while ($year <= $curyearm1)
   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.old
             endif
             if ($highcount > 0) then
                echo  ${year}`echo $ff | cut -d"." -f1` $highcount >> ${rdir}/counts.high.old
             endif
             if ($lowhighcount > 0) then
                echo  ${year}`echo $ff | cut -d"." -f1` $lowhighcount >> ${rdir}/counts.lowhigh.old
             endif
         end
         cd ..
      endif
   end
   set year = `expr $year {+} 1`
end
