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 @ 8310

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

#1882 works with AGRIF and few small fixes/changes

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