#!/bin/sh
date=$1
vmin=$2
vmax=$3
pas=$4
longmin=$5
longmax=$6
latmin=$7
latmax=$8
xtick=$9
ytick=${10}
color=${11}
affi=${12}
graphform=${13}
date2=${14}

pars="-JX15/12.1  -R"$longmin"/"$longmax"/"$latmin"/"$latmax
box="-Bfa"${xtick}"/fa"${ytick}"WSne"
psca="-D15.2/6./12./0.4"

echo ${longmax} ${latmax} ${longmin} ${latmin} > .test
gawk  '{
# regional
   if($1-$3 <= 7){
      dx1=0.3
      dx2=0.13*200.
      dx3=0.05
      dx4=0.04*200.
      dx5=0.08
      titlat=$2-200.
      titlon=$3+0.1
   }
# continental
   if($1-$3 > 7){
      dx1=3
      dx2=1.5*150.
      dx3=0.5
      dx4=0.5*150.
      dx5=0.8*150.
      titlat=$2-200.
      titlon=$3+1.
   }
   bxmin=$1-dx1
   bxmax=$1
   bymin=$2-dx2
   bymax=$2
   bxarrow=bxmin+dx3
   byarrow=bymin+dx4
   bytext=bymin+dx5
   print bxmax " " bymin >  "boxwind"
   print bxmax " " bymax >> "boxwind"
   print bxmin " " bymax >> "boxwind"
   print bxmin " " bymin >> "boxwind"
   print bxmax " " bymin >> "boxwind"
   print bxarrow "  " byarrow "  0. 1." > "wind2.data"
   print bxarrow "  " bytext  "  11 0 0 0  5 m/s" > "text_wind"
   print titlon " " titlat " 13 0 0 0" > ".txt1"
}' ".test"

palette=".pal"
palette2=".pal2"

# construction du titre a partir de la 1ere ligne du fichier
gawk '{
  if(FNR==1){printf("%-s %-s %-s %-s %-s %-s %-s %-s \n",$2,$3,$4,$5,$6,$7,$8,$9) > ".txt2"}
}' $date
# concatenation de la chaine de caractere et de sa position sur l'image
paste .txt1 .txt2 > .txt
       
pscoast ${pars} ${box} -S255/255/255WS -W0  -K -P  > $date.ps

gawk '{
       ff=sqrt($4*$4+$5*$5)
       dir=atan2($5,$4)
       if(dir >= 0.)dir2=dir/3.14159*180.
       if(dir < 0.)dir2=360.+dir/3.14159*180.
       printf("%-f %-f %f %-f \n",$1,$2,dir2,ff/5.) >> "wind.data"
       }' $date
 
gawk '{
       if($1 != 0. && $2 != 0.){
       printf("%-f %-f %f %-f \n",$1,$2,$3,$4) >> "pol.data"
       }
}' $date


if [ $color = "1" ]; then
   makecpt -Crainbow -T${vmin}/${vmax}/${pas}  > $palette
   pscontour $date  ${pars} ${box} -C$palette -I -O -P -K -Af1  >> $date.ps
   psscale -C$palette ${psca} -B${pas} -L -O -K -P  >> $date.ps
fi
if [ $color = "2" ]; then
   makecpt -Cgray -T${vmin}/${vmax}/${pas} -I > $palette
   pscontour $date  ${pars} ${box} -C$palette -I -O -P -K -Af1  >> $date.ps
   psscale -C$palette ${psca} -B${pas} -L -O -K -P  >> $date.ps
fi
if [ $color = "3" ]; then
   makecpt -Cgray -T${vmin}/${vmax}/${pas} -I > $palette
   pscontour $date  ${pars} ${box} -C$palette  -W3/0 -O -P -K -Af10  >> $date.ps
fi
   
# palette du relief uniquement
makecpt -Crainbow -T0./100./10.  > $palette2
psxy $date2  ${pars} ${box} -G150 -M  -O -P  -K  >> $date.ps

psxy wind.data ${pars}  -Sv0.02/0.1/0.05 -G150 -M -K -O -P  >> $date.ps

# Figure title
pstext .txt ${pars} -W245  -O -P -K >>  $date.ps
# echelle du vecteur vent
psxy boxwind ${pars}  -M -W5/0/0/0 -O -P  -K -G255 >> $date.ps
psxy wind2.data ${pars}  -Sv0.02/0.1/0.05  -O -P -K  -G0 >> $date.ps
pstext text_wind ${pars} -O -P -K >> $date.ps

psxy statlines ${pars} -W5/0/0/0 -O -P -M -K  >>  $date.ps
pstext statnames ${pars} -O -P   >>  $date.ps

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

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

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


