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

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

source: NEMO/branches/2020/dev_r12527_Gurvan_ShallowWater/src/SWE/iom.F90 @ 13047

Last change on this file since 13047 was 13047, checked in by gm, 4 years ago

F grid available to XIOS

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