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/releases/r4.0/r4.0-HEAD/src/OCE/IOM – NEMO

source: NEMO/releases/r4.0/r4.0-HEAD/src/OCE/IOM/iom.F90 @ 13280

Last change on this file since 13280 was 13280, checked in by smueller, 4 years ago

Remedy for the defect described in ticket #2376

This modification comprises the merging of changesets [12591] and [12602] made in branch /NEMO/branches/2020/ticket2377 as a remedy for the corresponding defect in the trunk version of NEMO (see ticket #2377).

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