#!/bin/csh

set fns = `ls 2*/*.data`
foreach fn ($fns)

set ayear = `echo $fn | cut -c1-4`
set amonth = `echo $fn | cut -c5-6`
set aday = `echo $fn | cut -c10-11`
set ahr = `echo $fn | cut -c12-13`

set dtg = "$ayear$amonth$aday$ahr"
set sdtg = "$amonth$aday$ahr"

if (! ( -e ${ayear}${amonth}/${sdtg}.html ) ) then

setenv GADDIR /home/coolwx/public_html/lib/grads/
setenv GAUDFT /home/coolwx/public_html/lib/grads/udf/regrid2/udft.regrid2

set scount = `wc -l $fn | cut -d" " -f1`

echo "<center>http://coolwx.com/record archive for $dtg" > temp.php

set chk = `egrep -c "(MONTH|YEAR)" $fn`
if ($chk > 0) then
    echo "<P><font color=#ff0000>*X* = Monthly record &nbsp;&nbsp **X** = All-time record</font><P>" >> temp.php
endif

echo "<table border=1>" >> temp.php
echo "<TR><td bgcolor=#00ff00><font size=-2>ID</font></td><td bgcolor=#00ff00><font size=-2>City</font></td><td bgcolor=#00ff00><font size=-2>Country</font></td><td bgcolor=#00ff00><font size=-2><center>Type and<BR>unofficial<BR>status</center></font></td><td bgcolor=#00ff00><font size=-2><center>Current<BR>Temp.<BR>(&deg;F)</center></font></td><td bgcolor=#00ff00><font size=-2><center>Unofficial<BR>Daily/Monthly<BR>Record (&deg;F)</center></font></td><td bgcolor=#00ff00><font size=-2><center>Margin of<BR>Daily<BR>Record (&deg;F)</center></font></td><td bgcolor=#00ff00><font size=-2><center>Year of<BR>Daily<BR>Record</center></font></td><td bgcolor=#00ff00><font size=-2><center>Database<BR>Length<BR>(years)</center></font></td>" >> temp.php

set stns = `grep BROKEN $fn | grep HIGH | sort -r -n -k 7 | cut -d" " -f2`
foreach stn ($stns)
  set data = `grep " $stn " $fn`
  set temp = `echo $data | cut -d" " -f3`
  set rech = `echo $data | cut -d" " -f5`
  set delta = `echo $data | cut -d" " -f7`
  set delta = `echo "scale=1;${delta}/1.0" | bc -l`
  set mchk = `grep  " ${stn} " $fn | grep -c "MONTHH"`
  set ychk = `grep  " ${stn} " $fn | grep -c "YEARH"`
  set star = ""
  if ($mchk > 0) then
      set star = "<font size=+1><font color=#ff0000>*</font></font>"
  endif
  if ($ychk > 0) then
      set star = "<font size=+1><font color=#ff0000>**</font></font>"
  endif
  set dh = `echo $data | cut -d" " -f7`
  set yearh = `echo $data | cut -d" " -f9`
  set numy = `echo $data | cut -d" " -f10`
  set monthrech = `echo $data | cut -d" " -f13`
  set yearrech = `echo $data | cut -d" " -f14`
  set city = `grep "^$stn;" ../../extreme/stations.world | cut -d";" -f2`
  set cchk = `echo $city | cut -d"," -f4 | egrep -c "[a-z,A-Z]"`
  set comma = 4
  if ($cchk == 0) then
     set cchk = `echo $city | cut -d"," -f3 | egrep -c "[a-z,A-Z]"`
     set comma = 3
  endif
  if ($cchk == 0) then
     set cchk = `echo $city | cut -d"," -f2 | egrep -c "[a-z,A-Z]"`
     set comma = 2
  endif
  if ($cchk == 0) then
     set cchk = `echo $city | cut -d"," -f1 | egrep -c "[a-z,A-Z]"`
     set comma = 1
  endif
  set city2 = `echo $city | cut -d"," -f1,${comma}`
  set country = `grep "^$stn;" ../../extreme/stations.world | cut -d";" -f3`
  echo "<TR><td><font size=-2>${stn}</font></td><td><font size=-2>$city2</font></td><td><font size=-2>${country}</font></td><td bgcolor=#ff00ff><font size=-2><font color=#ffffff><center>HIGH: BROKEN</center></font></font></td><td><font size=-2><center>${star}${temp}${star}</center></font></td><td><font size=-2><center>${rech}/${monthrech}</center></font></td><td><font size=-2><center>${delta}</center></font></td><td><font size=-2><center>$yearh</center></font></td><td><font size=-2><center>${numy}</center></font></td>" >> temp.php
end

