source: CONFIG_DEVT/IPSLCM6.5_work_ENSEMBLES/config/IPSLCM6/SOURCES/NEMO/iom.F90 @ 5866

Last change on this file since 5866 was 5866, checked in by ymipsl, 3 years ago

Adapt NEMO to ensemble management.

Yann

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