Index: /NEMO/branches/2019/dev_r11879_ENHANCE-05_SimonM-Harmonic_Analysis/src/OCE/DIA/diamlr.F90
===================================================================
--- /NEMO/branches/2019/dev_r11879_ENHANCE-05_SimonM-Harmonic_Analysis/src/OCE/DIA/diamlr.F90	(revision 11949)
+++ /NEMO/branches/2019/dev_r11879_ENHANCE-05_SimonM-Harmonic_Analysis/src/OCE/DIA/diamlr.F90	(revision 11950)
@@ -8,4 +8,5 @@
 
    USE par_oce        , ONLY :   wp, jpi, jpj
+   USE phycst         , ONLY :   rpi
    USE in_out_manager , ONLY :   lwp, numout, ln_timing
    USE iom            , ONLY :   iom_put, iom_use, iom_update_file_name
@@ -13,4 +14,5 @@
    USE timing         , ONLY :   timing_start, timing_stop
    USE xios
+   USE tide_mod       , ONLY :   tide_harmo, jpmax_harmo, Wave
 
    IMPLICIT NONE
@@ -71,5 +73,12 @@
       CHARACTER(LEN=1)                            ::   clgt
       CHARACTER(LEN=2)                            ::   clgd
-      INTEGER                                     ::   jm, jn
+      CHARACTER(LEN=25)                           ::   clfloat
+      CHARACTER(LEN=32)                           ::   clrepl
+      INTEGER                                     ::   jl, jm, jn
+      INTEGER                                     ::   itide                       ! Number of available tidal components
+      INTEGER,  ALLOCATABLE, DIMENSION(:)         ::   itide_const                 ! Index list of selected tidal constituents
+      REAL(wp), ALLOCATABLE, DIMENSION(:)         ::   ztide_omega, ztide_u,   &   ! Tidal frequency, phase, nodal correction
+         &                                             ztide_v, ztide_f
+      REAL(wp)                                    ::   ztide_phase                 ! Tidal-constituent phase at adatrj=0
 
       IF(lwp) THEN
@@ -121,8 +130,20 @@
          ! Compile lists of active regressors and of fields selected for
          ! analysis (fields "diamlr_r<nnn>" and "diamlr_f<nnn>", where <nnn> is
-         ! a 3-digit integer)
+         ! a 3-digit integer); also carry out placeholder substitution of tidal
+         ! parameters in regressor expressions
+         !
          ALLOCATE( slxhdl_regs( jpscanmax ), slxhdl_flds( jpscanmax ) )
          ireg = 0
          ifld = 0
+         !
+         ! Retrieve information (frequency, phase, nodal correction) about all
+         ! available tidal constituents for placeholder substitution below
+         itide = jpmax_harmo
+         ALLOCATE(itide_const(itide), ztide_omega(itide), ztide_u(itide), ztide_v(itide), ztide_f(itide))
+         DO jn = 1, itide
+            itide_const(jn) = jn   ! Select all available tidal constituents
+         END DO
+         CALL tide_harmo( ztide_omega, ztide_v, ztide_u, ztide_f, itide_const, itide )
+         
          DO jm = 1, jpscanmax
             WRITE (cl3i, '(i3.3)') jm
