#!/bin/bash

echo "Fitting thermal model(s) to " $1 data
echo "3.6/V    D      eD    pV    epV  eta   eeta  X^2"  
#     1.0 o> 119.404 4.471 0.034 0.003 0.791 0.037 30.4

for virratio in 1.0 2.0 3.0
do 
   fittm -n 0 -V $virratio < $1 > .fit
   D=`grep "o>" .fit | awk '{print $2}'`
   pV=`grep "o>" .fit | awk '{print $4}'`
   eta=`grep "o>" .fit | awk '{print $6}'`
   geo=`head -1 $1 | awk '{print $7, $8, $9}'`

   echo $D $pV $eta $geo | tmflux -V $virratio -r -l 1 24 0.1 | jy2w >.flux$virratio
   awk 'BEGIN{N=0}{if (N>0) print $0; N++;}' $1 | jy2w >.obs
   res=`grep "o>" .fit`
   echo $virratio $res
done

echo 'set term postscript enhanced "Helvetica" 18' >.gnuplot
echo 'set out "test.ps"'>>.gnuplot
echo "set size 0.7,0.7" >>.gnuplot
echo 'set xlabel "Wavelength (um)"' >>.gnuplot
echo 'set ylabel "Flux (W/m^2/micron)"'>>.gnuplot
echo 'set logscale y'>>.gnuplot
echo 'set xrange [1:24]'>>.gnuplot
echo 'set key right bottom font "Helvetica,14"' >>.gnuplot
#echo 'set title "$1" font "Times-Roman,20"'>>.gnuplot
echo 'plot ".obs" u 1:2:3 w error lw 4 tit "Data", \' >>.gnuplot
echo      '".flux1.0" w l lw 2 tit "nIR/V=1.0",\'>>.gnuplot
echo      '".flux2.0" w l lw 2 tit "nIR/V=2.0", \' >>.gnuplot
echo      '".flux3.0" w l lw 2 tit "nIR/V=3.0"' >>.gnuplot
echo 'set out'>>.gnuplot
gnuplot .gnuplot

echo run gv test.ps to see the plot
rm .fit .flux1.0 .flux2.0 .flux3.0 .gnuplot