#! /bin/tcsh -f

set id1 = $1
set id2 = $2
set dir = ${id2}_${id1}
set sen1 = `../scripts/get_sensor.csh $id1`
set sen2 = `../scripts/get_sensor.csh $id2`
set sennam = ${sen2}-vs-${sen1}
set testid = $dir
set sdir = images
set subset = `cat ../common/global.lis ../common/regional.lis ../common/zonal.lis | cut -c"1-6"`

set period = "4-Day"
echo $id1 | grep "?r" >> /dev/null
if ($status == 0) set period = "8-Day"
echo $id1 | grep 'w$' >> /dev/null
if ($status == 0) set period = "8-Day"
echo $id2 | grep "mr" >> /dev/null
if ($status == 0) set period = "Monthly"
echo $id2 | grep 'm$' >> /dev/null
if ($status == 0) set period = "Monthly"

foreach t ($subset)
foreach p (chlor_a) # chl_giop)

    echo $p | grep "^chl" >> /dev/null
    if ($status == 0) then 
        set range="0.01-1 mg/m^3"
        set drange="-0.1 - 0.1 mW/cm^2/um/sr"
        set prodn="Chlorophyll"
        set file = "${t}_chlor_a_images.html"
    endif
    set rrange="0.5 - 1.5"

    set title = "$sen1 & $sen2 $period $t Subset $p Images"
    if ($t == "deep") then 
        set title = "$sen1 & $sen2 $period Deep-Water $p Image"
    endif
    if ($t == "olig") then 
        set title = "$sen1 & $sen2 $period Oligotrophic $p Image"
    endif
    if ($t == "meso") then
        set title = "$sen1 & $sen2 $period Mesotrophic $p Images"
    endif
    if ($t == "eutr") then
        set title = "$sen1 & $sen2 $period Eutrophic $p Images"
    endif

    echo "<html>" > $file
    echo "<head>" >> $file
    echo "<title>$title</title>" >> $file
    echo "</head>" >> $file
    echo "<center>" >> $file
    echo "<body>" >> $file
    echo "<h1>$title</h1>" >> $file

    echo "<table>" >> $file

    echo "<tr>" >> $file
    echo "<td align=center><b>$id1</b> (${range})</td>" >> $file
    echo "<td align=center><b>$id2</b> (${range})</td>" >> $file
    echo "</tr>" >> $file
    echo "<tr>" >> $file
    echo "<td align=center><img src=../common/colorbar.small.jpg></td>" >> $file
    echo "<td align=center><img src=../common/colorbar.small.jpg></td>" >> $file
    echo "</tr>" >> $file

    set files = `ls -1 ${sdir}/${id1}*${t}_chl*.jpg ${sdir}/${id1}*${t}_Chl*.jpg | grep -v ".small."`
    foreach f ($files)
        set x = `basename $f | cut -d_ -f2`
        set y = `basename $x | cut -c1-4`
        set d = `basename $x | cut -c5-7`
        echo "<tr>" >> $file
        echo "<td>$y $d</td>" >> $file
        echo "<td>$y $d</td>" >> $file
        echo "</tr>" >> $file
        echo "<tr>" >> $file
        set prod = `echo $f | cut -d_ -f4-5 | cut -d. -f1`
        echo $y $d $prod
        set sf   = `ls -1 ${sdir}/${id1}_${y}${d}*${t}_${prod}.jpg`
        set mf   = `ls -1 ${sdir}/${id2}_${y}${d}*${t}_${prod}.jpg`
        set sftn = `echo $sf | sed "s/jpg/small.jpg/g"`
        set mftn = `echo $mf | sed "s/jpg/small.jpg/g"`
        if ($t == 'olig' || $t == 'global' || $t == 'deep' || $t == 'eutr' || $t == 'meso') then
            reduce_jpg.csh 400 $sf
            reduce_jpg.csh 400 $mf
        else
            ln -s `basename $sf` $sftn
            ln -s `basename $mf` $mftn
        endif
        echo "<td><a href=${sf}><img src=${sftn}></a></td>" >> $file
        echo "<td><a href=${mf}><img src=${mftn}></a></td>" >> $file
        echo "</tr>" >> $file
    end
    echo "</table>" >> $file
end
end

cd ..

exit 0



