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

source: NEMO/trunk/src/OCE/IOM/iom.F90 @ 14553

Last change on this file since 14553 was 14553, checked in by gsamson, 3 years ago

merge ticket2628_r14502_abl_restart_xios branch into trunk; sette identical between r14502 and r14544; ticket #2628

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