#!/bin/csh

set id1   = $1
set id2   = $2
set ofile = $3
set prods1 = "`ls aplots/${id1}*deep* | cut -d_ -f3-5 | cut -d. -f1 | sort | uniq`"
set prods2 = "`ls aplots/${id2}*deep* | cut -d_ -f3-5 | cut -d. -f1 | sort | uniq`"
echo $prods1
echo $prods2
set sens1  = "`../scripts/get_sensor.csh $id1`"
set sens2  = "`../scripts/get_sensor.csh $id2`"
set regs = "deep olig meso eutr `ls aplots/${id1}*chlor_a* | cut -d_ -f2 | grep -v 'repeat\|deep\|_olig\|meso\|eutr' | uniq` `ls aplots/${id1}*bbp_443* | cut -d_ -f2 | grep -v 'repeat\|deep\|_olig\|meso\|eutr' | uniq`"
set prodpair = `../scripts/match_prods.csh "$prods1" "$prods2"`
#echo $prods1
#echo
#echo $prods2
echo
echo $prodpair
echo $regs
echo

echo "<html>" > ${ofile}
echo "" >> ${ofile}
echo "<head>" >> ${ofile}
echo "    <title>$sens1 and $sens2 Trends</title>" >> ${ofile}
echo "  </head>" >> ${ofile}
echo "" >> ${ofile}
echo "<CENTER>" >> ${ofile}
echo "" >> ${ofile}
echo "  <body>" >> ${ofile}
echo "    <h1>$sens1 ($id1) and $sens2 ($id2) Trends</h1>" >> ${ofile}
echo "    <h2>Global Subsets</h2>" >> ${ofile}
echo "    Derived from Binned Level-3<br>" >> ${ofile}
echo "<p>" >> ${ofile}
echo "<TABLE width="100%">" >> ${ofile}
echo "  <TBODY>" >> ${ofile}
    echo "<TR>" >> ${ofile}
      echo "<TD align="CENTER"><B>Product</B></TD>" >> ${ofile}
      echo "<TD align="CENTER" colspan=2><B>Annual Cycle (olig, deep, meso)</B></TD>" >> ${ofile}
      foreach rid ($regs)
         set reg = `../scripts/get_subname.csh $rid`
         echo "<TD align="CENTER" colspan=2><B>$reg Trend</B></TD>" >> ${ofile}
      end
    echo "</TR>" >> ${ofile}

    foreach p ($prodpair) 
        set p1 = `echo $p | cut -d"," -f1` 
        set p2 = `echo $p | cut -d"," -f2` 
    echo "<TR>" >> ${ofile}
      echo "<TD align=Center><b>${p1}<br>${p2}<b></TD>" >> ${ofile}
      echo "<TD align=CENTER><a href=aplots/${id1}_repeat_${p1}.png><IMG src=aplots/${id1}_repeat_${p1}.small.png></TD>" >> ${ofile}
      echo "<TD align=CENTER><a href=aplots/${id2}_repeat_${p2}.png><IMG src=aplots/${id2}_repeat_${p2}.small.png></TD>" >> ${ofile}
      foreach rid ($regs)
         set reg = `../scripts/get_subname.csh $rid`
         echo "<TD align=CENTER><a href=aplots/${id1}_${rid}_${p1}.png><IMG src=aplots/${id1}_${rid}_${p1}.small.png></TD>" >> ${ofile}
         echo "<TD align=CENTER><a href=aplots/${id2}_${rid}_${p2}.png><IMG src=aplots/${id2}_${rid}_${p2}.small.png></TD>" >> ${ofile}
      end
    echo "</TR>" >> ${ofile}
end

echo "</TBODY>" >> ${ofile}
echo "</TABLE>" >> ${ofile}

echo "" >> ${ofile}
echo "<hr>" >> ${ofile}
echo "" >> ${ofile}
echo "</CENTER>" >> ${ofile}
echo "<address><a href=mailto:bryan.a.franz@nasa.gov>Bryan A. Franz</a></address>" >> ${ofile}
echo "</body>" >> ${ofile}
echo "</html>" >> ${ofile}


