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

source: NEMO/branches/2020/dev_12905_xios_restart/src/OCE/IOM/iom.F90 @ 12961

Last change on this file since 12961 was 12961, checked in by andmirek, 4 years ago

Ticket #2462: read/write restart with XIOS in TOP (with debug print statements)

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