#!/bin/ksh

ECEARTH=unknown
RUN_length_prev=0
FCLENGTH=24
IFSRESOL=T159L62
NPROCIFS=56
WAM=false
NFRCO=-1
NPROMA=40
LLAST=.TRUE.
DATADIR=unknown
NPRTRVIFS=1
EXPID=paaa
LFASTRES=.FALSE.
NFRP=6
LCMIP5=.TRUE.
NRCP=0
NFIXYR=0

while getopts e:d:l:r:n:w:c:p:o:0:1:2:3:4:5:6:7: option
do
  case $option in
    e) ECEARTH=$OPTARG;;
    d) RUN_length_prev=$OPTARG;;
    l) FCLENGTH=$OPTARG;;
    r) IFSRESOL=$OPTARG;;
    n) NPROCIFS=$OPTARG;;
    w) WAM=$OPTARG;;
    c) NFRCO=$OPTARG;;
    p) NPROMA=$OPTARG;;
    o) LLAST=$OPTARG;;
    0) DATADIR=$OPTARG;;
    1) NPRTRVIFS=$OPTARG;;
    2) EXPID=$OPTARG;;
    3) LFASTRES=$OPTARG;;
    4) NFRP=$OPTARG;;
    5) LCMIP5=$OPTARG;;
    6) NRCP=$OPTARG;;
    7) NFIXYR=$OPTARG;;
    \?) exit 1;;
  esac
done

EXPVER=unknown
TIMESTEP=3600.0

case $IFSRESOL in
  T21L31 ) EXPVER=ewhs
           WAMRESOL=global300 
	   NSTPW=1
           TSTEP=3600;;
  T42L40 ) EXPVER=ewhw
           WAMRESOL=global300
	   NSTPW=1
           TSTEP=3600;;
  T95L40 ) EXPVER=ewhx
           WAMRESOL=global300
	   NSTPW=1
           TSTEP=3600;;
  T159L62) EXPVER=b0if
           WAMRESOL=global150
	   NSTPW=1
           TSTEP=3600;;
  T159L91) EXPVER=b0if
           WAMRESOL=global150
	   NSTPW=1
           TSTEP=3600;;
  T255L62) EXPVER=ewi0
           WAMRESOL=global50
	   NSTPW=1
           TSTEP=2700;;
  T399L62) EXPVER=ewim
           WAMRESOL=global50
	   NSTPW=2
           TSTEP=1800;;
  *      ) print "Unknown testcase in setupifs.ksh"
           exit 1;;  
esac
EXPVER=$EXPID

# Time related settings
NFRMASSCON=$((6*3600/TSTEP))
NFRHIS=$((NFRP*3600/TSTEP))
NFRPOS=$((NFRP*3600/TSTEP))
FCLEN=$((FCLENGTH*3600/TSTEP))

# Coupling
if ((NFRCO>0)); then
  NFRCO=$((NFRCO*3600/TSTEP))
  LMCCIEC=true
  LMCCEC=true
  LMCC04=false
  NOACOMM=5
  LMCCICEIC=false
  LECURR=false
else
  LMCCIEC=true
  LMCCEC=true
  LMCC04=false
  NOACOMM=0
  LMCCICEIC=false
  LECURR=false
fi

# WAM resolution
if [[ $WAMRESOL = global36 ]] ; then
  rsoutw="-78.12"
  rnortw="81.00"
  rdegrew="0.36"
elif [[ $WAMRESOL = global50 ]] ; then
  rsoutw="-81.00"
  rnortw="81.00"
  rdegrew="0.5"
elif [[ $WAMRESOL = global100 ]] ; then
  rsoutw="-78.00"
  rnortw="81.00"
  rdegrew="1.0"
elif [[ $WAMRESOL = global150 ]] ; then
  rsoutw="-81.00"
  rnortw="81."
  rdegrew="1.5"
elif [[ $WAMRESOL = global300 ]] ; then
  rsoutw="-78.00"
  rnortw="81.00"
  rdegrew="3.0"
elif [[ $WAMRESOL = global511 ]] ; then
  rsoutw="-89.731"
  rnortw="89.731"
  rdegrew="0.3514"
