#!/bin/bash

ulimit -s unlimited

#
EXPNO=47023000 
EXPNO1=47023000 # EXPNO with rerun files
EXPERIMENTSYEAR=1981
EXPERIMENTEYEAR=1996

# Restart:
RESTARTYEAR=1983                   # Set to ${EXPERIMENTSYEAR} for initial run 
RESTARTMONTH=5                  # Month (01, 02 etc.);  Set to 01 for initial run
#
EXPERIMENTEYEAR1=`expr ${EXPERIMENTEYEAR} + 1`

#set -ex
set -e
export MPLARGS="verbose=1;check=0"
export VPP_MBX_SIZE=64000000
export VPP_STATS=10
#
# Job file to run echam model on euler cluster
# =============================================
#
F_RECLUNIT=BYTE ; export F_RECLUNIT
MPIPROGINF=detail ; export MPIPROGINF
F_SYSLEN=600 ; export F_SYSLEN
#
NCPUS=64
NPROCA=8
NPROCB=8
NPROMA=16
#
RES=42
LEV=39
#
# Pressure levels for output files <EXPNO>_chem_m_<YYYY><MM>.nc
# =============================================================
rm -f afterburner_levels.nml
cat > afterburner_levels.nml << EOL 
&AFTERBURNER_LEVELS
  LEVELS = 1, 5, 10, 30, 50, 100, 160, 250, 400, 630, 1000,  1585, 2512,  3981, 
  6310, 10000, 15000, 20000, 25000, 35000, 45000, 55000, 65000, 75000, 80000, 
  85000, 90000, 95000, 98000, 100000 /
EOL
#
LSOCOL=.TRUE.     # Switch for SOCOL model
LCHEM=.TRUE.      # Switch for chemistry module
#
ECHAM5DIR=/cluster/home/sutimofe/socol_aer_3
WORKDIR=/cluster/work/climate/stenkea/echam5
RUNDIR=/cluster/work/climate/sutimofe
DAT=$RUNDIR/run$EXPNO
DPATH=$DAT/

if [ ! -d $DAT ]
then
  mkdir $DAT
fi

#
################################################################################
#
cd $DPATH               # output and rerun files are written into $DPATH
#
#if [ ! -f $WORKDIR/bin/echam5_${EXPNO} ]
#then
#  cp $ECHAM5DIR/bin/echam5 $WORKDIR/bin/echam5_${EXPNO}
#fi
#if [ $ECHAM5DIR/bin/echam5 -nt $WORKDIR/bin/echam5_${EXPNO} ]
#then
#  cp $ECHAM5DIR/bin/echam5 $WORKDIR/bin/echam5_${EXPNO}
#fi
#
MODEL=$ECHAM5DIR/bin/echam5_${EXPNO}  # <--- path to the executable
#
INI=$WORKDIR/data/T${RES}
INIAMIP=$WORKDIR/data/T${RES}/amip2
INIHADLEY=$WORKDIR/data/T${RES}/hadley
INISOCOL=$WORKDIR/data/SOCOL/T${RES}
#
################################################################################
#
# specification of file structures
#
# stop execution after the first run time error
F_ERRCNT=0
export F_ERRCNT
#
################################################################################
#
rm -f unit.?? sst* ice* rrtadata *.codes atmout chem_initial
#
ln -s  ${INI}/T${RES}L${LEV}_jan_spec.nc      unit.23
ln -s  ${INI}/T${RES}_jan_surf.nc             unit.24
#
################################################################################
#
#  Initial file for chemical species (MEZON):

#ln -s  ${INISOCOL}/chem_initial/T${RES}L${LEV}_restc_y1974_m12.dat  chem_initial
ln -s  ${INISOCOL}/chem_initial/T${RES}L${LEV}_20000restc_197412_sulfur.nc  chem_initial
#
################################################################################
## for climatological sst and ice (LAMIP=F) use:
#ln -s  ${INIAMIP}/T${RES}_amip2sst_clim.nc    unit.20
#ln -s  ${INIAMIP}/T${RES}_amip2sic_clim.nc    unit.96
##
# for AMIP (variable) sst and ice (LAMIP=T) use:

