#!/bin/sh
tsfile=${1}
ymin=${2}
ymax=${3}
xtick=${4}
ytick=${5}
tmin=${6}
tmax=${7}
color=${8}
affi=${9}
graphform=${10}
param=${11}
stname=${12}
c2ftmp=${13}

cd ${c2ftmp}

tsbase=`echo ${tsfile}|sed s/.asc$//`


#xann=`echo ${xtick}|gawk '{print 10*$1}'`

pars="-JX15/12.1  -R"$tmin"/"$tmax"/"$ymin"/"$ymax
box="-B${xtick}/${ytick}WSne"
psca="-D15.2/6./12./0.4"

echo ${tmax} ${ymax} ${tmin} ${ymin}  > .test

gawk  '{
   dt1=0.1*($1-$3)
   dy2=0.05*($2-$4)
   dt3=0.007*($1-$3)
   dy4=0.007*($2-$4)
   dy5=0.015*($2-$4)
   titlat=$4-0.07*($2-$4)
   titlon=($3+$1)/2.
   hdrlat= $2-dy5
   print titlon " " titlat " 13 0 0 TC" > ".txt1"
   print titlon " " hdrlat " 13 0 0 TC" > ".hdr1"
}' ".test"

# construction du titre a partir des 3 1eres lignes du fichier
gawk '{
  if(FNR==1){printf("%-s %-s %-s \n",$2,$3,$4) > ".txt2"}
}' $tsfile
gawk '{
  if(FNR==2){printf("%-s %-s %-s %-s \n",$2,$3,$4,$5) > ".txt3"}
}' $tsfile

gawk '{
  if(FNR==3){printf("%-s \n",$4) > ".hdr2"}
}' $tsfile

gawk '{
  if(FNR==4){printf(" LEV=%-s \n",$4) > ".hdr3"}
}' $tsfile


paste .txt1 .txt2 .txt3> .txt
paste .hdr1 .hdr2 .hdr3> .hdr
       
psbasemap ${pars} ${box} -P -K  > $tsbase.ps

psxy $tsfile  ${pars} ${box}  -O -P -K   >> $tsbase.ps

# Figure title
pstext .txt ${pars} -W245  -O -P -K -N >>  $tsbase.ps
# figure header
pstext .hdr ${pars} -W245  -O -P -K -N >>  $tsbase.ps

# Bug dans le set-up des post-scripts
gawk '{
       if($0 !~ "/ImagingBBox")print $0 >> "test.ps"
       }' $tsbase.ps
\mv test.ps $tsbase.ps
#\rm .nbline
#echo "-----------------------------"
#echo " Output file = " $tsbase".eps"
ps2epsi $tsbase.ps $tsbase.eps
\rm $tsbase.ps

if [ $graphform = "1" ]; then
    if [ $affi = "1" ]; then
	ghostview  -font 6x13 $tsbase.eps &
    fi
fi

if [ $graphform = "2" ]; then
#    echo " conversion of EPS file into GIF format"
    convert $tsbase.eps $tsbase.gif
    display  $tsbase.gif &
fi



