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

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

source: NEMO/branches/UKMO/dev_r10448_bdyvol/src/OCE/IOM/iom.F90 @ 10455

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

trunk: merge back dev_r10164_HPC09_ESIWACE_PREP_MERGE@10424 into the trunk

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