
Preparation of forcing files
============================

Mesh building with BMGTOOLS 
----------------------------

#. Get BMGTOOLS here 
   ::

      mkdir BMG 
      cp /home/datawork-mars/TOOLS/BATHY/BMGTOOLS/bmg-linux64b-rev1489.tar.gz .
      tar -xzvf bmg-linux64b-rev1489.tar.gz


#. Open the **Create module** in a terminal 
   ::
   
      cd create_bmg-5.0.0
      ./CreateBMG.sh

   .. warning :: If a memory is requested put 4GO 
   
#. Get the appropriate coastline to build your configuration here 
   ::

      /home/datawork-croco/datarmor-only/DATA/COASTLINE/BMGTOOLS_FORMAT/france.line 
      /home/datawork-croco/datarmor-only/DATA/COASTLINE/BMGTOOLS_FORMAT/europa.closed.line 
      /home/datawork-croco/datarmor-only/DATA/COASTLINE/BMGTOOLS_FORMAT/med_sea.line

#. Create a new project (Top left button)

#. Create a grid with the following features (Button on the top right bar) and follow the instructions 


   * Click and drag on the map to define approximatively your domain
   * In the popup window you can define :

     * The limits of your area of interest
     * If you want to choose your grid resolution by meters choose the option **Grid defined by curvilinear mesh size** 
        

#. Save your project and check in the directory that you have a file head.TEST with the features of your grid 
   ::
        
       TEST0  65.0000000  40.0000000  15.0000000 -20.0000000   0.0500000   0.0833333        5662.40   5563.84 421 501 167 158 190 180 TEST



   .. image :: FIG/FORMATION/formation_bmg1.png
                :width: 800px
                :height: 600px

   
#. Interpolation of the bathymetry on the grid

   * Get the fortran executable, the associated namelist and the batch 
     ::
 
         /home/datawork-mars/TOOLS/BATHY/INTERP/interp_bathy/INTERP_BATHY.exe
         /home/datawork-mars/TOOLS/BATHY/INTERP/interp_bathy/namelist
         /home/datawork-mars/TOOLS/BATHY/INTERP/interp_bathy/batch_interp
  
   * Build a text file which list the MNT files you want to use and pickup from here 
     ::

         cat catalog.dat :
         /home/datawork-croco/datarmor-only/DATA/MNT_HOMONIM/MNT_ATL100m_HOMONIM_WGS84_NM.nc 
     
   * Edit the namelist 
     ::
    
         Choose the grid2grid mode 
         &flags
         l_interp_soundings2grid=.false.
         l_interp_grid2grid=.true.
         l_smooth=.false.
         l_connect=.false. /

         &interp_soundings2grid
         coastfile='/home/datawork-croco/datarmor-only/DATA/TDC/france.line'

         &interp_grid2grid
         data_catalog='catalog.cat'
         l_bathy_bmg=.true.
         l_closed_line=.true.
         landvalue=-999
         grid_file='RootGrid.nc'
         nivmoypath=''
         l_bathy_threshold=.true.
         bathy_threshold=2.0
         mask_method='HXHY' /

   * Launch the executable  
     ::
      
         qsub batch_interp 

#. Open in another terminal the **Check BMG** module to view and edit (if needed) your bathymetry

   * Open 
     ::

         cd check_bmg-5.0.0
         ./CheckBMG.sh

   * Load your grid file (*RootGrid.nc*) and your coastline file


   * You can edit your grid with the button in the top right pannel with different ways 

     * Single (one mesh )
     * Sequential (several meshes in sequential)
     * Polygon (group of meshes within a polygon)
     * Rectangle (groupe meshes within a rectangle)

     You juste have to select the nodes and edit the bathymetry values
    
   .. image :: FIG/FORMATION/formation_bmg2.png
       :width: 600px
       :height: 400px
   .. image :: FIG/FORMATION/formation_bmg3.png
       :width: 600px
       :height: 400px
   
   .. warning :: dont forget to save your project to take into acount your modifications 


