New URL for NEMO forge!   http://forge.nemo-ocean.eu

Since March 2022 along with NEMO 4.2 release, the code development moved to a self-hosted GitLab.
This present forge is now archived and remained online for history.
iom.F90 in NEMO/branches/2020/dev_r14116_HPC-04_mcastril_Mixed_Precision_implementation_final/src/OCE/IOM – NEMO

source: NEMO/branches/2020/dev_r14116_HPC-04_mcastril_Mixed_Precision_implementation_final/src/OCE/IOM/iom.F90 @ 14644

Last change on this file since 14644 was 14644, checked in by sparonuz, 3 years ago

Merge trunk -r14642:HEAD

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