set stns = `grep TIE $fn | grep HIGH | sort -r -n -k 7 | cut -d" " -f2`
foreach stn ($stns)
  set data = `grep " $stn " $fn`
  set temp = `echo $data | cut -d" " -f3`
  set rech = `echo $data | cut -d" " -f5`
  set delta = `echo $data | cut -d" " -f7`
  set delta = `echo "scale=1;${delta}/1.0" | bc -l`
  set dh = `echo $data | cut -d" " -f7`
  set yearh = `echo $data | cut -d" " -f9`
  set mchk = `grep  " ${stn} " $fn | grep -c "MONTHH"`
  set ychk = `grep  " ${stn} " $fn | grep -c "YEARH"`
  set star = ""
  if ($mchk > 0) then
      set star = "<font size=+1><font color=#ff0000>*</font></font>"
  endif
  if ($ychk > 0) then
      set star = "<font size=+1><font color=#ff0000>**</font></font>"
  endif
  set numy = `echo $data | cut -d" " -f10`
  set monthrech = `echo $data | cut -d" " -f13`
  set yearrech = `echo $data | cut -d" " -f14`
  set city = `grep "^$stn;" ../../extreme/stations.world | cut -d";" -f2`
  set cchk = `echo $city | cut -d"," -f4 | egrep -c "[a-z,A-Z]"`
  set comma = 4
  if ($cchk == 0) then
     set cchk = `echo $city | cut -d"," -f3 | egrep -c "[a-z,A-Z]"`
     set comma = 3
  endif
  if ($cchk == 0) then
     set cchk = `echo $city | cut -d"," -f2 | egrep -c "[a-z,A-Z]"`
     set comma = 2
  endif
  if ($cchk == 0) then
     set cchk = `echo $city | cut -d"," -f1 | egrep -c "[a-z,A-Z]"`
     set comma = 1
  endif
  set city2 = `echo $city | cut -d"," -f1,${comma}`
  set country = `grep "^$stn;" ../../extreme/stations.world | cut -d";" -f3`
  echo "<TR><td><font size=-2>${stn}</font></td><td><font size=-2>$city2</font></td><td><font size=-2>${country}</font></td><td bgcolor=#ff0000><font size=-2><font color=#ffffff><center>HIGH: TIE</center></font></font></td><td><font size=-2><center>${star}${temp}${star}</center></font></td><td><font size=-2><center>${rech}/${monthrech}</center></font></td><td><font size=-2><center>${delta}</center></font></td><td><font size=-2><center>$yearh</center></font></td><td><font size=-2><center>${numy}</center></font></td>" >> temp.php
end

set stns = `grep APPROACH $fn | grep HIGH | sort -r -n -k 7 |  cut -d" " -f2`
foreach stn ($stns)
  set data = `grep " $stn " $fn`
  set temp = `echo $data | cut -d" " -f3`
  set rech = `echo $data | cut -d" " -f5`
  set delta = `echo $data | cut -d" " -f7`
  set delta = `echo "scale=1;${delta}/1.0" | bc -l`
  set dh = `echo $data | cut -d" " -f7`
  set yearh = `echo $data | cut -d" " -f9`
  set numy = `echo $data | cut -d" " -f10`
  set monthrech = `echo $data | cut -d" " -f13`
  set yearrech = `echo $data | cut -d" " -f14`
  set city = `grep "^$stn;" ../../extreme/stations.world | cut -d";" -f2`
  set cchk = `echo $city | cut -d"," -f4 | egrep -c "[a-z,A-Z]"`
  set comma = 4
  if ($cchk == 0) then
     set cchk = `echo $city | cut -d"," -f3 | egrep -c "[a-z,A-Z]"`
     set comma = 3
  endif
  if ($cchk == 0) then
     set cchk = `echo $city | cut -d"," -f2 | egrep -c "[a-z,A-Z]"`
     set comma = 2
  endif
  if ($cchk == 0) then
     set cchk = `echo $city | cut -d"," -f1 | egrep -c "[a-z,A-Z]"`
     set comma = 1
  endif
  set city2 = `echo $city | cut -d"," -f1,${comma}`
  set country = `grep "^$stn;" ../../extreme/stations.world | cut -d";" -f3`
  echo "<TR><td><font size=-2>${stn}</font></td><td><font size=-2>$city2</font></td><td><font size=-2>${country}</font></td><td bgcolor=#ffff00><font size=-2><font color=#000000><center>HIGH: CLOSE</center></font></font></td><td><font size=-2><center>$temp</center></font></td><td><font size=-2><center>${rech}/${monthrech}</center></font></td><td><font size=-2><center>${delta}</center></font></td><td><font size=-2><center>$yearh</center></font></td><td><font size=-2><center>${numy}</center></font></td>" >> temp.php
end