YY=`expr ${RESTARTYEAR} - 1`
while [ ${YY} -le ${EXPERIMENTEYEAR1} ]; do
    #ln -s  ${INIAMIP}/T${RES}_amip2sst_${YY}.nc  sst${YY}
    #ln -s  ${INIAMIP}/T${RES}_amip2sic_${YY}.nc  ice${YY}
    ln -s  ${INIHADLEY}/T${RES}_HadISST_sst_${YY}.nc sst${YY}
    ln -s  ${INIHADLEY}/T${RES}_HadISST_sic_${YY}.nc ice${YY}
    #ln -s  ${INIHADLEY}/T${RES}_HadISST_sst_1990.nc sst${YY}
    #ln -s  ${INIHADLEY}/T${RES}_HadISST_sic_1990.nc ice${YY}
    YY=`expr ${YY} + 1`
done
#
################################################################################
#
ln -s  ${INI}/T${RES}_O3clim2.nc    unit.21
ln -s  ${INISOCOL}/misc/OH_ACCENT_T${RES}.nc unit.22
ln -s  ${INI}/T${RES}_VLTCLIM.nc    unit.90
ln -s  ${INI}/T${RES}_VGRATCLIM.nc  unit.91
ln -s  ${INI}/T${RES}_TSLCLIM2.nc   unit.92
ln -s  ${INI}/surrta_data           rrtadata
#
################################################################################
#
# Boundary conditions for SOCOL:

# Greenhouse gases and ODSs:
############################
#rm -f co2_m ch4_m n2o_m odsmem_m odscll_m odscls_m odsbr_m
rm -f co2_y ch4_y n2o_y odsmem_y odscll_y odscls_y odsbr_y

#ln -s  ${INISOCOL}/../GHG_ODS/co2_1959-2004_m.dat             co2_m
#ln -s  ${INISOCOL}/../GHG_ODS/ch4_1959-2004_m.dat             ch4_m
#ln -s  ${INISOCOL}/../GHG_ODS/n2o_1959-2004_m.dat             n2o_m
#ln -s  ${INISOCOL}/../GHG_ODS/odsext_1959-2004_m.dat          odsmem_m
#ln -s  ${INISOCOL}/../GHG_ODS/odsclsext_chem_1959-2004_m.dat  odscls_m
#ln -s  ${INISOCOL}/../GHG_ODS/odscllext_chem_1959-2004_m.dat  odscll_m
#ln -s  ${INISOCOL}/../GHG_ODS/odsbrext_chem_1959-2004_m.dat   odsbr_m

ln -s  ${INISOCOL}/../GHG_ODS/co2_socol_1950-2100.txt         co2_y
ln -s  ${INISOCOL}/../GHG_ODS/ch4_socol_1950-2100.txt         ch4_y
ln -s  ${INISOCOL}/../GHG_ODS/n2o_socol_1950-2100.txt         n2o_y
ln -s  ${INISOCOL}/../GHG_ODS/odsmem_socol_1950-2100.txt      odsmem_y
ln -s  ${INISOCOL}/../GHG_ODS/odscls_socol_1950-2100.txt      odscls_y
ln -s  ${INISOCOL}/../GHG_ODS/odscll_socol_1950-2100.txt      odscll_y
ln -s  ${INISOCOL}/../GHG_ODS/odsbr_socol_1950-2100.txt       odsbr_y

# Solar irradiation, Schumann-Runge bands/Lyman alpha line heating 
# parameterisation, lookup-tables for photolysis rates:
#######################################################
#rm -f sun_irrad sun_par phot_* 
rm -f sun_irrad_y sun_par_y photolysis* 

#ln -s ${INISOCOL}/../sun/sun_irrad.dat                        sun_irrad
#ln -s ${INISOCOL}/../sun/sun_par.dat                          sun_par

ln -s ${INISOCOL}/../sun/sun_irrad_socol_1950-2007.txt        sun_irrad_y
ln -s ${INISOCOL}/../sun/sun_par_socol_1950-2007.txt          sun_par_y

