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

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

source: NEMO/branches/2019/dev_r11943_MERGE_2019/src/OCE/IOM/iom.F90 @ 12150

Last change on this file since 12150 was 12150, checked in by davestorkey, 4 years ago

2019/dev_r11943_MERGE_2019: Merge in UKMO_MERGE_2019.

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