#!/bin/sh
#========================================================
# General GMT script for CHIMERE output results plots
#     version for horizontal maps
#     Used by chim2fig fortran program
#     Last revision: January 2003
#========================================================
mapfile=$1
vmin=$2
vmax=$3
pas=$4
latmin=$5
latmax=$6
longmin=$7
longmax=$8
xtick=$9
ytick=${10}
color=${11}
affi=${12}
pas2=${13}
of=${14}
param=${15}
plot_wind=${16}
c2ftmp=${17}

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

mapbase=`echo ${mapfile}|sed s/.asc$//`


pars="-JX15d/12.1d  -R"$longmin"/"$longmax"/"$latmin"/"$latmax
#box="-Bf"${xtick}"a"${ann}"/f"${ytick}"a"${ann}"WSne"
box="-B"${xtick}"/"${ytick}"WSne"
psca="-D15.2/6./12./0.4"

echo ${longmax} ${latmax} ${longmin} ${latmin} > .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  10 m/s" > "text_wind"
   print titlon " " titlat " 13 0 0 TC" > ".txt1"
}' ".test"

palette="PAL"
palette2="PALBIS"

# Making title with the data file header (1st line only)
gawk '{
  if(FNR==1){printf("%-s %-s %-s %-s %-s %-s %-s %-s \n",$2,$3,$4,$5,$6,$7,$8,$9) > ".txt2"}
}' $mapfile
# concatenation de la chaine de caractere et de sa position sur l'image
paste .txt1 .txt2 > .txt
       
#pscoast ${pars} ${box} -A10 -Dc -W1  -K -P  > ${mapbase}.ps

pscoast ${pars} ${box} -S205/255/255 -A10 -Df -W1  -K -P  > ${mapbase}.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/10.) >> "wind.data"
       }' $mapfile
       
#echo "    o main colortable step " ${pas}
#echo "    o secondary step       " ${pas2}

if [ $color = "1" ]; then
   makecpt -Crainbow -T${vmin}/${vmax}/${pas}  > $palette
   makecpt -Crainbow -T${vmin}/${vmax}/${pas2}  > $palette2
fi

if [ $color = "2" ]; then
   makecpt -Cgray -T${vmin}/${vmax}/${pas} -I > $palette
fi
if [ $color = "3" ]; then
   makecpt -Cgray -T${vmin}/${vmax}/${pas} -I > $palette
fi

suppr=1
if [ $suppr = "1" ]; then
   gawk '{
       if(FNR<=3 || $1 ~ "B" || $1 ~ "F" || $1 ~ "N"){
          print $0 >> ".pal2"
       }
       if(FNR==4){
          min=$1
          cmin1=255
          cmin2=255
          cmin3=255
	  max=$5
          cmax1=255
          cmax2=255
          cmax3=255
       printf("%-f %i %i %i %-f %i %i %i\n",min,cmin1,cmin2,cmin3,max,cmax1,cmax2,cmax3) >> ".pal2"
       }
       if(FNR>=5 && $1 !~ "B" && $1 !~ "F" && $1 !~ "N" && $1 !~ "#"){
          min=$1
	  cmin1=$2
	  cmin2=$3
	  cmin3=$4
	  max=$5
	  cmax1=$6
	  cmax2=$7
	  cmax3=$8
       printf("%-f %i %i %i %-f %i %i %i\n",min,cmin1,cmin2,cmin3,max,cmax1,cmax2,cmax3) >> ".pal2"
       }
   }' $palette
\mv .pal2 $palette
fi

if [ $color = "1" ]; then
    if [ -s palettes/${param}.cpt ]; then
	palette=palettes/${param}.cpt
	pscontour $mapfile  ${pars} ${box} -C$palette -I -O -P -K -Af1  >> ${mapbase}.ps
	psscale -C$palette  ${psca} -O -K -P  >> ${mapbase}.ps
    else
	pscontour $mapfile  ${pars} ${box} -C$palette -I -O -P -K -Af1  >> ${mapbase}.ps
	psscale -C$palette -B${pas2} ${psca} -O -K -P  >> ${mapbase}.ps
    fi
fi

if [ $color = "2" ]; then
   pscontour $mapfile  ${pars} ${box} -C$palette -I -O -P -K -Af1  >> ${mapbase}.ps
   psscale -C$palette ${psca} -B${pas} -L -O -K -P  >> ${mapbase}.ps
fi
if [ $color = "3" ]; then
   pscontour $mapfile  ${pars} ${box} -C$palette -W2/0 -O -P -K -Af1  >> ${mapbase}.ps
fi
   
# plot coastlines
pscoast ${pars} ${box}  -W2/0 -Df -O  -K -P >> ${mapbase}.ps
# plot wind vectors 
if [ ${plot_wind} = "1" ]; then  
    psxy wind.data ${pars}  -Sv0.02/0.1/0.05 -G150 -M -K -O -P  >> ${mapbase}.ps
    psxy boxwind ${pars}  -M -W5/0/0/0 -O -P  -K -G255 >> ${mapbase}.ps
    psxy wind2.data ${pars}  -Sv0.02/0.1/0.05  -O -P -K  -G0 >> ${mapbase}.ps
    pstext text_wind ${pars} -O -P -K >> ${mapbase}.ps
fi
pstext .txt ${pars} -W250/200/200  -O -P -K -N  >>  ${mapbase}.ps
# Names of stations
pstext statnames ${pars} -O -P  >>  ${mapbase}.ps

\rm wind.data
# Known GMT bug in the set-up of the post-scripts files
gawk '{
       if($0 !~ "/ImagingBBox")print $0 >> "test.ps"
       }' ${mapbase}.ps
\mv test.ps ${mapbase}.ps
#\rm .nbline

#echo " > " ${mapbase}".eps"
ps2epsi ${mapbase}.ps ${mapbase}.eps
\rm ${mapbase}.ps

if [ $affi = "1" ]; then
    ghostview  ${mapbase}.eps &
fi

if [ $of = "2" ]; then
    convert ${mapbase}.eps ${mapbase}.gif
fi
if [ $of = "3" ]; then
    convert ${mapbase}.eps ${mapbase}.png
fi
