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

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

Adjustments to variable names and overlong lines (ticket #2175)

File size: 16.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
66      CHARACTER(LEN=256)                          ::   clxatt_expr
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="Intermediary 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 name attribute (and overwrite possible pre-configured name)
196                  ! with field id to enable id string retrieval from stored handle
197                  ! below; also re-set expression with possible substitutions
198                  CALL xios_set_attr  ( slxhdl_regs(ireg+1), name="diamlr_r"//cl3i, expr=TRIM( clxatt_expr ) )
199
200                  ireg = ireg + 1   ! Accept regressor in list of active regressors
201
202               END IF
203            END IF
204
205            ! Look for field
206            IF ( xios_is_valid_field( "diamlr_f"//cl3i ) ) THEN
207
208               CALL xios_get_handle( "diamlr_f"//cl3i, slxhdl_flds(ifld+1) )
209               ! Retrieve pre-configured value of "enabled" attribute
210               CALL xios_get_attr  ( slxhdl_flds(ifld+1), enabled=llxatt_enabled )
211               ! If enabled, keep handle in list of fields selected for analysis
212               IF ( llxatt_enabled ) THEN
213                 
214                  ! Set name attribute (and overwrite possible pre-configured name)
215                  ! with field id to enable id string retrieval from stored handle
216                  ! below
217                  CALL xios_set_attr  ( slxhdl_flds(ifld+1), name="diamlr_f"//cl3i )
218
219                  ifld = ifld + 1   ! Accept field in list of fields selected for analysis
220
221               END IF
222            END IF
223
224         END DO
225
226         ! Release tidal data
227         DEALLOCATE( itide_const, ztide_omega, ztide_u, ztide_v, ztide_f )
228
229         ! Output number of active regressors and fields selected for analysis
230         IF ( lwp ) WRITE(numout,'(a,i3,a)' ) 'diamlr: ', ireg, ' active regressors found'
231         IF ( lwp ) WRITE(numout,'(a,i3,a)' ) 'diamlr: ', ifld, ' fields selected for analysis'
232
233         ! For each active regressor:
234         DO jm = 1, ireg
235
236            !   i) set up 2-dimensional and 3-dimensional versions of the
237            !      regressors; explicitely set "enabled" attribute; note, while
238            !      the scalar versions of regressors are part of the
239            !      configuration, the respective 2-dimensional versions take
240            !      over the defining expression, while the scalar and
241            !      3-dimensional versions are simply obtained via grid
242            !      transformations from the 2-dimensional version.
243            CALL xios_get_attr  ( slxhdl_regs( jm ), name=clxatt_name1, expr=clxatt_expr, enabled=llxatt_enabled )
244            CALL xios_add_child ( slxhdl_fldgrp, slxhdl_fld, TRIM( clxatt_name1 )//"_2D" )
245            CALL xios_set_attr  ( slxhdl_fld, expr=TRIM( clxatt_expr ), grid_ref="diamlr_grid_2D",     &
246               &                  field_ref="diamlr_time", enabled=llxatt_enabled )
247            CALL xios_add_child ( slxhdl_fldgrp, slxhdl_fld, TRIM( clxatt_name1 )//"_3D")
248            CALL xios_set_attr  ( slxhdl_fld, expr="this", grid_ref="diamlr_grid_2D_to_3D",            &
249               &                  field_ref=TRIM( clxatt_name1 )//"_2D", enabled=llxatt_enabled)
250            CALL xios_set_attr  ( slxhdl_regs(jm), expr="this", grid_ref="diamlr_grid_2D_to_scalar",   &
251               &                  field_ref=TRIM( clxatt_name1 )//"_2D", enabled=llxatt_enabled)
252
253            !  ii) set up definitions for the output of scalar products with
254            !      itself and with other active regressors
255            CALL xios_get_attr  ( slxhdl_regs(jm), name=clxatt_name1 )
256            CALL xios_get_handle( "diamlr_file_scalar", slxhdl_fil)
257            DO jn = 1, jm
258               ! Field for product between regressors
259               CALL xios_get_attr  ( slxhdl_regs(jn), name=clxatt_name2, enabled=llxatt_enabled )
260               CALL xios_add_child ( slxhdl_fldgrp, slxhdl_fld, TRIM( clxatt_name1 )//"."//TRIM( clxatt_name2 ) )
261               ! Set appropriate name attribute to avoid the possibility of
262               ! using an inappropriate inherited name attribute as the variable
263               ! name in the output file
264               CALL xios_set_attr  ( slxhdl_fld, name=TRIM( clxatt_name1 )//"."//TRIM( clxatt_name2 ),      &
265                  &                  grid_ref="diamlr_grid_scalar", expr="this * "//TRIM( clxatt_name2 ),   &
266                  &                  field_ref=TRIM( clxatt_name1 ), enabled=llxatt_enabled, operation="accumulate")
267               ! Add regressor-product field to output file
268               CALL xios_add_child ( slxhdl_fil, slxhdl_fld, TRIM( clxatt_name1 )//"."//TRIM( clxatt_name2 ) )
269            END DO
270
271            ! iii) set up definitions for the output of scalar products with
272            !      fields selected for analysis
273            DO jn = 1, ifld
274               CALL xios_get_attr( slxhdl_flds(jn), name=clxatt_name2, grid_ref=clxatt_gridref, field_ref=clxatt_fieldref )
275               clgt="T"
276               IF ( INDEX( clxatt_gridref, "_U_" ) > 0 ) clgt="U"
277               IF ( INDEX( clxatt_gridref, "_V_" ) > 0 ) clgt="V"
278               IF ( INDEX( clxatt_gridref, "_W_" ) > 0 ) clgt="W"
279               clgd="2D"
280               IF ( INDEX( clxatt_gridref, "_3D" ) > 0 ) clgd="3D"
281               CALL xios_add_child ( slxhdl_fldgrp, slxhdl_fld, TRIM( clxatt_name2 )//"."//TRIM( clxatt_name1 ) )
282               ! Set appropriate name attribute to avoid the possibility of
283               ! using an inappropriate inherited name attribute as the variable
284               ! name in the output file
285               CALL xios_set_attr  ( slxhdl_fld, name=TRIM( clxatt_name2 )//"."//TRIM( clxatt_name1 ),         &
286                  &                  expr="this * "//TRIM( clxatt_fieldref ), grid_ref="diamlr_grid_"//clgd,   &
287                  &                  field_ref=TRIM( clxatt_name1 )//"_"//clgd, enabled=llxatt_enabled, operation="accumulate" )
288               CALL xios_get_handle( "diamlr_file_grid_"//clgt, slxhdl_fil )
289               CALL xios_add_child ( slxhdl_fil, slxhdl_fld, TRIM( clxatt_name2 )//"."//TRIM( clxatt_name1 ) )
290            END DO
291
292         END DO
293
294         ! Release list of active regressors and fields selected for analysis
295         DEALLOCATE( slxhdl_regs, slxhdl_flds )
296
297      END IF
298
299   END SUBROUTINE dia_mlr_iom_init
300
301   SUBROUTINE dia_mlr
302      !!----------------------------------------------------------------------
303      !!                   ***  ROUTINE dia_mlr  ***
304      !!
305      !! ** Purpose : update time used in multiple-linear-regression analysis
306      !!
307      !!----------------------------------------------------------------------
308
309      REAL(wp), DIMENSION(jpi,jpj) ::   zadatrj2d
310
311      IF( ln_timing )   CALL timing_start('dia_mlr')
312
313      ! Update time to the continuous time since the start of the model run
314      ! (value of adatrj converted to time in units of seconds)
315      !
316      ! A 2-dimensional field of constant value is sent, and subsequently used
317      ! directly or transformed to a scalar or a constant 3-dimensional field as
318      ! required.
319      zadatrj2d(:,:) = adatrj*86400.0_wp
320      IF ( iom_use('diamlr_time') ) CALL iom_put('diamlr_time', zadatrj2d)
321     
322      IF( ln_timing )   CALL timing_stop('dia_mlr')
323
324   END SUBROUTINE dia_mlr
325
326END MODULE diamlr
Note: See TracBrowser for help on using the repository browser.