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 branches/UKMO/dev_r7573_xios_write/NEMOGCM/NEMO/OPA_SRC/IOM – NEMO

source: branches/UKMO/dev_r7573_xios_write/NEMOGCM/NEMO/OPA_SRC/IOM/iom.F90 @ 8299

Last change on this file since 8299 was 8299, checked in by andmirek, 7 years ago

#1882 no need to define context in iodef.xml

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