else
  print - "$0: ERROR -  WAMRESOL=$WAMRESOL.  Invalid resolution."
  print - "$0: ERROR -  EXIT"
  exit 1
fi

cat >namelistfc <<EOF
&NAMCT0
  LFPOS = true,
  LSLAG=true,
  CFCLASS = 'rd',
  CTYPE   = "fc",
  LWROUTLAST = $LLAST,
  LSMSSIG  = false,
  NSTOP=$FCLEN,
  NFRDHP =240,
  NFRCO=$NFRCO,
  NFRMASSCON = $NFRMASSCON,
  NFRSDI = 1,
  NFRHIS = $NFRHIS,
  NHISTS(0) = 0,
  NFRPOS = $NFRPOS,
  NPOSTS(0) = 0,
  CFDIRLST = 'dirlist',
  CNPPATH  = 'postins',
/
&NAMCAL
  calendar = "Gregorian"
/
&NAMGFL
  YO3_NL%LGP=false,
  YO3_NL%LSP=false,
  AEROPAR=1,
  AEROCON=1,
  AEROIND=0,
  AERODIR=1,
/
&NAMFPC
  CFPFMT ='MODEL',
  LFITP   =true,
  NFP3DFP =        5,
  MFP3DFP = 129,130,138,155,133,
  RFP3P   = 100000,92500,85000,70000,50000,40000,30000,20000,10000,5000,1000,
  NFPPHY  =       40,
  MFPPHY  = 151,164,165,166,167,168,31,39,40,41,42,49,78,79,137,139,141,170,183,186,201,202,236,243,142,143,144,146,147,169,175,176,177,178,179,180,181,182,189,205,
  LFITT   =true,
  NFP3DFT = 1,
  MFP3DFT = 60,
  RFP3TH  = 315,330,
  LFITV   =true,
  NFP3DFV = 1,
  MFP3DFV = 3,
  RFP3PV  = 2000E-9,
/
&NAMFPDY2
/
&NAMFPDYP
/
&NAMFPDYH
/
&NAMFPDYT
/
&NAMFPDYV
/
&NAMFPDYS
/
&NAMFPPHY
/
&NAMMCC
  LMCCIEC=$LMCCIEC
  LMCCEC=$LMCCEC
  LMCC04=$LMCC04
  NOACOMM=$NOACOMM
  LMCCICEIC=$LMCCICEIC
/
&NAMSTOPH
  LSTOPH=false
  AMAGSTOPH=0.5
/
&NAMGRIB
  NSTREAM=1220
  NLOCGRB=15
  NENSFNB=0
  NSYSTEM=0
  NMETHOD=1
/
&NAMDDH
  NDHKD=120,
  LHDGLB=false,
  LHDPRG=false,
  LHDHKS=false,
  LHDZON=false,
  LHDDOP=false,
/
&NAMDYN
  VMAX1=210.0,
  VMAX2=260.0,
  TSTEP=${TSTEP}.0,
/
&NEMINI
  NEINI=1,
/
&NAMRES
  NFRRES=1,
  NRESTS=1,$FCLEN,
  LFASTRES=$LFASTRES,
  LFASTRESW=.TRUE.,
/
&NAMDIM
  NPROMA=$NPROMA
/
&NAERAD
  NRADFR=3,
  NRPROMA=$NPROMA,
  LECO2VAR=true,
  LHGHG=true,
  NSCEN=1,
  NHINCSOL=1,
  LHVOLCA=true,
  CRTABLEDIR="$DATADIR/ifs/common/rtables/",
  LCMIP5=$LCMIP5,
  NRCP=$NRCP,
  NFIXYR=$NFIXYR,
/
&NAMPAR0
 LSTATS=true,
 LDETAILED_STATS=true,
 LSTATSCPU=true,
 NOUTPUT=1,
 NPRTRW=$((NPROCIFS/NPRTRVIFS))
 NPRTRV=$NPRTRVIFS
 NPROC=$NPROCIFS,
 MP_TYPE=2,
 MBX_SIZE=128000000,