@@ -130,29 +151,78 @@
             ! Look for regressor
             IF ( xios_is_valid_field( "diamlr_r"//cl3i ) ) THEN
+
                CALL xios_get_handle( "diamlr_r"//cl3i, slxhdl_regs(ireg+1) )
-               ! Set name attribute (and overwrite possible pre-configured name)
-               ! with field id to enable id string retrieval from stored handle
-               ! below
-               CALL xios_set_attr  ( slxhdl_regs(ireg+1), name="diamlr_r"//cl3i )
-               ! Retrieve pre-configured value of "enabled" attribute
-               CALL xios_get_attr  ( slxhdl_regs(ireg+1), enabled=slxatt_enabled )
-               ! If enabled, keep handle in list of activ regressors
-               IF ( slxatt_enabled ) ireg = ireg + 1
+               ! Retrieve pre-configured value of "enabled" attribute and
+               ! regressor expression
+               CALL xios_get_attr  ( slxhdl_regs(ireg+1), enabled=slxatt_enabled, expr=slxatt_expr )
+               ! If enabled, keep handle in list of active regressors; also
+               ! substitute placeholders for tidal frequencies, phases, and
+               ! nodal corrections in regressor expressions
+               IF ( slxatt_enabled ) THEN
+
+                  ! Substitution of placeholders for tidal-constituent
+                  ! parameters (amplitudes, angular veloccities, nodal phase
+                  ! correction) with values that have been obtained from the
+                  ! tidal-forcing implementation
+                  DO jn = 1, itide
+                     ! Compute phase of tidal constituent (incl. current nodal
+                     ! correction) at the start of the model run (i.e. for
+                     ! adatrj=0)
+                     ztide_phase = MOD( ztide_u(jn) +  ztide_v(jn) - adatrj * 86400.0_wp * ztide_omega(jn), 2.0_wp * rpi )
+                     clrepl = "__TDE_"//TRIM( Wave(jn)%cname_tide )//"_omega__"
+                     DO WHILE ( INDEX( slxatt_expr, TRIM( clrepl ) ) > 0 )
+                        WRITE (clfloat, '(e25.18)') ztide_omega(jn)
+                        jl = INDEX( slxatt_expr, TRIM( clrepl ) )
+                        slxatt_expr = slxatt_expr(1:jl - 1)//clfloat//slxatt_expr(jl + LEN( TRIM( clrepl ) ):LEN( TRIM( slxatt_expr ) ))
+                     END DO
+                     clrepl = "__TDE_"//TRIM( Wave(jn)%cname_tide )//"_phase__"
+                     DO WHILE ( INDEX( slxatt_expr, TRIM( clrepl ) ) > 0 )
+                        WRITE (clfloat, '(e25.18)') ztide_phase
+                        jl = INDEX( slxatt_expr, TRIM( clrepl ) )
+                        slxatt_expr = slxatt_expr(1:jl - 1)//clfloat//slxatt_expr(jl + LEN( TRIM( clrepl ) ):LEN( TRIM( slxatt_expr ) ))
+                     END DO
+                     clrepl = "__TDE_"//TRIM( Wave(jn)%cname_tide )//"_amplitude__"
+                     DO WHILE (INDEX( slxatt_expr, TRIM( clrepl ) ) > 0 )
+                        WRITE (clfloat, '(e25.18)') ztide_f(jn)
+                        jl = INDEX( slxatt_expr, TRIM( clrepl ) )
+                        slxatt_expr = slxatt_expr(1:jl - 1)//clfloat//slxatt_expr(jl + LEN( TRIM( clrepl ) ):LEN( TRIM( slxatt_expr ) ))
+                     END DO
+                  END DO
+
+                  ! Set name attribute (and overwrite possible pre-configured name)
+                  ! with field id to enable id string retrieval from stored handle
+                  ! below; also re-set expression with possible substitutions
+                  CALL xios_set_attr  ( slxhdl_regs(ireg+1), name="diamlr_r"//cl3i, expr=TRIM( slxatt_expr ) )
+
+                  ireg = ireg + 1   ! Accept regressor in list of active regressors
+
+               END IF
             END IF
 
             ! Look for field
             IF ( xios_is_valid_field( "diamlr_f"//cl3i ) ) THEN
+
                CALL xios_get_handle( "diamlr_f"//cl3i, slxhdl_flds(ifld+1) )
-               ! Set name attribute (and overwrite possible pre-configured name)
-               ! with field id to enable id string retrieval from stored handle
-               ! below
-               CALL xios_set_attr  ( slxhdl_flds(ifld+1), name="diamlr_f"//cl3i )
                ! Retrieve pre-configured value of "enabled" attribute
                CALL xios_get_attr  ( slxhdl_flds(ifld+1), enabled=slxatt_enabled )
                ! If enabled, keep handle in list of fields selected for analysis
-               IF ( slxatt_enabled ) ifld = ifld + 1
+               IF ( slxatt_enabled ) THEN
+                  
+                  ! Set name attribute (and overwrite possible pre-configured name)
+                  ! with field id to enable id string retrieval from stored handle
+                  ! below
+                  CALL xios_set_attr  ( slxhdl_flds(ifld+1), name="diamlr_f"//cl3i )
+
+                  ifld = ifld + 1   ! Accept field in list of fields selected for analysis
+
+               END IF
             END IF
 
          END DO
+
+         ! Release tidal data
+         DEALLOCATE( itide_const, ztide_omega, ztide_u, ztide_v, ztide_f )
+
+         ! Output number of active regressors and fields selected for analysis
          IF ( lwp ) WRITE(numout,'(a,i3,a)' ) 'diamlr: ', ireg, ' active regressors found'
          IF ( lwp ) WRITE(numout,'(a,i3,a)' ) 'diamlr: ', ifld, ' fields selected for analysis'
@@ -219,9 +289,9 @@
          END DO
 
+         ! Release list of active regressors and fields selected for analysis
+         DEALLOCATE( slxhdl_regs, slxhdl_flds )
+
       END IF
 
-      ! Release list of active regressors and fields selected for analysis
-      DEALLOCATE( slxhdl_regs, slxhdl_flds )
-      
    END SUBROUTINE dia_mlr_iom_init
 
@@ -234,5 +304,5 @@
       !!----------------------------------------------------------------------
 
-      REAL, DIMENSION(jpi,jpj) ::   zadatrj2d
+      REAL(wp), DIMENSION(jpi,jpj) ::   zadatrj2d
 
       IF( ln_timing )   CALL timing_start('dia_mlr')
