{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "# Introduction \n",
    "The Data Coupler is being developed at the BSC to implement a CO2 box model for EC-Earth3 and to read and interpolate CO2 anthropogenic emissions (from e.g. CMIP6 input4MIPS) and CO2 surface fluxes (from e.g. vegetation+fire+ocean fluxes from SiB4). \n",
    "\n",
    "## Getting the model \n",
    "The model can be cloned from (BSC-ES gitlab repository)[https://earth.bsc.es/gitlab/es/python-amip-reader]  \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "git clone https://earth.bsc.es/gitlab/es/python-amip-reader.git\n",
    "cd python-amip-reader\n",
    "git checkout data-coupler"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Setting up the model\n",
    "First copy the datasets from Etienne's directory ```/gpfs/scratch/bsc32/bsc32051/git/python-amip-reader/tests/data/```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "datadir=/gpfs/scratch/bsc32/bsc32051/git/python-amip-reader/tests/data/\n",
    "cd /python-amip-reader\n",
    "cp -r $datadir/forcing/* tests/data/forcing\n",
    "cp -r $datadir/co2/* tests/data/co2"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To build oasis and the optional fortran amip-forcing follow the instructions in https://earth.bsc.es/gitlab/es/python-amip-reader/-/blob/data-coupler/README.md#compile"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "cd /sources/oasis3-mct/util/make_dir\n",
    "cp make.templ.inc make.inc"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Please chnage the path of the make file to include MN4, and then complie the as follows: "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "include  $(curr_path)/make.mn4\n",
    "\n",
    " \n",
    "cd ../sources/amip-forcing/src/fortran\n",
    " \n",
    "module load impi/2017.4\n",
    "module load mkl/2017.4\n",
    "module load intel/2021.4\n",
    "module load python/3.6.1\n",
    "module load netcdf/4.2\n",
    "module load cdo/1.9.3\n",
    "\n",
    "make realclean && make oasis && make"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Running the model\n",
    "To run the model you need to edit file  $HPCPERM/work/python-amip-reader/tests/launch-hpc2020.cmd and make sure the proper configuration is un-commented at the end, for example\n",
    "\n",
    "For a simple AMIP run using the classic fortran amip-forcing:   \n",
    "\n",
    "bash ./run_example.sh fortran forcing 4 2005-01-01 2005-03-01 0 128 true false\n",
    "\n",
    "For the CO2 box model :\n",
    "\n",
    "bash ./run_example.sh co2box co2 4 2005-01-01 2005-05-01 0 128 true false\n",
    "\n",
    "The following lines hard-coded in run_example.sh control the options of the CO2 box model\n",
    "\n",
    "        CO2_CMODE=true # set to false to disable co2 concentrations reading\n",
    "        CO2_EMODE=true # set to false to disable emissions reading\n",
    "\n",
    "\n",
    "\n",
    "There are 2 ways to run the model: \n",
    "\n",
    "1) run an interactive session and launch the job with bash (for short runs and debugging)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ecinteractive -c6 -t 12:00:00 -m 32GB\n",
    "cd $HPCPERM/work/python-amip-reader/tests/\n",
    "bash launch-hpc2020.cmd"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2) launch the job with sbatch (for longer runs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "cd $HPCPERM/work/python-amip-reader/tests/\n",
    "sbatch launch-hpc2020.cmd"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If the run is successful, output will be present in a folder representing the run you made e.g. work_fortran_forcing_4_2005-01-01_2005-03-01_0_128_true\n",
    "\n",
    "Some files are post-processed with proper grid and time metadata applied."
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
