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_r13296_HPC-07_mocavero_mpi3/src/OCE/IOM – NEMO

source: NEMO/branches/2020/dev_r13296_HPC-07_mocavero_mpi3/src/OCE/IOM/iom.F90 @ 13630

Last change on this file since 13630 was 13630, checked in by mocavero, 4 years ago

Add neighborhood collectives calls in the NEMO src - ticket #2496

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