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/2018/dev_r9838_ENHANCE04_RK3/src/OCE/IOM – NEMO

source: NEMO/branches/2018/dev_r9838_ENHANCE04_RK3/src/OCE/IOM/iom.F90 @ 10009

Last change on this file since 10009 was 10009, checked in by gm, 6 years ago

#1911 (ENHANCE-04): RK3 branch - step II.1 time-level dimension on ssh

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