/
&NAMPAR1
  LSPLIT=true,
  NFLDIN=0,
  NFLDOUT=0,
  NSTRIN=$NPROCIFS,
  NSTROUT=1,
  NOUTTYPE=1
/
&NAEPHY
  LWCOU=$WAM,
  LWCOU2W=true,
  NSTPW=$NSTPW,
  RSOUTW=$rsoutw,
  RNORTW=$rnortw,
  RDEGREW=$rdegrew,
  LEO3CH=false,
  LECURR=$LECURR,
  LESICE=false,
  LEOCCO=false,
  LEOCWA=false,
  LEOCSA=true,
  RTHRFRTI=0.02
/
&NAEPHY
/
&NAERAD
/
&NALBAR
/
&NALORI
/
&NAM_DISTRIBUTED_VECTORS
/
&NAM926
/
&NAMAFN
/
&NAMANA
/
&NAMARPHY
/
&NAMCAPE
/
&NAMCFU
/
&NAMCHK
/
&NAMCHET
/
&NAMCLTC
/
&NAMCOM
/
&NAMCOS
/
&NAMCTAN
/
&NAMCUMFS
/
&NAMCT1
/
&NAMCVA
/
&NAMDDH
/
&NAMDFHD
/
&NAMDFI
/
&NAMDIF
/
&NAMDIM
/
&NAMDMSP
/
&NAMDPHY
/
&NAMDYN
/
&NAMDYNA
/
&NAMFA
/
&NAMFFT
/
&NAMFPD
/
&NAMFPDY2
/
&NAMFPDYH
/
&NAMFPDYP
/
&NAMFPDYS
/
&NAMFPDYT
/
&NAMFPDYV
/
&NAMFPEZO
/
&NAMFPF
/
&NAMFPG
/
&NAMFPIOS
/
&NAMFPPHY
/
&NAMFPSC2
/
&NAMFY2
/
&NAMGEM
/
&NAMGMS
/
&NAMGOES
/
&NAMGOM
/
&NAMGRIB
/
&NAMINI
/
&NAMIOMI
/
&NAMIOS
/
&NAMJBCODES
/
&NAMJFH
/
&NAMJG
/
&NAMJO
/
&NAMKAP
/
&NAMLCZ
/
&NAMLEG
/
&NAMLFI
/
&NAMMCC
/
&NAMMCUF
/
&NAMMETEOSAT
/
&NAMMTS
/
&NAMMTSAT
/
&NAMMTT
/
&NAMMUL
/
&NAMNMI
/
&NAMNASA
/
&NAMNN
/
&NAMNPROF
/
&NAMNUD
/
&NAMOBS
/
&NAMOPH
/
&NAMO3
 LEO3VAR=true
 CO3DATADIR="${DATADIR}/ifs/cmip5_o3/"
/
&NAMPAR0
/
&NAMPAR1
/
&NAMPHY
/
&NAMPHY0
/
&NAMPHY1
/
&NAMPHY2
/
&NAMPHY3
/
&NAMPHYDS
/
&NAMPPC
 LRSACC=.TRUE.
/
&NAMPONG
/
&NAMRAD15
/
&NAMRCOEF
/
&NAMRINC
/
&NAMRIP
/
&NAMSCC
/
&NAMSCM
/
&NAMSENS
/
&NAMSIMPHL
/
&NAMSKF
/
&NAMSTA
/
&NAMSTOPH
/
&NAMTCWV
/
&NAMTESTVAR
/
&NAMTLEVOL
/
&NAMTOPH
/
&NAMTOVS
/
&NAMTRAJP
/
&NAMTRANS
/
&NAMTRM
/
&NAMVAR
/
&NAMVAREPS
/
&NAMVDOZ
/
&NAMVFP
/
&NAMVRTL
/
&NAMVV1
/
&NAMVV2
/
&NAMVWRK
/
&NAMXFU
/
&NAMZDI
/
&NAPHLC
/
&NAV1IS
/
EOF

ln -sf namelistfc fort.4

