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

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

source: NEMO/branches/2019/ENHANCE-03_domcfg/src/iom.F90 @ 11129

Last change on this file since 11129 was 11129, checked in by mathiot, 5 years ago

simplification of domcfg (rm all var_n and var_b as it is not needed) (ticket #2143)

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