YY=`expr ${RESTARTYEAR} - 1`
while [ ${YY} -le ${EXPERIMENTEYEAR1} ]; do
    #for MM in 01 02 03 04 05 06 07 08 09 10 11 12; do
    #	ln -s ${INISOCOL}/../sun/phot_${YY}${MM}.dat phot_${YY}${MM}
    #done
    #YY=`expr ${YY} + 1`
    ln -s ${INISOCOL}/../sun_sulfur/photolysis_socol_${YY}.nc photolysis${YY}
    YY=`expr ${YY} + 1`
done
## Mean 1977-1998:
#for MM in 01 02 03 04 05 06 07 08 09 10 11 12; do
#    ln -s ${INISOCOL}/../sun/phot_mean7798_${MM}.dat phot_mean_${MM}
#done
ln -s ${INISOCOL}/../sun_sulfur/photolysis_socol_mean_1977_1998.nc photolysis_mean # mean 1977-1998 (two solar cycles)

# Delta-E photolysis corrections:
#ln -s ${INISOCOL}/../sun/L${LEV}_phot_delta_e_corr.nc         phot_delta_e_corr
ln -s ${INISOCOL}/../sun/L${LEV}_photolysis_delta_e_corr_socol.nc photolysis_delta_e_corr

# Stratospheric aerosols:
#########################
#rm -f strata?? strata????
rm -f strataer*

YY=`expr ${RESTARTYEAR} - 1`
while [ ${YY} -le ${EXPERIMENTEYEAR1} ]; do
#    ln -s  ${INISOCOL}/strat_aerosols/T${RES}L${LEV}_sad_nd_ext_omega_g_socol_1990_prov.dat strata${YY}

#    ln -s  ${INISOCOL}/strat_aerosols/T${RES}L${LEV}_sad_nd_ext_omega_g_socol_${YY}.nc strataer${YY}
    ln -s ${INISOCOL}/strat_aerosols/T${RES}L${LEV}_sad_nd_ext_omega_g_socol_mean_1995_2002.nc strataer${YY}
    YY=`expr ${YY} + 1`
done
#ln -s  ${INISOCOL}/strat_aerosols/T${RES}L${LEV}_sad_nd_ext_omega_g_socol_mean_9502_prov.dat stratabg  # background aerosol climatology (1995-2002)
ln -s  ${INISOCOL}/strat_aerosols/T${RES}L${LEV}_sad_nd_ext_omega_g_socol_mean_1995_2002.nc strataerbg


# Troposperic aerosol climatology:
##################################
#rm -f tropoa??
rm -f tropoaer

#for MM in 01 02 03 04 05 06 07 08 09 10 11 12; do
#    ln -s  ${INISOCOL}/tropo_aerosols/T${RES}L${LEV}_tropoaero_socol_${MM}.dat tropoa${MM}
#done
ln -s  ${INISOCOL}/tropo_aerosols/T${RES}L${LEV}_tropoaero_socol_clim.nc tropoaer


# CO and NOx emissions:
#######################
rm -f co_nox_emiss_surf_aircr???? nox_lightning

YY=`expr ${RESTARTYEAR} - 1`
while [ ${YY} -le ${EXPERIMENTEYEAR1} ]; do
    ln -s  ${INISOCOL}/co_nox/T${RES}L${LEV}_co_nox_emiss_surf_aircr_socol_${YY}.nc co_nox_emiss_surf_aircr${YY}
    YY=`expr ${YY} + 1`
done
ln -s  ${INISOCOL}/co_nox/T${RES}L${LEV}_nox_lightning_socol.nc nox_lightning

# SO2, OCS surface emission, DMS sea concentration
###################################################
rm -f so2_surf_emiss* ocs_surf_emiss* dms_sea_con* cs2_surf_emiss* band_echam* so2_cvol_emiss*

