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

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

source: NEMO/branches/UKMO/dev_r9922_GC3_cpl_pkg/src/OCE/IOM/iom.F90 @ 9947

Last change on this file since 9947 was 9947, checked in by timgraham, 6 years ago

Clear svn keywords

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