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

source: NEMO/branches/2020/r12377_ticket2386/src/OCE/IOM/iom.F90 @ 13542

Last change on this file since 13542 was 13542, checked in by andmirek, 4 years ago

Ticket #2386: additional fixes for trunk changes

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