ln -s ${INISOCOL}/band_echam5_aer_2012.dat band_echam5
YY=`expr ${RESTARTYEAR} - 1`
while [ ${YY} -le ${EXPERIMENTEYEAR1} ]; do
    ln -s  ${INISOCOL}/sulfur/T${RES}L39_SO2_surf_emiss_${YY}.nc  so2_surf_emiss${YY}
    ln -s  ${INISOCOL}/sulfur/T${RES}L39_OCS_surf_emiss_${YY}.nc ocs_surf_emiss${YY}
    YY=`expr ${YY} + 1`
done
ln -s  ${INISOCOL}/sulfur/T${RES}L39_DMS_sea_concen.nc dms_sea_con
ln -s  ${INISOCOL}/sulfur/T${RES}L39_CS2_surf_emiss.nc cs2_surf_emiss
ln -s  ${INISOCOL}/sulfur/T${RES}_so2_cvol_emiss.nc    so2_cvol_emiss
# QBO:
######
rm -f qbo????

YY=`expr ${RESTARTYEAR} - 1`
while [ ${YY} -le ${EXPERIMENTEYEAR1} ]; do
    ln -s  ${INISOCOL}/../QBO/QBO_socol_${YY}.nc qbo${YY}
    YY=`expr ${YY} + 1`
done
#ln -s ${INISOCOL}/../QBO/QBO_socol_2003_NaN.nc qbo2003
 
#
#################################################################################
# Prepare rerun files (if necessary):

if [ ${RESTARTYEAR} -gt ${EXPERIMENTSYEAR} -o ${RESTARTMONTH} -gt 01 ]; then
    if [ ${RESTARTMONTH} -eq 01 ]; then
	MM1=12
	YY1=`expr ${RESTARTYEAR} - 1`
    else
	MM1=`expr ${RESTARTMONTH} - 1`
	YY1=${RESTARTYEAR}
    fi
    if [ ${MM1} -le 9 ]; then
	MM1=0${MM1}
    fi

    if [ ${EXPNO1} -ne ${EXPNO} ]; then
       cp -f /cluster/home/sutimofe/run${EXPNO1}/${EXPNO1}_rerun_${YY1}${MM1}_echam.nc.gz $DPATH/${EXPNO}_rerun_${YY1}${MM1}_echam.nc.gz
       cp -f /cluster/home/sutimofe/run${EXPNO1}/${EXPNO1}_rerun_${YY1}${MM1}_chem1.nc.gz $DPATH/${EXPNO}_rerun_${YY1}${MM1}_chem1.nc.gz
       cp -f /cluster/home/sutimofe/run${EXPNO1}/rerun_${EXPNO1}_aero2 $DPATH/rerun_${EXPNO}_aero2
       cp -f /cluster/home/sutimofe/run${EXPNO1}/rerun_${EXPNO1}_chem2 $DPATH/rerun_${EXPNO}_chem2
       cp -f /cluster/home/sutimofe/run${EXPNO1}/rerun_${EXPNO1}_tracer $DPATH/rerun_${EXPNO}_tracer
       cp -f /cluster/home/sutimofe/run${EXPNO1}/rerun_${EXPNO1}_chem_m $DPATH/rerun_${EXPNO}_chem_m
       cp -f /cluster/home/sutimofe/run${EXPNO1}/rerun_${EXPNO1}_echam $DPATH/rerun_${EXPNO}_echam
    fi
    
#     a) ECHAM5:
    if [ ! -s ${EXPNO}_rerun_${YY1}${MM1}_echam.nc.gz ]; then
     	echo ${EXPNO}_rerun_${YY1}${MM1}_echam.nc.gz does not exist!
    	exit
    fi
    cp -f ${EXPNO}_rerun_${YY1}${MM1}_echam.nc.gz rerun_${EXPNO}_echam.gz
    gunzip -f rerun_${EXPNO}_echam.gz
 
    if [ ${LSOCOL} = .TRUE. -a ${LCHEM} = .TRUE. ]; then 
