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

source: NEMO/trunk/src/OCE/IOM/iom.F90 @ 12597

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

trunk: ice_agrif bugfix, see #2421

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