Index: NEMO/branches/2019/dev_r11879_ENHANCE-05_SimonM-Harmonic_Analysis/cfgs/SHARED/field_def_nemo-oce.xml
===================================================================
--- NEMO/branches/2019/dev_r11879_ENHANCE-05_SimonM-Harmonic_Analysis/cfgs/SHARED/field_def_nemo-oce.xml	(revision 12021)
+++ NEMO/branches/2019/dev_r11879_ENHANCE-05_SimonM-Harmonic_Analysis/cfgs/SHARED/field_def_nemo-oce.xml	(revision 12022)
@@ -80,4 +80,24 @@
 ============================================================================================================
     -->
+
+    <field_group id="diadetide_fields">
+
+    <!--
+       =====================================================================================================
+                 Weight fields for the computation of daily detided model diagnostics (diadetide)
+       =====================================================================================================
+
+       -->
+
+      <field id="diadetide_weight" grid_ref="diadetide_grid_T_2D" enabled=".TRUE." />
+      <field id="diadetide_weight_grid_T_2D" field_ref="diadetide_weight" grid_ref="diadetide_grid_T_2D" enabled=".TRUE." > this </field>
+      <field id="diadetide_weight_grid_U_2D" field_ref="diadetide_weight" grid_ref="diadetide_grid_U_2D" enabled=".TRUE." > this </field>
+      <field id="diadetide_weight_grid_V_2D" field_ref="diadetide_weight" grid_ref="diadetide_grid_V_2D" enabled=".TRUE." > this </field>
+      <field id="diadetide_weight_grid_T_3D" field_ref="diadetide_weight" grid_ref="diadetide_grid_2D_to_grid_T_3D" enabled=".TRUE." > this </field>
+      <field id="diadetide_weight_grid_U_3D" field_ref="diadetide_weight" grid_ref="diadetide_grid_2D_to_grid_U_3D" enabled=".TRUE." > this </field>
+      <field id="diadetide_weight_grid_V_3D" field_ref="diadetide_weight" grid_ref="diadetide_grid_2D_to_grid_V_3D" enabled=".TRUE." > this </field>
+      <field id="diadetide_weight_grid_W_3D" field_ref="diadetide_weight" grid_ref="diadetide_grid_2D_to_grid_W_3D" enabled=".TRUE." > this </field>
+
+    </field_group>
 
     <!-- 
Index: NEMO/branches/2019/dev_r11879_ENHANCE-05_SimonM-Harmonic_Analysis/cfgs/SHARED/grid_def_nemo.xml
===================================================================
--- NEMO/branches/2019/dev_r11879_ENHANCE-05_SimonM-Harmonic_Analysis/cfgs/SHARED/grid_def_nemo.xml	(revision 12021)
+++ NEMO/branches/2019/dev_r11879_ENHANCE-05_SimonM-Harmonic_Analysis/cfgs/SHARED/grid_def_nemo.xml	(revision 12022)
@@ -109,4 +109,41 @@
 	<scalar />
       </grid>
+      <!-- grid definitions for the computation of daily detided model diagnostics (diadetide) -->
+      <grid id="diadetide_grid_T_2D" >
+	<domain domain_ref="grid_T" />
+	<scalar />
+      </grid>
+      <grid id="diadetide_grid_U_2D" >
+	<domain domain_ref="grid_U" />
+	<scalar />
+      </grid>
+      <grid id="diadetide_grid_V_2D" >
+	<domain domain_ref="grid_V" />
+	<scalar />
+      </grid>
+      <grid id="diadetide_grid_2D_to_grid_T_3D" >
+	<domain domain_ref="grid_T" />
+        <axis axis_ref="deptht">
+          <duplicate_scalar />
+        </axis>
+      </grid>
+      <grid id="diadetide_grid_2D_to_grid_U_3D" >
+	<domain domain_ref="grid_U" />
+        <axis axis_ref="depthu">
+          <duplicate_scalar />
+        </axis>
+      </grid>
+      <grid id="diadetide_grid_2D_to_grid_V_3D" >
+	<domain domain_ref="grid_V" />
+        <axis axis_ref="depthv">
+          <duplicate_scalar />
+        </axis>
+      </grid>
+      <grid id="diadetide_grid_2D_to_grid_W_3D" >
+	<domain domain_ref="grid_W" />
+        <axis axis_ref="depthw">
+          <duplicate_scalar />
+        </axis>
+      </grid>
 
     </grid_definition>
