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/UKMO/NEMO_4.0.4_icesheet_and_river_coupling/src/OCE/IOM – NEMO

source: NEMO/branches/UKMO/NEMO_4.0.4_icesheet_and_river_coupling/src/OCE/IOM/iom.F90 @ 14643

Last change on this file since 14643 was 14643, checked in by dancopsey, 3 years ago

Output timing information

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