.. _tuto_tide:

Build tidal forcing
-------------------

Tidal forcing may be important in coastal configurations, and in certain 
locations of the word. 

CROCO can propagate tides if those are provided at its boundaries. 

In case where lateral boundary forcing do not include tides, barotropic tidal 
forcing can be added at CROCO boundaries from a tidal atlas. 
CROCO is therefore able to propagate the different tidal constituents from its 
lateral boundaries by forcing the tidal signal both in elevation and velocity, 
using the method described by Flather and Davies [1976]. 

``make_tides`` creates a forcing file containing the amplitude and phase 
of each of the desired tidal components for tide elevation and currents. 
These values are interpolated over the entire CROCO grid, but the simulation 
only uses the values at boundaries for forcing.  

.. note:: 
  
  To get a clean signal you need to provide harmonic components from both tide 
  elevation and tide velocity. In case you don’t have velocity harmonics 
  (``undef UV_TIDES`` in ``cppdefs.h``) a set of reduced equations is 
  available to compute velocity from SSH (cpp key : ``OBC_REDUCED_PHYSICS``).


Edit the user-defined parameters
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

All the requested user-defined parameters to build the tidal forcing are 
gathered in a configuration file, here named ``tides.ini``. 

This file contains: 

.. literalinclude:: ../../prepro/Examples/benguela_multifiles/tides.ini
   :language: ini

The **[Croco_Files]** section defines the path and prefix for CROCO files:

.. list-table::
  
   * - ``croco_files_dir``
     - Path of the CROCO files directory
   * - ``croco_grd_prefix``
     - Prefix for the CROCO grid file 
   * - ``croco_tide_prefix``
     -  Prefix for CROCO tidal forcing file (usually = croco_frc)

The **[Zoom_Options]** section defines if the grid is the main grid
(parent grid) or a nested grid (zoom or child grid):

.. list-table::
  
   * - ``is_zoom``
     - True/False flag to define if the grid is a zoom or the main grid
   * - ``is_agrif``
     - True/False flag to define if the grid is an agrif nested grid
   * - ``agrif_level``
     - integer
       level of the agrif grid to consider (0 is parent, 1 is first child)

The **[Times]** section defines the choice of dates, used for
eventual corrections to apply:

.. list-table::

  * - ``Ystart``, ``Mstart``, ``Dstart``
    - year, month, day, hour of the chosen start date of the run at which the
      nodal correction is computed if needed
  * - ``Yorig``, ``Morig``, ``Dorig``
    - year, month, day, hour of the chosen time origin date. If using
      ``TIDES_MAS``, it MUST be equal to 1900, otherwise it MUST be consistent
      with the time origin date chosen for IBC and atmospheric forcing.
  * - ``use_calendar``
    - If you put True for this logical, the script force
      origin date to 1900/01/01 regardless of the ``Yorig``, ``Morig``, 
      ``Dorig`` values. Note that if you use ``USE_CALENDAR`` 
      during your CROCO run, origin date for tides must be 1900/01/01. 
      If you use ``TIDES_MAS``, ``USE_CALENDAR`` is mandatory. 
      

The **[Tide_Options]** section defines the choice of tidal components, and the
eventual corrections to apply:

.. list-table::

  * - ``tide_waves``
    - Tidal components to use
  * - ``is_tide_current``
    - True/False flag to compute currents
  * - ``is_tide_potential``
    - True/False flag to compute tidal generating potential
  * - ``is_correction_ssh``
    - True/False flag to apply nodal correction on elevation
  * - ``is_correction_uv``
    - True/False flag to apply nodal correction on currents

The **[Tide_Input_Files]** section defines several parameters regarding the input 
data:

.. list-table::

   * - ``tide_reader`` 
     - keyword of the reader to use as defined in ``readers.jsonc`` 
   * - ``tide_dir`` 
     - path of tidal atlas input data
   * - ``tide_single_file``
     - name of the input tidal atlas file
   * - ``tide_type``
     - format of the input data 'Amp_phase'or 'Re_Im'
   * - ``tide_multi_files``
     - True/False flag to indicate if the different variables are in separated 
       files or in the same input file
   * - ``tide_multi_files_waves_separated``
     - set to True if input file waves are separated
   * - ``tide_multi_files_elev_file``
     - elevation file names. if wave_separated put <tide_wave> where wave name is 
       found
   * - ``tide_multi_files_u_file``
     - eastward currents file names. if wave_separated put <tide_wave> where wave 
       name is found
   * - ``tide_multi_files_v_file``
     - northward currents file names. if wave_separated put <tide_wave> where wave 
       name is found


In most cases, the simulations are only forced by the main tidal components. 
To take disturbance waves into account, a nodal correction is applied to these 
main waves. This nodal correction is of low amplitude and only slightly 
variable (considered constant over a year) but can have a significant impact 
on the tidal solution in coastal areas. 

CROCO's common practice is to set the nodal correction value at the simulation 
start date and keep it constant thereafter. In this case, the correction is 
directly included in the forcing file. However, CROCO also has the ability to 
calculate these corrections at each time step in the simulation with the cppkey 
``TIDES_MAS``. In this case, the nodal correction is not included in the 
forcing file. In the current state, it is therefore necessary to keep the nodal 
correction on currents in the forcing file.

.. warning::

  When using ``TIDES_MAS`` (e.g. nodal correction on elevation computed during 
  the run), ``USE_CALENDAR`` is mandatory, thus you should set 
  ``use_calendar = True``, and you must set ``is_correction_ssh = False``, 
  and choose ``Yorig = 1900``. 


In the provided example of ``tides.ini``, we use as initial and boundary 
conditions the TPXO7 data provided within 
``DATASETS_CROCOTOOLS <https://data-croco.ifremer.fr/DATASETS/TPXO7.tar.gz>``. 
This dataset has the Real/Imaginary format, and is referred with the 
``tpxo7_croco`` keyword in ``readers.jsonc``. 

You can find other tidal Atlas in the following urls :

* `TPXO10 <https://www.tpxo.net/global/tpxo10-atlas>`_ : , 1/30 degree, 
  15 waves: M2, S2, N2, K2, K1, O1, P1, Q1, Mf, Mm, M4, MS4, MN4 , 2N2 and S1
* `FES2022 <https://www.aviso.altimetry.fr/en/data/products/auxiliary-products/global-tide-fes.html>`_ :
  1/30 degree, 34 waves: M2, S2, K1, O1, P1, Q1, Nu2, Mu2, N2, K2, Eps2, L2, 
  Lambda2, M4, 2N2, J1, M3, M6, M8, Mf, MKS2, Mm, MN4, MS4, MSf, MSqm, Mtm, 
  N4, R2, S1, S4, Sa, Ssa,T2
* `Previmer <https://marc.ifremer.fr/produits/atlas_de_composantes_harmoniques>`_ : French 
  coast , several resolutions up to 250m 
   


Check or add reader for tidal atlas data
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  
For TPXO7 data provided within 
``DATASETS_CROCOTOOLS <https://data-croco.ifremer.fr/DATASETS/TPXO7.tar.gz>``, 
the reader writes:

.. code-block:: json

      "tpxo7_croco": {
        "lonr": "lon_r",
        "lonu": "lon_u",
        "lonv": "lon_r",
        "latr": "lat_r",
        "latu": "lat_r",
        "latv": "lat_v",
        "H_r": "ssh_r",
        "H_i": "ssh_i",
        "U_r": "u_r",
        "U_i": "u_i",
        "V_r": "v_r",
        "V_i": "v_i",
        "topor": "h",
        "topou": "h",
        "topov": "h",
        "invert_phase": true


Build the tidal forcing conditions with ``make_tides.py``
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

* Edit the ``tides.ini`` configuration file. 
* Eventually edit the ``readers.jsonc`` reader dictionnary
* Launch the script:

  ::

    python make_tides.py tides.ini

* Check the generated file in the directory defined in ``croco_dir`` 
  with a filename defined as``tides.ini`` to which the reader name will be added. 
  In this example it will be: 

  ``croco_frc_tpxo7_croco.nc``

