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/2020/dev_r12512_HPC-04_mcastril_Mixed_Precision_implementation/src/OCE/IOM – NEMO

source: NEMO/branches/2020/dev_r12512_HPC-04_mcastril_Mixed_Precision_implementation/src/OCE/IOM/iom.F90 @ 12603

Last change on this file since 12603 was 12603, checked in by orioltp, 4 years ago

Adding several interfaces to work with both single and double precision

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