I/O and Online Diagnostics
==========================

General ouptut options
----------------------

================== =======================================================================
AVERAGES           Process and output time-averaged data
AVERAGES_K         Process and output time-averaged vertical mixing
PARALLEL_FILES     Output one file per CPU
NC4PAR             Use NetCDF4 parallel capabilities
XIOS               Use XIOS IO server (only version >= 2 is supported)
================== =======================================================================

XIOS is an external library for output (developed at IPSL) providing for flexibility and 
design to improve performances for HPC : see http://forge.ipsl.jussieu.fr/ioserver

*Preselected options:*
::

# define AVERAGES 
# define AVERAGES_K 
# undef PARALLEL_FILES
# undef NC4PAR
# undef XIOS


By default, with MPI activated input and output files are treated in a
pseudo-sequential way, and one NetCDFfile corresponds to the whole domain.
This has drawbacks when using a large number of computational cores, since each
core is writing its part of the domain sequentially, the time dedicated to outputs
increase with the number of cores. Three alternatives are implemented within CROCO.

**Splited files (#define PARALLEL_FILES)**

In this case, each core is writing its part only of the domain in separated files
(one per MPI domain). This writing is performed concurrently. One other advantage is
to avoid the creation of huge output files. The domain related output files can be
recombined using ncjoin utility (in fortran) compiled in the same time than CROCO.
Note that in this case, input files have to be splited as well, using partit utility.

**Parallel NetCDF(#define NC4PAR)**

This option requires NetcDF4 verion, installed with parallel capabilities. All cores
are writing concurrently but in the same time.

**IO server (#define XIOS)**

XIOS is an external IO server interfaced with CROCO. Informations about use and
installation can be found there https://forge.ipsl.jussieu.fr/ioserver. In this case,
output variables are defined in .xml files. See also the Diagnostics chapter.


Advanced diagnostics options
----------------------------

======================= =======================================================================
DIAGNOSTICS_TS          Store and output budget terms of the tracer equations
DIAGNOSTICS_TS_ADV      Choose advection rather than transport formulation for tracer budgets
DIAGNOSTICS_TS_MLD      Integrate tracer budgets over the mixing layer depth (hbl, defined as the turbulent layer from the mixing param)
DIAGNOSTICS_TS_MLD_DENS Integrate tracer budgets over the mixed-layer depth (MLD, defined with criterion in density)
DIAGNOSTICS_TSVAR       Store and output budget terms of the tracer variance equations (instead of tracer)
DIAGNOSTICS_UV          Store and output budget terms of the momentum equations. 
DIAGNOSTICS_BARO        Isolate contribution from barotropic/baroclinic coupling (included in the 
                        vertical mixing term otherwise) for momentum, barotropic vorticity and kinetic energy budgets
DIAGNOSTICS_VRT         Store and output budget terms of the barotropic vorticity equation
DIAGNOSTICS_EK          Store and output budget terms of the kinetic energy equation (vertically integrated)
DIAGNOSTICS_EDDY        Store and output time-averaged quadratic quantities u^2, v^2, u*v, u*w, v*w, u*b, 
                        v*b, w*b, u*sustr, v*svstr, u*bustr, v*bvstr, zeta^2
DIAGNOSTICS_PV          Store and output non conservative term in the momentum equations and diabatic term 
                        in the tracer equations. if DIAGNOSTICS_DISS is also defined, terms are multiplied by momentum and 
                        thermal/saline expension coefficients to be used to estimate kinetic and potential energy dissipation.

======================= =======================================================================

*Preselected options:*
::

# undef DIAGNOSTICS_TS 
# undef DIAGNOSTICS_TS_ADV
# undef DIAGNOSTICS_TS_MLD
# undef DIAGNOSTICS_TS_MLD_DENS
# undef DIAGNOSTICS_TSVAR 
# undef DIAGNOSTICS_UV
# undef DIAGNOSTICS_BARO
# undef DIAGNOSTICS_VRT
# undef DIAGNOSTICS_EK
# undef DIAGNOSTICS_PV
# undef DIAGNOSTICS_EDDY


Tracer budget terms
^^^^^^^^^^^^^^^^^^^

**DIAGNOSTICS_TS** computes the tracer budgets in 3D.

**DIAGNOSTICS_TS_MLD** computes the tracer budgets in 2D:

* either over the mixing layer (*hbl*, defined as the turbulent layer from the vertical mixing parametrization)
* or over the mixed layer (MLD, defined as the homogeneous surface layer in density) if **DIAGNOSTICS_TS_MLD_DENS** is defined in addition.

.. note ::

    To integrate tracer budgets over the mixed layer depth, the CPP key **LMD_SKPP** needs to be activated.

Tracer budgets will be stored in **croco_dia.nc** (instantaneous) and/or **croco_dia_avg.nc**
(average) output files.

Mixing layer calculation
~~~~~~~~~~~~~~~~~~~~~~~~

The turbulent mixing layer (hbl) is defined depending on the mixing parameterization (see :ref:`vertical-mixing-parametrizations`).

Mixed layer calculation
~~~~~~~~~~~~~~~~~~~~~~~

The mixed layer depth (MLD) is generally defined as the slice of the surface ocean where
temperature, salinity and density are homogeneous.
In reality, the mixed layer is not perfectly homogeneous, and a threshold value
is applied to determine when temperature/density is no longer considered
homogeneous. 
In addition, in regions of heavy rainfall (e.g. in some tropical regions), 
the density-homogeneous layer and the thermal-homogeneous layer may be different, and a salt barrier layer (BL) can form. 

The MLD is thus evaluated in CROCO under the **DIAGNOSTICS_TS_MLD_DENS** key as the depth of homogeneous density,
i.e. where the density is equal to the surface density plus
a threshold defined as 0.03 kg/m³ by default (according to :cite:`deboyer_mixedlayer_2004`), but that can be modified by the user in **croco.in**.
Starting from the surface, the mixed layer level corresponds to the last
level that satisfies the following condition:

.. math::

   D(x,y,k,t) \le D(x,y,N,t) + D_{\text{threshold}}

where :math:`D(x,y,k,t)` is the density, and :math:`D_{\text{threshold}}`
is 0.03 kg/m\ :sup:`3` by default (this value can be modified by the user
in **croco.in**).

The MLD is stored in the variable ``dens_mld_depth``.

Barrier layer computation
~~~~~~~~~~~~~~~~~~~~~~~~~

In addition the isothermal layer depth is computed, to allow the evaluation of an eventual BL.
Starting from the surface, the isothermal layer is computed as the last level that satisfies the following condition:

.. math::

   T(x,y,k,t) \ge T(x,y,N,t) - T_{\text{threshold}}

where :math:`T(x,y,k,t)` is the temperature, and :math:`T_{\text{threshold}}`
is 0.2°C by default (this value can be modified by the user
in **croco.in**).

The isothermal layer depth is stored in the variable ``temp_mld_depth``.

From ``dens_mld_depth`` and ``temp_mld_depth`` the BL thickness (BLT) can be computed:

.. math::

   BLT = temp\_mld\_depth - dens\_mld\_depth


.. figure:: figures/schema_MLD_BLT.png
   :align: center

   Fig: schematic view of the calculation of the MLD based on the
   density criterion, and the calculation of the BLT (when existing, left).


MLD thresholds
~~~~~~~~~~~~~~

The thresholds are defined and can be modified in the **croco.in** file (or **croco_inter.in**):

::

   diag_mld_dens: RHO threshold [kg/m3], T threshold [Celsius]
                             3.d-2                2.d-1


.. note ::

    These values depend on the study area and default values are generally valid for deep waters.
    The threshold values and the reference depth are user-modifiable parameters.


Budget terms outputs
~~~~~~~~~~~~~~~~~~~~

The temperature and salinity budget terms over the homogenous-density MLD are computed, and stored in the following variables:

- ``temp_aver_mld / salt_aver_mld``: temperature/salinity averaged over the MLD
- ``temp_rate_mld / salt_rate_mld``: temperature/salinity MLD rate of change term
- ``temp_entr_mld / salt_entr_mld``: temperature/salinity MLD entrainment term
- ``temp_xadv_mld / salt_xadv_mld``: temperature/salinity MLD x-component horizontal advection term
- ``temp_yadv_mld / salt_yadv_mld``: temperature/salinity MLD y-component horizontal advection term
- ``temp_vadv_mld / salt_vadv_mld``: temperature/salinity MLD vertical advection term
- ``temp_hmix_mld / salt_hmix_mld``: temperature/salinity MLD horizontal mixing term
- ``temp_vmix_mld / salt_vmix_mld``: temperature/salinity MLD vertical mixing term
- ``temp_forc_mld / salt_forc_mld``: temperature/salinity MLD forcing term


The user can choose which variables to include in the output files by flagging the tracers with T (True) or F (False).

Temperature and salinity are examples of tracers. 

::

   diag3D_history_fields:    diag_tracers3D(1:NT)
                    T T 50*F

   diag2D_history_fields:    diag_tracers2D(1:NT)
                    T T 50*F

   diag3D_average_fields:    diag_tracers3D_avg(1:NT)
                    T T 50*F

   diag2D_average_fields:    diag_tracers2D_avg(1:NT)
                    T T 50*F


Energy budget terms
^^^^^^^^^^^^^^^^^^^

The different budgets and their computation are detailled in https://www.jgula.fr/Croco/diagnostics_croco.pdf

