#! /bin/ksh
### # QSUB  -r vtest1  -eo  -lM 2000kw  -lT 60
### # QSUB
set -xve
# --------- VBAROT2 - vtest1_x86 ------ #
# Barotropic vorticity equation model.  #
# Test job for vbarot2.npl: sun/sgi     #
# UTF 1.4 output. Balancing option.     #
# Super-rotn initial flow. 10 day run.  #
# vtest1:  Evolution of zonal flow.     #
#      *** No balancing forcing.        #
#          Additional div.blob forcing. #
#          No restorn of zonal mean.    #
# *** GWTLT from precompiled library.   #
# Mike Blackburn,  13.05.2015.          #
# ------------------------------------- #
set +v
date

############################################# MACHINE SET-UP

MACHINE=x86                                 # (x86/sun) Sun(linux/sparc)

[ $MACHINE = x86 ]  &&  { IGCM=/home/mike  ; }
[ $MACHINE = sun ]  &&  { IGCM=/home/mike  ; }

FORTRAN=f90                                 # (f90 only)
whence $FORTRAN  ||  echo "$FORTRAN NOT FOUND"

TMPDIR=/tmp/$LOGNAME/scratch
[ -d $TMPDIR ]  ||  mkdir -p $TMPDIR
cd $TMPDIR
pwd

############################################# SWITCHES & VARIABLES

COMPILE=yes                                 # Update, compile, load.
RUN=yes                                     # Run program.
EXPID=vtest1_x86                            # Experiment identifier.
WORK=/net/jasmin/users/mike/vbarot          # Working directory node.
EXPDIR=$WORK/$EXPID                         # Experiment directory.
EXEDIR=$WORK                                # Experiment directory.
UTF=$EXPID"_rws1_utf"                       # Output UTF name.
EXEFILE=$EXEDIR/vbarot2_exe                 # Executable file.
[ ! -d $EXEDIR ]  &&  mkdir -p $EXEDIR      # Create directory.
[ ! -d $EXPDIR ]  &&  mkdir -p $EXPDIR      # Create directory.

############################################# UPDATE DIRECTIVES

cat  <<  /EOF  >  updates
*/ -------------------------- Standard updates -----------------------
*NOLIST
*READ $IGCM/vbarot/up/vbarot2_bugs4
*READ $IGCM/vbarot/up/vbarot2_utf14
*READ $IGCM/vbarot/up/vbarot2_chi1
*READ $IGCM/vbarot/up/vbarot2_ibal1
*READ $IGCM/vbarot/up/vbarot2_rws1
*/ -------------------------- Personal updates -----------------------
*/
*IDENT ZINIT
*/     Initial super-rotation flow.
*/     Optional m=1 planetary wave in z(m=1,n=4).
*D INIPSI.22
C     Set equatorial super-rotation velocity in m/s.
      ZUSR=15.4
      Z(1)=EZ*(1.+ZUSR/CV)
      IF (ZUSR.NE.0.) THEN
         PRINT *,'INITIAL SUPER-ROTATION SET-UP, U-EQ = ',ZUSR,' M/S'
      ENDIF
