#!/bin/sh
prfile=${1}
xmin=${2}
xmax=${3}
xtick=${4}
ytick=${5}
ymin=${6}
ymax=${7}
color=${8}
affi=${9}
graphform=${10}
c2ftmp=${11}

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

prbase=`echo ${prfile}|sed s/.asc$//`

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

echo ${xmax} ${ymax} ${xmin} ${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"}
}' $prfile
gawk '{
  if(FNR==2){printf("%-s \n",$3) > ".txt3"}
}' $prfile

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


paste .txt1 .txt2 .txt3> .txt
paste .hdr1 .hdr2 > .hdr

       
psbasemap ${pars} ${box} -P -K  > $prbase.ps


psxy $prfile  ${pars} ${box}  -O -P -K   >> $prbase.ps

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

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

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

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