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

source: NEMO/trunk/src/OCE/IOM/iom.F90 @ 9802

Last change on this file since 9802 was 9802, checked in by smasson, 6 years ago

trunk: put back data in outputs file for e-w periodicity, see #2100

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