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

source: NEMO/branches/2019/dev_r11943_MERGE_2019/src/OCE/IOM/iom.F90 @ 12182

Last change on this file since 12182 was 12182, checked in by davestorkey, 4 years ago

2019/dev_r11943_MERGE_2019: Merge in dev_ASINTER-01-05_merge.

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