#!/bin/ksh

eval `cclargs $0 \
  -genphysx "0"     "1"     "[Produce geophysical fields with genphysx]"\
  -xrec     "0"     "1"     "[Visualize grid with xrec                ]"\
  -spi      "0"     "1"     "[Visualize grid with spi                 ]"\
  ++ $*`
#
printf "\n#############################################\n"
printf " GRILLE utility for gem version v_${ATM_MODEL_VERSION}\n"
printf "############################################# \n\n"

GENGRID=gemgrid_${BASE_ARCH}.Abs

EDITFST=editfst
PGSM=pgsm
yy2global=yy2global.Abs

if [ ! -s gem_settings.nml ] ; then
  printf "\n Must provide file gem_settings.nml\n containing a valid &grid namelist\n\n"
  exit
fi

cp gem_settings.nml model_settings.cfg

#================== Produce Grid Pos Rec ================
/bin/rm -f tape1 tape2 gfilemap.txt gfilemap2.txt 2> /dev/null

$GENGRID

/bin/rm -f model_settings.cfg

yinyang=0
if [ -s tape2 ] ; then yinyang=1; fi

#================== View the grid(s) ================
function launch_spi {
   local _args="$*"
   local _spi_version=7.6.2

   (. ssmuse-sh -p /ssm/net/cmoe/apps/SPI_${_spi_version} ; SPI -field $_args)
}

viewer=""
if [ $xrec -eq 1 ]; then
   viewer="xrec -imflds"
elif [ $spi -eq 1 ]; then
   viewer="launch_spi"
fi

if [ -n "$viewer" ] ; then
  cat > p.dir <<pgsm100
 sortie (std,1000,a)
 compac=-12
 etiksrt='GRID'
 LAGRILLE
*
 heure(0)
 outlalo(-1,-1,-1)
*
 setintx(cubique)
 champ('ME')
end
pgsm100
  if [ -s tape1 ] ; then
    r.fstliste -izfst tape1 -nomvar ">>" | sed 's/\:/ /g' > liste
    ip1=`cat liste | awk '{print $3}'`
    ip2=`cat liste | awk '{print $4}'`
    ip3=`cat liste | awk '{print $5}'`
    ETK=`cat liste | awk '{print $9}'`
    cat p.dir | sed "s/LAGRILLE/grille (tape2,$ip1,$ip2,$ip3)/" | sed "s/GRID/$ETK/" > p1.dir
    cp tape1 tmp1$$
    $PGSM -iment $ATM_MODEL_DFILES/bcmk/geophy/Gem_geophy.fst -ozsrt tmp1$$ -i p1.dir
  fi
  if [ -s tape2 ] ; then
    r.fstliste -izfst tape2 -nomvar ">>" | sed 's/\:/ /g' > liste
    ip1=`cat liste | awk '{print $3}'`
    ip2=`cat liste | awk '{print $4}'`
    ip3=`cat liste | awk '{print $5}'`
    ETK=`cat liste | awk '{print $9}'`
    cat p.dir | sed "s/LAGRILLE/grille (tape2,$ip1,$ip2,$ip3)/" | sed "s/GRID/$ETK/" > p2.dir
    cp tape2 tmp2$$
    $PGSM -iment $ATM_MODEL_DFILES/bcmk/geophy/Gem_geophy.fst -ozsrt tmp2$$ -i p2.dir
    $yy2global -IYIN tmp1$$ -IYAN tmp2$$ -OGLOB tmpglb$$
    editfst -s tmp2$$   -d tmp1$$ -i /dev/null
    editfst -s tmpglb$$ -d tmp1$$ <<EOF
 zap(-1,-1,'PARPOS_GLB')
EOF
    /bin/rm -f tmp2$$ tmpglb$$
  fi
  $viewer tmp1$$
  /bin/rm -f p.dir p1.dir p2.dir liste tmp1$$
fi

if [ $genphysx -gt 0 ] ; then
  export PATH=$PATH:/data/dormrb04/genphysx/src/tcl
  ext=''
  if [ $yinyang -gt 0 ] ; then
    ext=_YIN
  fi
  GenPhysX.tcl -gridfile `pwd`/tape1 -batch -target GEMMESO -t 21600 -mail $USER@ec.gc.ca -result Gem_geophy${ext}
  if [ $yinyang -gt 0 ] ; then
    GenPhysX.tcl -gridfile `pwd`/tape2 -batch -target GEMMESO -t 21600 -mail $USER@ec.gc.ca -result Gem_geophy_YAN
  fi
fi