#     b) MEZON:
    	if [ ! -s ${EXPNO}_rerun_${YY1}${MM1}_chem1.nc.gz ]; then
    	    echo ${EXPNO}_rerun_${YY1}${MM1}_chem1.nc.gz does not exist!
    	    exit
    	fi
    	cp -f ${EXPNO}_rerun_${YY1}${MM1}_chem1.nc.gz rerun_${EXPNO}_tracer.gz
    	gunzip -f rerun_${EXPNO}_tracer.gz

#     c) chem2 and chem_m streams 
#        (no restart fields included, but to be provided nevertheless):
#    	if [ ! -s rerun_${EXPNO}_chem2 ]; then
#    	    if [ -s $WORK_DIR/rerun_chem/runnr_rerun_chem2.nc ]; then
#    		cp -f $WORK_DIR/rerun_chem/runnr_rerun_chem2.nc rerun_${EXPNO}_chem2
#    	    else
#    		echo Neither ${EXPNO}_rerun_chem2.nc nor $WORK_DIR/rerun_chem/runnr_rerun_chem2.nc exists!
#    	 	exit
#    	    fi
#    	fi
#    	if [ ! -s rerun_${EXPNO}_chem_m ]; then
#    	    if [ -s $WORK_DIR/rerun_chem/runnr_rerun_chem_m.nc ]; then
#    		cp -f $WORK_DIR/rerun_chem/runnr_rerun_chem_m.nc rerun_${EXPNO}_chem_m
#    	    else
#    		echo Neither ${EXPNO}_rerun_chem_m.nc nor $WORK_DIR/rerun_chem/runnr_rerun_chem_m.nc exists!
#    		exit
#    	    fi
#    	fi
    fi

    RERUN=.TRUE.     # Rerun switch; .false. for initial run, .true. for rerun
else
    RERUN=.FALSE.
fi

rm -f fortran_error_messages
#
#######################################################################################################################################################
#
# Set namelist and call model:  
YY=${RESTARTYEAR}

while [ ${YY} -le ${EXPERIMENTEYEAR} ]; do
    for ((MM=${RESTARTMONTH}; MM<=12; MM++)); do
       MM2=${MM}
       if [ ${MM} -le 9 ]; then
	   MM2=0${MM}
       fi

       echo ${MM}!!!!!!!!!!!! ${EXPNO}_${YY}${MM2}.01_chem_m.nc

	if [ ${YY} -gt ${RESTARTYEAR} -o ${MM} -ge ${RESTARTMONTH} ]; then

	    if [ -s namelist.echam ]; then
		rm -f namelist.echam
	fi

#  namelist control variables and output control for grid variables
#  spectral variables are written out by default except liq. water
#  for production runs set LABORT=.FALSE.
#
	    cat > namelist.echam << EOF
&RUNCTL
  LSOCOL=${LSOCOL}
  LRESUME=$RERUN,
  OUT_DATAPATH = "$DPATH"
  OUT_EXPNAME  = "$EXPNO"
  OUT_FILETYPE = 2
  TRAC_FILETYPE = 2
  DT_START  = ${EXPERIMENTSYEAR},01,01,12,0,0
  DT_STOP   = ${EXPERIMENTEYEAR},01,01,12,0,0
  NO_CYCLES = 1
  PUTDATA   = 12, 'hours', 'first', 0
  PUTRERUN  = 1, 'months', 'last', 0
  DELTA_TIME = 600
  LAMIP=.TRUE.
  LMIDATM=.TRUE.
  LABORT=.FALSE.
  NPROCA=${NPROCA}
  NPROCB=${NPROCB}
  NPROMA=${NPROMA}
/
&SOCOLCTL
  LCHEM=${LCHEM}
  lradcoup=.true.
  CYEAR=1991
  delta_time_chem=7200.
  lvolcano=.true.
  lqbonudg=.false.
  nastep=120
/
&DYNCTL
  VCHECK=235.
  SPDRAG=0.926E-4
/
&RADCTL
  IAERO=10
  LGADSRH=.TRUE.
