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/2020/dev_r12512_HPC-04_mcastril_Mixed_Precision_implementation/src/OCE/IOM – NEMO

source: NEMO/branches/2020/dev_r12512_HPC-04_mcastril_Mixed_Precision_implementation/src/OCE/IOM/iom.F90 @ 13220

Last change on this file since 13220 was 13220, checked in by orioltp, 4 years ago

dev_r12512_HPC-04_mcastril_Mixed_Precision_implementation: updating from trunk r13218

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