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/2020/dev_r12558_HPC-08_epico_Extra_Halo/src/OCE/IOM – NEMO

source: NEMO/branches/2020/dev_r12558_HPC-08_epico_Extra_Halo/src/OCE/IOM/iom.F90 @ 12738

Last change on this file since 12738 was 12738, checked in by smasson, 4 years ago

Extra_Halo: iom cleaning/update to work only with unknown, global or local (without halos) domains, see #2366

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