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

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

#1882 New way of defining what is in restart file

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