echo "<TR><td bgcolor=#777777><td bgcolor=#777777><td bgcolor=#777777><td bgcolor=#777777><td bgcolor=#777777><td bgcolor=#777777><td bgcolor=#777777><td bgcolor=#777777><td bgcolor=#777777><TR>" >> temp.php
echo "<TR><td bgcolor=#777777><td bgcolor=#777777><td bgcolor=#777777><td bgcolor=#777777><td bgcolor=#777777><td bgcolor=#777777><td bgcolor=#777777><td bgcolor=#777777><td bgcolor=#777777><TR>" >> temp.php

set stns = `grep APPROACH $fn | egrep "[E,H,N] LOW" | sort -r -n -k 6 | cut -d" " -f2`
foreach stn ($stns)
  set data = `grep " $stn " $fn`
  set temp = `echo $data | cut -d" " -f3 | cut -c1-5`
  set recl = `echo $data | cut -d" " -f4 | cut -c1-5`
  echo $data 
  set delta = `echo $data | cut -d" " -f6`
  set delta = `echo "scale=1;${delta}/1.0" | bc -l`
  set dl = `echo $data | cut -d" " -f6`
  set yearl = `echo $data | cut -d" " -f8`
  set numy = `echo $data | cut -d" " -f10`
  set monthrecl = `echo $data | cut -d" " -f11`
  set yearrecl = `echo $data | cut -d" " -f13`
  set city = `grep "^$stn;" ../../extreme/stations.world | cut -d";" -f2`
  set cchk = `echo $city | cut -d"," -f4 | egrep -c "[a-z,A-Z]"`
  set comma = 4
  if ($cchk == 0) then
     set cchk = `echo $city | cut -d"," -f3 | egrep -c "[a-z,A-Z]"`
     set comma = 3
  endif
  if ($cchk == 0) then
     set cchk = `echo $city | cut -d"," -f2 | egrep -c "[a-z,A-Z]"`
     set comma = 2
  endif
  if ($cchk == 0) then
     set cchk = `echo $city | cut -d"," -f1 | egrep -c "[a-z,A-Z]"`
     set comma = 1
  endif
  set city2 = `echo $city | cut -d"," -f1,${comma}`
  set country = `grep "^$stn;" ../../extreme/stations.world | cut -d";" -f3`
  echo "<TR><td><font size=-2>${stn}</font></td><td><font size=-2>$city2</font></td><td><font size=-2>${country}</font></td><td bgcolor=#00ffff><font size=-2><font color=#000000><center>LOW: CLOSE</center></font></font></td><td><font size=-2><center>$temp</center></font></td><td><font size=-2><center>${recl}/${monthrecl}</center></font></td><td><font size=-2><center>${delta}</center></font></td><td><font size=-2><center>$yearl</center></font></td><td><font size=-2><center>${numy}</center></font></td>" >> temp.php
end


set stns = `grep TIE $fn | egrep "[E,H,N] LOW" | sort -r -n -k 6 | cut -d" " -f2`
foreach stn ($stns)
  set data = `grep " $stn " $fn`
  set temp = `echo $data | cut -d" " -f3`
  set recl = `echo $data | cut -d" " -f4`
  set delta = `echo $data | cut -d" " -f6`
  set delta = `echo "scale=1;${delta}/1.0" | bc -l`
  set mchk = `grep  " ${stn} " $fn | grep -c "MONTHL"`
  set ychk = `grep  " ${stn} " $fn | grep -c "YEARL"`
  set star = ""
  if ($mchk > 0) then
      set star = "<font size=+1><font color=#ff0000>*</font></font>"
  endif
  if ($ychk > 0) then
      set star = "<font size=+1><font color=#ff0000>**</font></font>"
  endif
  set dl = `echo $data | cut -d" " -f6`
  set yearl = `echo $data | cut -d" " -f8`
  set numy = `echo $data | cut -d" " -f10`
  set monthrecl = `echo $data | cut -d" " -f11`
  set yearrecl = `echo $data | cut -d" " -f13`
  set city = `grep "^$stn;" ../../extreme/stations.world | cut -d";" -f2`
  set cchk = `echo $city | cut -d"," -f4 | egrep -c "[a-z,A-Z]"`
  set comma = 4
  if ($cchk == 0) then
     set cchk = `echo $city | cut -d"," -f3 | egrep -c "[a-z,A-Z]"`
     set comma = 3
  endif
  if ($cchk == 0) then
     set cchk = `echo $city | cut -d"," -f2 | egrep -c "[a-z,A-Z]"`
     set comma = 2
  endif
  if ($cchk == 0) then
     set cchk = `echo $city | cut -d"," -f1 | egrep -c "[a-z,A-Z]"`
     set comma = 1
  endif
  set city2 = `echo $city | cut -d"," -f1,${comma}`
  set country = `grep "^$stn;" ../../extreme/stations.world | cut -d";" -f3`
  echo "<TR><td><font size=-2>${stn}</font></td><td><font size=-2>$city2</font></td><td><font size=-2>${country}</font></td><td bgcolor=#0000ff><font size=-2><font color=#ffffff><center>LOW: TIE</center></font></font></td><td><font size=-2><center>${star}${temp}${star}</center></font></td><td><font size=-2><center>${recl}/${monthrecl}</center></font></td><td><font size=-2><center>${delta}</center></font></td><td><font size=-2><center>$yearl</center></font></td><td><font size=-2><center>${numy}</center></font></td>" >> temp.php
