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

Last change on this file since 11536 was 11536, checked in by smasson, 5 years ago

trunk: merge dev_r10984_HPC-13 into the trunk

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