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.
iom.F90 in NEMO/branches/2020/ticket2377/src/OCE/IOM – NEMO

source: NEMO/branches/2020/ticket2377/src/OCE/IOM/iom.F90 @ 12602

Last change on this file since 12602 was 12602, checked in by smueller, 3 years ago

Addition of XIOS-based restart I/O to the restart capability of option 2 of the freshwater-budget adjustment mechanism (ticket #2377)

This changeset extends the standard restart capability added in r12591 by adding optional XIOS-based restart I/O according to the recipe provided in section 10.2.1 of the reference manual for NEMO version 4.0.1.

  • Property svn:keywords set to Id
File size: 130.2 KB
Line 
1MODULE iom
2   !!======================================================================
3   !!                    ***  MODULE  iom ***
4   !! Input/Output manager :  Library to read input files
5   !!======================================================================
6   !! History :  2.0  ! 2005-12  (J. Belier) Original code
7   !!            2.0  ! 2006-02  (S. Masson) Adaptation to NEMO
8   !!            3.0  ! 2007-07  (D. Storkey) Changes to iom_gettime
9   !!            3.4  ! 2012-12  (R. Bourdalle-Badie and G. Reffray)  add C1D case 
10   !!            3.6  ! 2014-15  DIMG format removed
11   !!            3.6  ! 2015-15  (J. Harle) Added procedure to read REAL attributes
12   !!            4.0  ! 2017-11  (M. Andrejczuk) Extend IOM interface to write any 3D fields
13   !!----------------------------------------------------------------------
14
15   !!----------------------------------------------------------------------
16   !!   iom_open       : open a file read only
17   !!   iom_close      : close a file or all files opened by iom
18   !!   iom_get        : read a field (interfaced to several routines)
19   !!   iom_varid      : get the id of a variable in a file
20   !!   iom_rstput     : write a field in a restart file (interfaced to several routines)
21   !!----------------------------------------------------------------------
22   USE dom_oce         ! ocean space and time domain
23   USE c1d             ! 1D vertical configuration
24   USE flo_oce         ! floats module declarations
25   USE lbclnk          ! lateal boundary condition / mpp exchanges
26   USE iom_def         ! iom variables definitions
27   USE iom_nf90        ! NetCDF format with native NetCDF library
28   USE in_out_manager  ! I/O manager
29   USE lib_mpp           ! MPP library
30#if defined key_iomput
31   USE sbc_oce  , ONLY :   nn_fsbc, ght_abl, ghw_abl, e3t_abl, e3w_abl, jpka, jpkam1
32   USE icb_oce  , ONLY :   nclasses, class_num       !  !: iceberg classes
33#if defined key_si3
34   USE ice      , ONLY :   jpl
35#endif
36   USE domngb          ! ocean space and time domain
37   USE phycst          ! physical constants
38   USE dianam          ! build name of file
39   USE xios
40# endif
41   USE ioipsl, ONLY :  ju2ymds    ! for calendar
42   USE crs             ! Grid coarsening
43#if defined key_top
44   USE trc, ONLY    :  profsed
45#endif
46   USE lib_fortran 
47   USE diu_bulk, ONLY : ln_diurnal_only, ln_diurnal
48
49   IMPLICIT NONE
50   PUBLIC   !   must be public to be able to access iom_def through iom
51   
52#if defined key_iomput
53   LOGICAL, PUBLIC, PARAMETER ::   lk_iomput = .TRUE.        !: iom_put flag
54#else
55   LOGICAL, PUBLIC, PARAMETER ::   lk_iomput = .FALSE.       !: iom_put flag
56#endif
57   PUBLIC iom_init, iom_init_closedef, iom_swap, iom_open, iom_close, iom_setkt, iom_varid, iom_get, iom_get_var
58   PUBLIC iom_chkatt, iom_getatt, iom_putatt, iom_getszuld, iom_rstput, iom_delay_rst, iom_put
59   PUBLIC iom_use, iom_context_finalize, iom_update_file_name, iom_miss_val
60
61   PRIVATE iom_rp0d, iom_rp1d, iom_rp2d, iom_rp3d
62   PRIVATE iom_g0d, iom_g1d, iom_g2d, iom_g3d, iom_get_123d
63   PRIVATE iom_p1d, iom_p2d, iom_p3d, iom_p4d
64#if defined key_iomput
65   PRIVATE iom_set_domain_attr, iom_set_axis_attr, iom_set_field_attr, iom_set_file_attr, iom_get_file_attr, iom_set_grid_attr
66   PRIVATE set_grid, set_grid_bounds, set_scalar, set_xmlatt, set_mooring, iom_sdate
67   PRIVATE iom_set_rst_context, iom_set_rstw_active, iom_set_rstr_active
68# endif
69   PUBLIC iom_set_rstw_var_active, iom_set_rstw_core, iom_set_rst_vars
70
71   INTERFACE iom_get
72      MODULE PROCEDURE iom_g0d, iom_g1d, iom_g2d, iom_g3d
73   END INTERFACE
74   INTERFACE iom_getatt
75      MODULE PROCEDURE iom_g0d_iatt, iom_g1d_iatt, iom_g0d_ratt, iom_g1d_ratt, iom_g0d_catt
76   END INTERFACE
77   INTERFACE iom_putatt
78      MODULE PROCEDURE iom_p0d_iatt, iom_p1d_iatt, iom_p0d_ratt, iom_p1d_ratt, iom_p0d_catt
79   END INTERFACE
80   INTERFACE iom_rstput
81      MODULE PROCEDURE iom_rp0d, iom_rp1d, iom_rp2d, iom_rp3d
82   END INTERFACE
83   INTERFACE iom_put
84      MODULE PROCEDURE iom_p0d, iom_p1d, iom_p2d, iom_p3d, iom_p4d
85   END INTERFACE iom_put
86 
87   !! * Substitutions
88#  include "do_loop_substitute.h90"
89   !!----------------------------------------------------------------------
90   !! NEMO/OCE 4.0 , NEMO Consortium (2018)
91   !! $Id$
92   !! Software governed by the CeCILL license (see ./LICENSE)
93   !!----------------------------------------------------------------------
94CONTAINS
95
96   SUBROUTINE iom_init( cdname, fname, ld_tmppatch, ld_closedef ) 
97      !!----------------------------------------------------------------------
98      !!                     ***  ROUTINE   ***
99      !!
100      !! ** Purpose :   
101      !!
102      !!----------------------------------------------------------------------
103      CHARACTER(len=*),           INTENT(in)  :: cdname
104      CHARACTER(len=*), OPTIONAL, INTENT(in)  :: fname
105      LOGICAL         , OPTIONAL, INTENT(in)  :: ld_tmppatch
106      LOGICAL         , OPTIONAL, INTENT(in)  :: ld_closedef
107#if defined key_iomput
108      !
109      TYPE(xios_duration) :: dtime    = xios_duration(0, 0, 0, 0, 0, 0)
110      TYPE(xios_date)     :: start_date
111      CHARACTER(len=lc) :: clname
112      INTEGER             :: irefyear, irefmonth, irefday
113      INTEGER           :: ji, jkmin
114      LOGICAL :: llrst_context              ! is context related to restart
115      !
116      REAL(wp), ALLOCATABLE, DIMENSION(:,:) :: zt_bnds, zw_bnds
117      REAL(wp), DIMENSION(2,jpkam1)         :: za_bnds   ! ABL vertical boundaries
118      LOGICAL ::   ll_tmppatch = .TRUE.    !: seb: patch before we remove periodicity
119      INTEGER ::   nldi_save, nlei_save    !:      and close boundaries in output files
120      INTEGER ::   nldj_save, nlej_save    !:
121      LOGICAL ::   ll_closedef = .TRUE.
122      !!----------------------------------------------------------------------
123      !
124      ! seb: patch before we remove periodicity and close boundaries in output files
125      IF( PRESENT(ld_tmppatch) ) THEN   ;   ll_tmppatch = ld_tmppatch
126      ELSE                              ;   ll_tmppatch = .TRUE.
127      ENDIF
128      IF ( ll_tmppatch ) THEN
129         nldi_save = nldi   ;   nlei_save = nlei
130         nldj_save = nldj   ;   nlej_save = nlej
131         IF( nimpp           ==      1 ) nldi = 1
132         IF( nimpp + jpi - 1 == jpiglo ) nlei = jpi
133         IF( njmpp           ==      1 ) nldj = 1
134         IF( njmpp + jpj - 1 == jpjglo ) nlej = jpj
135      ENDIF
136      IF ( PRESENT(ld_closedef) ) ll_closedef = ld_closedef
137      !
138      ALLOCATE( zt_bnds(2,jpk), zw_bnds(2,jpk) )
139      !
140      clname = cdname
141      IF( TRIM(Agrif_CFixed()) /= '0' )   clname = TRIM(Agrif_CFixed())//"_"//TRIM(cdname)
142      CALL xios_context_initialize(TRIM(clname), mpi_comm_oce)
143      CALL iom_swap( cdname )
144      llrst_context =  (TRIM(cdname) == TRIM(crxios_context) .OR. TRIM(cdname) == TRIM(cwxios_context))
145
146      ! Calendar type is now defined in xml file
147      IF (.NOT.(xios_getvar('ref_year' ,irefyear ))) irefyear  = 1900
148      IF (.NOT.(xios_getvar('ref_month',irefmonth))) irefmonth = 01
149      IF (.NOT.(xios_getvar('ref_day'  ,irefday  ))) irefday   = 01
150
151      SELECT CASE ( nleapy )        ! Choose calendar for IOIPSL
152      CASE ( 1)   ; CALL xios_define_calendar( TYPE = "Gregorian", time_origin = xios_date(irefyear,irefmonth,irefday,00,00,00), &
153          &                                    start_date = xios_date(nyear,nmonth,nday,0,0,0) )
154      CASE ( 0)   ; CALL xios_define_calendar( TYPE = "NoLeap"   , time_origin = xios_date(irefyear,irefmonth,irefday,00,00,00), &
155          &                                    start_date = xios_date(nyear,nmonth,nday,0,0,0) )
156      CASE (30)   ; CALL xios_define_calendar( TYPE = "D360"     , time_origin = xios_date(irefyear,irefmonth,irefday,00,00,00), &
157          &                                    start_date = xios_date(nyear,nmonth,nday,0,0,0) )
158      END SELECT
159
160      ! horizontal grid definition
161      IF(.NOT.llrst_context) CALL set_scalar
162      !
163      IF( TRIM(cdname) == TRIM(cxios_context) ) THEN 
164         CALL set_grid( "T", glamt, gphit, .FALSE., .FALSE. ) 
165         CALL set_grid( "U", glamu, gphiu, .FALSE., .FALSE. )
166         CALL set_grid( "V", glamv, gphiv, .FALSE., .FALSE. )
167         CALL set_grid( "W", glamt, gphit, .FALSE., .FALSE. )
168         CALL set_grid_znl( gphit )
169         !
170         IF( ln_cfmeta ) THEN   ! Add additional grid metadata
171            CALL iom_set_domain_attr("grid_T", area = e1e2t(nldi:nlei, nldj:nlej))
172            CALL iom_set_domain_attr("grid_U", area = e1e2u(nldi:nlei, nldj:nlej))
173            CALL iom_set_domain_attr("grid_V", area = e1e2v(nldi:nlei, nldj:nlej))
174            CALL iom_set_domain_attr("grid_W", area = e1e2t(nldi:nlei, nldj:nlej))
175            CALL set_grid_bounds( "T", glamf, gphif, glamt, gphit )
176            CALL set_grid_bounds( "U", glamv, gphiv, glamu, gphiu )
177            CALL set_grid_bounds( "V", glamu, gphiu, glamv, gphiv )
178            CALL set_grid_bounds( "W", glamf, gphif, glamt, gphit )
179         ENDIF
180      ENDIF
181      !
182      IF( TRIM(cdname) == TRIM(cxios_context)//"_crs" ) THEN 
183         CALL dom_grid_crs   ! Save the parent grid information  & Switch to coarse grid domain
184         !
185         CALL set_grid( "T", glamt_crs, gphit_crs, .FALSE., .FALSE. ) 
186         CALL set_grid( "U", glamu_crs, gphiu_crs, .FALSE., .FALSE. ) 
187         CALL set_grid( "V", glamv_crs, gphiv_crs, .FALSE., .FALSE. ) 
188         CALL set_grid( "W", glamt_crs, gphit_crs, .FALSE., .FALSE. ) 
189         CALL set_grid_znl( gphit_crs )
190          !
191         CALL dom_grid_glo   ! Return to parent grid domain
192         !
193         IF( ln_cfmeta .AND. .NOT. llrst_context) THEN   ! Add additional grid metadata
194            CALL iom_set_domain_attr("grid_T", area = e1e2t_crs(nldi:nlei, nldj:nlej))
195            CALL iom_set_domain_attr("grid_U", area = e1u_crs(nldi:nlei, nldj:nlej) * e2u_crs(nldi:nlei, nldj:nlej))
196            CALL iom_set_domain_attr("grid_V", area = e1v_crs(nldi:nlei, nldj:nlej) * e2v_crs(nldi:nlei, nldj:nlej))
197            CALL iom_set_domain_attr("grid_W", area = e1e2t_crs(nldi:nlei, nldj:nlej))
198            CALL set_grid_bounds( "T", glamf_crs, gphif_crs, glamt_crs, gphit_crs )
199            CALL set_grid_bounds( "U", glamv_crs, gphiv_crs, glamu_crs, gphiu_crs )
200            CALL set_grid_bounds( "V", glamu_crs, gphiu_crs, glamv_crs, gphiv_crs )
201            CALL set_grid_bounds( "W", glamf_crs, gphif_crs, glamt_crs, gphit_crs )
202         ENDIF
203      ENDIF
204      !
205      ! vertical grid definition
206      IF(.NOT.llrst_context) THEN
207          CALL iom_set_axis_attr(  "deptht", paxis = gdept_1d )
208          CALL iom_set_axis_attr(  "depthu", paxis = gdept_1d )
209          CALL iom_set_axis_attr(  "depthv", paxis = gdept_1d )
210          CALL iom_set_axis_attr(  "depthw", paxis = gdepw_1d )
211
212          ! ABL
213          IF( .NOT. ALLOCATED(ght_abl) ) THEN   ! force definition for xml files (xios)
214             ALLOCATE( ght_abl(jpka), ghw_abl(jpka), e3t_abl(jpka), e3w_abl(jpka) )   ! default allocation needed by iom
215             ght_abl(:) = -1._wp   ;   ghw_abl(:) = -1._wp
216             e3t_abl(:) = -1._wp   ;   e3w_abl(:) = -1._wp
217          ENDIF
218          CALL iom_set_axis_attr( "ght_abl", ght_abl(2:jpka) )
219          CALL iom_set_axis_attr( "ghw_abl", ghw_abl(2:jpka) )
220         
221          ! Add vertical grid bounds
222          jkmin = MIN(2,jpk)  ! in case jpk=1 (i.e. sas2D)
223          zt_bnds(2,:        ) = gdept_1d(:)
224          zt_bnds(1,jkmin:jpk) = gdept_1d(1:jpkm1)
225          zt_bnds(1,1        ) = gdept_1d(1) - e3w_1d(1)
226          zw_bnds(1,:        ) = gdepw_1d(:)
227          zw_bnds(2,1:jpkm1  ) = gdepw_1d(jkmin:jpk)
228          zw_bnds(2,jpk:     ) = gdepw_1d(jpk) + e3t_1d(jpk)
229          CALL iom_set_axis_attr(  "deptht", bounds=zw_bnds )
230          CALL iom_set_axis_attr(  "depthu", bounds=zw_bnds )
231          CALL iom_set_axis_attr(  "depthv", bounds=zw_bnds )
232          CALL iom_set_axis_attr(  "depthw", bounds=zt_bnds )
233
234          ! ABL
235          za_bnds(1,:) = ghw_abl(1:jpkam1)
236          za_bnds(2,:) = ghw_abl(2:jpka  )
237          CALL iom_set_axis_attr( "ght_abl", bounds=za_bnds )
238          za_bnds(1,:) = ght_abl(2:jpka  )
239          za_bnds(2,:) = ght_abl(2:jpka  ) + e3w_abl(2:jpka)
240          CALL iom_set_axis_attr( "ghw_abl", bounds=za_bnds )
241
242          CALL iom_set_axis_attr( "nfloat", (/ (REAL(ji,wp), ji=1,jpnfl) /) )
243# if defined key_si3
244          CALL iom_set_axis_attr( "ncatice", (/ (REAL(ji,wp), ji=1,jpl) /) )
245          ! SIMIP diagnostics (4 main arctic straits)
246          CALL iom_set_axis_attr( "nstrait", (/ (REAL(ji,wp), ji=1,4) /) )
247# endif
248#if defined key_top
249          IF( ALLOCATED(profsed) ) CALL iom_set_axis_attr( "profsed", paxis = profsed )
250#endif
251          CALL iom_set_axis_attr( "icbcla", class_num )
252          CALL iom_set_axis_attr( "iax_20C", (/ REAL(20,wp) /) )   ! strange syntaxe and idea...
253          CALL iom_set_axis_attr( "iax_26C", (/ REAL(26,wp) /) )   ! strange syntaxe and idea...
254          CALL iom_set_axis_attr( "iax_28C", (/ REAL(28,wp) /) )   ! strange syntaxe and idea...
255          CALL iom_set_axis_attr( "basin"  , (/ (REAL(ji,wp), ji=1,5) /) )
256      ENDIF
257      !
258      ! automatic definitions of some of the xml attributs
259      IF( TRIM(cdname) == TRIM(crxios_context) ) THEN
260!set names of the fields in restart file IF using XIOS to read data
261          CALL iom_set_rst_context(.TRUE.)
262          CALL iom_set_rst_vars(rst_rfields)
263!set which fields are to be read from restart file
264          CALL iom_set_rstr_active()
265      ELSE IF( TRIM(cdname) == TRIM(cwxios_context) ) THEN
266!set names of the fields in restart file IF using XIOS to write data
267          CALL iom_set_rst_context(.FALSE.)
268          CALL iom_set_rst_vars(rst_wfields)
269!set which fields are to be written to a restart file
270          CALL iom_set_rstw_active(fname)
271      ELSE
272          CALL set_xmlatt
273      ENDIF
274      !
275      ! set time step length
276      dtime%second = rn_Dt
277      CALL xios_set_timestep( dtime )
278      !
279      ! conditional closure of context definition
280      IF ( ll_closedef ) CALL iom_init_closedef
281      !
282      DEALLOCATE( zt_bnds, zw_bnds )
283      !
284      IF ( ll_tmppatch ) THEN
285         nldi = nldi_save   ;   nlei = nlei_save
286         nldj = nldj_save   ;   nlej = nlej_save
287      ENDIF
288#endif
289      !
290   END SUBROUTINE iom_init
291
292   SUBROUTINE iom_init_closedef
293      !!----------------------------------------------------------------------
294      !!            ***  SUBROUTINE iom_init_closedef  ***
295      !!----------------------------------------------------------------------
296      !!
297      !! ** Purpose : Closure of context definition
298      !!
299      !!----------------------------------------------------------------------
300
301#if defined key_iomput
302      CALL xios_close_context_definition()
303      CALL xios_update_calendar( 0 )
304#else
305      IF( .FALSE. )   WRITE(numout,*) 'iom_init_closedef: should not see this'   ! useless statement to avoid compilation warnings
306#endif
307
308   END SUBROUTINE iom_init_closedef
309
310   SUBROUTINE iom_set_rstw_var_active(field)
311      !!---------------------------------------------------------------------
312      !!                   ***  SUBROUTINE  iom_set_rstw_var_active  ***
313      !!
314      !! ** Purpose :  enable variable in restart file when writing with XIOS
315      !!---------------------------------------------------------------------
316   CHARACTER(len = *), INTENT(IN) :: field
317   INTEGER :: i
318   LOGICAL :: llis_set
319   CHARACTER(LEN=256) :: clinfo    ! info character
320
321#if defined key_iomput
322   llis_set = .FALSE.
323
324   DO i = 1, max_rst_fields
325       IF(TRIM(rst_wfields(i)%vname) == field) THEN
326          rst_wfields(i)%active = .TRUE.
327          llis_set = .TRUE.
328          EXIT
329       ENDIF
330   ENDDO
331!Warn if variable is not in defined in rst_wfields
332   IF(.NOT.llis_set) THEN
333      WRITE(ctmp1,*) 'iom_set_rstw_var_active: variable ', field ,' is available for writing but not defined' 
334      CALL ctl_stop( 'iom_set_rstw_var_active:', ctmp1 )
335   ENDIF
336#else
337        clinfo = 'iom_set_rstw_var_active: key_iomput is needed to use XIOS restart read/write functionality'
338        CALL ctl_stop('STOP', TRIM(clinfo))
339#endif
340
341   END SUBROUTINE iom_set_rstw_var_active
342
343   SUBROUTINE iom_set_rstr_active()
344      !!---------------------------------------------------------------------
345      !!                   ***  SUBROUTINE  iom_set_rstr_active  ***
346      !!
347      !! ** Purpose :  define file name in XIOS context for reading restart file,
348      !!               enable variables present in restart file for reading with XIOS
349      !!---------------------------------------------------------------------
350
351!sets enabled = .TRUE. for each field in restart file
352   CHARACTER(len=256) :: rst_file
353
354#if defined key_iomput
355   TYPE(xios_field) :: field_hdl
356   TYPE(xios_file) :: file_hdl
357   TYPE(xios_filegroup) :: filegroup_hdl
358   INTEGER :: i
359   CHARACTER(lc)  ::   clpath
360
361        clpath = TRIM(cn_ocerst_indir)
362        IF( clpath(LEN_TRIM(clpath):) /= '/' ) clpath = TRIM(clpath) // '/'
363        IF( TRIM(Agrif_CFixed()) == '0' ) THEN
364           rst_file = TRIM(clpath)//TRIM(cn_ocerst_in)
365        ELSE
366           rst_file = TRIM(clpath)//'1_'//TRIM(cn_ocerst_in)
367        ENDIF
368!set name of the restart file and enable available fields
369        if(lwp) WRITE(numout,*) 'Setting restart filename (for XIOS) to: ',rst_file
370        CALL xios_get_handle("file_definition", filegroup_hdl )
371        CALL xios_add_child(filegroup_hdl, file_hdl, 'rrestart')
372        CALL xios_set_file_attr( "rrestart", name=trim(rst_file), type="one_file", &
373             par_access="collective", enabled=.TRUE., mode="read",                 &
374             output_freq=xios_timestep)
375!define variables for restart context
376        DO i = 1, max_rst_fields
377         IF( TRIM(rst_rfields(i)%vname) /= "NO_NAME") THEN
378           IF( iom_varid( numror, TRIM(rst_rfields(i)%vname), ldstop = .FALSE. ) > 0 ) THEN
379                CALL xios_add_child(file_hdl, field_hdl, TRIM(rst_rfields(i)%vname))
380                SELECT CASE (TRIM(rst_rfields(i)%grid))
381                 CASE ("grid_N_3D")
382                    CALL xios_set_attr (field_hdl, enabled = .TRUE., name = TRIM(rst_rfields(i)%vname), &
383                        domain_ref="grid_N", axis_ref="nav_lev", operation = "instant")
384                 CASE ("grid_N")
385                    CALL xios_set_attr (field_hdl, enabled = .TRUE., name = TRIM(rst_rfields(i)%vname), &
386                        domain_ref="grid_N", operation = "instant") 
387                CASE ("grid_vector")
388                    CALL xios_set_attr (field_hdl, enabled = .TRUE., name = TRIM(rst_rfields(i)%vname), &
389                         axis_ref="nav_lev", operation = "instant")
390                 CASE ("grid_scalar")
391                    CALL xios_set_attr (field_hdl, enabled = .TRUE., name = TRIM(rst_rfields(i)%vname), &
392                        scalar_ref = "grid_scalar", operation = "instant")
393                END SELECT
394                IF(lwp) WRITE(numout,*) 'XIOS read: ', TRIM(rst_rfields(i)%vname), ' enabled in ', TRIM(rst_file)
395           ENDIF
396         ENDIF
397        END DO
398#endif
399   END SUBROUTINE iom_set_rstr_active
400
401   SUBROUTINE iom_set_rstw_core(cdmdl)
402      !!---------------------------------------------------------------------
403      !!                   ***  SUBROUTINE  iom_set_rstw_core  ***
404      !!
405      !! ** Purpose :  set variables which are always in restart file
406      !!---------------------------------------------------------------------
407   CHARACTER (len=*), INTENT (IN) :: cdmdl ! model OPA or SAS
408   CHARACTER(LEN=256)             :: clinfo    ! info character
409#if defined key_iomput
410   IF(cdmdl == "OPA") THEN
411!from restart.F90
412   CALL iom_set_rstw_var_active("rn_Dt")
413   IF ( .NOT. ln_diurnal_only ) THEN
414        CALL iom_set_rstw_var_active('ub'  )
415        CALL iom_set_rstw_var_active('vb'  )
416        CALL iom_set_rstw_var_active('tb'  )
417        CALL iom_set_rstw_var_active('sb'  )
418        CALL iom_set_rstw_var_active('sshb')
419        !
420        CALL iom_set_rstw_var_active('un'  )
421        CALL iom_set_rstw_var_active('vn'  )
422        CALL iom_set_rstw_var_active('tn'  )
423        CALL iom_set_rstw_var_active('sn'  )
424        CALL iom_set_rstw_var_active('sshn')
425        CALL iom_set_rstw_var_active('rhop')
426      ENDIF
427      IF(ln_diurnal) CALL iom_set_rstw_var_active('Dsst')
428!from trasbc.F90
429         CALL iom_set_rstw_var_active('sbc_hc_b')
430         CALL iom_set_rstw_var_active('sbc_sc_b')
431   ENDIF
432#else
433        clinfo = 'iom_set_rstw_core: key_iomput is needed to use XIOS restart read/write functionality'
434        CALL ctl_stop('STOP', TRIM(clinfo))
435#endif
436   END SUBROUTINE iom_set_rstw_core
437
438   SUBROUTINE iom_set_rst_vars(fields)
439      !!---------------------------------------------------------------------
440      !!                   ***  SUBROUTINE iom_set_rst_vars   ***
441      !!
442      !! ** Purpose :  Fill array fields with the information about all
443      !!               possible variables and corresponding grids definition
444      !!               for reading/writing restart with XIOS
445      !!---------------------------------------------------------------------
446   TYPE(RST_FIELD), INTENT(INOUT) :: fields(max_rst_fields)
447   INTEGER :: i
448
449        i = 0
450        i = i + 1; fields(i)%vname="rn_Dt";            fields(i)%grid="grid_scalar"
451        i = i + 1; fields(i)%vname="un";             fields(i)%grid="grid_N_3D"
452        i = i + 1; fields(i)%vname="ub";             fields(i)%grid="grid_N_3D"
453        i = i + 1; fields(i)%vname="vn";             fields(i)%grid="grid_N_3D"
454        i = i + 1; fields(i)%vname="vb";             fields(i)%grid="grid_N_3D" 
455        i = i + 1; fields(i)%vname="tn";             fields(i)%grid="grid_N_3D"
456        i = i + 1; fields(i)%vname="tb";             fields(i)%grid="grid_N_3D"
457        i = i + 1; fields(i)%vname="sn";             fields(i)%grid="grid_N_3D"
458        i = i + 1; fields(i)%vname="sb";             fields(i)%grid="grid_N_3D"
459        i = i + 1; fields(i)%vname="sshn";           fields(i)%grid="grid_N"
460        i = i + 1; fields(i)%vname="sshb";           fields(i)%grid="grid_N"
461        i = i + 1; fields(i)%vname="rhop";           fields(i)%grid="grid_N_3D"
462        i = i + 1; fields(i)%vname="kt";             fields(i)%grid="grid_scalar"
463        i = i + 1; fields(i)%vname="ndastp";         fields(i)%grid="grid_scalar"
464        i = i + 1; fields(i)%vname="adatrj";         fields(i)%grid="grid_scalar"
465        i = i + 1; fields(i)%vname="utau_b";         fields(i)%grid="grid_N"
466        i = i + 1; fields(i)%vname="vtau_b";         fields(i)%grid="grid_N"
467        i = i + 1; fields(i)%vname="qns_b";          fields(i)%grid="grid_N"
468        i = i + 1; fields(i)%vname="emp_b";          fields(i)%grid="grid_N"
469        i = i + 1; fields(i)%vname="sfx_b";          fields(i)%grid="grid_N"
470        i = i + 1; fields(i)%vname="en" ;            fields(i)%grid="grid_N_3D" 
471        i = i + 1; fields(i)%vname="avt_k";            fields(i)%grid="grid_N_3D"
472        i = i + 1; fields(i)%vname="avm_k";            fields(i)%grid="grid_N_3D"
473        i = i + 1; fields(i)%vname="dissl";          fields(i)%grid="grid_N_3D"
474        i = i + 1; fields(i)%vname="sbc_hc_b";       fields(i)%grid="grid_N"
475        i = i + 1; fields(i)%vname="sbc_sc_b";       fields(i)%grid="grid_N"
476        i = i + 1; fields(i)%vname="qsr_hc_b";       fields(i)%grid="grid_N_3D"
477        i = i + 1; fields(i)%vname="fraqsr_1lev";    fields(i)%grid="grid_N"
478        i = i + 1; fields(i)%vname="greenland_icesheet_mass"
479                                               fields(i)%grid="grid_scalar"
480        i = i + 1; fields(i)%vname="greenland_icesheet_timelapsed"
481                                               fields(i)%grid="grid_scalar"
482        i = i + 1; fields(i)%vname="greenland_icesheet_mass_roc"
483                                               fields(i)%grid="grid_scalar"
484        i = i + 1; fields(i)%vname="antarctica_icesheet_mass"
485                                               fields(i)%grid="grid_scalar"
486        i = i + 1; fields(i)%vname="antarctica_icesheet_timelapsed"
487                                               fields(i)%grid="grid_scalar"
488        i = i + 1; fields(i)%vname="antarctica_icesheet_mass_roc"
489                                               fields(i)%grid="grid_scalar"
490        i = i + 1; fields(i)%vname="frc_v";          fields(i)%grid="grid_scalar"
491        i = i + 1; fields(i)%vname="frc_t";          fields(i)%grid="grid_scalar"
492        i = i + 1; fields(i)%vname="frc_s";          fields(i)%grid="grid_scalar"
493        i = i + 1; fields(i)%vname="frc_wn_t";       fields(i)%grid="grid_scalar"
494        i = i + 1; fields(i)%vname="frc_wn_s";       fields(i)%grid="grid_scalar"
495        i = i + 1; fields(i)%vname="ssh_ini";        fields(i)%grid="grid_N"
496        i = i + 1; fields(i)%vname="e3t_ini";        fields(i)%grid="grid_N_3D"
497        i = i + 1; fields(i)%vname="hc_loc_ini";     fields(i)%grid="grid_N_3D"
498        i = i + 1; fields(i)%vname="sc_loc_ini";     fields(i)%grid="grid_N_3D"
499        i = i + 1; fields(i)%vname="ssh_hc_loc_ini"; fields(i)%grid="grid_N"
500        i = i + 1; fields(i)%vname="ssh_sc_loc_ini"; fields(i)%grid="grid_N"
501        i = i + 1; fields(i)%vname="tilde_e3t_b";    fields(i)%grid="grid_N"
502        i = i + 1; fields(i)%vname="tilde_e3t_n";    fields(i)%grid="grid_N"
503        i = i + 1; fields(i)%vname="hdiv_lf";        fields(i)%grid="grid_N"
504        i = i + 1; fields(i)%vname="ub2_b";          fields(i)%grid="grid_N"
505        i = i + 1; fields(i)%vname="vb2_b";          fields(i)%grid="grid_N"
506        i = i + 1; fields(i)%vname="sshbb_e";        fields(i)%grid="grid_N"
507        i = i + 1; fields(i)%vname="ubb_e";          fields(i)%grid="grid_N"
508        i = i + 1; fields(i)%vname="vbb_e";          fields(i)%grid="grid_N"
509        i = i + 1; fields(i)%vname="sshb_e";         fields(i)%grid="grid_N"
510        i = i + 1; fields(i)%vname="ub_e";           fields(i)%grid="grid_N"
511        i = i + 1; fields(i)%vname="vb_e";           fields(i)%grid="grid_N"
512        i = i + 1; fields(i)%vname="fwf_isf_b";      fields(i)%grid="grid_N"
513        i = i + 1; fields(i)%vname="isf_sc_b";       fields(i)%grid="grid_N"
514        i = i + 1; fields(i)%vname="isf_hc_b";       fields(i)%grid="grid_N"
515        i = i + 1; fields(i)%vname="ssh_ibb";        fields(i)%grid="grid_N"
516        i = i + 1; fields(i)%vname="rnf_b";          fields(i)%grid="grid_N"
517        i = i + 1; fields(i)%vname="rnf_hc_b";       fields(i)%grid="grid_N"
518        i = i + 1; fields(i)%vname="rnf_sc_b";       fields(i)%grid="grid_N"
519        i = i + 1; fields(i)%vname="nn_fsbc";        fields(i)%grid="grid_scalar"
520        i = i + 1; fields(i)%vname="ssu_m";          fields(i)%grid="grid_N"
521        i = i + 1; fields(i)%vname="ssv_m";          fields(i)%grid="grid_N"
522        i = i + 1; fields(i)%vname="sst_m";          fields(i)%grid="grid_N"
523        i = i + 1; fields(i)%vname="sss_m";          fields(i)%grid="grid_N"
524        i = i + 1; fields(i)%vname="ssh_m";          fields(i)%grid="grid_N"
525        i = i + 1; fields(i)%vname="e3t_m";          fields(i)%grid="grid_N"
526        i = i + 1; fields(i)%vname="frq_m";          fields(i)%grid="grid_N"
527        i = i + 1; fields(i)%vname="avmb";           fields(i)%grid="grid_vector"
528        i = i + 1; fields(i)%vname="avtb";           fields(i)%grid="grid_vector"
529        i = i + 1; fields(i)%vname="ub2_i_b";        fields(i)%grid="grid_N"
530        i = i + 1; fields(i)%vname="vb2_i_b";        fields(i)%grid="grid_N"
531        i = i + 1; fields(i)%vname="ntime";          fields(i)%grid="grid_scalar"
532        i = i + 1; fields(i)%vname="Dsst";           fields(i)%grid="grid_scalar"
533        i = i + 1; fields(i)%vname="tmask";          fields(i)%grid="grid_N_3D"
534        i = i + 1; fields(i)%vname="umask";          fields(i)%grid="grid_N_3D"
535        i = i + 1; fields(i)%vname="vmask";          fields(i)%grid="grid_N_3D"
536        i = i + 1; fields(i)%vname="smask";          fields(i)%grid="grid_N_3D"
537        i = i + 1; fields(i)%vname="gdepw_n";        fields(i)%grid="grid_N_3D"
538        i = i + 1; fields(i)%vname="e3t_n";          fields(i)%grid="grid_N_3D"
539        i = i + 1; fields(i)%vname="e3u_n";          fields(i)%grid="grid_N_3D"
540        i = i + 1; fields(i)%vname="e3v_n";          fields(i)%grid="grid_N_3D"
541        i = i + 1; fields(i)%vname="surf_ini";       fields(i)%grid="grid_N"
542        i = i + 1; fields(i)%vname="e3t_b";          fields(i)%grid="grid_N_3D"
543        i = i + 1; fields(i)%vname="hmxl_n";         fields(i)%grid="grid_N_3D"
544        i = i + 1; fields(i)%vname="un_bf";          fields(i)%grid="grid_N"
545        i = i + 1; fields(i)%vname="vn_bf";          fields(i)%grid="grid_N"
546        i = i + 1; fields(i)%vname="hbl";            fields(i)%grid="grid_N"
547        i = i + 1; fields(i)%vname="hbli";           fields(i)%grid="grid_N"
548        i = i + 1; fields(i)%vname="wn";             fields(i)%grid="grid_N_3D"
549        i = i + 1; fields(i)%vname="a_fwb_b";        fields(i)%grid="grid_scalar"
550        i = i + 1; fields(i)%vname="a_fwb";          fields(i)%grid="grid_scalar"
551
552        IF( i-1 > max_rst_fields) THEN
553           WRITE(ctmp1,*) 'E R R O R : iom_set_rst_vars SIZE of RST_FIELD array is too small'
554           CALL ctl_stop( 'iom_set_rst_vars:', ctmp1 )
555        ENDIF
556   END SUBROUTINE iom_set_rst_vars
557
558
559   SUBROUTINE iom_set_rstw_active(cdrst_file)
560      !!---------------------------------------------------------------------
561      !!                   ***  SUBROUTINE iom_set_rstw_active   ***
562      !!
563      !! ** Purpose :  define file name in XIOS context for writing restart
564      !!               enable variables present in restart file for writing
565      !!---------------------------------------------------------------------
566!sets enabled = .TRUE. for each field in restart file
567   CHARACTER(len=*) :: cdrst_file
568#if defined key_iomput
569   TYPE(xios_field) :: field_hdl
570   TYPE(xios_file) :: file_hdl
571   TYPE(xios_filegroup) :: filegroup_hdl
572   INTEGER :: i
573   CHARACTER(lc)  ::   clpath
574
575!set name of the restart file and enable available fields
576        IF(lwp) WRITE(numout,*) 'Setting restart filename (for XIOS write) to: ',cdrst_file
577        CALL xios_get_handle("file_definition", filegroup_hdl )
578        CALL xios_add_child(filegroup_hdl, file_hdl, 'wrestart')
579        IF(nxioso.eq.1) THEN
580           CALL xios_set_file_attr( "wrestart", type="one_file", enabled=.TRUE.,& 
581                                    mode="write", output_freq=xios_timestep) 
582           if(lwp) write(numout,*) 'OPEN ', trim(cdrst_file), ' in one_file mode' 
583        ELSE 
584           CALL xios_set_file_attr( "wrestart", type="multiple_file", enabled=.TRUE.,& 
585                                    mode="write", output_freq=xios_timestep) 
586           if(lwp) write(numout,*) 'OPEN ', trim(cdrst_file), ' in multiple_file mode' 
587        ENDIF
588        CALL xios_set_file_attr( "wrestart", name=trim(cdrst_file))
589!define fields for restart context
590        DO i = 1, max_rst_fields
591         IF( rst_wfields(i)%active ) THEN
592                CALL xios_add_child(file_hdl, field_hdl, TRIM(rst_wfields(i)%vname))
593                SELECT CASE (TRIM(rst_wfields(i)%grid))
594                 CASE ("grid_N_3D")
595                    CALL xios_set_attr (field_hdl, enabled = .TRUE., name = TRIM(rst_wfields(i)%vname), &
596                        domain_ref="grid_N", axis_ref="nav_lev", prec = 8, operation = "instant")
597                 CASE ("grid_N")
598                    CALL xios_set_attr (field_hdl, enabled = .TRUE., name = TRIM(rst_wfields(i)%vname), &
599                        domain_ref="grid_N", prec = 8, operation = "instant") 
600                 CASE ("grid_vector")
601                    CALL xios_set_attr (field_hdl, enabled = .TRUE., name = TRIM(rst_wfields(i)%vname), &
602                         axis_ref="nav_lev", prec = 8, operation = "instant")
603                 CASE ("grid_scalar")
604                    CALL xios_set_attr (field_hdl, enabled = .TRUE., name = TRIM(rst_wfields(i)%vname), &
605                        scalar_ref = "grid_scalar", prec = 8, operation = "instant")
606                END SELECT
607         ENDIF
608        END DO
609#endif
610   END SUBROUTINE iom_set_rstw_active
611
612   SUBROUTINE iom_set_rst_context(ld_rstr) 
613     !!---------------------------------------------------------------------
614      !!                   ***  SUBROUTINE  iom_set_rst_context  ***
615      !!
616      !! ** Purpose : Define domain, axis and grid for restart (read/write)
617      !!              context
618      !!               
619      !!---------------------------------------------------------------------
620   LOGICAL, INTENT(IN)               :: ld_rstr
621!ld_rstr is true for restart context. There is no need to define grid for
622!restart read, because it's read from file
623#if defined key_iomput
624   TYPE(xios_domaingroup)            :: domaingroup_hdl 
625   TYPE(xios_domain)                 :: domain_hdl 
626   TYPE(xios_axisgroup)              :: axisgroup_hdl 
627   TYPE(xios_axis)                   :: axis_hdl 
628   TYPE(xios_scalar)                 :: scalar_hdl 
629   TYPE(xios_scalargroup)            :: scalargroup_hdl 
630
631     CALL xios_get_handle("domain_definition",domaingroup_hdl) 
632     CALL xios_add_child(domaingroup_hdl, domain_hdl, "grid_N") 
633     CALL set_grid("N", glamt, gphit, .TRUE., ld_rstr) 
634 
635     CALL xios_get_handle("axis_definition",axisgroup_hdl) 
636     CALL xios_add_child(axisgroup_hdl, axis_hdl, "nav_lev") 
637!AGRIF fails to compile when unit= is in call to xios_set_axis_attr
638!    CALL xios_set_axis_attr( "nav_lev", long_name="Vertical levels",  unit="m", positive="down")
639     CALL xios_set_axis_attr( "nav_lev", long_name="Vertical levels in meters", positive="down")
640     CALL iom_set_axis_attr( "nav_lev", paxis = gdept_1d ) 
641
642     CALL xios_get_handle("scalar_definition", scalargroup_hdl) 
643     CALL xios_add_child(scalargroup_hdl, scalar_hdl, "grid_scalar") 
644#endif
645   END SUBROUTINE iom_set_rst_context
646
647   SUBROUTINE iom_swap( cdname )
648      !!---------------------------------------------------------------------
649      !!                   ***  SUBROUTINE  iom_swap  ***
650      !!
651      !! ** Purpose :  swap context between different agrif grid for xmlio_server
652      !!---------------------------------------------------------------------
653      CHARACTER(len=*), INTENT(in) :: cdname
654#if defined key_iomput
655      TYPE(xios_context) :: nemo_hdl
656
657      IF( TRIM(Agrif_CFixed()) == '0' ) THEN
658        CALL xios_get_handle(TRIM(cdname),nemo_hdl)
659      ELSE
660        CALL xios_get_handle(TRIM(Agrif_CFixed())//"_"//TRIM(cdname),nemo_hdl)
661      ENDIF
662      !
663      CALL xios_set_current_context(nemo_hdl)
664#endif
665      !
666   END SUBROUTINE iom_swap
667
668
669   SUBROUTINE iom_open( cdname, kiomid, ldwrt, kdom, ldstop, ldiof, kdlev )
670      !!---------------------------------------------------------------------
671      !!                   ***  SUBROUTINE  iom_open  ***
672      !!
673      !! ** Purpose :  open an input file (return 0 if not found)
674      !!---------------------------------------------------------------------
675      CHARACTER(len=*), INTENT(in   )           ::   cdname   ! File name
676      INTEGER         , INTENT(  out)           ::   kiomid   ! iom identifier of the opened file
677      LOGICAL         , INTENT(in   ), OPTIONAL ::   ldwrt    ! open in write modeb          (default = .FALSE.)
678      INTEGER         , INTENT(in   ), OPTIONAL ::   kdom     ! Type of domain to be written (default = jpdom_local_noovlap)
679      LOGICAL         , INTENT(in   ), OPTIONAL ::   ldstop   ! stop if open to read a non-existing file (default = .TRUE.)
680      LOGICAL         , INTENT(in   ), OPTIONAL ::   ldiof    ! Interp On the Fly, needed for AGRIF (default = .FALSE.)
681      INTEGER         , INTENT(in   ), OPTIONAL ::   kdlev    ! number of vertical levels
682      !
683      CHARACTER(LEN=256)    ::   clname    ! the name of the file based on cdname [[+clcpu]+clcpu]
684      CHARACTER(LEN=256)    ::   cltmpn    ! tempory name to store clname (in writting mode)
685      CHARACTER(LEN=10)     ::   clsuffix  ! ".nc"
686      CHARACTER(LEN=15)     ::   clcpu     ! the cpu number (max jpmax_digits digits)
687      CHARACTER(LEN=256)    ::   clinfo    ! info character
688      LOGICAL               ::   llok      ! check the existence
689      LOGICAL               ::   llwrt     ! local definition of ldwrt
690      LOGICAL               ::   llnoov    ! local definition to read overlap
691      LOGICAL               ::   llstop    ! local definition of ldstop
692      LOGICAL               ::   lliof     ! local definition of ldiof
693      INTEGER               ::   icnt      ! counter for digits in clcpu (max = jpmax_digits)
694      INTEGER               ::   iln, ils  ! lengths of character
695      INTEGER               ::   idom      ! type of domain
696      INTEGER               ::   istop     !
697      INTEGER, DIMENSION(2,5) ::   idompar ! domain parameters:
698      ! local number of points for x,y dimensions
699      ! position of first local point for x,y dimensions
700      ! position of last local point for x,y dimensions
701      ! start halo size for x,y dimensions
702      ! end halo size for x,y dimensions
703      !---------------------------------------------------------------------
704      ! Initializations and control
705      ! =============
706      kiomid = -1
707      clinfo = '                    iom_open ~~~  '
708      istop = nstop
709      ! if iom_open is called for the first time: initialize iom_file(:)%nfid to 0
710      ! (could be done when defining iom_file in f95 but not in f90)
711      IF( Agrif_Root() ) THEN
712         IF( iom_open_init == 0 ) THEN
713            iom_file(:)%nfid = 0
714            iom_open_init = 1
715         ENDIF
716      ENDIF
717      ! do we read or write the file?
718      IF( PRESENT(ldwrt) ) THEN   ;   llwrt = ldwrt
719      ELSE                        ;   llwrt = .FALSE.
720      ENDIF
721      ! do we call ctl_stop if we try to open a non-existing file in read mode?
722      IF( PRESENT(ldstop) ) THEN   ;   llstop = ldstop
723      ELSE                         ;   llstop = .TRUE.
724      ENDIF
725      ! are we using interpolation on the fly?
726      IF( PRESENT(ldiof) ) THEN   ;   lliof = ldiof
727      ELSE                        ;   lliof = .FALSE.
728      ENDIF
729      ! do we read the overlap
730      ! ugly patch SM+JMM+RB to overwrite global definition in some cases
731      llnoov = (jpni * jpnj ) == jpnij .AND. .NOT. lk_agrif
732      ! create the file name by added, if needed, TRIM(Agrif_CFixed()) and TRIM(clsuffix)
733      ! =============
734      clname   = trim(cdname)
735      IF ( .NOT. Agrif_Root() .AND. .NOT. lliof ) THEN
736         iln    = INDEX(clname,'/') 
737         cltmpn = clname(1:iln)
738         clname = clname(iln+1:LEN_TRIM(clname))
739         clname=TRIM(cltmpn)//TRIM(Agrif_CFixed())//'_'//TRIM(clname)
740      ENDIF
741      ! which suffix should we use?
742      clsuffix = '.nc'
743      ! Add the suffix if needed
744      iln = LEN_TRIM(clname)
745      ils = LEN_TRIM(clsuffix)
746      IF( iln <= ils .OR. INDEX( TRIM(clname), TRIM(clsuffix), back = .TRUE. ) /= iln - ils + 1 )   &
747         &   clname = TRIM(clname)//TRIM(clsuffix)
748      cltmpn = clname   ! store this name
749      ! try to find if the file to be opened already exist
750      ! =============
751      INQUIRE( FILE = clname, EXIST = llok )
752      IF( .NOT.llok ) THEN
753         ! we try to add the cpu number to the name
754         WRITE(clcpu,*) narea-1
755
756         clcpu  = TRIM(ADJUSTL(clcpu))
757         iln = INDEX(clname,TRIM(clsuffix), back = .TRUE.)
758         clname = clname(1:iln-1)//'_'//TRIM(clcpu)//TRIM(clsuffix)
759         icnt = 0
760         INQUIRE( FILE = clname, EXIST = llok ) 
761         ! we try different formats for the cpu number by adding 0
762         DO WHILE( .NOT.llok .AND. icnt < jpmax_digits )
763            clcpu  = "0"//trim(clcpu)
764            clname = clname(1:iln-1)//'_'//TRIM(clcpu)//TRIM(clsuffix)
765            INQUIRE( FILE = clname, EXIST = llok )
766            icnt = icnt + 1
767         END DO
768      ELSE
769         lxios_sini = .TRUE.
770      ENDIF
771      IF( llwrt ) THEN
772         ! check the domain definition
773! JMM + SM: ugly patch before getting the new version of lib_mpp)
774!         idom = jpdom_local_noovlap   ! default definition
775         IF( llnoov ) THEN   ;   idom = jpdom_local_noovlap   ! default definition
776         ELSE                ;   idom = jpdom_local_full      ! default definition
777         ENDIF
778         IF( PRESENT(kdom) )   idom = kdom
779         ! create the domain informations
780         ! =============
781         SELECT CASE (idom)
782         CASE (jpdom_local_full)
783            idompar(:,1) = (/ jpi             , jpj              /)
784            idompar(:,2) = (/ nimpp           , njmpp            /)
785            idompar(:,3) = (/ nimpp + jpi - 1 , njmpp + jpj - 1  /)
786            idompar(:,4) = (/ nldi - 1        , nldj - 1         /)
787            idompar(:,5) = (/ jpi - nlei      , jpj - nlej       /)
788         CASE (jpdom_local_noextra)
789            idompar(:,1) = (/ nlci            , nlcj             /)
790            idompar(:,2) = (/ nimpp           , njmpp            /)
791            idompar(:,3) = (/ nimpp + nlci - 1, njmpp + nlcj - 1 /)
792            idompar(:,4) = (/ nldi - 1        , nldj - 1         /)
793            idompar(:,5) = (/ nlci - nlei     , nlcj - nlej      /)
794         CASE (jpdom_local_noovlap)
795            idompar(:,1) = (/ nlei  - nldi + 1, nlej  - nldj + 1 /)
796            idompar(:,2) = (/ nimpp + nldi - 1, njmpp + nldj - 1 /)
797            idompar(:,3) = (/ nimpp + nlei - 1, njmpp + nlej - 1 /)
798            idompar(:,4) = (/ 0               , 0                /)
799            idompar(:,5) = (/ 0               , 0                /)
800         CASE DEFAULT
801            CALL ctl_stop( TRIM(clinfo), 'wrong value of kdom, only jpdom_local* cases are accepted' )
802         END SELECT
803      ENDIF
804      ! Open the NetCDF file
805      ! =============
806      ! do we have some free file identifier?
807      IF( MINVAL(iom_file(:)%nfid) /= 0 )   &
808         &   CALL ctl_stop( TRIM(clinfo), 'No more free file identifier', 'increase jpmax_files in iom_def' )
809      ! if no file was found...
810      IF( .NOT. llok ) THEN
811         IF( .NOT. llwrt ) THEN   ! we are in read mode
812            IF( llstop ) THEN   ;   CALL ctl_stop( TRIM(clinfo), 'File '//TRIM(cltmpn)//'* not found' )
813            ELSE                ;   istop = nstop + 1   ! make sure that istop /= nstop so we don't open the file
814            ENDIF
815         ELSE                     ! we are in write mode so we
816            clname = cltmpn       ! get back the file name without the cpu number
817         ENDIF
818      ELSE
819         IF( llwrt .AND. .NOT. ln_clobber ) THEN   ! we stop as we want to write in a new file
820            CALL ctl_stop( TRIM(clinfo), 'We want to write in a new file but '//TRIM(clname)//' already exists...' )
821            istop = nstop + 1                      ! make sure that istop /= nstop so we don't open the file
822         ELSEIF( llwrt ) THEN     ! the file exists and we are in write mode with permission to
823            clname = cltmpn       ! overwrite so get back the file name without the cpu number
824         ENDIF
825      ENDIF
826      IF( istop == nstop ) THEN   ! no error within this routine
827         CALL iom_nf90_open( clname, kiomid, llwrt, llok, idompar, kdlev = kdlev )
828      ENDIF
829      !
830   END SUBROUTINE iom_open
831
832
833   SUBROUTINE iom_close( kiomid )
834      !!--------------------------------------------------------------------
835      !!                   ***  SUBROUTINE  iom_close  ***
836      !!
837      !! ** Purpose : close an input file, or all files opened by iom
838      !!--------------------------------------------------------------------
839      INTEGER, INTENT(inout), OPTIONAL ::   kiomid   ! iom identifier of the file to be closed
840      !                                              ! return 0 when file is properly closed
841      !                                              ! No argument: all files opened by iom are closed
842
843      INTEGER ::   jf         ! dummy loop indices
844      INTEGER ::   i_s, i_e   ! temporary integer
845      CHARACTER(LEN=100)    ::   clinfo    ! info character
846      !---------------------------------------------------------------------
847      !
848      IF( iom_open_init == 0 )   RETURN   ! avoid to use iom_file(jf)%nfid that us not yet initialized
849      !
850      clinfo = '                    iom_close ~~~  '
851      IF( PRESENT(kiomid) ) THEN
852         i_s = kiomid
853         i_e = kiomid
854      ELSE
855         i_s = 1
856         i_e = jpmax_files
857      ENDIF
858
859      IF( i_s > 0 ) THEN
860         DO jf = i_s, i_e
861            IF( iom_file(jf)%nfid > 0 ) THEN
862               CALL iom_nf90_close( jf )
863               iom_file(jf)%nfid       = 0          ! free the id
864               IF( PRESENT(kiomid) )   kiomid = 0   ! return 0 as id to specify that the file was closed
865               IF(lwp) WRITE(numout,*) TRIM(clinfo)//' close file: '//TRIM(iom_file(jf)%name)//' ok'
866            ELSEIF( PRESENT(kiomid) ) THEN
867               WRITE(ctmp1,*) '--->',  kiomid
868               CALL ctl_stop( TRIM(clinfo)//' Invalid file identifier', ctmp1 )
869            ENDIF
870         END DO
871      ENDIF
872      !   
873   END SUBROUTINE iom_close
874
875
876   FUNCTION iom_varid ( kiomid, cdvar, kdimsz, kndims, lduld, ldstop ) 
877      !!-----------------------------------------------------------------------
878      !!                  ***  FUNCTION  iom_varid  ***
879      !!
880      !! ** Purpose : get the id of a variable in a file (return 0 if not found)
881      !!-----------------------------------------------------------------------
882      INTEGER              , INTENT(in   )           ::   kiomid   ! file Identifier
883      CHARACTER(len=*)     , INTENT(in   )           ::   cdvar    ! name of the variable
884      INTEGER, DIMENSION(:), INTENT(  out), OPTIONAL ::   kdimsz   ! size of each dimension
885      INTEGER              , INTENT(  out), OPTIONAL ::   kndims   ! number of dimensions
886      LOGICAL              , INTENT(  out), OPTIONAL ::   lduld    ! true if the last dimension is unlimited (time)
887      LOGICAL              , INTENT(in   ), OPTIONAL ::   ldstop   ! stop if looking for non-existing variable (default = .TRUE.)
888      !
889      INTEGER                        ::   iom_varid, iiv, i_nvd
890      LOGICAL                        ::   ll_fnd
891      CHARACTER(LEN=100)             ::   clinfo                   ! info character
892      LOGICAL                        ::   llstop                   ! local definition of ldstop
893      !!-----------------------------------------------------------------------
894      iom_varid = 0                         ! default definition
895      ! do we call ctl_stop if we look for non-existing variable?
896      IF( PRESENT(ldstop) ) THEN   ;   llstop = ldstop
897      ELSE                         ;   llstop = .TRUE.
898      ENDIF
899      !
900      IF( kiomid > 0 ) THEN
901         clinfo = 'iom_varid, file: '//trim(iom_file(kiomid)%name)//', var: '//trim(cdvar)
902         IF( iom_file(kiomid)%nfid == 0 ) THEN
903            CALL ctl_stop( trim(clinfo), 'the file is not open' )
904         ELSE
905            ll_fnd  = .FALSE.
906            iiv = 0
907            !
908            DO WHILE ( .NOT.ll_fnd .AND. iiv < iom_file(kiomid)%nvars )
909               iiv = iiv + 1
910               ll_fnd  = ( TRIM(cdvar) == TRIM(iom_file(kiomid)%cn_var(iiv)) )
911            END DO
912            !
913            IF( .NOT.ll_fnd ) THEN
914               iiv = iiv + 1
915               IF( iiv <= jpmax_vars ) THEN
916                  iom_varid = iom_nf90_varid( kiomid, cdvar, iiv, kdimsz, kndims, lduld )
917               ELSE
918                  CALL ctl_stop( trim(clinfo), 'Too many variables in the file '//iom_file(kiomid)%name,   &
919                        &                      'increase the parameter jpmax_vars')
920               ENDIF
921               IF( llstop .AND. iom_varid == -1 )   CALL ctl_stop( TRIM(clinfo)//' not found' ) 
922            ELSE
923               iom_varid = iiv
924               IF( PRESENT(kdimsz) ) THEN
925                  i_nvd = iom_file(kiomid)%ndims(iiv)
926                  IF( i_nvd <= size(kdimsz) ) THEN
927                     kdimsz(1:i_nvd) = iom_file(kiomid)%dimsz(1:i_nvd,iiv)
928                  ELSE
929                     WRITE(ctmp1,*) i_nvd, size(kdimsz)
930                     CALL ctl_stop( trim(clinfo), 'error in kdimsz size'//trim(ctmp1) )
931                  ENDIF
932               ENDIF
933               IF( PRESENT(kndims) )  kndims = iom_file(kiomid)%ndims(iiv)
934               IF( PRESENT( lduld) )  lduld  = iom_file(kiomid)%luld( iiv)
935            ENDIF
936         ENDIF
937      ENDIF
938      !
939   END FUNCTION iom_varid
940
941
942   !!----------------------------------------------------------------------
943   !!                   INTERFACE iom_get
944   !!----------------------------------------------------------------------
945   SUBROUTINE iom_g0d( kiomid, cdvar, pvar, ktime, ldxios )
946      INTEGER         , INTENT(in   )                 ::   kiomid    ! Identifier of the file
947      CHARACTER(len=*), INTENT(in   )                 ::   cdvar     ! Name of the variable
948      REAL(wp)        , INTENT(  out)                 ::   pvar      ! read field
949      INTEGER         , INTENT(in   ),     OPTIONAL   ::   ktime     ! record number
950      LOGICAL         , INTENT(in   ),     OPTIONAL   ::   ldxios    ! use xios to read restart
951      !
952      INTEGER                                         ::   idvar     ! variable id
953      INTEGER                                         ::   idmspc    ! number of spatial dimensions
954      INTEGER         , DIMENSION(1)                  ::   itime     ! record number
955      CHARACTER(LEN=100)                              ::   clinfo    ! info character
956      CHARACTER(LEN=100)                              ::   clname    ! file name
957      CHARACTER(LEN=1)                                ::   cldmspc   !
958      LOGICAL                                         ::   llxios
959      !
960      llxios = .FALSE.
961      IF( PRESENT(ldxios) ) llxios = ldxios
962
963      IF(.NOT.llxios) THEN  ! read data using default library
964         itime = 1
965         IF( PRESENT(ktime) ) itime = ktime
966         !
967         clname = iom_file(kiomid)%name
968         clinfo = '          iom_g0d, file: '//trim(clname)//', var: '//trim(cdvar)
969         !
970         IF( kiomid > 0 ) THEN
971            idvar = iom_varid( kiomid, cdvar )
972            IF( iom_file(kiomid)%nfid > 0 .AND. idvar > 0 ) THEN
973               idmspc = iom_file ( kiomid )%ndims( idvar )
974               IF( iom_file(kiomid)%luld(idvar) )  idmspc = idmspc - 1
975               WRITE(cldmspc , fmt='(i1)') idmspc
976               IF( idmspc > 0 )  CALL ctl_stop( TRIM(clinfo), 'When reading to a 0D array, we do not accept data', &
977                                    &                         'with 1 or more spatial dimensions: '//cldmspc//' were found.' , &
978                                    &                         'Use ncwa -a to suppress the unnecessary dimensions' )
979               CALL iom_nf90_get( kiomid, idvar, pvar, itime )
980            ENDIF
981         ENDIF
982      ELSE
983#if defined key_iomput
984         IF(lwp) WRITE(numout,*) 'XIOS RST READ (0D): ', trim(cdvar)
985         CALL iom_swap( TRIM(crxios_context) )
986         CALL xios_recv_field( trim(cdvar), pvar)
987         CALL iom_swap( TRIM(cxios_context) )
988#else
989         WRITE(ctmp1,*) 'Can not use XIOS in iom_g0d, file: '//trim(clname)//', var:'//trim(cdvar)
990         CALL ctl_stop( 'iom_g0d', ctmp1 )
991#endif
992      ENDIF
993   END SUBROUTINE iom_g0d
994
995   SUBROUTINE iom_g1d( kiomid, kdom, cdvar, pvar, ktime, kstart, kcount, ldxios )
996      INTEGER         , INTENT(in   )                         ::   kiomid    ! Identifier of the file
997      INTEGER         , INTENT(in   )                         ::   kdom      ! Type of domain to be read
998      CHARACTER(len=*), INTENT(in   )                         ::   cdvar     ! Name of the variable
999      REAL(wp)        , INTENT(  out), DIMENSION(:)           ::   pvar      ! read field
1000      INTEGER         , INTENT(in   )              , OPTIONAL ::   ktime     ! record number
1001      INTEGER         , INTENT(in   ), DIMENSION(1), OPTIONAL ::   kstart    ! start axis position of the reading
1002      INTEGER         , INTENT(in   ), DIMENSION(1), OPTIONAL ::   kcount    ! number of points in each axis
1003      LOGICAL         , INTENT(in   ),               OPTIONAL ::   ldxios    ! read data using XIOS
1004      !
1005      IF( kiomid > 0 ) THEN
1006         IF( iom_file(kiomid)%nfid > 0 ) CALL iom_get_123d( kiomid, kdom       , cdvar        , pv_r1d=pvar,   &
1007              &                                                     ktime=ktime, kstart=kstart, kcount=kcount, &
1008              &                                                     ldxios=ldxios )
1009      ENDIF
1010   END SUBROUTINE iom_g1d
1011
1012   SUBROUTINE iom_g2d( kiomid, kdom, cdvar, pvar, ktime, kstart, kcount, lrowattr, ldxios)
1013      INTEGER         , INTENT(in   )                           ::   kiomid    ! Identifier of the file
1014      INTEGER         , INTENT(in   )                           ::   kdom      ! Type of domain to be read
1015      CHARACTER(len=*), INTENT(in   )                           ::   cdvar     ! Name of the variable
1016      REAL(wp)        , INTENT(  out), DIMENSION(:,:)           ::   pvar      ! read field
1017      INTEGER         , INTENT(in   )                , OPTIONAL ::   ktime     ! record number
1018      INTEGER         , INTENT(in   ), DIMENSION(2)  , OPTIONAL ::   kstart    ! start axis position of the reading
1019      INTEGER         , INTENT(in   ), DIMENSION(2)  , OPTIONAL ::   kcount    ! number of points in each axis
1020      LOGICAL         , INTENT(in   )                , OPTIONAL ::   lrowattr  ! logical flag telling iom_get to
1021                                                                               ! look for and use a file attribute
1022                                                                               ! called open_ocean_jstart to set the start
1023                                                                               ! value for the 2nd dimension (netcdf only)
1024      LOGICAL         , INTENT(in   ),               OPTIONAL ::   ldxios      ! read data using XIOS
1025      !
1026      IF( kiomid > 0 ) THEN
1027         IF( iom_file(kiomid)%nfid > 0 ) CALL iom_get_123d( kiomid, kdom       , cdvar        , pv_r2d=pvar,   &
1028              &                                                     ktime=ktime, kstart=kstart, kcount=kcount, &
1029              &                                                     lrowattr=lrowattr,  ldxios=ldxios)
1030      ENDIF
1031   END SUBROUTINE iom_g2d
1032
1033   SUBROUTINE iom_g3d( kiomid, kdom, cdvar, pvar, ktime, kstart, kcount, lrowattr, ldxios )
1034      INTEGER         , INTENT(in   )                             ::   kiomid    ! Identifier of the file
1035      INTEGER         , INTENT(in   )                             ::   kdom      ! Type of domain to be read
1036      CHARACTER(len=*), INTENT(in   )                             ::   cdvar     ! Name of the variable
1037      REAL(wp)        , INTENT(  out), DIMENSION(:,:,:)           ::   pvar      ! read field
1038      INTEGER         , INTENT(in   )                  , OPTIONAL ::   ktime     ! record number
1039      INTEGER         , INTENT(in   ), DIMENSION(3)    , OPTIONAL ::   kstart    ! start axis position of the reading
1040      INTEGER         , INTENT(in   ), DIMENSION(3)    , OPTIONAL ::   kcount    ! number of points in each axis
1041      LOGICAL         , INTENT(in   )                  , OPTIONAL ::   lrowattr  ! logical flag telling iom_get to
1042                                                                                 ! look for and use a file attribute
1043                                                                                 ! called open_ocean_jstart to set the start
1044                                                                                 ! value for the 2nd dimension (netcdf only)
1045      LOGICAL         , INTENT(in   ),               OPTIONAL ::   ldxios        ! read data using XIOS
1046      !
1047      IF( kiomid > 0 ) THEN
1048         IF( iom_file(kiomid)%nfid > 0 ) CALL iom_get_123d( kiomid, kdom       , cdvar        , pv_r3d=pvar,   &
1049              &                                                     ktime=ktime, kstart=kstart, kcount=kcount, &
1050              &                                                     lrowattr=lrowattr, ldxios=ldxios )
1051      ENDIF
1052   END SUBROUTINE iom_g3d
1053   !!----------------------------------------------------------------------
1054
1055   SUBROUTINE iom_get_123d( kiomid, kdom  , cdvar ,   &
1056         &                  pv_r1d, pv_r2d, pv_r3d,   &
1057         &                  ktime , kstart, kcount,   &
1058         &                  lrowattr, ldxios        )
1059      !!-----------------------------------------------------------------------
1060      !!                  ***  ROUTINE  iom_get_123d  ***
1061      !!
1062      !! ** Purpose : read a 1D/2D/3D variable
1063      !!
1064      !! ** Method : read ONE record at each CALL
1065      !!-----------------------------------------------------------------------
1066      INTEGER                    , INTENT(in   )           ::   kiomid     ! Identifier of the file
1067      INTEGER                    , INTENT(in   )           ::   kdom       ! Type of domain to be read
1068      CHARACTER(len=*)           , INTENT(in   )           ::   cdvar      ! Name of the variable
1069      REAL(wp), DIMENSION(:)     , INTENT(  out), OPTIONAL ::   pv_r1d     ! read field (1D case)
1070      REAL(wp), DIMENSION(:,:)   , INTENT(  out), OPTIONAL ::   pv_r2d     ! read field (2D case)
1071      REAL(wp), DIMENSION(:,:,:) , INTENT(  out), OPTIONAL ::   pv_r3d     ! read field (3D case)
1072      INTEGER                    , INTENT(in   ), OPTIONAL ::   ktime      ! record number
1073      INTEGER , DIMENSION(:)     , INTENT(in   ), OPTIONAL ::   kstart     ! start position of the reading in each axis
1074      INTEGER , DIMENSION(:)     , INTENT(in   ), OPTIONAL ::   kcount     ! number of points to be read in each axis
1075      LOGICAL                    , INTENT(in   ), OPTIONAL ::   lrowattr   ! logical flag telling iom_get to
1076                                                                           ! look for and use a file attribute
1077                                                                           ! called open_ocean_jstart to set the start
1078                                                                           ! value for the 2nd dimension (netcdf only)
1079      LOGICAL                    , INTENT(in   ), OPTIONAL ::   ldxios     ! use XIOS to read restart
1080      !
1081      LOGICAL                        ::   llxios       ! local definition for XIOS read
1082      LOGICAL                        ::   llnoov      ! local definition to read overlap
1083      LOGICAL                        ::   luse_jattr  ! local definition to read open_ocean_jstart file attribute
1084      INTEGER                        ::   jstartrow   ! start point for 2nd dimension optionally set by file attribute
1085      INTEGER                        ::   jl          ! loop on number of dimension
1086      INTEGER                        ::   idom        ! type of domain
1087      INTEGER                        ::   idvar       ! id of the variable
1088      INTEGER                        ::   inbdim      ! number of dimensions of the variable
1089      INTEGER                        ::   idmspc      ! number of spatial dimensions
1090      INTEGER                        ::   itime       ! record number
1091      INTEGER                        ::   istop       ! temporary value of nstop
1092      INTEGER                        ::   ix1, ix2, iy1, iy2   ! subdomain indexes
1093      INTEGER                        ::   ji, jj      ! loop counters
1094      INTEGER                        ::   irankpv     !
1095      INTEGER                        ::   ind1, ind2  ! substring index
1096      INTEGER, DIMENSION(jpmax_dims) ::   istart      ! starting point to read for each axis
1097      INTEGER, DIMENSION(jpmax_dims) ::   icnt        ! number of value to read along each axis
1098      INTEGER, DIMENSION(jpmax_dims) ::   idimsz      ! size of the dimensions of the variable
1099      INTEGER, DIMENSION(jpmax_dims) ::   ishape      ! size of the dimensions of the variable
1100      REAL(wp)                       ::   zscf, zofs  ! sacle_factor and add_offset
1101      INTEGER                        ::   itmp        ! temporary integer
1102      CHARACTER(LEN=256)             ::   clinfo      ! info character
1103      CHARACTER(LEN=256)             ::   clname      ! file name
1104      CHARACTER(LEN=1)               ::   clrankpv, cldmspc      !
1105      LOGICAL                        ::   ll_depth_spec ! T => if kstart, kcount present then *only* use values for 3rd spatial dimension.
1106      INTEGER                        ::   inlev       ! number of levels for 3D data
1107      REAL(wp)                       ::   gma, gmi
1108      !---------------------------------------------------------------------
1109      !
1110      inlev = -1
1111      IF( PRESENT(pv_r3d) )   inlev = SIZE(pv_r3d, 3)
1112      !
1113      llxios = .FALSE.
1114      if(PRESENT(ldxios)) llxios = ldxios
1115      idvar = iom_varid( kiomid, cdvar ) 
1116      idom = kdom
1117      !
1118      IF(.NOT.llxios) THEN
1119         clname = iom_file(kiomid)%name   !   esier to read
1120         clinfo = '          iom_get_123d, file: '//trim(clname)//', var: '//trim(cdvar)
1121         ! local definition of the domain ?
1122         ! do we read the overlap
1123         ! ugly patch SM+JMM+RB to overwrite global definition in some cases
1124         llnoov = (jpni * jpnj ) == jpnij .AND. .NOT. lk_agrif 
1125         ! check kcount and kstart optionals parameters...
1126         IF( PRESENT(kcount) .AND. (.NOT. PRESENT(kstart)) ) CALL ctl_stop(trim(clinfo), 'kcount present needs kstart present')
1127         IF( PRESENT(kstart) .AND. (.NOT. PRESENT(kcount)) ) CALL ctl_stop(trim(clinfo), 'kstart present needs kcount present')
1128         IF( PRESENT(kstart) .AND. idom /= jpdom_unknown .AND.  idom /= jpdom_autoglo_xy  ) &
1129     &          CALL ctl_stop(trim(clinfo), 'kstart present needs kdom = jpdom_unknown or kdom = jpdom_autoglo_xy')
1130
1131         luse_jattr = .false.
1132         IF( PRESENT(lrowattr) ) THEN
1133            IF( lrowattr .AND. idom /= jpdom_data   ) CALL ctl_stop(trim(clinfo), 'lrowattr present and true needs kdom = jpdom_data')
1134            IF( lrowattr .AND. idom == jpdom_data   ) luse_jattr = .true.
1135         ENDIF
1136
1137         ! Search for the variable in the data base (eventually actualize data)
1138         istop = nstop
1139         !
1140         IF( idvar > 0 ) THEN
1141            ! to write iom_file(kiomid)%dimsz in a shorter way !
1142            idimsz(:) = iom_file(kiomid)%dimsz(:, idvar) 
1143            inbdim = iom_file(kiomid)%ndims(idvar)            ! number of dimensions in the file
1144            idmspc = inbdim                                   ! number of spatial dimensions in the file
1145            IF( iom_file(kiomid)%luld(idvar) )   idmspc = inbdim - 1
1146            IF( idmspc > 3 )   CALL ctl_stop(trim(clinfo), 'the file has more than 3 spatial dimensions this case is not coded...') 
1147            !
1148            ! update idom definition...
1149            ! Identify the domain in case of jpdom_auto(glo/dta) definition
1150            IF( idom == jpdom_autoglo_xy ) THEN
1151               ll_depth_spec = .TRUE.
1152               idom = jpdom_autoglo
1153            ELSE
1154               ll_depth_spec = .FALSE.
1155            ENDIF
1156            IF( idom == jpdom_autoglo .OR. idom == jpdom_autodta ) THEN           
1157               IF( idom == jpdom_autoglo ) THEN   ;   idom = jpdom_global 
1158               ELSE                               ;   idom = jpdom_data
1159               ENDIF
1160               ind1 = INDEX( clname, '_', back = .TRUE. ) + 1
1161               ind2 = INDEX( clname, '.', back = .TRUE. ) - 1
1162               IF( ind2 > ind1 ) THEN   ;   IF( VERIFY( clname(ind1:ind2), '0123456789' ) == 0 )   idom = jpdom_local   ;   ENDIF
1163            ENDIF
1164            ! Identify the domain in case of jpdom_local definition
1165            IF( idom == jpdom_local ) THEN
1166               IF(     idimsz(1) == jpi               .AND. idimsz(2) == jpj               ) THEN   ;   idom = jpdom_local_full
1167               ELSEIF( idimsz(1) == nlci              .AND. idimsz(2) == nlcj              ) THEN   ;   idom = jpdom_local_noextra
1168               ELSEIF( idimsz(1) == (nlei - nldi + 1) .AND. idimsz(2) == (nlej - nldj + 1) ) THEN   ;   idom = jpdom_local_noovlap
1169               ELSE   ;   CALL ctl_stop( trim(clinfo), 'impossible to identify the local domain' )
1170               ENDIF
1171            ENDIF
1172            !
1173            ! check the consistency between input array and data rank in the file
1174            !
1175            ! initializations
1176            itime = 1
1177            IF( PRESENT(ktime) ) itime = ktime
1178            !
1179            irankpv = 1 * COUNT( (/PRESENT(pv_r1d)/) ) + 2 * COUNT( (/PRESENT(pv_r2d)/) ) + 3 * COUNT( (/PRESENT(pv_r3d)/) )
1180            WRITE(clrankpv, fmt='(i1)') irankpv
1181            WRITE(cldmspc , fmt='(i1)') idmspc
1182            !
1183            !!GS: we consider 2D data as 3D data with vertical dim size = 1
1184            !IF(     idmspc <  irankpv ) THEN
1185            !   CALL ctl_stop( TRIM(clinfo), 'The file has only '//cldmspc//' spatial dimension',   &
1186            !      &                         'it is impossible to read a '//clrankpv//'D array from this file...' )
1187            !ELSEIF( idmspc == irankpv ) THEN
1188            IF( idmspc == irankpv ) THEN
1189               IF( PRESENT(pv_r1d) .AND. idom /= jpdom_unknown )   &
1190                  &   CALL ctl_stop( TRIM(clinfo), 'case not coded...You must use jpdom_unknown' )
1191            ELSEIF( idmspc >  irankpv ) THEN
1192                  IF( PRESENT(pv_r2d) .AND. itime == 1 .AND. idimsz(3) == 1 .AND. idmspc == 3 ) THEN
1193                     CALL ctl_warn( trim(clinfo), '2D array but 3 spatial dimensions for the data...'              ,   &
1194                           &         'As the size of the z dimension is 1 and as we try to read the first record, ',   &
1195                           &         'we accept this case, even if there is a possible mix-up between z and time dimension' )   
1196                     idmspc = idmspc - 1
1197                  ELSE
1198                     CALL ctl_stop( TRIM(clinfo), 'To keep iom lisibility, when reading a '//clrankpv//'D array,'         ,   &
1199                        &                         'we do not accept data with '//cldmspc//' spatial dimensions',   &
1200                        &                         'Use ncwa -a to suppress the unnecessary dimensions' )
1201                  ENDIF
1202            ENDIF
1203            !
1204            ! definition of istart and icnt
1205            !
1206            icnt  (:) = 1
1207            istart(:) = 1
1208            istart(idmspc+1) = itime
1209   
1210            IF( PRESENT(kstart) .AND. .NOT. ll_depth_spec ) THEN
1211               istart(1:idmspc) = kstart(1:idmspc) 
1212               icnt  (1:idmspc) = kcount(1:idmspc)
1213            ELSE
1214               IF(idom == jpdom_unknown ) THEN
1215                  icnt(1:idmspc) = idimsz(1:idmspc)
1216               ELSE
1217                  IF( .NOT. PRESENT(pv_r1d) ) THEN   !   not a 1D array
1218                     IF(     idom == jpdom_data    ) THEN
1219                        jstartrow = 1
1220                        IF( luse_jattr ) THEN
1221                           CALL iom_getatt(kiomid, 'open_ocean_jstart', jstartrow ) ! -999 is returned if the attribute is not found
1222                           jstartrow = MAX(1,jstartrow)
1223                        ENDIF
1224                        istart(1:2) = (/ mig(1), mjg(1) + jstartrow - 1 /)  ! icnt(1:2) done below
1225                     ELSEIF( idom == jpdom_global  ) THEN ; istart(1:2) = (/ nimpp , njmpp  /)  ! icnt(1:2) done below
1226                     ENDIF
1227                     ! we do not read the overlap                     -> we start to read at nldi, nldj
1228! JMM + SM: ugly patch before getting the new version of lib_mpp)
1229!                  IF( idom /= jpdom_local_noovlap )   istart(1:2) = istart(1:2) + (/ nldi - 1, nldj - 1 /)
1230                     IF( llnoov .AND. idom /= jpdom_local_noovlap ) istart(1:2) = istart(1:2) + (/ nldi - 1, nldj - 1 /)
1231                  ! we do not read the overlap and the extra-halos -> from nldi to nlei and from nldj to nlej
1232! JMM + SM: ugly patch before getting the new version of lib_mpp)
1233!                  icnt(1:2) = (/ nlei - nldi + 1, nlej - nldj + 1 /)
1234                     IF( llnoov ) THEN   ;   icnt(1:2) = (/ nlei - nldi + 1, nlej - nldj + 1 /)
1235                     ELSE                ;   icnt(1:2) = (/ nlci           , nlcj            /)
1236                     ENDIF
1237                     IF( PRESENT(pv_r3d) ) THEN
1238                        IF( idom == jpdom_data ) THEN                        ;                               icnt(3) = inlev
1239                        ELSEIF( ll_depth_spec .AND. PRESENT(kstart) ) THEN   ;   istart(3) = kstart(3)   ;   icnt(3) = kcount(3)
1240                        ELSE                                                 ;                               icnt(3) = inlev
1241                        ENDIF
1242                     ENDIF
1243                  ENDIF
1244               ENDIF
1245            ENDIF
1246
1247            ! check that istart and icnt can be used with this file
1248            !-
1249            DO jl = 1, jpmax_dims
1250               itmp = istart(jl)+icnt(jl)-1
1251               IF( itmp > idimsz(jl) .AND. idimsz(jl) /= 0 ) THEN
1252                  WRITE( ctmp1, FMT="('(istart(', i1, ') + icnt(', i1, ') - 1) = ', i5)" ) jl, jl, itmp
1253                  WRITE( ctmp2, FMT="(' is larger than idimsz(', i1,') = ', i5)"         ) jl, idimsz(jl)
1254                  CALL ctl_stop( trim(clinfo), 'start and count too big regarding to the size of the data, ', ctmp1, ctmp2 )     
1255               ENDIF
1256            END DO
1257
1258            ! check that icnt matches the input array
1259            !-     
1260            IF( idom == jpdom_unknown ) THEN
1261               IF( irankpv == 1 )        ishape(1:1) = SHAPE(pv_r1d)
1262               IF( irankpv == 2 )        ishape(1:2) = SHAPE(pv_r2d)
1263               IF( irankpv == 3 )        ishape(1:3) = SHAPE(pv_r3d)
1264               ctmp1 = 'd'
1265            ELSE
1266               IF( irankpv == 2 ) THEN
1267! JMM + SM: ugly patch before getting the new version of lib_mpp)
1268!               ishape(1:2) = SHAPE(pv_r2d(nldi:nlei,nldj:nlej  ))   ;   ctmp1 = 'd(nldi:nlei,nldj:nlej)'
1269                  IF( llnoov ) THEN ; ishape(1:2)=SHAPE(pv_r2d(nldi:nlei,nldj:nlej  )) ; ctmp1='d(nldi:nlei,nldj:nlej)'
1270                  ELSE              ; ishape(1:2)=SHAPE(pv_r2d(1   :nlci,1   :nlcj  )) ; ctmp1='d(1:nlci,1:nlcj)'
1271                  ENDIF
1272               ENDIF
1273               IF( irankpv == 3 ) THEN 
1274! JMM + SM: ugly patch before getting the new version of lib_mpp)
1275!               ishape(1:3) = SHAPE(pv_r3d(nldi:nlei,nldj:nlej,:))   ;   ctmp1 = 'd(nldi:nlei,nldj:nlej,:)'
1276                  IF( llnoov ) THEN ; ishape(1:3)=SHAPE(pv_r3d(nldi:nlei,nldj:nlej,:)) ; ctmp1='d(nldi:nlei,nldj:nlej,:)'
1277                  ELSE              ; ishape(1:3)=SHAPE(pv_r3d(1   :nlci,1   :nlcj,:)) ; ctmp1='d(1:nlci,1:nlcj,:)'
1278                  ENDIF
1279               ENDIF
1280            ENDIF
1281         
1282            DO jl = 1, irankpv
1283               WRITE( ctmp2, FMT="(', ', i1,'): ', i5,' /= icnt(', i1,'):', i5)" ) jl, ishape(jl), jl, icnt(jl)
1284               IF( ishape(jl) /= icnt(jl) )   CALL ctl_stop( TRIM(clinfo), 'size(pv_r'//clrankpv//TRIM(ctmp1)//TRIM(ctmp2) )
1285            END DO
1286
1287         ENDIF
1288
1289         ! read the data
1290         !-     
1291         IF( idvar > 0 .AND. istop == nstop ) THEN   ! no additional errors until this point...
1292            !
1293         ! find the right index of the array to be read
1294! JMM + SM: ugly patch before getting the new version of lib_mpp)
1295!         IF( idom /= jpdom_unknown ) THEN   ;   ix1 = nldi   ;   ix2 = nlei      ;   iy1 = nldj   ;   iy2 = nlej
1296!         ELSE                               ;   ix1 = 1      ;   ix2 = icnt(1)   ;   iy1 = 1      ;   iy2 = icnt(2)
1297!         ENDIF
1298            IF( llnoov ) THEN
1299               IF( idom /= jpdom_unknown ) THEN   ;   ix1 = nldi   ;   ix2 = nlei      ;   iy1 = nldj   ;   iy2 = nlej
1300               ELSE                               ;   ix1 = 1      ;   ix2 = icnt(1)   ;   iy1 = 1      ;   iy2 = icnt(2)
1301               ENDIF
1302            ELSE
1303               IF( idom /= jpdom_unknown ) THEN   ;   ix1 = 1      ;   ix2 = nlci      ;   iy1 = 1      ;   iy2 = nlcj
1304               ELSE                               ;   ix1 = 1      ;   ix2 = icnt(1)   ;   iy1 = 1      ;   iy2 = icnt(2)
1305               ENDIF
1306            ENDIF
1307     
1308            CALL iom_nf90_get( kiomid, idvar, inbdim, istart, icnt, ix1, ix2, iy1, iy2, pv_r1d, pv_r2d, pv_r3d )
1309
1310            IF( istop == nstop ) THEN   ! no additional errors until this point...
1311               IF(lwp) WRITE(numout,"(10x,' read ',a,' (rec: ',i6,') in ',a,' ok')") TRIM(cdvar), itime, TRIM(iom_file(kiomid)%name)
1312             
1313               !--- overlap areas and extra hallows (mpp)
1314               IF(     PRESENT(pv_r2d) .AND. idom /= jpdom_unknown ) THEN
1315                  CALL lbc_lnk( 'iom', pv_r2d,'Z', -999., kfillmode = jpfillnothing )
1316               ELSEIF( PRESENT(pv_r3d) .AND. idom /= jpdom_unknown ) THEN
1317                  ! this if could be simplified with the new lbc_lnk that works with any size of the 3rd dimension
1318                  IF( icnt(3) == inlev ) THEN
1319                     CALL lbc_lnk( 'iom', pv_r3d,'Z', -999., kfillmode = jpfillnothing )
1320                  ELSE   ! put some arbitrary value (a call to lbc_lnk will be done later...)
1321                     DO jj = nlcj+1, jpj   ;   pv_r3d(1:nlci, jj, :) = pv_r3d(1:nlci, nlej, :)   ;   END DO
1322                     DO ji = nlci+1, jpi   ;   pv_r3d(ji    , : , :) = pv_r3d(nlei  , :   , :)   ;   END DO
1323                  ENDIF
1324               ENDIF
1325               !
1326            ELSE
1327               ! return if istop == nstop is false
1328               RETURN
1329            ENDIF
1330         ELSE
1331            ! return if statment idvar > 0 .AND. istop == nstop is false
1332            RETURN
1333         ENDIF
1334         !
1335      ELSE        ! read using XIOS. Only if KEY_IOMPUT is defined
1336#if defined key_iomput
1337!would be good to be able to check which context is active and swap only if current is not restart
1338         CALL iom_swap( TRIM(crxios_context) ) 
1339         IF( PRESENT(pv_r3d) ) THEN
1340            pv_r3d(:, :, :) = 0.
1341            if(lwp) write(numout,*) 'XIOS RST READ (3D): ',trim(cdvar)
1342            CALL xios_recv_field( trim(cdvar), pv_r3d)
1343            IF(idom /= jpdom_unknown ) then
1344                CALL lbc_lnk( 'iom', pv_r3d,'Z', -999., kfillmode = jpfillnothing)
1345            ENDIF
1346         ELSEIF( PRESENT(pv_r2d) ) THEN
1347            pv_r2d(:, :) = 0.
1348            if(lwp) write(numout,*) 'XIOS RST READ (2D): ', trim(cdvar)
1349            CALL xios_recv_field( trim(cdvar), pv_r2d)
1350            IF(idom /= jpdom_unknown ) THEN
1351                CALL lbc_lnk('iom', pv_r2d,'Z',-999., kfillmode = jpfillnothing)
1352            ENDIF
1353         ELSEIF( PRESENT(pv_r1d) ) THEN
1354            pv_r1d(:) = 0.
1355            if(lwp) write(numout,*) 'XIOS RST READ (1D): ', trim(cdvar)
1356            CALL xios_recv_field( trim(cdvar), pv_r1d)
1357         ENDIF
1358         CALL iom_swap( TRIM(cxios_context) )
1359#else
1360         istop = istop + 1 
1361         clinfo = 'Can not use XIOS in iom_get_123d, file: '//trim(clname)//', var:'//trim(cdvar)
1362#endif
1363      ENDIF
1364!some final adjustments
1365      ! C1D case : always call lbc_lnk to replicate the central value over the whole 3X3 domain
1366      IF( lk_c1d .AND. PRESENT(pv_r2d) )   CALL lbc_lnk( 'iom', pv_r2d,'Z',1. )
1367      IF( lk_c1d .AND. PRESENT(pv_r3d) )   CALL lbc_lnk( 'iom', pv_r3d,'Z',1. )
1368
1369      !--- Apply scale_factor and offset
1370      zscf = iom_file(kiomid)%scf(idvar)      ! scale factor
1371      zofs = iom_file(kiomid)%ofs(idvar)      ! offset
1372      IF(     PRESENT(pv_r1d) ) THEN
1373         IF( zscf /= 1. )   pv_r1d(:) = pv_r1d(:) * zscf 
1374         IF( zofs /= 0. )   pv_r1d(:) = pv_r1d(:) + zofs
1375      ELSEIF( PRESENT(pv_r2d) ) THEN
1376         IF( zscf /= 1.)   pv_r2d(:,:) = pv_r2d(:,:) * zscf
1377         IF( zofs /= 0.)   pv_r2d(:,:) = pv_r2d(:,:) + zofs
1378      ELSEIF( PRESENT(pv_r3d) ) THEN
1379         IF( zscf /= 1.)   pv_r3d(:,:,:) = pv_r3d(:,:,:) * zscf
1380         IF( zofs /= 0.)   pv_r3d(:,:,:) = pv_r3d(:,:,:) + zofs
1381      ENDIF
1382      !
1383   END SUBROUTINE iom_get_123d
1384
1385   SUBROUTINE iom_get_var( cdname, z2d)
1386      CHARACTER(LEN=*), INTENT(in ) ::   cdname
1387      REAL(wp), DIMENSION(jpi,jpj) ::   z2d 
1388#if defined key_iomput
1389      IF( xios_field_is_active( cdname, at_current_timestep_arg = .TRUE. ) ) THEN
1390         z2d(:,:) = 0._wp
1391         CALL xios_recv_field( cdname, z2d)
1392      ENDIF
1393#else
1394      IF( .FALSE. )   WRITE(numout,*) cdname, z2d ! useless test to avoid compilation warnings
1395#endif
1396   END SUBROUTINE iom_get_var
1397
1398
1399   FUNCTION iom_getszuld ( kiomid ) 
1400      !!-----------------------------------------------------------------------
1401      !!                  ***  FUNCTION  iom_getszuld  ***
1402      !!
1403      !! ** Purpose : get the size of the unlimited dimension in a file
1404      !!              (return -1 if not found)
1405      !!-----------------------------------------------------------------------
1406      INTEGER, INTENT(in   ) ::   kiomid   ! file Identifier
1407      !
1408      INTEGER                ::   iom_getszuld
1409      !!-----------------------------------------------------------------------
1410      iom_getszuld = -1
1411      IF( kiomid > 0 ) THEN
1412         IF( iom_file(kiomid)%iduld > 0 )   iom_getszuld = iom_file(kiomid)%lenuld
1413      ENDIF
1414   END FUNCTION iom_getszuld
1415   
1416
1417   !!----------------------------------------------------------------------
1418   !!                   INTERFACE iom_chkatt
1419   !!----------------------------------------------------------------------
1420   SUBROUTINE iom_chkatt( kiomid, cdatt, llok, ksize, cdvar )
1421      INTEGER         , INTENT(in   )                 ::   kiomid    ! Identifier of the file
1422      CHARACTER(len=*), INTENT(in   )                 ::   cdatt     ! Name of the attribute
1423      LOGICAL         , INTENT(  out)                 ::   llok      ! Error code
1424      INTEGER         , INTENT(  out), OPTIONAL       ::   ksize     ! Size of the attribute array
1425      CHARACTER(len=*), INTENT(in   ), OPTIONAL       ::   cdvar     ! Name of the variable
1426      !
1427      IF( kiomid > 0 ) THEN
1428         IF( iom_file(kiomid)%nfid > 0 )   CALL iom_nf90_chkatt( kiomid, cdatt, llok, ksize=ksize, cdvar=cdvar )
1429      ENDIF
1430      !
1431   END SUBROUTINE iom_chkatt
1432
1433   !!----------------------------------------------------------------------
1434   !!                   INTERFACE iom_getatt
1435   !!----------------------------------------------------------------------
1436   SUBROUTINE iom_g0d_iatt( kiomid, cdatt, katt0d, cdvar )
1437      INTEGER               , INTENT(in   )           ::   kiomid    ! Identifier of the file
1438      CHARACTER(len=*)      , INTENT(in   )           ::   cdatt     ! Name of the attribute
1439      INTEGER               , INTENT(  out)           ::   katt0d    ! read field
1440      CHARACTER(len=*)      , INTENT(in   ), OPTIONAL ::   cdvar     ! Name of the variable
1441      !
1442      IF( kiomid > 0 ) THEN
1443         IF( iom_file(kiomid)%nfid > 0 )   CALL iom_nf90_getatt( kiomid, cdatt,  katt0d =  katt0d, cdvar=cdvar )
1444      ENDIF
1445   END SUBROUTINE iom_g0d_iatt
1446
1447   SUBROUTINE iom_g1d_iatt( kiomid, cdatt, katt1d, cdvar )
1448      INTEGER               , INTENT(in   )           ::   kiomid    ! Identifier of the file
1449      CHARACTER(len=*)      , INTENT(in   )           ::   cdatt     ! Name of the attribute
1450      INTEGER, DIMENSION(:) , INTENT(  out)           ::   katt1d    ! read field
1451      CHARACTER(len=*)      , INTENT(in   ), OPTIONAL ::   cdvar     ! Name of the variable
1452      !
1453      IF( kiomid > 0 ) THEN
1454         IF( iom_file(kiomid)%nfid > 0 )   CALL iom_nf90_getatt( kiomid, cdatt,  katt1d =  katt1d, cdvar=cdvar )
1455      ENDIF
1456   END SUBROUTINE iom_g1d_iatt
1457
1458   SUBROUTINE iom_g0d_ratt( kiomid, cdatt, patt0d, cdvar )
1459      INTEGER               , INTENT(in   )           ::   kiomid    ! Identifier of the file
1460      CHARACTER(len=*)      , INTENT(in   )           ::   cdatt     ! Name of the attribute
1461      REAL(wp)              , INTENT(  out)           ::   patt0d    ! read field
1462      CHARACTER(len=*)      , INTENT(in   ), OPTIONAL ::   cdvar     ! Name of the variable
1463      !
1464      IF( kiomid > 0 ) THEN
1465         IF( iom_file(kiomid)%nfid > 0 )   CALL iom_nf90_getatt( kiomid, cdatt,  patt0d =  patt0d, cdvar=cdvar )
1466      ENDIF
1467   END SUBROUTINE iom_g0d_ratt
1468
1469   SUBROUTINE iom_g1d_ratt( kiomid, cdatt, patt1d, cdvar )
1470      INTEGER               , INTENT(in   )           ::   kiomid    ! Identifier of the file
1471      CHARACTER(len=*)      , INTENT(in   )           ::   cdatt     ! Name of the attribute
1472      REAL(wp), DIMENSION(:), INTENT(  out)           ::   patt1d    ! read field
1473      CHARACTER(len=*)      , INTENT(in   ), OPTIONAL ::   cdvar     ! Name of the variable
1474      !
1475      IF( kiomid > 0 ) THEN
1476         IF( iom_file(kiomid)%nfid > 0 )   CALL iom_nf90_getatt( kiomid, cdatt,  patt1d =  patt1d, cdvar=cdvar )
1477      ENDIF
1478   END SUBROUTINE iom_g1d_ratt
1479   
1480   SUBROUTINE iom_g0d_catt( kiomid, cdatt, cdatt0d, cdvar )
1481      INTEGER               , INTENT(in   )           ::   kiomid    ! Identifier of the file
1482      CHARACTER(len=*)      , INTENT(in   )           ::   cdatt     ! Name of the attribute
1483      CHARACTER(len=*)      , INTENT(  out)           ::   cdatt0d   ! read field
1484      CHARACTER(len=*)      , INTENT(in   ), OPTIONAL ::   cdvar     ! Name of the variable
1485      !
1486      IF( kiomid > 0 ) THEN
1487         IF( iom_file(kiomid)%nfid > 0 )   CALL iom_nf90_getatt( kiomid, cdatt, cdatt0d = cdatt0d, cdvar=cdvar )
1488      ENDIF
1489   END SUBROUTINE iom_g0d_catt
1490
1491
1492   !!----------------------------------------------------------------------
1493   !!                   INTERFACE iom_putatt
1494   !!----------------------------------------------------------------------
1495   SUBROUTINE iom_p0d_iatt( kiomid, cdatt, katt0d, cdvar )
1496      INTEGER               , INTENT(in   )           ::   kiomid    ! Identifier of the file
1497      CHARACTER(len=*)      , INTENT(in   )           ::   cdatt     ! Name of the attribute
1498      INTEGER               , INTENT(in   )           ::   katt0d    ! written field
1499      CHARACTER(len=*)      , INTENT(in   ), OPTIONAL ::   cdvar     ! Name of the variable
1500      !
1501      IF( kiomid > 0 ) THEN
1502         IF( iom_file(kiomid)%nfid > 0 )   CALL iom_nf90_putatt( kiomid, cdatt,  katt0d =  katt0d, cdvar=cdvar )
1503      ENDIF
1504   END SUBROUTINE iom_p0d_iatt
1505
1506   SUBROUTINE iom_p1d_iatt( kiomid, cdatt, katt1d, cdvar )
1507      INTEGER               , INTENT(in   )           ::   kiomid    ! Identifier of the file
1508      CHARACTER(len=*)      , INTENT(in   )           ::   cdatt     ! Name of the attribute
1509      INTEGER, DIMENSION(:) , INTENT(in   )           ::   katt1d    ! written field
1510      CHARACTER(len=*)      , INTENT(in   ), OPTIONAL ::   cdvar     ! Name of the variable
1511      !
1512      IF( kiomid > 0 ) THEN
1513         IF( iom_file(kiomid)%nfid > 0 )   CALL iom_nf90_putatt( kiomid, cdatt,  katt1d =  katt1d, cdvar=cdvar )
1514      ENDIF
1515   END SUBROUTINE iom_p1d_iatt
1516
1517   SUBROUTINE iom_p0d_ratt( kiomid, cdatt, patt0d, cdvar )
1518      INTEGER               , INTENT(in   )           ::   kiomid    ! Identifier of the file
1519      CHARACTER(len=*)      , INTENT(in   )           ::   cdatt     ! Name of the attribute
1520      REAL(wp)              , INTENT(in   )           ::   patt0d    ! written field
1521      CHARACTER(len=*)      , INTENT(in   ), OPTIONAL ::   cdvar     ! Name of the variable
1522      !
1523      IF( kiomid > 0 ) THEN
1524         IF( iom_file(kiomid)%nfid > 0 )   CALL iom_nf90_putatt( kiomid, cdatt,  patt0d =  patt0d, cdvar=cdvar )
1525      ENDIF
1526   END SUBROUTINE iom_p0d_ratt
1527
1528   SUBROUTINE iom_p1d_ratt( kiomid, cdatt, patt1d, cdvar )
1529      INTEGER               , INTENT(in   )           ::   kiomid    ! Identifier of the file
1530      CHARACTER(len=*)      , INTENT(in   )           ::   cdatt     ! Name of the attribute
1531      REAL(wp), DIMENSION(:), INTENT(in   )           ::   patt1d    ! written field
1532      CHARACTER(len=*)      , INTENT(in   ), OPTIONAL ::   cdvar     ! Name of the variable
1533      !
1534      IF( kiomid > 0 ) THEN
1535         IF( iom_file(kiomid)%nfid > 0 )   CALL iom_nf90_putatt( kiomid, cdatt,  patt1d =  patt1d, cdvar=cdvar )
1536      ENDIF
1537   END SUBROUTINE iom_p1d_ratt
1538   
1539   SUBROUTINE iom_p0d_catt( kiomid, cdatt, cdatt0d, cdvar )
1540      INTEGER               , INTENT(in   )           ::   kiomid    ! Identifier of the file
1541      CHARACTER(len=*)      , INTENT(in   )           ::   cdatt     ! Name of the attribute
1542      CHARACTER(len=*)      , INTENT(in   )           ::   cdatt0d   ! written field
1543      CHARACTER(len=*)      , INTENT(in   ), OPTIONAL ::   cdvar     ! Name of the variable
1544      !
1545      IF( kiomid > 0 ) THEN
1546         IF( iom_file(kiomid)%nfid > 0 )   CALL iom_nf90_putatt( kiomid, cdatt, cdatt0d = cdatt0d, cdvar=cdvar )
1547      ENDIF
1548   END SUBROUTINE iom_p0d_catt
1549
1550
1551   !!----------------------------------------------------------------------
1552   !!                   INTERFACE iom_rstput
1553   !!----------------------------------------------------------------------
1554   SUBROUTINE iom_rp0d( kt, kwrite, kiomid, cdvar, pvar, ktype, ldxios )
1555      INTEGER         , INTENT(in)                         ::   kt       ! ocean time-step
1556      INTEGER         , INTENT(in)                         ::   kwrite   ! writing time-step
1557      INTEGER         , INTENT(in)                         ::   kiomid   ! Identifier of the file
1558      CHARACTER(len=*), INTENT(in)                         ::   cdvar    ! time axis name
1559      REAL(wp)        , INTENT(in)                         ::   pvar     ! written field
1560      INTEGER         , INTENT(in), OPTIONAL               ::   ktype    ! variable external type
1561      LOGICAL, OPTIONAL :: ldxios   ! xios write flag
1562      LOGICAL :: llx                ! local xios write flag
1563      INTEGER :: ivid   ! variable id
1564
1565      llx = .FALSE.
1566      IF(PRESENT(ldxios)) llx = ldxios
1567      IF( llx ) THEN
1568#ifdef key_iomput
1569      IF( kt == kwrite ) THEN
1570          IF(lwp) write(numout,*) 'RESTART: write (XIOS 0D) ',trim(cdvar)
1571          CALL xios_send_field(trim(cdvar), pvar)
1572      ENDIF
1573#endif
1574      ELSE
1575         IF( kiomid > 0 ) THEN
1576            IF( iom_file(kiomid)%nfid > 0 ) THEN
1577               ivid = iom_varid( kiomid, cdvar, ldstop = .FALSE. )
1578               CALL iom_nf90_rstput( kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r0d = pvar )
1579            ENDIF
1580         ENDIF
1581      ENDIF
1582   END SUBROUTINE iom_rp0d
1583
1584   SUBROUTINE iom_rp1d( kt, kwrite, kiomid, cdvar, pvar, ktype, ldxios )
1585      INTEGER         , INTENT(in)                         ::   kt       ! ocean time-step
1586      INTEGER         , INTENT(in)                         ::   kwrite   ! writing time-step
1587      INTEGER         , INTENT(in)                         ::   kiomid   ! Identifier of the file
1588      CHARACTER(len=*), INTENT(in)                         ::   cdvar    ! time axis name
1589      REAL(wp)        , INTENT(in), DIMENSION(          :) ::   pvar     ! written field
1590      INTEGER         , INTENT(in), OPTIONAL               ::   ktype    ! variable external type
1591      LOGICAL, OPTIONAL                                    ::   ldxios   ! xios write flag
1592      LOGICAL :: llx                ! local xios write flag
1593      INTEGER :: ivid   ! variable id
1594
1595      llx = .FALSE.
1596      IF(PRESENT(ldxios)) llx = ldxios
1597      IF( llx ) THEN
1598#ifdef key_iomput
1599      IF( kt == kwrite ) THEN
1600         IF(lwp) write(numout,*) 'RESTART: write (XIOS 1D) ',trim(cdvar)
1601         CALL xios_send_field(trim(cdvar), pvar)
1602      ENDIF
1603#endif
1604      ELSE
1605         IF( kiomid > 0 ) THEN
1606            IF( iom_file(kiomid)%nfid > 0 ) THEN
1607               ivid = iom_varid( kiomid, cdvar, ldstop = .FALSE. )
1608               CALL iom_nf90_rstput( kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r1d = pvar )
1609            ENDIF
1610         ENDIF
1611      ENDIF
1612   END SUBROUTINE iom_rp1d
1613
1614   SUBROUTINE iom_rp2d( kt, kwrite, kiomid, cdvar, pvar, ktype, ldxios )
1615      INTEGER         , INTENT(in)                         ::   kt       ! ocean time-step
1616      INTEGER         , INTENT(in)                         ::   kwrite   ! writing time-step
1617      INTEGER         , INTENT(in)                         ::   kiomid   ! Identifier of the file
1618      CHARACTER(len=*), INTENT(in)                         ::   cdvar    ! time axis name
1619      REAL(wp)        , INTENT(in), DIMENSION(:,    :    ) ::   pvar     ! written field
1620      INTEGER         , INTENT(in), OPTIONAL               ::   ktype    ! variable external type
1621      LOGICAL, OPTIONAL :: ldxios   ! xios write flag
1622      LOGICAL :: llx
1623      INTEGER :: ivid   ! variable id
1624
1625      llx = .FALSE.
1626      IF(PRESENT(ldxios)) llx = ldxios
1627      IF( llx ) THEN
1628#ifdef key_iomput
1629      IF( kt == kwrite ) THEN
1630         IF(lwp) write(numout,*) 'RESTART: write (XIOS 2D) ',trim(cdvar)
1631         CALL xios_send_field(trim(cdvar), pvar)
1632      ENDIF
1633#endif
1634      ELSE
1635         IF( kiomid > 0 ) THEN
1636            IF( iom_file(kiomid)%nfid > 0 ) THEN
1637               ivid = iom_varid( kiomid, cdvar, ldstop = .FALSE. )
1638               CALL iom_nf90_rstput( kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r2d = pvar )
1639            ENDIF
1640         ENDIF
1641      ENDIF
1642   END SUBROUTINE iom_rp2d
1643
1644   SUBROUTINE iom_rp3d( kt, kwrite, kiomid, cdvar, pvar, ktype, ldxios )
1645      INTEGER         , INTENT(in)                         ::   kt       ! ocean time-step
1646      INTEGER         , INTENT(in)                         ::   kwrite   ! writing time-step
1647      INTEGER         , INTENT(in)                         ::   kiomid   ! Identifier of the file
1648      CHARACTER(len=*), INTENT(in)                         ::   cdvar    ! time axis name
1649      REAL(wp)        , INTENT(in),       DIMENSION(:,:,:) ::   pvar     ! written field
1650      INTEGER         , INTENT(in), OPTIONAL               ::   ktype    ! variable external type
1651      LOGICAL, OPTIONAL :: ldxios   ! xios write flag
1652      LOGICAL :: llx                 ! local xios write flag
1653      INTEGER :: ivid   ! variable id
1654
1655      llx = .FALSE.
1656      IF(PRESENT(ldxios)) llx = ldxios
1657      IF( llx ) THEN
1658#ifdef key_iomput
1659      IF( kt == kwrite ) THEN
1660         IF(lwp) write(numout,*) 'RESTART: write (XIOS 3D) ',trim(cdvar)
1661         CALL xios_send_field(trim(cdvar), pvar)
1662      ENDIF
1663#endif
1664      ELSE
1665         IF( kiomid > 0 ) THEN
1666            IF( iom_file(kiomid)%nfid > 0 ) THEN
1667               ivid = iom_varid( kiomid, cdvar, ldstop = .FALSE. )
1668               CALL iom_nf90_rstput( kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r3d = pvar )
1669            ENDIF
1670         ENDIF
1671      ENDIF
1672   END SUBROUTINE iom_rp3d
1673
1674
1675  SUBROUTINE iom_delay_rst( cdaction, cdcpnt, kncid )
1676      !!---------------------------------------------------------------------
1677      !!   Routine iom_delay_rst: used read/write restart related to mpp_delay
1678      !!
1679      !!---------------------------------------------------------------------
1680      CHARACTER(len=*), INTENT(in   ) ::   cdaction        !
1681      CHARACTER(len=*), INTENT(in   ) ::   cdcpnt
1682      INTEGER         , INTENT(in   ) ::   kncid
1683      !
1684      INTEGER  :: ji
1685      INTEGER  :: indim
1686      LOGICAL  :: llattexist
1687      REAL(wp), ALLOCATABLE, DIMENSION(:) ::   zreal1d
1688      !!---------------------------------------------------------------------
1689      !
1690      !                                      ===================================
1691      IF( TRIM(cdaction) == 'READ' ) THEN   ! read restart related to mpp_delay !
1692         !                                   ===================================
1693         DO ji = 1, nbdelay
1694            IF ( c_delaycpnt(ji) == cdcpnt ) THEN
1695               CALL iom_chkatt( kncid, 'DELAY_'//c_delaylist(ji), llattexist, indim )
1696               IF( llattexist )  THEN
1697                  ALLOCATE( todelay(ji)%z1d(indim) )
1698                  CALL iom_getatt( kncid, 'DELAY_'//c_delaylist(ji), todelay(ji)%z1d(:) )
1699                  ndelayid(ji) = 0   ! set to 0 to specify that the value was read in the restart
1700               ENDIF
1701           ENDIF
1702         END DO
1703         !                                   ====================================
1704      ELSE                                  ! write restart related to mpp_delay !
1705         !                                   ====================================
1706         DO ji = 1, nbdelay   ! save only ocean delayed global communication variables
1707            IF ( c_delaycpnt(ji) == cdcpnt ) THEN
1708               IF( ASSOCIATED(todelay(ji)%z1d) ) THEN
1709                  CALL mpp_delay_rcv(ji)   ! make sure %z1d is received
1710                  CALL iom_putatt( kncid, 'DELAY_'//c_delaylist(ji), todelay(ji)%z1d(:) )
1711               ENDIF
1712            ENDIF
1713         END DO
1714         !
1715      ENDIF
1716     
1717   END SUBROUTINE iom_delay_rst
1718 
1719   
1720
1721   !!----------------------------------------------------------------------
1722   !!                   INTERFACE iom_put
1723   !!----------------------------------------------------------------------
1724   SUBROUTINE iom_p0d( cdname, pfield0d )
1725      CHARACTER(LEN=*), INTENT(in) ::   cdname
1726      REAL(wp)        , INTENT(in) ::   pfield0d
1727!!      REAL(wp)        , DIMENSION(jpi,jpj) ::   zz     ! masson
1728#if defined key_iomput
1729!!clem      zz(:,:)=pfield0d
1730!!clem      CALL xios_send_field(cdname, zz)
1731      CALL xios_send_field(cdname, (/pfield0d/)) 
1732#else
1733      IF( .FALSE. )   WRITE(numout,*) cdname, pfield0d   ! useless test to avoid compilation warnings
1734#endif
1735   END SUBROUTINE iom_p0d
1736
1737   SUBROUTINE iom_p1d( cdname, pfield1d )
1738      CHARACTER(LEN=*)          , INTENT(in) ::   cdname
1739      REAL(wp),     DIMENSION(:), INTENT(in) ::   pfield1d
1740#if defined key_iomput
1741      CALL xios_send_field( cdname, RESHAPE( (/pfield1d/), (/1,1,SIZE(pfield1d)/) ) )
1742#else
1743      IF( .FALSE. )   WRITE(numout,*) cdname, pfield1d   ! useless test to avoid compilation warnings
1744#endif
1745   END SUBROUTINE iom_p1d
1746
1747   SUBROUTINE iom_p2d( cdname, pfield2d )
1748      CHARACTER(LEN=*)            , INTENT(in) ::   cdname
1749      REAL(wp),     DIMENSION(:,:), INTENT(in) ::   pfield2d
1750#if defined key_iomput
1751      CALL xios_send_field(cdname, pfield2d)
1752#else
1753      IF( .FALSE. )   WRITE(numout,*) cdname, pfield2d   ! useless test to avoid compilation warnings
1754#endif
1755   END SUBROUTINE iom_p2d
1756
1757   SUBROUTINE iom_p3d( cdname, pfield3d )
1758      CHARACTER(LEN=*)                , INTENT(in) ::   cdname
1759      REAL(wp),       DIMENSION(:,:,:), INTENT(in) ::   pfield3d
1760#if defined key_iomput
1761      CALL xios_send_field( cdname, pfield3d )
1762#else
1763      IF( .FALSE. )   WRITE(numout,*) cdname, pfield3d   ! useless test to avoid compilation warnings
1764#endif
1765   END SUBROUTINE iom_p3d
1766
1767   SUBROUTINE iom_p4d( cdname, pfield4d )
1768      CHARACTER(LEN=*)                , INTENT(in) ::   cdname
1769      REAL(wp),       DIMENSION(:,:,:,:), INTENT(in) ::   pfield4d
1770#if defined key_iomput
1771      CALL xios_send_field(cdname, pfield4d)
1772#else
1773      IF( .FALSE. )   WRITE(numout,*) cdname, pfield4d   ! useless test to avoid compilation warnings
1774#endif
1775   END SUBROUTINE iom_p4d
1776
1777
1778#if defined key_iomput
1779   !!----------------------------------------------------------------------
1780   !!   'key_iomput'                                         XIOS interface
1781   !!----------------------------------------------------------------------
1782
1783   SUBROUTINE iom_set_domain_attr( cdid, ni_glo, nj_glo, ibegin, jbegin, ni, nj,                                               &
1784      &                                    data_dim, data_ibegin, data_ni, data_jbegin, data_nj, lonvalue, latvalue, mask,     &
1785      &                                    nvertex, bounds_lon, bounds_lat, area )
1786      !!----------------------------------------------------------------------
1787      !!----------------------------------------------------------------------
1788      CHARACTER(LEN=*)                  , INTENT(in) ::   cdid
1789      INTEGER                 , OPTIONAL, INTENT(in) ::   ni_glo, nj_glo, ibegin, jbegin, ni, nj
1790      INTEGER                 , OPTIONAL, INTENT(in) ::   data_dim, data_ibegin, data_ni, data_jbegin, data_nj
1791      INTEGER                 , OPTIONAL, INTENT(in) ::   nvertex
1792      REAL(wp), DIMENSION(:)  , OPTIONAL, INTENT(in) ::   lonvalue, latvalue
1793      REAL(wp), DIMENSION(:,:), OPTIONAL, INTENT(in) ::   bounds_lon, bounds_lat, area
1794      LOGICAL , DIMENSION(:)  , OPTIONAL, INTENT(in) ::   mask
1795      !!----------------------------------------------------------------------
1796      !
1797      IF( xios_is_valid_domain     (cdid) ) THEN
1798         CALL xios_set_domain_attr     ( cdid, ni_glo=ni_glo, nj_glo=nj_glo, ibegin=ibegin, jbegin=jbegin, ni=ni, nj=nj,   &
1799            &    data_dim=data_dim, data_ibegin=data_ibegin, data_ni=data_ni, data_jbegin=data_jbegin, data_nj=data_nj ,   &
1800            &    lonvalue_1D=lonvalue, latvalue_1D=latvalue, mask_1D=mask, nvertex=nvertex, bounds_lon_1D=bounds_lon,      &
1801            &    bounds_lat_1D=bounds_lat, area=area, type='curvilinear')
1802      ENDIF
1803      IF( xios_is_valid_domaingroup(cdid) ) THEN
1804         CALL xios_set_domaingroup_attr( cdid, ni_glo=ni_glo, nj_glo=nj_glo, ibegin=ibegin, jbegin=jbegin, ni=ni, nj=nj,   &
1805            &    data_dim=data_dim, data_ibegin=data_ibegin, data_ni=data_ni, data_jbegin=data_jbegin, data_nj=data_nj ,   &
1806            &    lonvalue_1D=lonvalue, latvalue_1D=latvalue, mask_1D=mask, nvertex=nvertex, bounds_lon_1D=bounds_lon,      &
1807            &    bounds_lat_1D=bounds_lat, area=area, type='curvilinear' )
1808      ENDIF
1809      !
1810      CALL xios_solve_inheritance()
1811      !
1812   END SUBROUTINE iom_set_domain_attr
1813
1814
1815   SUBROUTINE iom_set_zoom_domain_attr( cdid, ibegin, jbegin, ni, nj )
1816      !!----------------------------------------------------------------------
1817      !!----------------------------------------------------------------------
1818      CHARACTER(LEN=*), INTENT(in) ::   cdid
1819      INTEGER         , INTENT(in) ::   ibegin, jbegin, ni, nj
1820      !
1821      TYPE(xios_gridgroup) :: gridgroup_hdl
1822      TYPE(xios_grid)      :: grid_hdl
1823      TYPE(xios_domain)    :: domain_hdl 
1824      TYPE(xios_axis)      :: axis_hdl 
1825      CHARACTER(LEN=64)    :: cldomrefid   ! domain_ref name
1826      CHARACTER(len=1)     :: cl1          ! last character of this name
1827      !!----------------------------------------------------------------------
1828      !
1829      IF( xios_is_valid_zoom_domain(cdid) ) THEN
1830         ! define the zoom_domain attributs
1831         CALL xios_set_zoom_domain_attr( cdid, ibegin=ibegin, jbegin=jbegin, ni=ni, nj=nj )
1832         ! define a new 2D grid with this new domain
1833         CALL xios_get_handle("grid_definition", gridgroup_hdl )
1834         CALL xios_add_child(gridgroup_hdl, grid_hdl, TRIM(cdid)//'_2D' )   ! add a new 2D grid to grid_definition
1835         CALL xios_add_child(grid_hdl, domain_hdl, TRIM(cdid) )             ! add its domain
1836         ! define a new 3D grid with this new domain
1837         CALL xios_add_child(gridgroup_hdl, grid_hdl, TRIM(cdid)//'_3D' )   ! add a new 3D grid to grid_definition
1838         CALL xios_add_child(grid_hdl, domain_hdl, TRIM(cdid) )             ! add its domain
1839         ! vertical axis
1840         cl1 = cdid(LEN_TRIM(cdid):)                                        ! last letter of cdid
1841         cl1 = CHAR(ICHAR(cl1)+32)                                          ! from upper to lower case
1842         CALL xios_add_child(grid_hdl, axis_hdl, 'depth'//cl1)              ! add its axis
1843      ENDIF
1844      !     
1845   END SUBROUTINE iom_set_zoom_domain_attr
1846
1847
1848   SUBROUTINE iom_set_axis_attr( cdid, paxis, bounds )
1849      !!----------------------------------------------------------------------
1850      !!----------------------------------------------------------------------
1851      CHARACTER(LEN=*)      , INTENT(in) ::   cdid
1852      REAL(wp), DIMENSION(:)  , OPTIONAL, INTENT(in) ::   paxis
1853      REAL(wp), DIMENSION(:,:), OPTIONAL, INTENT(in) ::   bounds
1854      !!----------------------------------------------------------------------
1855      IF( PRESENT(paxis) ) THEN
1856         IF( xios_is_valid_axis     (cdid) )   CALL xios_set_axis_attr     ( cdid, n_glo=SIZE(paxis), value=paxis )
1857         IF( xios_is_valid_axisgroup(cdid) )   CALL xios_set_axisgroup_attr( cdid, n_glo=SIZE(paxis), value=paxis )
1858      ENDIF
1859      IF( xios_is_valid_axis     (cdid) )   CALL xios_set_axis_attr     ( cdid, bounds=bounds )
1860      IF( xios_is_valid_axisgroup(cdid) )   CALL xios_set_axisgroup_attr( cdid, bounds=bounds )
1861      CALL xios_solve_inheritance()
1862   END SUBROUTINE iom_set_axis_attr
1863
1864
1865   SUBROUTINE iom_set_field_attr( cdid, freq_op, freq_offset )
1866      !!----------------------------------------------------------------------
1867      !!----------------------------------------------------------------------
1868      CHARACTER(LEN=*)             , INTENT(in) ::   cdid
1869      TYPE(xios_duration), OPTIONAL, INTENT(in) ::   freq_op
1870      TYPE(xios_duration), OPTIONAL, INTENT(in) ::   freq_offset
1871      !!----------------------------------------------------------------------
1872      IF( xios_is_valid_field     (cdid) )   CALL xios_set_field_attr     ( cdid, freq_op=freq_op, freq_offset=freq_offset )
1873      IF( xios_is_valid_fieldgroup(cdid) )   CALL xios_set_fieldgroup_attr( cdid, freq_op=freq_op, freq_offset=freq_offset )
1874      CALL xios_solve_inheritance()
1875   END SUBROUTINE iom_set_field_attr
1876
1877
1878   SUBROUTINE iom_set_file_attr( cdid, name, name_suffix )
1879      !!----------------------------------------------------------------------
1880      !!----------------------------------------------------------------------
1881      CHARACTER(LEN=*)          , INTENT(in) ::   cdid
1882      CHARACTER(LEN=*),OPTIONAL , INTENT(in) ::   name, name_suffix
1883      !!----------------------------------------------------------------------
1884      IF( xios_is_valid_file     (cdid) )   CALL xios_set_file_attr     ( cdid, name=name, name_suffix=name_suffix )
1885      IF( xios_is_valid_filegroup(cdid) )   CALL xios_set_filegroup_attr( cdid, name=name, name_suffix=name_suffix )
1886      CALL xios_solve_inheritance()
1887   END SUBROUTINE iom_set_file_attr
1888
1889
1890   SUBROUTINE iom_get_file_attr( cdid, name, name_suffix, output_freq )
1891      !!----------------------------------------------------------------------
1892      !!----------------------------------------------------------------------
1893      CHARACTER(LEN=*)          , INTENT(in ) ::   cdid
1894      CHARACTER(LEN=*),OPTIONAL , INTENT(out) ::   name, name_suffix
1895      TYPE(xios_duration), OPTIONAL , INTENT(out) :: output_freq
1896      LOGICAL                                 ::   llexist1,llexist2,llexist3
1897      !---------------------------------------------------------------------
1898      IF( PRESENT( name        ) )   name = ''          ! default values
1899      IF( PRESENT( name_suffix ) )   name_suffix = ''
1900      IF( PRESENT( output_freq ) )   output_freq = xios_duration(0,0,0,0,0,0)
1901      IF( xios_is_valid_file     (cdid) ) THEN
1902         CALL xios_solve_inheritance()
1903         CALL xios_is_defined_file_attr     ( cdid, name = llexist1, name_suffix = llexist2, output_freq = llexist3)
1904         IF(llexist1)   CALL xios_get_file_attr     ( cdid, name = name )
1905         IF(llexist2)   CALL xios_get_file_attr     ( cdid, name_suffix = name_suffix )
1906         IF(llexist3)   CALL xios_get_file_attr     ( cdid, output_freq = output_freq )
1907      ENDIF
1908      IF( xios_is_valid_filegroup(cdid) ) THEN
1909         CALL xios_solve_inheritance()
1910         CALL xios_is_defined_filegroup_attr( cdid, name = llexist1, name_suffix = llexist2, output_freq = llexist3)
1911         IF(llexist1)   CALL xios_get_filegroup_attr( cdid, name = name )
1912         IF(llexist2)   CALL xios_get_filegroup_attr( cdid, name_suffix = name_suffix )
1913         IF(llexist3)   CALL xios_get_filegroup_attr( cdid, output_freq = output_freq )
1914      ENDIF
1915   END SUBROUTINE iom_get_file_attr
1916
1917
1918   SUBROUTINE iom_set_grid_attr( cdid, mask )
1919      !!----------------------------------------------------------------------
1920      !!----------------------------------------------------------------------
1921      CHARACTER(LEN=*)                   , INTENT(in) ::   cdid
1922      LOGICAL, DIMENSION(:,:,:), OPTIONAL, INTENT(in) ::   mask
1923      !!----------------------------------------------------------------------
1924      IF( xios_is_valid_grid     (cdid) )   CALL xios_set_grid_attr     ( cdid, mask_3D=mask )
1925      IF( xios_is_valid_gridgroup(cdid) )   CALL xios_set_gridgroup_attr( cdid, mask_3D=mask )
1926      CALL xios_solve_inheritance()
1927   END SUBROUTINE iom_set_grid_attr
1928
1929   SUBROUTINE iom_setkt( kt, cdname )
1930      !!----------------------------------------------------------------------
1931      !!----------------------------------------------------------------------
1932      INTEGER         , INTENT(in) ::   kt 
1933      CHARACTER(LEN=*), INTENT(in) ::   cdname
1934      !!----------------------------------------------------------------------
1935      CALL iom_swap( cdname )   ! swap to cdname context
1936      CALL xios_update_calendar(kt)
1937      IF( cdname /= TRIM(cxios_context) )   CALL iom_swap( TRIM(cxios_context) )   ! return back to nemo context
1938   END SUBROUTINE iom_setkt
1939
1940   SUBROUTINE iom_context_finalize( cdname )
1941      !!----------------------------------------------------------------------
1942      !!----------------------------------------------------------------------
1943      CHARACTER(LEN=*), INTENT(in) :: cdname
1944      CHARACTER(LEN=120)           :: clname
1945      !!----------------------------------------------------------------------
1946      clname = cdname
1947      IF( TRIM(Agrif_CFixed()) .NE. '0' ) clname = TRIM(Agrif_CFixed())//"_"//clname 
1948      IF( xios_is_valid_context(clname) ) THEN
1949         CALL iom_swap( cdname )   ! swap to cdname context
1950         CALL xios_context_finalize() ! finalize the context
1951         IF( cdname /= TRIM(cxios_context) ) CALL iom_swap( TRIM(cxios_context) )   ! return back to nemo context
1952      ENDIF
1953      !
1954   END SUBROUTINE iom_context_finalize
1955
1956
1957   SUBROUTINE set_grid( cdgrd, plon, plat, ldxios, ldrxios )
1958      !!----------------------------------------------------------------------
1959      !!                     ***  ROUTINE set_grid  ***
1960      !!
1961      !! ** Purpose :   define horizontal grids
1962      !!----------------------------------------------------------------------
1963      CHARACTER(LEN=1)            , INTENT(in) ::   cdgrd
1964      REAL(wp), DIMENSION(jpi,jpj), INTENT(in) ::   plon
1965      REAL(wp), DIMENSION(jpi,jpj), INTENT(in) ::   plat
1966      !
1967      INTEGER  :: ni, nj
1968      REAL(wp), DIMENSION(jpi,jpj,jpk) ::   zmask
1969      LOGICAL, INTENT(IN) :: ldxios, ldrxios
1970      !!----------------------------------------------------------------------
1971      !
1972      ni = nlei-nldi+1
1973      nj = nlej-nldj+1
1974      !
1975      CALL iom_set_domain_attr("grid_"//cdgrd, ni_glo=jpiglo, nj_glo=jpjglo, ibegin=nimpp+nldi-2, jbegin=njmpp+nldj-2, ni=ni, nj=nj)
1976      CALL iom_set_domain_attr("grid_"//cdgrd, data_dim=2, data_ibegin = 1-nldi, data_ni = jpi, data_jbegin = 1-nldj, data_nj = jpj)
1977!don't define lon and lat for restart reading context.
1978      IF ( .NOT.ldrxios ) &
1979         CALL iom_set_domain_attr("grid_"//cdgrd, lonvalue = RESHAPE(plon(nldi:nlei, nldj:nlej),(/ ni*nj /)),   &
1980         &                                     latvalue = RESHAPE(plat(nldi:nlei, nldj:nlej),(/ ni*nj /))) 
1981      !
1982      IF ( ln_mskland .AND. (.NOT.ldxios) ) THEN
1983         ! mask land points, keep values on coast line -> specific mask for U, V and W points
1984         SELECT CASE ( cdgrd )
1985         CASE('T')   ;   zmask(:,:,:)       = tmask(:,:,:)
1986         CASE('U')   ;   zmask(2:jpim1,:,:) = tmask(2:jpim1,:,:) + tmask(3:jpi,:,:)   ;   CALL lbc_lnk( 'iom', zmask, 'U', 1. )
1987         CASE('V')   ;   zmask(:,2:jpjm1,:) = tmask(:,2:jpjm1,:) + tmask(:,3:jpj,:)   ;   CALL lbc_lnk( 'iom', zmask, 'V', 1. )
1988         CASE('W')   ;   zmask(:,:,2:jpk  ) = tmask(:,:,1:jpkm1) + tmask(:,:,2:jpk)   ;   zmask(:,:,1) = tmask(:,:,1)
1989         END SELECT
1990         !
1991         CALL iom_set_domain_attr( "grid_"//cdgrd       , mask = RESHAPE(zmask(nldi:nlei,nldj:nlej,1),(/ni*nj    /)) /= 0. )
1992         CALL iom_set_grid_attr  ( "grid_"//cdgrd//"_3D", mask = RESHAPE(zmask(nldi:nlei,nldj:nlej,:),(/ni,nj,jpk/)) /= 0. )
1993      ENDIF
1994      !
1995   END SUBROUTINE set_grid
1996
1997
1998   SUBROUTINE set_grid_bounds( cdgrd, plon_cnr, plat_cnr, plon_pnt, plat_pnt )
1999      !!----------------------------------------------------------------------
2000      !!                   ***  ROUTINE set_grid_bounds  ***
2001      !!
2002      !! ** Purpose :   define horizontal grid corners
2003      !!
2004      !!----------------------------------------------------------------------
2005      CHARACTER(LEN=1)                      , INTENT(in) :: cdgrd
2006      REAL(wp), DIMENSION(jpi,jpj)          , INTENT(in) :: plon_cnr, plat_cnr  ! Lat/lon coord. of a contiguous vertex of cell (i,j)
2007      REAL(wp), DIMENSION(jpi,jpj), OPTIONAL, INTENT(in) :: plon_pnt, plat_pnt  ! Lat/lon coord. of the point of cell (i,j)
2008      !
2009      INTEGER :: ji, jj, jn, ni, nj
2010      INTEGER :: icnr, jcnr                             ! Offset such that the vertex coordinate (i+icnr,j+jcnr)
2011      !                                                 ! represents the bottom-left corner of cell (i,j)
2012      REAL(wp), ALLOCATABLE, DIMENSION(:,:,:,:) :: z_bnds      ! Lat/lon coordinates of the vertices of cell (i,j)
2013      REAL(wp), ALLOCATABLE, DIMENSION(:,:)     :: z_fld       ! Working array to determine where to rotate cells
2014      REAL(wp), ALLOCATABLE, DIMENSION(:,:)     :: z_rot       ! Lat/lon working array for rotation of cells
2015      !!----------------------------------------------------------------------
2016      !
2017      ALLOCATE( z_bnds(4,jpi,jpj,2), z_fld(jpi,jpj), z_rot(4,2)  )
2018      !
2019      ! Offset of coordinate representing bottom-left corner
2020      SELECT CASE ( TRIM(cdgrd) )
2021      CASE ('T', 'W')   ;   icnr = -1   ;   jcnr = -1
2022      CASE ('U')        ;   icnr =  0   ;   jcnr = -1
2023      CASE ('V')        ;   icnr = -1   ;   jcnr =  0
2024      END SELECT
2025      !
2026      ni = nlei-nldi+1   ! Dimensions of subdomain interior
2027      nj = nlej-nldj+1
2028      !
2029      z_fld(:,:) = 1._wp
2030      CALL lbc_lnk( 'iom', z_fld, cdgrd, -1. )    ! Working array for location of northfold
2031      !
2032      ! Cell vertices that can be defined
2033      DO jj = 2, jpjm1
2034         DO ji = 2, jpim1
2035            z_bnds(1,ji,jj,1) = plat_cnr(ji+icnr,  jj+jcnr  ) ! Bottom-left
2036            z_bnds(2,ji,jj,1) = plat_cnr(ji+icnr+1,jj+jcnr  ) ! Bottom-right
2037            z_bnds(3,ji,jj,1) = plat_cnr(ji+icnr+1,jj+jcnr+1) ! Top-right
2038            z_bnds(4,ji,jj,1) = plat_cnr(ji+icnr,  jj+jcnr+1) ! Top-left
2039            z_bnds(1,ji,jj,2) = plon_cnr(ji+icnr,  jj+jcnr  ) ! Bottom-left
2040            z_bnds(2,ji,jj,2) = plon_cnr(ji+icnr+1,jj+jcnr  ) ! Bottom-right
2041            z_bnds(3,ji,jj,2) = plon_cnr(ji+icnr+1,jj+jcnr+1) ! Top-right
2042            z_bnds(4,ji,jj,2) = plon_cnr(ji+icnr,  jj+jcnr+1) ! Top-left
2043         END DO
2044      END DO
2045      !
2046      ! Cell vertices on boundries
2047      DO jn = 1, 4
2048         CALL lbc_lnk( 'iom', z_bnds(jn,:,:,1), cdgrd, 1., pfillval=999._wp )
2049         CALL lbc_lnk( 'iom', z_bnds(jn,:,:,2), cdgrd, 1., pfillval=999._wp )
2050      END DO
2051      !
2052      ! Zero-size cells at closed boundaries if cell points provided,
2053      ! otherwise they are closed cells with unrealistic bounds
2054      IF( PRESENT(plon_pnt) .AND. PRESENT(plat_pnt) ) THEN
2055         IF( (nbondi == -1 .OR. nbondi == 2) .AND. .NOT. (jperio == 1 .OR. jperio == 4 .OR. jperio == 6) ) THEN
2056            DO jn = 1, 4        ! (West or jpni = 1), closed E-W
2057               z_bnds(jn,1,:,1) = plat_pnt(1,:)  ;  z_bnds(jn,1,:,2) = plon_pnt(1,:)
2058            END DO
2059         ENDIF
2060         IF( (nbondi == 1 .OR. nbondi == 2) .AND. .NOT. (jperio == 1 .OR. jperio == 4 .OR. jperio == 6) ) THEN
2061            DO jn = 1, 4        ! (East or jpni = 1), closed E-W
2062               z_bnds(jn,nlci,:,1) = plat_pnt(nlci,:)  ;  z_bnds(jn,nlci,:,2) = plon_pnt(nlci,:)
2063            END DO
2064         ENDIF
2065         IF( nbondj == -1 .OR. (nbondj == 2 .AND. jperio /= 2) ) THEN
2066            DO jn = 1, 4        ! South or (jpnj = 1, not symmetric)
2067               z_bnds(jn,:,1,1) = plat_pnt(:,1)  ;  z_bnds(jn,:,1,2) = plon_pnt(:,1)
2068            END DO
2069         ENDIF
2070         IF( (nbondj == 1 .OR. nbondj == 2) .AND. jperio  < 3 ) THEN
2071            DO jn = 1, 4        ! (North or jpnj = 1), no north fold
2072               z_bnds(jn,:,nlcj,1) = plat_pnt(:,nlcj)  ;  z_bnds(jn,:,nlcj,2) = plon_pnt(:,nlcj)
2073            END DO
2074         ENDIF
2075      ENDIF
2076      !
2077      IF( (nbondj == 1 .OR. nbondj == 2) .AND. jperio >= 3 ) THEN    ! Rotate cells at the north fold
2078         DO jj = 1, jpj
2079            DO ji = 1, jpi
2080               IF( z_fld(ji,jj) == -1. ) THEN
2081                  z_rot(1,:) = z_bnds(3,ji,jj,:) ; z_rot(2,:) = z_bnds(4,ji,jj,:)
2082                  z_rot(3,:) = z_bnds(1,ji,jj,:) ; z_rot(4,:) = z_bnds(2,ji,jj,:)
2083                  z_bnds(:,ji,jj,:) = z_rot(:,:)
2084               ENDIF
2085            END DO
2086         END DO
2087      ELSE IF( nbondj == 2 .AND. jperio == 2 ) THEN                  ! Invert cells at the symmetric equator
2088         DO ji = 1, jpi
2089            z_rot(1:2,:) = z_bnds(3:4,ji,1,:)
2090            z_rot(3:4,:) = z_bnds(1:2,ji,1,:)
2091            z_bnds(:,ji,1,:) = z_rot(:,:)
2092         END DO
2093      ENDIF
2094      !
2095      CALL iom_set_domain_attr("grid_"//cdgrd, bounds_lat = RESHAPE(z_bnds(:,nldi:nlei,nldj:nlej,1),(/ 4,ni*nj /)),           &
2096          &                                    bounds_lon = RESHAPE(z_bnds(:,nldi:nlei,nldj:nlej,2),(/ 4,ni*nj /)), nvertex=4 )
2097      !
2098      DEALLOCATE( z_bnds, z_fld, z_rot ) 
2099      !
2100   END SUBROUTINE set_grid_bounds
2101
2102
2103   SUBROUTINE set_grid_znl( plat )
2104      !!----------------------------------------------------------------------
2105      !!                     ***  ROUTINE set_grid_znl  ***
2106      !!
2107      !! ** Purpose :   define grids for zonal mean
2108      !!
2109      !!----------------------------------------------------------------------
2110      REAL(wp), DIMENSION(jpi,jpj), INTENT(in) ::   plat
2111      !
2112      INTEGER  :: ni, nj, ix, iy
2113      REAL(wp), DIMENSION(:), ALLOCATABLE  ::   zlon
2114      !!----------------------------------------------------------------------
2115      !
2116      ni=nlei-nldi+1       ! define zonal mean domain (jpj*jpk)
2117      nj=nlej-nldj+1
2118      ALLOCATE( zlon(ni*nj) )       ;       zlon(:) = 0._wp
2119      !
2120!      CALL dom_ngb( -168.53, 65.03, ix, iy, 'T' ) !  i-line that passes through Bering Strait: Reference latitude (used in plots)
2121      CALL dom_ngb( 180., 90., ix, iy, 'T' ) !  i-line that passes near the North Pole : Reference latitude (used in plots)
2122      CALL iom_set_domain_attr("gznl", ni_glo=jpiglo, nj_glo=jpjglo, ibegin=nimpp+nldi-2, jbegin=njmpp+nldj-2, ni=ni, nj=nj)
2123      CALL iom_set_domain_attr("gznl", data_dim=2, data_ibegin = 1-nldi, data_ni = jpi, data_jbegin = 1-nldj, data_nj = jpj)
2124      CALL iom_set_domain_attr("gznl", lonvalue = zlon,   &
2125         &                             latvalue = RESHAPE(plat(nldi:nlei, nldj:nlej),(/ ni*nj /))) 
2126      CALL iom_set_zoom_domain_attr("ptr", ibegin=ix-1, jbegin=0, ni=1, nj=jpjglo)
2127      !
2128      CALL iom_update_file_name('ptr')
2129      !
2130   END SUBROUTINE set_grid_znl
2131
2132
2133   SUBROUTINE set_scalar
2134      !!----------------------------------------------------------------------
2135      !!                     ***  ROUTINE set_scalar  ***
2136      !!
2137      !! ** Purpose :   define fake grids for scalar point
2138      !!
2139      !!----------------------------------------------------------------------
2140      REAL(wp), DIMENSION(1)   ::   zz = 1.
2141      !!----------------------------------------------------------------------
2142      !
2143      CALL iom_set_domain_attr('scalarpoint', ni_glo=jpnij, nj_glo=1, ibegin=narea-1, jbegin=0, ni=1, nj=1)
2144      CALL iom_set_domain_attr('scalarpoint', data_dim=2, data_ibegin = 1, data_ni = 1, data_jbegin = 1, data_nj = 1)
2145      !
2146      zz = REAL( narea, wp )
2147      CALL iom_set_domain_attr('scalarpoint', lonvalue=zz, latvalue=zz)
2148      !
2149   END SUBROUTINE set_scalar
2150
2151
2152   SUBROUTINE set_xmlatt
2153      !!----------------------------------------------------------------------
2154      !!                     ***  ROUTINE set_xmlatt  ***
2155      !!
2156      !! ** Purpose :   automatic definitions of some of the xml attributs...
2157      !!
2158      !!----------------------------------------------------------------------
2159      CHARACTER(len=1),DIMENSION( 3) ::   clgrd                    ! suffix name
2160      CHARACTER(len=256)             ::   clsuff                   ! suffix name
2161      CHARACTER(len=1)               ::   cl1                      ! 1 character
2162      CHARACTER(len=2)               ::   cl2                      ! 2 characters
2163      CHARACTER(len=3)               ::   cl3                      ! 3 characters
2164      INTEGER                        ::   ji, jg                   ! loop counters
2165      INTEGER                        ::   ix, iy                   ! i-,j- index
2166      REAL(wp)        ,DIMENSION(11) ::   zlontao                  ! longitudes of tao    moorings
2167      REAL(wp)        ,DIMENSION( 7) ::   zlattao                  ! latitudes  of tao    moorings
2168      REAL(wp)        ,DIMENSION( 4) ::   zlonrama                 ! longitudes of rama   moorings
2169      REAL(wp)        ,DIMENSION(11) ::   zlatrama                 ! latitudes  of rama   moorings
2170      REAL(wp)        ,DIMENSION( 3) ::   zlonpira                 ! longitudes of pirata moorings
2171      REAL(wp)        ,DIMENSION( 9) ::   zlatpira                 ! latitudes  of pirata moorings
2172      TYPE(xios_duration)            ::   f_op, f_of
2173      !!----------------------------------------------------------------------
2174      !
2175      ! frequency of the call of iom_put (attribut: freq_op)
2176      f_op%timestep = 1        ;  f_of%timestep =  0  ; CALL iom_set_field_attr('field_definition', freq_op=f_op, freq_offset=f_of)
2177      f_op%timestep = 2        ;  f_of%timestep =  0  ; CALL iom_set_field_attr('trendT_even'     , freq_op=f_op, freq_offset=f_of)
2178      f_op%timestep = 2        ;  f_of%timestep = -1  ; CALL iom_set_field_attr('trendT_odd'      , freq_op=f_op, freq_offset=f_of)
2179      f_op%timestep = nn_fsbc  ;  f_of%timestep =  0  ; CALL iom_set_field_attr('SBC'             , freq_op=f_op, freq_offset=f_of)
2180      f_op%timestep = nn_fsbc  ;  f_of%timestep =  0  ; CALL iom_set_field_attr('SBC_scalar'      , freq_op=f_op, freq_offset=f_of)
2181      f_op%timestep = nn_fsbc  ;  f_of%timestep =  0  ; CALL iom_set_field_attr('ABL'             , freq_op=f_op, freq_offset=f_of)
2182
2183      ! output file names (attribut: name)
2184      DO ji = 1, 9
2185         WRITE(cl1,'(i1)') ji 
2186         CALL iom_update_file_name('file'//cl1)
2187      END DO
2188      DO ji = 1, 99
2189         WRITE(cl2,'(i2.2)') ji 
2190         CALL iom_update_file_name('file'//cl2)
2191      END DO
2192      DO ji = 1, 999
2193         WRITE(cl3,'(i3.3)') ji 
2194         CALL iom_update_file_name('file'//cl3)
2195      END DO
2196
2197      ! Zooms...
2198      clgrd = (/ 'T', 'U', 'W' /) 
2199      DO jg = 1, SIZE(clgrd)                                                                   ! grid type
2200         cl1 = clgrd(jg)
2201         ! Equatorial section (attributs: jbegin, ni, name_suffix)
2202         CALL dom_ngb( 0., 0., ix, iy, cl1 )
2203         CALL iom_set_zoom_domain_attr('Eq'//cl1, ibegin=0, jbegin=iy-1, ni=jpiglo, nj=1 )
2204         CALL iom_get_file_attr   ('Eq'//cl1, name_suffix = clsuff             )
2205         CALL iom_set_file_attr   ('Eq'//cl1, name_suffix = TRIM(clsuff)//'_Eq')
2206         CALL iom_update_file_name('Eq'//cl1)
2207      END DO
2208      ! TAO moorings (attributs: ibegin, jbegin, name_suffix)
2209      zlontao = (/ 137.0, 147.0, 156.0, 165.0, -180.0, -170.0, -155.0, -140.0, -125.0, -110.0, -95.0 /)
2210      zlattao = (/  -8.0,  -5.0,  -2.0,   0.0,    2.0,    5.0,    8.0 /)
2211      CALL set_mooring( zlontao, zlattao )
2212      ! RAMA moorings (attributs: ibegin, jbegin, name_suffix)
2213      zlonrama = (/  55.0,  67.0, 80.5, 90.0 /)
2214      zlatrama = (/ -16.0, -12.0, -8.0, -4.0, -1.5, 0.0, 1.5, 4.0, 8.0, 12.0, 15.0 /)
2215      CALL set_mooring( zlonrama, zlatrama )
2216      ! PIRATA moorings (attributs: ibegin, jbegin, name_suffix)
2217      zlonpira = (/ -38.0, -23.0, -10.0 /)
2218      zlatpira = (/ -19.0, -14.0,  -8.0, 0.0, 4.0, 8.0, 12.0, 15.0, 20.0 /)
2219      CALL set_mooring( zlonpira, zlatpira )
2220      !
2221   END SUBROUTINE set_xmlatt
2222
2223
2224   SUBROUTINE set_mooring( plon, plat )
2225      !!----------------------------------------------------------------------
2226      !!                     ***  ROUTINE set_mooring  ***
2227      !!
2228      !! ** Purpose :   automatic definitions of moorings xml attributs...
2229      !!
2230      !!----------------------------------------------------------------------
2231      REAL(wp), DIMENSION(:), INTENT(in) ::   plon, plat   ! longitudes/latitudes oft the mooring
2232      !
2233!!$      CHARACTER(len=1),DIMENSION(4) ::   clgrd = (/ 'T', 'U', 'V', 'W' /)   ! suffix name
2234      CHARACTER(len=1),DIMENSION(1) ::   clgrd = (/ 'T' /)        ! suffix name
2235      CHARACTER(len=256)            ::   clname                   ! file name
2236      CHARACTER(len=256)            ::   clsuff                   ! suffix name
2237      CHARACTER(len=1)              ::   cl1                      ! 1 character
2238      CHARACTER(len=6)              ::   clon,clat                ! name of longitude, latitude
2239      INTEGER                       ::   ji, jj, jg               ! loop counters
2240      INTEGER                       ::   ix, iy                   ! i-,j- index
2241      REAL(wp)                      ::   zlon, zlat
2242      !!----------------------------------------------------------------------
2243      DO jg = 1, SIZE(clgrd)
2244         cl1 = clgrd(jg)
2245         DO ji = 1, SIZE(plon)
2246            DO jj = 1, SIZE(plat)
2247               zlon = plon(ji)
2248               zlat = plat(jj)
2249               ! modifications for RAMA moorings
2250               IF( zlon ==  67. .AND. zlat ==  15. )   zlon =  65.
2251               IF( zlon ==  90. .AND. zlat <=  -4. )   zlon =  95.
2252               IF( zlon ==  95. .AND. zlat ==  -4. )   zlat =  -5.
2253               ! modifications for PIRATA moorings
2254               IF( zlon == -38. .AND. zlat == -19. )   zlon = -34.
2255               IF( zlon == -38. .AND. zlat == -14. )   zlon = -32.
2256               IF( zlon == -38. .AND. zlat ==  -8. )   zlon = -30.
2257               IF( zlon == -38. .AND. zlat ==   0. )   zlon = -35.
2258               IF( zlon == -23. .AND. zlat ==  20. )   zlat =  21.
2259               IF( zlon == -10. .AND. zlat == -14. )   zlat = -10.
2260               IF( zlon == -10. .AND. zlat ==  -8. )   zlat =  -6.
2261               IF( zlon == -10. .AND. zlat ==   4. ) THEN   ;   zlon = 0.   ;   zlat = 0.   ;   ENDIF
2262               CALL dom_ngb( zlon, zlat, ix, iy, cl1 )
2263               IF( zlon >= 0. ) THEN 
2264                  IF( zlon == REAL(NINT(zlon), wp) ) THEN   ;   WRITE(clon, '(i3,  a)') NINT( zlon), 'e'
2265                  ELSE                                      ;   WRITE(clon, '(f5.1,a)')       zlon , 'e'
2266                  ENDIF
2267               ELSE             
2268                  IF( zlon == REAL(NINT(zlon), wp) ) THEN   ;   WRITE(clon, '(i3,  a)') NINT(-zlon), 'w'
2269                  ELSE                                      ;   WRITE(clon, '(f5.1,a)')      -zlon , 'w'
2270                  ENDIF
2271               ENDIF
2272               IF( zlat >= 0. ) THEN 
2273                  IF( zlat == REAL(NINT(zlat), wp) ) THEN   ;   WRITE(clat, '(i2,  a)') NINT( zlat), 'n'
2274                  ELSE                                      ;   WRITE(clat, '(f4.1,a)')       zlat , 'n'
2275                  ENDIF
2276               ELSE             
2277                  IF( zlat == REAL(NINT(zlat), wp) ) THEN   ;   WRITE(clat, '(i2,  a)') NINT(-zlat), 's'
2278                  ELSE                                      ;   WRITE(clat, '(f4.1,a)')      -zlat , 's'
2279                  ENDIF
2280               ENDIF
2281               clname = TRIM(ADJUSTL(clat))//TRIM(ADJUSTL(clon))
2282               CALL iom_set_zoom_domain_attr(TRIM(clname)//cl1, ibegin= ix-1, jbegin= iy-1, ni=1, nj=1)
2283
2284               CALL iom_get_file_attr   (TRIM(clname)//cl1, name_suffix = clsuff                         )
2285               CALL iom_set_file_attr   (TRIM(clname)//cl1, name_suffix = TRIM(clsuff)//'_'//TRIM(clname))
2286               CALL iom_update_file_name(TRIM(clname)//cl1)
2287            END DO
2288         END DO
2289      END DO
2290     
2291   END SUBROUTINE set_mooring
2292
2293   
2294   SUBROUTINE iom_update_file_name( cdid )
2295      !!----------------------------------------------------------------------
2296      !!                     ***  ROUTINE iom_update_file_name  ***
2297      !!
2298      !! ** Purpose :   
2299      !!
2300      !!----------------------------------------------------------------------
2301      CHARACTER(LEN=*)          , INTENT(in) ::   cdid
2302      !
2303      CHARACTER(LEN=256) ::   clname
2304      CHARACTER(LEN=20)  ::   clfreq
2305      CHARACTER(LEN=20)  ::   cldate
2306      INTEGER            ::   idx
2307      INTEGER            ::   jn
2308      INTEGER            ::   itrlen
2309      INTEGER            ::   iyear, imonth, iday, isec
2310      REAL(wp)           ::   zsec
2311      LOGICAL            ::   llexist
2312      TYPE(xios_duration)   ::   output_freq 
2313      !!----------------------------------------------------------------------
2314      !
2315      DO jn = 1, 2
2316         !
2317         output_freq = xios_duration(0,0,0,0,0,0)
2318         IF( jn == 1 )   CALL iom_get_file_attr( cdid, name        = clname, output_freq = output_freq )
2319         IF( jn == 2 )   CALL iom_get_file_attr( cdid, name_suffix = clname )
2320         !
2321         IF ( TRIM(clname) /= '' ) THEN 
2322            !
2323            idx = INDEX(clname,'@expname@') + INDEX(clname,'@EXPNAME@')
2324            DO WHILE ( idx /= 0 ) 
2325               clname = clname(1:idx-1)//TRIM(cexper)//clname(idx+9:LEN_TRIM(clname))
2326               idx = INDEX(clname,'@expname@') + INDEX(clname,'@EXPNAME@')
2327            END DO
2328            !
2329            idx = INDEX(clname,'@freq@') + INDEX(clname,'@FREQ@')
2330            DO WHILE ( idx /= 0 ) 
2331              IF ( output_freq%timestep /= 0) THEN
2332                  WRITE(clfreq,'(I18,A2)')INT(output_freq%timestep),'ts' 
2333                  itrlen = LEN_TRIM(ADJUSTL(clfreq))
2334              ELSE IF ( output_freq%second /= 0 ) THEN
2335                  WRITE(clfreq,'(I19,A1)')INT(output_freq%second),'s' 
2336                  itrlen = LEN_TRIM(ADJUSTL(clfreq))
2337              ELSE IF ( output_freq%minute /= 0 ) THEN
2338                  WRITE(clfreq,'(I18,A2)')INT(output_freq%minute),'mi' 
2339                  itrlen = LEN_TRIM(ADJUSTL(clfreq))
2340              ELSE IF ( output_freq%hour /= 0 ) THEN
2341                  WRITE(clfreq,'(I19,A1)')INT(output_freq%hour),'h' 
2342                  itrlen = LEN_TRIM(ADJUSTL(clfreq))
2343              ELSE IF ( output_freq%day /= 0 ) THEN
2344                  WRITE(clfreq,'(I19,A1)')INT(output_freq%day),'d' 
2345                  itrlen = LEN_TRIM(ADJUSTL(clfreq))
2346              ELSE IF ( output_freq%month /= 0 ) THEN   
2347                  WRITE(clfreq,'(I19,A1)')INT(output_freq%month),'m' 
2348                  itrlen = LEN_TRIM(ADJUSTL(clfreq))
2349              ELSE IF ( output_freq%year /= 0 ) THEN   
2350                  WRITE(clfreq,'(I19,A1)')INT(output_freq%year),'y' 
2351                  itrlen = LEN_TRIM(ADJUSTL(clfreq))
2352              ELSE
2353                  CALL ctl_stop('error in the name of file id '//TRIM(cdid),   &
2354                     & ' attribute output_freq is undefined -> cannot replace @freq@ in '//TRIM(clname) )
2355              ENDIF
2356              clname = clname(1:idx-1)//TRIM(ADJUSTL(clfreq))//clname(idx+6:LEN_TRIM(clname))
2357              idx = INDEX(clname,'@freq@') + INDEX(clname,'@FREQ@')
2358            END DO
2359            !
2360            idx = INDEX(clname,'@startdate@') + INDEX(clname,'@STARTDATE@')
2361            DO WHILE ( idx /= 0 ) 
2362               cldate = iom_sdate( fjulday - rn_Dt / rday )
2363               clname = clname(1:idx-1)//TRIM(cldate)//clname(idx+11:LEN_TRIM(clname))
2364               idx = INDEX(clname,'@startdate@') + INDEX(clname,'@STARTDATE@')
2365            END DO
2366            !
2367            idx = INDEX(clname,'@startdatefull@') + INDEX(clname,'@STARTDATEFULL@')
2368            DO WHILE ( idx /= 0 ) 
2369               cldate = iom_sdate( fjulday - rn_Dt / rday, ldfull = .TRUE. )
2370               clname = clname(1:idx-1)//TRIM(cldate)//clname(idx+15:LEN_TRIM(clname))
2371               idx = INDEX(clname,'@startdatefull@') + INDEX(clname,'@STARTDATEFULL@')
2372            END DO
2373            !
2374            idx = INDEX(clname,'@enddate@') + INDEX(clname,'@ENDDATE@')
2375            DO WHILE ( idx /= 0 ) 
2376               cldate = iom_sdate( fjulday + rn_Dt / rday * REAL( nitend - nit000, wp ), ld24 = .TRUE. )
2377               clname = clname(1:idx-1)//TRIM(cldate)//clname(idx+9:LEN_TRIM(clname))
2378               idx = INDEX(clname,'@enddate@') + INDEX(clname,'@ENDDATE@')
2379            END DO
2380            !
2381            idx = INDEX(clname,'@enddatefull@') + INDEX(clname,'@ENDDATEFULL@')
2382            DO WHILE ( idx /= 0 ) 
2383               cldate = iom_sdate( fjulday + rn_Dt / rday * REAL( nitend - nit000, wp ), ld24 = .TRUE., ldfull = .TRUE. )
2384               clname = clname(1:idx-1)//TRIM(cldate)//clname(idx+13:LEN_TRIM(clname))
2385               idx = INDEX(clname,'@enddatefull@') + INDEX(clname,'@ENDDATEFULL@')
2386            END DO
2387            !
2388            IF( jn == 1 .AND. TRIM(Agrif_CFixed()) /= '0' )   clname = TRIM(Agrif_CFixed())//"_"//TRIM(clname)
2389            IF( jn == 1 )   CALL iom_set_file_attr( cdid, name        = clname )
2390            IF( jn == 2 )   CALL iom_set_file_attr( cdid, name_suffix = clname )
2391            !
2392         ENDIF
2393         !
2394      END DO
2395      !
2396   END SUBROUTINE iom_update_file_name
2397
2398
2399   FUNCTION iom_sdate( pjday, ld24, ldfull )
2400      !!----------------------------------------------------------------------
2401      !!                     ***  ROUTINE iom_sdate  ***
2402      !!
2403      !! ** Purpose :   send back the date corresponding to the given julian day
2404      !!----------------------------------------------------------------------
2405      REAL(wp), INTENT(in   )           ::   pjday    ! julian day
2406      LOGICAL , INTENT(in   ), OPTIONAL ::   ld24     ! true to force 24:00 instead of 00:00
2407      LOGICAL , INTENT(in   ), OPTIONAL ::   ldfull   ! true to get the compleate date: yyyymmdd_hh:mm:ss
2408      !
2409      CHARACTER(LEN=20) ::   iom_sdate
2410      CHARACTER(LEN=50) ::   clfmt                         !  format used to write the date
2411      INTEGER           ::   iyear, imonth, iday, ihour, iminute, isec
2412      REAL(wp)          ::   zsec
2413      LOGICAL           ::   ll24, llfull
2414      !!----------------------------------------------------------------------
2415      !
2416      IF( PRESENT(ld24) ) THEN   ;   ll24 = ld24
2417      ELSE                       ;   ll24 = .FALSE.
2418      ENDIF
2419      !
2420      IF( PRESENT(ldfull) ) THEN   ;   llfull = ldfull
2421      ELSE                         ;   llfull = .FALSE.
2422      ENDIF
2423      !
2424      CALL ju2ymds( pjday, iyear, imonth, iday, zsec )
2425      isec = NINT(zsec)
2426      !
2427      IF ( ll24 .AND. isec == 0 ) THEN   ! 00:00 of the next day -> move to 24:00 of the current day
2428         CALL ju2ymds( pjday - 1., iyear, imonth, iday, zsec )
2429         isec = 86400
2430      ENDIF
2431      !
2432      IF( iyear < 10000 ) THEN   ;   clfmt = "i4.4,2i2.2"                ! format used to write the date
2433      ELSE                       ;   WRITE(clfmt, "('i',i1,',2i2.2')") INT(LOG10(REAL(iyear,wp))) + 1
2434      ENDIF
2435      !
2436!$AGRIF_DO_NOT_TREAT     
2437      ! needed in the conv
2438      IF( llfull ) THEN
2439         clfmt = TRIM(clfmt)//",'_',i2.2,':',i2.2,':',i2.2"
2440         ihour   = isec / 3600
2441         isec    = MOD(isec, 3600)
2442         iminute = isec / 60
2443         isec    = MOD(isec, 60)
2444         WRITE(iom_sdate, '('//TRIM(clfmt)//')') iyear, imonth, iday, ihour, iminute, isec    ! date of the end of run
2445      ELSE
2446         WRITE(iom_sdate, '('//TRIM(clfmt)//')') iyear, imonth, iday                          ! date of the end of run
2447      ENDIF
2448!$AGRIF_END_DO_NOT_TREAT     
2449      !
2450   END FUNCTION iom_sdate
2451
2452#else
2453   !!----------------------------------------------------------------------
2454   !!   NOT 'key_iomput'                               a few dummy routines
2455   !!----------------------------------------------------------------------
2456   SUBROUTINE iom_setkt( kt, cdname )
2457      INTEGER         , INTENT(in)::   kt 
2458      CHARACTER(LEN=*), INTENT(in) ::   cdname
2459      IF( .FALSE. )   WRITE(numout,*) kt, cdname   ! useless test to avoid compilation warnings
2460   END SUBROUTINE iom_setkt
2461
2462   SUBROUTINE iom_context_finalize( cdname )
2463      CHARACTER(LEN=*), INTENT(in) ::   cdname
2464      IF( .FALSE. )   WRITE(numout,*)  cdname   ! useless test to avoid compilation warnings
2465   END SUBROUTINE iom_context_finalize
2466
2467   SUBROUTINE iom_update_file_name( cdid )
2468      CHARACTER(LEN=*), INTENT(in) ::   cdid
2469      IF( .FALSE. )   WRITE(numout,*)  cdid   ! useless test to avoid compilation warnings
2470   END SUBROUTINE iom_update_file_name
2471
2472#endif
2473
2474   LOGICAL FUNCTION iom_use( cdname )
2475      CHARACTER(LEN=*), INTENT(in) ::   cdname
2476#if defined key_iomput
2477      iom_use = xios_field_is_active( cdname )
2478#else
2479      iom_use = .FALSE.
2480#endif
2481   END FUNCTION iom_use
2482
2483   SUBROUTINE iom_miss_val( cdname, pmiss_val )
2484      CHARACTER(LEN=*), INTENT(in ) ::   cdname
2485      REAL(wp)        , INTENT(out) ::   pmiss_val   
2486#if defined key_iomput
2487      ! get missing value
2488      CALL xios_get_field_attr( cdname, default_value = pmiss_val )
2489#else
2490      IF( .FALSE. )   WRITE(numout,*) cdname, pmiss_val   ! useless test to avoid compilation warnings
2491      IF( .FALSE. )   pmiss_val = 0._wp                   ! useless assignment to avoid compilation warnings
2492#endif
2493   END SUBROUTINE iom_miss_val
2494 
2495   !!======================================================================
2496END MODULE iom
Note: See TracBrowser for help on using the repository browser.