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

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

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

Last change on this file since 12283 was 12283, checked in by cetlod, 4 years ago

trunk:revert some changes done in sbccpl.F90

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