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

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

source: NEMO/branches/2020/dev_r12558_HPC-08_epico_Extra_Halo/src/OCE/IOM/iom.F90 @ 12939

Last change on this file since 12939 was 12939, checked in by smasson, 4 years ago

Extra_Halo: update with trunk@12933, see #2366

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