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

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

source: NEMO/branches/UKMO/dev_r9950_old_tidal_mixing/src/OCE/IOM/iom.F90 @ 10324

Last change on this file since 10324 was 10324, checked in by davestorkey, 5 years ago

UKMO/dev_r9950_old_tidal_mixing: Update to be relative to rev 10321 of NEMO4_beta_mirror branch.

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