end


set stns = `grep BROKEN $fn | egrep "[E,H,N] LOW" | sort -r -n -k 6 | cut -d" " -f2`
foreach stn ($stns)
  set data = `grep " $stn " $fn`
  set temp = `echo $data | cut -d" " -f3`
  set recl = `echo $data | cut -d" " -f4`
  set delta = `echo $data | cut -d" " -f6`
  set delta = `echo "scale=1;${delta}/1.0" | bc -l`
  set mchk = `grep  " ${stn} " $fn | grep -c "MONTHL"`
  set ychk = `grep  " ${stn} " $fn | grep -c "YEARL"`
  set star = ""
  if ($mchk > 0) then
      set star = "<font size=+1><font color=#ff0000>*</font></font>"
  endif
  if ($ychk > 0) then
      set star = "<font size=+1><font color=#ff0000>**</font></font>"
  endif
  set dl = `echo $data | cut -d" " -f6`
  set yearl = `echo $data | cut -d" " -f8`
  set numy = `echo $data | cut -d" " -f10`
  set monthrecl = `echo $data | cut -d" " -f11`
  set yearrecl = `echo $data | cut -d" " -f13`
  set city = `grep "^$stn;" ../../extreme/stations.world | cut -d";" -f2`
  set cchk = `echo $city | cut -d"," -f4 | egrep -c "[a-z,A-Z]"`
  set comma = 4
  if ($cchk == 0) then
     set cchk = `echo $city | cut -d"," -f3 | egrep -c "[a-z,A-Z]"`
     set comma = 3
  endif
  if ($cchk == 0) then
     set cchk = `echo $city | cut -d"," -f2 | egrep -c "[a-z,A-Z]"`
     set comma = 2
  endif
  if ($cchk == 0) then
     set cchk = `echo $city | cut -d"," -f1 | egrep -c "[a-z,A-Z]"`
     set comma = 1
  endif
  set city2 = `echo $city | cut -d"," -f1,${comma}`
  set country = `grep "^$stn;" ../../extreme/stations.world | cut -d";" -f3`
  echo "<TR><td><font size=-2>${stn}</font></td><td><font size=-2>$city2</font></td><td><font size=-2>${country}</font></td><td bgcolor=#000000><font size=-2><font color=#ffffff><center>LOW: BROKEN</center></font></font></td><td><font size=-2><center>${star}${temp}${star}</center></font></td><td><font size=-2><center>${recl}/${monthrecl}</center></font></td><td><font size=-2><center>${delta}</center></font></td><td><font size=-2><center>$yearl</center></font></td><td><font size=-2><center>${numy}</center></font></td>" >> temp.php
end

echo "<TR><td bgcolor=#00ff00><font size=-2>ID</font></td><td bgcolor=#00ff00><font size=-2>City</font></td><td bgcolor=#00ff00><font size=-2>Country</font></td><td bgcolor=#00ff00><font size=-2><center>Type and<BR>unofficial<BR>status</center></font></td><td bgcolor=#00ff00><font size=-2><center>Current<BR>Temp.<BR>(&deg;F)</center></font></td><td bgcolor=#00ff00><font size=-2><center>Unofficial<BR>Daily/Monthly<BR>Record (&deg;F)</center></font></td><td bgcolor=#00ff00><font size=-2><center>Margin of<BR>Daily<BR>Record (&deg;F)</center></font></td><td bgcolor=#00ff00><font size=-2><center>Year of<BR>Daily<BR>Record</center></font></td><td bgcolor=#00ff00><font size=-2><center>Database<BR>Length<BR>(years)</center></font></td>" >> temp.php
echo "</table>" >> temp.php
set chk = `egrep -c -i "(LOW|HIGH)" temp.php`
if ($chk == 0) then
   echo "<font color=#0000ff>None of the $scount stations is currently within 3 degrees of daily records.</font>" > temp.php
endif
echo "</center>" >> temp.php
mv temp.php ${ayear}${amonth}/${sdtg}.html
endif
end
