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

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

source: NEMO/branches/2019/ENHANCE-02_ISF_nemo_TEST_MERGE/src/OCE/IOM/iom.F90 @ 11967

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

2019/ENHANCE-02_ISF_nemo_TEST_MERGE : Update to rev 11953.

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