/
&SET_STREAM_ELEMENT  name = 'ClOx_m'   lpost = 0  /
&SET_STREAM_ELEMENT  name = 'Cly_m'    lpost = 0  /
&SET_STREAM_ELEMENT  name = 'CCly_m'   lpost = 0  /
&SET_STREAM_ELEMENT  name = 'Bry_m'    lpost = 0  /
&SET_STREAM_ELEMENT  name = 'CBry_m'   lpost = 0  /
&SET_STREAM_ELEMENT  name = 'NOx_m'    lpost = 0  /
&SET_STREAM_ELEMENT  name = 'NOy_m'    lpost = 0  /
&SET_STREAM_ELEMENT  name = 'amlcorac' lpost = 0  /
&SET_STREAM_ELEMENT  name = 'friac'    lpost = 0  /
&SET_STREAM_ELEMENT  name = 'ustri'    lpost = 0  /
&SET_STREAM_ELEMENT  name = 'vstri'    lpost = 0  /
&SET_STREAM_ELEMENT  name = 'ustrw'    lpost = 0  /
&SET_STREAM_ELEMENT  name = 'vstrw'    lpost = 0  /
&SET_STREAM_ELEMENT  name = 'ustrl'    lpost = 0  /
&SET_STREAM_ELEMENT  name = 'vstrl'    lpost = 0  /
&SET_STREAM_ELEMENT  name = 'ahfliac'  lpost = 0  /
&SET_STREAM_ELEMENT  name = 'ahflwac'  lpost = 0  /
&SET_STREAM_ELEMENT  name = 'ahfllac'  lpost = 0  /
&SET_STREAM_ELEMENT  name = 'evapiac'  lpost = 0  /
&SET_STREAM_ELEMENT  name = 'evapwac'  lpost = 0  /
&SET_STREAM_ELEMENT  name = 'evaplac'  lpost = 0  /
&SET_STREAM_ELEMENT  name = 'az0i'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'az0w'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'az0l'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'ahfsiac'  lpost = 0  /
&SET_STREAM_ELEMENT  name = 'ahfswac'  lpost = 0  /
&SET_STREAM_ELEMENT  name = 'ahfslac'  lpost = 0  /
&SET_STREAM_ELEMENT  name = 'alsoi'    lpost = 0  /
&SET_STREAM_ELEMENT  name = 'alsow'    lpost = 0  /
&SET_STREAM_ELEMENT  name = 'alsol'    lpost = 0  /
&SET_STREAM_ELEMENT  name = 'ahfice'   lpost = 0  /
&SET_STREAM_ELEMENT  name = 'qres'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'apmeb'    lpost = 0  /
&SET_STREAM_ELEMENT  name = 'sn'       lpost = 0  /
&SET_STREAM_ELEMENT  name = 'xivi'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'relhum'   lpost = 0  /
&SET_STREAM_ELEMENT  name = 'runoff'   lpost = 0  /
&SET_STREAM_ELEMENT  name = 'drain'    lpost = 0  /
&SET_STREAM_ELEMENT  name = 'u10'      lpost = 0  /
&SET_STREAM_ELEMENT  name = 'v10'      lpost = 0  /
&SET_STREAM_ELEMENT  name = 'dew2'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'wind10'   lpost = 0  /
&SET_STREAM_ELEMENT  name = 'slm'      lpost = 0  /
&SET_STREAM_ELEMENT  name = 'ustr'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'vstr'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'wl'       lpost = 0  /
&SET_STREAM_ELEMENT  name = 'vdisgw'   lpost = 0  /
&SET_STREAM_ELEMENT  name = 't2max'    lpost = 0  /
&SET_STREAM_ELEMENT  name = 't2min'    lpost = 0  /
&SET_STREAM_ELEMENT  name = 'grndflux' lpost = 0  /
&SET_STREAM_ELEMENT  name = 'ahfcon'   lpost = 0  /
&SET_STREAM_ELEMENT  name = 'ahfres'   lpost = 0  /
&SET_STREAM_ELEMENT  name = 'siced'    lpost = 0  /
&SET_STREAM_ELEMENT  name = 'gld'      lpost = 0  /
&SET_STREAM_ELEMENT  name = 'sni'      lpost = 0  /
&SET_STREAM_ELEMENT  name = 'wimax'    lpost = 0  /
&SET_STREAM_ELEMENT  name = 'topmax'   lpost = 0  /
&SET_STREAM_ELEMENT  name = 'snmel'    lpost = 0  /
&SET_STREAM_ELEMENT  name = 'apmegl'   lpost = 0  /
&SET_STREAM_ELEMENT  name = 'snacl'    lpost = 0  /
&SET_STREAM_ELEMENT  name = 'fao'      lpost = 0  /
&SET_STREAM_ELEMENT  name = 'rgcgn'    lpost = 0  /
&SET_STREAM_ELEMENT  name = 'wsmx'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'glac'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'snc'      lpost = 0  /
&SET_STREAM_ELEMENT  name = 'amass'    lpost = 0  /
&SET_STREAM_ELEMENT  name = 'asulfur'  lpost = 1  /
&SET_STREAM_ELEMENT  name = 'anrate'   lpost = 1  /
&SET_STREAM_ELEMENT  name = 'effr'     lpost = 1  /
&SET_STREAM_ELEMENT  name = 'OH'     lpost = 1  /
&SET_STREAM_ELEMENT  name = 'hs1'      lpost = 0  /
&SET_STREAM_ELEMENT  name = 'shs2'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'hs2'      lpost = 0  /
&SET_STREAM_ELEMENT  name = 'hs3'      lpost = 0  /
&SET_STREAM_ELEMENT  name = 'rsulf01'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'rsulf02'     lpost = 1  /
&SET_STREAM_ELEMENT  name = 'rsulf03'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'rsulf04'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'rsulf05'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'rsulf06'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'rsulf07'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'rsulf08'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'rsulf09'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'rsulf10'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'rsulf11'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'rsulf12'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'rsulf13'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'rsulf14'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'rsulf15'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'rsulf16'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'rsulf17'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'rsulf18'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'rsulf19'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'rsulf20'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'rsulf21'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'rsulf22'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'rsulf23'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'rsulf24'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'rsulf25'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'rsulf26'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'rsulf27'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'srsulf01'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'srsulf02'     lpost = 1  /
&SET_STREAM_ELEMENT  name = 'srsulf03'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'srsulf04'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'srsulf05'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'srsulf06'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'srsulf07'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'srsulf08'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'srsulf09'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'srsulf10'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'srsulf11'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'srsulf12'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'srsulf13'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'srsulf14'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'srsulf15'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'srsulf16'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'srsulf17'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'srsulf18'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'srsulf19'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'srsulf20'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'srsulf21'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'srsulf22'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'srsulf23'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'srsulf24'     lpost = 1  /
&SET_STREAM_ELEMENT  name = 'srsulf25'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'srsulf26'     lpost = 1  /
&SET_STREAM_ELEMENT  name = 'srsulf27'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'tau01'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'tau02'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'tau03'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'tau04'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'tau05'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'tau06'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'tau07'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'tau08'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'tau09'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'tau10'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'tau11'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'tau12'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'tau13'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'tau14'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'tau15'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'tau16'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'tau17'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'tau18'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'tau19'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'tau20'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'tau21'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'tau22'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'tau23'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'tau24'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'tau25'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'tau26'     lpost = 0  /
&SET_STREAM_ELEMENT  name = 'tau27'     lpost = 0  /
EOF

	    # Call model:
	    date
	    #$MODEL
	    mpirun $MODEL

      
	    # Rename output files:
	    # a) ECHAM5:
