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

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

source: NEMO/branches/UKMO/dev_r12866_HPC-02_Daley_Tiling_trial_extra_halo/src/OCE/IOM/iom.F90 @ 12962

Last change on this file since 12962 was 12962, checked in by hadcv, 4 years ago

Update with [12960] fixes

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