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/dev_12905_xios_ancil/src/OCE/IOM – NEMO

source: NEMO/branches/2020/dev_12905_xios_ancil/src/OCE/IOM/iom.F90 @ 13016

Last change on this file since 13016 was 13016, checked in by andmirek, 4 years ago

Ticket #2475 implementation of new interface

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