#!/bin/sh
#========================================================
# General GMT script for CHIMERE output results plots
#     version for horizontal maps
#     Used by chim2fig fortran program
#     Last revision: January 2003
#========================================================
# reading input parameters
date=$1
vmin=$2
vmax=$3
pas=$4
latmin=$5
latmax=$6
longmin=$7
longmax=$8
xtick=$9
ytick=${10}
color=${11}
affi=${12}
graphform=2

pars="-JX15/12.1  -R"$longmin"/"$longmax"/"$latmin"/"$latmax
box="-Bfa"${xtick}"/fa"${ytick}"WSne"
psca="-D15.2/6./12./0.4"
# Making locations of text frames
echo ${longmax} ${latmax} ${longmin} ${latmin} > .test
gawk  '{
# regional
   if($1-$3 <= 7){
      dx1=0.3
      dx2=0.13
      dx3=0.05
      dx4=0.04
      dx5=0.08
      titlat=$2-0.1
      titlon=$3+0.1
   }
# continental
   if($1-$3 > 7){
      dx1=3
      dx2=1.5
      dx3=0.5
      dx4=0.5
      dx5=0.8
      titlat=$2-1.
      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"

# 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"}
}' $date
# cat of text and position into the plot
paste .txt1 .txt2 > .txt
       
pscoast ${pars} ${box} -S205/255/255WS -A10 -Df -W1  -K -P  > $date.ps
# calculation of wind vectors
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
# Choice for color, black and white or iso-lines
if [ $color = "1" ]; then
   makecpt -Crainbow -T${vmin}/${vmax}/${pas}  > $palette
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
echo "   o Changing color table to avoid magenta"
   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
   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
   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
   pscontour $date  ${pars} ${box} -C$palette  -W3/0 -O -P -K -Af10  >> $date.ps
fi
# plot wind vectors   
psxy wind.data ${pars}  -Sv0.02/0.1/0.05 -G150 -M -K -O -P  >> $date.ps
# plot coastlines
pscoast ${pars} ${box}  -W2/0 -Df -O  -K -P >> $date.ps
# Figure title
pstext .txt ${pars} -W245  -O -P -K >>  $date.ps
# Wind vector scale
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
# Names of stations
pstext statnames ${pars} -O -P   >>  $date.ps

\rm wind.data
# Known GMT bug in the set-up of the post-scripts files
gawk '{
       if($0 !~ "/ImagingBBox")print $0 >> "test.ps"
       }' $date.ps
\mv test.ps $date.ps
echo "-----------------------------"
echo " Output file = " $date".eps"
ps2epsi $date.ps $date.eps
\rm $date.ps
# choice between .EPS or .GIF output format
if [ $graphform = "1" ]; then
   if [ $affi = "1" ]; then
      ghostview  $date.eps &
   fi
fi
if [ $graphform = "2" ]; then
    echo "conversion of EPS file into GIF format"
    convert $date.eps $date.gif
    \rm $date.eps
fi


