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/2018/dev_r10164_HPC09_ESIWACE_PREP_MERGE/src/OCE/IOM – NEMO

source: NEMO/branches/2018/dev_r10164_HPC09_ESIWACE_PREP_MERGE/src/OCE/IOM/iom.F90 @ 10397

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

dev_r10164_HPC09_ESIWACE_PREP_MERGE: action 5 introduce mpp_delay_sum, see #2133

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