#!/bin/sh
crfile=${1}
vmin=${2}
vmax=${3}
pas=${4}
pas2=${5}
xmin=${6}
xmax=${7}
zmin=${8}
zmax=${9}
xtick=${10}
ytick=${11}
color=${12}
affi=${13}
graphform=${14}
crfile2=${15}
param=${16}
plot_wind=${17}
c2ftmp=${18}
nlev=${19}

plot_wind=0

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

crbase=`echo ${crfile}|sed s/.asc$//`


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

echo ${xmax} ${zmax} ${xmin} ${zmin}  > .test

gawk  '{
   dx1=0.1*($1-$3)
   dx2=0.05*($2-$4)
   dx3=0.007*($1-$3)
   dx4=0.007*($2-$4)
   dx5=0.015*($2-$4)
   titlat=$4-0.07*($2-$4)
   titlon=($3+$1)/2.
   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  20 m/s" > "text_wind"
   print titlon " " titlat " 13 0 0 TC" > ".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"}
}' $crfile
paste .txt1 .txt2 > .txt
       
#### pscoast ${pars} ${box} -S255/255/255WS -W0  -K -P  > $crbase.ps

# print every 10th wind arrow group
gawk -v nlev=${nlev} '{ 
        if (((FNR/nlev)%10>=0) && ((FNR/nlev)%10<1)) {
           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/20.) > "wind.data" 
           }
       }' $crfile
 
gawk '{
        if($1 != 0. && $2 != 0.){
        printf("%-f %-f %f %-f \n",$1,$2,$3,$4) >> "pol.data"
        }
      }' $crfile


if [ $color = "1" ]; then
    if [ -s palettes/${param}.cpt ]; then
	palette=palettes/${param}.cpt
	pscontour $crfile  ${pars} ${box} -C$palette -I -P -K -Af1  >> $crbase.ps
	psscale -C$palette ${psca}  -O -K -P -L >> $crbase.ps
    else
	makecpt -Crainbow -T${vmin}/${vmax}/${pas}  > $palette
	makecpt -Crainbow -T${vmin}/${vmax}/${pas2}  > $palette2
	pscontour $crfile  ${pars} ${box} -C$palette -I  -P -K -Af1  >> $crbase.ps
	psscale -C$palette ${psca} -B${pas2} -O -K -P  >> $crbase.ps
    fi
fi
if [ $color = "2" ]; then
    makecpt -Cgray -T${vmin}/${vmax}/${pas} -I > $palette
    pscontour $crfile  ${pars} ${box} -C$palette -I -P -K -Af1  >> $crbase.ps
    psscale -C$palette ${psca} -B${pas} -L -O -K -P  >> $crbase.ps
fi
if [ $color = "3" ]; then
    makecpt -Cgray -T${vmin}/${vmax}/${pas} -I > $palette
    pscontour $crfile  ${pars} ${box} -C$palette  -W3/0  -P -K -Af10  >> $crbase.ps
fi

# palette du relief uniquement
#makecpt -Crainbow -T0./100./10.  > $paletteR
psxy $crfile2  ${pars} ${box} -G150 -M -O -P  -K  >> $crbase.ps

# Plot wind vectors
if [ ${plot_wind} == "1" ]; then
    psxy wind.data ${pars}  -Sv0.02/0.1/0.05 -G150 -M -K -O -P  >> $crbase.ps
    psxy boxwind ${pars}  -M -W5/0/0/0 -O -P  -K -G255 >> $crbase.ps
    psxy wind2.data ${pars}  -Sv0.02/0.1/0.05  -O -P -K  -G0 >> $crbase.ps
    pstext text_wind ${pars} -O -P -K >> $crbase.ps
fi

# Figure title
pstext .txt ${pars} -W245  -O -P -K -N >>  $crbase.ps

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

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

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

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




