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/2019/dev_ASINTER-01-05_merged/src/OCE/IOM – NEMO

source: NEMO/branches/2019/dev_ASINTER-01-05_merged/src/OCE/IOM/iom.F90 @ 12165

Last change on this file since 12165 was 11857, checked in by gsamson, 4 years ago

dev_r11265_ABL: bulks compatibility with 3D atmospheric forcing (by reading only the first level) (#2131)

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