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 branches/2017/dev_merge_2017/NEMOGCM/NEMO/OCE_SRC/IOM – NEMO

source: branches/2017/dev_merge_2017/NEMOGCM/NEMO/OCE_SRC/IOM/iom.F90 @ 9580

Last change on this file since 9580 was 9580, checked in by nicolasmartin, 6 years ago

Push again float bugfix for #2069 already done on the trunk after the revision was set for MP, the other commit r9401 from Tomas is not going to be propagated

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