Index: NEMO/branches/2019/dev_r11879_ENHANCE-05_SimonM-Harmonic_Analysis/src/OCE/DIA/diadetide.F90
===================================================================
--- NEMO/branches/2019/dev_r11879_ENHANCE-05_SimonM-Harmonic_Analysis/src/OCE/DIA/diadetide.F90	(revision 12022)
+++ NEMO/branches/2019/dev_r11879_ENHANCE-05_SimonM-Harmonic_Analysis/src/OCE/DIA/diadetide.F90	(revision 12022)
@@ -0,0 +1,114 @@
+MODULE diadetide
+   !!======================================================================
+   !!                      ***  MODULE diadetide  ***
+   !! Computation of weights for daily detided model diagnostics
+   !!======================================================================
+   !! History :       !  2019  (S. Mueller)
+   !!----------------------------------------------------------------------
+   USE par_oce        , ONLY :   wp, jpi, jpj
+   USE in_out_manager , ONLY :   lwp, numout
+   USE iom            , ONLY :   iom_put
+   USE dom_oce        , ONLY :   rdt, nsec_day
+   USE phycst         , ONLY :   rpi
+   USE tide_mod       , ONLY :   tide_harmo, jpmax_harmo, Wave
+   USE xios
+
+   IMPLICIT NONE
+   PRIVATE
+
+   LOGICAL, PUBLIC                               ::   lk_diadetide
+   INTEGER                                       ::   ndiadetide
+   REAL(wp), SAVE, ALLOCATABLE, DIMENSION(:)     ::   tdiadetide
+
+   PUBLIC ::   dia_detide_init, dia_detide
+
+   !!----------------------------------------------------------------------
+   !! NEMO/OCE 4.0 , NEMO Consortium (2019)
+   !! $Id$
+   !! Software governed by the CeCILL license (see ./LICENSE)
+   !!----------------------------------------------------------------------
+CONTAINS
+
+   SUBROUTINE dia_detide_init
+      !!----------------------------------------------------------------------
+      !!               ***  ROUTINE dia_detide_init  ***
+      !!
+      !! ** Purpose : initialisation of the weight computation for daily
+      !!              detided diagnostics (currently M2-detiding only)
+      !!
+      !!----------------------------------------------------------------------
+
+      LOGICAL                             ::   llxatt_enabled
+      INTEGER, ALLOCATABLE, DIMENSION(:)  ::   itide                                    ! Tidal-constituent index
+      REAL(wp), ALLOCATABLE, DIMENSION(:) ::   ztide_omega, ztide_u, ztide_v, ztide_f   ! Tidal-constituent parameters
+      REAL(wp)                            ::   zdt
+      INTEGER                             ::   jn
+
+      ! Enquire detiding activation state (test for presence of detiding-related
+      ! weights field and output file group)
+      IF ( xios_is_valid_field( "diadetide_weight" ).AND.xios_is_valid_filegroup( "diadetide_files" ) ) THEN
+         llxatt_enabled = .TRUE.
+      ELSE
+         llxatt_enabled = .FALSE.
+      END IF
+      lk_diadetide = llxatt_enabled
+
+      IF (lwp) THEN
+         WRITE (numout, *)
+         WRITE (numout, *) 'dia_detide_init : weight computation for daily detided model diagnostics'
+         WRITE (numout, *) '~~~~~~~~~~~~~~~'
+         WRITE (numout, *) '                  lk_diadetide = ', lk_diadetide
+      END IF
+
+      IF (lk_diadetide) THEN
+         ! Retrieve information about M2 tidal constituent
+         ALLOCATE( ztide_omega(1), ztide_v(1), ztide_u(1), ztide_f(1), itide(1) )
+         DO jn = 1, jpmax_harmo
+            IF (TRIM( Wave(jn)%cname_tide ) == 'M2') itide(1) = jn
+         END DO
+         CALL tide_harmo( ztide_omega, ztide_v, ztide_u, ztide_f, itide, 1 )
+         ! For M2, twice the tidal period spans slightly more than one full
+         ! day. Compute the maximum number of equal intervals that span exactly
+         ! twice the tidal period *and* whose mid-points fall within a 24-hour
+         ! period from midnight to midnight.
+         zdt = Wave(itide(1))%nt * 2.0_wp * rpi / ztide_omega(1)
+         ndiadetide = FLOOR( zdt / ( zdt - 86400.0_wp ) )
+         DEALLOCATE( ztide_omega, ztide_v, ztide_u, ztide_f, itide )
+         ! Compute mid-points of the intervals to be included in the detided
+         ! average
+         ALLOCATE ( tdiadetide(ndiadetide) )
+         DO jn = 1, ndiadetide
+            tdiadetide(jn) = ( REAL( jn, KIND=wp) - 0.5_wp ) * zdt / REAL( ndiadetide, KIND=wp ) - ( zdt - 86400.0_wp ) * 0.5_wp
+         END DO
+      END IF
+
+   END SUBROUTINE dia_detide_init
+
+   SUBROUTINE dia_detide( kt )
+      !!----------------------------------------------------------------------
+      !!                  ***  ROUTINE dia_detide  ***
+      !!
+      !! ** Purpose : weight computation for daily detided model diagnostics
+      !!----------------------------------------------------------------------
+
+      INTEGER, INTENT(in)          ::   kt
+      REAL(wp), DIMENSION(jpi,jpj) ::   zwght_2D
+      REAL(wp)                     ::   zwght, ztmp
+      INTEGER                      ::   jn
+
+      ! Compute detiding weight at the current time-step; the daily total weight
+      ! is one, and the daily summation of a diagnosed field multiplied by this
+      ! weight should provide daily detided averages
+      zwght = 0.0_wp
+      DO jn = 1, ndiadetide
+         ztmp = ( tdiadetide(jn) - REAL( nsec_day, KIND=wp ) ) / rdt
+         IF ( ( ztmp < 0.5_wp ).AND.( ztmp >= -0.5_wp ) ) THEN
+            zwght = zwght + 1.0_wp / REAL( ndiadetide, KIND=wp )
+         END IF
+      END DO
+      zwght_2D(:,:) = zwght
+      CALL iom_put( "diadetide_weight", zwght_2D)
+
+   END SUBROUTINE dia_detide
+
+END MODULE diadetide
Index: NEMO/branches/2019/dev_r11879_ENHANCE-05_SimonM-Harmonic_Analysis/src/OCE/nemogcm.F90
===================================================================
--- NEMO/branches/2019/dev_r11879_ENHANCE-05_SimonM-Harmonic_Analysis/src/OCE/nemogcm.F90	(revision 12021)
+++ NEMO/branches/2019/dev_r11879_ENHANCE-05_SimonM-Harmonic_Analysis/src/OCE/nemogcm.F90	(revision 12022)
@@ -74,4 +74,5 @@
    USE diatmb         ! Top,middle,bottom output
    USE dia25h         ! 25h mean output
