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

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

source: NEMO/branches/UKMO/NEMO_4.0.4_CO9_shelf_climate/src/OCE/IOM/iom.F90 @ 15309

Last change on this file since 15309 was 14078, checked in by cguiavarch, 4 years ago

UKMO/NEMO_4.0.4_GO8_package: copy over changes from NEMO_4.0.3_GO8_package branch @13647.

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