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

source: NEMO/branches/2020/ticket2529/src/OCE/IOM/iom.F90 @ 13713

Last change on this file since 13713 was 13713, checked in by ayoung, 3 years ago

Changed ptr bounds from local to global. See ticket #2529.

  • Property svn:keywords set to Id
File size: 139.7 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( lk_c1d .AND. PRESENT(pv_r2d) )   CALL lbc_lnk( 'iom', pv_r2d,'Z',1.0_wp )
1383      IF( lk_c1d .AND. PRESENT(pv_r3d) )   CALL lbc_lnk( 'iom', pv_r3d,'Z',1.0_wp )
1384
1385      !--- Apply scale_factor and offset
1386      zscf = iom_file(kiomid)%scf(idvar)      ! scale factor
1387      zofs = iom_file(kiomid)%ofs(idvar)      ! offset
1388      IF(     PRESENT(pv_r1d) ) THEN
1389         IF( zscf /= 1. )   pv_r1d(:) = pv_r1d(:) * zscf 
1390         IF( zofs /= 0. )   pv_r1d(:) = pv_r1d(:) + zofs
1391      ELSEIF( PRESENT(pv_r2d) ) THEN
1392         IF( zscf /= 1.)   pv_r2d(:,:) = pv_r2d(:,:) * zscf
1393         IF( zofs /= 0.)   pv_r2d(:,:) = pv_r2d(:,:) + zofs
1394      ELSEIF( PRESENT(pv_r3d) ) THEN
1395         IF( zscf /= 1.)   pv_r3d(:,:,:) = pv_r3d(:,:,:) * zscf
1396         IF( zofs /= 0.)   pv_r3d(:,:,:) = pv_r3d(:,:,:) + zofs
1397      ENDIF
1398      !
1399   END SUBROUTINE iom_get_123d
1400
1401   SUBROUTINE iom_get_var( cdname, z2d)
1402      CHARACTER(LEN=*), INTENT(in ) ::   cdname
1403      REAL(wp), DIMENSION(jpi,jpj) ::   z2d 
1404#if defined key_iomput
1405      IF( xios_field_is_active( cdname, at_current_timestep_arg = .TRUE. ) ) THEN
1406         z2d(:,:) = 0._wp
1407         CALL xios_recv_field( cdname, z2d)
1408      ENDIF
1409#else
1410      IF( .FALSE. )   WRITE(numout,*) cdname, z2d ! useless test to avoid compilation warnings
1411#endif
1412   END SUBROUTINE iom_get_var
1413
1414
1415   FUNCTION iom_getszuld ( kiomid ) 
1416      !!-----------------------------------------------------------------------
1417      !!                  ***  FUNCTION  iom_getszuld  ***
1418      !!
1419      !! ** Purpose : get the size of the unlimited dimension in a file
1420      !!              (return -1 if not found)
1421      !!-----------------------------------------------------------------------
1422      INTEGER, INTENT(in   ) ::   kiomid   ! file Identifier
1423      !
1424      INTEGER                ::   iom_getszuld
1425      !!-----------------------------------------------------------------------
1426      iom_getszuld = -1
1427      IF( kiomid > 0 ) THEN
1428         IF( iom_file(kiomid)%iduld > 0 )   iom_getszuld = iom_file(kiomid)%lenuld
1429      ENDIF
1430   END FUNCTION iom_getszuld
1431   
1432
1433   !!----------------------------------------------------------------------
1434   !!                   INTERFACE iom_chkatt
1435   !!----------------------------------------------------------------------
1436   SUBROUTINE iom_chkatt( kiomid, cdatt, llok, ksize, cdvar )
1437      INTEGER         , INTENT(in   )                 ::   kiomid    ! Identifier of the file
1438      CHARACTER(len=*), INTENT(in   )                 ::   cdatt     ! Name of the attribute
1439      LOGICAL         , INTENT(  out)                 ::   llok      ! Error code
1440      INTEGER         , INTENT(  out), OPTIONAL       ::   ksize     ! Size of the attribute array
1441      CHARACTER(len=*), INTENT(in   ), OPTIONAL       ::   cdvar     ! Name of the variable
1442      !
1443      IF( kiomid > 0 ) THEN
1444         IF( iom_file(kiomid)%nfid > 0 )   CALL iom_nf90_chkatt( kiomid, cdatt, llok, ksize=ksize, cdvar=cdvar )
1445      ENDIF
1446      !
1447   END SUBROUTINE iom_chkatt
1448
1449   !!----------------------------------------------------------------------
1450   !!                   INTERFACE iom_getatt
1451   !!----------------------------------------------------------------------
1452   SUBROUTINE iom_g0d_iatt( kiomid, cdatt, katt0d, cdvar )
1453      INTEGER               , INTENT(in   )           ::   kiomid    ! Identifier of the file
1454      CHARACTER(len=*)      , INTENT(in   )           ::   cdatt     ! Name of the attribute
1455      INTEGER               , INTENT(  out)           ::   katt0d    ! read field
1456      CHARACTER(len=*)      , INTENT(in   ), OPTIONAL ::   cdvar     ! Name of the variable
1457      !
1458      IF( kiomid > 0 ) THEN
1459         IF( iom_file(kiomid)%nfid > 0 )   CALL iom_nf90_getatt( kiomid, cdatt,  katt0d =  katt0d, cdvar=cdvar )
1460      ENDIF
1461   END SUBROUTINE iom_g0d_iatt
1462
1463   SUBROUTINE iom_g1d_iatt( kiomid, cdatt, katt1d, cdvar )
1464      INTEGER               , INTENT(in   )           ::   kiomid    ! Identifier of the file
1465      CHARACTER(len=*)      , INTENT(in   )           ::   cdatt     ! Name of the attribute
1466      INTEGER, DIMENSION(:) , INTENT(  out)           ::   katt1d    ! read field
1467      CHARACTER(len=*)      , INTENT(in   ), OPTIONAL ::   cdvar     ! Name of the variable
1468      !
1469      IF( kiomid > 0 ) THEN
1470         IF( iom_file(kiomid)%nfid > 0 )   CALL iom_nf90_getatt( kiomid, cdatt,  katt1d =  katt1d, cdvar=cdvar )
1471      ENDIF
1472   END SUBROUTINE iom_g1d_iatt
1473
1474   SUBROUTINE iom_g0d_ratt( kiomid, cdatt, patt0d, cdvar )
1475      INTEGER               , INTENT(in   )           ::   kiomid    ! Identifier of the file
1476      CHARACTER(len=*)      , INTENT(in   )           ::   cdatt     ! Name of the attribute
1477      REAL(wp)              , INTENT(  out)           ::   patt0d    ! read field
1478      CHARACTER(len=*)      , INTENT(in   ), OPTIONAL ::   cdvar     ! Name of the variable
1479      !
1480      IF( kiomid > 0 ) THEN
1481         IF( iom_file(kiomid)%nfid > 0 )   CALL iom_nf90_getatt( kiomid, cdatt,  patt0d =  patt0d, cdvar=cdvar )
1482      ENDIF
1483   END SUBROUTINE iom_g0d_ratt
1484
1485   SUBROUTINE iom_g1d_ratt( kiomid, cdatt, patt1d, cdvar )
1486      INTEGER               , INTENT(in   )           ::   kiomid    ! Identifier of the file
1487      CHARACTER(len=*)      , INTENT(in   )           ::   cdatt     ! Name of the attribute
1488      REAL(wp), DIMENSION(:), INTENT(  out)           ::   patt1d    ! read field
1489      CHARACTER(len=*)      , INTENT(in   ), OPTIONAL ::   cdvar     ! Name of the variable
1490      !
1491      IF( kiomid > 0 ) THEN
1492         IF( iom_file(kiomid)%nfid > 0 )   CALL iom_nf90_getatt( kiomid, cdatt,  patt1d =  patt1d, cdvar=cdvar )
1493      ENDIF
1494   END SUBROUTINE iom_g1d_ratt
1495   
1496   SUBROUTINE iom_g0d_catt( kiomid, cdatt, cdatt0d, cdvar )
1497      INTEGER               , INTENT(in   )           ::   kiomid    ! Identifier of the file
1498      CHARACTER(len=*)      , INTENT(in   )           ::   cdatt     ! Name of the attribute
1499      CHARACTER(len=*)      , INTENT(  out)           ::   cdatt0d   ! read field
1500      CHARACTER(len=*)      , INTENT(in   ), OPTIONAL ::   cdvar     ! Name of the variable
1501      !
1502      IF( kiomid > 0 ) THEN
1503         IF( iom_file(kiomid)%nfid > 0 )   CALL iom_nf90_getatt( kiomid, cdatt, cdatt0d = cdatt0d, cdvar=cdvar )
1504      ENDIF
1505   END SUBROUTINE iom_g0d_catt
1506
1507
1508   !!----------------------------------------------------------------------
1509   !!                   INTERFACE iom_putatt
1510   !!----------------------------------------------------------------------
1511   SUBROUTINE iom_p0d_iatt( kiomid, cdatt, katt0d, cdvar )
1512      INTEGER               , INTENT(in   )           ::   kiomid    ! Identifier of the file
1513      CHARACTER(len=*)      , INTENT(in   )           ::   cdatt     ! Name of the attribute
1514      INTEGER               , INTENT(in   )           ::   katt0d    ! written field
1515      CHARACTER(len=*)      , INTENT(in   ), OPTIONAL ::   cdvar     ! Name of the variable
1516      !
1517      IF( kiomid > 0 ) THEN
1518         IF( iom_file(kiomid)%nfid > 0 )   CALL iom_nf90_putatt( kiomid, cdatt,  katt0d =  katt0d, cdvar=cdvar )
1519      ENDIF
1520   END SUBROUTINE iom_p0d_iatt
1521
1522   SUBROUTINE iom_p1d_iatt( kiomid, cdatt, katt1d, cdvar )
1523      INTEGER               , INTENT(in   )           ::   kiomid    ! Identifier of the file
1524      CHARACTER(len=*)      , INTENT(in   )           ::   cdatt     ! Name of the attribute
1525      INTEGER, DIMENSION(:) , INTENT(in   )           ::   katt1d    ! written field
1526      CHARACTER(len=*)      , INTENT(in   ), OPTIONAL ::   cdvar     ! Name of the variable
1527      !
1528      IF( kiomid > 0 ) THEN
1529         IF( iom_file(kiomid)%nfid > 0 )   CALL iom_nf90_putatt( kiomid, cdatt,  katt1d =  katt1d, cdvar=cdvar )
1530      ENDIF
1531   END SUBROUTINE iom_p1d_iatt
1532
1533   SUBROUTINE iom_p0d_ratt( kiomid, cdatt, patt0d, cdvar )
1534      INTEGER               , INTENT(in   )           ::   kiomid    ! Identifier of the file
1535      CHARACTER(len=*)      , INTENT(in   )           ::   cdatt     ! Name of the attribute
1536      REAL(wp)              , INTENT(in   )           ::   patt0d    ! written field
1537      CHARACTER(len=*)      , INTENT(in   ), OPTIONAL ::   cdvar     ! Name of the variable
1538      !
1539      IF( kiomid > 0 ) THEN
1540         IF( iom_file(kiomid)%nfid > 0 )   CALL iom_nf90_putatt( kiomid, cdatt,  patt0d =  patt0d, cdvar=cdvar )
1541      ENDIF
1542   END SUBROUTINE iom_p0d_ratt
1543
1544   SUBROUTINE iom_p1d_ratt( kiomid, cdatt, patt1d, cdvar )
1545      INTEGER               , INTENT(in   )           ::   kiomid    ! Identifier of the file
1546      CHARACTER(len=*)      , INTENT(in   )           ::   cdatt     ! Name of the attribute
1547      REAL(wp), DIMENSION(:), INTENT(in   )           ::   patt1d    ! written field
1548      CHARACTER(len=*)      , INTENT(in   ), OPTIONAL ::   cdvar     ! Name of the variable
1549      !
1550      IF( kiomid > 0 ) THEN
1551         IF( iom_file(kiomid)%nfid > 0 )   CALL iom_nf90_putatt( kiomid, cdatt,  patt1d =  patt1d, cdvar=cdvar )
1552      ENDIF
1553   END SUBROUTINE iom_p1d_ratt
1554   
1555   SUBROUTINE iom_p0d_catt( kiomid, cdatt, cdatt0d, cdvar )
1556      INTEGER               , INTENT(in   )           ::   kiomid    ! Identifier of the file
1557      CHARACTER(len=*)      , INTENT(in   )           ::   cdatt     ! Name of the attribute
1558      CHARACTER(len=*)      , INTENT(in   )           ::   cdatt0d   ! written field
1559      CHARACTER(len=*)      , INTENT(in   ), OPTIONAL ::   cdvar     ! Name of the variable
1560      !
1561      IF( kiomid > 0 ) THEN
1562         IF( iom_file(kiomid)%nfid > 0 )   CALL iom_nf90_putatt( kiomid, cdatt, cdatt0d = cdatt0d, cdvar=cdvar )
1563      ENDIF
1564   END SUBROUTINE iom_p0d_catt
1565
1566
1567   !!----------------------------------------------------------------------
1568   !!                   INTERFACE iom_rstput
1569   !!----------------------------------------------------------------------
1570   SUBROUTINE iom_rp0d_sp( kt, kwrite, kiomid, cdvar, pvar, ktype, ldxios )
1571      INTEGER         , INTENT(in)                         ::   kt       ! ocean time-step
1572      INTEGER         , INTENT(in)                         ::   kwrite   ! writing time-step
1573      INTEGER         , INTENT(in)                         ::   kiomid   ! Identifier of the file
1574      CHARACTER(len=*), INTENT(in)                         ::   cdvar    ! time axis name
1575      REAL(sp)        , INTENT(in)                         ::   pvar     ! written field
1576      INTEGER         , INTENT(in), OPTIONAL               ::   ktype    ! variable external type
1577      LOGICAL, OPTIONAL :: ldxios   ! xios write flag
1578      LOGICAL :: llx                ! local xios write flag
1579      INTEGER :: ivid   ! variable id
1580
1581      llx = .FALSE.
1582      IF(PRESENT(ldxios)) llx = ldxios
1583      IF( llx ) THEN
1584#ifdef key_iomput
1585      IF( kt == kwrite ) THEN
1586          IF(lwp) write(numout,*) 'RESTART: write (XIOS 0D) ',trim(cdvar)
1587          CALL xios_send_field(trim(cdvar), pvar)
1588      ENDIF
1589#endif
1590      ELSE
1591         IF( kiomid > 0 ) THEN
1592            IF( iom_file(kiomid)%nfid > 0 ) THEN
1593               ivid = iom_varid( kiomid, cdvar, ldstop = .FALSE. )
1594               CALL iom_nf90_rstput( kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r0d = real(pvar, dp) )
1595            ENDIF
1596         ENDIF
1597      ENDIF
1598   END SUBROUTINE iom_rp0d_sp
1599
1600   SUBROUTINE iom_rp0d_dp( kt, kwrite, kiomid, cdvar, pvar, ktype, ldxios )
1601      INTEGER         , INTENT(in)                         ::   kt       ! ocean time-step
1602      INTEGER         , INTENT(in)                         ::   kwrite   ! writing time-step
1603      INTEGER         , INTENT(in)                         ::   kiomid   ! Identifier of the file
1604      CHARACTER(len=*), INTENT(in)                         ::   cdvar    ! time axis name
1605      REAL(dp)        , INTENT(in)                         ::   pvar     ! written field
1606      INTEGER         , INTENT(in), OPTIONAL               ::   ktype    ! variable external type
1607      LOGICAL, OPTIONAL :: ldxios   ! xios write flag
1608      LOGICAL :: llx                ! local xios write flag
1609      INTEGER :: ivid   ! variable id
1610
1611      llx = .FALSE.
1612      IF(PRESENT(ldxios)) llx = ldxios
1613      IF( llx ) THEN
1614#ifdef key_iomput
1615      IF( kt == kwrite ) THEN
1616          IF(lwp) write(numout,*) 'RESTART: write (XIOS 0D) ',trim(cdvar)
1617          CALL xios_send_field(trim(cdvar), pvar)
1618      ENDIF
1619#endif
1620      ELSE
1621         IF( kiomid > 0 ) THEN
1622            IF( iom_file(kiomid)%nfid > 0 ) THEN
1623               ivid = iom_varid( kiomid, cdvar, ldstop = .FALSE. )
1624               CALL iom_nf90_rstput( kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r0d = pvar )
1625            ENDIF
1626         ENDIF
1627      ENDIF
1628   END SUBROUTINE iom_rp0d_dp
1629
1630
1631   SUBROUTINE iom_rp1d_sp( kt, kwrite, kiomid, cdvar, pvar, ktype, ldxios )
1632      INTEGER         , INTENT(in)                         ::   kt       ! ocean time-step
1633      INTEGER         , INTENT(in)                         ::   kwrite   ! writing time-step
1634      INTEGER         , INTENT(in)                         ::   kiomid   ! Identifier of the file
1635      CHARACTER(len=*), INTENT(in)                         ::   cdvar    ! time axis name
1636      REAL(sp)        , INTENT(in), DIMENSION(          :) ::   pvar     ! written field
1637      INTEGER         , INTENT(in), OPTIONAL               ::   ktype    ! variable external type
1638      LOGICAL, OPTIONAL                                    ::   ldxios   ! xios write flag
1639      LOGICAL :: llx                ! local xios write flag
1640      INTEGER :: ivid   ! variable id
1641
1642      llx = .FALSE.
1643      IF(PRESENT(ldxios)) llx = ldxios
1644      IF( llx ) THEN
1645#ifdef key_iomput
1646      IF( kt == kwrite ) THEN
1647         IF(lwp) write(numout,*) 'RESTART: write (XIOS 1D) ',trim(cdvar)
1648         CALL xios_send_field(trim(cdvar), pvar)
1649      ENDIF
1650#endif
1651      ELSE
1652         IF( kiomid > 0 ) THEN
1653            IF( iom_file(kiomid)%nfid > 0 ) THEN
1654               ivid = iom_varid( kiomid, cdvar, ldstop = .FALSE. )
1655               CALL iom_nf90_rstput( kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r1d = real(pvar, dp) )
1656            ENDIF
1657         ENDIF
1658      ENDIF
1659   END SUBROUTINE iom_rp1d_sp
1660
1661   SUBROUTINE iom_rp1d_dp( kt, kwrite, kiomid, cdvar, pvar, ktype, ldxios )
1662      INTEGER         , INTENT(in)                         ::   kt       ! ocean time-step
1663      INTEGER         , INTENT(in)                         ::   kwrite   ! writing time-step
1664      INTEGER         , INTENT(in)                         ::   kiomid   ! Identifier of the file
1665      CHARACTER(len=*), INTENT(in)                         ::   cdvar    ! time axis name
1666      REAL(dp)        , INTENT(in), DIMENSION(          :) ::   pvar     ! written field
1667      INTEGER         , INTENT(in), OPTIONAL               ::   ktype    ! variable external type
1668      LOGICAL, OPTIONAL                                    ::   ldxios   ! xios write flag
1669      LOGICAL :: llx                ! local xios write flag
1670      INTEGER :: ivid   ! variable id
1671
1672      llx = .FALSE.
1673      IF(PRESENT(ldxios)) llx = ldxios
1674      IF( llx ) THEN
1675#ifdef key_iomput
1676      IF( kt == kwrite ) THEN
1677         IF(lwp) write(numout,*) 'RESTART: write (XIOS 1D) ',trim(cdvar)
1678         CALL xios_send_field(trim(cdvar), pvar)
1679      ENDIF
1680#endif
1681      ELSE
1682         IF( kiomid > 0 ) THEN
1683            IF( iom_file(kiomid)%nfid > 0 ) THEN
1684               ivid = iom_varid( kiomid, cdvar, ldstop = .FALSE. )
1685               CALL iom_nf90_rstput( kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r1d = pvar )
1686            ENDIF
1687         ENDIF
1688      ENDIF
1689   END SUBROUTINE iom_rp1d_dp
1690
1691
1692   SUBROUTINE iom_rp2d_sp( kt, kwrite, kiomid, cdvar, pvar, ktype, ldxios )
1693      INTEGER         , INTENT(in)                         ::   kt       ! ocean time-step
1694      INTEGER         , INTENT(in)                         ::   kwrite   ! writing time-step
1695      INTEGER         , INTENT(in)                         ::   kiomid   ! Identifier of the file
1696      CHARACTER(len=*), INTENT(in)                         ::   cdvar    ! time axis name
1697      REAL(sp)        , INTENT(in), DIMENSION(:,    :    ) ::   pvar     ! written field
1698      INTEGER         , INTENT(in), OPTIONAL               ::   ktype    ! variable external type
1699      LOGICAL, OPTIONAL :: ldxios   ! xios write flag
1700      LOGICAL :: llx
1701      INTEGER :: ivid   ! variable id
1702
1703      llx = .FALSE.
1704      IF(PRESENT(ldxios)) llx = ldxios
1705      IF( llx ) THEN
1706#ifdef key_iomput
1707      IF( kt == kwrite ) THEN
1708         IF(lwp) write(numout,*) 'RESTART: write (XIOS 2D) ',trim(cdvar)
1709         CALL xios_send_field(trim(cdvar), pvar)
1710      ENDIF
1711#endif
1712      ELSE
1713         IF( kiomid > 0 ) THEN
1714            IF( iom_file(kiomid)%nfid > 0 ) THEN
1715               ivid = iom_varid( kiomid, cdvar, ldstop = .FALSE. )
1716               CALL iom_nf90_rstput( kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r2d = real(pvar, dp) )
1717            ENDIF
1718         ENDIF
1719      ENDIF
1720   END SUBROUTINE iom_rp2d_sp
1721
1722   SUBROUTINE iom_rp2d_dp( kt, kwrite, kiomid, cdvar, pvar, ktype, ldxios )
1723      INTEGER         , INTENT(in)                         ::   kt       ! ocean time-step
1724      INTEGER         , INTENT(in)                         ::   kwrite   ! writing time-step
1725      INTEGER         , INTENT(in)                         ::   kiomid   ! Identifier of the file
1726      CHARACTER(len=*), INTENT(in)                         ::   cdvar    ! time axis name
1727      REAL(dp)        , INTENT(in), DIMENSION(:,    :    ) ::   pvar     ! written field
1728      INTEGER         , INTENT(in), OPTIONAL               ::   ktype    ! variable external type
1729      LOGICAL, OPTIONAL :: ldxios   ! xios write flag
1730      LOGICAL :: llx
1731      INTEGER :: ivid   ! variable id
1732
1733      llx = .FALSE.
1734      IF(PRESENT(ldxios)) llx = ldxios
1735      IF( llx ) THEN
1736#ifdef key_iomput
1737      IF( kt == kwrite ) THEN
1738         IF(lwp) write(numout,*) 'RESTART: write (XIOS 2D) ',trim(cdvar)
1739         CALL xios_send_field(trim(cdvar), pvar)
1740      ENDIF
1741#endif
1742      ELSE
1743         IF( kiomid > 0 ) THEN
1744            IF( iom_file(kiomid)%nfid > 0 ) THEN
1745               ivid = iom_varid( kiomid, cdvar, ldstop = .FALSE. )
1746               CALL iom_nf90_rstput( kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r2d = pvar )
1747            ENDIF
1748         ENDIF
1749      ENDIF
1750   END SUBROUTINE iom_rp2d_dp
1751
1752
1753   SUBROUTINE iom_rp3d_sp( kt, kwrite, kiomid, cdvar, pvar, ktype, ldxios )
1754      INTEGER         , INTENT(in)                         ::   kt       ! ocean time-step
1755      INTEGER         , INTENT(in)                         ::   kwrite   ! writing time-step
1756      INTEGER         , INTENT(in)                         ::   kiomid   ! Identifier of the file
1757      CHARACTER(len=*), INTENT(in)                         ::   cdvar    ! time axis name
1758      REAL(sp)        , INTENT(in),       DIMENSION(:,:,:) ::   pvar     ! written field
1759      INTEGER         , INTENT(in), OPTIONAL               ::   ktype    ! variable external type
1760      LOGICAL, OPTIONAL :: ldxios   ! xios write flag
1761      LOGICAL :: llx                 ! local xios write flag
1762      INTEGER :: ivid   ! variable id
1763
1764      llx = .FALSE.
1765      IF(PRESENT(ldxios)) llx = ldxios
1766      IF( llx ) THEN
1767#ifdef key_iomput
1768      IF( kt == kwrite ) THEN
1769         IF(lwp) write(numout,*) 'RESTART: write (XIOS 3D) ',trim(cdvar)
1770         CALL xios_send_field(trim(cdvar), pvar)
1771      ENDIF
1772#endif
1773      ELSE
1774         IF( kiomid > 0 ) THEN
1775            IF( iom_file(kiomid)%nfid > 0 ) THEN
1776               ivid = iom_varid( kiomid, cdvar, ldstop = .FALSE. )
1777               CALL iom_nf90_rstput( kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r3d = real(pvar, dp) )
1778            ENDIF
1779         ENDIF
1780      ENDIF
1781   END SUBROUTINE iom_rp3d_sp
1782
1783   SUBROUTINE iom_rp3d_dp( kt, kwrite, kiomid, cdvar, pvar, ktype, ldxios )
1784      INTEGER         , INTENT(in)                         ::   kt       ! ocean time-step
1785      INTEGER         , INTENT(in)                         ::   kwrite   ! writing time-step
1786      INTEGER         , INTENT(in)                         ::   kiomid   ! Identifier of the file
1787      CHARACTER(len=*), INTENT(in)                         ::   cdvar    ! time axis name
1788      REAL(dp)        , INTENT(in),       DIMENSION(:,:,:) ::   pvar     ! written field
1789      INTEGER         , INTENT(in), OPTIONAL               ::   ktype    ! variable external type
1790      LOGICAL, OPTIONAL :: ldxios   ! xios write flag
1791      LOGICAL :: llx                 ! local xios write flag
1792      INTEGER :: ivid   ! variable id
1793
1794      llx = .FALSE.
1795      IF(PRESENT(ldxios)) llx = ldxios
1796      IF( llx ) THEN
1797#ifdef key_iomput
1798      IF( kt == kwrite ) THEN
1799         IF(lwp) write(numout,*) 'RESTART: write (XIOS 3D) ',trim(cdvar)
1800         CALL xios_send_field(trim(cdvar), pvar)
1801      ENDIF
1802#endif
1803      ELSE
1804         IF( kiomid > 0 ) THEN
1805            IF( iom_file(kiomid)%nfid > 0 ) THEN
1806               ivid = iom_varid( kiomid, cdvar, ldstop = .FALSE. )
1807               CALL iom_nf90_rstput( kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r3d = pvar )
1808            ENDIF
1809         ENDIF
1810      ENDIF
1811   END SUBROUTINE iom_rp3d_dp
1812
1813
1814
1815  SUBROUTINE iom_delay_rst( cdaction, cdcpnt, kncid )
1816      !!---------------------------------------------------------------------
1817      !!   Routine iom_delay_rst: used read/write restart related to mpp_delay
1818      !!
1819      !!---------------------------------------------------------------------
1820      CHARACTER(len=*), INTENT(in   ) ::   cdaction        !
1821      CHARACTER(len=*), INTENT(in   ) ::   cdcpnt
1822      INTEGER         , INTENT(in   ) ::   kncid
1823      !
1824      INTEGER  :: ji
1825      INTEGER  :: indim
1826      LOGICAL  :: llattexist
1827      REAL(wp), ALLOCATABLE, DIMENSION(:) ::   zreal1d
1828      !!---------------------------------------------------------------------
1829      !
1830      !                                      ===================================
1831      IF( TRIM(cdaction) == 'READ' ) THEN   ! read restart related to mpp_delay !
1832         !                                   ===================================
1833         DO ji = 1, nbdelay
1834            IF ( c_delaycpnt(ji) == cdcpnt ) THEN
1835               CALL iom_chkatt( kncid, 'DELAY_'//c_delaylist(ji), llattexist, indim )
1836               IF( llattexist )  THEN
1837                  ALLOCATE( todelay(ji)%z1d(indim) )
1838                  CALL iom_getatt( kncid, 'DELAY_'//c_delaylist(ji), todelay(ji)%z1d(:) )
1839                  ndelayid(ji) = 0   ! set to 0 to specify that the value was read in the restart
1840               ENDIF
1841           ENDIF
1842         END DO
1843         !                                   ====================================
1844      ELSE                                  ! write restart related to mpp_delay !
1845         !                                   ====================================
1846         DO ji = 1, nbdelay   ! save only ocean delayed global communication variables
1847            IF ( c_delaycpnt(ji) == cdcpnt ) THEN
1848               IF( ASSOCIATED(todelay(ji)%z1d) ) THEN
1849                  CALL mpp_delay_rcv(ji)   ! make sure %z1d is received
1850                  CALL iom_putatt( kncid, 'DELAY_'//c_delaylist(ji), todelay(ji)%z1d(:) )
1851               ENDIF
1852            ENDIF
1853         END DO
1854         !
1855      ENDIF
1856     
1857   END SUBROUTINE iom_delay_rst
1858 
1859   
1860
1861   !!----------------------------------------------------------------------
1862   !!                   INTERFACE iom_put
1863   !!----------------------------------------------------------------------
1864   SUBROUTINE iom_p0d_sp( cdname, pfield0d )
1865      CHARACTER(LEN=*), INTENT(in) ::   cdname
1866      REAL(sp)        , INTENT(in) ::   pfield0d
1867!!      REAL(wp)        , DIMENSION(jpi,jpj) ::   zz     ! masson
1868#if defined key_iomput
1869!!clem      zz(:,:)=pfield0d
1870!!clem      CALL xios_send_field(cdname, zz)
1871      CALL xios_send_field(cdname, (/pfield0d/)) 
1872#else
1873      IF( .FALSE. )   WRITE(numout,*) cdname, pfield0d   ! useless test to avoid compilation warnings
1874#endif
1875   END SUBROUTINE iom_p0d_sp
1876
1877   SUBROUTINE iom_p0d_dp( cdname, pfield0d )
1878      CHARACTER(LEN=*), INTENT(in) ::   cdname
1879      REAL(dp)        , INTENT(in) ::   pfield0d
1880!!      REAL(wp)        , DIMENSION(jpi,jpj) ::   zz     ! masson
1881#if defined key_iomput
1882!!clem      zz(:,:)=pfield0d
1883!!clem      CALL xios_send_field(cdname, zz)
1884      CALL xios_send_field(cdname, (/pfield0d/)) 
1885#else
1886      IF( .FALSE. )   WRITE(numout,*) cdname, pfield0d   ! useless test to avoid compilation warnings
1887#endif
1888   END SUBROUTINE iom_p0d_dp
1889
1890
1891   SUBROUTINE iom_p1d_sp( cdname, pfield1d )
1892      CHARACTER(LEN=*)          , INTENT(in) ::   cdname
1893      REAL(sp),     DIMENSION(:), INTENT(in) ::   pfield1d
1894#if defined key_iomput
1895      CALL xios_send_field( cdname, RESHAPE( (/pfield1d/), (/1,1,SIZE(pfield1d)/) ) )
1896#else
1897      IF( .FALSE. )   WRITE(numout,*) cdname, pfield1d   ! useless test to avoid compilation warnings
1898#endif
1899   END SUBROUTINE iom_p1d_sp
1900
1901   SUBROUTINE iom_p1d_dp( cdname, pfield1d )
1902      CHARACTER(LEN=*)          , INTENT(in) ::   cdname
1903      REAL(dp),     DIMENSION(:), INTENT(in) ::   pfield1d
1904#if defined key_iomput
1905      CALL xios_send_field( cdname, RESHAPE( (/pfield1d/), (/1,1,SIZE(pfield1d)/) ) )
1906#else
1907      IF( .FALSE. )   WRITE(numout,*) cdname, pfield1d   ! useless test to avoid compilation warnings
1908#endif
1909   END SUBROUTINE iom_p1d_dp
1910
1911   SUBROUTINE iom_p2d_sp( cdname, pfield2d )
1912      CHARACTER(LEN=*)            , INTENT(in) ::   cdname
1913      REAL(sp),     DIMENSION(:,:), INTENT(in) ::   pfield2d
1914      IF( iom_use(cdname) ) THEN
1915#if defined key_iomput
1916         IF( SIZE(pfield2d, dim=1) == jpi .AND. SIZE(pfield2d, dim=2) == jpj ) THEN
1917            CALL xios_send_field( cdname, pfield2d(Nis0:Nie0, Njs0:Nje0) )       ! this extraction will create a copy of pfield2d
1918         ELSE
1919            CALL xios_send_field( cdname, pfield2d )
1920         ENDIF
1921#else
1922         WRITE(numout,*) pfield2d   ! iom_use(cdname) = .F. -> useless test to avoid compilation warnings
1923#endif
1924      ENDIF
1925   END SUBROUTINE iom_p2d_sp
1926
1927   SUBROUTINE iom_p2d_dp( cdname, pfield2d )
1928      CHARACTER(LEN=*)            , INTENT(in) ::   cdname
1929      REAL(dp),     DIMENSION(:,:), INTENT(in) ::   pfield2d
1930      IF( iom_use(cdname) ) THEN
1931#if defined key_iomput
1932         IF( SIZE(pfield2d, dim=1) == jpi .AND. SIZE(pfield2d, dim=2) == jpj ) THEN
1933            CALL xios_send_field( cdname, pfield2d(Nis0:Nie0, Njs0:Nje0) )       ! this extraction will create a copy of pfield2d
1934         ELSE
1935            CALL xios_send_field( cdname, pfield2d )
1936         ENDIF
1937#else
1938         WRITE(numout,*) pfield2d   ! iom_use(cdname) = .F. -> useless test to avoid compilation warnings
1939#endif
1940      ENDIF
1941   END SUBROUTINE iom_p2d_dp
1942
1943   SUBROUTINE iom_p3d_sp( cdname, pfield3d )
1944      CHARACTER(LEN=*)                , INTENT(in) ::   cdname
1945      REAL(sp),       DIMENSION(:,:,:), INTENT(in) ::   pfield3d
1946      IF( iom_use(cdname) ) THEN
1947#if defined key_iomput
1948         IF( SIZE(pfield3d, dim=1) == jpi .AND. SIZE(pfield3d, dim=2) == jpj ) THEN
1949            CALL xios_send_field( cdname, pfield3d(Nis0:Nie0, Njs0:Nje0,:) )     ! this extraction will create a copy of pfield3d
1950         ELSE
1951            CALL xios_send_field( cdname, pfield3d )
1952         ENDIF
1953#else
1954         WRITE(numout,*) pfield3d   ! iom_use(cdname) = .F. -> useless test to avoid compilation warnings
1955#endif
1956      ENDIF
1957   END SUBROUTINE iom_p3d_sp
1958
1959   SUBROUTINE iom_p3d_dp( cdname, pfield3d )
1960      CHARACTER(LEN=*)                , INTENT(in) ::   cdname
1961      REAL(dp),       DIMENSION(:,:,:), INTENT(in) ::   pfield3d
1962      IF( iom_use(cdname) ) THEN
1963#if defined key_iomput
1964         IF( SIZE(pfield3d, dim=1) == jpi .AND. SIZE(pfield3d, dim=2) == jpj ) THEN
1965            CALL xios_send_field( cdname, pfield3d(Nis0:Nie0, Njs0:Nje0,:) )     ! this extraction will create a copy of pfield3d
1966         ELSE
1967            CALL xios_send_field( cdname, pfield3d )
1968         ENDIF
1969#else
1970         WRITE(numout,*) pfield3d   ! iom_use(cdname) = .F. -> useless test to avoid compilation warnings
1971#endif
1972      ENDIF
1973   END SUBROUTINE iom_p3d_dp
1974
1975   SUBROUTINE iom_p4d_sp( cdname, pfield4d )
1976      CHARACTER(LEN=*)                , INTENT(in) ::   cdname
1977      REAL(sp),       DIMENSION(:,:,:,:), INTENT(in) ::   pfield4d
1978      IF( iom_use(cdname) ) THEN
1979#if defined key_iomput
1980         IF( SIZE(pfield4d, dim=1) == jpi .AND. SIZE(pfield4d, dim=2) == jpj ) THEN
1981            CALL xios_send_field( cdname, pfield4d(Nis0:Nie0, Njs0:Nje0,:,:) )   ! this extraction will create a copy of pfield4d
1982         ELSE
1983            CALL xios_send_field (cdname, pfield4d )
1984         ENDIF
1985#else
1986         WRITE(numout,*) pfield4d   ! iom_use(cdname) = .F. -> useless test to avoid compilation warnings
1987#endif
1988      ENDIF
1989   END SUBROUTINE iom_p4d_sp
1990
1991   SUBROUTINE iom_p4d_dp( cdname, pfield4d )
1992      CHARACTER(LEN=*)                , INTENT(in) ::   cdname
1993      REAL(dp),       DIMENSION(:,:,:,:), INTENT(in) ::   pfield4d
1994      IF( iom_use(cdname) ) THEN
1995#if defined key_iomput
1996         IF( SIZE(pfield4d, dim=1) == jpi .AND. SIZE(pfield4d, dim=2) == jpj ) THEN
1997            CALL xios_send_field( cdname, pfield4d(Nis0:Nie0, Njs0:Nje0,:,:) )   ! this extraction will create a copy of pfield4d
1998         ELSE
1999            CALL xios_send_field (cdname, pfield4d )
2000         ENDIF
2001#else
2002         WRITE(numout,*) pfield4d   ! iom_use(cdname) = .F. -> useless test to avoid compilation warnings
2003#endif
2004      ENDIF
2005   END SUBROUTINE iom_p4d_dp
2006
2007#if defined key_iomput
2008   !!----------------------------------------------------------------------
2009   !!   'key_iomput'                                         XIOS interface
2010   !!----------------------------------------------------------------------
2011
2012   SUBROUTINE iom_set_domain_attr( cdid, ni_glo, nj_glo, ibegin, jbegin, ni, nj,                                               &
2013      &                                    data_dim, data_ibegin, data_ni, data_jbegin, data_nj, lonvalue, latvalue, mask,     &
2014      &                                    nvertex, bounds_lon, bounds_lat, area )
2015      !!----------------------------------------------------------------------
2016      !!----------------------------------------------------------------------
2017      CHARACTER(LEN=*)                  , INTENT(in) ::   cdid
2018      INTEGER                 , OPTIONAL, INTENT(in) ::   ni_glo, nj_glo, ibegin, jbegin, ni, nj
2019      INTEGER                 , OPTIONAL, INTENT(in) ::   data_dim, data_ibegin, data_ni, data_jbegin, data_nj
2020      INTEGER                 , OPTIONAL, INTENT(in) ::   nvertex
2021      REAL(dp), DIMENSION(:)  , OPTIONAL, INTENT(in) ::   lonvalue, latvalue
2022      REAL(dp), DIMENSION(:,:), OPTIONAL, INTENT(in) ::   bounds_lon, bounds_lat, area
2023      LOGICAL , DIMENSION(:)  , OPTIONAL, INTENT(in) ::   mask
2024      !!----------------------------------------------------------------------
2025      !
2026      IF( xios_is_valid_domain     (cdid) ) THEN
2027         CALL xios_set_domain_attr     ( cdid, ni_glo=ni_glo, nj_glo=nj_glo, ibegin=ibegin, jbegin=jbegin, ni=ni, nj=nj,   &
2028            &    data_dim=data_dim, data_ibegin=data_ibegin, data_ni=data_ni, data_jbegin=data_jbegin, data_nj=data_nj ,   &
2029            &    lonvalue_1D=lonvalue, latvalue_1D=latvalue, mask_1D=mask, nvertex=nvertex, bounds_lon_1D=bounds_lon,      &
2030            &    bounds_lat_1D=bounds_lat, area=area, type='curvilinear')
2031      ENDIF
2032      IF( xios_is_valid_domaingroup(cdid) ) THEN
2033         CALL xios_set_domaingroup_attr( cdid, ni_glo=ni_glo, nj_glo=nj_glo, ibegin=ibegin, jbegin=jbegin, ni=ni, nj=nj,   &
2034            &    data_dim=data_dim, data_ibegin=data_ibegin, data_ni=data_ni, data_jbegin=data_jbegin, data_nj=data_nj ,   &
2035            &    lonvalue_1D=lonvalue, latvalue_1D=latvalue, mask_1D=mask, nvertex=nvertex, bounds_lon_1D=bounds_lon,      &
2036            &    bounds_lat_1D=bounds_lat, area=area, type='curvilinear' )
2037      ENDIF
2038      !
2039      CALL xios_solve_inheritance()
2040      !
2041   END SUBROUTINE iom_set_domain_attr
2042
2043
2044   SUBROUTINE iom_set_zoom_domain_attr( cdid, ibegin, jbegin, ni, nj )
2045      !!----------------------------------------------------------------------
2046      !!----------------------------------------------------------------------
2047      CHARACTER(LEN=*), INTENT(in) ::   cdid
2048      INTEGER         , INTENT(in) ::   ibegin, jbegin, ni, nj
2049      !
2050      TYPE(xios_gridgroup) :: gridgroup_hdl
2051      TYPE(xios_grid)      :: grid_hdl
2052      TYPE(xios_domain)    :: domain_hdl 
2053      TYPE(xios_axis)      :: axis_hdl 
2054      CHARACTER(LEN=64)    :: cldomrefid   ! domain_ref name
2055      CHARACTER(len=1)     :: cl1          ! last character of this name
2056      !!----------------------------------------------------------------------
2057      !
2058      IF( xios_is_valid_zoom_domain(cdid) ) THEN
2059         ! define the zoom_domain attributs
2060         CALL xios_set_zoom_domain_attr( cdid, ibegin=ibegin, jbegin=jbegin, ni=ni, nj=nj )
2061         ! define a new 2D grid with this new domain
2062         CALL xios_get_handle("grid_definition", gridgroup_hdl )
2063         CALL xios_add_child(gridgroup_hdl, grid_hdl, TRIM(cdid)//'_2D' )   ! add a new 2D grid to grid_definition
2064         CALL xios_add_child(grid_hdl, domain_hdl, TRIM(cdid) )             ! add its domain
2065         ! define a new 3D grid with this new domain
2066         CALL xios_add_child(gridgroup_hdl, grid_hdl, TRIM(cdid)//'_3D' )   ! add a new 3D grid to grid_definition
2067         CALL xios_add_child(grid_hdl, domain_hdl, TRIM(cdid) )             ! add its domain
2068         ! vertical axis
2069         cl1 = cdid(LEN_TRIM(cdid):)                                        ! last letter of cdid
2070         cl1 = CHAR(ICHAR(cl1)+32)                                          ! from upper to lower case
2071         CALL xios_add_child(grid_hdl, axis_hdl, 'depth'//cl1)              ! add its axis
2072      ENDIF
2073      !     
2074   END SUBROUTINE iom_set_zoom_domain_attr
2075
2076
2077   SUBROUTINE iom_set_axis_attr( cdid, paxis, bounds )
2078      !!----------------------------------------------------------------------
2079      !!----------------------------------------------------------------------
2080      CHARACTER(LEN=*)      , INTENT(in) ::   cdid
2081      REAL(wp), DIMENSION(:)  , OPTIONAL, INTENT(in) ::   paxis
2082      REAL(wp), DIMENSION(:,:), OPTIONAL, INTENT(in) ::   bounds
2083      !!----------------------------------------------------------------------
2084      IF( PRESENT(paxis) ) THEN
2085         IF( xios_is_valid_axis     (cdid) )   CALL xios_set_axis_attr     ( cdid, n_glo=SIZE(paxis), value=real(paxis, dp) )
2086         IF( xios_is_valid_axisgroup(cdid) )   CALL xios_set_axisgroup_attr( cdid, n_glo=SIZE(paxis), value=real(paxis, dp) )
2087      ENDIF
2088      IF( PRESENT(bounds) ) THEN
2089         IF( xios_is_valid_axis     (cdid) )   CALL xios_set_axis_attr     ( cdid, bounds=real(bounds, dp) )
2090         IF( xios_is_valid_axisgroup(cdid) )   CALL xios_set_axisgroup_attr( cdid, bounds=real(bounds, dp) )
2091      ELSE
2092         IF( xios_is_valid_axis     (cdid) )   CALL xios_set_axis_attr     ( cdid)
2093         IF( xios_is_valid_axisgroup(cdid) )   CALL xios_set_axisgroup_attr( cdid)
2094      END IF
2095      CALL xios_solve_inheritance()
2096   END SUBROUTINE iom_set_axis_attr
2097
2098
2099   SUBROUTINE iom_set_field_attr( cdid, freq_op, freq_offset )
2100      !!----------------------------------------------------------------------
2101      !!----------------------------------------------------------------------
2102      CHARACTER(LEN=*)             , INTENT(in) ::   cdid
2103      TYPE(xios_duration), OPTIONAL, INTENT(in) ::   freq_op
2104      TYPE(xios_duration), OPTIONAL, INTENT(in) ::   freq_offset
2105      !!----------------------------------------------------------------------
2106      IF( xios_is_valid_field     (cdid) )   CALL xios_set_field_attr     ( cdid, freq_op=freq_op, freq_offset=freq_offset )
2107      IF( xios_is_valid_fieldgroup(cdid) )   CALL xios_set_fieldgroup_attr( cdid, freq_op=freq_op, freq_offset=freq_offset )
2108      CALL xios_solve_inheritance()
2109   END SUBROUTINE iom_set_field_attr
2110
2111
2112   SUBROUTINE iom_set_file_attr( cdid, name, name_suffix )
2113      !!----------------------------------------------------------------------
2114      !!----------------------------------------------------------------------
2115      CHARACTER(LEN=*)          , INTENT(in) ::   cdid
2116      CHARACTER(LEN=*),OPTIONAL , INTENT(in) ::   name, name_suffix
2117      !!----------------------------------------------------------------------
2118      IF( xios_is_valid_file     (cdid) )   CALL xios_set_file_attr     ( cdid, name=name, name_suffix=name_suffix )
2119      IF( xios_is_valid_filegroup(cdid) )   CALL xios_set_filegroup_attr( cdid, name=name, name_suffix=name_suffix )
2120      CALL xios_solve_inheritance()
2121   END SUBROUTINE iom_set_file_attr
2122
2123
2124   SUBROUTINE iom_get_file_attr( cdid, name, name_suffix, output_freq )
2125      !!----------------------------------------------------------------------
2126      !!----------------------------------------------------------------------
2127      CHARACTER(LEN=*)          , INTENT(in ) ::   cdid
2128      CHARACTER(LEN=*),OPTIONAL , INTENT(out) ::   name, name_suffix
2129      TYPE(xios_duration), OPTIONAL , INTENT(out) :: output_freq
2130      LOGICAL                                 ::   llexist1,llexist2,llexist3
2131      !---------------------------------------------------------------------
2132      IF( PRESENT( name        ) )   name = ''          ! default values
2133      IF( PRESENT( name_suffix ) )   name_suffix = ''
2134      IF( PRESENT( output_freq ) )   output_freq = xios_duration(0,0,0,0,0,0)
2135      IF( xios_is_valid_file     (cdid) ) THEN
2136         CALL xios_solve_inheritance()
2137         CALL xios_is_defined_file_attr     ( cdid, name = llexist1, name_suffix = llexist2, output_freq = llexist3)
2138         IF(llexist1)   CALL xios_get_file_attr     ( cdid, name = name )
2139         IF(llexist2)   CALL xios_get_file_attr     ( cdid, name_suffix = name_suffix )
2140         IF(llexist3)   CALL xios_get_file_attr     ( cdid, output_freq = output_freq )
2141      ENDIF
2142      IF( xios_is_valid_filegroup(cdid) ) THEN
2143         CALL xios_solve_inheritance()
2144         CALL xios_is_defined_filegroup_attr( cdid, name = llexist1, name_suffix = llexist2, output_freq = llexist3)
2145         IF(llexist1)   CALL xios_get_filegroup_attr( cdid, name = name )
2146         IF(llexist2)   CALL xios_get_filegroup_attr( cdid, name_suffix = name_suffix )
2147         IF(llexist3)   CALL xios_get_filegroup_attr( cdid, output_freq = output_freq )
2148      ENDIF
2149   END SUBROUTINE iom_get_file_attr
2150
2151
2152   SUBROUTINE iom_set_grid_attr( cdid, mask )
2153      !!----------------------------------------------------------------------
2154      !!----------------------------------------------------------------------
2155      CHARACTER(LEN=*)                   , INTENT(in) ::   cdid
2156      LOGICAL, DIMENSION(:,:,:), OPTIONAL, INTENT(in) ::   mask
2157      !!----------------------------------------------------------------------
2158      IF( xios_is_valid_grid     (cdid) )   CALL xios_set_grid_attr     ( cdid, mask_3D=mask )
2159      IF( xios_is_valid_gridgroup(cdid) )   CALL xios_set_gridgroup_attr( cdid, mask_3D=mask )
2160      CALL xios_solve_inheritance()
2161   END SUBROUTINE iom_set_grid_attr
2162
2163   SUBROUTINE iom_setkt( kt, cdname )
2164      !!----------------------------------------------------------------------
2165      !!----------------------------------------------------------------------
2166      INTEGER         , INTENT(in) ::   kt 
2167      CHARACTER(LEN=*), INTENT(in) ::   cdname
2168      !!----------------------------------------------------------------------
2169      CALL iom_swap( cdname )   ! swap to cdname context
2170      CALL xios_update_calendar(kt)
2171      IF( cdname /= TRIM(cxios_context) )   CALL iom_swap( TRIM(cxios_context) )   ! return back to nemo context
2172   END SUBROUTINE iom_setkt
2173
2174   SUBROUTINE iom_context_finalize( cdname )
2175      !!----------------------------------------------------------------------
2176      !!----------------------------------------------------------------------
2177      CHARACTER(LEN=*), INTENT(in) :: cdname
2178      CHARACTER(LEN=120)           :: clname
2179      !!----------------------------------------------------------------------
2180      clname = cdname
2181      IF( TRIM(Agrif_CFixed()) .NE. '0' ) clname = TRIM(Agrif_CFixed())//"_"//clname 
2182      IF( xios_is_valid_context(clname) ) THEN
2183         CALL iom_swap( cdname )   ! swap to cdname context
2184         CALL xios_context_finalize() ! finalize the context
2185         IF( cdname /= TRIM(cxios_context) ) CALL iom_swap( TRIM(cxios_context) )   ! return back to nemo context
2186      ENDIF
2187      !
2188   END SUBROUTINE iom_context_finalize
2189
2190
2191   SUBROUTINE set_grid( cdgrd, plon, plat, ldxios, ldrxios )
2192      !!----------------------------------------------------------------------
2193      !!                     ***  ROUTINE set_grid  ***
2194      !!
2195      !! ** Purpose :   define horizontal grids
2196      !!----------------------------------------------------------------------
2197      CHARACTER(LEN=1)            , INTENT(in) ::   cdgrd
2198      REAL(wp), DIMENSION(jpi,jpj), INTENT(in) ::   plon
2199      REAL(wp), DIMENSION(jpi,jpj), INTENT(in) ::   plat
2200      !
2201      REAL(wp), DIMENSION(jpi,jpj,jpk) ::   zmask
2202      LOGICAL, INTENT(IN) :: ldxios, ldrxios
2203      !!----------------------------------------------------------------------
2204      !
2205      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)
2206      CALL iom_set_domain_attr("grid_"//cdgrd, data_dim=2, data_ibegin = 0, data_ni = Ni_0, data_jbegin = 0, data_nj = Nj_0)
2207!don't define lon and lat for restart reading context.
2208      IF ( .NOT.ldrxios ) &
2209         CALL iom_set_domain_attr("grid_"//cdgrd, lonvalue = real(RESHAPE(plon(Nis0:Nie0, Njs0:Nje0),(/ Ni_0*Nj_0 /)),dp),   &
2210         &                                        latvalue = real(RESHAPE(plat(Nis0:Nie0, Njs0:Nje0),(/ Ni_0*Nj_0 /)),dp )) 
2211      !
2212      IF ( ln_mskland .AND. (.NOT.ldxios) ) THEN
2213         ! mask land points, keep values on coast line -> specific mask for U, V and W points
2214         SELECT CASE ( cdgrd )
2215         CASE('T')   ;   zmask(:,:,:)       = tmask(:,:,:)
2216         CASE('U')   ;   zmask(2:jpim1,:,:) = tmask(2:jpim1,:,:) + tmask(3:jpi,:,:)
2217         CASE('V')   ;   zmask(:,2:jpjm1,:) = tmask(:,2:jpjm1,:) + tmask(:,3:jpj,:)
2218         CASE('W')   ;   zmask(:,:,2:jpk  ) = tmask(:,:,1:jpkm1) + tmask(:,:,2:jpk)   ;   zmask(:,:,1) = tmask(:,:,1)
2219         END SELECT
2220         !
2221         CALL iom_set_domain_attr( "grid_"//cdgrd       , mask = RESHAPE(zmask(Nis0:Nie0,Njs0:Nje0,1),(/Ni_0*Nj_0    /)) /= 0. )
2222         CALL iom_set_grid_attr  ( "grid_"//cdgrd//"_3D", mask = RESHAPE(zmask(Nis0:Nie0,Njs0:Nje0,:),(/Ni_0,Nj_0,jpk/)) /= 0. )
2223      ENDIF
2224      !
2225   END SUBROUTINE set_grid
2226
2227   SUBROUTINE set_grid_bounds( cdgrd, plon_cnr, plat_cnr, plon_pnt, plat_pnt )
2228      !!----------------------------------------------------------------------
2229      !!                   ***  ROUTINE set_grid_bounds  ***
2230      !!
2231      !! ** Purpose :   define horizontal grid corners
2232      !!
2233      !!----------------------------------------------------------------------
2234      CHARACTER(LEN=1)                      , INTENT(in) :: cdgrd
2235      REAL(wp), DIMENSION(jpi,jpj)          , INTENT(in) :: plon_cnr, plat_cnr  ! Lat/lon coord. of a contiguous vertex of cell (i,j)
2236      REAL(wp), DIMENSION(jpi,jpj), OPTIONAL, INTENT(in) :: plon_pnt, plat_pnt  ! Lat/lon coord. of the point of cell (i,j)
2237      !
2238      INTEGER :: ji, jj, jn
2239      INTEGER :: icnr, jcnr                             ! Offset such that the vertex coordinate (i+icnr,j+jcnr)
2240      !                                                 ! represents the
2241      !                                                 bottom-left corner of
2242      !                                                 cell (i,j)
2243      REAL(wp), ALLOCATABLE, DIMENSION(:,:,:,:) :: z_bnds      ! Lat/lon coordinates of the vertices of cell (i,j)
2244      REAL(wp), ALLOCATABLE, DIMENSION(:,:)     :: z_fld       ! Working array to determine where to rotate cells
2245      REAL(wp), ALLOCATABLE, DIMENSION(:,:)     :: z_rot       ! Lat/lon working array for rotation of cells
2246      !!----------------------------------------------------------------------
2247      !
2248      ALLOCATE( z_bnds(4,jpi,jpj,2), z_fld(jpi,jpj), z_rot(4,2)  )
2249      !
2250      ! Offset of coordinate representing bottom-left corner
2251      SELECT CASE ( TRIM(cdgrd) )
2252      CASE ('T', 'W')   ;   icnr = -1   ;   jcnr = -1
2253      CASE ('U')        ;   icnr =  0   ;   jcnr = -1
2254      CASE ('V')        ;   icnr = -1   ;   jcnr =  0
2255      END SELECT
2256      !
2257      z_fld(:,:) = 1._wp
2258      CALL lbc_lnk( 'iom', z_fld, cdgrd, -1.0_wp )    ! Working array for location of northfold
2259      !
2260      ! Cell vertices that can be defined
2261      DO_2D( 0, 0, 0, 0 )
2262         z_bnds(1,ji,jj,1) = plat_cnr(ji+icnr,  jj+jcnr  ) ! Bottom-left
2263         z_bnds(2,ji,jj,1) = plat_cnr(ji+icnr+1,jj+jcnr  ) ! Bottom-right
2264         z_bnds(3,ji,jj,1) = plat_cnr(ji+icnr+1,jj+jcnr+1) ! Top-right
2265         z_bnds(4,ji,jj,1) = plat_cnr(ji+icnr,  jj+jcnr+1) ! Top-left
2266         z_bnds(1,ji,jj,2) = plon_cnr(ji+icnr,  jj+jcnr  ) ! Bottom-left
2267         z_bnds(2,ji,jj,2) = plon_cnr(ji+icnr+1,jj+jcnr  ) ! Bottom-right
2268         z_bnds(3,ji,jj,2) = plon_cnr(ji+icnr+1,jj+jcnr+1) ! Top-right
2269         z_bnds(4,ji,jj,2) = plon_cnr(ji+icnr,  jj+jcnr+1) ! Top-left
2270      END_2D
2271      !
2272      DO_2D( 0, 0, 0, 0 )
2273         IF( z_fld(ji,jj) == -1. ) THEN
2274            z_rot(1,:) = z_bnds(3,ji,jj,:) ; z_rot(2,:) = z_bnds(4,ji,jj,:)
2275            z_rot(3,:) = z_bnds(1,ji,jj,:) ; z_rot(4,:) = z_bnds(2,ji,jj,:)
2276            z_bnds(:,ji,jj,:) = z_rot(:,:)
2277         ENDIF
2278      END_2D
2279      !
2280      CALL iom_set_domain_attr("grid_"//cdgrd, bounds_lat = real(RESHAPE(z_bnds(:,Nis0:Nie0,Njs0:Nje0,1),(/ 4,Ni_0*Nj_0 /)), dp),           &
2281          &                                    bounds_lon = real(RESHAPE(z_bnds(:,Nis0:Nie0,Njs0:Nje0,2),(/ 4,Ni_0*Nj_0 /)), dp), nvertex=4 )
2282      !
2283      DEALLOCATE( z_bnds, z_fld, z_rot )
2284      !
2285   END SUBROUTINE set_grid_bounds
2286
2287   SUBROUTINE set_grid_znl( plat )
2288      !!----------------------------------------------------------------------
2289      !!                     ***  ROUTINE set_grid_znl  ***
2290      !!
2291      !! ** Purpose :   define grids for zonal mean
2292      !!
2293      !!----------------------------------------------------------------------
2294      REAL(wp), DIMENSION(jpi,jpj), INTENT(in) ::   plat
2295      !
2296      INTEGER  :: ix, iy
2297      REAL(wp), DIMENSION(:), ALLOCATABLE  ::   zlon
2298      !!----------------------------------------------------------------------
2299      !
2300      ALLOCATE( zlon(Ni_0*Nj_0) )       ;       zlon(:) = 0._wp
2301      !
2302!      CALL dom_ngb( -168.53_wp, 65.03_wp, ix, iy, 'T' ) !  i-line that passes through Bering Strait: Reference latitude (used in plots)
2303      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)
2304      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)
2305      CALL iom_set_domain_attr("gznl", data_dim=2, data_ibegin = 0, data_ni = Ni_0, data_jbegin = 0, data_nj = Nj_0)
2306      CALL iom_set_domain_attr("gznl", lonvalue = real(zlon, dp),   &
2307         &                             latvalue = real(RESHAPE(plat(Nis0:Nie0, Njs0:Nje0),(/ Ni_0*Nj_0 /)),dp)) 
2308      CALL iom_set_zoom_domain_attr("ptr", ibegin=ix-1, jbegin=0, ni=1, nj=Nj0glo)
2309      !
2310      CALL iom_update_file_name('ptr')
2311      !
2312   END SUBROUTINE set_grid_znl
2313
2314
2315   SUBROUTINE set_scalar
2316      !!----------------------------------------------------------------------
2317      !!                     ***  ROUTINE set_scalar  ***
2318      !!
2319      !! ** Purpose :   define fake grids for scalar point
2320      !!
2321      !!----------------------------------------------------------------------
2322      REAL(dp), DIMENSION(1)   ::   zz = 1.
2323      !!----------------------------------------------------------------------
2324      !
2325      CALL iom_set_domain_attr('scalarpoint', ni_glo=jpnij, nj_glo=1, ibegin=narea-1, jbegin=0, ni=1, nj=1)
2326      CALL iom_set_domain_attr('scalarpoint', data_dim=2, data_ibegin = 1, data_ni = 1, data_jbegin = 1, data_nj = 1)
2327      !
2328      zz = REAL( narea, wp )
2329      CALL iom_set_domain_attr('scalarpoint', lonvalue=zz, latvalue=zz)
2330      !
2331   END SUBROUTINE set_scalar
2332
2333
2334   SUBROUTINE set_xmlatt
2335      !!----------------------------------------------------------------------
2336      !!                     ***  ROUTINE set_xmlatt  ***
2337      !!
2338      !! ** Purpose :   automatic definitions of some of the xml attributs...
2339      !!
2340      !!----------------------------------------------------------------------
2341      CHARACTER(len=1),DIMENSION( 3) ::   clgrd                    ! suffix name
2342      CHARACTER(len=256)             ::   clsuff                   ! suffix name
2343      CHARACTER(len=1)               ::   cl1                      ! 1 character
2344      CHARACTER(len=2)               ::   cl2                      ! 2 characters
2345      CHARACTER(len=3)               ::   cl3                      ! 3 characters
2346      INTEGER                        ::   ji, jg                   ! loop counters
2347      INTEGER                        ::   ix, iy                   ! i-,j- index
2348      REAL(wp)        ,DIMENSION(11) ::   zlontao                  ! longitudes of tao    moorings
2349      REAL(wp)        ,DIMENSION( 7) ::   zlattao                  ! latitudes  of tao    moorings
2350      REAL(wp)        ,DIMENSION( 4) ::   zlonrama                 ! longitudes of rama   moorings
2351      REAL(wp)        ,DIMENSION(11) ::   zlatrama                 ! latitudes  of rama   moorings
2352      REAL(wp)        ,DIMENSION( 3) ::   zlonpira                 ! longitudes of pirata moorings
2353      REAL(wp)        ,DIMENSION( 9) ::   zlatpira                 ! latitudes  of pirata moorings
2354      TYPE(xios_duration)            ::   f_op, f_of
2355      !!----------------------------------------------------------------------
2356      !
2357      ! frequency of the call of iom_put (attribut: freq_op)
2358      f_op%timestep = 1        ;  f_of%timestep =  0  ; CALL iom_set_field_attr('field_definition', freq_op=f_op, freq_offset=f_of)
2359      f_op%timestep = 2        ;  f_of%timestep =  0  ; CALL iom_set_field_attr('trendT_even'     , freq_op=f_op, freq_offset=f_of)
2360      f_op%timestep = 2        ;  f_of%timestep = -1  ; CALL iom_set_field_attr('trendT_odd'      , freq_op=f_op, freq_offset=f_of)
2361      f_op%timestep = nn_fsbc  ;  f_of%timestep =  0  ; CALL iom_set_field_attr('SBC'             , freq_op=f_op, freq_offset=f_of)
2362      f_op%timestep = nn_fsbc  ;  f_of%timestep =  0  ; CALL iom_set_field_attr('SBC_scalar'      , freq_op=f_op, freq_offset=f_of)
2363      f_op%timestep = nn_fsbc  ;  f_of%timestep =  0  ; CALL iom_set_field_attr('ABL'             , freq_op=f_op, freq_offset=f_of)
2364
2365      ! output file names (attribut: name)
2366      DO ji = 1, 9
2367         WRITE(cl1,'(i1)') ji 
2368         CALL iom_update_file_name('file'//cl1)
2369      END DO
2370      DO ji = 1, 99
2371         WRITE(cl2,'(i2.2)') ji 
2372         CALL iom_update_file_name('file'//cl2)
2373      END DO
2374      DO ji = 1, 999
2375         WRITE(cl3,'(i3.3)') ji 
2376         CALL iom_update_file_name('file'//cl3)
2377      END DO
2378
2379      ! Zooms...
2380      clgrd = (/ 'T', 'U', 'W' /) 
2381      DO jg = 1, SIZE(clgrd)                                                                   ! grid type
2382         cl1 = clgrd(jg)
2383         ! Equatorial section (attributs: jbegin, ni, name_suffix)
2384         CALL dom_ngb( 0.0_wp, 0.0_wp, ix, iy, cl1 )
2385         CALL iom_set_zoom_domain_attr('Eq'//cl1, ibegin=0, jbegin=iy-1, ni=Ni0glo, nj=1 )
2386         CALL iom_get_file_attr   ('Eq'//cl1, name_suffix = clsuff             )
2387         CALL iom_set_file_attr   ('Eq'//cl1, name_suffix = TRIM(clsuff)//'_Eq')
2388         CALL iom_update_file_name('Eq'//cl1)
2389      END DO
2390      ! TAO moorings (attributs: ibegin, jbegin, name_suffix)
2391      zlontao = (/ 137.0, 147.0, 156.0, 165.0, -180.0, -170.0, -155.0, -140.0, -125.0, -110.0, -95.0 /)
2392      zlattao = (/  -8.0,  -5.0,  -2.0,   0.0,    2.0,    5.0,    8.0 /)
2393      CALL set_mooring( zlontao, zlattao )
2394      ! RAMA moorings (attributs: ibegin, jbegin, name_suffix)
2395      zlonrama = (/  55.0,  67.0, 80.5, 90.0 /)
2396      zlatrama = (/ -16.0, -12.0, -8.0, -4.0, -1.5, 0.0, 1.5, 4.0, 8.0, 12.0, 15.0 /)
2397      CALL set_mooring( zlonrama, zlatrama )
2398      ! PIRATA moorings (attributs: ibegin, jbegin, name_suffix)
2399      zlonpira = (/ -38.0, -23.0, -10.0 /)
2400      zlatpira = (/ -19.0, -14.0,  -8.0, 0.0, 4.0, 8.0, 12.0, 15.0, 20.0 /)
2401      CALL set_mooring( zlonpira, zlatpira )
2402      !
2403   END SUBROUTINE set_xmlatt
2404
2405
2406   SUBROUTINE set_mooring( plon, plat )
2407      !!----------------------------------------------------------------------
2408      !!                     ***  ROUTINE set_mooring  ***
2409      !!
2410      !! ** Purpose :   automatic definitions of moorings xml attributs...
2411      !!
2412      !!----------------------------------------------------------------------
2413      REAL(wp), DIMENSION(:), INTENT(in) ::   plon, plat   ! longitudes/latitudes oft the mooring
2414      !
2415!!$      CHARACTER(len=1),DIMENSION(4) ::   clgrd = (/ 'T', 'U', 'V', 'W' /)   ! suffix name
2416      CHARACTER(len=1),DIMENSION(1) ::   clgrd = (/ 'T' /)        ! suffix name
2417      CHARACTER(len=256)            ::   clname                   ! file name
2418      CHARACTER(len=256)            ::   clsuff                   ! suffix name
2419      CHARACTER(len=1)              ::   cl1                      ! 1 character
2420      CHARACTER(len=6)              ::   clon,clat                ! name of longitude, latitude
2421      INTEGER                       ::   ji, jj, jg               ! loop counters
2422      INTEGER                       ::   ix, iy                   ! i-,j- index
2423      REAL(wp)                      ::   zlon, zlat
2424      !!----------------------------------------------------------------------
2425      DO jg = 1, SIZE(clgrd)
2426         cl1 = clgrd(jg)
2427         DO ji = 1, SIZE(plon)
2428            DO jj = 1, SIZE(plat)
2429               zlon = plon(ji)
2430               zlat = plat(jj)
2431               ! modifications for RAMA moorings
2432               IF( zlon ==  67. .AND. zlat ==  15. )   zlon =  65.
2433               IF( zlon ==  90. .AND. zlat <=  -4. )   zlon =  95.
2434               IF( zlon ==  95. .AND. zlat ==  -4. )   zlat =  -5.
2435               ! modifications for PIRATA moorings
2436               IF( zlon == -38. .AND. zlat == -19. )   zlon = -34.
2437               IF( zlon == -38. .AND. zlat == -14. )   zlon = -32.
2438               IF( zlon == -38. .AND. zlat ==  -8. )   zlon = -30.
2439               IF( zlon == -38. .AND. zlat ==   0. )   zlon = -35.
2440               IF( zlon == -23. .AND. zlat ==  20. )   zlat =  21.
2441               IF( zlon == -10. .AND. zlat == -14. )   zlat = -10.
2442               IF( zlon == -10. .AND. zlat ==  -8. )   zlat =  -6.
2443               IF( zlon == -10. .AND. zlat ==   4. ) THEN   ;   zlon = 0.   ;   zlat = 0.   ;   ENDIF
2444               CALL dom_ngb( zlon, zlat, ix, iy, cl1 )
2445               IF( zlon >= 0. ) THEN 
2446                  IF( zlon == REAL(NINT(zlon), wp) ) THEN   ;   WRITE(clon, '(i3,  a)') NINT( zlon), 'e'
2447                  ELSE                                      ;   WRITE(clon, '(f5.1,a)')       zlon , 'e'
2448                  ENDIF
2449               ELSE             
2450                  IF( zlon == REAL(NINT(zlon), wp) ) THEN   ;   WRITE(clon, '(i3,  a)') NINT(-zlon), 'w'
2451                  ELSE                                      ;   WRITE(clon, '(f5.1,a)')      -zlon , 'w'
2452                  ENDIF
2453               ENDIF
2454               IF( zlat >= 0. ) THEN 
2455                  IF( zlat == REAL(NINT(zlat), wp) ) THEN   ;   WRITE(clat, '(i2,  a)') NINT( zlat), 'n'
2456                  ELSE                                      ;   WRITE(clat, '(f4.1,a)')       zlat , 'n'
2457                  ENDIF
2458               ELSE             
2459                  IF( zlat == REAL(NINT(zlat), wp) ) THEN   ;   WRITE(clat, '(i2,  a)') NINT(-zlat), 's'
2460                  ELSE                                      ;   WRITE(clat, '(f4.1,a)')      -zlat , 's'
2461                  ENDIF
2462               ENDIF
2463               clname = TRIM(ADJUSTL(clat))//TRIM(ADJUSTL(clon))
2464               CALL iom_set_zoom_domain_attr(TRIM(clname)//cl1, ibegin= ix-1, jbegin= iy-1, ni=1, nj=1)
2465
2466               CALL iom_get_file_attr   (TRIM(clname)//cl1, name_suffix = clsuff                         )
2467               CALL iom_set_file_attr   (TRIM(clname)//cl1, name_suffix = TRIM(clsuff)//'_'//TRIM(clname))
2468               CALL iom_update_file_name(TRIM(clname)//cl1)
2469            END DO
2470         END DO
2471      END DO
2472     
2473   END SUBROUTINE set_mooring
2474
2475   
2476   SUBROUTINE iom_update_file_name( cdid )
2477      !!----------------------------------------------------------------------
2478      !!                     ***  ROUTINE iom_update_file_name  ***
2479      !!
2480      !! ** Purpose :   
2481      !!
2482      !!----------------------------------------------------------------------
2483      CHARACTER(LEN=*)          , INTENT(in) ::   cdid
2484      !
2485      CHARACTER(LEN=256) ::   clname
2486      CHARACTER(LEN=20)  ::   clfreq
2487      CHARACTER(LEN=20)  ::   cldate
2488      INTEGER            ::   idx
2489      INTEGER            ::   jn
2490      INTEGER            ::   itrlen
2491      INTEGER            ::   iyear, imonth, iday, isec
2492      REAL(wp)           ::   zsec
2493      LOGICAL            ::   llexist
2494      TYPE(xios_duration)   ::   output_freq 
2495      !!----------------------------------------------------------------------
2496      !
2497      DO jn = 1, 2
2498         !
2499         output_freq = xios_duration(0,0,0,0,0,0)
2500         IF( jn == 1 )   CALL iom_get_file_attr( cdid, name        = clname, output_freq = output_freq )
2501         IF( jn == 2 )   CALL iom_get_file_attr( cdid, name_suffix = clname )
2502         !
2503         IF ( TRIM(clname) /= '' ) THEN 
2504            !
2505            idx = INDEX(clname,'@expname@') + INDEX(clname,'@EXPNAME@')
2506            DO WHILE ( idx /= 0 ) 
2507               clname = clname(1:idx-1)//TRIM(cexper)//clname(idx+9:LEN_TRIM(clname))
2508               idx = INDEX(clname,'@expname@') + INDEX(clname,'@EXPNAME@')
2509            END DO
2510            !
2511            idx = INDEX(clname,'@freq@') + INDEX(clname,'@FREQ@')
2512            DO WHILE ( idx /= 0 ) 
2513              IF ( output_freq%timestep /= 0) THEN
2514                  WRITE(clfreq,'(I18,A2)')INT(output_freq%timestep),'ts' 
2515                  itrlen = LEN_TRIM(ADJUSTL(clfreq))
2516              ELSE IF ( output_freq%second /= 0 ) THEN
2517                  WRITE(clfreq,'(I19,A1)')INT(output_freq%second),'s' 
2518                  itrlen = LEN_TRIM(ADJUSTL(clfreq))
2519              ELSE IF ( output_freq%minute /= 0 ) THEN
2520                  WRITE(clfreq,'(I18,A2)')INT(output_freq%minute),'mi' 
2521                  itrlen = LEN_TRIM(ADJUSTL(clfreq))
2522              ELSE IF ( output_freq%hour /= 0 ) THEN
2523                  WRITE(clfreq,'(I19,A1)')INT(output_freq%hour),'h' 
2524                  itrlen = LEN_TRIM(ADJUSTL(clfreq))
2525              ELSE IF ( output_freq%day /= 0 ) THEN
2526                  WRITE(clfreq,'(I19,A1)')INT(output_freq%day),'d' 
2527                  itrlen = LEN_TRIM(ADJUSTL(clfreq))
2528              ELSE IF ( output_freq%month /= 0 ) THEN   
2529                  WRITE(clfreq,'(I19,A1)')INT(output_freq%month),'m' 
2530                  itrlen = LEN_TRIM(ADJUSTL(clfreq))
2531              ELSE IF ( output_freq%year /= 0 ) THEN   
2532                  WRITE(clfreq,'(I19,A1)')INT(output_freq%year),'y' 
2533                  itrlen = LEN_TRIM(ADJUSTL(clfreq))
2534              ELSE
2535                  CALL ctl_stop('error in the name of file id '//TRIM(cdid),   &
2536                     & ' attribute output_freq is undefined -> cannot replace @freq@ in '//TRIM(clname) )
2537              ENDIF
2538              clname = clname(1:idx-1)//TRIM(ADJUSTL(clfreq))//clname(idx+6:LEN_TRIM(clname))
2539              idx = INDEX(clname,'@freq@') + INDEX(clname,'@FREQ@')
2540            END DO
2541            !
2542            idx = INDEX(clname,'@startdate@') + INDEX(clname,'@STARTDATE@')
2543            DO WHILE ( idx /= 0 ) 
2544               cldate = iom_sdate( fjulday - rn_Dt / rday )
2545               clname = clname(1:idx-1)//TRIM(cldate)//clname(idx+11:LEN_TRIM(clname))
2546               idx = INDEX(clname,'@startdate@') + INDEX(clname,'@STARTDATE@')
2547            END DO
2548            !
2549            idx = INDEX(clname,'@startdatefull@') + INDEX(clname,'@STARTDATEFULL@')
2550            DO WHILE ( idx /= 0 ) 
2551               cldate = iom_sdate( fjulday - rn_Dt / rday, ldfull = .TRUE. )
2552               clname = clname(1:idx-1)//TRIM(cldate)//clname(idx+15:LEN_TRIM(clname))
2553               idx = INDEX(clname,'@startdatefull@') + INDEX(clname,'@STARTDATEFULL@')
2554            END DO
2555            !
2556            idx = INDEX(clname,'@enddate@') + INDEX(clname,'@ENDDATE@')
2557            DO WHILE ( idx /= 0 ) 
2558               cldate = iom_sdate( fjulday + rn_Dt / rday * REAL( nitend - nit000, wp ), ld24 = .TRUE. )
2559               clname = clname(1:idx-1)//TRIM(cldate)//clname(idx+9:LEN_TRIM(clname))
2560               idx = INDEX(clname,'@enddate@') + INDEX(clname,'@ENDDATE@')
2561            END DO
2562            !
2563            idx = INDEX(clname,'@enddatefull@') + INDEX(clname,'@ENDDATEFULL@')
2564            DO WHILE ( idx /= 0 ) 
2565               cldate = iom_sdate( fjulday + rn_Dt / rday * REAL( nitend - nit000, wp ), ld24 = .TRUE., ldfull = .TRUE. )
2566               clname = clname(1:idx-1)//TRIM(cldate)//clname(idx+13:LEN_TRIM(clname))
2567               idx = INDEX(clname,'@enddatefull@') + INDEX(clname,'@ENDDATEFULL@')
2568            END DO
2569            !
2570            IF( jn == 1 .AND. TRIM(Agrif_CFixed()) /= '0' )   clname = TRIM(Agrif_CFixed())//"_"//TRIM(clname)
2571            IF( jn == 1 )   CALL iom_set_file_attr( cdid, name        = clname )
2572            IF( jn == 2 )   CALL iom_set_file_attr( cdid, name_suffix = clname )
2573            !
2574         ENDIF
2575         !
2576      END DO
2577      !
2578   END SUBROUTINE iom_update_file_name
2579
2580
2581   FUNCTION iom_sdate( pjday, ld24, ldfull )
2582      !!----------------------------------------------------------------------
2583      !!                     ***  ROUTINE iom_sdate  ***
2584      !!
2585      !! ** Purpose :   send back the date corresponding to the given julian day
2586      !!----------------------------------------------------------------------
2587      REAL(wp), INTENT(in   )           ::   pjday    ! julian day
2588      LOGICAL , INTENT(in   ), OPTIONAL ::   ld24     ! true to force 24:00 instead of 00:00
2589      LOGICAL , INTENT(in   ), OPTIONAL ::   ldfull   ! true to get the compleate date: yyyymmdd_hh:mm:ss
2590      !
2591      CHARACTER(LEN=20) ::   iom_sdate
2592      CHARACTER(LEN=50) ::   clfmt                         !  format used to write the date
2593      INTEGER           ::   iyear, imonth, iday, ihour, iminute, isec
2594      REAL(wp)          ::   zsec
2595      LOGICAL           ::   ll24, llfull
2596      !!----------------------------------------------------------------------
2597      !
2598      IF( PRESENT(ld24) ) THEN   ;   ll24 = ld24
2599      ELSE                       ;   ll24 = .FALSE.
2600      ENDIF
2601      !
2602      IF( PRESENT(ldfull) ) THEN   ;   llfull = ldfull
2603      ELSE                         ;   llfull = .FALSE.
2604      ENDIF
2605      !
2606      CALL ju2ymds( pjday, iyear, imonth, iday, zsec )
2607      isec = NINT(zsec)
2608      !
2609      IF ( ll24 .AND. isec == 0 ) THEN   ! 00:00 of the next day -> move to 24:00 of the current day
2610         CALL ju2ymds( pjday - 1.0_wp, iyear, imonth, iday, zsec )
2611         isec = 86400
2612      ENDIF
2613      !
2614      IF( iyear < 10000 ) THEN   ;   clfmt = "i4.4,2i2.2"                ! format used to write the date
2615      ELSE                       ;   WRITE(clfmt, "('i',i1,',2i2.2')") INT(LOG10(REAL(iyear,wp))) + 1
2616      ENDIF
2617      !
2618!$AGRIF_DO_NOT_TREAT     
2619      ! needed in the conv
2620      IF( llfull ) THEN
2621         clfmt = TRIM(clfmt)//",'_',i2.2,':',i2.2,':',i2.2"
2622         ihour   = isec / 3600
2623         isec    = MOD(isec, 3600)
2624         iminute = isec / 60
2625         isec    = MOD(isec, 60)
2626         WRITE(iom_sdate, '('//TRIM(clfmt)//')') iyear, imonth, iday, ihour, iminute, isec    ! date of the end of run
2627      ELSE
2628         WRITE(iom_sdate, '('//TRIM(clfmt)//')') iyear, imonth, iday                          ! date of the end of run
2629      ENDIF
2630!$AGRIF_END_DO_NOT_TREAT     
2631      !
2632   END FUNCTION iom_sdate
2633
2634#else
2635   !!----------------------------------------------------------------------
2636   !!   NOT 'key_iomput'                               a few dummy routines
2637   !!----------------------------------------------------------------------
2638   SUBROUTINE iom_setkt( kt, cdname )
2639      INTEGER         , INTENT(in)::   kt 
2640      CHARACTER(LEN=*), INTENT(in) ::   cdname
2641      IF( .FALSE. )   WRITE(numout,*) kt, cdname   ! useless test to avoid compilation warnings
2642   END SUBROUTINE iom_setkt
2643
2644   SUBROUTINE iom_context_finalize( cdname )
2645      CHARACTER(LEN=*), INTENT(in) ::   cdname
2646      IF( .FALSE. )   WRITE(numout,*)  cdname   ! useless test to avoid compilation warnings
2647   END SUBROUTINE iom_context_finalize
2648
2649   SUBROUTINE iom_update_file_name( cdid )
2650      CHARACTER(LEN=*), INTENT(in) ::   cdid
2651      IF( .FALSE. )   WRITE(numout,*)  cdid   ! useless test to avoid compilation warnings
2652   END SUBROUTINE iom_update_file_name
2653
2654#endif
2655
2656   LOGICAL FUNCTION iom_use( cdname )
2657      CHARACTER(LEN=*), INTENT(in) ::   cdname
2658#if defined key_iomput
2659      iom_use = xios_field_is_active( cdname )
2660#else
2661      iom_use = .FALSE.
2662#endif
2663   END FUNCTION iom_use
2664
2665   SUBROUTINE iom_miss_val( cdname, pmiss_val )
2666      CHARACTER(LEN=*), INTENT(in ) ::   cdname
2667      REAL(wp)        , INTENT(out) ::   pmiss_val   
2668      REAL(dp)                      ::   ztmp_pmiss_val   
2669#if defined key_iomput
2670      ! get missing value
2671      CALL xios_get_field_attr( cdname, default_value = ztmp_pmiss_val )
2672      pmiss_val = ztmp_pmiss_val
2673#else
2674      IF( .FALSE. )   WRITE(numout,*) cdname, pmiss_val   ! useless test to avoid compilation warnings
2675      IF( .FALSE. )   pmiss_val = 0._wp                   ! useless assignment to avoid compilation warnings
2676#endif
2677   END SUBROUTINE iom_miss_val
2678 
2679   !!======================================================================
2680END MODULE iom
Note: See TracBrowser for help on using the repository browser.