#. Convert the bathymetry and the grid to CROCO framework

   Preprocessing of files are based on a set of python scripts.

   On Datarmor you can get a python with vacumm 
   ::

      module load vacumm/3.4.0


   * Get the python script from CROCO directory 
     ::

         cp -r /home/datawork-croco/datarmor-only/TRAININGS/TRAINING_2021/MARS2CROCO/BATHY .
         cd BATHY 

   * Edit the python script **convert_bathymars2croco.py** and set user parameters

   * Launch the script 
     ::

         python convert_bathymars2croco.py bathy_file.nc
     
     ==> You get croco_grd.nc

   * Check if your bathy seems ok 
     ::

      ncview croco_grd.nc


Build tidal atlas on CROCO grid 
--------------------------------


To get tide on your OBC, you need an atlas with harmonic constituents on your model grid **croco_grd.nc** 

* First you also need the following script 
  ::

      cp /home/datawork-croco/datarmor-only/TRAININGS/TRAINING_2021/MARS2CROCO/TIDES/convert_fes2croco.py .
      cp /home/datawork-croco/datarmor-only/TRAININGS/TRAINING_2021/MARS2CROCO/TIDES/tides.txt .
      cp /home/datawork-croco/datarmor-only/TRAININGS/TRAINING_2021/batch_python .

* Edit the python script to set the list of constituents you want in your Atlas

* The script need the **croco_grd.nc** file so you need to link it the directory 
  ::

      ln -s ../BATHY/croco_grd.nc .

* Run the script 
  ::
  
      qsub batch_python 
  

.. _OBCIC:

3D Initial and Boundary conditions 
-----------------------------------

This part deals with generation of OBC and IC for your grid, from an Ocean General Circulation Model (exemple :MERCATOR, HYCOM ..)

#. First you have to get the numerical solution which covers your grid and your period of simulation 
   ::

         /home/datawork-croco/datarmor-only/DATA/MERCATOR_SOLUTION

#. The second step is to interpolate this file on your grid. We use a fortran programm for this :

   * Get the following directory 
     ::

            cp -r /home/datawork-croco/datarmor-only/TRAININGS/TRAINING_2021/EXTRACT_CROCO .
            cd EXTRACT
          
   * Edit the namelist 
     ::

            vi IN/namelist

   * Change the mode (IC or OBC at once) 
     ::

            &extractmode
               l_extract_obc=.false.        ! compute Open Boundaries Conditions (set to true)
               l_extract_ic=.false. /       ! compute Initial Conditions         (set to true)

   * Change the name of the input file with the MERCATOR file 
     ::
       
            &namcoarse
               file_coarse = 'MERCATOR_PSY2V4.nc'   ! name of file containing data to be interpolated

   * In this section set the name of init and obc file, input bathy file and activate 
     which obc you want to extract 
     ::

            &namfine
               head_fine = 'head.useless'        ! head.CONF file (a line of the one used in MARS)
               file_ic='init.nc'          ! name of ic  output file
               file_fine_w = 'obc_west.nc'       ! name of west  obc output file
               file_fine_e = 'obc_east.nc'       ! name of east  obc output file
               file_fine_s = 'obc_south.nc'      ! name of south obc output file
               file_fine_n = 'obc_north.nc'      ! name of north obc output file
               l_obc_west  = .false.                  ! Interpolate west  obc ?
               l_obc_east  = .false.                 ! Interpolate east  obc ?
               l_obc_south = .false.                 ! Interpolate south obc ?
               l_obc_north = .false.                 ! Interpolate north obc ?
               obc_width = 2                        ! width of obc domain, must the same than in MARS
               file_bathy_fine = 'bathy_rang1_2500_final.nc'

   * Adpat the parameters for interpolation :

     * Set l_interpxyz to .false. ==> it enables 2D interpolation with SCRIP and vertical interpolation with splines
     * If your OGCM model is in SIGMA coordinates set your own intermediate Z vertical profile (used for vertical interpolation sigma to sigma) 
       ::

               Z=(0:immersion1:dh1_ref,immersion1:immersion2:dh2_ref,immersion2:immersion3:dh3_ref)
          
       ::
 
               &param_interp
                  l_interpxyz = .false.     !
                  rapdist = 6.0             ! only if l_interpxyz=.true. must be <= 6
                  radius  = 20.0e+3         ! only if l_interpxyz=.true.
                  aspect_ratio = 10         !  only if l_interpxyz=.true.
                  dh1_ref=1.0               ! only if OGCM in Sigma (dz between 0 and immersion1)
                  immersion1 =40.0          ! only if OGCM in Sigma (first immersion below 0 in z profil) 
                  dh2_ref= 2.0              ! only if OGCM in Sigma (dz between immersion1 and immersion2)
                  immersion2 =60.0          ! only if OGCM in Sigma (second immersion in z profil)  
                  dh3_ref=5.0               ! only if OGCM in Sigma (dz between immersion2 and immersion3)
                  immersion3=200.0          ! only if OGCM in Sigma (last immersion in z profil : must be >= MAX(H0) !!!) 
                  nextrap = 10              ! 2D spatial extrapolation iteration
                  l_correct_rho=.false.          ! correct vertical density instabilities  
                  l_complete_prof_first=.false.   ! extrapolate Z profils before doing interpolation
                  l_interp_conserv=.true.  /     ! perform conservative vericale interpolation instead of splines 


     * Launch the executable in batch mode 
       ::

                  qsub batch_extract


