#! /bin/sh -x
#################################
##
## Primary grid dimensions
##
## Note:  LM must be 
## 38, 45, 50, or 60 (when running Eta coordinates)
## 25, 30, 38, or 45 (when running sigma coordinates)
##
## Note:  JM MUST be odd!
#

confin=$1

##
Lon=-60.0
Lat=-16.0
IM=227
JM=351
LM=38
LSM=20
Res=40
Fct=6
IntFct=6
FInitBC=T062L028
InitBC=6
TInitBC=1
IntPhisAcum=6.0
LabRod=Etasa
TypRun=forecast
slope=.false.
soilmoist=.false.
vegflag=.true.
postout=latlonnopack
sstflg=.true.
qtdtypsolo=9
npio_server=24
##
#
#
########################
#
# Note: PT can only be set to 25 or 50 hPa to work properly with
#	the radiation files included in this package.  
#
PT=25
#
####################
#
# This is used to link files for use by various programs.  Most machines
# name the linked files as "fort.#", while HP's use "ftn#", where # is the
# unit number.
#
HVAL="fort."
#
##########################
#
## These should both be =1 unless using multiple CPUs on a machine
## using "real" MPI (as opposed to the included dummy MPI library).
##
## INPES X JNPES = total number of CPUs to be used
##
INPES=10
JNPES=12
#
##################################
#-----------------------------------------------------------------------------#
#-----------------------------------------------------------------------------#

##############################################
#
# should require no changes beyond this point
#
##############################################

# Define os diretorios raizes de home e saida
DIRINSTALL=`pwd`
DIREta=`dirname ${DIRINSTALL}`
DIRMODEL=`dirname ${DIREta}`

Eta_home=/stornext/home/etaclim/sazonal/worketa
Eta_home1=/scratchin/grupos/eta/home/etaclim/sazonal/worketa
Eta_wrk=/scratchout/grupos/eta/home/etaclim/sazonal/worketa

rm -R -f  ${Eta_home1}/eta/$confin
mkdir -p ${Eta_home1}/eta/$confin/exe

rm -R -f  ${Eta_home1}/eta/src
cp -R ${Eta_home}/eta/src     ${Eta_home1}/eta/
cp -R ${Eta_home}/eta/scripts ${Eta_home1}/eta/${confin}/scripts
cp -R ${Eta_home}/eta/ucl     ${Eta_home1}/eta/${confin}/ucl
cp -R ${Eta_home}/eta/grb     ${Eta_home1}/eta/${confin}/grb

cp -R ${Eta_home}/eta/fix  ${Eta_home1}/eta/

cp parmeta.mpiin ../src/etafcst_all
cp parmeta.mpiin ../src/etafcst_CC
cp parmeta.in ../src/include
cp parmeta.in ../src/post/post_new
cp parmeta.in ../src/post/post0

. /opt/modules/default/etc/modules.sh
module list 1> ${Eta_home1}/eta/${confin}/exe/module_list.txt 2>&1

cd ../src/etafcst_all

cat parmeta.mpiin | sed s:imin:$IM: | sed s:jmin:$JM: | \
sed s:lmin:$LM: | sed s:lsmin:$LSM: | sed s:ipcin:$INPES: | \
sed s:jpcin:$JNPES: > parmeta


if [ $INPES -eq 1 ]
then
	if [ $JNPES -eq 1 ]
	then
	cp makefile.dummympi makefile
	cp mpif.h_dummy mpif.h
	else
	cp makefile.realmpi makefile
	rm mpif.h
	fi
else
        cp makefile.realmpi makefile
        rm mpif.h
fi


cd ../etafcst_CC

cat parmeta.mpiin | sed s:imin:$IM: | sed s:jmin:$JM: | \
sed s:lmin:$LM: | sed s:lsmin:$LSM: | sed s:ipcin:$INPES: | \
sed s:jpcin:$JNPES: > parmeta


if [ $INPES -eq 1 ]
then
        if [ $JNPES -eq 1 ]
        then
        cp makefile.dummympi makefile
        cp mpif.h_dummy mpif.h
        else
        cp makefile.realmpi makefile
        rm mpif.h
        fi
else
        cp makefile.realmpi makefile
        rm mpif.h
fi


cd ../model_with_kf

if [ $INPES -eq 1 ]
then
	if [ $JNPES -eq 1 ]
	then
	cp makefile.dummympi makefile
	cp mpif.h_dummy mpif.h
	else
	cp makefile.realmpi makefile
	rm mpif.h
	fi
else
        cp makefile.realmpi makefile
        rm mpif.h
fi


## assume at if using MPI on model, can use for post as well
##
##  changed mind...force post to be a serial code

