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

source: NEMO/branches/2020/dev_12905_xios_restart/src/OCE/IOM/iom.F90 @ 13728

Last change on this file since 13728 was 13728, checked in by andmirek, 4 years ago

Ticket #2462: merge changes 13702,13717,13718 from #2386

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