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 @ 11971

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

Inclusion of average regressor values, regressor metadata, and references to the analysed fields in the intermediate-data output for multiple-linear-regression analysis (ticket #2175)

File size: 19.1 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         ! For each active regressor:
246         DO jm = 1, ireg
247
248            !   i) set up 2-dimensional and 3-dimensional versions of the
249            !      regressors; explicitely set "enabled" attribute; note, while
250            !      the scalar versions of regressors are part of the
251            !      configuration, the respective 2-dimensional versions take
252            !      over the defining expression, while the scalar and
253            !      3-dimensional versions are simply obtained via grid
254            !      transformations from the 2-dimensional version.
255            CALL xios_get_attr  ( slxhdl_regs( jm ), name=clxatt_name1, expr=clxatt_expr,              &
256               &                  enabled=llxatt_enabled, comment=clxatt_comment )
257            CALL xios_add_child ( slxhdl_fldgrp, slxhdl_fld, TRIM( clxatt_name1 )//"_2D" )
258            CALL xios_set_attr  ( slxhdl_fld, expr=TRIM( clxatt_expr ), grid_ref="diamlr_grid_2D",     &
259               &                  field_ref="diamlr_time", enabled=llxatt_enabled )
260            CALL xios_add_child ( slxhdl_fldgrp, slxhdl_fld, TRIM( clxatt_name1 )//"_3D")
261            CALL xios_set_attr  ( slxhdl_fld, expr="this", grid_ref="diamlr_grid_2D_to_3D",            &
262               &                  field_ref=TRIM( clxatt_name1 )//"_2D", enabled=llxatt_enabled)
263            CALL xios_set_attr  ( slxhdl_regs(jm), expr="this", grid_ref="diamlr_grid_2D_to_scalar",   &
264               &                  field_ref=TRIM( clxatt_name1 )//"_2D", enabled=llxatt_enabled)
265
266            !  ii) set up output of active regressors, including metadata
267            CALL xios_get_handle( "diamlr_file_scalar", slxhdl_fil )
268            ! Add regressor to output file
269            CALL xios_add_child ( slxhdl_fil, slxhdl_fld, TRIM( clxatt_name1 ) )
270            CALL xios_set_attr  ( slxhdl_fld, standard_name=TRIM( clxatt_comment ), long_name=TRIM( clxatt_expr ),   &
271               &                  operation="average" )
272               
273            ! iii) set up the output of scalar products with itself and with
274            !      other active regressors
275            CALL xios_get_attr  ( slxhdl_regs(jm), name=clxatt_name1 )
276            DO jn = 1, jm
277               ! Field for product between regressors
278               CALL xios_get_attr  ( slxhdl_regs(jn), name=clxatt_name2 )
279               CALL xios_add_child ( slxhdl_fldgrp, slxhdl_fld, TRIM( clxatt_name1 )//"."//TRIM( clxatt_name2 ) )
280               ! Set appropriate name attribute to avoid the possibility of
281               ! using an inappropriate inherited name attribute as the variable
282               ! name in the output file
283               CALL xios_set_attr  ( slxhdl_fld,                                                        &
284                  &                  name=TRIM( clxatt_name1 )//"."//TRIM( clxatt_name2 ),              &
285                  &                  grid_ref="diamlr_grid_scalar",                                     &
286                  &                  expr="this * "//TRIM( clxatt_name2 ),                              &
287                  &                  field_ref=TRIM( clxatt_name1 ),                                    &
288                  &                  enabled=llxatt_enabled,                                            &
289                  &                  long_name="Scalar product of regressor "//TRIM( clxatt_name1 )//   &
290                  &                     " and regressor "//TRIM( clxatt_name2 ),                        &
291                  &                  standard_name=TRIM( clxatt_name1 )//"."//TRIM( clxatt_name2 ),     &
292                  &                  operation="accumulate")
293               ! Add regressor-product field to output file
294               CALL xios_add_child ( slxhdl_fil, slxhdl_fld, TRIM( clxatt_name1 )//"."//TRIM( clxatt_name2 ) )
295            END DO
296
297            ! iii) set up definitions for the output of scalar products with
298            !      fields selected for analysis
299            DO jn = 1, ifld
300               CALL xios_get_attr( slxhdl_flds(jn), name=clxatt_name2, grid_ref=clxatt_gridref, field_ref=clxatt_fieldref )
301               clgt="T"
302               IF ( INDEX( clxatt_gridref, "_U_" ) > 0 ) clgt="U"
303               IF ( INDEX( clxatt_gridref, "_V_" ) > 0 ) clgt="V"
304               IF ( INDEX( clxatt_gridref, "_W_" ) > 0 ) clgt="W"
305               clgd="2D"
306               IF ( INDEX( clxatt_gridref, "_3D" ) > 0 ) clgd="3D"
307               CALL xios_add_child ( slxhdl_fldgrp, slxhdl_fld, TRIM( clxatt_name2 )//"."//TRIM( clxatt_name1 ) )
308               ! Set appropriate name attribute to avoid the possibility of
309               ! using an inappropriate inherited name attribute as the variable
310               ! name in the output file; use metadata (standard_name and
311               ! long_name) to refer to the id of the analysed field
312               CALL xios_set_attr  ( slxhdl_fld,                                                         &
313                  &                  name=TRIM( clxatt_name2 )//"."//TRIM( clxatt_name1 ),               &
314                  &                  expr="this * "//TRIM( clxatt_fieldref ),                            &
315                  &                  grid_ref="diamlr_grid_"//clgd,                                      &
316                  &                  field_ref=TRIM( clxatt_name1 )//"_"//clgd,                          &
317                  &                  enabled=llxatt_enabled,                                             &
318                  &                  long_name="Scalar product of "//TRIM( clxatt_fieldref )//           &
319                  &                     " and regressor "//TRIM( clxatt_name1 ),                         &
320                  &                  standard_name=TRIM( clxatt_fieldref )//"."//TRIM( clxatt_name1 ),   &
321                  &                  operation="accumulate" )
322               CALL xios_get_handle( "diamlr_file_grid_"//clgt, slxhdl_fil )
323               CALL xios_add_child ( slxhdl_fil, slxhdl_fld, TRIM( clxatt_name2 )//"."//TRIM( clxatt_name1 ) )
324            END DO
325
326         END DO
327
328         ! Release list of active regressors and fields selected for analysis
329         DEALLOCATE( slxhdl_regs, slxhdl_flds )
330
331      END IF
332
333   END SUBROUTINE dia_mlr_iom_init
334
335   SUBROUTINE dia_mlr
336      !!----------------------------------------------------------------------
337      !!                   ***  ROUTINE dia_mlr  ***
338      !!
339      !! ** Purpose : update time used in multiple-linear-regression analysis
340      !!
341      !!----------------------------------------------------------------------
342
343      REAL(wp), DIMENSION(jpi,jpj) ::   zadatrj2d
344
345      IF( ln_timing )   CALL timing_start('dia_mlr')
346
347      ! Update time to the continuous time since the start of the model run
348      ! (value of adatrj converted to time in units of seconds)
349      !
350      ! A 2-dimensional field of constant value is sent, and subsequently used
351      ! directly or transformed to a scalar or a constant 3-dimensional field as
352      ! required.
353      zadatrj2d(:,:) = adatrj*86400.0_wp
354      IF ( iom_use('diamlr_time') ) CALL iom_put('diamlr_time', zadatrj2d)
355     
356      IF( ln_timing )   CALL timing_stop('dia_mlr')
357
358   END SUBROUTINE dia_mlr
359
360END MODULE diamlr
Note: See TracBrowser for help on using the repository browser.