# Python AMIP reader <!-- omit in toc -->

Conversion from Fortran to Python of the EC Earth 4 project AMIP reader using the OASIS API from pyOASIS. This repository contains tests for running both implementations (Fortran code is taken from [EC Earth v3.3.3.1](https://earth.bsc.es/gitlab/es/auto-ecearth3/-/tree/3.3.3.1)) using the current setup for IFS and AMIP from EC Earth v3.3.3.1.

Running an example consists on executing one of the implementations (Fortran or Python) along with a mock component acting as IFS. This mock component simulates IFS reception behavior as designed in EC Earth v3.3.3.1. The detailed specification can be found below.
```python
# Constants
NX_ATMO = 88838  # number of LAND grid cells at T255 resolution
COUPLING_INTERVAL = 86400
RUN_LENGTH_SEC = 31536000
NUMBER_TIME_STEPS = int(RUN_LENGTH_SEC / COUPLING_INTERVAL)
```

## Table of contents <!-- omit in toc -->
- [Running an example](#running-an-example)
  - [Requirements](#requirements)
    - [Python dependencies](#python-dependencies)
  - [Compile](#compile)
  - [Run](#run)
- [Running the tests](#running-the-tests)


## Running an example

### Requirements
This documentation assumes you are running an UNIX-based OS with Python3 in the PATH, understand the basics of OASIS and are able to compile and run pyOASIS.

#### Python dependencies
The Python implementation relies on `f90nml`, `netCDF4` and `numpy` for running. In order to install these dependencies, you may execute the following code.
```bash
pip3 install f90nml netCDF4 numpy
```

Due to the large size of some of the required files and datasets, they are not provided in this repository. It is necessary to download them from MN before executing any code. Use the following script to download the required files (note you must change `bsc32074` to your username in MN):
```bash
scp -r bsc32074@mn1.bsc.es:/gpfs/projects/bsc32/models/ecearth/v3.3.3.1/inidata/oasis/AMIP/* ./tests/data
scp -r bsc32074@mn1.bsc.es:/gpfs/projects/bsc32/models/ecearth/v3.3.3.1/inidata/amip-forcing/* ./tests/data
```

### Compile
 Each of the example codes is located in its respect folder inside the [`amip-forcing/`](./sources/amip-forcing/) folder ([`src/fortran/`](./sources/amip-forcing/src/fortran/) and [`src/python/`](./sources/amip-forcing/src/python/)). In the source folder for the Fortran code there is a [Makefile](./sources/amip-forcing/src/fortran/Makefile). By executing the following command, you will compile OASIS, pyOASIS and the Fortran implementation of this model.
```bash
(cd ./sources/amip-forcing/src/fortran && make clean)
(cd ./sources/amip-forcing/src/fortran && make oasis)
(cd ./sources/amip-forcing/src/fortran && make)
```

If this is the first time you are compiling pyOASIS, you must issue one of the following commands.
```bash
source oasis3-mct/generated/python/init.sh
```
```bash
source oasis3-mct/generated/python/init.csh
```
Alternatively, these lines could be added to your `~/.bashrc` or `~/.cshrc` file.

### Run
In order to run the example code with your desired programming language you must execute the script `run_example.sh` inside the [`tests/`](./tests/) folder. Here is an example on how to run the example with the AMIP reader running in Python and the IFS mock component with 4 MPI processes.
```bash
bash ./run_example.sh python 4
```

Here is the complete specification for the script:
```bash
./run_example.sh <lang> <ifs_nproc>
```

Running this script will create a new folder in the root folder of this repository called `work_<lang>_<ifs_nproc>` with all the output and debug information from the run.

## Running the tests
Execute the following command in the root folder of this repository to run all tests (make sure to meet all the [requirements](#requirements) stated above).

```bash
bash run_tests.sh
```

This command will run both implementations (Fortran and Python) of the AMIP reader and check if all of them result in the same output. Here is an example of a successfull test run:
```
*****************************************************************
Retrieving IFS_mock outputs...
*****************************************************************
Checking results...
Test PASSED
```