cd ../post/post_new

 if [ $INPES -eq 1 ]
 then
         if [ $JNPES -eq 1 ]
	then

        cp makefile.dummympi makefile
        cp mpif.h_dummy mpif.h

        else
        cp makefile.realmpi makefile
        rm mpif.h
        fi
 else
        cp makefile.realmpi makefile
        rm mpif.h
 fi


cd ../../include

cat parmeta.in  | sed s:imin:$IM: | sed s:jmin:$JM: | \
sed s:lmin:$LM: | sed s:lsmin:$LSM:  > parmeta

cd ../post/quilt
cat parmeta.mpiin | sed s:imin:$IM: | sed s:jmin:$JM: | \
sed s:lmin:$LM: | sed s:lsmin:$LSM: | sed s:ipcin:$INPES: | \
sed s:jpcin:$JNPES: > parmeta 

cd ../post_new

cat parmeta.in  | sed s:imin:$IM: | sed s:jmin:$JM: | \
sed s:lmin:$LM: | sed s:lsmin:$LSM:  > parmeta

cd ../post0/

cat parmeta.in | sed s:imin:$IM: | sed s:jmin:$JM: | \
sed s:lmin:$LM: | sed s:lsmin:$LSM:  > parmeta

cd ../staids
cat parmeta.in | sed s:imin:$IM: | sed s:jmin:$JM: | \
sed s:lmin:$LM: | sed s:lsmin:$LSM:  > parmeta

cd ../profile
cat parmeta.f90.in | sed s:lmin:$LM: > parmeta.f90

cd ../sndp
cat parmeta.in | sed s:LMIN:$LM: > parmeta

if [ "${vegflag}" = ".true." ] ; then
   dlmd=`${Eta_home1}/eta/${confin}/scripts/def_eta.scr ${Res}|awk '{print $1}'`
   dphd=`${Eta_home1}/eta/${confin}/scripts/def_eta.scr ${Res}|awk '{print $2}'`

rm -f ${Eta_home1}/eta/src/prep/veg/parmeta   

cat << EOF > ${Eta_home1}/eta/src/prep/veg/parmeta
       PARAMETER (IM=${IM},JM=${JM},IMJM=IM*JM-JM/2)
       PARAMETER (DLMD=${dlmd},
     &  DPHD=${dphd})
       PARAMETER (TLM0D=${Lon},TPH0D=${Lat})
EOF

ftn -o ${Eta_home1}/eta/${confin}/exe/vegmsk.x ${Eta_home1}/eta/src/prep/veg/vegmsk.f 

fi

####PARA TOPOGRAFIA DE 1km
ftn -o ${Eta_home1}/eta/${confin}/exe/select.x ${Eta_home1}/eta/src/prep/smask/smask_select.f

pwd
cd ${Eta_home1}/eta/${confin}/scripts

# Configuracao do arquivo de fila para rodar na una -------------------------
# npr         = numero total de processadores (+1 = Servidor de I/O)
# filaUna.nqs = script de submissao no cluster
# 

#let npr=${INPES}*${JNPES}+1
let npr=${INPES}*${JNPES}

cat ${Eta_home}/eta/scripts/filaCray.sh | \
    sed -e "s:WORKDIR:${Eta_wrk}/eta/$confin:g" | \
#    sed -e "s:npr:${npr}:g" | \
    sed -e "s:WORKX:${Eta_home1}/eta/$confin/scripts:g" > ${Eta_home1}/eta/${confin}/scripts/filaCray.sh
    
#cat ${Eta_home}/eta/scripts/get_global_data.ksh | \
#    sed -e "s:DIR_OUT:${Eta_wrk}/eta/${confin}:g" > ${Eta_home1}/eta/${confin}/scripts/get_global_data.ksh 

cat ${Eta_home}/eta/scripts/outjob_special.ksh | \
    sed -e "s:RES:${Res}:g" > ${Eta_home1}/eta/${confin}/scripts/outjob_special.ksh 

# by Gustavo PROJETA/CPTEC/INPE - Lamosa PAD/CPTEC/INPE 2007 ----------------

##RESOLUCAO DA RODADA DIVIDIDA POR 100
resol=`echo ${Res} / 100 | bc -l | cut -c 1-3`

cat << EOF > ./DIRROOT
Eta_home=${Eta_home}
Eta_home1=${Eta_home1}
Eta_wrk=${Eta_wrk}
#Configuracao Inicial da rodada

