#!/usr/bin/env bash


WORK_DIR=/home/marcos/ptype
proy="033 036 042 060 081"

for PROY in ${proy} ; do
  echo "${PROY}"
  # Definiciones.
  gribFile=${WORK_DIR}/2018030800_${PROY}_LL
  ncFile=${gribFile}.nc
  psFile=${gribFile}.ps

  if test ! -s ${ncFile} ; then

    # Convertimos a NetCDF.
    grib_to_netcdf ${gribFile} -o ${ncFile}.tmp

    # Cambiamos de [-180,180) a [0,360).
    cdo griddes ${ncFile}.tmp > ${WORK_DIR}/grdInfoTmp
    cat ${WORK_DIR}/grdInfoTmp | replace "xfirst    = -180" "xfirst    = 0" > ${WORK_DIR}/grdInfo
    rm ${WORK_DIR}/grdInfoTmp
    cdo setgrid,${WORK_DIR}/grdInfo ${ncFile}.tmp ${ncFile}
    rm -f ${ncFile}.tmp
  fi

  #pscoast -R-10/20/-10/10r -JG0/0/15000/0/0/0/30/20/10c -A5000 -W -Bp5 -Bg5 -N1 > ${psFile}
  gmtset MAP_GRID_PEN_PRIMARY "0.4p,black,dotted"

  #R="-40/18/70/60+r" # For Ortographic projection.
  R="-10/5/35/45" # For Platee Carre projection.
  J="Q15c" # Plate Carree (Equidistant cylindrical)
  C="/home/marcos/wrf_model/share/cpt/ptype.cpt"

#  grdimage ${ncFile}?ptype -J${J} -R${R} -C${C} -K -nn > ${psFile}
  grdview ${ncFile}?ptype -J${J} -R${R} -C${C} -K -To"0.3p,gray" > ${psFile}
  pscoast -R -J -A200 -W -Dh -N1 -O -K >> ${psFile}
  echo "-3.702 40.178" | psxy -J -R -Sc0.5c -O -K >> ${psFile}
  echo "-3.702 40.178" | psxy -J -R -Sx0.2c -O -K -Gblack >> ${psFile}
  pslegend -R -J -F+gazure1 -DjBR+w3.2c -C0.3c/0.3c --FONT_ANNOT_PRIMARY="6p,Helvetica,black" -O << EOF >> ${psFile}
# Legend test for pslegend
# G is vertical gap, V is vertical line, N sets # of columns, D draws horizontal line.
# H is header, L is label, S is symbol, T is paragraph text, M is map scale.
#
S 0.1c r 0.3c 0/255/0     0.1p 0.3c Rain
S 0.1c r 0.3c 255/0/0     0.1p 0.3c Freezing rain
S 0.1c r 0.3c 0/0/255     0.1p 0.3c Snow
S 0.1c r 0.3c 0/0/102     0.1p 0.3c Wet snow
S 0.1c r 0.3c 28/184/166  0.1p 0.3c Mixture of rain and snow
S 0.1c r 0.3c 255/128/0   0.1p 0.3c Ice pellets
EOF
  psconvert -Z -A -P -Tg -Qg4 -Qt4 ${psFile}

done

