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

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

source: NEMO/branches/2019/dev_r11265_ASINTER-01_Guillaume_ABL1D/src/OCE/IOM/iom.F90 @ 11413

Last change on this file since 11413 was 11413, checked in by gsamson, 5 years ago

dev_r11265_ABL : see #2131

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