C       Commented lines for optional zonally symmetric jet flow.
C*      Z(2)=0.15
C*      PRINT *,'INITIAL Z(M=0,N=3) SET TO +0.15 NON-DIM'
C*      Z(3)=-0.10
C*      PRINT *,'INITIAL Z(M=0,N=5) SET TO -0.10 NON-DIM'
C       Commented lines for optional planetary waves.
C*      Z(IDM+1)=0.0125
C*      PRINT *,'INITIAL Z(M=1,N=2) SET TO +0.0125 NON-DIM'
C*      Z(IDM+2)=0.025
C*      PRINT *,'INITIAL Z(M=1,N=4) SET TO +0.0250 NON-DIM'
*/
*/ -------------------------------------------------------------------
*IDENT MODIFY
*/    T21  : MG= 64,JG=16,NWJ2=121
*/    T42  : MG=128,JG=32,NWJ2=462
*/    T63  : MG=192,JG=48,NWJ2=1024
*/    T106 : MG=320,JG=80,NWJ2=2862
*D PARAM1.2
      PARAMETER(MM=21,NN=21,NHEM=2,MOCT=1,MG=64,JG=16,NWJ2=121,
*/
*/    rms values every timestep.
*D ENERGY.9,12
*D ENERGY.31
*/
*/    Output vorticity tendency.
*I VAROT.61
          CALL OUTFLD(11)
*/ -------------------------------------------------------------------
*/ Optional restoration only on eddy vorticity - zonal mean undamped.
*D DIFUSE.27,28
      DO 20 IHEM=1,NHEM
         IOF=(IHEM-1)*NWJ2
         DO 20 I=IDM+1,NWJ2
            II=I+IOF
            ZT(II)=ZT(II)-(ZMI(II)-ZRES(II))*RESTIM
   20 CONTINUE
*/ -------------------------------------------------------------------
*/ Change divergence forcing (actually unchanged from original code).
*D INIDIV.27
        CALL FORCE(DG(1,JL),1.00,6.,90.,145.,6.E-6/WW)
*/ -------------------------------------------------------------------
/EOF

############################################# NAMELIST DATA

cat  <<  /EOF  > data
 &INPB RNTAPE=1.0,KITS=3,TSPD=24.,TDISS=-4.,NDEL=8,RESTIM=15.0,
 LSPO=3*.FALSE.,3*.TRUE.,4*.TRUE.,2*.TRUE.,.FALSE.,2*.TRUE.,
 LPRINT=.FALSE.,NLAT=16,NLONG=12,
 LOLD=.FALSE.,LCOR=.FALSE.,LDIVZN=.TRUE.,LFIXZN=.FALSE.,
 LUTF=.TRUE.,LIBAL=.FALSE.,
 KOUNTO=24,KTOTAL=-10,NLEVZ=-1,NLEVD=-1,HGT=0.0,&
/EOF

############################################# ERROR PROCESSING FUNCTION

ABORT ()
{
echo '!!!!!!!!!! ERROR PROCESSING !!!!!!!!!!'
set +e
if [ $1 = $FORTRAN ]
then
    mv prog.l $EXPDIR                       # Save compile listing
    rm updates prog.*
elif [ $1 = EXECUTE ]
then
    cat fort.6                              # List output.
    [ $MACHINE = sgi ]  &&  cvdump $EXEFILE core
    rm data
fi
beep1  "$1 FAILURE"
exit 1
}

############################################# COMPILE AND LOAD

if [ $COMPILE = yes ]
then
    nupdate  -p $IGCM/vbarot/vbarot2.npl  \
             -i updates  -c prog  -s prog.src  \
             -f  -w 72  -o id,nd,um,ssq  -m 2           ||  ABORT NUPDATE
    cp prog.src $EXPDIR

    if [ $MACHINE = x86 ]
    then
        ftype='-xtypemap=real:64,double:64,integer:32'
        if [ $FORTRAN = f90 ]
        then
            fopt="-V  -O3  -ansi  -f  $ftype  -xfilebyteorder=big16:%all"
            f90  $fopt  -o $EXEFILE  \
             -L $IGCM/igcm1/lib/rd64i32o3f  prog.f  \
             -lf90fft1 -lf90aux1 -lf90util1  \
             -L $IGCM/lib/rd64i32o3f  -lf90utf1314_4    ||  ABORT $FORTRAN
        else
            echo "Abort: incorrect Fortran compiler name"
            ABORT $FORTRAN
        fi
    elif [ $MACHINE = sun ]
    then
        ftype='-xtypemap=real:64,double:128,integer:32'
        if [ $FORTRAN = f90 ]
        then
            fopt="-V  -O3  -ansi  -f  $ftype"
            f90  $fopt  -o $EXEFILE  \
             -L $IGCM/igcm1/lib  prog.f  \
             -lf90fft1 -lf90aux1 -lf90util1  \
             -L $IGCM/lib  -lf90utf1314_4               ||  ABORT $FORTRAN
        else
            echo "Abort: incorrect Fortran compiler name"
            ABORT $FORTRAN
        fi
    else
        echo "Abort: incorrect machine name"
        ABORT $FORTRAN
    fi
    rm prog.* updates
fi

############################################# RUN PROGRAM

if [ $RUN = yes ]
then
    ## ln  -s $IGCM/r1103/t042day0.6_ieee64  fort.10  # Input vorticity.
    ## ln  -s $IGCM/vbarot/orog.sp  fort.36    # Spectral orography.
    ## ln  -s $IGCM/vbarot/orog.gp  fort.37    # Gridded orography.

    $EXEFILE  <  data  ||  ABORT EXECUTE

    ls -alF
    set +e
    mv fort.65 $EXPDIR/$UTF                 # Save output UTF.
    ## mv fort.24 $EXPDIR/$EXPID"_r24"         # Save restart file.
    ls -alF    $EXPDIR
    rm data fort.*
fi
beep1  SUCCESSFUL
exit 0

############################################# END OF JOB
