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

source: NEMO/branches/2020/test_12905_xios_restart/src/OCE/IOM/iom.F90 @ 13034

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

Ticket #2462 update test branch with changes from dev branch

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