Install the environnement
--------------------------

**croco_pytools/prepro** is a python toolbox for pre-processing CROCO input files. 

It uses several python packages (see ``env.yml``), as well as some Fortran scripts 
which have been interfaced with python through 
`f2py <https://numpy.org/doc/stable/f2py/>`_ and must therefore be compiled. 
The Fortran compilers are included in the python environment. 

You just have to follow the two following step to install the whole toolbox:

* Install the python environment
* Compile the Fortran dependencies

Install the ``croco_pyenv`` environment 
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

A global conda environment is available for the three parts of croco_pytools 
in the ``env.yml`` file, located at the root 
of the croco_pytools directory..

You can use your favorite python environment manager 
(e.g. conda, mamba, micromamba, miniforge)
to install the environment named ``croco_pyenv`` with this file.

Example with micromamba :

::

    micromamba create -f env.yml
    micromamba activate croco_pyenv

Example with conda :

::

    conda env create -f env.yml
    conda activate croco_pyenv

.. tip::

    You can make available your ``croco_pyenv`` kernel for 
    Jupyter Notebook by using the command : 
    ``ipython kernel install --user --name=croco_pyenv``

.. note:: 
  
  You will have to activate the ``croco_pyenv`` environment each time you 
  need to use the ``croco_pytools/prepro`` scripts!


Compile Fortran dependencies
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

The required fortran and C compilers are included in the python environment, 
thus your first need to activate your python environment:
::

    micromamba activate croco_pyenv

or

::

    conda activate croco_pyenv

Launch the Fortran routines compilation:
::

    cd prepro/Modules/tools_fort_routines
    make clean
    make

If successfull you should now have a file of this pattern in ``Modules``:
::

    toolsf.*.so


Notes on common errors
^^^^^^^^^^^^^^^^^^^^^^^

You might face some errors while trying to compile fortran tools. 
Here is a list of what have been already encountered by some users and 
the associated solution.

* ifort can raise wn error while compiling. 
  In ``Modules/tools_fort_routines/Makedefs`` try to add 
  ``--fcompiler=intelem`` in ``FFLAGS``.

* ImportError means you have missing librairies. 
  In your terminal do ``nf-config --flibs`` and check that you have 
  ``-lnetcdff -lnetcdf``. 

