WRF preprocessing
=================

WRF pre-processing system is WPS. 

.. warning:: It should be downloaded in the same version than WRF. 

Instructions, and scripts are provided in ``~/croco/croco_tools/Coupling_tools/WRF_WPS``. 
You can follow the instructions given in ``readme_wps``, and use the provided scripts:
``run_wps.bash``, ``job.wps.*``

Note: you will need to have WPS compiled before (see preivous compilation tuto).

Running WPS
~~~~~~~~~~~

WRF pre-preocessing with WPS contains 3 steps: 

* ``geogrid``: defining the horizontal domain and interpolating geographical static data
* ``ungrib``: decoding Grib meteorological data from reananlyses (or so)
* ``metgrid``: interpolating meteorological data on the model grid

To run WPS, you therefore need:

* Geographical data
  Geographical data for WRF are available on WRF users website 
  http://www2.mmm.ucar.edu/wrf/users/download/get_source.html. Geographical 
  data will be available following the link ”here” under WPS download section. 
  You can download the full complete set, but note that topo files are not all 
  in it. Download them individually in addition (e.g. topo_30s).
  Note that Geographical data file is a VERY LARGE file (49 Go uncompressed). 
  Uncompress them (tar xvjf or tar -zxvf).

* Reanalysis data in grib format (from CFSR for example) to build the boundary 
  and initial conditions.
  For example, CFSR data can be downloaded from: 
  https://rda.ucar.edu/datasets/ds093.0/index.html#!description
  A dedicated readme for CFSR data download is provided in 
  ``croco_tools/Coupling_tools/WRF_WPS``.
  You can use ``g1print.exe`` or ``g2print.exe`` (depending on you grib data format) 
  available in ``WPS/ungrib/`` to check the variables in your data files. Usage is
  
  ::
    
      ./g2print.exe YOURDATAFILE

* Vtable to read the grib data: exsiting Vtables can be found in WPS source 
  directory under ``WPS/ungrib/Variable_Tables``, and informations to choose 
  Vtables can be found here: http://www2.mmm.ucar.edu/wrf/users/download/free_data.html

  .. note:: 
      For CFSR, you will need 2 Vtables: one for the fields on pressure levels, 
      one for the fields on surface level. Both Vtables are available in 
      ``croco_tools/Coupling_tools/WRF_WPS`` directory
      ::
    
        Vtable.CFSR_press_pgbh06
        Vtable.CFSR_sfc_flxf06

  ``ungrib`` therefore needs to be run twice (once for each type). This is done 
  in ``run_wps.bash`` (see below). 

A few scripts have been made to help you run WPS. 
You can find them in your ``croco_tools/Coupling_tools/WRF_WPS`` directory:

* ``configure.namelist.wps`` 
* ``run_wps.bash``
* ``job.wps.*``


#. You should find them in ``YOURCONFIG/PREPRO/WRF_WPS``.  
   Edit all the required lines in ``configure.namelist.wps``,  
   and edit all the required paths in ``run_wps.bash``

#. Run WPS directly (or using ``job.wps.pbs`` if you need to submit it in batch)
   ::

       ./run_wps.bash configure.namelist.wps NBPROCS >& run_wps.log


   If WPS is successful, you will obtain in ``~/CONFIGS/BENGUELA_cpl/WRF_FILES/WPS_DATA``
   ::

        geo_em.d01.nc
        geo_em.d02.nc
        met_em.d01.....nc # numerous files where ’...’ are dates 
        met_em.d02.....nc # numerous files where ’...’ are dates

#. Check your metgrid files by looking at some variables with ncview 
   (*e.g.* LANDMASK, PSFC, PSML, SKINTEMP, TT ...) 

   If some variables are missing, it is probably because you did not process 
   ungrib and metgrid for all your input data. 

   If something appears weird, it may be due to a bad interpolation (for example 
   due to a too coarse land-sea mask in the original data). If so, re-run WPS 
   with an updated METGRID.TBL 

Running real.exe
~~~~~~~~~~~~~~~~

After running WPS pre-processing, you need to run ``real.exe`` program 
which actually creates WRF input files for realistic cases from WPS generated files.

.. warning:: 
    You need to use real.exe from uncoupled compilation even for a coupled run


A script has been made to help you run ``real.exe``: ``run_real.bash``. 
You can find it in your ``~/CONFIGS/BENGUELA_cpl/WRF_IN`` directory or 
in the ``croco/SCRIPTS/SCRIPTS_COUPLING/WRF_IN``. It also uses:

* ``configure.namelist.real``
* ``namelist.input.base.complete``

#. Edit all the required lines in ``configure.namelist.real``,
   and edit all the required paths in ``run_real.bash``

#. Eventually edit ``namelist.input.base.complete`` with you choice of 
   parameterization. *DO NOT EDIT* the stuff placed into brackets: ``<...>``, 
   it will be replaced by ``run_real.bash`` with appropriate values. 

   .. warning:: 
      For coupling with waves and currents, only YSU surface and boundary layer 
      schemes are possible at the moment. Be sure to select these. 

#. Run ``run_real.bash`` (eventually using a batch job as ``job.real.pbs``)::

        ./run_real.bash configure.namelist.real NBPROCS >& run_real.log

   If real is successful, you will obtain in ``~/CONFIGS/BENGUELA_cpl/WRF_FILES/YYYY``
   ::

        wrfinput_d01_DATE
        wrfbdy_d01_DATE
        wrflowinp_d01_DATE # if sst_update is set to 1
        wrfdda_d01_DATE # if nudging is activated i
        wrf*_d02_DATE # if you have 2 domains

Additional pre-processing for coupled runs
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

In addition to traditional WRF pre-processing, you will need to: 

* edit options in ``namelist.input``:

  * in ``&physics``: isftcflx = 5 if your are coupling with a wave model
  * in ``&physics``: sst_update = 1 if your are coupling with an ocean model
  * in ``&domains``: num_ext_model_couple_dom = X : number of domains of the 
    other model you are coupling to WRF

    * edit ``CPLMASK`` variable in ``wrfinput_d0X`` for all your coupled domains:

      * CPLMASK=1 where you want to couple
      * CPLMASK=0 when you do no want to couple

  * you may need to create a WRF grid file for OASIS, if you are using the 
    distributed version of WRF (at the date of 2021-Nov). If you are using the 
    github WRF-CROCO version, you don't need to create this grid file, it will 
    be created automatically. If necessary, a script is provided in 
    ``croco/SCRIPTS/SCRIPTS_COUPLING/SCRIPTS_TOOLBOX/OASIS_SCRIPTS``:

    Edit and run ``create_oasis_grids_for_wrf.sh``

Note that the CPLMASK creation may also be performed automatically in the 
coupling tools. 
