New URL for NEMO forge!   http://forge.nemo-ocean.eu

Since March 2022 along with NEMO 4.2 release, the code development moved to a self-hosted GitLab.
This present forge is now archived and remained online for history.
iom.F90 in NEMO/branches/2020/dev_r12558_HPC-08_epico_Extra_Halo/src/OCE/IOM – NEMO

source: NEMO/branches/2020/dev_r12558_HPC-08_epico_Extra_Halo/src/OCE/IOM/iom.F90 @ 12745

Last change on this file since 12745 was 12745, checked in by smasson, 4 years ago

Extra_Halo: iom cleaning and fix ICB restartability, see #2366

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