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 @ 11275

Last change on this file since 11275 was 11275, checked in by smasson, 5 years ago

dev_r11265_ABL : add ABL interface, see #2131

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