#	    mv -f ${EXPNO}_${YY}${MM}.01 ${EXPNO}_${YY}${MM}_echam
#	    mv -f ${EXPNO}_${YY}${MM}.01.codes ${EXPNO}_echam.codes

	    # b) MEZON:
#	    if [ ${LSOCOL} = .TRUE. -a ${LCHEM} = .TRUE. ]; then
#		mv -f ${EXPNO}_${YY}${MM}.01_tracer ${EXPNO}_${YY}${MM}_chem1
# 		mv -f ${EXPNO}_${YY}${MM}.01_tracer.codes ${EXPNO}_chem1.codes
# 		mv -f ${EXPNO}_${YY}${MM}.01_chem2 ${EXPNO}_${YY}${MM}_chem
# 		mv -f ${EXPNO}_${YY}${MM}.01_chem2.codes ${EXPNO}_chem2.codes
#		mv -f ${EXPNO}_${YY}${MM}.01_chem_m.nc ${EXPNO}_chem_m_${YY}${MM}.nc
#	    fi

	    # Save restart files:
            # a) ECHAM5:
	    cp -f rerun_${EXPNO}_echam ${EXPNO}_rerun_${YY}${MM2}_echam.nc
	    gzip -f ${EXPNO}_rerun_${YY}${MM2}_echam.nc

            # b) MEZON:
	    if [ ${LSOCOL} = .TRUE. -a ${LCHEM} = .TRUE. ]; then
		cp -f rerun_${EXPNO}_tracer ${EXPNO}_rerun_${YY}${MM2}_chem1.nc
		gzip -f ${EXPNO}_rerun_${YY}${MM2}_chem1.nc
	    