Build a new configuration  with CROCO
=====================================

Open a terminal and login to Datarmor 
::
    
   ssh -X login@datarmor



Environment and source code 
----------------------------

#. Setup environment 


   * Source this file to set some environment variables
     ::

         cd $HOME
         cp /home/datawork-croco/datarmor-only/TRAININGS/TRAINING_2021/croco_env.* .
         source croco_env.csh


   * Build a "CROCO" directory on your $DATAWORK  
     ::

         cd $work
         mkdir TRAINING_2021
         cd TRAINING_2021
         mkdir croco
         mkdir CONFIGS


#. Get the source code 
   ::

         cp -r $CROCO_DIR/../../SOURCE_CODES/CROCO/croco_git/croco_master/croco croco/.
         cp -r $CROCO_DIR/../../SOURCE_CODES/CROCO/croco_git/croco_tools croco/.


#. Create a new config 

   * Build a directory for your new configuration and get the following scripts 
     from the source code directory 
     ::
   
         cd $confs
         mkdir my_config 
         cd my_config
         mkdir CROCO_FILES
         cp $croco/OCEAN/param.h .
         cp $croco/OCEAN/cppdefs.h .
         cp $croco/OCEAN/jobcomp .
         cp $CROCO_DIR/job* .

Edit your configuration parameters files
----------------------------------------

#. Setup **param.h**

   * First section : define your domain dimensions (get xi_rho and eta_rho from **croco_grd.nc**) 
     ::

           LLm0 = xi_rho-2 ; MMm0=eta_rho-2 


           #elif defined REGIONAL
           # elif defined SEINE 
                parameter (LLm0=410,  MMm0=180,  N=20)   ! SEINE
 
   * Second section : define your MPI decomposition 
     ::

          Choose your decomposition so number_procs=NP_XI*NP_ETA

          #ifdef MPI
            integer NP_XI, NP_ETA, NNODES
            parameter (NP_XI=14, NP_ETA=6,  NNODES=NP_XI*NP_ETA)

   * If you are using WET_DRY set the critical depth 
     ::

          #ifdef WET_DRY
                real D_wetdry             ! Critical Depth for Drying cells
          # else
                parameter (D_wetdry=0.4)



   * Third section :: Number of harmonic components 
     ::

          #else
            parameter (Ntides=114)

   * Fourth section :: Number of river 
     ::

          #if defined PSOURCE || defined PSOURCE_NCFILE
                integer Msrc               ! Number of point sources
                parameter (Msrc=9)        ! ====== == ===== =======
          #endif

