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

Since March 2022 along with NEMO 4.2 release, the code development moved to a self-hosted GitLab.
This present forge is now archived and remained online for history.
iom.F90 in NEMO/branches/UKMO/NEMO_4.0.4_CO9_shelf_climate/src/OCE/IOM – NEMO

source: NEMO/branches/UKMO/NEMO_4.0.4_CO9_shelf_climate/src/OCE/IOM/iom.F90 @ 15402

Last change on this file since 15402 was 15384, checked in by hadjt, 3 years ago

Kara MLD code removed, as redundant

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