New URL for NEMO forge!   http://forge.nemo-ocean.eu

Since March 2022 along with NEMO 4.2 release, the code development moved to a self-hosted GitLab.
This present forge is now archived and remained online for history.
diamlr.F90 in NEMO/branches/2019/dev_r11879_ENHANCE-05_SimonM-Harmonic_Analysis/src/OCE/DIA – NEMO

source: NEMO/branches/2019/dev_r11879_ENHANCE-05_SimonM-Harmonic_Analysis/src/OCE/DIA/diamlr.F90 @ 11972

Last change on this file since 11972 was 11972, checked in by smueller, 4 years ago

Inclusion of minimum, maximum, and average values of the time variable available to compute regressors (diamlr_time) in the intermediate-data output for multiple-linear-regression analysis (ticket #2175)

File size: 20.6 KB
Line 
1MODULE diamlr
2   !!======================================================================
3   !!                       ***  MODULE  diamlr  ***
4   !! Management of the IOM context for multiple-linear-regression analysis
5   !!======================================================================
6   !! History :       !  2019  (S. Mueller)
7   !!----------------------------------------------------------------------
8
9   USE par_oce        , ONLY :   wp, jpi, jpj
10   USE phycst         , ONLY :   rpi
11   USE in_out_manager , ONLY :   lwp, numout, ln_timing
12   USE iom            , ONLY :   iom_put, iom_use, iom_update_file_name
13   USE dom_oce        , ONLY :   adatrj
14   USE timing         , ONLY :   timing_start, timing_stop
15   USE xios
16   USE tide_mod       , ONLY :   tide_harmo, jpmax_harmo, Wave
17
18   IMPLICIT NONE
19   PRIVATE
20
21   LOGICAL, PUBLIC ::   lk_diamlr = .FALSE.
22
23   PUBLIC ::   dia_mlr_init, dia_mlr_iom_init, dia_mlr
24
25   !!----------------------------------------------------------------------
26   !! NEMO/OCE 4.0 , NEMO Consortium (2019)
27   !! $Id$
28   !! Software governed by the CeCILL license (see ./LICENSE)
29   !!----------------------------------------------------------------------
30CONTAINS
31   
32   SUBROUTINE dia_mlr_init
33      !!----------------------------------------------------------------------
34      !!                 ***  ROUTINE dia_mlr_init  ***
35      !!
36      !! ** Purpose : initialisation of IOM context management for
37      !!              multiple-linear-regression analysis
38      !!
39      !!----------------------------------------------------------------------
40
41      lk_diamlr = .TRUE.
42
43      IF(lwp) THEN
44         WRITE(numout, *)
45         WRITE(numout, *) 'dia_mlr_init : initialisation of IOM context management for'
46         WRITE(numout, *) '~~~~~~~~~~~~   multiple-linear-regression analysis'
47      END IF
48
49   END SUBROUTINE dia_mlr_init
50
51   SUBROUTINE dia_mlr_iom_init
52      !!----------------------------------------------------------------------
53      !!               ***  ROUTINE dia_mlr_iom_init  ***
54      !!
55      !! ** Purpose : IOM context setup for multiple-linear-regression
56      !!              analysis
57      !!
58      !!----------------------------------------------------------------------
59
60      TYPE(xios_fieldgroup)                       ::   slxhdl_fldgrp
61      TYPE(xios_filegroup)                        ::   slxhdl_filgrp
62      TYPE(xios_field), ALLOCATABLE, DIMENSION(:) ::   slxhdl_regs,    slxhdl_flds
63      TYPE(xios_field)                            ::   slxhdl_fld
64      TYPE(xios_file)                             ::   slxhdl_fil
65      LOGICAL                                     ::   llxatt_enabled, llxatt_comment
66      CHARACTER(LEN=256)                          ::   clxatt_expr,    clxatt_comment
67      CHARACTER(LEN=32)                           ::   clxatt_name1,   clxatt_name2
68      CHARACTER(LEN=32)                           ::   clxatt_gridref, clxatt_fieldref
69      INTEGER, PARAMETER                          ::   jpscanmax = 999
70      INTEGER                                     ::   ireg, ifld
71      CHARACTER(LEN=3)                            ::   cl3i
72      CHARACTER(LEN=6)                            ::   cl6a
73      CHARACTER(LEN=1)                            ::   clgt
74      CHARACTER(LEN=2)                            ::   clgd
75      CHARACTER(LEN=25)                           ::   clfloat
76      CHARACTER(LEN=32)                           ::   clrepl
77      INTEGER                                     ::   jl, jm, jn
78      INTEGER                                     ::   itide                       ! Number of available tidal components
79      INTEGER,  ALLOCATABLE, DIMENSION(:)         ::   itide_const                 ! Index list of selected tidal constituents
80      REAL(wp), ALLOCATABLE, DIMENSION(:)         ::   ztide_omega, ztide_u,   &   ! Tidal frequency, phase, nodal correction
81         &                                             ztide_v, ztide_f
82      REAL(wp)                                    ::   ztide_phase                 ! Tidal-constituent phase at adatrj=0
83
84      IF(lwp) THEN
85         WRITE(numout, *)
86         WRITE(numout, *) 'dia_mlr_iom_init : IOM context setup for multiple-linear-regression'
87         WRITE(numout, *) '~~~~~~~~~~~~~~~~'
88      END IF
89
90      ! Get handles to multiple-linear-regression analysis configuration (field
91      ! group 'diamrl_fields' and file group 'diamlr_files'); if no suitable
92      ! configuration is found, disable diamlr
93      IF ( lk_diamlr .AND. xios_is_valid_fieldgroup( "diamlr_fields" ) .AND. xios_is_valid_field( "diamlr_time" ) .AND.   &
94         & xios_is_valid_filegroup( "diamlr_files" ) ) THEN
95         CALL xios_get_handle("diamlr_fields", slxhdl_fldgrp)
96         CALL xios_get_handle("diamlr_files",  slxhdl_filgrp)
97      ELSE
98         IF (lwp) THEN
99            WRITE(numout, *) "diamlr: configuration not found or icomplete (field group 'diamlr_fields'"
100            WRITE(numout, *) "        and/or file group 'diamlr_files' and/or field 'diamlr_time' missing);"
101            WRITE(numout, *) "        disabling output for multiple-linear-regression analysis."
102         END IF
103         lk_diamlr = .FALSE.
104      END IF
105
106      ! Set up IOM context for multiple-linear-regression analysis
107      IF ( lk_diamlr ) THEN
108
109         ! Set up output files for grid types scalar, grid_T, grid_U, grid_V,
110         ! and grid_W
111         DO jm = 1, 5
112            SELECT CASE( jm )
113            CASE( 1 )
114               cl6a = 'scalar'
115            CASE( 2 )
116               cl6a = 'grid_T'
117            CASE( 3 )
118               cl6a = 'grid_U'
119            CASE( 4 )
120               cl6a = 'grid_V'
121            CASE( 5 )
122               cl6a = 'grid_W'
123            END SELECT
124            CALL xios_add_child      ( slxhdl_filgrp, slxhdl_fil, "diamlr_file_"//cl6a )
125            CALL xios_set_attr       ( slxhdl_fil, name_suffix="_diamlr_"//cl6a,   &
126               &                       description="Intermediate output for multiple-linear-regression analysis - "//cl6a )
127            CALL iom_update_file_name( "diamlr_file_"//cl6a )
128         END DO
129
130         ! Compile lists of active regressors and of fields selected for
131         ! analysis (fields "diamlr_r<nnn>" and "diamlr_f<nnn>", where <nnn> is
132         ! a 3-digit integer); also carry out placeholder substitution of tidal
133         ! parameters in regressor expressions
134         !
135         ALLOCATE( slxhdl_regs( jpscanmax ), slxhdl_flds( jpscanmax ) )
136         ireg = 0
137         ifld = 0
138         !
139         ! Retrieve information (frequency, phase, nodal correction) about all
140         ! available tidal constituents for placeholder substitution below
141         itide = jpmax_harmo
142         ALLOCATE(itide_const(itide), ztide_omega(itide), ztide_u(itide), ztide_v(itide), ztide_f(itide))
143         DO jn = 1, itide
144            itide_const(jn) = jn   ! Select all available tidal constituents
145         END DO
146         CALL tide_harmo( ztide_omega, ztide_v, ztide_u, ztide_f, itide_const, itide )
147         
148         DO jm = 1, jpscanmax
149            WRITE (cl3i, '(i3.3)') jm
150
151            ! Look for regressor
152            IF ( xios_is_valid_field( "diamlr_r"//cl3i ) ) THEN
153
154               CALL xios_get_handle( "diamlr_r"//cl3i, slxhdl_regs(ireg+1) )
155               ! Retrieve pre-configured value of "enabled" attribute and
156               ! regressor expression
157               CALL xios_get_attr  ( slxhdl_regs(ireg+1), enabled=llxatt_enabled, expr=clxatt_expr )
158               ! If enabled, keep handle in list of active regressors; also
159               ! substitute placeholders for tidal frequencies, phases, and
160               ! nodal corrections in regressor expressions
161               IF ( llxatt_enabled ) THEN
162
163                  ! Substitution of placeholders for tidal-constituent
164                  ! parameters (amplitudes, angular veloccities, nodal phase
165                  ! correction) with values that have been obtained from the
166                  ! tidal-forcing implementation
167                  DO jn = 1, itide
168                     ! Compute phase of tidal constituent (incl. current nodal
169                     ! correction) at the start of the model run (i.e. for
170                     ! adatrj=0)
171                     ztide_phase = MOD( ztide_u(jn) +  ztide_v(jn) - adatrj * 86400.0_wp * ztide_omega(jn), 2.0_wp * rpi )
172                     clrepl = "__TDE_"//TRIM( Wave(jn)%cname_tide )//"_omega__"
173                     DO WHILE ( INDEX( clxatt_expr, TRIM( clrepl ) ) > 0 )
174                        WRITE (clfloat, '(e25.18)') ztide_omega(jn)
175                        jl = INDEX( clxatt_expr, TRIM( clrepl ) )
176                        clxatt_expr = clxatt_expr(1:jl - 1)//clfloat//   &
177                           &          clxatt_expr(jl + LEN( TRIM( clrepl ) ):LEN( TRIM( clxatt_expr ) ))
178                     END DO
179                     clrepl = "__TDE_"//TRIM( Wave(jn)%cname_tide )//"_phase__"
180                     DO WHILE ( INDEX( clxatt_expr, TRIM( clrepl ) ) > 0 )
181                        WRITE (clfloat, '(e25.18)') ztide_phase
182                        jl = INDEX( clxatt_expr, TRIM( clrepl ) )
183                        clxatt_expr = clxatt_expr(1:jl - 1)//clfloat//   &
184                           &          clxatt_expr(jl + LEN( TRIM( clrepl ) ):LEN( TRIM( clxatt_expr ) ))
185                     END DO
186                     clrepl = "__TDE_"//TRIM( Wave(jn)%cname_tide )//"_amplitude__"
187                     DO WHILE (INDEX( clxatt_expr, TRIM( clrepl ) ) > 0 )
188                        WRITE (clfloat, '(e25.18)') ztide_f(jn)
189                        jl = INDEX( clxatt_expr, TRIM( clrepl ) )
190                        clxatt_expr = clxatt_expr(1:jl - 1)//clfloat//   &
191                           &          clxatt_expr(jl + LEN( TRIM( clrepl ) ):LEN( TRIM( clxatt_expr ) ))
192                     END DO
193                  END DO
194
195                  ! Set standard value for comment attribute, including possible
196                  ! existing comment added in parantheses
197                  CALL xios_is_defined_attr( slxhdl_regs(ireg+1), comment=llxatt_comment )
198                  IF ( llxatt_comment ) THEN
199                     CALL xios_get_attr( slxhdl_regs(ireg+1), comment=clxatt_comment )
200                     clxatt_comment = "Regressor "//cl3i//" ("//TRIM( clxatt_comment )//") "
201                  ELSE
202                     clxatt_comment = "Regressor "//cl3i
203                  END IF
204
205                  ! Set name attribute (and overwrite possible pre-configured
206                  ! name) with field id to enable id string retrieval from
207                  ! stored handle below, re-set expression with possible
208                  ! substitutions, and set or re-set comment attribute
209                  CALL xios_set_attr  ( slxhdl_regs(ireg+1), name="diamlr_r"//cl3i, expr=TRIM( clxatt_expr ),   &
210                     &                  comment=TRIM( clxatt_comment ) )
211
212                  ireg = ireg + 1   ! Accept regressor in list of active regressors
213
214               END IF
215            END IF
216
217            ! Look for field
218            IF ( xios_is_valid_field( "diamlr_f"//cl3i ) ) THEN
219
220               CALL xios_get_handle( "diamlr_f"//cl3i, slxhdl_flds(ifld+1) )
221               ! Retrieve pre-configured value of "enabled" attribute
222               CALL xios_get_attr  ( slxhdl_flds(ifld+1), enabled=llxatt_enabled )
223               ! If enabled, keep handle in list of fields selected for analysis
224               IF ( llxatt_enabled ) THEN
225                 
226                  ! Set name attribute (and overwrite possible pre-configured name)
227                  ! with field id to enable id string retrieval from stored handle
228                  ! below
229                  CALL xios_set_attr  ( slxhdl_flds(ifld+1), name="diamlr_f"//cl3i )
230
231                  ifld = ifld + 1   ! Accept field in list of fields selected for analysis
232
233               END IF
234            END IF
235
236         END DO
237
238         ! Release tidal data
239         DEALLOCATE( itide_const, ztide_omega, ztide_u, ztide_v, ztide_f )
240
241         ! Output number of active regressors and fields selected for analysis
242         IF ( lwp ) WRITE(numout,'(a,i3,a)' ) 'diamlr: ', ireg, ' active regressors found'
243         IF ( lwp ) WRITE(numout,'(a,i3,a)' ) 'diamlr: ', ifld, ' fields selected for analysis'
244
245         ! Set up output of minimum, maximum, and average values of the time
246         ! variable available for the computation of regressors (diamlr_time)
247         CALL xios_get_handle( "diamlr_file_scalar", slxhdl_fil )
248         CALL xios_add_child ( slxhdl_fil, slxhdl_fld, "diamlr_time_average" )
249         CALL xios_set_attr  ( slxhdl_fld, standard_name="diamlr_time",                          &
250            &                  long_name="Elapsed model time at start of regression interval",   &
251            &                  unit="s", operation="average", field_ref="diamlr_time",           &
252            &                  grid_ref="diamlr_grid_2D_to_scalar" )
253         CALL xios_add_child ( slxhdl_fil, slxhdl_fld, "diamlr_time_minimum" )
254         CALL xios_set_attr  ( slxhdl_fld, standard_name="diamlr_time",                          &
255            &                  long_name="Elapsed model time at start of regression interval",   &
256            &                  unit="s", operation="minimum", field_ref="diamlr_time",           &
257            &                  grid_ref="diamlr_grid_2D_to_scalar" )
258         CALL xios_add_child ( slxhdl_fil, slxhdl_fld, "diamlr_time_maximum" )
259         CALL xios_set_attr  ( slxhdl_fld, standard_name="diamlr_time",                          &
260            &                  long_name="Elapsed model time at start of regression interval",   &
261            &                  unit="s", operation="maximum", field_ref="diamlr_time",           &
262            &                  grid_ref="diamlr_grid_2D_to_scalar" )
263
264         ! For each active regressor:
265         DO jm = 1, ireg
266
267            !   i) set up 2-dimensional and 3-dimensional versions of the
268            !      regressors; explicitely set "enabled" attribute; note, while
269            !      the scalar versions of regressors are part of the
270            !      configuration, the respective 2-dimensional versions take
271            !      over the defining expression, while the scalar and
272            !      3-dimensional versions are simply obtained via grid
273            !      transformations from the 2-dimensional version.
274            CALL xios_get_attr  ( slxhdl_regs( jm ), name=clxatt_name1, expr=clxatt_expr,              &
275               &                  enabled=llxatt_enabled, comment=clxatt_comment )
276            CALL xios_add_child ( slxhdl_fldgrp, slxhdl_fld, TRIM( clxatt_name1 )//"_2D" )
277            CALL xios_set_attr  ( slxhdl_fld, expr=TRIM( clxatt_expr ), grid_ref="diamlr_grid_2D",     &
278               &                  field_ref="diamlr_time", enabled=llxatt_enabled )
279            CALL xios_add_child ( slxhdl_fldgrp, slxhdl_fld, TRIM( clxatt_name1 )//"_3D")
280            CALL xios_set_attr  ( slxhdl_fld, expr="this", grid_ref="diamlr_grid_2D_to_3D",            &
281               &                  field_ref=TRIM( clxatt_name1 )//"_2D", enabled=llxatt_enabled)
282            CALL xios_set_attr  ( slxhdl_regs(jm), expr="this", grid_ref="diamlr_grid_2D_to_scalar",   &
283               &                  field_ref=TRIM( clxatt_name1 )//"_2D", enabled=llxatt_enabled)
284
285            !  ii) set up output of active regressors, including metadata
286            CALL xios_get_handle( "diamlr_file_scalar", slxhdl_fil )
287            ! Add regressor to output file
288            CALL xios_add_child ( slxhdl_fil, slxhdl_fld, TRIM( clxatt_name1 ) )
289            CALL xios_set_attr  ( slxhdl_fld, standard_name=TRIM( clxatt_comment ), long_name=TRIM( clxatt_expr ),   &
290               &                  operation="average" )
291               
292            ! iii) set up the output of scalar products with itself and with
293            !      other active regressors
294            CALL xios_get_attr  ( slxhdl_regs(jm), name=clxatt_name1 )
295            DO jn = 1, jm
296               ! Field for product between regressors
297               CALL xios_get_attr  ( slxhdl_regs(jn), name=clxatt_name2 )
298               CALL xios_add_child ( slxhdl_fldgrp, slxhdl_fld, TRIM( clxatt_name1 )//"."//TRIM( clxatt_name2 ) )
299               ! Set appropriate name attribute to avoid the possibility of
300               ! using an inappropriate inherited name attribute as the variable
301               ! name in the output file
302               CALL xios_set_attr  ( slxhdl_fld,                                                        &
303                  &                  name=TRIM( clxatt_name1 )//"."//TRIM( clxatt_name2 ),              &
304                  &                  grid_ref="diamlr_grid_scalar",                                     &
305                  &                  expr="this * "//TRIM( clxatt_name2 ),                              &
306                  &                  field_ref=TRIM( clxatt_name1 ),                                    &
307                  &                  enabled=llxatt_enabled,                                            &
308                  &                  long_name="Scalar product of regressor "//TRIM( clxatt_name1 )//   &
309                  &                     " and regressor "//TRIM( clxatt_name2 ),                        &
310                  &                  standard_name=TRIM( clxatt_name1 )//"."//TRIM( clxatt_name2 ),     &
311                  &                  operation="accumulate")
312               ! Add regressor-product field to output file
313               CALL xios_add_child ( slxhdl_fil, slxhdl_fld, TRIM( clxatt_name1 )//"."//TRIM( clxatt_name2 ) )
314            END DO
315
316            !  iv) set up definitions for the output of scalar products with
317            !      fields selected for analysis
318            DO jn = 1, ifld
319               CALL xios_get_attr( slxhdl_flds(jn), name=clxatt_name2, grid_ref=clxatt_gridref, field_ref=clxatt_fieldref )
320               clgt="T"
321               IF ( INDEX( clxatt_gridref, "_U_" ) > 0 ) clgt="U"
322               IF ( INDEX( clxatt_gridref, "_V_" ) > 0 ) clgt="V"
323               IF ( INDEX( clxatt_gridref, "_W_" ) > 0 ) clgt="W"
324               clgd="2D"
325               IF ( INDEX( clxatt_gridref, "_3D" ) > 0 ) clgd="3D"
326               CALL xios_add_child ( slxhdl_fldgrp, slxhdl_fld, TRIM( clxatt_name2 )//"."//TRIM( clxatt_name1 ) )
327               ! Set appropriate name attribute to avoid the possibility of
328               ! using an inappropriate inherited name attribute as the variable
329               ! name in the output file; use metadata (standard_name and
330               ! long_name) to refer to the id of the analysed field
331               CALL xios_set_attr  ( slxhdl_fld,                                                         &
332                  &                  name=TRIM( clxatt_name2 )//"."//TRIM( clxatt_name1 ),               &
333                  &                  expr="this * "//TRIM( clxatt_fieldref ),                            &
334                  &                  grid_ref="diamlr_grid_"//clgd,                                      &
335                  &                  field_ref=TRIM( clxatt_name1 )//"_"//clgd,                          &
336                  &                  enabled=llxatt_enabled,                                             &
337                  &                  long_name="Scalar product of "//TRIM( clxatt_fieldref )//           &
338                  &                     " and regressor "//TRIM( clxatt_name1 ),                         &
339                  &                  standard_name=TRIM( clxatt_fieldref )//"."//TRIM( clxatt_name1 ),   &
340                  &                  operation="accumulate" )
341               CALL xios_get_handle( "diamlr_file_grid_"//clgt, slxhdl_fil )
342               CALL xios_add_child ( slxhdl_fil, slxhdl_fld, TRIM( clxatt_name2 )//"."//TRIM( clxatt_name1 ) )
343            END DO
344
345         END DO
346
347         ! Release list of active regressors and fields selected for analysis
348         DEALLOCATE( slxhdl_regs, slxhdl_flds )
349
350      END IF
351
352   END SUBROUTINE dia_mlr_iom_init
353
354   SUBROUTINE dia_mlr
355      !!----------------------------------------------------------------------
356      !!                   ***  ROUTINE dia_mlr  ***
357      !!
358      !! ** Purpose : update time used in multiple-linear-regression analysis
359      !!
360      !!----------------------------------------------------------------------
361
362      REAL(wp), DIMENSION(jpi,jpj) ::   zadatrj2d
363
364      IF( ln_timing )   CALL timing_start('dia_mlr')
365
366      ! Update time to the continuous time since the start of the model run
367      ! (value of adatrj converted to time in units of seconds)
368      !
369      ! A 2-dimensional field of constant value is sent, and subsequently used
370      ! directly or transformed to a scalar or a constant 3-dimensional field as
371      ! required.
372      zadatrj2d(:,:) = adatrj*86400.0_wp
373      IF ( iom_use('diamlr_time') ) CALL iom_put('diamlr_time', zadatrj2d)
374     
375      IF( ln_timing )   CALL timing_stop('dia_mlr')
376
377   END SUBROUTINE dia_mlr
378
379END MODULE diamlr
Note: See TracBrowser for help on using the repository browser.