Changes between Initial Version and Version 1 of ImplementAnalysislknetf


Ignore:
Timestamp:
Feb 19, 2023, 8:21:47 AM (13 months ago)
Author:
lnerger
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • ImplementAnalysislknetf

    v1 v1  
     1= Implementation of the Analysis step for the LKNETF (Local Kalman-Nonlinear Ensemble Transform Filter) algorithm =
     2
     3{{{
     4#!html
     5<div class="wiki-toc">
     6<h4>Implementation Guide</h4>
     7<ol><li><a href="ImplementationGuide">Main page</a></li>
     8<li><a href="AdaptParallelization">Adaptation of the parallelization</a></li>
     9<li><a href="InitPdaf">Initialization of PDAF</a></li>
     10<li><a href="ModifyModelforEnsembleIntegration">Modifications for ensemble integration</a></li>
     11<li><a href="ImplementationofAnalysisStep">Implementation of the analysis step</a></li>
     12<ol>
     13<li><a href="ImplementAnalysisestkf">Implementation for ESTKF</a></li>
     14<li><a href="ImplementAnalysislestkf">Implementation for LESTKF</a></li>
     15<li><a href="ImplementAnalysisetkf">Implementation for ETKF</a></li>
     16<li><a href="ImplementAnalysisletkf">Implementation for LETKF</a></li>
     17<li><a href="ImplementAnalysisseik">Implementation for SEIK</a></li>
     18<li><a href="ImplementAnalysislseik">Implementation for LSEIK</a></li>
     19<li><a href="ImplementAnalysisseek">Implementation for SEEK</a></li>
     20<li><a href="ImplementAnalysisenkf">Implementation for EnKF</a></li>
     21<li><a href="ImplementAnalysislenkf">Implementation for LEnKF</a></li>
     22<li><a href="ImplementAnalysisnetf">Implementation for NETF</a></li>
     23<li><a href="ImplementAnalysislnetf">Implementation for LNETF</a></li>
     24<li>Implementation for LKNETF</li>
     25<li><a href="ImplementAnalysispf">Implementation for PF</a></li>
     26<li><a href="ImplementAnalysis_3DVar_classical">Implementation for 3D-Var</a></li>
     27<li><a href="ImplementAnalysis_3DEnVar_classical">Implementation for 3D Ensemble Var</a></li>
     28<li><a href="ImplementAnalysis_Hyb3DVar_classical">Implementation for Hybrid 3D-Var</a></li>
     29</ol>
     30<li><a href="AddingMemoryandTimingInformation">Memory and timing information</a></li>
     31<li><a href="EnsembleGeneration">Ensemble Generation</a></li>
     32<li><a href="DataAssimilationDiagnostics">Diagnostics</a></li>
     33</ol>
     34</div>
     35}}}
     36
     37
     38[[PageOutline(2-3,Contents of this page)]]
     39
     40== Overview ==
     41
     42The LKNETF algorithm was added with PDAF V2.1. If is a hybrid method that combines the nonlinear LNETF and linear LETKF analysis updates. The hybridization allows to choose the strength of both filter methods according to the effective ensemble size and the non-Gaussianity of the observed ensemble.  The details of the LKNETF method are described in the article: Nerger, L. (2022) Data assimilation for nonlinear systems with a hybrid nonlinear-Kalman ensemble transform filter. Q. J. Meteorol. Soc., 148, 620-640 [https://doi.org/10.1002/qj.4221 doi:10.1002/qj.4221].
     43
     44For the analysis step of the LKNETF algorithm, several operations related to the observations are needed. These operations are requested by PDAF by calling user-supplied routines. Intentionally, the operations are split into separate routines in order to keep the operations rather elementary as this procedure should simplify the implementation. The names of the required routines are specified in the call to the routine `PDAF_assimilate_lknetf` in the fully-parallel implementation (or `PDAF_put_state_lknetf` for the 'flexible' implementation) described below. With regard to the parallelization, all these routines (except `U_collect_state`) are executed by the filter processes (`filterpe=.true.`) only.
     45
     46For completeness we discuss here all user-supplied routines that are specified in the interface to `PDAF_assimilate_lknetf`. Since the LKNETF method combined the LNETF and LETKF methods, one can use user routines that were implemented for these filters. Note, that there is no global variant of the LKNETF implemented in PDAF.
     47
     48== `PDAF_put_state_lknetf` ==
     49
     50The general espects of the filter-specific routines `PDAF_assimilate_*` have been described on the page [ModifyModelforEnsembleIntegration Modification of the model code for the ensemble integration].
     51The interface for the routine `PDAF_assimilate_lknetf` contains several routine names for routines that operate on the local analysis domains (marked by `_l` at the end of the routine name). In addition, there are names for routines that consider all available observations required to perform local analyses with LKNETF within some sub-domain of a domain-decomposed model (marked by `_f` at the end of the routine name). In case of a serial execution of the assimilation program, these will be all globally available observations. However, if the program is executed with parallelization, this might be a smaller set of observations.
     52
     53To explain the  difference, it is assumed, for simplicity, that a local analysis domain consists of a single vertical column of the model grid. In addition, we assume that the domain decomposition splits the global model domain by vertical boundaries as is typical for ocean models and that the observations are spatially distributed observations of model fields that are part of the state vector.  Under these assumptions, the situation is the following: When a model uses domain decomposition, the LKNETF algorithm is executed such that for each model sub-domain a loop over all local analysis domains (e.g. vertical columns) that belong to the model sub-domain is performed. As each model sub-domain is treated by a different process, all loops are executed in parallel to each other.
     54
     55For the update of each single vertical column, observations from some larger domain surrounding the vertical column are required. If the influence radius for the observations is sufficiently small there will be vertical columns for which the relevant observations reside completely inside the model sub-domain of the process. However, if a vertical column is considered that is located close to the boundary of the model sub-domain, there will be some observations that don't belong spatially to the local model sub-domain, but to a neighboring model sub-domain. Nonetheless, these observations are required on the local model sub-domain. A simple way to handle this situation is to initialize for each process all globally available observations, together with their coordinates. While this method is simple, it can also become inefficient if the assimilation program is executed using a large number of processes. As for an initial implementation it is usually not the concern to obtain the highest parallel efficiency, the description below assumes that 'full' refers to the global model domain.
     56
     57The interface when using the LKNETF algorithm is the following:
     58{{{
     59  SUBROUTINE PDAF_assimilate_lknetf(U_collect_state, U_distribute_state, &
     60       U_init_dim_obs_f, U_obs_op_f, U_init_obs_f, U_init_obs_l, U_prepoststep, &
     61       U_prodRinvA_l, U_prodRinvA_hyb_l, U_init_n_domains, U_init_dim_l, U_init_dim_obs_l, &
     62       U_g2l_state, U_l2g_state, U_g2l_obs, U_init_obsvar, U_init_obsvar_l, &
     63       U_likelihood_l, U_likelihood_hyb_l, U_next_observation, outflag)
     64  SUBROUTINE PDAF_assimilate_letkf(U_collect_state, U_distribute_state, U_init_dim_obs_f, U_obs_op_f, &
     65                                  U_init_obs_f, U_init_obs_l, U_prepoststep, U_prodRinvA_l, &
     66                                  U_init_n_domains, U_init_dim_l, U_init_dim_obs_l, &
     67                                  U_g2l_state, U_l2g_state, U_g2l_obs, &
     68                                  U_init_obsvar, U_init_obsvar_l, U_next_observation, status)
     69}}}
     70with the following arguments:
     71 * [#U_collect_statecollect_state_pdaf.F90 U_collect_state]: The name of the user-supplied routine that initializes a state vector from the array holding the ensemble of model states from the model fields. This is basically the inverse operation to `U_distribute_state` used in [ModifyModelforEnsembleIntegration#PDAF_get_state PDAF_get_state]
     72 * [#U_distribute_statedistribute_state_pdaf.F90 U_distribute_state]:  The name of a user supplied routine that initializes the model fields from the array holding the ensemble of model state vectors.
     73 * [#U_init_dim_obs_finit_dim_obs_f_pdaf.F90 U_init_dim_obs_f]: The name of the user-supplied routine that provides the size of the full observation vector
     74 * [#U_obs_op_fobs_op_f_pdaf.F90 U_obs_op_f]: The name of the user-supplied routine that acts as the full observation operator on some state vector
     75 * [#U_init_obs_finit_obs_f_pdaf.F90 U_init_obs_f]: The name of the user-supplied routine that initializes the full vector of observations
     76 * [#U_init_obs_linit_obs_l_pdaf.F90 U_init_obs_l]: The name of the user-supplied routine that initializes the vector of observations for a local analysis domain
     77 * [#U_prepoststepprepoststep_ens_pdaf.F90 U_prepoststep]: The name of the pre/poststep routine as in `PDAF_get_state`
     78 * [#U_prodRinvA_lprodrinva_l_pdaf.F90 U_prodRinvA_l]: The name of the user-supplied routine that computes the product of the inverse of the observation error covariance matrix with some matrix provided to the routine by PDAF.
     79 * [#U_init_n_domainsinit_n_domains_pdaf.F90 U_init_n_domains]: The name of the routine that provides the number of local analysis domains
     80 * [#U_init_dim_linit_dim_l_pdaf.F90 U_init_dim_l]: The name of the routine that provides the state dimension for a local analysis domain
     81 * [#U_init_dim_obs_linit_dim_obs_l_pdaf.F90 U_init_dim_obs_l]: The name of the routine that initializes the size of the observation vector for a local analysis domain
     82 * [#U_g2l_stateg2l_state_pdaf.F90 U_g2l_state]: The name of the routine that initializes a local state vector from the global state vector
     83 * [#U_l2g_statel2g_state_pdaf.F90 U_l2g_state]: The name of the routine that initializes the corresponding part of the global state vector from the the provided local state vector
     84 * [#U_g2l_obsg2l_obs_pdaf.F90 U_g2l_obs]: The name of the routine that initializes a local observation vector from a full observation vector
     85 * [#U_init_obsvarinit_obsvar_pdaf.F90 U_init_obsvar]: The name of the user-supplied routine that provides a global mean observation error variance (This routine will only be executed, if an adaptive forgetting factor is used)
     86 * [#U_init_obsvar_linit_obsvar_l_pdaf.F90 U_init_obsvar_l]: The name of the user-supplied routine that provides a mean observation error variance for the local analysis domain (This routine will only be executed, if a local adaptive forgetting factor is used)
     87 * [#U_next_observationnext_observation.F90 U_next_observation]: The name of a user supplied routine that initializes the variables `nsteps`, `timenow`, and `doexit`. The same routine is also used in `PDAF_get_state`.
     88 * `status`: The integer status flag. It is zero, if `PDAF_assimilate_letkf` is exited without errors.
     89
     90Note:
     91 * The order of the routine names does not show the order in which these routines are executed. See the [#Executionorderofuser-suppliedroutines section on the order of the execution] at the bottom of this page.
     92
     93
     94== `PDAF_put_state_letkf` ==
     95
     96When the 'flexible' implementation variant is chosen for the assimilation system, the routine `PDAF_put_state_letkf` has to be used instead of `PDAF_assimilate_letkf`. The general aspects of the filter specific routines `PDAF_put_state_*` have been described on the page [ModifyModelforEnsembleIntegration Modification of the model code for the ensemble integration]. The interface of the routine is identical with that of `PDAF_assimilate_letkf` with the exception the specification of the user-supplied routines `U_distribute_state` and `U_next_observation` are missing.
     97
     98The interface when using the LETKF algorithm is the following:
     99{{{
     100  SUBROUTINE PDAF_put_state_letkf(U_collect_state, U_init_dim_obs_f, U_obs_op_f, U_init_obs_f, &
     101                                  U_init_obs_l, U_prepoststep, U_prodRinvA_l, U_init_n_domains, &
     102                                  U_init_dim_l, U_init_dim_obs_l, &
     103                                  U_g2l_state, U_l2g_state, U_g2l_obs, &
     104                                  U_init_obsvar, U_init_obsvar_l, status)
     105}}}
     106
     107
     108== User-supplied routines ==
     109
     110Here, all user-supplied routines are described that are required in the call to `PDAF_assimilate_letkf`. For some of the generic routines, we link to the page on [ModifyModelforEnsembleIntegration modifying the model code for the ensemble integration].
     111
     112To indicate user-supplied routines we use the prefix `U_`. In the template directory `templates/` as well as in the example implementation in `testsuite/src/dummymodel_1D` these routines exist without the prefix, but with the extension `_pdaf.F90`. In the section titles below we provide the name of the template file in parentheses.
     113
     114In the subroutine interfaces some variables appear with the suffix `_p` (short for 'process'). This suffix indicates that the variable is particular to a model sub-domain, if a domain decomposed model is used. Thus, the value(s) in the variable will be different for different model sub-domains. In addition, there will be variables with the suffix `_f` (for 'full') and with the suffix `_l` (for 'local').
     115
     116=== `U_collect_state` (collect_state_pdaf.F90) ===
     117
     118This routine is independent from the filter algorithm used.
     119See the page on [InsertAnalysisStep#U_collect_statecollect_state_pdaf.F90 inserting the analysis step] for the description of this routine.
     120
     121=== `U_distribute_state` (distribute_state_pdaf.F90) ===
     122
     123This routine is independent of the filter algorithm used.
     124See the page on [InsertAnalysisStep#U_distribute_statedistribute_state_pdaf.F90 inserting the analysis step] for the description of this routine.
     125
     126
     127=== `U_init_dim_obs_f` (init_dim_obs_f_pdaf.F90) ===
     128
     129This routine is used by all filter algorithms with domain-localization (LSEIK, LETKF) and is independent of the particular algorithm.
     130
     131The interface for this routine is:
     132{{{
     133SUBROUTINE init_dim_obs_f(step, dim_obs_f)
     134
     135  INTEGER, INTENT(in)  :: step       ! Current time step
     136  INTEGER, INTENT(out) :: dim_obs_f  ! Dimension of full observation vector
     137}}}
     138
     139The routine is called at the beginning of each analysis step, before the loop over all local analysis domains is entered.  It has to initialize the size `dim_obs_f` of the full observation vector according to the current time step. For simplicity, `dim_obs_f` can be the size for the global model domain.
     140
     141Some hints:
     142 * It can be useful to not only determine the size of the observation vector at this point. One can also already gather information about the location of the observations, which can be used later, e.g. to implement the observation operator. In addition, one can already prepare an array that holds the full observation vector. This can be used later by `U_init_obs_l` to initialize a local vector of observations by selecting the relevant parts of the full observation vector. The required arrays can be defined in a module like `mod_assimilation`.
     143 * The routine is similar to `init_dim_obs` used in the global filters. However, if the global filter is used with a domain-decomposed model, it only initializes the size of the observation vector for the local model sub-domain. This is different for the local filters, as the local analysis also requires observational data from neighboring model sub-domains. Nonetheless, one can base on an implemented routine `init_dim_obs` to implement `init_dim_obs_f`.
     144
     145=== `U_obs_op_f` (obs_op_f_pdaf.F90) ===
     146
     147This routine is used by all filter algorithms with domain-localization (LSEIK, LETKF) and is independent of the particular algorithm.
     148
     149The interface for this routine is:
     150{{{
     151SUBROUTINE obs_op_f(step, dim_p, dim_obs_f, state_p, m_state_f)
     152
     153  INTEGER, INTENT(in) :: step               ! Current time step
     154  INTEGER, INTENT(in) :: dim_p              ! PE-local dimension of state
     155  INTEGER, INTENT(in) :: dim_obs_f          ! Dimension of the full observed state
     156  REAL, INTENT(in)    :: state_p(dim_p)     ! PE-local model state
     157  REAL, INTENT(out) :: m_state_f(dim_obs_f) ! Full observed state
     158}}}
     159
     160The routine is called during the analysis step, before the loop over the local analysis domain is entered. It has to perform the operation of the observation operator acting on a state vector, which is provided as `state_p`. The observed state has to be returned in `m_state_f`. It is the observed state corresponding to the 'full' observation vector.
     161
     162Hint:
     163 * The routine is similar to `init_dim_obs` used for the global filters. However, with a domain-decomposed model `m_state_f` will contain parts of the state vector from neighboring model sub-domains. To make these parts accessible, some parallel communication will be necessary (The state information for a neighboring model sub-domain, will be in the memory of the process that handles that sub-domain). The example implementation in `testsuite/dummymodel_1d` uses the function `MPI_AllGatherV` for this communication.
     164
     165=== `U_init_obs_f` (init_obs_f_pdaf.F90) ===
     166
     167This routine is used by all filter algorithms with domain-localization (LSEIK, LETKF) and is independent of the particular algorithm.
     168The routine is only called if the globally adaptive forgetting factor is used (`type_forget=1` in the example implementation). For the local filters there is also the alternative to use locally adaptive forgetting factors (`type_forget=2` in the example implementation)
     169
     170The interface for this routine is:
     171{{{
     172SUBROUTINE init_obs_f(step, dim_obs_f, observation_f)
     173
     174  INTEGER, INTENT(in) :: step                     ! Current time step
     175  INTEGER, INTENT(in) :: dim_obs_f                ! Dimension of full observation vector
     176  REAL, INTENT(out)   :: observation_f(dim_obs_f) ! Full observation vector
     177}}}
     178
     179The routine is called during the analysis step before the loop over the local analysis domains is entered. It has to provide the full vector of observations in `observation_f` for the current time step. The caller is the routine that computes an adaptive forgetting factor (PDAF_set_forget).
     180
     181Hints:
     182 * As for the other 'full' routines: While the global counterpart of this routine (`init_obs`) has to initialize the observation vector only for the local model sub-domain, the 'full' routine has to include observations that spatially belong to neighboring model sub-domains. As an easy choice one can simply initialize a vector of all globally available observations.
     183 * If the adaptive forgetting factor is not used, this routine only has to exist. However, no functionality is required.
     184
     185
     186=== `U_init_obs_l` (init_obs_l_pdaf.F90) ===
     187
     188This routine is used by all filter algorithms with domain-localization (LSEIK, LETKF) and is independent of the particular algorithm.
     189
     190The interface for this routine is:
     191{{{
     192SUBROUTINE init_obs_l(domain_p, step, dim_obs_l, observation_l)
     193
     194  INTEGER, INTENT(in) :: domain_p                 ! Current local analysis domain
     195  INTEGER, INTENT(in) :: step                     ! Current time step
     196  INTEGER, INTENT(in) :: dim_obs_l                ! Local dimension of observation vector
     197  REAL, INTENT(out)   :: observation_l(dim_obs_l) ! Local observation vector
     198}}}
     199
     200The routine is called during the analysis step during the loop over the local analysis domain.
     201It has to provide the vector of observations for the analysis in the local analysis domain with index `domain_p` in `observation_l` for the current time step.
     202
     203Hints:
     204 * For parallel efficiency, the LETKF algorithm is implemented in a way that first the full vectors are initialized. These are then restricted to the local analysis domain during the loop over all local analysis domains. Thus, if the full vector of observations has been initialized before `U_init_obs_l` is executed (e.g. by `U_init_dim_obs_f`), the operations performed in this routine will be to select the part of the full observation vector that is relevant for the current local analysis domain.
     205 * The routine `U_init_dim_obs_l` is executed before this routine. Thus, if that routine already prepares the information which elements of `observation_f` are needed for `observation_l`, this information can be used efficiently here.
     206
     207
     208=== `U_prepoststep` (prepoststep_ens_pdaf.F90) ===
     209
     210This routine can generally be identical to that used for the global SEIK filter, which has already been described on the [ModifyModelforEnsembleIntegration#U_prepoststepprepoststep_ens_pdaf.F90 page on modifying the model code for the ensemble integration]. For completeness, the description is repeated:
     211
     212The interface of the routine is identical for all filters. However, the particular operations that are performed in the routine can be specific for each filter algorithm. Here, we exemplify the interface on the example of the ETKF.
     213
     214The interface for this routine is
     215{{{
     216SUBROUTINE prepoststep(step, dim_p, dim_ens, dim_ens_p, dim_obs_p, &
     217                       state_p, Uinv, ens_p, flag)
     218
     219  INTEGER, INTENT(in) :: step        ! Current time step
     220                         ! (When the routine is called before the analysis -step is provided.)
     221  INTEGER, INTENT(in) :: dim_p       ! PE-local state dimension
     222  INTEGER, INTENT(in) :: dim_ens     ! Size of state ensemble
     223  INTEGER, INTENT(in) :: dim_ens_p   ! PE-local size of ensemble
     224  INTEGER, INTENT(in) :: dim_obs_p   ! PE-local dimension of observation vector
     225  REAL, INTENT(inout) :: state_p(dim_p) ! PE-local forecast/analysis state
     226                                     ! The array 'state_p' is not generally not initialized in the case of SEIK/EnKF/ETKF.
     227                                     ! It can be used freely in this routine.
     228  REAL, INTENT(inout) :: Uinv(dim_ens, dim_ens)  ! Inverse of matrix U
     229  REAL, INTENT(inout) :: ens_p(dim_p, dim_ens)   ! PE-local state ensemble
     230  INTEGER, INTENT(in) :: flag        ! PDAF status flag
     231}}}
     232
     233The routine `U_prepoststep` is called once at the beginning of the assimilation process. In addition, it is called during the assimilation cycles before the analysis step and after the ensemble transformation. The routine is called by all filter processes (that is `filterpe=1`).
     234
     235The routine provides for the user the full access to the ensemble of model states. Thus, user-controlled pre- and post-step operations can be performed.  For example the forecast and the analysis states and ensemble covariance matrix can be analyzed, e.g. by computing the estimated variances. In addition, the estimates can be written to disk.
     236
     237Hint:
     238 * If a user considers to perform adjustments to the estimates (e.g. for balances), this routine is the right place for it.
     239 * Only for the SEEK filter the state vector (`state_p`) is initialized. For all other filters, the array is allocated, but it can be used freely during the execution of `U_prepoststep`.
     240 * The interface has a difference for ETKF and SEIK: For the ETKF, the array `Uinv` has size `dim_ens` x `dim_ens`. In contrast it has size `dim_ens-1` x `dim_ens-1` for the SEIK filter.
     241 * The interface through which `U_prepoststep` is called does not include the array of smoothed ensembles. In order to access the smoother ensemble array one has to set a pointer to it using a call to the routine `PDAF_get_smootherens` (see page on [AuxiliaryRoutines auxiliary routines])
     242
     243
     244
     245=== `U_prodRinvA_l` (prodrinva_l_pdaf.F90) ===
     246
     247This routine is used by the local filters (LSEIK and LETKF). There is a slight difference between LSEIK and LETKF for this routine, which is described below.
     248
     249The interface for this routine is:
     250{{{
     251SUBROUTINE prodRinvA_l(domain_p, step, dim_obs_l, dim_ens, obs_l, A_l, C_l)
     252
     253  INTEGER, INTENT(in) :: domain_p             ! Current local analysis domain
     254  INTEGER, INTENT(in) :: step                 ! Current time step
     255  INTEGER, INTENT(in) :: dim_obs_l            ! Dimension of local observation vector
     256  INTEGER, INTENT(in) :: dim_ens              ! Ensemble size
     257  REAL, INTENT(in)    :: obs_l(dim_obs_l)     ! Local vector of observations
     258  REAL, INTENT(inout) :: A_l(dim_obs_l, dim_ens) ! Input matrix from analysis routine
     259  REAL, INTENT(out)   :: C_l(dim_obs_l, dim_ens) ! Output matrix
     260}}}
     261
     262The routine is called during the loop over the local analysis domains. In the algorithm, the product of the inverse of the observation error covariance matrix with some matrix has to be computed. For the SEIK filter this matrix holds the observed part of the ensemble perturbations for the local analysis domain of index `domain_p`. The matrix is provided as `A_l`. The product has to be given as `C_l`.
     263
     264This routine is also the place to perform observation localization. To initialize a vector of weights, the routine `PDAF_local_weight` can be called. The procedure is used in the example implementation and also demonstrated in the template routine.
     265
     266Hints:
     267 * The routine is a local variant of the routine `U_prodRinvA`. Thus if that routine has been implemented before, it can be adapted here for the local filter.
     268 * The routine does not require that the product is implemented as a real matrix-matrix product. Rather, the product can be implemented in its most efficient form. For example, if the observation error covariance matrix is diagonal, only the multiplication of the diagonal with matrix `A_l` has to be implemented.
     269 * The observation vector `obs_l` is provided through the interface for cases where the observation error variance is relative to the actual value of the observations.
     270 * The interface has a difference for SEIK and ETKF: For ETKF the third argument is the ensemble size (`dim_ens`), while for SEIK it is the rank (`rank`) of the covariance matrix (usually ensemble size minus one). In addition, the second dimension of `A_l` and `C_l` has size `dim_ens` for ETKF, while it is `rank` for the SEIK filter. (Practically, one can usually ignore this difference as the fourth argument of the interface can be named arbitrarily in the routine.)
     271
     272
     273=== `U_init_n_domains` (init_n_domains_pdaf.F90) ===
     274
     275This routine is used by all filter algorithms with domain-localization (LSEIK, LETKF) and is independent of the particular algorithm.
     276
     277The interface for this routine is:
     278{{{
     279SUBROUTINE init_n_domains(step, n_domains_p)
     280
     281  INTEGER, INTENT(in)  :: step        ! Current time step
     282  INTEGER, INTENT(out) :: n_domains_p ! Number of analysis domains for local model sub-domain
     283}}}
     284
     285The routine is called during the analysis step before the loop over the local analysis domains is entered.
     286It has to provide the number of local analysis domains. In case of a domain-decomposed model the number of local analysis domain for the model sub-domain of the calling process has to be initialized.
     287
     288Hints:
     289 * As a simple case, if the localization is only performed horizontally, the local analysis domains can be single vertical columns of the model grid. In this case, `n_domains_p` is simply the number of vertical columns in the local model sub-domain.
     290
     291
     292=== `U_init_dim_l` (init_dim_l_pdaf.F90) ===
     293
     294This routine is used by all filter algorithms with domain-localization (LSEIK, LETKF) and is independent of the particular algorithm.
     295
     296The interface for this routine is:
     297{{{
     298SUBROUTINE init_dim_l(step, domain_p, dim_l)
     299
     300  INTEGER, INTENT(in)  :: step        ! Current time step
     301  INTEGER, INTENT(in)  :: domain_p    ! Current local analysis domain
     302  INTEGER, INTENT(out) :: dim_l       ! Local state dimension
     303}}}
     304
     305The routine is called during the loop over the local analysis domains in the analysis step.
     306It has to provide in `dim_l` the dimension of the state vector for the local analysis domain with index `domain_p`.
     307
     308Hints:
     309 * If a local analysis domain is a single vertical column of the model grid, the size of the state in the local analysis domain will be just the number of vertical grid points at this location.
     310
     311
     312=== `U_init_dim_obs_l` (init_dim_obs_l_pdaf.F90) ===
     313
     314This routine is used by all filter algorithms with domain-localization (LSEIK, LETKF) and is independent of the particular algorithm.
     315
     316The interface for this routine is:
     317{{{
     318SUBROUTINE init_dim_obs_l(domain_p, step, dim_obs_f, dim_obs_l)
     319
     320  INTEGER, INTENT(in)  :: domain_p   ! Current local analysis domain
     321  INTEGER, INTENT(in)  :: step       ! Current time step
     322  INTEGER, INTENT(in)  :: dim_obs_f  ! Full dimension of observation vector
     323  INTEGER, INTENT(out) :: dim_obs_l  ! Local dimension of observation vector
     324}}}
     325
     326The routine is called during the loop over the local analysis domains in the analysis step.
     327It has to initialize in `dim_obs_l` the size of the observation vector used for the local analysis domain with index `domain_p`.
     328
     329Some hints:
     330 * Usually, the observations to be considered for a local analysis are those which reside within some distance from the local analysis domain. Thus, if the local analysis domain is a single vertical column of the model grid and if the model grid is a regular ij-grid, then one could use some range of i/j indices to select the observations and determine the local number of them. More generally, one can compute the physical distance of an observation from the local analysis domain and decide on this basis, if the observation has to be considered.
     331 * In the loop over the local analysis domains, the routine is always called before `U_init_obs_l` is executed. Thus, as `U_init_dim_obs_local` has to check which observations should be used for the local analysis domain, one can already initialize an integer array that stores the index of observations to be considered. This index should be the position of the observation in the array `observation_f`. With this, the initialization of the local observation vector in `U_init_obs_l` can be sped up.
     332 * For PDAF, we could not join the routines `U_init_dim_obs_l` and `U_init_obs_l`, because the array for the local observations is allocated internally to PDAF after its size has been determined in `U_init_dim_obs_l`.
     333
     334
     335=== `U_g2l_state` (g2l_state_pdaf.F90) ===
     336
     337This routine is used by all filter algorithms with domain-localization (LSEIK, LETKF) and is independent of the particular algorithm.
     338
     339The interface for this routine is:
     340{{{
     341SUBROUTINE global2local_state(step, domain_p, dim_p, state_p, dim_l, state_l)
     342
     343  INTEGER, INTENT(in) :: step           ! Current time step
     344  INTEGER, INTENT(in) :: domain_p       ! Current local analysis domain
     345  INTEGER, INTENT(in) :: dim_p          ! State dimension for model sub-domain
     346  INTEGER, INTENT(in) :: dim_l          ! Local state dimension
     347  REAL, INTENT(in)    :: state_p(dim_p) ! State vector for model sub-domain
     348  REAL, INTENT(out)   :: state_l(dim_l) ! State vector on local analysis domain
     349}}}
     350
     351The routine is called during the loop over the local analysis domains in the analysis step. It has to provide the local state vector `state_l` that corresponds to the local analysis domain with index `domain_p`. Provided to the routine is the state vector `state_p`. With a domain decomposed model, this is the state for the local model sub-domain.
     352
     353Hints:
     354 * In the simple case that a local analysis domain is a single vertical column of the model grid, the operation in this routine would be to take out of `state_p` the data for the vertical column indexed by `domain_p`.
     355
     356
     357=== `U_l2g_state` (l2g_state_pdaf.F90) ===
     358
     359This routine is used by all filter algorithms with domain-localization (LSEIK, LETKF) and is independent of the particular algorithm.
     360
     361The interface for this routine is:
     362{{{
     363SUBROUTINE l2g_state(step, domain_p, dim_l, state_l, dim_p, state_p)
     364
     365  INTEGER, INTENT(in) :: step           ! Current time step
     366  INTEGER, INTENT(in) :: domain_p       ! Current local analysis domain
     367  INTEGER, INTENT(in) :: dim_p          ! State dimension for model sub-domain
     368  INTEGER, INTENT(in) :: dim_l          ! Local state dimension
     369  REAL, INTENT(in)    :: state_p(dim_p) ! State vector for model sub-domain
     370  REAL, INTENT(out)   :: state_l(dim_l) ! State vector on local analysis domain
     371}}}
     372
     373The routine is called during the loop over the local analysis domains in the analysis step. It has to initialize the part of the global state vector `state_p` that corresponds to the local analysis domain with index `domain_p`. Provided to the routine is the state vector `state_l` for the local analysis domain.
     374
     375Hints:
     376 * In the simple case that a local analysis domain is a single vertical column of the model grid, the operation in this routine would be to write into `state_p` the data for the vertical column indexed by `domain_p`.
     377
     378
     379=== `U_g2l_obs` (g2l_obs_pdaf.F90) ===
     380
     381This routine is used by all filter algorithms with domain-localization (LSEIK, LETKF) and is independent of the particular algorithm.
     382
     383The interface for this routine is:
     384{{{
     385SUBROUTINE g2l_obs(domain_p, step, dim_obs_f, dim_obs_l, mstate_f, mstate_l)
     386
     387  INTEGER, INTENT(in) :: domain_p              ! Current local analysis domain
     388  INTEGER, INTENT(in) :: step                  ! Current time step
     389  INTEGER, INTENT(in) :: dim_obs_f             ! Dimension of full observation vector for model sub-domain
     390  INTEGER, INTENT(in) :: dim_obs_l             ! Dimension of observation vector for local analysis domain
     391  REAL, INTENT(in)    :: mstate_f(dim_obs_f)   ! Full observation vector for model sub-domain
     392  REAL, INTENT(out)   :: mstate_l(dim_obs_l)   ! Observation vector for local analysis domain
     393}}}
     394
     395The routine is called during the loop over the local analysis domains in the analysis step. It has to provide a local observation vector `mstate_l` for the observation domain that corresponds to the local analysis domain with index `domain_p`. Provided to the routine is the full observation vector `mstate_f` from which the local part has to be extracted.
     396
     397Hints:
     398 * The  vector `mstate_f` that is provided to the routine is one of the observed state vectors that are produced by `U_obs_op_full`.
     399 * Some operations performed here are analogous to those required to initialize a local vector of observations in `U_init_obs_l`. If that routine reads first a full vector of observations (e.g. in `U_init_dim_obs_f`), this vector has to be restricted to the relevant observations for the current local analysis domain. For this operation, one can for example initialize an index array when `U_init_dim_obs_l` is executed. (Which happens before `U_global2local_obs`)
     400
     401
     402=== `U_init_obsvar` (init_obsvar_pdaf.F90) ===
     403
     404This routine is used by the global filter algorithms SEIK and  ETKF as well as the local filters LSEIK and LETKF. The routine is only called if the adaptive forgetting factor is used (`type_forget=1` in the example implementation). The difference in this routine between global and local filters is that the global filters use 'global' while the local filters use 'full' quantities.
     405
     406The interface for this routine is:
     407{{{
     408SUBROUTINE init_obsvar(step, dim_obs_f, obs_f, meanvar_f)
     409
     410  INTEGER, INTENT(in) :: step             ! Current time step
     411  INTEGER, INTENT(in) :: dim_obs_f        ! Full dimension of observation vector
     412  REAL, INTENT(in)    :: obs_f(dim_obs_f) ! Full observation vector
     413  REAL, INTENT(out)   :: meanvar_f        ! Mean observation error variance
     414}}}
     415
     416The routine is called in the local filters before the loop over all local analysis domains is entered. The call is by the routine that computes an adaptive forgetting factor (`PDAF_set_forget`).
     417The routine has to initialize an average full observation error variance, which should be consistent with the observation vector initialized in `U_init_ob_f`.
     418
     419
     420Hints:
     421 * For a model with domain-decomposition one might use the mean variance for the model sub-domain of the calling process. Alternatively one can compute a mean variance for the full model domain using MPI communication (e.g. the function `MPI_allreduce`).
     422 * The observation vector `obs_p` is provided to the routine for the case that the observation error variance is relative to the value of the observations.
     423 * If the adaptive forgetting factor is not used, this routine has only to exist for the compilation, but it does not need functionality.
     424
     425
     426=== `U_init_obsvar_l` (init_obsvar_l_pdaf.F90) ===
     427
     428This routine is used by all filter algorithms with domain-localization (LSEIK, LETKF) and is independent of the particular algorithm. The routine is only called if the local adaptive forgetting factor is used (`type_forget=2` in the example implementation).
     429
     430The interface for this routine is:
     431{{{
     432SUBROUTINE init_obsvar_l(domain_p, step, dim_obs_l, obs_l, meanvar_l)
     433
     434  INTEGER, INTENT(in) :: domain_p         ! Current local analysis domain
     435  INTEGER, INTENT(in) :: step             ! Current time step
     436  INTEGER, INTENT(in) :: dim_obs_l        ! Local dimension of observation vector
     437  REAL, INTENT(in)    :: obs_l(dim_obs_l) ! Local observation vector
     438  REAL, INTENT(out)   :: meanvar_l        ! Mean local observation error variance
     439}}}
     440
     441The routine is called in the local filters during the loop over all local analysis domains by the routine that computes a local adaptive forgetting factor (`PDAF_set_forget_local`). The routine has to initialize a local mean observation error variance for all observations used for the analysis in the current local analysis domain.
     442
     443Hints:
     444 * If the local adaptive forgetting factor is not used, this routine has only to exist for the compilation, but it does not need functionality.
     445
     446
     447=== `U_next_observation` (next_observation_pdaf.F90) ===
     448
     449This routine is independent of the filter algorithm used.
     450See the page on [InsertAnalysisStep#U_next_observationnext_observation_pdaf.F90 inserting the analysis step] for the description of this routine.
     451
     452== Execution order of user-supplied routines ==
     453
     454The user-supplied routines are executed in the order listed below. The order can be important as some routines can perform preparatory work for routines executed later on during the analysis. For example, `U_init_dim_obs_l` can prepare an index array that provides the information how to localize a 'full' vector of observations. Some hints one the efficient implementation strategy are given with the descriptions of the routine interfaces above.
     455
     456Before the analysis step is called the following is executed:
     457 1. [#U_collect_statecollect_state_pdaf.F90 U_collect_state] (called once for each ensemble member)
     458
     459When the ensemble integration of the forecast is completed, the analysis step is executed. Before the loop over all local analysis domains, the following routines are executed:
     460 1. [#U_prepoststepprepoststep_ens_pdaf.F90 U_prepoststep] (Call to act on the forecast ensemble, called with negative value of the time step)
     461 1. [#U_init_n_domainsinit_n_domains_pdaf.F90 U_init_n_domains]
     462 1. [#U_init_dim_obs_finit_dim_obs_f_pdaf.F90 U_init_dim_obs_f]
     463 1. [#U_obs_op_fobs_op_f_pdaf.F90 U_obs_op_f] (Called `dim_ens` times; once for each ensemble member)
     464 1. [#U_init_obs_finit_obs_f_pdaf.F90 U_init_obs_f] (Only executed, if the global adaptive forgetting factor is used (`type_forget=1` in the example implementation))
     465 1. [#U_init_obsvarinit_obsvar_pdaf.F90 U_init_obsvar] (Only executed, if the global adaptive forgetting factor is used (`type_forget=1` in the example implementation))
     466
     467In the loop over all local analysis domains, it is executed for each local analysis domain:
     468 1. [#U_init_dim_linit_dim_l_pdaf.F90 U_init_dim_l]
     469 1. [#U_init_dim_obs_linit_dim_obs_l_pdaf.F90 U_init_dim_obs_l]
     470 1. [#U_g2l_stateg2l_state_pdaf.F90 U_g2l_state] (Called `dim_ens+1` times: Once for each ensemble member and once for the mean state estimate)
     471 1. [#U_g2l_obsg2l_obs_pdaf.F90 U_g2l_obs] (A single call to localize the mean observed state)
     472 1. [#U_init_obs_linit_obs_l_pdaf.F90 U_init_obs_l]
     473 1. [#U_g2l_obsg2l_obs_pdaf.F90 U_g2l_obs] (`dim_ens` calls: one call to localize the observed part of each ensemble member)
     474 1. [#U_init_obsvar_linit_obsvar_l_pdaf.F90 U_init_obsvar_l] (Only called, if the local adaptive forgetting factor is used (`type_forget=2` in the example implementation))
     475 1. [#U_prodRinvA_lprodrinva_l_pdaf.F90 U_prodRinvA_l]
     476 1. [#U_l2g_statel2g_state_pdaf.F90 U_l2g_state] (Called `dim_ens+1` times: Once for each ensemble member and once for the mean state estimate)
     477
     478After the loop over all local analysis domains, it is executed:
     479 1. [#U_prepoststepprepoststep_ens_pdaf.F90 U_prepoststep] (Call to act on the analysis ensemble, called with (positive) value of the time step)
     480
     481In case of the routine `PDAF_assimilate_letkf`, the following routines are executed after the analysis step:
     482 1. [#U_distribute_statedistribute_state_pdaf.F90 U_distribute_state]
     483 1. [#U_next_observationnext_observation_pdaf.F90 U_next_observation]
     484
     485