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/dev_r10984_HPC-13_IRRMANN_BDY_optimization/src/OCE/IOM – NEMO

source: NEMO/branches/2019/dev_r10984_HPC-13_IRRMANN_BDY_optimization/src/OCE/IOM/iom.F90 @ 11362

Last change on this file since 11362 was 11362, checked in by clem, 5 years ago

debug the ice output by adding a missing value to the outputed fields. Unfortunately this method imposes a value of 1.e20 where there is no ice, which is annoying when using ncview for example, but this is the only way (from what I know) to output averages

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