Lon=${Lon}
Lat=${Lat}
IM=${IM} 
JM=${JM} 
LM=${LM} 
LSM=${LSM}
Res=${Res}
resol=0${resol}
Fctexec=etafcst_zhao.x 
LabRod=${LabRod} 
Conf=${confin}
Fct=${Fct}
IntFct=${IntFct}
FInitBC=${FInitBC}
PT=${PT}
InitBC=${InitBC}
TInitBC=${TInitBC}
IntPhisAcum=${IntPhisAcum}
TypRun=${TypRun}
slope=${slope}
soilmoist=${soilmoist}
vegflag=${vegflag}
postout=${postout}
sstflg=${sstflg}
qtdtypsolo=${qtdtypsolo}
npr=${npr}
npio_server=${npio_server}
EOF

###CRIANDO O ARQUIVO namelist QUE SERA USADO PELO PROGRAMA corners.f90
dlmd=`${Eta_home1}/eta/${confin}/scripts/def_eta.scr ${Res}|awk '{print $1}'`
dphd=`${Eta_home1}/eta/${confin}/scripts/def_eta.scr ${Res}|awk '{print $2}'`
let imjm=${IM}*${JM}-${JM}/2
cat << EOF > ${Eta_home}/eta/install/namelist
${IM}
${JM}
${imjm}
${Lat}
${Lon}
${dlmd}
${dphd}
0${resol}
EOF
##############