#	           # c) chem2 (MEZON):
#	           cp -f rerun_${EXPNO}_chem2 ${EXPNO}_rerun_${YY}${MM2}_chem2.nc
#	           gzip -f ${EXPNO}_rerun_${YY}${MM2}_chem2.nc

#	      # d) ch4:
#		   cp -f rerun_${EXPNO}_ch4 ${EXPNO}_rerun_${YY}${MM2}_ch4.nc
#	           gzip -f ${EXPNO}_rerun_${YY}${MM2}_ch4.nc
	    fi


#	    # Postprocessing for chem_m.nc files:
#	    if [ ${LSOCOL} = .TRUE. -a ${LCHEM} = .TRUE. ]; then

#		# Call echamnetcdf_afterburner (!CLUSTER!):
#		$WORK_DIR/../burn/echamnetcdf_afterburner ${EXPNO}_${YY}${MM2}.01_chem_m.nc echamnetcdf_afterburner_out.nc $WORK_DIR/afterburner_levels ${EXPERIMENT_SYEAR} >> fortran_error_messages
#		if [ ! -s fortran_error_messages ]; then
#		    mv -f echamnetcdf_afterburner_out.nc ${EXPNO}_${YY}${MM2}.01_chem_m.nc
#                  echo !!!!!!!!!
#		    rm -f fortran_error_messages  
#		fi
		
#		# Concatenate chem_m.nc files at the end of every year (!CLUSTER!):
#		if [ ${MM} -eq 12 ]; then
#		    LNCEX=.TRUE.
#		    for MI in 01 02 03 04 05 06 07 08 09 10 11 12; do
#			if [ ! -s ${EXPNO}_chem_m_${YY}${MI}.nc ]; then
#			    LNCEX=.FALSE.
#			fi
#		    done
#		    if [ ${LNCEX} = .TRUE. ]; then 
#			ncrcat -n 12,2,1 -O ${EXPNO}_chem_m_${YY}01.nc ${EXPNO}_chem_m_${YY}.nc
#			if [ $? -eq 0 ]; then 
#			    rm -f ${EXPNO}_chem_m_${YY}??.nc
#			fi
#		    fi
#		fi
#	    fi

	    RERUN=.true.

           cdo monmean ${EXPNO}_${YY}${MM2}.01_aero2.nc ${EXPNO}_${YY}${MM2}.01_aero2_m.nc
           rm ${EXPNO}_${YY}${MM2}.01_aero2.nc
           cdo monmean ${EXPNO}_${YY}${MM2}.01.nc ${EXPNO}_${YY}${MM2}.01_m.nc
           rm ${EXPNO}_${YY}${MM2}.01.nc
           cdo monmean ${EXPNO}_${YY}${MM2}.01_tracer.nc ${EXPNO}_${YY}${MM2}.01_tracer_m.nc
           rm ${EXPNO}_${YY}${MM2}.01_tracer.nc
           echo aero2 is converted to monthly

	fi

    done

    YY=`expr ${YY} + 1`
    RESTARTMONTH=1

done

ls -lt
