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

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

Ticket #2462: remove commentted out testing lines

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