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

Last change on this file since 14219 was 14219, checked in by mcastril, 4 years ago

Add Mixed Precision support by Oriol Tintó

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