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

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

source: NEMO/branches/UKMO/NEMO_4.0.2_ENHANCE-02_ISF_nemo/src/OCE/IOM/iom.F90 @ 12708

Last change on this file since 12708 was 12708, checked in by mathiot, 4 years ago

NEMO_4.0.2_ENHANCE-02_ISF_nemo: in sync with UKMO/NEMO_4.0.2_mirror (svn merge -r 12657:12658 /NEMO/branches/UKMO/NEMO_4.0.2_mirror)

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