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

Last change on this file since 8301 was 8301, checked in by andmirek, 6 years ago

no scalar domain value in output file

File size: 126.7 KB
Line 
1MODULE iom
2   !!=====================================================================
3   !!                    ***  MODULE  iom ***
4   !! Input/Output manager :  Library to read input files
5   !!====================================================================
6   !! History :  2.0  ! 2005-12  (J. Belier) Original code
7   !!            2.0  ! 2006-02  (S. Masson) Adaptation to NEMO
8   !!            3.0  ! 2007-07  (D. Storkey) Changes to iom_gettime
9   !!            3.4  ! 2012-12  (R. Bourdalle-Badie and G. Reffray)  add C1D case 
10   !!--------------------------------------------------------------------
11
12   !!--------------------------------------------------------------------
13   !!   iom_open       : open a file read only
14   !!   iom_close      : close a file or all files opened by iom
15   !!   iom_get        : read a field (interfaced to several routines)
16   !!   iom_gettime    : read the time axis cdvar in the file
17   !!   iom_varid      : get the id of a variable in a file
18   !!   iom_rstput     : write a field in a restart file (interfaced to several routines)
19   !!--------------------------------------------------------------------
20   USE dom_oce         ! ocean space and time domain
21   USE c1d             ! 1D vertical configuration
22   USE flo_oce         ! floats module declarations
23   USE lbclnk          ! lateal boundary condition / mpp exchanges
24   USE iom_def         ! iom variables definitions
25   USE iom_ioipsl      ! NetCDF format with IOIPSL library
26   USE iom_nf90        ! NetCDF format with native NetCDF library
27   USE iom_rstdimg     ! restarts access direct format "dimg" style...
28   USE in_out_manager  ! I/O manager
29   USE lib_mpp           ! MPP library
30#if defined key_iomput
31   USE sbc_oce, ONLY :   nn_fsbc         ! ocean space and time domain
32   USE trc_oce, ONLY :   nn_dttrc        !  !: frequency of step on passive tracers
33   USE icb_oce, ONLY :   nclasses, class_num       !  !: iceberg classes
34#if defined key_lim3
35   USE ice    , ONLY :   jpl
36#elif defined key_lim2
37   USE par_ice_2
38#endif
39   USE domngb          ! ocean space and time domain
40   USE phycst          ! physical constants
41   USE dianam          ! build name of file
42   USE xios
43   USE iom_def, ONLY : wxioso
44# endif
45   USE ioipsl, ONLY :  ju2ymds    ! for calendar
46   USE crs             ! Grid coarsening
47   USE sbc_oce, ONLY : lk_oasis, ln_coupled_iceshelf_fluxes, ln_apr_dyn, ln_rnf, nn_components, jp_iam_sas
48   USE diadct, ONLY : lk_diadct
49
50   IMPLICIT NONE
51   !  values needed to set correctlyfiles in reast file when using XIOS for writing
52   LOGICAL, PUBLIC :: lr_vvl_ztilde, lr_vvl_layer, lr_traadv_cen2
53
54   PUBLIC   !   must be public to be able to access iom_def through iom
55#if defined key_iomput
56   LOGICAL, PUBLIC, PARAMETER ::   lk_iomput = .TRUE.        !: iom_put flag
57#else
58   LOGICAL, PUBLIC, PARAMETER ::   lk_iomput = .FALSE.       !: iom_put flag
59#endif
60
61   PUBLIC iom_init, iom_swap, iom_open, iom_close, iom_setkt, iom_varid, iom_get, iom_gettime, iom_rstput, iom_put
62   PUBLIC iom_getatt, iom_use, iom_context_finalize
63
64   PRIVATE iom_rp0d, iom_rp1d, iom_rp2d, iom_rp3d
65   PRIVATE iom_g0d, iom_g1d, iom_g2d, iom_g3d, iom_get_123d
66   PRIVATE iom_p1d, iom_p2d, iom_p3d
67#if defined key_iomput
68   PRIVATE iom_set_domain_attr, iom_set_axis_attr, iom_set_field_attr, iom_set_file_attr, iom_get_file_attr, iom_set_grid_attr
69   PRIVATE set_grid, set_grid_bounds, set_scalar, set_xmlatt, set_mooring, iom_update_file_name, iom_sdate
70   PRIVATE set_rstw_active
71# endif
72
73   INTERFACE iom_get
74      MODULE PROCEDURE iom_g0d, iom_g1d, iom_g2d, iom_g3d
75   END INTERFACE
76   INTERFACE iom_getatt
77      MODULE PROCEDURE iom_g0d_intatt
78   END INTERFACE
79   INTERFACE iom_rstput
80      MODULE PROCEDURE iom_rp0d, iom_rp1d, iom_rp2d, iom_rp3d
81   END INTERFACE
82  INTERFACE iom_put
83     MODULE PROCEDURE iom_p0d, iom_p1d, iom_p2d, iom_p3d
84  END INTERFACE
85
86   !!----------------------------------------------------------------------
87   !! NEMO/OPA 3.3 , NEMO Consortium (2010)
88   !! $Id$
89   !! Software governed by the CeCILL licence (NEMOGCM/NEMO_CeCILL.txt)
90   !!----------------------------------------------------------------------
91
92CONTAINS
93
94   SUBROUTINE iom_init( cdname ) 
95      !!----------------------------------------------------------------------
96      !!                     ***  ROUTINE   ***
97      !!
98      !! ** Purpose :   
99      !!
100      !!----------------------------------------------------------------------
101      CHARACTER(len=*), INTENT(in)  :: cdname
102#if defined key_iomput
103#if ! defined key_xios2
104      TYPE(xios_time)     :: dtime    = xios_time(0, 0, 0, 0, 0, 0)
105      CHARACTER(len=19)   :: cldate 
106#else
107      TYPE(xios_duration) :: dtime    = xios_duration(0, 0, 0, 0, 0, 0)
108      TYPE(xios_date)     :: start_date
109#endif
110      CHARACTER(len=10)   :: clname
111      INTEGER             :: ji
112      !
113      REAL(wp), ALLOCATABLE, DIMENSION(:,:) :: z_bnds
114      !!----------------------------------------------------------------------
115#if ! defined key_xios2
116      ALLOCATE( z_bnds(jpk,2) )
117#else
118      ALLOCATE( z_bnds(2,jpk) )
119#endif
120
121      clname = cdname
122      IF( TRIM(Agrif_CFixed()) /= '0' )   clname = TRIM(Agrif_CFixed())//"_"//TRIM(cdname)
123      CALL xios_context_initialize(TRIM(clname), mpi_comm_opa)
124      CALL iom_swap( cdname )
125
126      ! calendar parameters
127#if ! defined key_xios2
128      SELECT CASE ( nleapy )        ! Choose calendar for IOIPSL
129      CASE ( 1)   ;   CALL xios_set_context_attr(TRIM(clname), calendar_type= "Gregorian")
130      CASE ( 0)   ;   CALL xios_set_context_attr(TRIM(clname), calendar_type= "NoLeap")
131      CASE (30)   ;   CALL xios_set_context_attr(TRIM(clname), calendar_type= "D360")
132      END SELECT
133      WRITE(cldate,"(i4.4,'-',i2.2,'-',i2.2,' 00:00:00')") nyear,nmonth,nday 
134      CALL xios_set_context_attr(TRIM(clname), start_date=cldate )
135#else
136      ! Calendar type is now defined in xml file
137      SELECT CASE ( nleapy )        ! Choose calendar for IOIPSL
138      CASE ( 1)   ; CALL xios_define_calendar( TYPE = "Gregorian", time_origin = xios_date(1900,01,01,00,00,00), &
139          &                                    start_date = xios_date(nyear,nmonth,nday,0,0,0) )
140      CASE ( 0)   ; CALL xios_define_calendar( TYPE = "NoLeap"   , time_origin = xios_date(1900,01,01,00,00,00), &
141          &                                    start_date = xios_date(nyear,nmonth,nday,0,0,0) )
142      CASE (30)   ; CALL xios_define_calendar( TYPE = "D360"     , time_origin = xios_date(1900,01,01,00,00,00), &
143          &                                    start_date = xios_date(nyear,nmonth,nday,0,0,0) )
144      END SELECT
145
146#endif
147      ! horizontal grid definition
148
149      CALL set_scalar
150
151      IF( TRIM(cdname) == TRIM(cxios_context)) THEN 
152         CALL set_grid( "T", glamt, gphit, .TRUE. ) 
153         CALL set_grid( "U", glamu, gphiu, .TRUE. )
154         CALL set_grid( "V", glamv, gphiv, .TRUE. )
155         CALL set_grid( "W", glamt, gphit, .TRUE. )
156         CALL set_grid_znl( gphit )
157         !
158         IF( ln_cfmeta ) THEN   ! Add additional grid metadata
159            CALL iom_set_domain_attr("grid_T", area = e12t(nldi:nlei, nldj:nlej))
160            CALL iom_set_domain_attr("grid_U", area = e12u(nldi:nlei, nldj:nlej))
161            CALL iom_set_domain_attr("grid_V", area = e12v(nldi:nlei, nldj:nlej))
162            CALL iom_set_domain_attr("grid_W", area = e12t(nldi:nlei, nldj:nlej))
163            CALL set_grid_bounds( "T", glamf, gphif, glamt, gphit )
164            CALL set_grid_bounds( "U", glamv, gphiv, glamu, gphiu )
165            CALL set_grid_bounds( "V", glamu, gphiu, glamv, gphiv )
166            CALL set_grid_bounds( "W", glamf, gphif, glamt, gphit )
167         ENDIF
168      ENDIF
169
170      IF( TRIM(cdname) == TRIM(cxios_context)//"_crs" ) THEN 
171         CALL dom_grid_crs   ! Save the parent grid information  & Switch to coarse grid domain
172         !
173         CALL set_grid( "T", glamt_crs, gphit_crs, ln_mskland ) 
174         CALL set_grid( "U", glamu_crs, gphiu_crs, ln_mskland ) 
175         CALL set_grid( "V", glamv_crs, gphiv_crs, ln_mskland ) 
176         CALL set_grid( "W", glamt_crs, gphit_crs, ln_mskland ) 
177         CALL set_grid_znl( gphit_crs )
178          !
179         CALL dom_grid_glo   ! Return to parent grid domain
180         !
181         IF( ln_cfmeta) THEN   ! Add additional grid metadata
182            CALL iom_set_domain_attr("grid_T", area = e1e2t_crs(nldi:nlei, nldj:nlej))
183            CALL iom_set_domain_attr("grid_U", area = e1u_crs(nldi:nlei, nldj:nlej) * e2u_crs(nldi:nlei, nldj:nlej))
184            CALL iom_set_domain_attr("grid_V", area = e1v_crs(nldi:nlei, nldj:nlej) * e2v_crs(nldi:nlei, nldj:nlej))
185            CALL iom_set_domain_attr("grid_W", area = e1e2t_crs(nldi:nlei, nldj:nlej))
186            CALL set_grid_bounds( "T", glamf_crs, gphif_crs, glamt_crs, gphit_crs )
187            CALL set_grid_bounds( "U", glamv_crs, gphiv_crs, glamu_crs, gphiu_crs )
188            CALL set_grid_bounds( "V", glamu_crs, gphiu_crs, glamv_crs, gphiv_crs )
189            CALL set_grid_bounds( "W", glamf_crs, gphif_crs, glamt_crs, gphit_crs )
190         ENDIF
191      ENDIF
192
193      ! vertical grid definition
194      CALL iom_set_axis_attr( "deptht", paxis = gdept_1d )
195      CALL iom_set_axis_attr( "depthu", paxis = gdept_1d )
196      CALL iom_set_axis_attr( "depthv", paxis = gdept_1d )
197      CALL iom_set_axis_attr( "depthw", paxis = gdepw_1d )
198
199      ! Add vertical grid bounds
200#if ! defined key_xios2
201      z_bnds(:      ,1) = gdepw_1d(:)
202      z_bnds(1:jpkm1,2) = gdepw_1d(2:jpk)
203      z_bnds(jpk:   ,2) = gdepw_1d(jpk) + e3t_1d(jpk)
204#else
205      z_bnds(1      ,:) = gdepw_1d(:)
206      z_bnds(2,1:jpkm1) = gdepw_1d(2:jpk)
207      z_bnds(2,jpk:   ) = gdepw_1d(jpk) + e3t_1d(jpk)
208#endif
209
210      CALL iom_set_axis_attr( "deptht", bounds=z_bnds )
211      CALL iom_set_axis_attr( "depthu", bounds=z_bnds )
212      CALL iom_set_axis_attr( "depthv", bounds=z_bnds )
213
214#if ! defined key_xios2
215      z_bnds(:    ,2)  = gdept_1d(:)
216      z_bnds(2:jpk,1)  = gdept_1d(1:jpkm1)
217      z_bnds(1    ,1)  = gdept_1d(1) - e3w_1d(1)
218#else
219      z_bnds(2,:    )  = gdept_1d(:)
220      z_bnds(1,2:jpk)  = gdept_1d(1:jpkm1)
221      z_bnds(1,1    )  = gdept_1d(1) - e3w_1d(1)
222#endif
223      CALL iom_set_axis_attr( "depthw", bounds=z_bnds )
224
225
226# if defined key_floats
227      CALL iom_set_axis_attr( "nfloat", (/ (REAL(ji,wp), ji=1,nfloat) /) )
228# endif
229#if defined key_lim3 || defined key_lim2
230      CALL iom_set_axis_attr( "ncatice", (/ (REAL(ji,wp), ji=1,jpl) /) )
231#endif
232      CALL iom_set_axis_attr( "icbcla", class_num )
233      CALL iom_set_axis_attr( "iax_20C", (/ REAL(20,wp) /) )
234      CALL iom_set_axis_attr( "iax_28C", (/ REAL(28,wp) /) )
235     
236      CALL set_xmlatt
237
238      CALL set_1point
239
240      ! end file definition
241      dtime%second = rdt
242      CALL xios_set_timestep(dtime)
243      CALL xios_close_context_definition()
244     
245      CALL xios_update_calendar(0)
246
247      DEALLOCATE( z_bnds )
248
249#endif
250
251   END SUBROUTINE iom_init
252
253   SUBROUTINE iom_rstw_init( cdname, filename, it ) 
254      !!----------------------------------------------------------------------
255      !!                     ***  ROUTINE   ***
256      !!
257      !! ** Purpose :   
258      !!
259      !!----------------------------------------------------------------------
260      CHARACTER(len=*), INTENT(in)  :: cdname
261#if defined key_iomput
262#if defined key_xios2
263      TYPE(xios_duration) :: dtime    = xios_duration(0, 0, 0, 0, 0, 0)
264      TYPE(xios_date)     :: start_date
265      TYPE(xios_domaingroup)            :: domaingroup_hdl
266      TYPE(xios_domain)                 :: domain_hdl
267      TYPE(xios_axisgroup)              :: axisgroup_hdl
268      TYPE(xios_axis)                   :: axis_hdl
269      TYPE(xios_scalar)                 :: scalar_hdl
270      TYPE(xios_scalargroup)            :: scalargroup_hdl
271#endif
272      CHARACTER(len=128)   :: clname
273      INTEGER             :: ji
274      !
275      CHARACTER(len=*), OPTIONAL, INTENT(in)  :: filename 
276      INTEGER, OPTIONAL :: it              ! timestep when subroutine was called
277      !!----------------------------------------------------------------------
278
279      clname = cdname 
280      if(lwp) write(numout,*) 'initialize CONTEXT:', TRIM(cdname)
281      IF( TRIM(Agrif_CFixed()) /= '0' )   clname = TRIM(Agrif_CFixed())//"_"//TRIM(cdname)
282      CALL xios_context_initialize(TRIM(clname), mpi_comm_opa)
283      CALL iom_swap( cdname )
284      ! calendar parameters
285#if defined key_xios2
286      ! Calendar type is now defined in xml file
287      SELECT CASE ( nleapy )        ! Choose calendar for IOIPSL
288      CASE ( 1)   ; CALL xios_define_calendar( TYPE = "Gregorian", time_origin = xios_date(1900,01,01,00,00,00), &
289          &                                    start_date = xios_date(nyear,nmonth,nday,0,0,0) )
290      CASE ( 0)   ; CALL xios_define_calendar( TYPE = "NoLeap"   , time_origin = xios_date(1900,01,01,00,00,00), &
291          &                                    start_date = xios_date(nyear,nmonth,nday,0,0,0) )
292      CASE (30)   ; CALL xios_define_calendar( TYPE = "D360"     , time_origin = xios_date(1900,01,01,00,00,00), &
293          &                                    start_date = xios_date(nyear,nmonth,nday,0,0,0) )
294      END SELECT
295
296#endif
297      CALL xios_get_handle("domain_definition",domaingroup_hdl)
298      CALL xios_add_child(domaingroup_hdl, domain_hdl, "grid_N")
299      CALL set_grid("N", glamt, gphit, .FALSE.)
300
301      CALL xios_get_handle("axis_definition",axisgroup_hdl)
302      CALL xios_add_child(axisgroup_hdl, axis_hdl, "deptht")
303      CALL xios_set_axis_attr( "deptht", long_name="Vertical levels",  unit="m", positive="down")
304      ! vertical grid definition
305      CALL iom_set_axis_attr( "deptht", paxis = gdept_1d )
306
307      CALL XIOS_GET_HANDLE("scalar_definition",scalargroup_hdl)
308      CALL xios_add_child(scalargroup_hdl, scalar_hdl, "grid_scalar")
309
310      ! automatic definitions of some of the xml attributs
311      IF( TRIM(cdname) == TRIM(wxios_context)) THEN
312!set which fields are to be read from restart file
313       CALL set_rstw_active(filename, it)
314      ENDIF
315
316      ! end file definition
317      dtime%second = rdt
318      CALL xios_set_timestep(dtime)
319      CALL xios_close_context_definition()
320     
321#endif
322
323   END SUBROUTINE iom_rstw_init
324
325   SUBROUTINE set_rstw_active(rst_file, it)
326!sets enabled = .TRUE. for each field in restart file
327   CHARACTER(len=*) :: rst_file
328   INTEGER, INTENT(in) :: it ! timestep when iom_init was called
329   TYPE(xios_file) :: file_hdl
330   TYPE(xios_filegroup) :: filegroup_hdl
331
332!set then name of the restart file (OUTPUT!) and enable available fields
333        if(lwp) WRITE(numout,*) 'Setting (output) restart filename (for XIOS) to: ',TRIM(rst_file)
334        CALL xios_get_handle("file_definition", filegroup_hdl )
335        CALL xios_add_child(filegroup_hdl, file_hdl, 'wrestart')
336        IF(wxioso.eq.1) THEN
337            CALL xios_set_file_attr( "wrestart", type="one_file", name = TRIM(rst_file), &
338               enabled=.TRUE., mode="write", output_freq=xios_timestep)
339            if(lwp) write(numout,*) 'OPEN ', trim(rst_file), ' in one_file mode'
340        ELSE
341            CALL xios_set_file_attr( "wrestart", type="multiple_file", name = TRIM(rst_file),&
342               enabled=.TRUE., mode="write", output_freq=xios_timestep)
343          if(lwp) write(numout,*) 'OPEN ', trim(rst_file), ' in multiple_file mode'
344        ENDIF
345
346        CALL xios_set_file_attr( "wrestart", name=trim(rst_file))
347        CALL set_numro_active(file_hdl)
348
349    END SUBROUTINE set_rstw_active
350
351    SUBROUTINE set_numro_active(file_hdl)
352    TYPE(xios_field) :: field_hdl
353    TYPE(xios_file) :: file_hdl
354
355        CALL xios_add_child(file_hdl, field_hdl, "rdt")
356        CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
357                  scalar_ref = "grid_scalar", operation = "instant")
358
359        CALL xios_add_child(file_hdl, field_hdl, "rdttra1")
360        CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
361                  scalar_ref = "grid_scalar", operation = "instant")
362
363        CALL xios_add_child(file_hdl, field_hdl, "un")
364        CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
365                  domain_ref="grid_N", axis_ref="deptht", operation = "instant")
366
367        CALL xios_add_child(file_hdl, field_hdl, "ub")
368        CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
369                  domain_ref="grid_N", axis_ref="deptht", operation = "instant")
370
371        CALL xios_add_child(file_hdl, field_hdl, "vn")
372        CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
373                  domain_ref="grid_N", axis_ref="deptht", operation = "instant")
374
375        CALL xios_add_child(file_hdl, field_hdl, "vb")
376        CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
377                  domain_ref="grid_N", axis_ref="deptht", operation = "instant")
378
379        CALL xios_add_child(file_hdl, field_hdl, "tn")
380        CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
381                  domain_ref="grid_N", axis_ref="deptht", operation = "instant")
382
383        CALL xios_add_child(file_hdl, field_hdl, "tb")
384        CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
385                  domain_ref="grid_N", axis_ref="deptht", operation = "instant")
386
387        CALL xios_add_child(file_hdl, field_hdl, "sn")
388        CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
389                  domain_ref="grid_N", axis_ref="deptht", operation = "instant")
390
391        CALL xios_add_child(file_hdl, field_hdl, "sb")
392        CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
393                  domain_ref="grid_N", axis_ref="deptht", operation = "instant")
394
395        CALL xios_add_child(file_hdl, field_hdl, "sshn")
396        CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
397                  domain_ref = "grid_N", operation = "instant")
398
399        CALL xios_add_child(file_hdl, field_hdl, "sshb")
400        CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
401                  domain_ref = "grid_N", operation = "instant")
402
403        CALL xios_add_child(file_hdl, field_hdl, "hdivn")
404        CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
405                  domain_ref="grid_N", axis_ref="deptht", operation = "instant")
406
407        CALL xios_add_child(file_hdl, field_hdl, "hdivb")
408        CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
409                  domain_ref="grid_N", axis_ref="deptht", operation = "instant")
410
411        CALL xios_add_child(file_hdl, field_hdl, "rhop")
412        CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
413                  domain_ref="grid_N", axis_ref="deptht", operation = "instant")
414
415        CALL xios_add_child(file_hdl, field_hdl, "rotn")
416        CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
417                  domain_ref="grid_N", axis_ref="deptht", operation = "instant")
418
419        CALL xios_add_child(file_hdl, field_hdl, "rotb")
420        CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
421                  domain_ref="grid_N", axis_ref="deptht", operation = "instant")
422
423!in daymod.F90
424           CALL xios_add_child(file_hdl, field_hdl, "kt")
425           CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
426                     scalar_ref = "grid_scalar", operation = "instant")
427
428           CALL xios_add_child(file_hdl, field_hdl, "ndastp")
429           CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
430                     scalar_ref = "grid_scalar", operation = "instant")
431
432           CALL xios_add_child(file_hdl, field_hdl, "adatrj")
433           CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
434                     scalar_ref = "grid_scalar", operation = "instant")
435!end daymod.F90
436!sbcmod.F90
437           CALL xios_add_child(file_hdl, field_hdl, "utau_b")
438           CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
439                     domain_ref = "grid_N", operation = "instant")
440
441           CALL xios_add_child(file_hdl, field_hdl, "vtau_b")
442           CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
443                     domain_ref = "grid_N", operation = "instant")
444
445           CALL xios_add_child(file_hdl, field_hdl, "qns_b")
446           CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
447                     domain_ref = "grid_N", operation = "instant")
448
449           CALL xios_add_child(file_hdl, field_hdl, "emp_b")
450           CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
451                     domain_ref = "grid_N", operation = "instant")
452
453           CALL xios_add_child(file_hdl, field_hdl, "sfx_b")
454           CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
455                     domain_ref = "grid_N", operation = "instant")
456!end sbcmod.F90
457!ALL FIELDS ABOUVE ALWAYS
458!zdftke.F90
459#if defined key_zdftke   ||   defined key_esopa
460           CALL xios_add_child(file_hdl, field_hdl, "en")
461           CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
462                     domain_ref="grid_N", axis_ref="deptht", operation = "instant")
463
464           CALL xios_add_child(file_hdl, field_hdl, "avt")
465           CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
466                     domain_ref="grid_N", axis_ref="deptht", operation = "instant")
467
468           CALL xios_add_child(file_hdl, field_hdl, "avm")
469           CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
470                     domain_ref="grid_N", axis_ref="deptht", operation = "instant")
471
472           CALL xios_add_child(file_hdl, field_hdl, "avmu")
473           CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
474                     domain_ref="grid_N", axis_ref="deptht", operation = "instant")
475
476           CALL xios_add_child(file_hdl, field_hdl, "avmv")
477           CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
478                     domain_ref="grid_N", axis_ref="deptht", operation = "instant")
479
480           CALL xios_add_child(file_hdl, field_hdl, "dissl")
481           CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
482                     domain_ref="grid_N", axis_ref="deptht", operation = "instant")
483#endif
484!end zdftke.F90
485!traqsr.F90
486       CALL xios_add_child(file_hdl, field_hdl, "qsr_hc_b")
487       CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
488                     domain_ref="grid_N", axis_ref="deptht" , operation = "instant")       
489
490       CALL xios_add_child(file_hdl, field_hdl, "fraqsr_1lev")
491       CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
492                     domain_ref = "grid_N", operation = "instant")       
493!END traqsr.F90
494#if defined key_dynspg_flt   ||   defined key_esopa 
495!dynspg_flt.F90
496           CALL xios_add_child(file_hdl, field_hdl, "gcx")
497           CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
498                     domain_ref = "grid_N", operation = "instant")
499
500           CALL xios_add_child(file_hdl, field_hdl, "gcxb")
501           CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
502                     domain_ref = "grid_N", operation = "instant")
503!end dynspg_flt.F90
504#endif
505!trasbc.F90 START
506           CALL xios_add_child(file_hdl, field_hdl, "sbc_hc_b")
507           CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
508                     domain_ref = "grid_N", operation = "instant")
509
510           CALL xios_add_child(file_hdl, field_hdl, "sbc_sc_b")
511           CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
512                     domain_ref = "grid_N", operation = "instant")
513
514           CALL xios_add_child(file_hdl, field_hdl, "fwf_isf_b")
515           CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
516                     domain_ref = "grid_N", operation = "instant")
517
518           CALL xios_add_child(file_hdl, field_hdl, "isf_sc_b")
519           CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
520                     domain_ref = "grid_N", operation = "instant")
521
522           CALL xios_add_child(file_hdl, field_hdl, "isf_hc_b")
523           CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
524                     domain_ref = "grid_N", operation = "instant")
525!trasbc.F90 END
526        IF( lk_oasis) THEN
527        ! ln_coupled_iceshelf_fluxes uninitialised unless lk_oasis=true
528          IF( ln_coupled_iceshelf_fluxes ) THEN
529             CALL xios_add_child(file_hdl, field_hdl, "greenland_icesheet_mass")
530             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
531                  scalar_ref = "grid_scalar", operation = "instant")
532
533             CALL xios_add_child(file_hdl, field_hdl, "greenland_icesheet_timelapsed")
534             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
535                  scalar_ref = "grid_scalar", operation = "instant")
536
537             CALL xios_add_child(file_hdl, field_hdl, "greenland_icesheet_mass_roc")
538             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
539                  scalar_ref = "grid_scalar", operation = "instant")
540
541             CALL xios_add_child(file_hdl, field_hdl, "antarctica_icesheet_mass")
542             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
543                  scalar_ref = "grid_scalar", operation = "instant")
544
545             CALL xios_add_child(file_hdl, field_hdl, "antarctica_icesheet_timelapsed")
546             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
547                  scalar_ref = "grid_scalar", operation = "instant")
548
549             CALL xios_add_child(file_hdl, field_hdl, "antarctica_icesheet_mass_roc")
550             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
551                  scalar_ref = "grid_scalar", operation = "instant")
552          ENDIF
553        ENDIF
554#if defined key_zdfkpp
555        CALL xios_add_child(file_hdl, field_hdl, "rhd")
556        CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
557             domain_ref="grid_N", axis_ref="deptht", operation = "instant")
558#endif
559!dia_hsb_rst
560#if defined key_diadct
561        IF( lk_diadct     ) THEN
562             CALL xios_add_child(file_hdl, field_hdl, "frc_v")
563             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
564                  scalar_ref = "grid_scalar", operation = "instant")
565
566             CALL xios_add_child(file_hdl, field_hdl, "frc_t")
567             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
568                  scalar_ref = "grid_scalar", operation = "instant")
569
570             CALL xios_add_child(file_hdl, field_hdl, "frc_s")
571             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
572                  scalar_ref = "grid_scalar", operation = "instant")
573
574             CALL xios_add_child(file_hdl, field_hdl, "ssh_ini")
575             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
576                  domain_ref = "grid_N", operation = "instant")
577
578             CALL xios_add_child(file_hdl, field_hdl, "e3t_ini")
579             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
580                  domain_ref="grid_N", axis_ref="deptht", operation = "instant")
581
582             CALL xios_add_child(file_hdl, field_hdl, "hc_loc_ini")
583             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
584                  domain_ref="grid_N", axis_ref="deptht", operation = "instant")
585
586             CALL xios_add_child(file_hdl, field_hdl, "sc_loc_ini")
587             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
588                  domain_ref="grid_N", axis_ref="deptht", operation = "instant")
589
590
591             IF( .NOT. lk_vvl ) THEN
592                CALL xios_add_child(file_hdl, field_hdl, "frc_wn_t")
593                CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
594                     scalar_ref = "grid_scalar", operation = "instant")
595
596                CALL xios_add_child(file_hdl, field_hdl, "frc_wn_s")
597                CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
598                     scalar_ref = "grid_scalar", operation = "instant")
599
600                CALL xios_add_child(file_hdl, field_hdl, "ssh_hc_loc_ini")
601                CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
602                     domain_ref = "grid_N", operation = "instant")
603
604                CALL xios_add_child(file_hdl, field_hdl, "ssh_sc_loc_ini")
605                CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
606                     domain_ref = "grid_N", operation = "instant")
607
608
609             ENDIF
610        ENDIF
611#endif
612!end dia_hsb_rst
613!domvvl.F90
614        IF( lk_vvl ) THEN
615             CALL xios_add_child(file_hdl, field_hdl, "fse3t_b")
616             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
617                  domain_ref="grid_N", axis_ref="deptht", operation = "instant")
618
619             CALL xios_add_child(file_hdl, field_hdl, "fse3t_n")
620             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
621                  domain_ref="grid_N", axis_ref="deptht", operation = "instant")
622
623           IF( lr_vvl_ztilde .OR. lr_vvl_layer ) THEN  ! z_tilde and layer cases !
624             CALL xios_add_child(file_hdl, field_hdl, "tilde_e3t_b")
625             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
626                  domain_ref = "grid_N", operation = "instant")
627
628             CALL xios_add_child(file_hdl, field_hdl, "tilde_e3t_n")
629             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
630                  domain_ref = "grid_N", operation = "instant")
631            END IF
632            IF( lr_vvl_ztilde ) THEN                    ! z_tilde case !
633            CALL xios_add_child(file_hdl, field_hdl, "hdiv_lf")
634            CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
635                     domain_ref = "grid_N", operation = "instant")
636            ENDIF
637         ENDIF
638!end domvvl.F90
639!dynspg_ts.F90
640#if defined key_dynspg_ts   ||   defined key_esopa
641         CALL xios_add_child(file_hdl, field_hdl, "ub2_b")
642         CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
643              domain_ref = "grid_N", operation = "instant")
644
645         CALL xios_add_child(file_hdl, field_hdl, "vb2_b")
646         CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
647              domain_ref = "grid_N", operation = "instant")
648
649
650         IF (.NOT.ln_bt_av) THEN
651            CALL xios_add_child(file_hdl, field_hdl, "sshbb_e")
652            CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
653                 domain_ref = "grid_N", operation = "instant")
654
655            CALL xios_add_child(file_hdl, field_hdl, "ubb_e")
656            CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
657                 domain_ref = "grid_N", operation = "instant")
658
659            CALL xios_add_child(file_hdl, field_hdl, "vbb_e")
660            CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
661                 domain_ref = "grid_N", operation = "instant")
662
663            CALL xios_add_child(file_hdl, field_hdl, "sshb_e")
664            CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
665                 domain_ref = "grid_N", operation = "instant")
666
667            CALL xios_add_child(file_hdl, field_hdl, "ub_e")
668            CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
669                 domain_ref = "grid_N", operation = "instant")
670
671            CALL xios_add_child(file_hdl, field_hdl, "vb_e")
672            CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
673                 domain_ref = "grid_N", operation = "instant")
674         ENDIF
675#if defined key_agrif
676         ! Save time integrated fluxes
677         IF ( .NOT.Agrif_Root() ) THEN
678               CALL xios_add_child(file_hdl, field_hdl, "ub2_i_b")
679               CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
680                    domain_ref = "grid_N", operation = "instant")
681
682               CALL xios_add_child(file_hdl, field_hdl, "vb2_i_b")
683               CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
684                    domain_ref = "grid_N", operation = "instant")
685         ENDIF
686#endif
687#endif
688!end dynspg_ts.F90
689!sbcapr.F90
690          IF( ln_apr_dyn) THEN
691             CALL xios_add_child(file_hdl, field_hdl, "ssh_ibb")
692             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
693                     domain_ref = "grid_N", operation = "instant")
694          ENDIF
695!end sbcapr.F90
696!sbcrnf.F90
697        IF( ln_rnf      ) THEN
698             CALL xios_add_child(file_hdl, field_hdl, "rnf_b")
699             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
700                  domain_ref = "grid_N", operation = "instant")
701
702             CALL xios_add_child(file_hdl, field_hdl, "rnf_hc_b")
703             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
704                  domain_ref = "grid_N", operation = "instant")
705
706             CALL xios_add_child(file_hdl, field_hdl, "rnf_sc_b")
707             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
708                  domain_ref = "grid_N", operation = "instant")
709        ENDIF 
710!end sbcrnf.F90
711!sbcssm.F90
712        IF( nn_components /= jp_iam_sas .AND. nn_fsbc .NE. 1) THEN
713             CALL xios_add_child(file_hdl, field_hdl, "nn_fsbc")
714             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
715                  scalar_ref = "grid_scalar", operation = "instant")
716
717             CALL xios_add_child(file_hdl, field_hdl, "ssu_m")
718             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
719                  domain_ref = "grid_N", operation = "instant")
720
721             CALL xios_add_child(file_hdl, field_hdl, "ssv_m")
722             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
723                  domain_ref = "grid_N", operation = "instant")
724
725             CALL xios_add_child(file_hdl, field_hdl, "sst_m")
726             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
727                  domain_ref = "grid_N", operation = "instant")
728
729             CALL xios_add_child(file_hdl, field_hdl, "sss_m")
730             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
731                  domain_ref = "grid_N", operation = "instant")
732
733             CALL xios_add_child(file_hdl, field_hdl, "ssh_m")
734             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
735                  domain_ref = "grid_N", operation = "instant")
736
737             CALL xios_add_child(file_hdl, field_hdl, "frq_m")
738             CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
739                     domain_ref = "grid_N", operation = "instant")
740
741             IF( lk_vvl )  THEN
742                CALL xios_add_child(file_hdl, field_hdl, "e3t_m")
743                CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
744                     domain_ref = "grid_N", operation = "instant")
745             ENDIF
746        ENDIF
747!end sbcssm.F90
748       IF( lr_traadv_cen2   ) THEN
749          CALL xios_add_child(file_hdl, field_hdl, "avmb")
750          CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
751               axis_ref = "deptht", operation = "instant")
752
753          CALL xios_add_child(file_hdl, field_hdl, "avtb")
754          CALL xios_set_attr (field_hdl, enabled = .TRUE., prec = 8,           &
755               axis_ref = "deptht", operation = "instant")
756       ENDIF
757
758   END SUBROUTINE set_numro_active
759
760   SUBROUTINE iom_swap( cdname )
761      !!---------------------------------------------------------------------
762      !!                   ***  SUBROUTINE  iom_swap  ***
763      !!
764      !! ** Purpose :  swap context between different agrif grid for xmlio_server
765      !!---------------------------------------------------------------------
766      CHARACTER(len=*), INTENT(in) :: cdname
767#if defined key_iomput
768      TYPE(xios_context) :: nemo_hdl
769
770      if(lwp) write(numout,*) 'iom_swap to: ',trim(cdname)
771      IF( TRIM(Agrif_CFixed()) == '0' ) THEN
772        CALL xios_get_handle(TRIM(cdname),nemo_hdl)
773      ELSE
774        CALL xios_get_handle(TRIM(Agrif_CFixed())//"_"//TRIM(cdname),nemo_hdl)
775      ENDIF
776      !
777      CALL xios_set_current_context(nemo_hdl)
778#endif
779      !
780   END SUBROUTINE iom_swap
781
782
783   SUBROUTINE iom_open( cdname, kiomid, ldwrt, kdom, kiolib, ldstop, ldiof )
784      !!---------------------------------------------------------------------
785      !!                   ***  SUBROUTINE  iom_open  ***
786      !!
787      !! ** Purpose :  open an input file (return 0 if not found)
788      !!---------------------------------------------------------------------
789      CHARACTER(len=*), INTENT(in   )           ::   cdname   ! File name
790      INTEGER         , INTENT(  out)           ::   kiomid   ! iom identifier of the opened file
791      LOGICAL         , INTENT(in   ), OPTIONAL ::   ldwrt    ! open in write modeb          (default = .FALSE.)
792      INTEGER         , INTENT(in   ), OPTIONAL ::   kdom     ! Type of domain to be written (default = jpdom_local_noovlap)
793      INTEGER         , INTENT(in   ), OPTIONAL ::   kiolib   ! library used to open the file (default = jpnf90)
794      LOGICAL         , INTENT(in   ), OPTIONAL ::   ldstop   ! stop if open to read a non-existing file (default = .TRUE.)
795      LOGICAL         , INTENT(in   ), OPTIONAL ::   ldiof    ! Interp On the Fly, needed for AGRIF (default = .FALSE.)
796
797      CHARACTER(LEN=256)    ::   clname    ! the name of the file based on cdname [[+clcpu]+clcpu]
798      CHARACTER(LEN=256)    ::   cltmpn    ! tempory name to store clname (in writting mode)
799      CHARACTER(LEN=10)     ::   clsuffix  ! ".nc" or ".dimg"
800      CHARACTER(LEN=15)     ::   clcpu     ! the cpu number (max jpmax_digits digits)
801      CHARACTER(LEN=256)    ::   clinfo    ! info character
802      LOGICAL               ::   llok      ! check the existence
803      LOGICAL               ::   llwrt     ! local definition of ldwrt
804      LOGICAL               ::   llnoov    ! local definition to read overlap
805      LOGICAL               ::   llstop    ! local definition of ldstop
806      LOGICAL               ::   lliof     ! local definition of ldiof
807      INTEGER               ::   iolib     ! library do we use to open the file
808      INTEGER               ::   icnt      ! counter for digits in clcpu (max = jpmax_digits)
809      INTEGER               ::   iln, ils  ! lengths of character
810      INTEGER               ::   idom      ! type of domain
811      INTEGER               ::   istop     !
812      INTEGER, DIMENSION(2,5) ::   idompar ! domain parameters:
813      ! local number of points for x,y dimensions
814      ! position of first local point for x,y dimensions
815      ! position of last local point for x,y dimensions
816      ! start halo size for x,y dimensions
817      ! end halo size for x,y dimensions
818      !---------------------------------------------------------------------
819      ! Initializations and control
820      ! =============
821      kiomid = -1
822      clinfo = '                    iom_open ~~~  '
823      istop = nstop
824      ! if iom_open is called for the first time: initialize iom_file(:)%nfid to 0
825      ! (could be done when defining iom_file in f95 but not in f90)
826      IF( Agrif_Root() ) THEN
827         IF( iom_open_init == 0 ) THEN
828            iom_file(:)%nfid = 0
829            iom_open_init = 1
830         ENDIF
831      ENDIF
832      ! do we read or write the file?
833      IF( PRESENT(ldwrt) ) THEN   ;   llwrt = ldwrt
834      ELSE                        ;   llwrt = .FALSE.
835      ENDIF
836      ! do we call ctl_stop if we try to open a non-existing file in read mode?
837      IF( PRESENT(ldstop) ) THEN   ;   llstop = ldstop
838      ELSE                         ;   llstop = .TRUE.
839      ENDIF
840      ! what library do we use to open the file?
841      IF( PRESENT(kiolib) ) THEN   ;   iolib = kiolib
842      ELSE                         ;   iolib = jpnf90
843      ENDIF
844      ! are we using interpolation on the fly?
845      IF( PRESENT(ldiof) ) THEN   ;   lliof = ldiof
846      ELSE                        ;   lliof = .FALSE.
847      ENDIF
848      ! do we read the overlap
849      ! ugly patch SM+JMM+RB to overwrite global definition in some cases
850      llnoov = (jpni * jpnj ) == jpnij .AND. .NOT. lk_agrif 
851      ! create the file name by added, if needed, TRIM(Agrif_CFixed()) and TRIM(clsuffix)
852      ! =============
853      clname   = trim(cdname)
854      IF ( .NOT. Agrif_Root() .AND. .NOT. lliof ) THEN
855         iln    = INDEX(clname,'/') 
856         cltmpn = clname(1:iln)
857         clname = clname(iln+1:LEN_TRIM(clname))
858         clname=TRIM(cltmpn)//TRIM(Agrif_CFixed())//'_'//TRIM(clname)
859      ENDIF
860      ! which suffix should we use?
861      SELECT CASE (iolib)
862      CASE (jpioipsl ) ;   clsuffix = '.nc'
863      CASE (jpnf90   ) ;   clsuffix = '.nc'
864      CASE (jprstdimg) ;   clsuffix = '.dimg'
865      CASE DEFAULT     ;   clsuffix = ''
866         CALL ctl_stop( TRIM(clinfo), 'accepted IO library are only jpioipsl, jpnf90 and jprstdimg' )
867      END SELECT
868      ! Add the suffix if needed
869      iln = LEN_TRIM(clname)
870      ils = LEN_TRIM(clsuffix)
871      IF( iln <= ils .OR. INDEX( TRIM(clname), TRIM(clsuffix), back = .TRUE. ) /= iln - ils + 1 )   &
872         &   clname = TRIM(clname)//TRIM(clsuffix)
873      cltmpn = clname   ! store this name
874      ! try to find if the file to be opened already exist
875      ! =============
876      INQUIRE( FILE = clname, EXIST = llok )
877      IF( .NOT.llok ) THEN
878         ! we try to add the cpu number to the name
879         IF( iolib == jprstdimg ) THEN   ;   WRITE(clcpu,*) narea
880         ELSE                            ;   WRITE(clcpu,*) narea-1
881         ENDIF
882         clcpu  = TRIM(ADJUSTL(clcpu))
883         iln = INDEX(clname,TRIM(clsuffix), back = .TRUE.)
884         clname = clname(1:iln-1)//'_'//TRIM(clcpu)//TRIM(clsuffix)
885         icnt = 0
886         INQUIRE( FILE = clname, EXIST = llok ) 
887         ! we try different formats for the cpu number by adding 0
888         DO WHILE( .NOT.llok .AND. icnt < jpmax_digits )
889            clcpu  = "0"//trim(clcpu)
890            clname = clname(1:iln-1)//'_'//TRIM(clcpu)//TRIM(clsuffix)
891            INQUIRE( FILE = clname, EXIST = llok )
892            icnt = icnt + 1
893         END DO
894      ENDIF
895      IF( llwrt ) THEN
896         ! check the domain definition
897! JMM + SM: ugly patch before getting the new version of lib_mpp)
898!         idom = jpdom_local_noovlap   ! default definition
899         IF( llnoov ) THEN   ;   idom = jpdom_local_noovlap   ! default definition
900         ELSE                ;   idom = jpdom_local_full      ! default definition
901         ENDIF
902         IF( PRESENT(kdom) )   idom = kdom
903         ! create the domain informations
904         ! =============
905         SELECT CASE (idom)
906         CASE (jpdom_local_full)
907            idompar(:,1) = (/ jpi             , jpj              /)
908            idompar(:,2) = (/ nimpp           , njmpp            /)
909            idompar(:,3) = (/ nimpp + jpi - 1 , njmpp + jpj - 1  /)
910            idompar(:,4) = (/ nldi - 1        , nldj - 1         /)
911            idompar(:,5) = (/ jpi - nlei      , jpj - nlej       /)
912         CASE (jpdom_local_noextra)
913            idompar(:,1) = (/ nlci            , nlcj             /)
914            idompar(:,2) = (/ nimpp           , njmpp            /)
915            idompar(:,3) = (/ nimpp + nlci - 1, njmpp + nlcj - 1 /)
916            idompar(:,4) = (/ nldi - 1        , nldj - 1         /)
917            idompar(:,5) = (/ nlci - nlei     , nlcj - nlej      /)
918         CASE (jpdom_local_noovlap)
919            idompar(:,1) = (/ nlei  - nldi + 1, nlej  - nldj + 1 /)
920            idompar(:,2) = (/ nimpp + nldi - 1, njmpp + nldj - 1 /)
921            idompar(:,3) = (/ nimpp + nlei - 1, njmpp + nlej - 1 /)
922            idompar(:,4) = (/ 0               , 0                /)
923            idompar(:,5) = (/ 0               , 0                /)
924         CASE DEFAULT
925            CALL ctl_stop( TRIM(clinfo), 'wrong value of kdom, only jpdom_local* cases are accepted' )
926         END SELECT
927      ENDIF
928      ! Open the NetCDF or RSTDIMG file
929      ! =============
930      ! do we have some free file identifier?
931      IF( MINVAL(iom_file(:)%nfid) /= 0 )   &
932         &   CALL ctl_stop( TRIM(clinfo), 'No more free file identifier', 'increase jpmax_files in iom_def' )
933      ! if no file was found...
934      IF( .NOT. llok ) THEN
935         IF( .NOT. llwrt ) THEN   ! we are in read mode
936            IF( llstop ) THEN   ;   CALL ctl_stop( TRIM(clinfo), 'File '//TRIM(cltmpn)//'* not found' )
937            ELSE                ;   istop = nstop + 1   ! make sure that istop /= nstop so we don't open the file
938            ENDIF
939         ELSE                     ! we are in write mode so we
940            clname = cltmpn       ! get back the file name without the cpu number
941         ENDIF
942      ELSE
943         IF( llwrt .AND. .NOT. ln_clobber ) THEN   ! we stop as we want to write in a new file
944            CALL ctl_stop( TRIM(clinfo), 'We want to write in a new file but '//TRIM(clname)//' already exists...' )
945            istop = nstop + 1                      ! make sure that istop /= nstop so we don't open the file
946         ELSEIF( llwrt ) THEN     ! the file exists and we are in write mode with permission to
947            clname = cltmpn       ! overwrite so get back the file name without the cpu number
948         ENDIF
949      ENDIF
950      IF( istop == nstop ) THEN   ! no error within this routine
951         SELECT CASE (iolib)
952         CASE (jpioipsl )   ;   CALL iom_ioipsl_open(  clname, kiomid, llwrt, llok, idompar )
953         CASE (jpnf90   )   ;   CALL iom_nf90_open(    clname, kiomid, llwrt, llok, idompar )
954         CASE (jprstdimg)   ;   CALL iom_rstdimg_open( clname, kiomid, llwrt, llok, idompar )
955         CASE DEFAULT
956            CALL ctl_stop( TRIM(clinfo)//' accepted IO library are only jpioipsl, jpnf90 and jprstdimg' )
957         END SELECT
958      ENDIF
959      !
960   END SUBROUTINE iom_open
961
962
963   SUBROUTINE iom_close( kiomid )
964      !!--------------------------------------------------------------------
965      !!                   ***  SUBROUTINE  iom_close  ***
966      !!
967      !! ** Purpose : close an input file, or all files opened by iom
968      !!--------------------------------------------------------------------
969      INTEGER, INTENT(inout), OPTIONAL ::   kiomid   ! iom identifier of the file to be closed
970      !                                              ! return 0 when file is properly closed
971      !                                              ! No argument: all files opened by iom are closed
972
973      INTEGER ::   jf         ! dummy loop indices
974      INTEGER ::   i_s, i_e   ! temporary integer
975      CHARACTER(LEN=100)    ::   clinfo    ! info character
976      !---------------------------------------------------------------------
977      !
978      clinfo = '                    iom_close ~~~  '
979      IF( PRESENT(kiomid) ) THEN
980         i_s = kiomid
981         i_e = kiomid
982      ELSE
983         i_s = 1
984         i_e = jpmax_files
985      ENDIF
986
987      IF( i_s > 0 ) THEN
988         DO jf = i_s, i_e
989            IF( iom_file(jf)%nfid > 0 ) THEN
990               SELECT CASE (iom_file(jf)%iolib)
991               CASE (jpioipsl )   ;   CALL iom_ioipsl_close(  jf )
992               CASE (jpnf90   )   ;   CALL iom_nf90_close(    jf )
993               CASE (jprstdimg)   ;   CALL iom_rstdimg_close( jf )
994               CASE DEFAULT
995                  CALL ctl_stop( TRIM(clinfo)//' accepted IO library are only jpioipsl, jpnf90 and jprstdimg' )
996               END SELECT
997               iom_file(jf)%nfid       = 0          ! free the id
998               IF( PRESENT(kiomid) )   kiomid = 0   ! return 0 as id to specify that the file was closed
999               IF(lwp) WRITE(numout,*) TRIM(clinfo)//' close file: '//TRIM(iom_file(jf)%name)//' ok'
1000            ELSEIF( PRESENT(kiomid) ) THEN
1001               WRITE(ctmp1,*) '--->',  kiomid
1002               CALL ctl_stop( TRIM(clinfo)//' Invalid file identifier', ctmp1 )
1003            ENDIF
1004         END DO
1005      ENDIF
1006      !   
1007   END SUBROUTINE iom_close
1008
1009
1010   FUNCTION iom_varid ( kiomid, cdvar, kdimsz, kndims, ldstop ) 
1011      !!-----------------------------------------------------------------------
1012      !!                  ***  FUNCTION  iom_varid  ***
1013      !!
1014      !! ** Purpose : get the id of a variable in a file (return 0 if not found)
1015      !!-----------------------------------------------------------------------
1016      INTEGER              , INTENT(in   )           ::   kiomid   ! file Identifier
1017      CHARACTER(len=*)     , INTENT(in   )           ::   cdvar    ! name of the variable
1018      INTEGER, DIMENSION(:), INTENT(  out), OPTIONAL ::   kdimsz   ! size of the dimensions
1019      INTEGER,               INTENT(  out), OPTIONAL ::   kndims   ! size of the dimensions
1020      LOGICAL              , INTENT(in   ), OPTIONAL ::   ldstop   ! stop if looking for non-existing variable (default = .TRUE.)
1021      !
1022      INTEGER                        ::   iom_varid, iiv, i_nvd
1023      LOGICAL                        ::   ll_fnd
1024      CHARACTER(LEN=100)             ::   clinfo                   ! info character
1025      LOGICAL                        ::   llstop                   ! local definition of ldstop
1026      !!-----------------------------------------------------------------------
1027      iom_varid = 0                         ! default definition
1028      ! do we call ctl_stop if we look for non-existing variable?
1029      IF( PRESENT(ldstop) ) THEN   ;   llstop = ldstop
1030      ELSE                         ;   llstop = .TRUE.
1031      ENDIF
1032      !
1033      IF( kiomid > 0 ) THEN
1034         clinfo = 'iom_varid, file: '//trim(iom_file(kiomid)%name)//', var: '//trim(cdvar)
1035         IF( iom_file(kiomid)%nfid == 0 ) THEN
1036            CALL ctl_stop( trim(clinfo), 'the file is not open' )
1037         ELSE
1038            ll_fnd  = .FALSE.
1039            iiv = 0
1040            !
1041            DO WHILE ( .NOT.ll_fnd .AND. iiv < iom_file(kiomid)%nvars )
1042               iiv = iiv + 1
1043               ll_fnd  = ( TRIM(cdvar) == TRIM(iom_file(kiomid)%cn_var(iiv)) )
1044            END DO
1045            !
1046            IF( .NOT.ll_fnd ) THEN
1047               iiv = iiv + 1
1048               IF( iiv <= jpmax_vars ) THEN
1049                  SELECT CASE (iom_file(kiomid)%iolib)
1050                  CASE (jpioipsl )   ;   iom_varid = iom_ioipsl_varid( kiomid, cdvar, iiv, kdimsz )
1051                  CASE (jpnf90   )   ;   iom_varid = iom_nf90_varid  ( kiomid, cdvar, iiv, kdimsz, kndims )
1052                  CASE (jprstdimg)   ;   iom_varid = -1   ! all variables are listed in iom_file
1053                  CASE DEFAULT   
1054                     CALL ctl_stop( TRIM(clinfo)//' accepted IO library are only jpioipsl, jpnf90 and jprstdimg' )
1055                  END SELECT
1056               ELSE
1057                  CALL ctl_stop( trim(clinfo), 'Too many variables in the file '//iom_file(kiomid)%name,   &
1058                        &                         'increase the parameter jpmax_vars')
1059               ENDIF
1060               IF( llstop .AND. iom_varid == -1 )   CALL ctl_stop( TRIM(clinfo)//' not found' ) 
1061            ELSE
1062               iom_varid = iiv
1063               IF( PRESENT(kdimsz) ) THEN
1064                  i_nvd = iom_file(kiomid)%ndims(iiv)
1065                  IF( i_nvd == size(kdimsz) ) THEN
1066                     kdimsz(:) = iom_file(kiomid)%dimsz(1:i_nvd,iiv)
1067                  ELSE
1068                     WRITE(ctmp1,*) i_nvd, size(kdimsz)
1069                     CALL ctl_stop( trim(clinfo), 'error in kdimsz size'//trim(ctmp1) )
1070                  ENDIF
1071               ENDIF
1072               IF( PRESENT(kndims) )  kndims = iom_file(kiomid)%ndims(iiv)
1073            ENDIF
1074         ENDIF
1075      ENDIF
1076      !
1077   END FUNCTION iom_varid
1078
1079
1080   !!----------------------------------------------------------------------
1081   !!                   INTERFACE iom_get
1082   !!----------------------------------------------------------------------
1083   SUBROUTINE iom_g0d( kiomid, cdvar, pvar, ktime )
1084      INTEGER         , INTENT(in   )                 ::   kiomid    ! Identifier of the file
1085      CHARACTER(len=*), INTENT(in   )                 ::   cdvar     ! Name of the variable
1086      REAL(wp)        , INTENT(  out)                 ::   pvar      ! read field
1087      INTEGER         , INTENT(in   ),     OPTIONAL   ::   ktime     ! record number
1088      !
1089      INTEGER                                         ::   idvar     ! variable id
1090      INTEGER                                         ::   idmspc    ! number of spatial dimensions
1091      INTEGER         , DIMENSION(1)                  ::   itime     ! record number
1092      CHARACTER(LEN=100)                              ::   clinfo    ! info character
1093      CHARACTER(LEN=100)                              ::   clname    ! file name
1094      CHARACTER(LEN=1)                                ::   cldmspc   !
1095      !
1096      itime = 1
1097      IF( PRESENT(ktime) ) itime = ktime
1098      !
1099      clname = iom_file(kiomid)%name
1100      clinfo = '          iom_g0d, file: '//trim(clname)//', var: '//trim(cdvar)
1101      !
1102      IF( kiomid > 0 ) THEN
1103         idvar = iom_varid( kiomid, cdvar )
1104         IF( iom_file(kiomid)%nfid > 0 .AND. idvar > 0 ) THEN
1105            idmspc = iom_file ( kiomid )%ndims( idvar )
1106            IF( iom_file(kiomid)%luld(idvar) )  idmspc = idmspc - 1
1107            WRITE(cldmspc , fmt='(i1)') idmspc
1108            IF( idmspc > 0 )  CALL ctl_stop( TRIM(clinfo), 'When reading to a 0D array, we do not accept data', &
1109                                 &                         'with 1 or more spatial dimensions: '//cldmspc//' were found.' , &
1110                                 &                         'Use ncwa -a to suppress the unnecessary dimensions' )
1111            SELECT CASE (iom_file(kiomid)%iolib)
1112            CASE (jpioipsl )   ;   CALL iom_ioipsl_get(  kiomid, idvar, pvar, itime )
1113            CASE (jpnf90   )   ;   CALL iom_nf90_get(    kiomid, idvar, pvar, itime )
1114            CASE (jprstdimg)   ;   CALL iom_rstdimg_get( kiomid, idvar, pvar )
1115            CASE DEFAULT   
1116               CALL ctl_stop( 'iom_g0d: accepted IO library are only jpioipsl, jpnf90 and jprstdimg' )
1117            END SELECT
1118         ENDIF
1119      ENDIF
1120   END SUBROUTINE iom_g0d
1121
1122   SUBROUTINE iom_g1d( kiomid, kdom, cdvar, pvar, ktime, kstart, kcount )
1123      INTEGER         , INTENT(in   )                         ::   kiomid    ! Identifier of the file
1124      INTEGER         , INTENT(in   )                         ::   kdom      ! Type of domain to be read
1125      CHARACTER(len=*), INTENT(in   )                         ::   cdvar     ! Name of the variable
1126      REAL(wp)        , INTENT(  out), DIMENSION(:)           ::   pvar      ! read field
1127      INTEGER         , INTENT(in   )              , OPTIONAL ::   ktime     ! record number
1128      INTEGER         , INTENT(in   ), DIMENSION(1), OPTIONAL ::   kstart    ! start axis position of the reading
1129      INTEGER         , INTENT(in   ), DIMENSION(1), OPTIONAL ::   kcount    ! number of points in each axis
1130      !
1131      IF( kiomid > 0 ) THEN
1132         IF( iom_file(kiomid)%nfid > 0 ) CALL iom_get_123d( kiomid, kdom       , cdvar        , pv_r1d=pvar,   &
1133              &                                                     ktime=ktime, kstart=kstart, kcount=kcount )
1134      ENDIF
1135   END SUBROUTINE iom_g1d
1136
1137   SUBROUTINE iom_g2d( kiomid, kdom, cdvar, pvar, ktime, kstart, kcount, lrowattr )
1138      INTEGER         , INTENT(in   )                           ::   kiomid    ! Identifier of the file
1139      INTEGER         , INTENT(in   )                           ::   kdom      ! Type of domain to be read
1140      CHARACTER(len=*), INTENT(in   )                           ::   cdvar     ! Name of the variable
1141      REAL(wp)        , INTENT(  out), DIMENSION(:,:)           ::   pvar      ! read field
1142      INTEGER         , INTENT(in   )                , OPTIONAL ::   ktime     ! record number
1143      INTEGER         , INTENT(in   ), DIMENSION(2)  , OPTIONAL ::   kstart    ! start axis position of the reading
1144      INTEGER         , INTENT(in   ), DIMENSION(2)  , OPTIONAL ::   kcount    ! number of points in each axis
1145      LOGICAL         , INTENT(in   )                , OPTIONAL ::   lrowattr  ! logical flag telling iom_get to
1146                                                                               ! look for and use a file attribute
1147                                                                               ! called open_ocean_jstart to set the start
1148                                                                               ! value for the 2nd dimension (netcdf only)
1149      !
1150      IF( kiomid > 0 ) THEN
1151         IF( iom_file(kiomid)%nfid > 0 ) CALL iom_get_123d( kiomid, kdom       , cdvar        , pv_r2d=pvar,   &
1152              &                                                     ktime=ktime, kstart=kstart, kcount=kcount, &
1153              &                                                     lrowattr=lrowattr )
1154      ENDIF
1155   END SUBROUTINE iom_g2d
1156
1157   SUBROUTINE iom_g3d( kiomid, kdom, cdvar, pvar, ktime, kstart, kcount, lrowattr )
1158      INTEGER         , INTENT(in   )                             ::   kiomid    ! Identifier of the file
1159      INTEGER         , INTENT(in   )                             ::   kdom      ! Type of domain to be read
1160      CHARACTER(len=*), INTENT(in   )                             ::   cdvar     ! Name of the variable
1161      REAL(wp)        , INTENT(  out), DIMENSION(:,:,:)           ::   pvar      ! read field
1162      INTEGER         , INTENT(in   )                  , OPTIONAL ::   ktime     ! record number
1163      INTEGER         , INTENT(in   ), DIMENSION(3)    , OPTIONAL ::   kstart    ! start axis position of the reading
1164      INTEGER         , INTENT(in   ), DIMENSION(3)    , OPTIONAL ::   kcount    ! number of points in each axis
1165      LOGICAL         , INTENT(in   )                  , OPTIONAL ::   lrowattr  ! logical flag telling iom_get to
1166                                                                                 ! look for and use a file attribute
1167                                                                                 ! called open_ocean_jstart to set the start
1168                                                                                 ! value for the 2nd dimension (netcdf only)
1169      !
1170      IF( kiomid > 0 ) THEN
1171         IF( iom_file(kiomid)%nfid > 0 ) CALL iom_get_123d( kiomid, kdom       , cdvar        , pv_r3d=pvar,   &
1172              &                                                     ktime=ktime, kstart=kstart, kcount=kcount, &
1173              &                                                     lrowattr=lrowattr )
1174      ENDIF
1175   END SUBROUTINE iom_g3d
1176   !!----------------------------------------------------------------------
1177
1178   SUBROUTINE iom_get_123d( kiomid, kdom  , cdvar ,   &
1179         &                  pv_r1d, pv_r2d, pv_r3d,   &
1180         &                  ktime , kstart, kcount,   &
1181         &                  lrowattr                )
1182      !!-----------------------------------------------------------------------
1183      !!                  ***  ROUTINE  iom_get_123d  ***
1184      !!
1185      !! ** Purpose : read a 1D/2D/3D variable
1186      !!
1187      !! ** Method : read ONE record at each CALL
1188      !!-----------------------------------------------------------------------
1189      INTEGER                    , INTENT(in   )           ::   kiomid     ! Identifier of the file
1190      INTEGER                    , INTENT(in   )           ::   kdom       ! Type of domain to be read
1191      CHARACTER(len=*)           , INTENT(in   )           ::   cdvar      ! Name of the variable
1192      REAL(wp), DIMENSION(:)     , INTENT(  out), OPTIONAL ::   pv_r1d     ! read field (1D case)
1193      REAL(wp), DIMENSION(:,:)   , INTENT(  out), OPTIONAL ::   pv_r2d     ! read field (2D case)
1194      REAL(wp), DIMENSION(:,:,:) , INTENT(  out), OPTIONAL ::   pv_r3d     ! read field (3D case)
1195      INTEGER                    , INTENT(in   ), OPTIONAL ::   ktime      ! record number
1196      INTEGER , DIMENSION(:)     , INTENT(in   ), OPTIONAL ::   kstart     ! start position of the reading in each axis
1197      INTEGER , DIMENSION(:)     , INTENT(in   ), OPTIONAL ::   kcount     ! number of points to be read in each axis
1198      LOGICAL                    , INTENT(in   ), OPTIONAL ::   lrowattr   ! logical flag telling iom_get to
1199                                                                           ! look for and use a file attribute
1200                                                                           ! called open_ocean_jstart to set the start
1201                                                                           ! value for the 2nd dimension (netcdf only)
1202      !
1203      LOGICAL                        ::   llnoov      ! local definition to read overlap
1204      LOGICAL                        ::   luse_jattr  ! local definition to read open_ocean_jstart file attribute
1205      INTEGER                        ::   jstartrow   ! start point for 2nd dimension optionally set by file attribute
1206      INTEGER                        ::   jl          ! loop on number of dimension
1207      INTEGER                        ::   idom        ! type of domain
1208      INTEGER                        ::   idvar       ! id of the variable
1209      INTEGER                        ::   inbdim      ! number of dimensions of the variable
1210      INTEGER                        ::   idmspc      ! number of spatial dimensions
1211      INTEGER                        ::   itime       ! record number
1212      INTEGER                        ::   istop       ! temporary value of nstop
1213      INTEGER                        ::   ix1, ix2, iy1, iy2   ! subdomain indexes
1214      INTEGER                        ::   ji, jj      ! loop counters
1215      INTEGER                        ::   irankpv     !
1216      INTEGER                        ::   ind1, ind2  ! substring index
1217      INTEGER, DIMENSION(jpmax_dims) ::   istart      ! starting point to read for each axis
1218      INTEGER, DIMENSION(jpmax_dims) ::   icnt        ! number of value to read along each axis
1219      INTEGER, DIMENSION(jpmax_dims) ::   idimsz      ! size of the dimensions of the variable
1220      INTEGER, DIMENSION(jpmax_dims) ::   ishape      ! size of the dimensions of the variable
1221      REAL(wp)                       ::   zscf, zofs  ! sacle_factor and add_offset
1222      INTEGER                        ::   itmp        ! temporary integer
1223      CHARACTER(LEN=256)             ::   clinfo      ! info character
1224      CHARACTER(LEN=256)             ::   clname      ! file name
1225      CHARACTER(LEN=1)               ::   clrankpv, cldmspc      !
1226      LOGICAL                        ::   ll_depth_spec ! T => if kstart, kcount present then *only* use values for 3rd spatial dimension.
1227      !---------------------------------------------------------------------
1228      !
1229      clname = iom_file(kiomid)%name   !   esier to read
1230      clinfo = '          iom_get_123d, file: '//trim(clname)//', var: '//trim(cdvar)
1231      ! local definition of the domain ?
1232      idom = kdom
1233      ! do we read the overlap
1234      ! ugly patch SM+JMM+RB to overwrite global definition in some cases
1235      llnoov = (jpni * jpnj ) == jpnij .AND. .NOT. lk_agrif 
1236      ! check kcount and kstart optionals parameters...
1237      IF( PRESENT(kcount) .AND. (.NOT. PRESENT(kstart)) ) CALL ctl_stop(trim(clinfo), 'kcount present needs kstart present')
1238      IF( PRESENT(kstart) .AND. (.NOT. PRESENT(kcount)) ) CALL ctl_stop(trim(clinfo), 'kstart present needs kcount present')
1239      IF( PRESENT(kstart) .AND. idom /= jpdom_unknown .AND.  idom /= jpdom_autoglo_xy  ) &
1240     &           CALL ctl_stop(trim(clinfo), 'kstart present needs kdom = jpdom_unknown or kdom = jpdom_autoglo_xy')
1241
1242      luse_jattr = .false.
1243      IF( PRESENT(lrowattr) ) THEN
1244         IF( lrowattr .AND. idom /= jpdom_data   ) CALL ctl_stop(trim(clinfo), 'lrowattr present and true needs kdom = jpdom_data')
1245         IF( lrowattr .AND. idom == jpdom_data   ) luse_jattr = .true.
1246      ENDIF
1247      IF( luse_jattr ) THEN
1248         SELECT CASE (iom_file(kiomid)%iolib)
1249         CASE (jpioipsl, jprstdimg )
1250             CALL ctl_warn(trim(clinfo), 'lrowattr present and true but this only works with netcdf (jpnf90)')
1251             luse_jattr = .false.
1252         CASE (jpnf90   )   
1253             ! Ok
1254         CASE DEFAULT   
1255            CALL ctl_stop( TRIM(clinfo)//' accepted IO library are only jpioipsl, jpnf90 and jprstdimg' )
1256         END SELECT
1257      ENDIF
1258
1259      ! Search for the variable in the data base (eventually actualize data)
1260      istop = nstop
1261      idvar = iom_varid( kiomid, cdvar )
1262      !
1263      IF( idvar > 0 ) THEN
1264         ! to write iom_file(kiomid)%dimsz in a shorter way !
1265         idimsz(:) = iom_file(kiomid)%dimsz(:, idvar) 
1266         inbdim = iom_file(kiomid)%ndims(idvar)            ! number of dimensions in the file
1267         idmspc = inbdim                                   ! number of spatial dimensions in the file
1268         IF( iom_file(kiomid)%luld(idvar) )   idmspc = inbdim - 1
1269         IF( idmspc > 3 )   CALL ctl_stop(trim(clinfo), 'the file has more than 3 spatial dimensions this case is not coded...') 
1270         !
1271         ! update idom definition...
1272         ! Identify the domain in case of jpdom_auto(glo/dta) definition
1273         IF( idom == jpdom_autoglo_xy ) THEN
1274            ll_depth_spec = .TRUE.
1275            idom = jpdom_autoglo
1276         ELSE
1277            ll_depth_spec = .FALSE.
1278         ENDIF
1279         IF( idom == jpdom_autoglo .OR. idom == jpdom_autodta ) THEN           
1280            IF( idom == jpdom_autoglo ) THEN   ;   idom = jpdom_global 
1281            ELSE                               ;   idom = jpdom_data
1282            ENDIF
1283            ind1 = INDEX( clname, '_', back = .TRUE. ) + 1
1284            ind2 = INDEX( clname, '.', back = .TRUE. ) - 1
1285            IF( ind2 > ind1 ) THEN   ;   IF( VERIFY( clname(ind1:ind2), '0123456789' ) == 0 )   idom = jpdom_local   ;   ENDIF
1286         ENDIF
1287         ! Identify the domain in case of jpdom_local definition
1288         IF( idom == jpdom_local ) THEN
1289            IF(     idimsz(1) == jpi               .AND. idimsz(2) == jpj               ) THEN   ;   idom = jpdom_local_full
1290            ELSEIF( idimsz(1) == nlci              .AND. idimsz(2) == nlcj              ) THEN   ;   idom = jpdom_local_noextra
1291            ELSEIF( idimsz(1) == (nlei - nldi + 1) .AND. idimsz(2) == (nlej - nldj + 1) ) THEN   ;   idom = jpdom_local_noovlap
1292            ELSE   ;   CALL ctl_stop( trim(clinfo), 'impossible to identify the local domain' )
1293            ENDIF
1294         ENDIF
1295         !
1296         ! check the consistency between input array and data rank in the file
1297         !
1298         ! initializations
1299         itime = 1
1300         IF( PRESENT(ktime) ) itime = ktime
1301
1302         irankpv = 1 * COUNT( (/PRESENT(pv_r1d)/) ) + 2 * COUNT( (/PRESENT(pv_r2d)/) ) + 3 * COUNT( (/PRESENT(pv_r3d)/) )
1303         WRITE(clrankpv, fmt='(i1)') irankpv
1304         WRITE(cldmspc , fmt='(i1)') idmspc
1305         !
1306         IF(     idmspc <  irankpv ) THEN
1307            CALL ctl_stop( TRIM(clinfo), 'The file has only '//cldmspc//' spatial dimension',   &
1308               &                         'it is impossible to read a '//clrankpv//'D array from this file...' )
1309         ELSEIF( idmspc == irankpv ) THEN
1310            IF( PRESENT(pv_r1d) .AND. idom /= jpdom_unknown )   &
1311               &   CALL ctl_stop( TRIM(clinfo), 'case not coded...You must use jpdom_unknown' )
1312         ELSEIF( idmspc >  irankpv ) THEN
1313               IF( PRESENT(pv_r2d) .AND. itime == 1 .AND. idimsz(3) == 1 .AND. idmspc == 3 ) THEN
1314                  CALL ctl_warn( trim(clinfo), '2D array but 3 spatial dimensions for the data...'              ,   &
1315                        &         'As the size of the z dimension is 1 and as we try to read the first record, ',   &
1316                        &         'we accept this case, even if there is a possible mix-up between z and time dimension' )   
1317                  idmspc = idmspc - 1
1318               ELSE
1319                  CALL ctl_stop( TRIM(clinfo), 'To keep iom lisibility, when reading a '//clrankpv//'D array,'         ,   &
1320                     &                         'we do not accept data with '//cldmspc//' spatial dimensions',   &
1321                     &                         'Use ncwa -a to suppress the unnecessary dimensions' )
1322               ENDIF
1323         ENDIF
1324
1325         !
1326         ! definition of istart and icnt
1327         !
1328         icnt  (:) = 1
1329         istart(:) = 1
1330         istart(idmspc+1) = itime
1331
1332         IF( PRESENT(kstart) .AND. .NOT. ll_depth_spec ) THEN ; istart(1:idmspc) = kstart(1:idmspc) ; icnt(1:idmspc) = kcount(1:idmspc)
1333         ELSE
1334            IF(           idom == jpdom_unknown ) THEN                                                ; icnt(1:idmspc) = idimsz(1:idmspc)
1335            ELSE
1336               IF( .NOT. PRESENT(pv_r1d) ) THEN   !   not a 1D array
1337                  IF(     idom == jpdom_data    ) THEN
1338                     jstartrow = 1
1339                     IF( luse_jattr ) THEN
1340                        CALL iom_getatt(kiomid, 'open_ocean_jstart', jstartrow ) ! -999 is returned if the attribute is not found
1341                        jstartrow = MAX(1,jstartrow)
1342                     ENDIF
1343                     istart(1:2) = (/ mig(1), mjg(1) + jstartrow - 1 /)  ! icnt(1:2) done below
1344                  ELSEIF( idom == jpdom_global  ) THEN ; istart(1:2) = (/ nimpp , njmpp  /)  ! icnt(1:2) done below
1345                  ENDIF
1346                  ! we do not read the overlap                     -> we start to read at nldi, nldj
1347! JMM + SM: ugly patch before getting the new version of lib_mpp)
1348!                  IF( idom /= jpdom_local_noovlap )   istart(1:2) = istart(1:2) + (/ nldi - 1, nldj - 1 /)
1349                  IF( llnoov .AND. idom /= jpdom_local_noovlap ) istart(1:2) = istart(1:2) + (/ nldi - 1, nldj - 1 /)
1350                  ! we do not read the overlap and the extra-halos -> from nldi to nlei and from nldj to nlej
1351! JMM + SM: ugly patch before getting the new version of lib_mpp)
1352!                  icnt(1:2) = (/ nlei - nldi + 1, nlej - nldj + 1 /)
1353                  IF( llnoov ) THEN   ;   icnt(1:2) = (/ nlei - nldi + 1, nlej - nldj + 1 /)
1354                  ELSE                ;   icnt(1:2) = (/ nlci           , nlcj            /)
1355                  ENDIF
1356                  IF( PRESENT(pv_r3d) ) THEN
1357                     IF( idom == jpdom_data ) THEN                                  ; icnt(3) = jpkdta
1358                     ELSE IF( ll_depth_spec .AND. PRESENT(kstart) ) THEN            ; istart(3) = kstart(3); icnt(3) = kcount(3)
1359                     ELSE                                                           ; icnt(3) = jpk
1360                     ENDIF
1361                  ENDIF
1362               ENDIF
1363            ENDIF
1364         ENDIF
1365
1366         ! check that istart and icnt can be used with this file
1367         !-
1368         DO jl = 1, jpmax_dims
1369            itmp = istart(jl)+icnt(jl)-1
1370            IF( itmp > idimsz(jl) .AND. idimsz(jl) /= 0 ) THEN
1371               WRITE( ctmp1, FMT="('(istart(', i1, ') + icnt(', i1, ') - 1) = ', i5)" ) jl, jl, itmp
1372               WRITE( ctmp2, FMT="(' is larger than idimsz(', i1,') = ', i5)"         ) jl, idimsz(jl)
1373               CALL ctl_stop( trim(clinfo), 'start and count too big regarding to the size of the data, ', ctmp1, ctmp2 )     
1374            ENDIF
1375         END DO
1376
1377         ! check that icnt matches the input array
1378         !-     
1379         IF( idom == jpdom_unknown ) THEN
1380            IF( irankpv == 1 )        ishape(1:1) = SHAPE(pv_r1d)
1381            IF( irankpv == 2 )        ishape(1:2) = SHAPE(pv_r2d)
1382            IF( irankpv == 3 )        ishape(1:3) = SHAPE(pv_r3d)
1383            ctmp1 = 'd'
1384         ELSE
1385            IF( irankpv == 2 ) THEN
1386! JMM + SM: ugly patch before getting the new version of lib_mpp)
1387!               ishape(1:2) = SHAPE(pv_r2d(nldi:nlei,nldj:nlej  ))   ;   ctmp1 = 'd(nldi:nlei,nldj:nlej)'
1388               IF( llnoov ) THEN ; ishape(1:2)=SHAPE(pv_r2d(nldi:nlei,nldj:nlej  )) ; ctmp1='d(nldi:nlei,nldj:nlej)'
1389               ELSE              ; ishape(1:2)=SHAPE(pv_r2d(1   :nlci,1   :nlcj  )) ; ctmp1='d(1:nlci,1:nlcj)'
1390               ENDIF
1391            ENDIF
1392            IF( irankpv == 3 ) THEN 
1393! JMM + SM: ugly patch before getting the new version of lib_mpp)
1394!               ishape(1:3) = SHAPE(pv_r3d(nldi:nlei,nldj:nlej,:))   ;   ctmp1 = 'd(nldi:nlei,nldj:nlej,:)'
1395               IF( llnoov ) THEN ; ishape(1:3)=SHAPE(pv_r3d(nldi:nlei,nldj:nlej,:)) ; ctmp1='d(nldi:nlei,nldj:nlej,:)'
1396               ELSE              ; ishape(1:3)=SHAPE(pv_r3d(1   :nlci,1   :nlcj,:)) ; ctmp1='d(1:nlci,1:nlcj,:)'
1397               ENDIF
1398            ENDIF
1399         ENDIF
1400         
1401         DO jl = 1, irankpv
1402            WRITE( ctmp2, FMT="(', ', i1,'): ', i5,' /= icnt(', i1,'):', i5)" ) jl, ishape(jl), jl, icnt(jl)
1403            IF( ishape(jl) /= icnt(jl) )   CALL ctl_stop( TRIM(clinfo), 'size(pv_r'//clrankpv//TRIM(ctmp1)//TRIM(ctmp2) )
1404         END DO
1405
1406      ENDIF
1407
1408      ! read the data
1409      !-     
1410      IF( idvar > 0 .AND. istop == nstop ) THEN   ! no additional errors until this point...
1411         !
1412         ! find the right index of the array to be read
1413! JMM + SM: ugly patch before getting the new version of lib_mpp)
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         IF( llnoov ) THEN
1418            IF( idom /= jpdom_unknown ) THEN   ;   ix1 = nldi   ;   ix2 = nlei      ;   iy1 = nldj   ;   iy2 = nlej
1419            ELSE                               ;   ix1 = 1      ;   ix2 = icnt(1)   ;   iy1 = 1      ;   iy2 = icnt(2)
1420            ENDIF
1421         ELSE
1422            IF( idom /= jpdom_unknown ) THEN   ;   ix1 = 1      ;   ix2 = nlci      ;   iy1 = 1      ;   iy2 = nlcj
1423            ELSE                               ;   ix1 = 1      ;   ix2 = icnt(1)   ;   iy1 = 1      ;   iy2 = icnt(2)
1424            ENDIF
1425         ENDIF
1426     
1427         SELECT CASE (iom_file(kiomid)%iolib)
1428         CASE (jpioipsl )   ;   CALL iom_ioipsl_get(  kiomid, idvar, inbdim, istart, icnt, ix1, ix2, iy1, iy2,   &
1429            &                                         pv_r1d, pv_r2d, pv_r3d )
1430         CASE (jpnf90   )   ;   CALL iom_nf90_get(    kiomid, idvar, inbdim, istart, icnt, ix1, ix2, iy1, iy2,   &
1431            &                                         pv_r1d, pv_r2d, pv_r3d )
1432         CASE (jprstdimg)   ;   CALL iom_rstdimg_get( kiomid, idom, idvar, ix1, ix2, iy1, iy2,   &
1433            &                                         pv_r1d, pv_r2d, pv_r3d )
1434         CASE DEFAULT   
1435            CALL ctl_stop( TRIM(clinfo)//' accepted IO library are only jpioipsl, jpnf90 and jprstdimg' )
1436         END SELECT
1437
1438         IF( istop == nstop ) THEN   ! no additional errors until this point...
1439            IF(lwp) WRITE(numout,"(10x,' read ',a,' (rec: ',i6,') in ',a,' ok')") TRIM(cdvar), itime, TRIM(iom_file(kiomid)%name)
1440         
1441            !--- overlap areas and extra hallows (mpp)
1442            IF(     PRESENT(pv_r2d) .AND. idom /= jpdom_unknown ) THEN
1443               CALL lbc_lnk( pv_r2d,'Z',-999.,'no0' )
1444            ELSEIF( PRESENT(pv_r3d) .AND. idom /= jpdom_unknown ) THEN
1445               ! this if could be simplified with the new lbc_lnk that works with any size of the 3rd dimension
1446               IF( icnt(3) == jpk ) THEN
1447                  CALL lbc_lnk( pv_r3d,'Z',-999.,'no0' )
1448               ELSE   ! put some arbitrary value (a call to lbc_lnk will be done later...)
1449                  DO jj = nlcj+1, jpj   ;   pv_r3d(1:nlci, jj, :) = pv_r3d(1:nlci, nlej, :)   ;   END DO
1450                  DO ji = nlci+1, jpi   ;   pv_r3d(ji    , : , :) = pv_r3d(nlei  , :   , :)   ;   END DO
1451               ENDIF
1452            ENDIF
1453           
1454            ! C1D case : always call lbc_lnk to replicate the central value over the whole 3X3 domain
1455            IF( lk_c1d .AND. PRESENT(pv_r2d) )   CALL lbc_lnk( pv_r2d,'Z',1. )
1456            IF( lk_c1d .AND. PRESENT(pv_r3d) )   CALL lbc_lnk( pv_r3d,'Z',1. )
1457   
1458            !--- Apply scale_factor and offset
1459            zscf = iom_file(kiomid)%scf(idvar)      ! scale factor
1460            zofs = iom_file(kiomid)%ofs(idvar)      ! offset
1461            IF(     PRESENT(pv_r1d) ) THEN
1462               IF( zscf /= 1. )   pv_r1d(:) = pv_r1d(:) * zscf 
1463               IF( zofs /= 0. )   pv_r1d(:) = pv_r1d(:) + zofs
1464            ELSEIF( PRESENT(pv_r2d) ) THEN
1465!CDIR COLLAPSE
1466              IF( zscf /= 1.)   pv_r2d(:,:) = pv_r2d(:,:) * zscf
1467!CDIR COLLAPSE
1468               IF( zofs /= 0.)   pv_r2d(:,:) = pv_r2d(:,:) + zofs
1469            ELSEIF( PRESENT(pv_r3d) ) THEN
1470!CDIR COLLAPSE
1471               IF( zscf /= 1.)   pv_r3d(:,:,:) = pv_r3d(:,:,:) * zscf
1472!CDIR COLLAPSE
1473               IF( zofs /= 0.)   pv_r3d(:,:,:) = pv_r3d(:,:,:) + zofs
1474            ENDIF
1475            !
1476         ENDIF
1477         !
1478      ENDIF
1479      !
1480   END SUBROUTINE iom_get_123d
1481
1482
1483   SUBROUTINE iom_gettime( kiomid, ptime, cdvar, kntime, cdunits, cdcalendar )
1484      !!--------------------------------------------------------------------
1485      !!                   ***  SUBROUTINE iom_gettime  ***
1486      !!
1487      !! ** Purpose : read the time axis cdvar in the file
1488      !!--------------------------------------------------------------------
1489      INTEGER                    , INTENT(in   ) ::   kiomid     ! file Identifier
1490      REAL(wp), DIMENSION(:)     , INTENT(  out) ::   ptime      ! the time axis
1491      CHARACTER(len=*), OPTIONAL , INTENT(in   ) ::   cdvar      ! time axis name
1492      INTEGER         , OPTIONAL , INTENT(  out) ::   kntime     ! number of times in file
1493      CHARACTER(len=*), OPTIONAL , INTENT(  out) ::   cdunits    ! units attribute of time coordinate
1494      CHARACTER(len=*), OPTIONAL , INTENT(  out) ::   cdcalendar ! calendar attribute of
1495      !
1496      INTEGER, DIMENSION(1) :: kdimsz
1497      INTEGER            ::   idvar    ! id of the variable
1498      CHARACTER(LEN=32)  ::   tname    ! local name of time coordinate
1499      CHARACTER(LEN=100) ::   clinfo   ! info character
1500      !---------------------------------------------------------------------
1501      !
1502      IF ( PRESENT(cdvar) ) THEN
1503         tname = cdvar
1504      ELSE
1505         tname = iom_file(kiomid)%uldname
1506      ENDIF
1507      IF( kiomid > 0 ) THEN
1508         clinfo = 'iom_gettime, file: '//trim(iom_file(kiomid)%name)//', var: '//trim(tname)
1509         IF ( PRESENT(kntime) ) THEN
1510            idvar  = iom_varid( kiomid, tname, kdimsz = kdimsz )
1511            kntime = kdimsz(1)
1512         ELSE
1513            idvar = iom_varid( kiomid, tname )
1514         ENDIF
1515         !
1516         ptime(:) = 0. ! default definition
1517         IF( idvar > 0 ) THEN
1518            IF( iom_file(kiomid)%ndims(idvar) == 1 ) THEN
1519               IF( iom_file(kiomid)%luld(idvar) ) THEN
1520                  IF( iom_file(kiomid)%dimsz(1,idvar) <= size(ptime) ) THEN
1521                     SELECT CASE (iom_file(kiomid)%iolib)
1522                     CASE (jpioipsl )   ;   CALL iom_ioipsl_gettime( kiomid, idvar, ptime, cdunits, cdcalendar )
1523                     CASE (jpnf90   )   ;   CALL iom_nf90_gettime(   kiomid, idvar, ptime, cdunits, cdcalendar )
1524                     CASE (jprstdimg)   ;   CALL ctl_stop( TRIM(clinfo)//' case IO library == jprstdimg not coded...' )
1525                     CASE DEFAULT   
1526                        CALL ctl_stop( TRIM(clinfo)//' accepted IO library are only jpioipsl, jpnf90 and jprstdimg' )
1527                     END SELECT
1528                  ELSE
1529                     WRITE(ctmp1,*) 'error with the size of ptime ',size(ptime),iom_file(kiomid)%dimsz(1,idvar)
1530                     CALL ctl_stop( trim(clinfo), trim(ctmp1) )
1531                  ENDIF
1532               ELSE
1533                  CALL ctl_stop( trim(clinfo), 'variable dimension is not unlimited... use iom_get' )
1534               ENDIF
1535            ELSE
1536               CALL ctl_stop( trim(clinfo), 'the variable has more than 1 dimension' )
1537            ENDIF
1538         ELSE
1539            CALL ctl_stop( trim(clinfo), 'variable not found in '//iom_file(kiomid)%name )
1540         ENDIF
1541      ENDIF
1542      !
1543   END SUBROUTINE iom_gettime
1544
1545
1546   !!----------------------------------------------------------------------
1547   !!                   INTERFACE iom_getatt
1548   !!----------------------------------------------------------------------
1549   SUBROUTINE iom_g0d_intatt( kiomid, cdatt, pvar )
1550      INTEGER         , INTENT(in   )                 ::   kiomid    ! Identifier of the file
1551      CHARACTER(len=*), INTENT(in   )                 ::   cdatt     ! Name of the attribute
1552      INTEGER         , INTENT(  out)                 ::   pvar      ! read field
1553      !
1554      IF( kiomid > 0 ) THEN
1555         IF( iom_file(kiomid)%nfid > 0 ) THEN
1556            SELECT CASE (iom_file(kiomid)%iolib)
1557            CASE (jpioipsl )   ;   CALL ctl_stop('iom_getatt: only nf90 available')
1558            CASE (jpnf90   )   ;   CALL iom_nf90_getatt( kiomid, cdatt, pvar )
1559            CASE (jprstdimg)   ;   CALL ctl_stop('iom_getatt: only nf90 available')
1560            CASE DEFAULT   
1561               CALL ctl_stop( 'iom_g0d_att: accepted IO library are only jpioipsl, jpnf90 and jprstdimg' )
1562            END SELECT
1563         ENDIF
1564      ENDIF
1565   END SUBROUTINE iom_g0d_intatt
1566
1567
1568   !!----------------------------------------------------------------------
1569   !!                   INTERFACE iom_rstput
1570   !!----------------------------------------------------------------------
1571   SUBROUTINE iom_rp0d( kt, kwrite, kiomid, cdvar, pvar, ktype, lxios )
1572      INTEGER         , INTENT(in)                         ::   kt       ! ocean time-step
1573      INTEGER         , INTENT(in)                         ::   kwrite   ! writing time-step
1574      INTEGER         , INTENT(in)                         ::   kiomid   ! Identifier of the file
1575      CHARACTER(len=*), INTENT(in)                         ::   cdvar    ! time axis name
1576      REAL(wp)        , INTENT(in)                         ::   pvar     ! written field
1577      INTEGER         , INTENT(in), OPTIONAL               ::   ktype    ! variable external type
1578      LOGICAL, OPTIONAL :: lxios   ! xios write flag
1579      LOGICAL :: lx                ! local xios write flag
1580      INTEGER :: ivid   ! variable id
1581
1582      lx = .FALSE.
1583      IF(PRESENT(lxios)) lx = lxios
1584      IF( lx ) THEN
1585#ifdef key_iomput
1586      IF( kt == kwrite ) THEN
1587          IF(lwp) write(numout,*) 'RESTART: write (XIOS 0D) ',trim(cdvar)
1588          CALL xios_send_field(trim(cdvar), pvar)
1589      ENDIF
1590#endif
1591      ELSE
1592         IF( kiomid > 0 ) THEN
1593            IF( iom_file(kiomid)%nfid > 0 ) THEN
1594               ivid = iom_varid( kiomid, cdvar, ldstop = .FALSE. )
1595               SELECT CASE (iom_file(kiomid)%iolib)
1596               CASE (jpioipsl )   ;   CALL iom_ioipsl_rstput( kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r0d = pvar )
1597               CASE (jpnf90   )   ;   CALL iom_nf90_rstput(   kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r0d = pvar )
1598               CASE (jprstdimg)   ;   IF( kt == kwrite )    CALL iom_rstdimg_rstput( kiomid, cdvar, ivid, pvar )
1599               CASE DEFAULT     
1600                  CALL ctl_stop( 'iom_rp0d: accepted IO library are only jpioipsl, jpnf90 and jprstdimg' )
1601               END SELECT
1602            ENDIF
1603         ENDIF
1604      ENDIF
1605   END SUBROUTINE iom_rp0d
1606
1607   SUBROUTINE iom_rp1d( kt, kwrite, kiomid, cdvar, pvar, ktype, lxios )
1608      INTEGER         , INTENT(in)                         ::   kt       ! ocean time-step
1609      INTEGER         , INTENT(in)                         ::   kwrite   ! writing time-step
1610      INTEGER         , INTENT(in)                         ::   kiomid   ! Identifier of the file
1611      CHARACTER(len=*), INTENT(in)                         ::   cdvar    ! time axis name
1612      REAL(wp)        , INTENT(in), DIMENSION(          :) ::   pvar     ! written field
1613      INTEGER         , INTENT(in), OPTIONAL               ::   ktype    ! variable external type
1614      LOGICAL, OPTIONAL :: lxios   ! xios write flag
1615      LOGICAL :: lx                ! local xios write flag
1616      INTEGER :: ivid   ! variable id
1617
1618      lx = .FALSE.
1619      IF(PRESENT(lxios)) lx = lxios
1620      IF( lx ) THEN
1621#ifdef key_iomput
1622      IF( kt == kwrite ) THEN
1623         IF(lwp) write(numout,*) 'RESTART: write (XIOS 1D) ',trim(cdvar)
1624         CALL xios_send_field(trim(cdvar), pvar)
1625      ENDIF
1626#endif
1627      ELSE
1628         IF( kiomid > 0 ) THEN
1629            IF( iom_file(kiomid)%nfid > 0 ) THEN
1630               ivid = iom_varid( kiomid, cdvar, ldstop = .FALSE. )
1631               SELECT CASE (iom_file(kiomid)%iolib)
1632               CASE (jpioipsl )   ;   CALL iom_ioipsl_rstput( kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r1d = pvar )
1633               CASE (jpnf90   )   ;   CALL iom_nf90_rstput(   kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r1d = pvar )
1634               CASE (jprstdimg)   ;   IF( kt == kwrite )    CALL iom_rstdimg_rstput( kiomid, cdvar, ivid, pv_r1d = pvar )
1635               CASE DEFAULT     
1636                  CALL ctl_stop( 'iom_rp1d: accepted IO library are only jpioipsl, jpnf90 and jprstdimg' )
1637               END SELECT
1638            ENDIF
1639         ENDIF
1640      ENDIF
1641   END SUBROUTINE iom_rp1d
1642
1643   SUBROUTINE iom_rp2d( kt, kwrite, kiomid, cdvar, pvar, ktype, lxios )
1644      INTEGER         , INTENT(in)                         ::   kt       ! ocean time-step
1645      INTEGER         , INTENT(in)                         ::   kwrite   ! writing time-step
1646      INTEGER         , INTENT(in)                         ::   kiomid   ! Identifier of the file
1647      CHARACTER(len=*), INTENT(in)                         ::   cdvar    ! time axis name
1648      REAL(wp)        , INTENT(in), DIMENSION(:,    :    ) ::   pvar     ! written field
1649      INTEGER         , INTENT(in), OPTIONAL               ::   ktype    ! variable external type
1650      LOGICAL, OPTIONAL :: lxios   ! xios write flag
1651      LOGICAL :: lx                ! local xios write flag
1652      INTEGER :: ivid   ! variable id
1653
1654      lx = .FALSE.
1655      IF(PRESENT(lxios)) lx = lxios
1656      IF( lx ) THEN
1657#ifdef key_iomput
1658      IF( kt == kwrite ) THEN
1659         IF(lwp) write(numout,*) 'RESTART: write (XIOS 2D) ',trim(cdvar)
1660         CALL xios_send_field(trim(cdvar), pvar)
1661      ENDIF
1662#endif
1663      ELSE
1664         IF( kiomid > 0 ) THEN
1665            IF( iom_file(kiomid)%nfid > 0 ) THEN
1666               ivid = iom_varid( kiomid, cdvar, ldstop = .FALSE. )
1667               SELECT CASE (iom_file(kiomid)%iolib)
1668               CASE (jpioipsl )   ;   CALL iom_ioipsl_rstput( kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r2d = pvar )
1669               CASE (jpnf90   )   ;   CALL iom_nf90_rstput(   kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r2d = pvar )
1670               CASE (jprstdimg)   ;   IF( kt == kwrite )   CALL iom_rstdimg_rstput( kiomid, cdvar, ivid, pv_r2d = pvar ) 
1671               CASE DEFAULT     
1672                  CALL ctl_stop( 'iom_rp2d: accepted IO library are only jpioipsl, jpnf90 and jprstdimg' )
1673               END SELECT
1674            ENDIF
1675         ENDIF
1676      ENDIF
1677   END SUBROUTINE iom_rp2d
1678
1679   SUBROUTINE iom_rp3d( kt, kwrite, kiomid, cdvar, pvar, ktype, lxios )
1680      INTEGER         , INTENT(in)                         ::   kt       ! ocean time-step
1681      INTEGER         , INTENT(in)                         ::   kwrite   ! writing time-step
1682      INTEGER         , INTENT(in)                         ::   kiomid   ! Identifier of the file
1683      CHARACTER(len=*), INTENT(in)                         ::   cdvar    ! time axis name
1684      REAL(wp)        , INTENT(in),       DIMENSION(:,:,:) ::   pvar     ! written field
1685      INTEGER         , INTENT(in), OPTIONAL               ::   ktype    ! variable external type
1686      LOGICAL, OPTIONAL :: lxios   ! xios write flag
1687      LOGICAL :: lx                 ! local xios write flag
1688      INTEGER :: ivid   ! variable id
1689
1690      lx = .FALSE.
1691      IF(PRESENT(lxios)) lx = lxios
1692      IF( lx ) THEN
1693#ifdef key_iomput
1694      IF( kt == kwrite ) THEN
1695         IF(lwp) write(numout,*) 'RESTART: write (XIOS 3D) ',trim(cdvar)
1696         CALL xios_send_field(trim(cdvar), pvar)
1697      ENDIF
1698#endif
1699      ELSE
1700         IF( kiomid > 0 ) THEN
1701            IF( iom_file(kiomid)%nfid > 0 ) THEN
1702               ivid = iom_varid( kiomid, cdvar, ldstop = .FALSE. )
1703               SELECT CASE (iom_file(kiomid)%iolib)
1704               CASE (jpioipsl )   ;   CALL iom_ioipsl_rstput( kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r3d = pvar )
1705               CASE (jpnf90   )   ;   CALL iom_nf90_rstput(   kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r3d = pvar )
1706               CASE (jprstdimg)   ;   IF( kt == kwrite )   CALL iom_rstdimg_rstput( kiomid, cdvar, ivid, pv_r3d = pvar )
1707               CASE DEFAULT     
1708                  CALL ctl_stop( 'iom_rp3d: accepted IO library are only jpioipsl and jprstdimg' )
1709               END SELECT
1710            ENDIF
1711         ENDIF
1712      ENDIF
1713   END SUBROUTINE iom_rp3d
1714
1715
1716   !!----------------------------------------------------------------------
1717   !!                   INTERFACE iom_put
1718   !!----------------------------------------------------------------------
1719   SUBROUTINE iom_p0d( cdname, pfield0d )
1720      CHARACTER(LEN=*), INTENT(in) ::   cdname
1721      REAL(wp)        , INTENT(in) ::   pfield0d
1722      REAL(wp)        , DIMENSION(jpi,jpj) ::   zz     ! masson
1723#if defined key_iomput
1724      zz(:,:)=pfield0d
1725      CALL xios_send_field(cdname, zz)
1726      !CALL xios_send_field(cdname, (/pfield0d/))
1727#else
1728      IF( .FALSE. )   WRITE(numout,*) cdname, pfield0d   ! useless test to avoid compilation warnings
1729#endif
1730   END SUBROUTINE iom_p0d
1731
1732   SUBROUTINE iom_p1d( cdname, pfield1d )
1733      CHARACTER(LEN=*)          , INTENT(in) ::   cdname
1734      REAL(wp),     DIMENSION(:), INTENT(in) ::   pfield1d
1735#if defined key_iomput
1736      CALL xios_send_field( cdname, RESHAPE( (/pfield1d/), (/1,1,SIZE(pfield1d)/) ) )
1737#else
1738      IF( .FALSE. )   WRITE(numout,*) cdname, pfield1d   ! useless test to avoid compilation warnings
1739#endif
1740   END SUBROUTINE iom_p1d
1741
1742   SUBROUTINE iom_p2d( cdname, pfield2d )
1743      CHARACTER(LEN=*)            , INTENT(in) ::   cdname
1744      REAL(wp),     DIMENSION(:,:), INTENT(in) ::   pfield2d
1745#if defined key_iomput
1746      CALL xios_send_field(cdname, pfield2d)
1747#else
1748      IF( .FALSE. )   WRITE(numout,*) cdname, pfield2d   ! useless test to avoid compilation warnings
1749#endif
1750   END SUBROUTINE iom_p2d
1751
1752   SUBROUTINE iom_p3d( cdname, pfield3d )
1753      CHARACTER(LEN=*)                , INTENT(in) ::   cdname
1754      REAL(wp),       DIMENSION(:,:,:), INTENT(in) ::   pfield3d
1755#if defined key_iomput
1756      CALL xios_send_field(cdname, pfield3d)
1757#else
1758      IF( .FALSE. )   WRITE(numout,*) cdname, pfield3d   ! useless test to avoid compilation warnings
1759#endif
1760   END SUBROUTINE iom_p3d
1761   !!----------------------------------------------------------------------
1762
1763#if defined key_iomput
1764
1765   SUBROUTINE iom_set_domain_attr( cdid, ni_glo, nj_glo, ibegin, jbegin, ni, nj, zoom_ibegin, zoom_jbegin, zoom_ni, zoom_nj,   &
1766      &                                    data_dim, data_ibegin, data_ni, data_jbegin, data_nj, lonvalue, latvalue, mask,     &
1767      &                                    nvertex, bounds_lon, bounds_lat, area )
1768      CHARACTER(LEN=*)                   , INTENT(in) ::   cdid
1769      INTEGER                  , OPTIONAL, INTENT(in) ::   ni_glo, nj_glo, ibegin, jbegin, ni, nj
1770      INTEGER                  , OPTIONAL, INTENT(in) ::   data_dim, data_ibegin, data_ni, data_jbegin, data_nj
1771      INTEGER                  , OPTIONAL, INTENT(in) ::   zoom_ibegin, zoom_jbegin, zoom_ni, zoom_nj, nvertex
1772      REAL(wp), DIMENSION(:)   , OPTIONAL, INTENT(in) ::   lonvalue, latvalue
1773      REAL(wp), DIMENSION(:,:) , OPTIONAL, INTENT(in) ::   bounds_lon, bounds_lat, area
1774#if ! defined key_xios2
1775     LOGICAL,  DIMENSION(:,:) , OPTIONAL, INTENT(in) ::   mask
1776#else
1777      LOGICAL,  DIMENSION(:) , OPTIONAL, INTENT(in) ::   mask
1778#endif
1779
1780#if ! defined key_xios2
1781      IF ( xios_is_valid_domain     (cdid) ) THEN
1782         CALL xios_set_domain_attr     ( cdid, ni_glo=ni_glo, nj_glo=nj_glo, ibegin=ibegin, jbegin=jbegin, ni=ni, nj=nj,   &
1783            &    data_dim=data_dim, data_ibegin=data_ibegin, data_ni=data_ni, data_jbegin=data_jbegin, data_nj=data_nj ,   &
1784            &    zoom_ibegin=zoom_ibegin, zoom_jbegin=zoom_jbegin, zoom_ni=zoom_ni, zoom_nj=zoom_nj,                       &
1785            &    lonvalue=lonvalue, latvalue=latvalue, mask=mask, nvertex=nvertex, bounds_lon=bounds_lon,                  &
1786            &    bounds_lat=bounds_lat, area=area )
1787     ENDIF
1788      IF ( xios_is_valid_domaingroup(cdid) ) THEN
1789         CALL xios_set_domaingroup_attr( cdid, ni_glo=ni_glo, nj_glo=nj_glo, ibegin=ibegin, jbegin=jbegin, ni=ni, nj=nj,   &
1790            &    data_dim=data_dim, data_ibegin=data_ibegin, data_ni=data_ni, data_jbegin=data_jbegin, data_nj=data_nj ,   &
1791            &    zoom_ibegin=zoom_ibegin, zoom_jbegin=zoom_jbegin, zoom_ni=zoom_ni, zoom_nj=zoom_nj,                       &
1792            &    lonvalue=lonvalue, latvalue=latvalue, mask=mask, nvertex=nvertex, bounds_lon=bounds_lon,                  &
1793            &    bounds_lat=bounds_lat, area=area )
1794      ENDIF
1795
1796#else
1797      IF ( xios_is_valid_domain     (cdid) ) THEN
1798         CALL xios_set_domain_attr     ( cdid, ni_glo=ni_glo, nj_glo=nj_glo, ibegin=ibegin, jbegin=jbegin, ni=ni, nj=nj,   &
1799            &    data_dim=data_dim, data_ibegin=data_ibegin, data_ni=data_ni, data_jbegin=data_jbegin, data_nj=data_nj ,   &
1800            &    lonvalue_1D=lonvalue, latvalue_1D=latvalue, mask_1D=mask, nvertex=nvertex, bounds_lon_1D=bounds_lon,                  &
1801            &    bounds_lat_1D=bounds_lat, area=area, type='curvilinear')
1802     ENDIF
1803      IF ( xios_is_valid_domaingroup(cdid) ) THEN
1804         CALL xios_set_domaingroup_attr( cdid, ni_glo=ni_glo, nj_glo=nj_glo, ibegin=ibegin, jbegin=jbegin, ni=ni, nj=nj,   &
1805            &    data_dim=data_dim, data_ibegin=data_ibegin, data_ni=data_ni, data_jbegin=data_jbegin, data_nj=data_nj ,   &
1806            &    lonvalue_1D=lonvalue, latvalue_1D=latvalue, mask_1D=mask, nvertex=nvertex, bounds_lon_1D=bounds_lon,                  &
1807            &    bounds_lat_1D=bounds_lat, area=area, type='curvilinear' )
1808      ENDIF
1809#endif
1810      CALL xios_solve_inheritance()
1811
1812   END SUBROUTINE iom_set_domain_attr
1813
1814#if defined key_xios2
1815  SUBROUTINE iom_set_zoom_domain_attr( cdid, ibegin, jbegin, ni, nj)
1816     CHARACTER(LEN=*)                   , INTENT(in) ::   cdid
1817     INTEGER                  , OPTIONAL, INTENT(in) ::   ibegin, jbegin, ni, nj
1818
1819     IF ( xios_is_valid_zoom_domain     (cdid) ) THEN
1820         CALL xios_set_zoom_domain_attr     ( cdid, ibegin=ibegin, jbegin=jbegin, ni=ni,    &
1821           &   nj=nj)
1822    ENDIF
1823  END SUBROUTINE iom_set_zoom_domain_attr
1824#endif
1825
1826
1827   SUBROUTINE iom_set_axis_attr( cdid, paxis, bounds )
1828      CHARACTER(LEN=*)      , INTENT(in) ::   cdid
1829      REAL(wp), DIMENSION(:)  , OPTIONAL, INTENT(in) ::   paxis
1830      REAL(wp), DIMENSION(:,:), OPTIONAL, INTENT(in) ::   bounds
1831      IF ( PRESENT(paxis) ) THEN
1832#if ! defined key_xios2
1833         IF ( xios_is_valid_axis     (cdid) )   CALL xios_set_axis_attr     ( cdid, size=SIZE(paxis), value=paxis )
1834         IF ( xios_is_valid_axisgroup(cdid) )   CALL xios_set_axisgroup_attr( cdid, size=SIZE(paxis), value=paxis )
1835#else
1836         IF ( xios_is_valid_axis     (cdid) )   CALL xios_set_axis_attr     ( cdid, n_glo=SIZE(paxis), value=paxis )
1837         IF ( xios_is_valid_axisgroup(cdid) )   CALL xios_set_axisgroup_attr( cdid, n_glo=SIZE(paxis), value=paxis )
1838#endif
1839      ENDIF
1840      IF ( xios_is_valid_axis     (cdid) )   CALL xios_set_axis_attr     ( cdid, bounds=bounds )
1841      IF ( xios_is_valid_axisgroup(cdid) )   CALL xios_set_axisgroup_attr( cdid, bounds=bounds )
1842      CALL xios_solve_inheritance()
1843   END SUBROUTINE iom_set_axis_attr
1844
1845   SUBROUTINE iom_set_field_attr( cdid, freq_op, freq_offset )
1846      CHARACTER(LEN=*)          , INTENT(in) ::   cdid
1847#if ! defined key_xios2
1848      CHARACTER(LEN=*)   ,OPTIONAL , INTENT(in) ::   freq_op
1849      CHARACTER(LEN=*)   ,OPTIONAL , INTENT(in) ::   freq_offset
1850#else
1851      TYPE(xios_duration),OPTIONAL , INTENT(in) ::   freq_op
1852      TYPE(xios_duration),OPTIONAL , INTENT(in) ::   freq_offset
1853#endif
1854      IF ( xios_is_valid_field     (cdid) )   CALL xios_set_field_attr       &
1855    &     ( cdid, freq_op=freq_op, freq_offset=freq_offset )
1856      IF ( xios_is_valid_fieldgroup(cdid) )   CALL xios_set_fieldgroup_attr  &
1857    &                    ( cdid, freq_op=freq_op, freq_offset=freq_offset )
1858      CALL xios_solve_inheritance()
1859   END SUBROUTINE iom_set_field_attr
1860
1861   SUBROUTINE iom_set_file_attr( cdid, name, name_suffix )
1862      CHARACTER(LEN=*)          , INTENT(in) ::   cdid
1863      CHARACTER(LEN=*),OPTIONAL , INTENT(in) ::   name, name_suffix
1864      IF ( xios_is_valid_file     (cdid) )   CALL xios_set_file_attr     ( cdid, name=name, name_suffix=name_suffix )
1865      IF ( xios_is_valid_filegroup(cdid) )   CALL xios_set_filegroup_attr( cdid, name=name, name_suffix=name_suffix )
1866      CALL xios_solve_inheritance()
1867   END SUBROUTINE iom_set_file_attr
1868
1869
1870   SUBROUTINE iom_get_file_attr( cdid, name, name_suffix, output_freq )
1871      CHARACTER(LEN=*)          , INTENT(in ) ::   cdid
1872      CHARACTER(LEN=*),OPTIONAL , INTENT(out) ::   name, name_suffix
1873#if ! defined key_xios2
1874      CHARACTER(LEN=*),OPTIONAL , INTENT(out) ::    output_freq
1875#else
1876      TYPE(xios_duration)   ,OPTIONAL , INTENT(out) :: output_freq
1877#endif 
1878      LOGICAL                                 ::   llexist1,llexist2,llexist3
1879      !---------------------------------------------------------------------
1880      IF( PRESENT( name        ) )   name = ''          ! default values
1881      IF( PRESENT( name_suffix ) )   name_suffix = ''
1882#if ! defined key_xios2
1883      IF( PRESENT( output_freq ) )   output_freq = ''
1884#else
1885      IF( PRESENT( output_freq ) )   output_freq = xios_duration(0,0,0,0,0,0)
1886#endif
1887      IF ( xios_is_valid_file     (cdid) ) THEN
1888         CALL xios_solve_inheritance()
1889         CALL xios_is_defined_file_attr     ( cdid, name = llexist1, name_suffix = llexist2, output_freq = llexist3)
1890         IF(llexist1)   CALL xios_get_file_attr     ( cdid, name = name )
1891         IF(llexist2)   CALL xios_get_file_attr     ( cdid, name_suffix = name_suffix )
1892         IF(llexist3)   CALL xios_get_file_attr     ( cdid, output_freq = output_freq )
1893      ENDIF
1894      IF ( xios_is_valid_filegroup(cdid) ) THEN
1895         CALL xios_solve_inheritance()
1896         CALL xios_is_defined_filegroup_attr( cdid, name = llexist1, name_suffix = llexist2, output_freq = llexist3)
1897         IF(llexist1)   CALL xios_get_filegroup_attr( cdid, name = name )
1898         IF(llexist2)   CALL xios_get_filegroup_attr( cdid, name_suffix = name_suffix )
1899         IF(llexist3)   CALL xios_get_filegroup_attr( cdid, output_freq = output_freq )
1900      ENDIF
1901   END SUBROUTINE iom_get_file_attr
1902
1903
1904   SUBROUTINE iom_set_grid_attr( cdid, mask )
1905      CHARACTER(LEN=*)                   , INTENT(in) ::   cdid
1906      LOGICAL, DIMENSION(:,:,:), OPTIONAL, INTENT(in) ::   mask
1907#if ! defined key_xios2
1908      IF ( xios_is_valid_grid     (cdid) )   CALL xios_set_grid_attr     ( cdid, mask=mask )
1909      IF ( xios_is_valid_gridgroup(cdid) )   CALL xios_set_gridgroup_attr( cdid, mask=mask )
1910#else
1911      IF ( xios_is_valid_grid     (cdid) )   CALL xios_set_grid_attr     ( cdid, mask_3D=mask )
1912      IF ( xios_is_valid_gridgroup(cdid) )   CALL xios_set_gridgroup_attr( cdid, mask_3D=mask )
1913#endif
1914      CALL xios_solve_inheritance()
1915   END SUBROUTINE iom_set_grid_attr
1916
1917   SUBROUTINE iom_setkt( kt, cdname )
1918      INTEGER         , INTENT(in) ::   kt 
1919      CHARACTER(LEN=*), INTENT(in) ::   cdname
1920      !     
1921      CALL iom_swap( cdname )   ! swap to cdname context
1922      CALL xios_update_calendar(kt)
1923      IF( cdname /= TRIM(cxios_context) ) CALL iom_swap( TRIM(cxios_context) )   ! return back to nemo context
1924      !
1925   END SUBROUTINE iom_setkt
1926
1927   SUBROUTINE iom_context_finalize( cdname )
1928      CHARACTER(LEN=*), INTENT(in) :: cdname
1929      !
1930      IF( xios_is_valid_context(cdname) ) 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.