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

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

source: NEMO/branches/UKMO/NEMO_4.0.1_GO8_package/src/OCE/IOM/iom.F90

Last change on this file was 12574, checked in by cguiavarch, 4 years ago

Add Equation of State tag to salinity and temperature outputs and cell_methods attribute for mean_nemo (mean_nemo_wrapper not needed anymore)

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