cat ./start.ksh_Templ | sed "/INCLUI/r ./DIRROOT" > ./start.ksh
#cat ./cron.ksh_Templ | sed "/INCLUI/r ./DIRROOT" > ./cron.ksh
chmod 755 ./*
rm -f ./start.ksh_Templ ./cron.ksh_Templ 
# Make file variables.f90 in directore latlon2ll
cat << EOF > ${Eta_home}/eta/src/post/latlon2ll/variables.f90
    module variables
!
! Dimension 1D variable
!
      integer, parameter         :: kmax=${LSM}
      real,dimension(kmax)       :: pr       !

      DATA pr/50.,100.,150.,200.,250.,300.,350.,400.,450.,500.,           &
     &        550.,600.,650.,700.,750.,800.,850.,900.,925.,1000./
    
     end module variables
EOF

cat << EOF > ${Eta_home1}/eta/${confin}/ucl/CTLLEVELS
ZDEF   ${LSM} LEVELS 1000 925 900 850 800 750 700 650 600 550
                  500 450 400 350 300 250 200 150 100 50
EOF

# Make arquive with pressure levels for fsctdata.meso 
# The pressure levels must be the same as variables.f90 arquive above
cat << EOF > ${Eta_home1}/eta/${confin}/ucl/PressLevel
 SPL=5000.,10000.,15000.,20000.,25000.,
     30000.,35000.,40000.,45000.,
     50000.,55000.,60000.,65000.,
     70000.,75000.,80000.,85000.,
     90000.,92500.,100000.,
EOF

# Make arquive with pressure levels for namelist_grib
cat << EOF > ${Eta_home1}/eta/${confin}/ucl/namelist_indice
 &MODEL_LEVS
  mlevs=1000., 925., 900., 850., 800., 750., 700., 650., 600., 550.,
         500., 450., 400., 350., 300., 250., 200., 150., 100., 050.,
         000., 000., 000., 000., 000., 000., 000., 000., 000., 000.,
         000., 000., 000., 000., 000., 000., 000., 000., 000., 000.,
         000., 000., 000., 000., 000., 000., 000., 000., 000., 000.,
         000., 000., 000., 000., 000., 000., 000., 000., 000., 000.,
         000., 000., 000., 000., 000., 000., 000., 000., 000., 000.,
         000., 000., 000., 000., 000., 000., 000., 000., 000., 000.,
         000., 000., 000., 000., 000., 000., 000., 000., 000., 000.,
         000., 000., 000., 000., 000., 000., 000., 000., 000., 000.,
         000., 000., 000., 000., 000., 000., 000., 000., 000., 000.,
         000., 000., 000., 000., 000., 000., 000., 000., 000., 000.,
         000., 000., 000., 000., 000., 000., 000., 000., 000., 000.,
         000., 000., 000., 000., 000., 000., 000., 000., 000., 000.,
         000., 000., 000., 000., 000., 000., 000., 000., 000., 000.,
         000., 000., 000., 000., 000., 000., 000., 000., 000., 000.,
         000., 000., 000., 000., 000., 000., 000., 000., 000., 000.,
         000., 000., 000., 000., 000., 000., 000., 000., 000., 000.,
         000., 000., 000., 000., 000., 000., 000., 000., 000., 000.,
         000., 000., 000., 000., 000., 000., 000., 000., 000., 000.
 &END
EOF

# Cria CTLTEMPLATE 
cat << EOF > ${Eta_home1}/eta/${confin}/ucl/CTLTEMPLATE 
DSET ^NARQ
OPTIONS TEMPLATE 
UNDEF -9999.
TITLE 29help Days of Sample Model Output 
XDEF  275 LINEAR  -115.0   0${resol}
YDEF  220 LINEAR  -58.0    0${resol}
EOF

cat ${Eta_home1}/eta/${confin}/ucl/CTLLEVELS >> ${Eta_home1}/eta/${confin}/ucl/CTLTEMPLATE
cat << EOF >> ${Eta_home1}/eta/${confin}/ucl/CTLTEMPLATE
TDEF   NTIME LINEAR LLTIME INTHR
VARS  45
PSLM    0  99     Mesinger M S L Pressure  (hPa)
PSLC    0  99     Surface Pressure         (hPa)
TOPO    0  99     Surface Height           (m)
TP2M    0  99     Shelter Temperature      (K)
MXTP    0  99     Max Temperature          (K)
MNTP    0  99     Min Temperature          (K)
DP2M    0  99     Shelter Dew Temp.        (K)
U10M    0  99     U 10m                    (m/s)
V10M    0  99     V 10m                    (m/s)
U100    0  99     U 100m                   (m/s)
V100    0  99     V 100m                   (m/s)
PREC    0  99     Total  6h Precip.        (m)
PRCV    0  99     Conv   6h Precip.        (m)
PRGE    0  99     Large Scl  6h Prec.      (m)
NEVE    0  99     Snowfall 6h              (m)
CLSF    0  99     Time Ave Sfc Lat Ht Flx  ()
CSSF    0  99     Time Ave Sfc Sen Ht Flx  ()
GHFL    0  99     Time Ave Ground  Ht Flx  ()
TSFC    0  99     Sfc (skin) Temperature   (K)
TGSC    0  99     Soil Temperature 0.1m    (K)
TGRZ    0  99     Soil Temperature 2.0m    (K)
USSL    0  99     Soil Moisture Cont. 0.1m (0-1)
UZRS    0  99     Soil Moisture Cont. 2.0m (0-1)
SMAV    0  99     Soil Moisture Avail      ()
RNOF    0  99     Storm Sfc Rnoff 6h       ()
EVPP    0  99     Potencial sfc Evaporation(m)
LWNV    0  99     Low cloud fraction       ()
MDNV    0  99     Mid cloud fraction       ()
HINV    0  99     High cloud fraction      ()
OCIS    0  99     Ave Incmg Sfc SW Rad     (W/m2)
OLIS    0  99     Ave Incmg Sfc LW Rad     (W/m2)
OCES    0  99     Ave Outgo Sfc SW Rad     (W/m2)
OLES    0  99     Ave Outgo Sfc LW Rad     (W/m2)
ROCE    0  99     Ave Outgo TOA SW Rad     (W/m2)
ROLE    0  99     Ave Outgo TOA LW Rad     (W/m2)
ALBE    0  99     Surface Albedo           ()
CAPE    0  99     CAPE                     (J/kg)
AGPL    0  99     Precipitable Water       (mm)
ZGEO   ${LSM}  99     geopotential height      (m)
UVEL   ${LSM}  99     zonal wind               (m/s)
VVEL   ${LSM}  99     meridional wind          (m/s)
TEMP   ${LSM}  99     temperature              (K)
UMRL   ${LSM}  99     relative humidity        (%)
OMEG   ${LSM}  99     Omega                    (hPa/s)
UMES   ${LSM}  99     specific humidity        (kg/kg)
ENDVARS
EOF

#Informacoes para o prof

WBD=`echo "- ( ${IM} - 1 ) * ${dlmd}" |bc -l |awk '{printf("%.13f\n",$1)}'`
SBD=`echo "-  ((${JM} - 1)  / 2 ) * ${dphd}" |bc -l |awk '{printf("%.13f\n",$1)}'`

cat << EOF > ${Eta_home}/eta/src/post/staids/stalst_add.txt
       DATA WBD/${WBD}/,SBD/${SBD}/
       DATA TPH0D/${Lat}/,TLM0D/${Lon}/
       DATA DPHD/${dphd}/,DLMD/${dlmd}/
       END BLOCK DATA STN
EOF

rm -f ${Eta_home}/eta/src/post/staids/STALST.f
cat ${Eta_home}/eta/src/post/staids/STALST.f.in >> ${Eta_home}/eta/src/post/staids/STALST.f
cat ${Eta_home}/eta/src/post/staids/stalst_add.txt >> ${Eta_home}/eta/src/post/staids/STALST.f

 # Cria arvore de diretorios de saida
mkdir -p ${Eta_home1}/eta/${confin}/fix
mkdir -p ${Eta_home1}/eta/${confin}/datain
mkdir -p ${Eta_home1}/eta/${confin}/grib
mkdir -p ${Eta_wrk}/eta/${confin}



