#!/bin/csh -f

set datadir = '../../csv'

set FC = /usr/bin/gfortran

set nobs = 30
set ndat = 121		

set prname = P
set psname = .csv

set NDATA = 27

set dTdtMIN = -5
set dTdtMAX =  5

$FC dTdt.f
mv a.out dTdt.out
$FC dTdtmean.f
mv a.out dTdtmean.out


@ n = 1
rm datafile dTdtall.dat
while ( $n <= $nobs )
  if ( $n < 10 ) then
      ln -s ${datadir}/${prname}0${n}${psname} datafile
    else
      ln -s ${datadir}/${prname}${n}${psname}  datafile
  endif

  ./dTdt.out > dTdt.dat
  cat dTdt.dat >> dTdtall.dat

gnuplot <<EOF
set term png
set out "temp.png"
unset key
unset colorbox
set xdata time
set timefmt "%H:%M:%S"
set title "Temperature"
set xrange ["09:30:00":"11:30:00"]
set format x "%H:%M"
set yrange [${dTdtMIN}:${dTdtMAX}]
set title "n = ${n}"
set xlabel "Time"
set ylabel "Temperature change rate (degree C / min)"
set palette rgbformulae 33,13,10
plot n=${n}, "dTdt.dat" using 1:2 w p pt 5 ps 0.4 lc palette frac (n-1)/(${nobs}-1.0)
EOF

  if ( $n < 10 ) then
      mv temp.png ${prname}0${n}.png
    else
      mv temp.png ${prname}${n}.png
  endif

  @ n = $n + 1
  rm datafile dTdt.dat
end

convert -delay 50 ${prname}*.png dTdt.gif
rm ${prname}*.png



./dTdtmean.out > dTdtmean.dat

gnuplot <<EOF
set term png
set out "dTdtall.png"
unset key
unset colorbox
set xdata time
set timefmt "%H:%M:%S"
set title "Temperature"
set xrange ["09:30:00":"11:30:00"]
set format x "%H:%M"
set yrange [${dTdtMIN}:${dTdtMAX}]
set xlabel "Time"
set ylabel "Temperature change rate (degree C / min)"
plot "dTdtall.dat" using 1:2 w p pt 5 ps 0.4 lc rgb 'red'
EOF

gnuplot <<EOF
set term png
set out "dTdtmean.png"
unset key
unset colorbox
set xdata time
set timefmt "%H:%M:%S"
set title "Mean of all station"
set xrange ["09:30:00":"11:30:00"]
set format x "%H:%M"
set xlabel "Time"
set ylabel "Temperature change rate (degree C / min)"
set yrange [-0.6:0.6]
plot 'dTdtmean.dat' u 1:3:(1.96*\$4/((${NDATA}-1)**0.5)) with errorbars pt 5 ps 0.4 lc rgb "red"
EOF
