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_r13327_KERNEL-06_2_techene_e3/src/OCE/IOM – NEMO

source: NEMO/branches/2020/dev_r13327_KERNEL-06_2_techene_e3/src/OCE/IOM/iom.F90 @ 13512

Last change on this file since 13512 was 13512, checked in by techene, 4 years ago

#2385 add the possibility of having F grid files / variables (needed for SWE and maybe useful for having the vorticity at the right point)

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