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/2019/dev_r12072_MERGE_OPTION2_2019/src/OCE/IOM – NEMO

source: NEMO/branches/2019/dev_r12072_MERGE_OPTION2_2019/src/OCE/IOM/iom.F90 @ 12184

Last change on this file since 12184 was 12184, checked in by smueller, 4 years ago

Merging of the developments in /NEMO/branches/2019/dev_r11879_ENHANCE-05_SimonM-Harmonic_Analysis@12122 with respect to /NEMO/trunk@12072 into /NEMO/branches/2019/dev_r12072_MERGE_OPTION2_2019 (tickets #2175 and #2194)

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