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

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

source: NEMO/branches/UKMO/dev_r9950_GO6_mixing/src/OCE/IOM/iom.F90 @ 10013

Last change on this file since 10013 was 10013, checked in by davestorkey, 6 years ago

UKMO dev_r9950_GO6_mixing branch : update to be relative to rev 10011 of NEMO4_beta_mirror branch.

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