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

source: NEMO/branches/2020/dev_12905_xios_restart/src/OCE/IOM/iom.F90 @ 12914

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

Ticket #2462: merge changes from ticket #2386 to fix XIOS write-read restart

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