cat > WAMINPUT_CPLD_000<<EOF
&NALINE
 CLHEADER =" WAVE MODEL ",
 CBPLTDT  ="200101010000",
 CEPLTDT  ="000000000000",
 CDATEF   ="200101010000",
 IDELPRO  = 900,
 IDELT    = 900,
 IDELWO   = 900,
 IDELWI   = 900,
 IDELINT  = 0000024,
 IDELINS  = 0000024,
 IDELSPT  = 0000024,
 IDELRES  = 240,
 CDATER   ="000000000000",
 CDATES   ="000000000000",
 FFLAG(17)= F,,
 PFLAG(1)= F,,
 PFLAG(3)= F,,
 PFLAG(6)= F,,
 PFLAG(7)= F,,
 PFLAG(8)= F,,
 PFLAG(10)= F,,
 PFLAG(25)= F,,
 GFLAG(1)= T,
 GFLAG(2)= T, 
 GFLAG(3)= T, 
 GFLAG(6)= T, 
 GFLAG(7)= T, 
 GFLAG(9)= T,
 GFLAG(10)= T,
 GFLAG(11)= F,
 GFLAG(12)= F,
 GFLAG(13)= F,
 GFLAG(14)= F,
 GFLAG(15)= F,
 GFLAG(16)= F,
 GFLAG(22)= F,
 GFLAG(23)= F,
 GFLAG(24)= F,
 GFLAG(25)= T,
 GFLAG(26)= T,
 GFLAG(27)= F,
 GFLAG(28)= F,
 GFLAG(29)= F,
 GFLAG(30)= F,
 GFLAG(31)= F,
 GFLAG(32)= F,
 GFLAG(33)= F,
 GFLAG(34)= F,
 GFLAG(35)= F,
 GFLAG(36)= F,
 GFLAG(37)= F,
 LFDBIOOUT = F,
 LFDB = F,
 ISHALLO  = 1,
 ITEST    = 0,
 ITESTB   = 1,
 IREST    = 0,
 IASSI    = 0,
 YCLASS   ="rd",
 NLOCGRB  = 15,
 ISTREAM  = 1082,
 YEXPVER  ="$EXPVER",
 NENSFNB  = 000,
 NTOTENS  = 000,
 NCONSENSUS = 0,
 NNCEP    = 0,
 NMFR     = 0,
 NDWD     = 0,
 NUKM     = 0,
 LMESSPASS = T,
 LWCOU    = T,
 CPATH    = "./",
 CFDBSF   = "./",
 CFDB2DSP = "./",
 LNOCDIN = F,
 IBUFRSAT(1) = 1,
 LALTCOR(1) = T,
 XKAPPA2(1) = 0.084,
 LALTGRDOUT(1) = F,
 IBUFRSAT(2) = 2,
 LALTCOR(2) = T,
 XKAPPA2(2) = 0.09,
 LALTGRDOUT(2) = F,
 ALTSDTHRSH(2) = 1.0,
 IBUFRSAT(3) = 60,
 LALTCOR(3) = F,
 XKAPPA2(3) = 0.09,
 HSALTCUT(3) = 0.1,
 LALTGRDOUT(3) = T,
 IBUFRSAT(4) = 260,
 LALTCOR(4) = F,
 XKAPPA2(4) = 0.09,
 HSALTCUT(4) = 0.1,
 ALTBGTHRSH(4) = 2.,
 LALTGRDOUT(4) = T,
 L4VTYPE = F,
 LFRSTFLD = F,
 LALTAS = F,
 LSARAS = F,
 LSARINV = F,
 IREFDATE=999,
 NSYSNB= 0,
 LGUST = T,
 LADEN = T,
 NMETNB= 1,
 LL1D = F,
 NWAM_BLKS = 32,
 LWCOUNORMS = F,
 SWAMPWIND = 18.45,
 SWAMPWIND2 = 0.,
 DTNEWWIND = 0.,
 LTURN90 = F,
 LGRHDIFS = F,
 LNEWLVTP = F
 /
EOF

ln -sf WAMINPUT_CPLD_000 fort.26

if [[ ${RUN_length_prev} = '0' ]]; then
   ln -sf $DATADIR/ifs/$IFSRESOL/* .
   \rm -f rcf
   cp $DATADIR/ifs/$IFSRESOL/rcf rcf
fi

print $EXPID

