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

source: NEMO/branches/2019/dev_r12072_MERGE_OPTION2_2019/src/OCE/IOM/iom.F90 @ 12154

Last change on this file since 12154 was 12154, checked in by cetlod, 4 years ago

commit

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