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

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

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

Last change on this file since 10016 was 10016, checked in by cbricaud, 6 years ago

commit bugfix for ticket #2085

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