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

source: NEMO/branches/2020/dev_12905_xios_restart/src/OCE/IOM/iom.F90 @ 12961

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

Ticket #2462: read/write restart with XIOS in TOP (with debug print statements)

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