#! /bin/bash
# Last edited on 2009-12-06 01:22:36 by stolfi

# Plots reformatted raw data around a counter's glitch.
# Assumes that input is "{TIME} {SZ}"
# Also plots two shifted exponentials.

WD="$1"; shift # Plot width.

D="$1"; shift  # Reference day.
R="$1"; shift  # Rate of increase ({day^{-1}}).

A1="$1"; shift # Count (1) on day {D}.
K1="$1"; shift # Constant (1) on day {D}.

A2="$1"; shift # Count (2) on day {D}.
K2="$1"; shift # Constant (2) on day {D}.

datafile="$1"; shift

echo "${0##*/} datafile = ${datafile}" 1>&2 

epsfile="/tmp/$$.eps"

gnuplot <<EOF
set term postscript eps color solid "TimesRoman" 24
set size ${WD},1.5
set output "${epsfile}"

D=${D}
R=${R}

A1=${A1}
K1=${K1}

A2=${A2}
K2=${K2}

set key left samplen 1.0

set xzeroaxis
set xtics 10
set mxtics 10
set grid xtics

#set yrange [30000:150000]
set ytics format "%6.0f"

sper=28
year(i)=(column(i)/365.25 + 2001)
plot \
  "${datafile}" using 1:(A1 + (K1/R/sper)*(exp(R*(column(1)-D))-1)) notitle with lines lt rgb '#ee4411', \
  "${datafile}" using 1:(A2 + (K2/R/sper)*(exp(R*(column(1)-D))-1)) notitle with lines lt rgb '#aa33dd', \
  "${datafile}" using 1:(column(5)) notitle with points pt 7 ps 0.75 lc rgb '#1144ee'
quit
EOF

cat ${epsfile}
gv ${epsfile}
rm -f ${epsfile}