#. Edit **cppdef.h** : Simple config with only tides at boundaries

   * Activate *REGIONAL* case 
     ::

         #define REGIONAL        /* REGIONAL Applications */

   * Set configuration name (same as **param.h**) 
     ::

         /* Configuration Name */
         # define  MEDI5KM
   
   * Set MPI parallelisation 
     ::

         # define  MPI

   * Activate TIDES and define Open boundary conditions according to your domain 
     ::

         /* Open Boundary Conditions */
         # define  TIDES
         # undef  OBC_EAST
         # define  OBC_WEST
         # define  OBC_NORTH
         # undef   OBC_SOUTH

   * In **preselected options** only change these ones :

     * vertical mixing ::

        /* Vertical Mixing */
        # define GLS_MIXING
  
     * Analytical surface fluxes ::

        /* Surface Forcing */
        # undef  BULK_FLUX
        /* Suppression des termes atmospheriques */
        # define ANA_SSFLUX   /* analytical salinity flux */
        # define ANA_STFLUX   /* analytical Latent and Sensible flux */
        # define ANA_SMFLUX   /* surface momentum flux = wind */
        # define ANA_SRFLUX   /* surface short surface radiative */
        # define ANA_SST     /* climatological surface temperature */
        # define ANA_SSS      /* climatological surface salinity */
        # undef ANA_TCLIMA   /* climatological surface for others tracers */
  
     * Lateral Forcing ::

        /* Lateral Forcing */
        # undef CLIMATOLOGY
        # define ANA_INITIAL
        # define ANA_BRY
        # define FRC_BRY
        # ifdef FRC_BRY
        #  define Z_FRC_BRY
        #  define M2_FRC_BRY
        #  undef M3_FRC_BRY
        #  undef T_FRC_BRY
        # endif

     * Bottom Forcing ::

        /* Bottom Forcing */
        # define ANA_BSFLUX
        # define ANA_BTFLUX
      
     * Desactivate source ::

        /* Point Sources - Rivers */
        # undef PSOURCE
        # undef PSOURCE_NCFILE
        # ifdef PSOURCE_NCFILE                    
        #   define PSOURCE_NCFILE_TS
        # endif
        
#. Compile the model in batch mode 
       
             
   * Edit your job comp and set the source code path  ::
       
        SOURCE=$croco/OCEAN
                 
   * Launch the compilation ::

        qsub job_comp_datarmor.pbs
       
     .. note :: you should get a **croco** executable file 