+   USE diadetide      ! Weights computation for daily detiding of model diagnostics
    USE sbc_oce , ONLY : lk_oasis
    USE wet_dry        ! Wetting and drying setting   (wad_init routine)
@@ -486,4 +487,5 @@
                            CALL dia_tmb_init    ! TMB outputs
                            CALL dia_25h_init    ! 25h mean  outputs
+                           CALL dia_detide_init ! Weights computation for daily detiding of model diagnostics
      IF( ln_diaobs    )    CALL dia_obs( nit000-1 )   ! Observation operator for restart
                            CALL dia_mlr_init    ! Initialisation of IOM context management for multiple-linear-regression analysis
Index: NEMO/branches/2019/dev_r11879_ENHANCE-05_SimonM-Harmonic_Analysis/src/OCE/step.F90
===================================================================
--- NEMO/branches/2019/dev_r11879_ENHANCE-05_SimonM-Harmonic_Analysis/src/OCE/step.F90	(revision 12021)
+++ NEMO/branches/2019/dev_r11879_ENHANCE-05_SimonM-Harmonic_Analysis/src/OCE/step.F90	(revision 12022)
@@ -212,4 +212,5 @@
                          CALL dia_wri ( kstp )        ! ocean model: outputs
       !
+      IF( lk_diadetide ) CALL dia_detide( kstp )      ! Weights computation for daily detiding of model diagnostics
       IF( lk_diamlr  )   CALL dia_mlr                 ! Update time used in multiple-linear-regression analysis
       IF( ln_crs     )   CALL crs_fld       ( kstp )  ! ocean model: online field coarsening & output
Index: NEMO/branches/2019/dev_r11879_ENHANCE-05_SimonM-Harmonic_Analysis/src/OCE/step_oce.F90
===================================================================
--- NEMO/branches/2019/dev_r11879_ENHANCE-05_SimonM-Harmonic_Analysis/src/OCE/step_oce.F90	(revision 12021)
+++ NEMO/branches/2019/dev_r11879_ENHANCE-05_SimonM-Harmonic_Analysis/src/OCE/step_oce.F90	(revision 12022)
@@ -81,4 +81,5 @@
    USE diacfl
    USE diaobs          ! Observation operator
+   USE diadetide       ! Weights computation for daily detiding of model diagnostics
    USE diamlr          ! IOM context management for multiple-linear-regression analysis
    USE flo_oce         ! floats variables