#. Edit the config input file **croco.in** 

        
   * Time stepping ::

       time_stepping: NTIMES   dt[sec]  NDTFAST  NINFO
                      594000      40     10      1

     * **NTIMES** : Number of global time step (dt) 
 
       you can use this script to get NTIMES given your start and end date ::

          /home/datawork-croco/datarmor-only/FORMATION/PREPROC/calc_steps.py
          Duration of your simulation == NTIMES*dt
 
     * **dt** : baroclinic time step (depend on your mesh grid size)

     * **NDTFAST** : number of fast time step in one baroclinic time step 

   * Sigma distribution ::

        S-coord: THETA_S,   THETA_B,    Hc (m)
                  0.0d0      0.0d0      2000.0d0
 
   * Origin date (only) for Netcdf ::
     
        start_date:
         01-01-1900 00:00:00
 
   * Set the path to your inputs files which should be in a **CROCO_FILES** directory ::

        grid:  filename
                         CROCO_FILES/croco_grd.nc
        forcing: filename
                         CROCO_FILES/croco_frc_manga16.nc
        bulk_forcing: filename
                         CROCO_FILES/bidon.nc
        climatology: filename
                         CROCO_FILES/croco_clm.nc
        boundary: filename
                         CROCO_FILES/croco_bry.nc
        initial: NRREC  filename
                  -1
                         CROCO_FILES/croco_ini.nc
        restart:          NRST, NRPFRST / filename
                         9000     -2
                         CROCO_FILES/croco_rst.nc
        history: LDEFHIS, NWRT, NRPFHIS / filename
                   T      90       0
                         CROCO_FILES/croco_his.nc
        averages: NTSAVG, NAVG, NRPFAVG / filename
                    1     2140     0
                          CROCO_FILES/croco_avg.nc

   * Choose which variables you want to save in your output (T/F to activate/desactivate) ::

       primary_history_fields: zeta UBAR VBAR  U  V   wrtT(1:NT)
                          T    F   F   T  T    30*T
       auxiliary_history_fields:   rho Omega  W  Akv  Akt  Aks Visc3d Diff3d  HBL HBBL Bostr Wstr Ustr Vstr Shfl Swfl rsw rlw lat sen  HEL
                                    F   F     F   F    F    F    F     F       F   F    F     F    F    F    F    F   10*F
       gls_history_fields:   TKE  GLS  Lscale
                              F     F    F
       
       primary_averages: zeta UBAR VBAR  U  V   wrtT(1:NT)
                          F    F    F    F  F   30*T
       auxiliary_averages: rho Omega  W  Akv  Akt  Aks  Visc3d Diff3d HBL HBBL Bostr Wstr Ustr Vstr Shfl Swfl rsw rlw lat sen HEL
                            F   F     F   F    F    F     F     F      F   F    F     F   F    F     F    F   10*F
       gls_averages:   TKE  GLS  Lscale
                        F     F    F

   * Set lateral viscosity (ONLY if UV_VIS2 or UV_VIS4 cpp key are  enabled) ::

       lateral_visc:   VISC2,    VISC4    [m^2/sec for all]
                       6.34       0.

   * Set lateral diffusivity (ONLY if UV_DIFF2 or UV_DIFF4 cpp key are  enabled) ::

       tracer_diff2: TNU2(1:NT)           [m^2/sec for all]
                   30*1.d-2

       tracer_diff4: TNU4(1:NT)           [m^4/sec for all]
                   30*0.d11

   * Set bottom drag ::

       bottom_drag:     RDRG [m/s],  RDRG2,  Zob [m],  Cdb_min, Cdb_max
                       0.0d-4      5.d-3    3.5d-3     1.d-4    1.d-1


     * Barotropic mode :: RDRG superseded by RDRG2

     * Baroclinc mode ::  RDRG superseded by RDRG2 superseded by Z0B


#. Edit **batch_mpt** to set the right number of nodes and walltime (1node=28 procs) ::

    #PBS -q mpi_3
    #PBS -l mem=8gb
    #PBS -l walltime=10:00:00
    #PBS -N CROCO_SEINE

#. Launch the model ::

    qsub batch_mpt

#. Visualize 
   
   * First use ncview ::

      module load ncview
      ncview CROCO_FILES/croc_his.nc  

         
Custom you configuration
==========================


Add a source for a river discharge
-----------------------------------

* In **cppdefs.h** you should activate

  * *PSOURCE* : activate tracer for sources

  * PSOURCE_NCFILE : if you want to use a chronological discharge (dont use it for now)

  ::

      /* Point Sources - Rivers */
      # define PSOURCE
      # undef PSOURCE_NCFILE
      # ifdef PSOURCE_NCFILE                    
      #   define PSOURCE_NCFILE_TS
      # endif

* In **param.h** set the number of source points 
  ::

      #if defined PSOURCE || defined PSOURCE_NCFILE
         integer Msrc               ! Number of point sources
         parameter (Msrc=5)        ! ====== == ===== =======
      #endif

* Compile your model

* Edit **croco.in** file 

  First line is for the number of sources then there should be one line by source 
  ::

      psource:   Nsrc  Isrc  Jsrc  Dsrc  Qbar [m3/s]    Lsrc        Tsrc
                  1
                        310  23     0    -900.            T T      0. 0.

  * **Isrc,Jsrc** : Coordinates of point sources
  * **Dsrc** : Direction of outflow (0 along u, 1 along v)
  * **Qbar** : Average discharge in m3/s (positive to the North/East, negative to the South/West)
  * **Lsrc** : Logical for associate tracers to the source (here Temp,Sal)
  * **Tsrc** : Tracer value (here Temp,Sal)


Add a real Atmospheric forcing 
-------------------------------

* In **cppdefs.h** you should activate 
  * *ONLINE* : Use online interpolation (spatial and temporal) from an meteo model on different grid
  * *AROME*  : data are formatted in MeteoFrance framework 
  * *BULK_FLUX* : Compute bulk fluxes

  * *BULK_FAIRALL* : use FAIRALL formulation for bulk
  * *BULK_SMFLUX*  : compute surface momentum flux (from wind stress)
  * *READ_PATM* : Read atmospherical pressure in atm file and use it in the code for bulk and surface pressure gradient
   
  ::

         # define  BULK_FLUX
         # ifdef BULK_FLUX
         #  define BULK_FAIRALL
         #  undef BULK_LW
         #  undef BULK_EP
         #  define BULK_SMFLUX
         #  ifdef BULK_SMFLUX
         #   define BULK_SM_UPDATE
         #  endif
         #  undef  SST_SKIN
         #  undef  ANA_DIURNAL_SW
         #  define  ONLINE
         #  define AROME
         #  define READ_PATM
         #  undef  ERA_ECMWF
         #  undef  RELATIVE_WIND
         # else
         #  undef QCORRECTION
         #  undef SFLX_CORR
         #  undef ANA_DIURNAL_SW
         # endif

  * Dont forget to remove analytical bulk fluxes 

  ::

         /* Suppression des termes atmospheriques */
         # define ANA_SSFLUX   /* surface salinity */
         # define ANA_STFLUX   /* surface temperature */
         # undef ANA_SMFLUX   /* surface momentum flux = wind */
         # undef ANA_SRFLUX   /* surface short surface radiative */
         # define ANA_SST
         # define ANA_SSS

* Recompile the model 

* Go to your configuration directory and make a link to this file

:: 
      cd /home1/datawork/login/CROCO/config
      cd CROCO_FILES
      ln -s /home/datawork-croco/datarmor-only/DATA/METEOROLOGICAL_FORCINGS/ARPEGE-HR_2017_final.nc . 

* Now edit the **croco.in** file  (see bottom of file)

  Set begin year, end year and mont, number of records per day in your dataset and the path of the file

  ::

     online:    byear  bmonth recordsperday byearend bmonthend / data path
                2017   1      24             2017     12
                CROCO_FILES/ARPEGE-HR_2017_final.nc 


Add 3D IC and OBC
------------------


* First you need to get your IC and OBC files see :ref:`OBCIC`

* Edit **cppdefs.h** to activate BRY conditions:
  
  * undefine analytical Init and boundary conditions 
  * activate BRY for Tracers (T_FRC_BRY)

  ::

      # undef ANA_INITIAL
      # undef ANA_BRY
      # define FRC_BRY
      # ifdef FRC_BRY
      #  define Z_FRC_BRY
      #  define M2_FRC_BRY
      #  undef M3_FRC_BRY
      #  undef T_FRC_BRY
      # endif


* Compile the model

* Copy you croco_ic.nc and croco_bry.nc files in the CROCO_FILES diretory

* Edit **croco.in** file

  Set the path to your IC/OBC files 

  ::

     boundary: filename
                 CROCO_FILES/croco_bry.nc
      initial: NRREC  filename
          -1
                 CROCO_FILES/croco_ini.nc

.. note :: When you start from an init file the start date of your simulation is the date of the file 
