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

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

Ticket #2462 read restart with XIOS in SAS

  • 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
[4152]620      IF( TRIM(Agrif_CFixed()) == '0' ) THEN
621        CALL xios_get_handle(TRIM(cdname),nemo_hdl)
622      ELSE
623        CALL xios_get_handle(TRIM(Agrif_CFixed())//"_"//TRIM(cdname),nemo_hdl)
624      ENDIF
625      !
626      CALL xios_set_current_context(nemo_hdl)
[1793]627#endif
[4152]628      !
[1793]629   END SUBROUTINE iom_swap
630
631
[12649]632   SUBROUTINE iom_open( cdname, kiomid, ldwrt, kdom, ldstop, ldiof, kdlev, cdcomp )
[544]633      !!---------------------------------------------------------------------
634      !!                   ***  SUBROUTINE  iom_open  ***
635      !!
636      !! ** Purpose :  open an input file (return 0 if not found)
637      !!---------------------------------------------------------------------
638      CHARACTER(len=*), INTENT(in   )           ::   cdname   ! File name
639      INTEGER         , INTENT(  out)           ::   kiomid   ! iom identifier of the opened file
640      LOGICAL         , INTENT(in   ), OPTIONAL ::   ldwrt    ! open in write modeb          (default = .FALSE.)
641      INTEGER         , INTENT(in   ), OPTIONAL ::   kdom     ! Type of domain to be written (default = jpdom_local_noovlap)
[679]642      LOGICAL         , INTENT(in   ), OPTIONAL ::   ldstop   ! stop if open to read a non-existing file (default = .TRUE.)
[1319]643      LOGICAL         , INTENT(in   ), OPTIONAL ::   ldiof    ! Interp On the Fly, needed for AGRIF (default = .FALSE.)
[9019]644      INTEGER         , INTENT(in   ), OPTIONAL ::   kdlev    ! number of vertical levels
[12649]645      CHARACTER(len=3), INTENT(in   ), OPTIONAL ::   cdcomp   ! name of component calling iom_nf90_open
[9019]646      !
[4148]647      CHARACTER(LEN=256)    ::   clname    ! the name of the file based on cdname [[+clcpu]+clcpu]
648      CHARACTER(LEN=256)    ::   cltmpn    ! tempory name to store clname (in writting mode)
[6140]649      CHARACTER(LEN=10)     ::   clsuffix  ! ".nc"
[550]650      CHARACTER(LEN=15)     ::   clcpu     ! the cpu number (max jpmax_digits digits)
[4148]651      CHARACTER(LEN=256)    ::   clinfo    ! info character
[544]652      LOGICAL               ::   llok      ! check the existence
[679]653      LOGICAL               ::   llwrt     ! local definition of ldwrt
[1200]654      LOGICAL               ::   llnoov    ! local definition to read overlap
[679]655      LOGICAL               ::   llstop    ! local definition of ldstop
[1319]656      LOGICAL               ::   lliof     ! local definition of ldiof
[544]657      INTEGER               ::   icnt      ! counter for digits in clcpu (max = jpmax_digits)
658      INTEGER               ::   iln, ils  ! lengths of character
659      INTEGER               ::   idom      ! type of domain
660      INTEGER               ::   istop     !
661      INTEGER, DIMENSION(2,5) ::   idompar ! domain parameters:
662      ! local number of points for x,y dimensions
663      ! position of first local point for x,y dimensions
664      ! position of last local point for x,y dimensions
665      ! start halo size for x,y dimensions
666      ! end halo size for x,y dimensions
667      !---------------------------------------------------------------------
668      ! Initializations and control
669      ! =============
[1341]670      kiomid = -1
[544]671      clinfo = '                    iom_open ~~~  '
672      istop = nstop
673      ! if iom_open is called for the first time: initialize iom_file(:)%nfid to 0
674      ! (could be done when defining iom_file in f95 but not in f90)
[1441]675      IF( Agrif_Root() ) THEN
676         IF( iom_open_init == 0 ) THEN
677            iom_file(:)%nfid = 0
678            iom_open_init = 1
679         ENDIF
[1409]680      ENDIF
[544]681      ! do we read or write the file?
682      IF( PRESENT(ldwrt) ) THEN   ;   llwrt = ldwrt
683      ELSE                        ;   llwrt = .FALSE.
684      ENDIF
[679]685      ! do we call ctl_stop if we try to open a non-existing file in read mode?
686      IF( PRESENT(ldstop) ) THEN   ;   llstop = ldstop
687      ELSE                         ;   llstop = .TRUE.
688      ENDIF
[1319]689      ! are we using interpolation on the fly?
690      IF( PRESENT(ldiof) ) THEN   ;   lliof = ldiof
691      ELSE                        ;   lliof = .FALSE.
692      ENDIF
[1200]693      ! do we read the overlap
694      ! ugly patch SM+JMM+RB to overwrite global definition in some cases
[10016]695      llnoov = (jpni * jpnj ) == jpnij .AND. .NOT. lk_agrif
[544]696      ! create the file name by added, if needed, TRIM(Agrif_CFixed()) and TRIM(clsuffix)
697      ! =============
698      clname   = trim(cdname)
[1319]699      IF ( .NOT. Agrif_Root() .AND. .NOT. lliof ) THEN
[12377]700         iln    = INDEX(clname,'/') 
[1200]701         cltmpn = clname(1:iln)
702         clname = clname(iln+1:LEN_TRIM(clname))
703         clname=TRIM(cltmpn)//TRIM(Agrif_CFixed())//'_'//TRIM(clname)
704      ENDIF
[544]705      ! which suffix should we use?
[10425]706      clsuffix = '.nc'
[544]707      ! Add the suffix if needed
708      iln = LEN_TRIM(clname)
709      ils = LEN_TRIM(clsuffix)
[742]710      IF( iln <= ils .OR. INDEX( TRIM(clname), TRIM(clsuffix), back = .TRUE. ) /= iln - ils + 1 )   &
711         &   clname = TRIM(clname)//TRIM(clsuffix)
[544]712      cltmpn = clname   ! store this name
713      ! try to find if the file to be opened already exist
714      ! =============
715      INQUIRE( FILE = clname, EXIST = llok )
716      IF( .NOT.llok ) THEN
717         ! we try to add the cpu number to the name
[6140]718         WRITE(clcpu,*) narea-1
719
[544]720         clcpu  = TRIM(ADJUSTL(clcpu))
[679]721         iln = INDEX(clname,TRIM(clsuffix), back = .TRUE.)
[544]722         clname = clname(1:iln-1)//'_'//TRIM(clcpu)//TRIM(clsuffix)
723         icnt = 0
724         INQUIRE( FILE = clname, EXIST = llok ) 
725         ! we try different formats for the cpu number by adding 0
726         DO WHILE( .NOT.llok .AND. icnt < jpmax_digits )
727            clcpu  = "0"//trim(clcpu)
728            clname = clname(1:iln-1)//'_'//TRIM(clcpu)//TRIM(clsuffix)
729            INQUIRE( FILE = clname, EXIST = llok )
730            icnt = icnt + 1
731         END DO
[9367]732      ELSE
733         lxios_sini = .TRUE.
[544]734      ENDIF
[679]735      IF( llwrt ) THEN
736         ! check the domain definition
737! JMM + SM: ugly patch before getting the new version of lib_mpp)
738!         idom = jpdom_local_noovlap   ! default definition
[1200]739         IF( llnoov ) THEN   ;   idom = jpdom_local_noovlap   ! default definition
740         ELSE                ;   idom = jpdom_local_full      ! default definition
[679]741         ENDIF
742         IF( PRESENT(kdom) )   idom = kdom
743         ! create the domain informations
744         ! =============
745         SELECT CASE (idom)
746         CASE (jpdom_local_full)
747            idompar(:,1) = (/ jpi             , jpj              /)
748            idompar(:,2) = (/ nimpp           , njmpp            /)
749            idompar(:,3) = (/ nimpp + jpi - 1 , njmpp + jpj - 1  /)
750            idompar(:,4) = (/ nldi - 1        , nldj - 1         /)
751            idompar(:,5) = (/ jpi - nlei      , jpj - nlej       /)
752         CASE (jpdom_local_noextra)
753            idompar(:,1) = (/ nlci            , nlcj             /)
754            idompar(:,2) = (/ nimpp           , njmpp            /)
755            idompar(:,3) = (/ nimpp + nlci - 1, njmpp + nlcj - 1 /)
756            idompar(:,4) = (/ nldi - 1        , nldj - 1         /)
757            idompar(:,5) = (/ nlci - nlei     , nlcj - nlej      /)
758         CASE (jpdom_local_noovlap)
759            idompar(:,1) = (/ nlei  - nldi + 1, nlej  - nldj + 1 /)
760            idompar(:,2) = (/ nimpp + nldi - 1, njmpp + nldj - 1 /)
761            idompar(:,3) = (/ nimpp + nlei - 1, njmpp + nlej - 1 /)
762            idompar(:,4) = (/ 0               , 0                /)
763            idompar(:,5) = (/ 0               , 0                /)
764         CASE DEFAULT
765            CALL ctl_stop( TRIM(clinfo), 'wrong value of kdom, only jpdom_local* cases are accepted' )
766         END SELECT
767      ENDIF
[6140]768      ! Open the NetCDF file
[544]769      ! =============
770      ! do we have some free file identifier?
771      IF( MINVAL(iom_file(:)%nfid) /= 0 )   &
[679]772         &   CALL ctl_stop( TRIM(clinfo), 'No more free file identifier', 'increase jpmax_files in iom_def' )
773      ! if no file was found...
774      IF( .NOT. llok ) THEN
775         IF( .NOT. llwrt ) THEN   ! we are in read mode
776            IF( llstop ) THEN   ;   CALL ctl_stop( TRIM(clinfo), 'File '//TRIM(cltmpn)//'* not found' )
777            ELSE                ;   istop = nstop + 1   ! make sure that istop /= nstop so we don't open the file
778            ENDIF
779         ELSE                     ! we are in write mode so we
780            clname = cltmpn       ! get back the file name without the cpu number
781         ENDIF
[2586]782      ELSE
783         IF( llwrt .AND. .NOT. ln_clobber ) THEN   ! we stop as we want to write in a new file
784            CALL ctl_stop( TRIM(clinfo), 'We want to write in a new file but '//TRIM(clname)//' already exists...' )
785            istop = nstop + 1                      ! make sure that istop /= nstop so we don't open the file
[4650]786         ELSEIF( llwrt ) THEN     ! the file exists and we are in write mode with permission to
787            clname = cltmpn       ! overwrite so get back the file name without the cpu number
[2586]788         ENDIF
[679]789      ENDIF
[544]790      IF( istop == nstop ) THEN   ! no error within this routine
[12649]791         CALL iom_nf90_open( clname, kiomid, llwrt, llok, idompar, kdlev = kdlev, cdcomp = cdcomp )
[544]792      ENDIF
793      !
794   END SUBROUTINE iom_open
795
796
797   SUBROUTINE iom_close( kiomid )
798      !!--------------------------------------------------------------------
799      !!                   ***  SUBROUTINE  iom_close  ***
800      !!
801      !! ** Purpose : close an input file, or all files opened by iom
802      !!--------------------------------------------------------------------
[1131]803      INTEGER, INTENT(inout), OPTIONAL ::   kiomid   ! iom identifier of the file to be closed
804      !                                              ! return 0 when file is properly closed
805      !                                              ! No argument: all files opened by iom are closed
[544]806
807      INTEGER ::   jf         ! dummy loop indices
808      INTEGER ::   i_s, i_e   ! temporary integer
809      CHARACTER(LEN=100)    ::   clinfo    ! info character
810      !---------------------------------------------------------------------
811      !
[12283]812      IF( iom_open_init == 0 )   RETURN   ! avoid to use iom_file(jf)%nfid that us not yet initialized
813      !
[544]814      clinfo = '                    iom_close ~~~  '
815      IF( PRESENT(kiomid) ) THEN
816         i_s = kiomid
817         i_e = kiomid
818      ELSE
819         i_s = 1
820         i_e = jpmax_files
821      ENDIF
822
823      IF( i_s > 0 ) THEN
824         DO jf = i_s, i_e
825            IF( iom_file(jf)%nfid > 0 ) THEN
[10425]826               CALL iom_nf90_close( jf )
[1131]827               iom_file(jf)%nfid       = 0          ! free the id
828               IF( PRESENT(kiomid) )   kiomid = 0   ! return 0 as id to specify that the file was closed
[679]829               IF(lwp) WRITE(numout,*) TRIM(clinfo)//' close file: '//TRIM(iom_file(jf)%name)//' ok'
[544]830            ELSEIF( PRESENT(kiomid) ) THEN
831               WRITE(ctmp1,*) '--->',  kiomid
832               CALL ctl_stop( TRIM(clinfo)//' Invalid file identifier', ctmp1 )
833            ENDIF
834         END DO
835      ENDIF
836      !   
837   END SUBROUTINE iom_close
838
839
[11536]840   FUNCTION iom_varid ( kiomid, cdvar, kdimsz, kndims, lduld, ldstop ) 
[544]841      !!-----------------------------------------------------------------------
842      !!                  ***  FUNCTION  iom_varid  ***
843      !!
844      !! ** Purpose : get the id of a variable in a file (return 0 if not found)
845      !!-----------------------------------------------------------------------
846      INTEGER              , INTENT(in   )           ::   kiomid   ! file Identifier
847      CHARACTER(len=*)     , INTENT(in   )           ::   cdvar    ! name of the variable
[9540]848      INTEGER, DIMENSION(:), INTENT(  out), OPTIONAL ::   kdimsz   ! size of each dimension
[11536]849      INTEGER              , INTENT(  out), OPTIONAL ::   kndims   ! number of dimensions
850      LOGICAL              , INTENT(  out), OPTIONAL ::   lduld    ! true if the last dimension is unlimited (time)
[745]851      LOGICAL              , INTENT(in   ), OPTIONAL ::   ldstop   ! stop if looking for non-existing variable (default = .TRUE.)
[544]852      !
853      INTEGER                        ::   iom_varid, iiv, i_nvd
854      LOGICAL                        ::   ll_fnd
855      CHARACTER(LEN=100)             ::   clinfo                   ! info character
[745]856      LOGICAL                        ::   llstop                   ! local definition of ldstop
[544]857      !!-----------------------------------------------------------------------
858      iom_varid = 0                         ! default definition
[745]859      ! do we call ctl_stop if we look for non-existing variable?
860      IF( PRESENT(ldstop) ) THEN   ;   llstop = ldstop
861      ELSE                         ;   llstop = .TRUE.
862      ENDIF
[544]863      !
864      IF( kiomid > 0 ) THEN
[679]865         clinfo = 'iom_varid, file: '//trim(iom_file(kiomid)%name)//', var: '//trim(cdvar)
[544]866         IF( iom_file(kiomid)%nfid == 0 ) THEN
867            CALL ctl_stop( trim(clinfo), 'the file is not open' )
868         ELSE
869            ll_fnd  = .FALSE.
870            iiv = 0
871            !
872            DO WHILE ( .NOT.ll_fnd .AND. iiv < iom_file(kiomid)%nvars )
873               iiv = iiv + 1
874               ll_fnd  = ( TRIM(cdvar) == TRIM(iom_file(kiomid)%cn_var(iiv)) )
875            END DO
876            !
877            IF( .NOT.ll_fnd ) THEN
878               iiv = iiv + 1
879               IF( iiv <= jpmax_vars ) THEN
[11536]880                  iom_varid = iom_nf90_varid( kiomid, cdvar, iiv, kdimsz, kndims, lduld )
[544]881               ELSE
882                  CALL ctl_stop( trim(clinfo), 'Too many variables in the file '//iom_file(kiomid)%name,   &
[10425]883                        &                      'increase the parameter jpmax_vars')
[544]884               ENDIF
[745]885               IF( llstop .AND. iom_varid == -1 )   CALL ctl_stop( TRIM(clinfo)//' not found' ) 
[544]886            ELSE
887               iom_varid = iiv
888               IF( PRESENT(kdimsz) ) THEN
889                  i_nvd = iom_file(kiomid)%ndims(iiv)
[9540]890                  IF( i_nvd <= size(kdimsz) ) THEN
891                     kdimsz(1:i_nvd) = iom_file(kiomid)%dimsz(1:i_nvd,iiv)
[544]892                  ELSE
893                     WRITE(ctmp1,*) i_nvd, size(kdimsz)
894                     CALL ctl_stop( trim(clinfo), 'error in kdimsz size'//trim(ctmp1) )
895                  ENDIF
896               ENDIF
[4205]897               IF( PRESENT(kndims) )  kndims = iom_file(kiomid)%ndims(iiv)
[11536]898               IF( PRESENT( lduld) )  lduld  = iom_file(kiomid)%luld( iiv)
[544]899            ENDIF
900         ENDIF
901      ENDIF
902      !
903   END FUNCTION iom_varid
904
905
906   !!----------------------------------------------------------------------
907   !!                   INTERFACE iom_get
908   !!----------------------------------------------------------------------
[9367]909   SUBROUTINE iom_g0d( kiomid, cdvar, pvar, ktime, ldxios )
[544]910      INTEGER         , INTENT(in   )                 ::   kiomid    ! Identifier of the file
911      CHARACTER(len=*), INTENT(in   )                 ::   cdvar     ! Name of the variable
912      REAL(wp)        , INTENT(  out)                 ::   pvar      ! read field
[4245]913      INTEGER         , INTENT(in   ),     OPTIONAL   ::   ktime     ! record number
[9367]914      LOGICAL         , INTENT(in   ),     OPTIONAL   ::   ldxios    ! use xios to read restart
[544]915      !
[4245]916      INTEGER                                         ::   idvar     ! variable id
917      INTEGER                                         ::   idmspc    ! number of spatial dimensions
918      INTEGER         , DIMENSION(1)                  ::   itime     ! record number
919      CHARACTER(LEN=100)                              ::   clinfo    ! info character
920      CHARACTER(LEN=100)                              ::   clname    ! file name
921      CHARACTER(LEN=1)                                ::   cldmspc   !
[9367]922      LOGICAL                                         ::   llxios
[544]923      !
[9367]924      llxios = .FALSE.
925      IF( PRESENT(ldxios) ) llxios = ldxios
926
927      IF(.NOT.llxios) THEN  ! read data using default library
928         itime = 1
929         IF( PRESENT(ktime) ) itime = ktime
930         !
931         clname = iom_file(kiomid)%name
932         clinfo = '          iom_g0d, file: '//trim(clname)//', var: '//trim(cdvar)
933         !
934         IF( kiomid > 0 ) THEN
935            idvar = iom_varid( kiomid, cdvar )
936            IF( iom_file(kiomid)%nfid > 0 .AND. idvar > 0 ) THEN
937               idmspc = iom_file ( kiomid )%ndims( idvar )
938               IF( iom_file(kiomid)%luld(idvar) )  idmspc = idmspc - 1
939               WRITE(cldmspc , fmt='(i1)') idmspc
940               IF( idmspc > 0 )  CALL ctl_stop( TRIM(clinfo), 'When reading to a 0D array, we do not accept data', &
941                                    &                         'with 1 or more spatial dimensions: '//cldmspc//' were found.' , &
942                                    &                         'Use ncwa -a to suppress the unnecessary dimensions' )
[10425]943               CALL iom_nf90_get( kiomid, idvar, pvar, itime )
[9367]944            ENDIF
[679]945         ENDIF
[9367]946      ELSE
[9535]947#if defined key_iomput
[9367]948         IF(lwp) WRITE(numout,*) 'XIOS RST READ (0D): ', trim(cdvar)
949         CALL xios_recv_field( trim(cdvar), pvar)
[9535]950#else
[10425]951         WRITE(ctmp1,*) 'Can not use XIOS in iom_g0d, file: '//trim(clname)//', var:'//trim(cdvar)
952         CALL ctl_stop( 'iom_g0d', ctmp1 )
[9535]953#endif
[679]954      ENDIF
[550]955   END SUBROUTINE iom_g0d
[544]956
[9367]957   SUBROUTINE iom_g1d( kiomid, kdom, cdvar, pvar, ktime, kstart, kcount, ldxios )
[544]958      INTEGER         , INTENT(in   )                         ::   kiomid    ! Identifier of the file
959      INTEGER         , INTENT(in   )                         ::   kdom      ! Type of domain to be read
960      CHARACTER(len=*), INTENT(in   )                         ::   cdvar     ! Name of the variable
961      REAL(wp)        , INTENT(  out), DIMENSION(:)           ::   pvar      ! read field
962      INTEGER         , INTENT(in   )              , OPTIONAL ::   ktime     ! record number
963      INTEGER         , INTENT(in   ), DIMENSION(1), OPTIONAL ::   kstart    ! start axis position of the reading
964      INTEGER         , INTENT(in   ), DIMENSION(1), OPTIONAL ::   kcount    ! number of points in each axis
[9367]965      LOGICAL         , INTENT(in   ),               OPTIONAL ::   ldxios    ! read data using XIOS
[544]966      !
[679]967      IF( kiomid > 0 ) THEN
968         IF( iom_file(kiomid)%nfid > 0 ) CALL iom_get_123d( kiomid, kdom       , cdvar        , pv_r1d=pvar,   &
[9367]969              &                                                     ktime=ktime, kstart=kstart, kcount=kcount, &
970              &                                                     ldxios=ldxios )
[679]971      ENDIF
[550]972   END SUBROUTINE iom_g1d
[544]973
[9367]974   SUBROUTINE iom_g2d( kiomid, kdom, cdvar, pvar, ktime, kstart, kcount, lrowattr, ldxios)
[544]975      INTEGER         , INTENT(in   )                           ::   kiomid    ! Identifier of the file
976      INTEGER         , INTENT(in   )                           ::   kdom      ! Type of domain to be read
977      CHARACTER(len=*), INTENT(in   )                           ::   cdvar     ! Name of the variable
978      REAL(wp)        , INTENT(  out), DIMENSION(:,:)           ::   pvar      ! read field
979      INTEGER         , INTENT(in   )                , OPTIONAL ::   ktime     ! record number
980      INTEGER         , INTENT(in   ), DIMENSION(2)  , OPTIONAL ::   kstart    ! start axis position of the reading
981      INTEGER         , INTENT(in   ), DIMENSION(2)  , OPTIONAL ::   kcount    ! number of points in each axis
[5118]982      LOGICAL         , INTENT(in   )                , OPTIONAL ::   lrowattr  ! logical flag telling iom_get to
983                                                                               ! look for and use a file attribute
984                                                                               ! called open_ocean_jstart to set the start
985                                                                               ! value for the 2nd dimension (netcdf only)
[9367]986      LOGICAL         , INTENT(in   ),               OPTIONAL ::   ldxios      ! read data using XIOS
[544]987      !
[679]988      IF( kiomid > 0 ) THEN
989         IF( iom_file(kiomid)%nfid > 0 ) CALL iom_get_123d( kiomid, kdom       , cdvar        , pv_r2d=pvar,   &
[5118]990              &                                                     ktime=ktime, kstart=kstart, kcount=kcount, &
[9367]991              &                                                     lrowattr=lrowattr,  ldxios=ldxios)
[679]992      ENDIF
[550]993   END SUBROUTINE iom_g2d
[544]994
[9367]995   SUBROUTINE iom_g3d( kiomid, kdom, cdvar, pvar, ktime, kstart, kcount, lrowattr, ldxios )
[544]996      INTEGER         , INTENT(in   )                             ::   kiomid    ! Identifier of the file
997      INTEGER         , INTENT(in   )                             ::   kdom      ! Type of domain to be read
998      CHARACTER(len=*), INTENT(in   )                             ::   cdvar     ! Name of the variable
999      REAL(wp)        , INTENT(  out), DIMENSION(:,:,:)           ::   pvar      ! read field
1000      INTEGER         , INTENT(in   )                  , OPTIONAL ::   ktime     ! record number
1001      INTEGER         , INTENT(in   ), DIMENSION(3)    , OPTIONAL ::   kstart    ! start axis position of the reading
1002      INTEGER         , INTENT(in   ), DIMENSION(3)    , OPTIONAL ::   kcount    ! number of points in each axis
[5118]1003      LOGICAL         , INTENT(in   )                  , OPTIONAL ::   lrowattr  ! logical flag telling iom_get to
1004                                                                                 ! look for and use a file attribute
1005                                                                                 ! called open_ocean_jstart to set the start
1006                                                                                 ! value for the 2nd dimension (netcdf only)
[9367]1007      LOGICAL         , INTENT(in   ),               OPTIONAL ::   ldxios        ! read data using XIOS
[544]1008      !
[679]1009      IF( kiomid > 0 ) THEN
1010         IF( iom_file(kiomid)%nfid > 0 ) CALL iom_get_123d( kiomid, kdom       , cdvar        , pv_r3d=pvar,   &
[5118]1011              &                                                     ktime=ktime, kstart=kstart, kcount=kcount, &
[9367]1012              &                                                     lrowattr=lrowattr, ldxios=ldxios )
[679]1013      ENDIF
[550]1014   END SUBROUTINE iom_g3d
[544]1015   !!----------------------------------------------------------------------
1016
1017   SUBROUTINE iom_get_123d( kiomid, kdom  , cdvar ,   &
[679]1018         &                  pv_r1d, pv_r2d, pv_r3d,   &
[5118]1019         &                  ktime , kstart, kcount,   &
[9367]1020         &                  lrowattr, ldxios        )
[544]1021      !!-----------------------------------------------------------------------
1022      !!                  ***  ROUTINE  iom_get_123d  ***
1023      !!
1024      !! ** Purpose : read a 1D/2D/3D variable
1025      !!
1026      !! ** Method : read ONE record at each CALL
1027      !!-----------------------------------------------------------------------
1028      INTEGER                    , INTENT(in   )           ::   kiomid     ! Identifier of the file
1029      INTEGER                    , INTENT(in   )           ::   kdom       ! Type of domain to be read
1030      CHARACTER(len=*)           , INTENT(in   )           ::   cdvar      ! Name of the variable
1031      REAL(wp), DIMENSION(:)     , INTENT(  out), OPTIONAL ::   pv_r1d     ! read field (1D case)
1032      REAL(wp), DIMENSION(:,:)   , INTENT(  out), OPTIONAL ::   pv_r2d     ! read field (2D case)
1033      REAL(wp), DIMENSION(:,:,:) , INTENT(  out), OPTIONAL ::   pv_r3d     ! read field (3D case)
1034      INTEGER                    , INTENT(in   ), OPTIONAL ::   ktime      ! record number
1035      INTEGER , DIMENSION(:)     , INTENT(in   ), OPTIONAL ::   kstart     ! start position of the reading in each axis
1036      INTEGER , DIMENSION(:)     , INTENT(in   ), OPTIONAL ::   kcount     ! number of points to be read in each axis
[5118]1037      LOGICAL                    , INTENT(in   ), OPTIONAL ::   lrowattr   ! logical flag telling iom_get to
[9367]1038                                                                           ! look for and use a file attribute
1039                                                                           ! called open_ocean_jstart to set the start
1040                                                                           ! value for the 2nd dimension (netcdf only)
1041      LOGICAL                    , INTENT(in   ), OPTIONAL ::   ldxios     ! use XIOS to read restart
[544]1042      !
[9367]1043      LOGICAL                        ::   llxios       ! local definition for XIOS read
[1200]1044      LOGICAL                        ::   llnoov      ! local definition to read overlap
[5118]1045      LOGICAL                        ::   luse_jattr  ! local definition to read open_ocean_jstart file attribute
1046      INTEGER                        ::   jstartrow   ! start point for 2nd dimension optionally set by file attribute
[544]1047      INTEGER                        ::   jl          ! loop on number of dimension
1048      INTEGER                        ::   idom        ! type of domain
1049      INTEGER                        ::   idvar       ! id of the variable
1050      INTEGER                        ::   inbdim      ! number of dimensions of the variable
1051      INTEGER                        ::   idmspc      ! number of spatial dimensions
1052      INTEGER                        ::   itime       ! record number
1053      INTEGER                        ::   istop       ! temporary value of nstop
[679]1054      INTEGER                        ::   ix1, ix2, iy1, iy2   ! subdomain indexes
1055      INTEGER                        ::   ji, jj      ! loop counters
[5118]1056      INTEGER                        ::   irankpv     !
[679]1057      INTEGER                        ::   ind1, ind2  ! substring index
[544]1058      INTEGER, DIMENSION(jpmax_dims) ::   istart      ! starting point to read for each axis
1059      INTEGER, DIMENSION(jpmax_dims) ::   icnt        ! number of value to read along each axis
1060      INTEGER, DIMENSION(jpmax_dims) ::   idimsz      ! size of the dimensions of the variable
[679]1061      INTEGER, DIMENSION(jpmax_dims) ::   ishape      ! size of the dimensions of the variable
[544]1062      REAL(wp)                       ::   zscf, zofs  ! sacle_factor and add_offset
1063      INTEGER                        ::   itmp        ! temporary integer
[4148]1064      CHARACTER(LEN=256)             ::   clinfo      ! info character
1065      CHARACTER(LEN=256)             ::   clname      ! file name
[679]1066      CHARACTER(LEN=1)               ::   clrankpv, cldmspc      !
[6140]1067      LOGICAL                        ::   ll_depth_spec ! T => if kstart, kcount present then *only* use values for 3rd spatial dimension.
[9019]1068      INTEGER                        ::   inlev       ! number of levels for 3D data
[9367]1069      REAL(wp)                       ::   gma, gmi
[544]1070      !---------------------------------------------------------------------
1071      !
[9019]1072      inlev = -1
1073      IF( PRESENT(pv_r3d) )   inlev = SIZE(pv_r3d, 3)
[9367]1074      !
1075      llxios = .FALSE.
1076      if(PRESENT(ldxios)) llxios = ldxios
1077      idvar = iom_varid( kiomid, cdvar ) 
[544]1078      idom = kdom
[9367]1079      !
1080      IF(.NOT.llxios) THEN
1081         clname = iom_file(kiomid)%name   !   esier to read
1082         clinfo = '          iom_get_123d, file: '//trim(clname)//', var: '//trim(cdvar)
1083         ! local definition of the domain ?
1084         ! do we read the overlap
1085         ! ugly patch SM+JMM+RB to overwrite global definition in some cases
1086         llnoov = (jpni * jpnj ) == jpnij .AND. .NOT. lk_agrif 
1087         ! check kcount and kstart optionals parameters...
1088         IF( PRESENT(kcount) .AND. (.NOT. PRESENT(kstart)) ) CALL ctl_stop(trim(clinfo), 'kcount present needs kstart present')
1089         IF( PRESENT(kstart) .AND. (.NOT. PRESENT(kcount)) ) CALL ctl_stop(trim(clinfo), 'kstart present needs kcount present')
1090         IF( PRESENT(kstart) .AND. idom /= jpdom_unknown .AND.  idom /= jpdom_autoglo_xy  ) &
1091     &          CALL ctl_stop(trim(clinfo), 'kstart present needs kdom = jpdom_unknown or kdom = jpdom_autoglo_xy')
[544]1092
[9367]1093         luse_jattr = .false.
1094         IF( PRESENT(lrowattr) ) THEN
1095            IF( lrowattr .AND. idom /= jpdom_data   ) CALL ctl_stop(trim(clinfo), 'lrowattr present and true needs kdom = jpdom_data')
1096            IF( lrowattr .AND. idom == jpdom_data   ) luse_jattr = .true.
[6140]1097         ENDIF
[679]1098
[9367]1099         ! Search for the variable in the data base (eventually actualize data)
1100         istop = nstop
[544]1101         !
[9367]1102         IF( idvar > 0 ) THEN
1103            ! to write iom_file(kiomid)%dimsz in a shorter way !
1104            idimsz(:) = iom_file(kiomid)%dimsz(:, idvar) 
1105            inbdim = iom_file(kiomid)%ndims(idvar)            ! number of dimensions in the file
1106            idmspc = inbdim                                   ! number of spatial dimensions in the file
1107            IF( iom_file(kiomid)%luld(idvar) )   idmspc = inbdim - 1
1108            IF( idmspc > 3 )   CALL ctl_stop(trim(clinfo), 'the file has more than 3 spatial dimensions this case is not coded...') 
1109            !
1110            ! update idom definition...
1111            ! Identify the domain in case of jpdom_auto(glo/dta) definition
1112            IF( idom == jpdom_autoglo_xy ) THEN
1113               ll_depth_spec = .TRUE.
1114               idom = jpdom_autoglo
1115            ELSE
1116               ll_depth_spec = .FALSE.
1117            ENDIF
1118            IF( idom == jpdom_autoglo .OR. idom == jpdom_autodta ) THEN           
1119               IF( idom == jpdom_autoglo ) THEN   ;   idom = jpdom_global 
1120               ELSE                               ;   idom = jpdom_data
[544]1121               ENDIF
[9367]1122               ind1 = INDEX( clname, '_', back = .TRUE. ) + 1
1123               ind2 = INDEX( clname, '.', back = .TRUE. ) - 1
1124               IF( ind2 > ind1 ) THEN   ;   IF( VERIFY( clname(ind1:ind2), '0123456789' ) == 0 )   idom = jpdom_local   ;   ENDIF
1125            ENDIF
1126            ! Identify the domain in case of jpdom_local definition
1127            IF( idom == jpdom_local ) THEN
1128               IF(     idimsz(1) == jpi               .AND. idimsz(2) == jpj               ) THEN   ;   idom = jpdom_local_full
1129               ELSEIF( idimsz(1) == nlci              .AND. idimsz(2) == nlcj              ) THEN   ;   idom = jpdom_local_noextra
1130               ELSEIF( idimsz(1) == (nlei - nldi + 1) .AND. idimsz(2) == (nlej - nldj + 1) ) THEN   ;   idom = jpdom_local_noovlap
1131               ELSE   ;   CALL ctl_stop( trim(clinfo), 'impossible to identify the local domain' )
1132               ENDIF
1133            ENDIF
1134            !
1135            ! check the consistency between input array and data rank in the file
1136            !
1137            ! initializations
1138            itime = 1
1139            IF( PRESENT(ktime) ) itime = ktime
1140            !
1141            irankpv = 1 * COUNT( (/PRESENT(pv_r1d)/) ) + 2 * COUNT( (/PRESENT(pv_r2d)/) ) + 3 * COUNT( (/PRESENT(pv_r3d)/) )
1142            WRITE(clrankpv, fmt='(i1)') irankpv
1143            WRITE(cldmspc , fmt='(i1)') idmspc
1144            !
[12377]1145            !!GS: we consider 2D data as 3D data with vertical dim size = 1
1146            !IF(     idmspc <  irankpv ) THEN
1147            !   CALL ctl_stop( TRIM(clinfo), 'The file has only '//cldmspc//' spatial dimension',   &
1148            !      &                         'it is impossible to read a '//clrankpv//'D array from this file...' )
1149            !ELSEIF( idmspc == irankpv ) THEN
1150            IF( idmspc == irankpv ) THEN
[9367]1151               IF( PRESENT(pv_r1d) .AND. idom /= jpdom_unknown )   &
1152                  &   CALL ctl_stop( TRIM(clinfo), 'case not coded...You must use jpdom_unknown' )
1153            ELSEIF( idmspc >  irankpv ) THEN
1154                  IF( PRESENT(pv_r2d) .AND. itime == 1 .AND. idimsz(3) == 1 .AND. idmspc == 3 ) THEN
1155                     CALL ctl_warn( trim(clinfo), '2D array but 3 spatial dimensions for the data...'              ,   &
1156                           &         'As the size of the z dimension is 1 and as we try to read the first record, ',   &
1157                           &         'we accept this case, even if there is a possible mix-up between z and time dimension' )   
1158                     idmspc = idmspc - 1
1159                  ELSE
1160                     CALL ctl_stop( TRIM(clinfo), 'To keep iom lisibility, when reading a '//clrankpv//'D array,'         ,   &
1161                        &                         'we do not accept data with '//cldmspc//' spatial dimensions',   &
1162                        &                         'Use ncwa -a to suppress the unnecessary dimensions' )
1163                  ENDIF
1164            ENDIF
1165            !
1166            ! definition of istart and icnt
1167            !
1168            icnt  (:) = 1
1169            istart(:) = 1
1170            istart(idmspc+1) = itime
1171   
1172            IF( PRESENT(kstart) .AND. .NOT. ll_depth_spec ) THEN
1173               istart(1:idmspc) = kstart(1:idmspc) 
1174               icnt  (1:idmspc) = kcount(1:idmspc)
1175            ELSE
1176               IF(idom == jpdom_unknown ) THEN
1177                  icnt(1:idmspc) = idimsz(1:idmspc)
1178               ELSE
1179                  IF( .NOT. PRESENT(pv_r1d) ) THEN   !   not a 1D array
1180                     IF(     idom == jpdom_data    ) THEN
1181                        jstartrow = 1
1182                        IF( luse_jattr ) THEN
1183                           CALL iom_getatt(kiomid, 'open_ocean_jstart', jstartrow ) ! -999 is returned if the attribute is not found
1184                           jstartrow = MAX(1,jstartrow)
1185                        ENDIF
1186                        istart(1:2) = (/ mig(1), mjg(1) + jstartrow - 1 /)  ! icnt(1:2) done below
1187                     ELSEIF( idom == jpdom_global  ) THEN ; istart(1:2) = (/ nimpp , njmpp  /)  ! icnt(1:2) done below
[5118]1188                     ENDIF
[9367]1189                     ! we do not read the overlap                     -> we start to read at nldi, nldj
[679]1190! JMM + SM: ugly patch before getting the new version of lib_mpp)
1191!                  IF( idom /= jpdom_local_noovlap )   istart(1:2) = istart(1:2) + (/ nldi - 1, nldj - 1 /)
[9367]1192                     IF( llnoov .AND. idom /= jpdom_local_noovlap ) istart(1:2) = istart(1:2) + (/ nldi - 1, nldj - 1 /)
[679]1193                  ! we do not read the overlap and the extra-halos -> from nldi to nlei and from nldj to nlej
1194! JMM + SM: ugly patch before getting the new version of lib_mpp)
1195!                  icnt(1:2) = (/ nlei - nldi + 1, nlej - nldj + 1 /)
[9367]1196                     IF( llnoov ) THEN   ;   icnt(1:2) = (/ nlei - nldi + 1, nlej - nldj + 1 /)
1197                     ELSE                ;   icnt(1:2) = (/ nlci           , nlcj            /)
[544]1198                     ENDIF
[9367]1199                     IF( PRESENT(pv_r3d) ) THEN
1200                        IF( idom == jpdom_data ) THEN                        ;                               icnt(3) = inlev
1201                        ELSEIF( ll_depth_spec .AND. PRESENT(kstart) ) THEN   ;   istart(3) = kstart(3)   ;   icnt(3) = kcount(3)
1202                        ELSE                                                 ;                               icnt(3) = inlev
1203                        ENDIF
1204                     ENDIF
[544]1205                  ENDIF
1206               ENDIF
[679]1207            ENDIF
[544]1208
[9367]1209            ! check that istart and icnt can be used with this file
1210            !-
1211            DO jl = 1, jpmax_dims
1212               itmp = istart(jl)+icnt(jl)-1
1213               IF( itmp > idimsz(jl) .AND. idimsz(jl) /= 0 ) THEN
1214                  WRITE( ctmp1, FMT="('(istart(', i1, ') + icnt(', i1, ') - 1) = ', i5)" ) jl, jl, itmp
1215                  WRITE( ctmp2, FMT="(' is larger than idimsz(', i1,') = ', i5)"         ) jl, idimsz(jl)
1216                  CALL ctl_stop( trim(clinfo), 'start and count too big regarding to the size of the data, ', ctmp1, ctmp2 )     
1217               ENDIF
1218            END DO
[544]1219
[9367]1220            ! check that icnt matches the input array
1221            !-     
1222            IF( idom == jpdom_unknown ) THEN
1223               IF( irankpv == 1 )        ishape(1:1) = SHAPE(pv_r1d)
1224               IF( irankpv == 2 )        ishape(1:2) = SHAPE(pv_r2d)
1225               IF( irankpv == 3 )        ishape(1:3) = SHAPE(pv_r3d)
1226               ctmp1 = 'd'
1227            ELSE
1228               IF( irankpv == 2 ) THEN
[679]1229! JMM + SM: ugly patch before getting the new version of lib_mpp)
1230!               ishape(1:2) = SHAPE(pv_r2d(nldi:nlei,nldj:nlej  ))   ;   ctmp1 = 'd(nldi:nlei,nldj:nlej)'
[9367]1231                  IF( llnoov ) THEN ; ishape(1:2)=SHAPE(pv_r2d(nldi:nlei,nldj:nlej  )) ; ctmp1='d(nldi:nlei,nldj:nlej)'
1232                  ELSE              ; ishape(1:2)=SHAPE(pv_r2d(1   :nlci,1   :nlcj  )) ; ctmp1='d(1:nlci,1:nlcj)'
1233                  ENDIF
[544]1234               ENDIF
[9367]1235               IF( irankpv == 3 ) THEN 
[679]1236! JMM + SM: ugly patch before getting the new version of lib_mpp)
1237!               ishape(1:3) = SHAPE(pv_r3d(nldi:nlei,nldj:nlej,:))   ;   ctmp1 = 'd(nldi:nlei,nldj:nlej,:)'
[9367]1238                  IF( llnoov ) THEN ; ishape(1:3)=SHAPE(pv_r3d(nldi:nlei,nldj:nlej,:)) ; ctmp1='d(nldi:nlei,nldj:nlej,:)'
1239                  ELSE              ; ishape(1:3)=SHAPE(pv_r3d(1   :nlci,1   :nlcj,:)) ; ctmp1='d(1:nlci,1:nlcj,:)'
1240                  ENDIF
[544]1241               ENDIF
[679]1242            ENDIF
1243         
[9367]1244            DO jl = 1, irankpv
1245               WRITE( ctmp2, FMT="(', ', i1,'): ', i5,' /= icnt(', i1,'):', i5)" ) jl, ishape(jl), jl, icnt(jl)
1246               IF( ishape(jl) /= icnt(jl) )   CALL ctl_stop( TRIM(clinfo), 'size(pv_r'//clrankpv//TRIM(ctmp1)//TRIM(ctmp2) )
1247            END DO
[679]1248
[9367]1249         ENDIF
[544]1250
[9367]1251         ! read the data
1252         !-     
1253         IF( idvar > 0 .AND. istop == nstop ) THEN   ! no additional errors until this point...
1254            !
[679]1255         ! find the right index of the array to be read
1256! JMM + SM: ugly patch before getting the new version of lib_mpp)
1257!         IF( idom /= jpdom_unknown ) THEN   ;   ix1 = nldi   ;   ix2 = nlei      ;   iy1 = nldj   ;   iy2 = nlej
1258!         ELSE                               ;   ix1 = 1      ;   ix2 = icnt(1)   ;   iy1 = 1      ;   iy2 = icnt(2)
1259!         ENDIF
[9367]1260            IF( llnoov ) THEN
1261               IF( idom /= jpdom_unknown ) THEN   ;   ix1 = nldi   ;   ix2 = nlei      ;   iy1 = nldj   ;   iy2 = nlej
1262               ELSE                               ;   ix1 = 1      ;   ix2 = icnt(1)   ;   iy1 = 1      ;   iy2 = icnt(2)
1263               ENDIF
1264            ELSE
1265               IF( idom /= jpdom_unknown ) THEN   ;   ix1 = 1      ;   ix2 = nlci      ;   iy1 = 1      ;   iy2 = nlcj
1266               ELSE                               ;   ix1 = 1      ;   ix2 = icnt(1)   ;   iy1 = 1      ;   iy2 = icnt(2)
1267               ENDIF
[679]1268            ENDIF
1269     
[10425]1270            CALL iom_nf90_get( kiomid, idvar, inbdim, istart, icnt, ix1, ix2, iy1, iy2, pv_r1d, pv_r2d, pv_r3d )
[679]1271
[9367]1272            IF( istop == nstop ) THEN   ! no additional errors until this point...
1273               IF(lwp) WRITE(numout,"(10x,' read ',a,' (rec: ',i6,') in ',a,' ok')") TRIM(cdvar), itime, TRIM(iom_file(kiomid)%name)
1274             
1275               !--- overlap areas and extra hallows (mpp)
1276               IF(     PRESENT(pv_r2d) .AND. idom /= jpdom_unknown ) THEN
[11536]1277                  CALL lbc_lnk( 'iom', pv_r2d,'Z', -999., kfillmode = jpfillnothing )
[9367]1278               ELSEIF( PRESENT(pv_r3d) .AND. idom /= jpdom_unknown ) THEN
1279                  ! this if could be simplified with the new lbc_lnk that works with any size of the 3rd dimension
1280                  IF( icnt(3) == inlev ) THEN
[11536]1281                     CALL lbc_lnk( 'iom', pv_r3d,'Z', -999., kfillmode = jpfillnothing )
[9367]1282                  ELSE   ! put some arbitrary value (a call to lbc_lnk will be done later...)
1283                     DO jj = nlcj+1, jpj   ;   pv_r3d(1:nlci, jj, :) = pv_r3d(1:nlci, nlej, :)   ;   END DO
1284                     DO ji = nlci+1, jpi   ;   pv_r3d(ji    , : , :) = pv_r3d(nlei  , :   , :)   ;   END DO
1285                  ENDIF
[679]1286               ENDIF
[9367]1287               !
1288            ELSE
1289               ! return if istop == nstop is false
1290               RETURN
[679]1291            ENDIF
[9367]1292         ELSE
1293            ! return if statment idvar > 0 .AND. istop == nstop is false
1294            RETURN
1295         ENDIF
1296         !
1297      ELSE        ! read using XIOS. Only if KEY_IOMPUT is defined
1298#if defined key_iomput
1299!would be good to be able to check which context is active and swap only if current is not restart
1300         IF( PRESENT(pv_r3d) ) THEN
[9535]1301            pv_r3d(:, :, :) = 0.
[9367]1302            if(lwp) write(numout,*) 'XIOS RST READ (3D): ',trim(cdvar)
1303            CALL xios_recv_field( trim(cdvar), pv_r3d)
1304            IF(idom /= jpdom_unknown ) then
[11536]1305                CALL lbc_lnk( 'iom', pv_r3d,'Z', -999., kfillmode = jpfillnothing)
[679]1306            ENDIF
[9367]1307         ELSEIF( PRESENT(pv_r2d) ) THEN
[9535]1308            pv_r2d(:, :) = 0.
[9367]1309            if(lwp) write(numout,*) 'XIOS RST READ (2D): ', trim(cdvar)
1310            CALL xios_recv_field( trim(cdvar), pv_r2d)
1311            IF(idom /= jpdom_unknown ) THEN
[11536]1312                CALL lbc_lnk('iom', pv_r2d,'Z',-999., kfillmode = jpfillnothing)
[9367]1313            ENDIF
1314         ELSEIF( PRESENT(pv_r1d) ) THEN
[9535]1315            pv_r1d(:) = 0.
[9367]1316            if(lwp) write(numout,*) 'XIOS RST READ (1D): ', trim(cdvar)
1317            CALL xios_recv_field( trim(cdvar), pv_r1d)
[544]1318         ENDIF
[9367]1319#else
1320         istop = istop + 1 
1321         clinfo = 'Can not use XIOS in iom_get_123d, file: '//trim(clname)//', var:'//trim(cdvar)
1322#endif
[544]1323      ENDIF
[9367]1324!some final adjustments
1325      ! C1D case : always call lbc_lnk to replicate the central value over the whole 3X3 domain
[10425]1326      IF( lk_c1d .AND. PRESENT(pv_r2d) )   CALL lbc_lnk( 'iom', pv_r2d,'Z',1. )
1327      IF( lk_c1d .AND. PRESENT(pv_r3d) )   CALL lbc_lnk( 'iom', pv_r3d,'Z',1. )
[9367]1328
1329      !--- Apply scale_factor and offset
1330      zscf = iom_file(kiomid)%scf(idvar)      ! scale factor
1331      zofs = iom_file(kiomid)%ofs(idvar)      ! offset
1332      IF(     PRESENT(pv_r1d) ) THEN
1333         IF( zscf /= 1. )   pv_r1d(:) = pv_r1d(:) * zscf 
1334         IF( zofs /= 0. )   pv_r1d(:) = pv_r1d(:) + zofs
1335      ELSEIF( PRESENT(pv_r2d) ) THEN
1336         IF( zscf /= 1.)   pv_r2d(:,:) = pv_r2d(:,:) * zscf
1337         IF( zofs /= 0.)   pv_r2d(:,:) = pv_r2d(:,:) + zofs
1338      ELSEIF( PRESENT(pv_r3d) ) THEN
1339         IF( zscf /= 1.)   pv_r3d(:,:,:) = pv_r3d(:,:,:) * zscf
1340         IF( zofs /= 0.)   pv_r3d(:,:,:) = pv_r3d(:,:,:) + zofs
1341      ENDIF
[544]1342      !
1343   END SUBROUTINE iom_get_123d
1344
[12276]1345   SUBROUTINE iom_get_var( cdname, z2d)
1346      CHARACTER(LEN=*), INTENT(in ) ::   cdname
1347      REAL(wp), DIMENSION(jpi,jpj) ::   z2d 
1348#if defined key_iomput
1349      IF( xios_field_is_active( cdname, at_current_timestep_arg = .TRUE. ) ) THEN
1350         z2d(:,:) = 0._wp
1351         CALL xios_recv_field( cdname, z2d)
1352      ENDIF
1353#else
1354      IF( .FALSE. )   WRITE(numout,*) cdname, z2d ! useless test to avoid compilation warnings
1355#endif
1356   END SUBROUTINE iom_get_var
[544]1357
[12276]1358
[10522]1359   FUNCTION iom_getszuld ( kiomid ) 
1360      !!-----------------------------------------------------------------------
1361      !!                  ***  FUNCTION  iom_getszuld  ***
[544]1362      !!
[10522]1363      !! ** Purpose : get the size of the unlimited dimension in a file
1364      !!              (return -1 if not found)
1365      !!-----------------------------------------------------------------------
1366      INTEGER, INTENT(in   ) ::   kiomid   ! file Identifier
[544]1367      !
[10522]1368      INTEGER                ::   iom_getszuld
1369      !!-----------------------------------------------------------------------
1370      iom_getszuld = -1
[679]1371      IF( kiomid > 0 ) THEN
[10523]1372         IF( iom_file(kiomid)%iduld > 0 )   iom_getszuld = iom_file(kiomid)%lenuld
[544]1373      ENDIF
[10522]1374   END FUNCTION iom_getszuld
1375   
[544]1376
1377   !!----------------------------------------------------------------------
[10425]1378   !!                   INTERFACE iom_chkatt
[2528]1379   !!----------------------------------------------------------------------
[10425]1380   SUBROUTINE iom_chkatt( kiomid, cdatt, llok, ksize, cdvar )
[2528]1381      INTEGER         , INTENT(in   )                 ::   kiomid    ! Identifier of the file
1382      CHARACTER(len=*), INTENT(in   )                 ::   cdatt     ! Name of the attribute
[10425]1383      LOGICAL         , INTENT(  out)                 ::   llok      ! Error code
1384      INTEGER         , INTENT(  out), OPTIONAL       ::   ksize     ! Size of the attribute array
[7646]1385      CHARACTER(len=*), INTENT(in   ), OPTIONAL       ::   cdvar     ! Name of the variable
[2528]1386      !
1387      IF( kiomid > 0 ) THEN
[10425]1388         IF( iom_file(kiomid)%nfid > 0 )   CALL iom_nf90_chkatt( kiomid, cdatt, llok, ksize=ksize, cdvar=cdvar )
[2528]1389      ENDIF
[10425]1390      !
1391   END SUBROUTINE iom_chkatt
1392
1393   !!----------------------------------------------------------------------
1394   !!                   INTERFACE iom_getatt
1395   !!----------------------------------------------------------------------
1396   SUBROUTINE iom_g0d_iatt( kiomid, cdatt, katt0d, cdvar )
1397      INTEGER               , INTENT(in   )           ::   kiomid    ! Identifier of the file
1398      CHARACTER(len=*)      , INTENT(in   )           ::   cdatt     ! Name of the attribute
1399      INTEGER               , INTENT(  out)           ::   katt0d    ! read field
1400      CHARACTER(len=*)      , INTENT(in   ), OPTIONAL ::   cdvar     ! Name of the variable
1401      !
1402      IF( kiomid > 0 ) THEN
1403         IF( iom_file(kiomid)%nfid > 0 )   CALL iom_nf90_getatt( kiomid, cdatt,  katt0d =  katt0d, cdvar=cdvar )
1404      ENDIF
[7646]1405   END SUBROUTINE iom_g0d_iatt
[2528]1406
[10425]1407   SUBROUTINE iom_g1d_iatt( kiomid, cdatt, katt1d, cdvar )
1408      INTEGER               , INTENT(in   )           ::   kiomid    ! Identifier of the file
1409      CHARACTER(len=*)      , INTENT(in   )           ::   cdatt     ! Name of the attribute
1410      INTEGER, DIMENSION(:) , INTENT(  out)           ::   katt1d    ! read field
1411      CHARACTER(len=*)      , INTENT(in   ), OPTIONAL ::   cdvar     ! Name of the variable
[7646]1412      !
1413      IF( kiomid > 0 ) THEN
[10425]1414         IF( iom_file(kiomid)%nfid > 0 )   CALL iom_nf90_getatt( kiomid, cdatt,  katt1d =  katt1d, cdvar=cdvar )
[7646]1415      ENDIF
[10425]1416   END SUBROUTINE iom_g1d_iatt
1417
1418   SUBROUTINE iom_g0d_ratt( kiomid, cdatt, patt0d, cdvar )
1419      INTEGER               , INTENT(in   )           ::   kiomid    ! Identifier of the file
1420      CHARACTER(len=*)      , INTENT(in   )           ::   cdatt     ! Name of the attribute
1421      REAL(wp)              , INTENT(  out)           ::   patt0d    ! read field
1422      CHARACTER(len=*)      , INTENT(in   ), OPTIONAL ::   cdvar     ! Name of the variable
1423      !
1424      IF( kiomid > 0 ) THEN
1425         IF( iom_file(kiomid)%nfid > 0 )   CALL iom_nf90_getatt( kiomid, cdatt,  patt0d =  patt0d, cdvar=cdvar )
1426      ENDIF
[7646]1427   END SUBROUTINE iom_g0d_ratt
[2528]1428
[10425]1429   SUBROUTINE iom_g1d_ratt( kiomid, cdatt, patt1d, cdvar )
1430      INTEGER               , INTENT(in   )           ::   kiomid    ! Identifier of the file
1431      CHARACTER(len=*)      , INTENT(in   )           ::   cdatt     ! Name of the attribute
1432      REAL(wp), DIMENSION(:), INTENT(  out)           ::   patt1d    ! read field
1433      CHARACTER(len=*)      , INTENT(in   ), OPTIONAL ::   cdvar     ! Name of the variable
[7646]1434      !
1435      IF( kiomid > 0 ) THEN
[10425]1436         IF( iom_file(kiomid)%nfid > 0 )   CALL iom_nf90_getatt( kiomid, cdatt,  patt1d =  patt1d, cdvar=cdvar )
[7646]1437      ENDIF
[10425]1438   END SUBROUTINE iom_g1d_ratt
1439   
1440   SUBROUTINE iom_g0d_catt( kiomid, cdatt, cdatt0d, cdvar )
1441      INTEGER               , INTENT(in   )           ::   kiomid    ! Identifier of the file
1442      CHARACTER(len=*)      , INTENT(in   )           ::   cdatt     ! Name of the attribute
1443      CHARACTER(len=*)      , INTENT(  out)           ::   cdatt0d   ! read field
1444      CHARACTER(len=*)      , INTENT(in   ), OPTIONAL ::   cdvar     ! Name of the variable
1445      !
1446      IF( kiomid > 0 ) THEN
1447         IF( iom_file(kiomid)%nfid > 0 )   CALL iom_nf90_getatt( kiomid, cdatt, cdatt0d = cdatt0d, cdvar=cdvar )
1448      ENDIF
[7646]1449   END SUBROUTINE iom_g0d_catt
1450
[10425]1451
[2528]1452   !!----------------------------------------------------------------------
[7646]1453   !!                   INTERFACE iom_putatt
1454   !!----------------------------------------------------------------------
[10425]1455   SUBROUTINE iom_p0d_iatt( kiomid, cdatt, katt0d, cdvar )
1456      INTEGER               , INTENT(in   )           ::   kiomid    ! Identifier of the file
1457      CHARACTER(len=*)      , INTENT(in   )           ::   cdatt     ! Name of the attribute
1458      INTEGER               , INTENT(in   )           ::   katt0d    ! written field
1459      CHARACTER(len=*)      , INTENT(in   ), OPTIONAL ::   cdvar     ! Name of the variable
[7646]1460      !
1461      IF( kiomid > 0 ) THEN
[10425]1462         IF( iom_file(kiomid)%nfid > 0 )   CALL iom_nf90_putatt( kiomid, cdatt,  katt0d =  katt0d, cdvar=cdvar )
[7646]1463      ENDIF
1464   END SUBROUTINE iom_p0d_iatt
1465
[10425]1466   SUBROUTINE iom_p1d_iatt( kiomid, cdatt, katt1d, cdvar )
1467      INTEGER               , INTENT(in   )           ::   kiomid    ! Identifier of the file
1468      CHARACTER(len=*)      , INTENT(in   )           ::   cdatt     ! Name of the attribute
1469      INTEGER, DIMENSION(:) , INTENT(in   )           ::   katt1d    ! written field
1470      CHARACTER(len=*)      , INTENT(in   ), OPTIONAL ::   cdvar     ! Name of the variable
[7646]1471      !
1472      IF( kiomid > 0 ) THEN
[10425]1473         IF( iom_file(kiomid)%nfid > 0 )   CALL iom_nf90_putatt( kiomid, cdatt,  katt1d =  katt1d, cdvar=cdvar )
[7646]1474      ENDIF
[10425]1475   END SUBROUTINE iom_p1d_iatt
1476
1477   SUBROUTINE iom_p0d_ratt( kiomid, cdatt, patt0d, cdvar )
1478      INTEGER               , INTENT(in   )           ::   kiomid    ! Identifier of the file
1479      CHARACTER(len=*)      , INTENT(in   )           ::   cdatt     ! Name of the attribute
1480      REAL(wp)              , INTENT(in   )           ::   patt0d    ! written field
1481      CHARACTER(len=*)      , INTENT(in   ), OPTIONAL ::   cdvar     ! Name of the variable
1482      !
1483      IF( kiomid > 0 ) THEN
1484         IF( iom_file(kiomid)%nfid > 0 )   CALL iom_nf90_putatt( kiomid, cdatt,  patt0d =  patt0d, cdvar=cdvar )
1485      ENDIF
[7646]1486   END SUBROUTINE iom_p0d_ratt
1487
[10425]1488   SUBROUTINE iom_p1d_ratt( kiomid, cdatt, patt1d, cdvar )
1489      INTEGER               , INTENT(in   )           ::   kiomid    ! Identifier of the file
1490      CHARACTER(len=*)      , INTENT(in   )           ::   cdatt     ! Name of the attribute
1491      REAL(wp), DIMENSION(:), INTENT(in   )           ::   patt1d    ! written field
1492      CHARACTER(len=*)      , INTENT(in   ), OPTIONAL ::   cdvar     ! Name of the variable
[7646]1493      !
1494      IF( kiomid > 0 ) THEN
[10425]1495         IF( iom_file(kiomid)%nfid > 0 )   CALL iom_nf90_putatt( kiomid, cdatt,  patt1d =  patt1d, cdvar=cdvar )
[7646]1496      ENDIF
[10425]1497   END SUBROUTINE iom_p1d_ratt
1498   
1499   SUBROUTINE iom_p0d_catt( kiomid, cdatt, cdatt0d, cdvar )
1500      INTEGER               , INTENT(in   )           ::   kiomid    ! Identifier of the file
1501      CHARACTER(len=*)      , INTENT(in   )           ::   cdatt     ! Name of the attribute
1502      CHARACTER(len=*)      , INTENT(in   )           ::   cdatt0d   ! written field
1503      CHARACTER(len=*)      , INTENT(in   ), OPTIONAL ::   cdvar     ! Name of the variable
1504      !
1505      IF( kiomid > 0 ) THEN
1506         IF( iom_file(kiomid)%nfid > 0 )   CALL iom_nf90_putatt( kiomid, cdatt, cdatt0d = cdatt0d, cdvar=cdvar )
1507      ENDIF
[7646]1508   END SUBROUTINE iom_p0d_catt
1509
[10425]1510
[7646]1511   !!----------------------------------------------------------------------
[544]1512   !!                   INTERFACE iom_rstput
1513   !!----------------------------------------------------------------------
[9367]1514   SUBROUTINE iom_rp0d( kt, kwrite, kiomid, cdvar, pvar, ktype, ldxios )
[544]1515      INTEGER         , INTENT(in)                         ::   kt       ! ocean time-step
1516      INTEGER         , INTENT(in)                         ::   kwrite   ! writing time-step
1517      INTEGER         , INTENT(in)                         ::   kiomid   ! Identifier of the file
1518      CHARACTER(len=*), INTENT(in)                         ::   cdvar    ! time axis name
[556]1519      REAL(wp)        , INTENT(in)                         ::   pvar     ! written field
[544]1520      INTEGER         , INTENT(in), OPTIONAL               ::   ktype    ! variable external type
[9367]1521      LOGICAL, OPTIONAL :: ldxios   ! xios write flag
1522      LOGICAL :: llx                ! local xios write flag
[544]1523      INTEGER :: ivid   ! variable id
[9367]1524
1525      llx = .FALSE.
1526      IF(PRESENT(ldxios)) llx = ldxios
1527      IF( llx ) THEN
1528#ifdef key_iomput
1529      IF( kt == kwrite ) THEN
1530          IF(lwp) write(numout,*) 'RESTART: write (XIOS 0D) ',trim(cdvar)
1531          CALL xios_send_field(trim(cdvar), pvar)
[12950]1532      ELSE
1533          IF(lwp) write(numout,*) 'RESTART: define (XIOS 0D) ',trim(cdvar)
1534          CALL iom_set_rstw_active( trim(cdvar), rd0 = pvar ) 
[9367]1535      ENDIF
1536#endif
1537      ELSE
1538         IF( kiomid > 0 ) THEN
1539            IF( iom_file(kiomid)%nfid > 0 ) THEN
1540               ivid = iom_varid( kiomid, cdvar, ldstop = .FALSE. )
[10425]1541               CALL iom_nf90_rstput( kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r0d = pvar )
[9367]1542            ENDIF
[679]1543         ENDIF
1544      ENDIF
[550]1545   END SUBROUTINE iom_rp0d
[544]1546
[9367]1547   SUBROUTINE iom_rp1d( kt, kwrite, kiomid, cdvar, pvar, ktype, ldxios )
[544]1548      INTEGER         , INTENT(in)                         ::   kt       ! ocean time-step
1549      INTEGER         , INTENT(in)                         ::   kwrite   ! writing time-step
1550      INTEGER         , INTENT(in)                         ::   kiomid   ! Identifier of the file
1551      CHARACTER(len=*), INTENT(in)                         ::   cdvar    ! time axis name
[2715]1552      REAL(wp)        , INTENT(in), DIMENSION(          :) ::   pvar     ! written field
[544]1553      INTEGER         , INTENT(in), OPTIONAL               ::   ktype    ! variable external type
[9367]1554      LOGICAL, OPTIONAL                                    ::   ldxios   ! xios write flag
1555      LOGICAL :: llx                ! local xios write flag
[544]1556      INTEGER :: ivid   ! variable id
[9367]1557
1558      llx = .FALSE.
1559      IF(PRESENT(ldxios)) llx = ldxios
1560      IF( llx ) THEN
1561#ifdef key_iomput
1562      IF( kt == kwrite ) THEN
1563         IF(lwp) write(numout,*) 'RESTART: write (XIOS 1D) ',trim(cdvar)
1564         CALL xios_send_field(trim(cdvar), pvar)
[12950]1565      ELSE
1566         IF(lwp) write(numout,*) 'RESTART: define (XIOS 1D) ',trim(cdvar)
1567         CALL iom_set_rstw_active( trim(cdvar), rd1 = pvar ) 
[9367]1568      ENDIF
1569#endif
1570      ELSE
1571         IF( kiomid > 0 ) THEN
1572            IF( iom_file(kiomid)%nfid > 0 ) THEN
1573               ivid = iom_varid( kiomid, cdvar, ldstop = .FALSE. )
[10425]1574               CALL iom_nf90_rstput( kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r1d = pvar )
[9367]1575            ENDIF
[679]1576         ENDIF
1577      ENDIF
[550]1578   END SUBROUTINE iom_rp1d
[544]1579
[9367]1580   SUBROUTINE iom_rp2d( kt, kwrite, kiomid, cdvar, pvar, ktype, ldxios )
[544]1581      INTEGER         , INTENT(in)                         ::   kt       ! ocean time-step
1582      INTEGER         , INTENT(in)                         ::   kwrite   ! writing time-step
1583      INTEGER         , INTENT(in)                         ::   kiomid   ! Identifier of the file
1584      CHARACTER(len=*), INTENT(in)                         ::   cdvar    ! time axis name
[2715]1585      REAL(wp)        , INTENT(in), DIMENSION(:,    :    ) ::   pvar     ! written field
[544]1586      INTEGER         , INTENT(in), OPTIONAL               ::   ktype    ! variable external type
[9367]1587      LOGICAL, OPTIONAL :: ldxios   ! xios write flag
1588      LOGICAL :: llx
[544]1589      INTEGER :: ivid   ! variable id
[9367]1590
1591      llx = .FALSE.
1592      IF(PRESENT(ldxios)) llx = ldxios
1593      IF( llx ) THEN
1594#ifdef key_iomput
1595      IF( kt == kwrite ) THEN
1596         IF(lwp) write(numout,*) 'RESTART: write (XIOS 2D) ',trim(cdvar)
1597         CALL xios_send_field(trim(cdvar), pvar)
[12950]1598      ELSE
1599         IF(lwp) write(numout,*) 'RESTART: define (XIOS 2D) ',trim(cdvar)
1600         CALL iom_set_rstw_active( trim(cdvar), rd2 = pvar ) 
[9367]1601      ENDIF
1602#endif
1603      ELSE
1604         IF( kiomid > 0 ) THEN
1605            IF( iom_file(kiomid)%nfid > 0 ) THEN
1606               ivid = iom_varid( kiomid, cdvar, ldstop = .FALSE. )
[10425]1607               CALL iom_nf90_rstput( kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r2d = pvar )
[9367]1608            ENDIF
[679]1609         ENDIF
1610      ENDIF
[550]1611   END SUBROUTINE iom_rp2d
[544]1612
[9367]1613   SUBROUTINE iom_rp3d( kt, kwrite, kiomid, cdvar, pvar, ktype, ldxios )
[544]1614      INTEGER         , INTENT(in)                         ::   kt       ! ocean time-step
1615      INTEGER         , INTENT(in)                         ::   kwrite   ! writing time-step
1616      INTEGER         , INTENT(in)                         ::   kiomid   ! Identifier of the file
1617      CHARACTER(len=*), INTENT(in)                         ::   cdvar    ! time axis name
[2715]1618      REAL(wp)        , INTENT(in),       DIMENSION(:,:,:) ::   pvar     ! written field
[544]1619      INTEGER         , INTENT(in), OPTIONAL               ::   ktype    ! variable external type
[9367]1620      LOGICAL, OPTIONAL :: ldxios   ! xios write flag
1621      LOGICAL :: llx                 ! local xios write flag
[544]1622      INTEGER :: ivid   ! variable id
[9367]1623
1624      llx = .FALSE.
1625      IF(PRESENT(ldxios)) llx = ldxios
1626      IF( llx ) THEN
1627#ifdef key_iomput
1628      IF( kt == kwrite ) THEN
1629         IF(lwp) write(numout,*) 'RESTART: write (XIOS 3D) ',trim(cdvar)
1630         CALL xios_send_field(trim(cdvar), pvar)
[12950]1631      ELSE
1632         IF(lwp) write(numout,*) 'RESTART: define (XIOS 3D) ',trim(cdvar)
1633         CALL iom_set_rstw_active( trim(cdvar), rd3 = pvar )
[9367]1634      ENDIF
1635#endif
1636      ELSE
1637         IF( kiomid > 0 ) THEN
1638            IF( iom_file(kiomid)%nfid > 0 ) THEN
1639               ivid = iom_varid( kiomid, cdvar, ldstop = .FALSE. )
[10425]1640               CALL iom_nf90_rstput( kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r3d = pvar )
[9367]1641            ENDIF
[679]1642         ENDIF
1643      ENDIF
[550]1644   END SUBROUTINE iom_rp3d
[1359]1645
1646
[10425]1647  SUBROUTINE iom_delay_rst( cdaction, cdcpnt, kncid )
1648      !!---------------------------------------------------------------------
1649      !!   Routine iom_delay_rst: used read/write restart related to mpp_delay
1650      !!
1651      !!---------------------------------------------------------------------
1652      CHARACTER(len=*), INTENT(in   ) ::   cdaction        !
1653      CHARACTER(len=*), INTENT(in   ) ::   cdcpnt
1654      INTEGER         , INTENT(in   ) ::   kncid
1655      !
1656      INTEGER  :: ji
1657      INTEGER  :: indim
1658      LOGICAL  :: llattexist
1659      REAL(wp), ALLOCATABLE, DIMENSION(:) ::   zreal1d
1660      !!---------------------------------------------------------------------
1661      !
1662      !                                      ===================================
1663      IF( TRIM(cdaction) == 'READ' ) THEN   ! read restart related to mpp_delay !
1664         !                                   ===================================
1665         DO ji = 1, nbdelay
1666            IF ( c_delaycpnt(ji) == cdcpnt ) THEN
1667               CALL iom_chkatt( kncid, 'DELAY_'//c_delaylist(ji), llattexist, indim )
1668               IF( llattexist )  THEN
1669                  ALLOCATE( todelay(ji)%z1d(indim) )
1670                  CALL iom_getatt( kncid, 'DELAY_'//c_delaylist(ji), todelay(ji)%z1d(:) )
1671                  ndelayid(ji) = 0   ! set to 0 to specify that the value was read in the restart
1672               ENDIF
1673           ENDIF
1674         END DO
1675         !                                   ====================================
1676      ELSE                                  ! write restart related to mpp_delay !
1677         !                                   ====================================
1678         DO ji = 1, nbdelay   ! save only ocean delayed global communication variables
1679            IF ( c_delaycpnt(ji) == cdcpnt ) THEN
1680               IF( ASSOCIATED(todelay(ji)%z1d) ) THEN
1681                  CALL mpp_delay_rcv(ji)   ! make sure %z1d is received
1682                  CALL iom_putatt( kncid, 'DELAY_'//c_delaylist(ji), todelay(ji)%z1d(:) )
1683               ENDIF
1684            ENDIF
1685         END DO
1686         !
1687      ENDIF
1688     
1689   END SUBROUTINE iom_delay_rst
1690 
1691   
1692
[544]1693   !!----------------------------------------------------------------------
[1457]1694   !!                   INTERFACE iom_put
[1359]1695   !!----------------------------------------------------------------------
[1738]1696   SUBROUTINE iom_p0d( cdname, pfield0d )
1697      CHARACTER(LEN=*), INTENT(in) ::   cdname
1698      REAL(wp)        , INTENT(in) ::   pfield0d
[11536]1699!!      REAL(wp)        , DIMENSION(jpi,jpj) ::   zz     ! masson
[1738]1700#if defined key_iomput
[11536]1701!!clem      zz(:,:)=pfield0d
1702!!clem      CALL xios_send_field(cdname, zz)
1703      CALL xios_send_field(cdname, (/pfield0d/)) 
[1738]1704#else
1705      IF( .FALSE. )   WRITE(numout,*) cdname, pfield0d   ! useless test to avoid compilation warnings
1706#endif
1707   END SUBROUTINE iom_p0d
1708
[3294]1709   SUBROUTINE iom_p1d( cdname, pfield1d )
1710      CHARACTER(LEN=*)          , INTENT(in) ::   cdname
1711      REAL(wp),     DIMENSION(:), INTENT(in) ::   pfield1d
1712#if defined key_iomput
[3695]1713      CALL xios_send_field( cdname, RESHAPE( (/pfield1d/), (/1,1,SIZE(pfield1d)/) ) )
[3294]1714#else
1715      IF( .FALSE. )   WRITE(numout,*) cdname, pfield1d   ! useless test to avoid compilation warnings
1716#endif
1717   END SUBROUTINE iom_p1d
1718
[1359]1719   SUBROUTINE iom_p2d( cdname, pfield2d )
1720      CHARACTER(LEN=*)            , INTENT(in) ::   cdname
[2715]1721      REAL(wp),     DIMENSION(:,:), INTENT(in) ::   pfield2d
[1412]1722#if defined key_iomput
[3695]1723      CALL xios_send_field(cdname, pfield2d)
[1520]1724#else
1725      IF( .FALSE. )   WRITE(numout,*) cdname, pfield2d   ! useless test to avoid compilation warnings
[1359]1726#endif
1727   END SUBROUTINE iom_p2d
[544]1728
[1359]1729   SUBROUTINE iom_p3d( cdname, pfield3d )
1730      CHARACTER(LEN=*)                , INTENT(in) ::   cdname
[2715]1731      REAL(wp),       DIMENSION(:,:,:), INTENT(in) ::   pfield3d
[1412]1732#if defined key_iomput
[9019]1733      CALL xios_send_field( cdname, pfield3d )
[1520]1734#else
1735      IF( .FALSE. )   WRITE(numout,*) cdname, pfield3d   ! useless test to avoid compilation warnings
[1359]1736#endif
1737   END SUBROUTINE iom_p3d
[544]1738
[12276]1739   SUBROUTINE iom_p4d( cdname, pfield4d )
[12377]1740      CHARACTER(LEN=*)                , INTENT(in) ::   cdname
[12276]1741      REAL(wp),       DIMENSION(:,:,:,:), INTENT(in) ::   pfield4d
[1412]1742#if defined key_iomput
[12276]1743      CALL xios_send_field(cdname, pfield4d)
1744#else
1745      IF( .FALSE. )   WRITE(numout,*) cdname, pfield4d   ! useless test to avoid compilation warnings
1746#endif
1747   END SUBROUTINE iom_p4d
1748
1749
1750#if defined key_iomput
[9019]1751   !!----------------------------------------------------------------------
[9802]1752   !!   'key_iomput'                                         XIOS interface
[9019]1753   !!----------------------------------------------------------------------
[1359]1754
[9930]1755   SUBROUTINE iom_set_domain_attr( cdid, ni_glo, nj_glo, ibegin, jbegin, ni, nj,                                               &
[5363]1756      &                                    data_dim, data_ibegin, data_ni, data_jbegin, data_nj, lonvalue, latvalue, mask,     &
[5415]1757      &                                    nvertex, bounds_lon, bounds_lat, area )
[9019]1758      !!----------------------------------------------------------------------
1759      !!----------------------------------------------------------------------
1760      CHARACTER(LEN=*)                  , INTENT(in) ::   cdid
1761      INTEGER                 , OPTIONAL, INTENT(in) ::   ni_glo, nj_glo, ibegin, jbegin, ni, nj
1762      INTEGER                 , OPTIONAL, INTENT(in) ::   data_dim, data_ibegin, data_ni, data_jbegin, data_nj
[9930]1763      INTEGER                 , OPTIONAL, INTENT(in) ::   nvertex
[9019]1764      REAL(wp), DIMENSION(:)  , OPTIONAL, INTENT(in) ::   lonvalue, latvalue
1765      REAL(wp), DIMENSION(:,:), OPTIONAL, INTENT(in) ::   bounds_lon, bounds_lat, area
1766      LOGICAL , DIMENSION(:)  , OPTIONAL, INTENT(in) ::   mask
1767      !!----------------------------------------------------------------------
1768      !
1769      IF( xios_is_valid_domain     (cdid) ) THEN
[4148]1770         CALL xios_set_domain_attr     ( cdid, ni_glo=ni_glo, nj_glo=nj_glo, ibegin=ibegin, jbegin=jbegin, ni=ni, nj=nj,   &
1771            &    data_dim=data_dim, data_ibegin=data_ibegin, data_ni=data_ni, data_jbegin=data_jbegin, data_nj=data_nj ,   &
[7646]1772            &    lonvalue_1D=lonvalue, latvalue_1D=latvalue, mask_1D=mask, nvertex=nvertex, bounds_lon_1D=bounds_lon,      &
1773            &    bounds_lat_1D=bounds_lat, area=area, type='curvilinear')
[9367]1774      ENDIF
[9019]1775      IF( xios_is_valid_domaingroup(cdid) ) THEN
[4148]1776         CALL xios_set_domaingroup_attr( cdid, ni_glo=ni_glo, nj_glo=nj_glo, ibegin=ibegin, jbegin=jbegin, ni=ni, nj=nj,   &
1777            &    data_dim=data_dim, data_ibegin=data_ibegin, data_ni=data_ni, data_jbegin=data_jbegin, data_nj=data_nj ,   &
[7646]1778            &    lonvalue_1D=lonvalue, latvalue_1D=latvalue, mask_1D=mask, nvertex=nvertex, bounds_lon_1D=bounds_lon,      &
1779            &    bounds_lat_1D=bounds_lat, area=area, type='curvilinear' )
[3695]1780      ENDIF
[9019]1781      !
[4148]1782      CALL xios_solve_inheritance()
[9019]1783      !
[3695]1784   END SUBROUTINE iom_set_domain_attr
1785
1786
[9019]1787   SUBROUTINE iom_set_zoom_domain_attr( cdid, ibegin, jbegin, ni, nj )
1788      !!----------------------------------------------------------------------
1789      !!----------------------------------------------------------------------
[9930]1790      CHARACTER(LEN=*), INTENT(in) ::   cdid
1791      INTEGER         , INTENT(in) ::   ibegin, jbegin, ni, nj
1792      !
1793      TYPE(xios_gridgroup) :: gridgroup_hdl
1794      TYPE(xios_grid)      :: grid_hdl
1795      TYPE(xios_domain)    :: domain_hdl 
1796      TYPE(xios_axis)      :: axis_hdl 
1797      CHARACTER(LEN=64)    :: cldomrefid   ! domain_ref name
1798      CHARACTER(len=1)     :: cl1          ! last character of this name
[9019]1799      !!----------------------------------------------------------------------
[9930]1800      !
1801      IF( xios_is_valid_zoom_domain(cdid) ) THEN
1802         ! define the zoom_domain attributs
1803         CALL xios_set_zoom_domain_attr( cdid, ibegin=ibegin, jbegin=jbegin, ni=ni, nj=nj )
1804         ! define a new 2D grid with this new domain
1805         CALL xios_get_handle("grid_definition", gridgroup_hdl )
1806         CALL xios_add_child(gridgroup_hdl, grid_hdl, TRIM(cdid)//'_2D' )   ! add a new 2D grid to grid_definition
1807         CALL xios_add_child(grid_hdl, domain_hdl, TRIM(cdid) )             ! add its domain
1808         ! define a new 3D grid with this new domain
1809         CALL xios_add_child(gridgroup_hdl, grid_hdl, TRIM(cdid)//'_3D' )   ! add a new 3D grid to grid_definition
1810         CALL xios_add_child(grid_hdl, domain_hdl, TRIM(cdid) )             ! add its domain
1811         ! vertical axis
1812         cl1 = cdid(LEN_TRIM(cdid):)                                        ! last letter of cdid
1813         cl1 = CHAR(ICHAR(cl1)+32)                                          ! from upper to lower case
1814         CALL xios_add_child(grid_hdl, axis_hdl, 'depth'//cl1)              ! add its axis
1815      ENDIF
1816      !     
[7646]1817   END SUBROUTINE iom_set_zoom_domain_attr
1818
1819
[5415]1820   SUBROUTINE iom_set_axis_attr( cdid, paxis, bounds )
[9019]1821      !!----------------------------------------------------------------------
1822      !!----------------------------------------------------------------------
[4148]1823      CHARACTER(LEN=*)      , INTENT(in) ::   cdid
[5415]1824      REAL(wp), DIMENSION(:)  , OPTIONAL, INTENT(in) ::   paxis
1825      REAL(wp), DIMENSION(:,:), OPTIONAL, INTENT(in) ::   bounds
[9019]1826      !!----------------------------------------------------------------------
1827      IF( PRESENT(paxis) ) THEN
1828         IF( xios_is_valid_axis     (cdid) )   CALL xios_set_axis_attr     ( cdid, n_glo=SIZE(paxis), value=paxis )
1829         IF( xios_is_valid_axisgroup(cdid) )   CALL xios_set_axisgroup_attr( cdid, n_glo=SIZE(paxis), value=paxis )
[5415]1830      ENDIF
[9019]1831      IF( xios_is_valid_axis     (cdid) )   CALL xios_set_axis_attr     ( cdid, bounds=bounds )
1832      IF( xios_is_valid_axisgroup(cdid) )   CALL xios_set_axisgroup_attr( cdid, bounds=bounds )
[4148]1833      CALL xios_solve_inheritance()
[3737]1834   END SUBROUTINE iom_set_axis_attr
1835
1836
[4148]1837   SUBROUTINE iom_set_field_attr( cdid, freq_op, freq_offset )
[9019]1838      !!----------------------------------------------------------------------
1839      !!----------------------------------------------------------------------
1840      CHARACTER(LEN=*)             , INTENT(in) ::   cdid
1841      TYPE(xios_duration), OPTIONAL, INTENT(in) ::   freq_op
1842      TYPE(xios_duration), OPTIONAL, INTENT(in) ::   freq_offset
1843      !!----------------------------------------------------------------------
1844      IF( xios_is_valid_field     (cdid) )   CALL xios_set_field_attr     ( cdid, freq_op=freq_op, freq_offset=freq_offset )
1845      IF( xios_is_valid_fieldgroup(cdid) )   CALL xios_set_fieldgroup_attr( cdid, freq_op=freq_op, freq_offset=freq_offset )
[4148]1846      CALL xios_solve_inheritance()
[3695]1847   END SUBROUTINE iom_set_field_attr
1848
1849
[4148]1850   SUBROUTINE iom_set_file_attr( cdid, name, name_suffix )
[9019]1851      !!----------------------------------------------------------------------
1852      !!----------------------------------------------------------------------
[4148]1853      CHARACTER(LEN=*)          , INTENT(in) ::   cdid
[3695]1854      CHARACTER(LEN=*),OPTIONAL , INTENT(in) ::   name, name_suffix
[9019]1855      !!----------------------------------------------------------------------
1856      IF( xios_is_valid_file     (cdid) )   CALL xios_set_file_attr     ( cdid, name=name, name_suffix=name_suffix )
1857      IF( xios_is_valid_filegroup(cdid) )   CALL xios_set_filegroup_attr( cdid, name=name, name_suffix=name_suffix )
[4148]1858      CALL xios_solve_inheritance()
[3771]1859   END SUBROUTINE iom_set_file_attr
[3695]1860
1861
[4148]1862   SUBROUTINE iom_get_file_attr( cdid, name, name_suffix, output_freq )
[9019]1863      !!----------------------------------------------------------------------
1864      !!----------------------------------------------------------------------
[4148]1865      CHARACTER(LEN=*)          , INTENT(in ) ::   cdid
[7646]1866      CHARACTER(LEN=*),OPTIONAL , INTENT(out) ::   name, name_suffix
1867      TYPE(xios_duration), OPTIONAL , INTENT(out) :: output_freq
[4148]1868      LOGICAL                                 ::   llexist1,llexist2,llexist3
1869      !---------------------------------------------------------------------
1870      IF( PRESENT( name        ) )   name = ''          ! default values
1871      IF( PRESENT( name_suffix ) )   name_suffix = ''
[7646]1872      IF( PRESENT( output_freq ) )   output_freq = xios_duration(0,0,0,0,0,0)
[9019]1873      IF( xios_is_valid_file     (cdid) ) THEN
[4148]1874         CALL xios_solve_inheritance()
1875         CALL xios_is_defined_file_attr     ( cdid, name = llexist1, name_suffix = llexist2, output_freq = llexist3)
1876         IF(llexist1)   CALL xios_get_file_attr     ( cdid, name = name )
1877         IF(llexist2)   CALL xios_get_file_attr     ( cdid, name_suffix = name_suffix )
1878         IF(llexist3)   CALL xios_get_file_attr     ( cdid, output_freq = output_freq )
1879      ENDIF
[9019]1880      IF( xios_is_valid_filegroup(cdid) ) THEN
[4148]1881         CALL xios_solve_inheritance()
1882         CALL xios_is_defined_filegroup_attr( cdid, name = llexist1, name_suffix = llexist2, output_freq = llexist3)
1883         IF(llexist1)   CALL xios_get_filegroup_attr( cdid, name = name )
1884         IF(llexist2)   CALL xios_get_filegroup_attr( cdid, name_suffix = name_suffix )
1885         IF(llexist3)   CALL xios_get_filegroup_attr( cdid, output_freq = output_freq )
1886      ENDIF
1887   END SUBROUTINE iom_get_file_attr
1888
1889
1890   SUBROUTINE iom_set_grid_attr( cdid, mask )
[9019]1891      !!----------------------------------------------------------------------
1892      !!----------------------------------------------------------------------
[4148]1893      CHARACTER(LEN=*)                   , INTENT(in) ::   cdid
[3771]1894      LOGICAL, DIMENSION(:,:,:), OPTIONAL, INTENT(in) ::   mask
[9019]1895      !!----------------------------------------------------------------------
1896      IF( xios_is_valid_grid     (cdid) )   CALL xios_set_grid_attr     ( cdid, mask_3D=mask )
1897      IF( xios_is_valid_gridgroup(cdid) )   CALL xios_set_gridgroup_attr( cdid, mask_3D=mask )
[4148]1898      CALL xios_solve_inheritance()
[3771]1899   END SUBROUTINE iom_set_grid_attr
[3695]1900
[4152]1901   SUBROUTINE iom_setkt( kt, cdname )
[9019]1902      !!----------------------------------------------------------------------
1903      !!----------------------------------------------------------------------
[4152]1904      INTEGER         , INTENT(in) ::   kt 
1905      CHARACTER(LEN=*), INTENT(in) ::   cdname
[9019]1906      !!----------------------------------------------------------------------
[4152]1907      CALL iom_swap( cdname )   ! swap to cdname context
1908      CALL xios_update_calendar(kt)
[9019]1909      IF( cdname /= TRIM(cxios_context) )   CALL iom_swap( TRIM(cxios_context) )   ! return back to nemo context
[4152]1910   END SUBROUTINE iom_setkt
[3695]1911
[4152]1912   SUBROUTINE iom_context_finalize( cdname )
[9019]1913      !!----------------------------------------------------------------------
1914      !!----------------------------------------------------------------------
[4152]1915      CHARACTER(LEN=*), INTENT(in) :: cdname
[9367]1916      CHARACTER(LEN=120)           :: clname
[9019]1917      !!----------------------------------------------------------------------
[9367]1918      clname = cdname
[12957]1919      IF(lwp) write(numout, *) 'Finalize: ', TRIM(cdname)
[9367]1920      IF( TRIM(Agrif_CFixed()) .NE. '0' ) clname = TRIM(Agrif_CFixed())//"_"//clname 
1921      IF( xios_is_valid_context(clname) ) THEN
[4990]1922         CALL iom_swap( cdname )   ! swap to cdname context
1923         CALL xios_context_finalize() ! finalize the context
[5407]1924         IF( cdname /= TRIM(cxios_context) ) CALL iom_swap( TRIM(cxios_context) )   ! return back to nemo context
[4990]1925      ENDIF
[9367]1926      !
[4152]1927   END SUBROUTINE iom_context_finalize
1928
1929
[9984]1930   SUBROUTINE set_grid( cdgrd, plon, plat, ldxios, ldrxios )
[1359]1931      !!----------------------------------------------------------------------
[4148]1932      !!                     ***  ROUTINE set_grid  ***
[1359]1933      !!
[1725]1934      !! ** Purpose :   define horizontal grids
[1359]1935      !!----------------------------------------------------------------------
[3771]1936      CHARACTER(LEN=1)            , INTENT(in) ::   cdgrd
[1359]1937      REAL(wp), DIMENSION(jpi,jpj), INTENT(in) ::   plon
1938      REAL(wp), DIMENSION(jpi,jpj), INTENT(in) ::   plat
[3771]1939      !
[9019]1940      INTEGER  :: ni, nj
[3771]1941      REAL(wp), DIMENSION(jpi,jpj,jpk) ::   zmask
[9984]1942      LOGICAL, INTENT(IN) :: ldxios, ldrxios
[9019]1943      !!----------------------------------------------------------------------
1944      !
1945      ni = nlei-nldi+1
1946      nj = nlej-nldj+1
1947      !
[7646]1948      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]1949      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]1950!don't define lon and lat for restart reading context.
1951      IF ( .NOT.ldrxios ) &
1952         CALL iom_set_domain_attr("grid_"//cdgrd, lonvalue = RESHAPE(plon(nldi:nlei, nldj:nlej),(/ ni*nj /)),   &
[3771]1953         &                                     latvalue = RESHAPE(plat(nldi:nlei, nldj:nlej),(/ ni*nj /))) 
[9019]1954      !
[9367]1955      IF ( ln_mskland .AND. (.NOT.ldxios) ) THEN
[3771]1956         ! mask land points, keep values on coast line -> specific mask for U, V and W points
1957         SELECT CASE ( cdgrd )
1958         CASE('T')   ;   zmask(:,:,:)       = tmask(:,:,:)
[10425]1959         CASE('U')   ;   zmask(2:jpim1,:,:) = tmask(2:jpim1,:,:) + tmask(3:jpi,:,:)   ;   CALL lbc_lnk( 'iom', zmask, 'U', 1. )
1960         CASE('V')   ;   zmask(:,2:jpjm1,:) = tmask(:,2:jpjm1,:) + tmask(:,3:jpj,:)   ;   CALL lbc_lnk( 'iom', zmask, 'V', 1. )
[3771]1961         CASE('W')   ;   zmask(:,:,2:jpk  ) = tmask(:,:,1:jpkm1) + tmask(:,:,2:jpk)   ;   zmask(:,:,1) = tmask(:,:,1)
1962         END SELECT
1963         !
[7646]1964         CALL iom_set_domain_attr( "grid_"//cdgrd       , mask = RESHAPE(zmask(nldi:nlei,nldj:nlej,1),(/ni*nj    /)) /= 0. )
[4148]1965         CALL iom_set_grid_attr  ( "grid_"//cdgrd//"_3D", mask = RESHAPE(zmask(nldi:nlei,nldj:nlej,:),(/ni,nj,jpk/)) /= 0. )
[3771]1966      ENDIF
[9019]1967      !
[1359]1968   END SUBROUTINE set_grid
1969
[1725]1970
[5415]1971   SUBROUTINE set_grid_bounds( cdgrd, plon_cnr, plat_cnr, plon_pnt, plat_pnt )
[5363]1972      !!----------------------------------------------------------------------
1973      !!                   ***  ROUTINE set_grid_bounds  ***
1974      !!
1975      !! ** Purpose :   define horizontal grid corners
1976      !!
1977      !!----------------------------------------------------------------------
[9019]1978      CHARACTER(LEN=1)                      , INTENT(in) :: cdgrd
1979      REAL(wp), DIMENSION(jpi,jpj)          , INTENT(in) :: plon_cnr, plat_cnr  ! Lat/lon coord. of a contiguous vertex of cell (i,j)
1980      REAL(wp), DIMENSION(jpi,jpj), OPTIONAL, INTENT(in) :: plon_pnt, plat_pnt  ! Lat/lon coord. of the point of cell (i,j)
[5363]1981      !
[9019]1982      INTEGER :: ji, jj, jn, ni, nj
[12377]1983      INTEGER :: icnr, jcnr                             ! Offset such that the vertex coordinate (i+icnr,j+jcnr)
1984      !                                                 ! represents the bottom-left corner of cell (i,j)
[9019]1985      REAL(wp), ALLOCATABLE, DIMENSION(:,:,:,:) :: z_bnds      ! Lat/lon coordinates of the vertices of cell (i,j)
1986      REAL(wp), ALLOCATABLE, DIMENSION(:,:)     :: z_fld       ! Working array to determine where to rotate cells
1987      REAL(wp), ALLOCATABLE, DIMENSION(:,:)     :: z_rot       ! Lat/lon working array for rotation of cells
1988      !!----------------------------------------------------------------------
[5415]1989      !
[9019]1990      ALLOCATE( z_bnds(4,jpi,jpj,2), z_fld(jpi,jpj), z_rot(4,2)  )
[5363]1991      !
[5415]1992      ! Offset of coordinate representing bottom-left corner
[5363]1993      SELECT CASE ( TRIM(cdgrd) )
[9019]1994      CASE ('T', 'W')   ;   icnr = -1   ;   jcnr = -1
1995      CASE ('U')        ;   icnr =  0   ;   jcnr = -1
1996      CASE ('V')        ;   icnr = -1   ;   jcnr =  0
[5363]1997      END SELECT
[9019]1998      !
1999      ni = nlei-nldi+1   ! Dimensions of subdomain interior
2000      nj = nlej-nldj+1
2001      !
[5363]2002      z_fld(:,:) = 1._wp
[10425]2003      CALL lbc_lnk( 'iom', z_fld, cdgrd, -1. )    ! Working array for location of northfold
[9019]2004      !
[5363]2005      ! Cell vertices that can be defined
2006      DO jj = 2, jpjm1
2007         DO ji = 2, jpim1
[5415]2008            z_bnds(1,ji,jj,1) = plat_cnr(ji+icnr,  jj+jcnr  ) ! Bottom-left
2009            z_bnds(2,ji,jj,1) = plat_cnr(ji+icnr+1,jj+jcnr  ) ! Bottom-right
2010            z_bnds(3,ji,jj,1) = plat_cnr(ji+icnr+1,jj+jcnr+1) ! Top-right
2011            z_bnds(4,ji,jj,1) = plat_cnr(ji+icnr,  jj+jcnr+1) ! Top-left
2012            z_bnds(1,ji,jj,2) = plon_cnr(ji+icnr,  jj+jcnr  ) ! Bottom-left
2013            z_bnds(2,ji,jj,2) = plon_cnr(ji+icnr+1,jj+jcnr  ) ! Bottom-right
2014            z_bnds(3,ji,jj,2) = plon_cnr(ji+icnr+1,jj+jcnr+1) ! Top-right
2015            z_bnds(4,ji,jj,2) = plon_cnr(ji+icnr,  jj+jcnr+1) ! Top-left
[5363]2016         END DO
2017      END DO
[9019]2018      !
[5363]2019      ! Cell vertices on boundries
2020      DO jn = 1, 4
[11536]2021         CALL lbc_lnk( 'iom', z_bnds(jn,:,:,1), cdgrd, 1., pfillval=999._wp )
2022         CALL lbc_lnk( 'iom', z_bnds(jn,:,:,2), cdgrd, 1., pfillval=999._wp )
[5363]2023      END DO
[9019]2024      !
[5415]2025      ! Zero-size cells at closed boundaries if cell points provided,
2026      ! otherwise they are closed cells with unrealistic bounds
2027      IF( PRESENT(plon_pnt) .AND. PRESENT(plat_pnt) ) THEN
2028         IF( (nbondi == -1 .OR. nbondi == 2) .AND. .NOT. (jperio == 1 .OR. jperio == 4 .OR. jperio == 6) ) THEN
2029            DO jn = 1, 4        ! (West or jpni = 1), closed E-W
2030               z_bnds(jn,1,:,1) = plat_pnt(1,:)  ;  z_bnds(jn,1,:,2) = plon_pnt(1,:)
2031            END DO
2032         ENDIF
2033         IF( (nbondi == 1 .OR. nbondi == 2) .AND. .NOT. (jperio == 1 .OR. jperio == 4 .OR. jperio == 6) ) THEN
2034            DO jn = 1, 4        ! (East or jpni = 1), closed E-W
2035               z_bnds(jn,nlci,:,1) = plat_pnt(nlci,:)  ;  z_bnds(jn,nlci,:,2) = plon_pnt(nlci,:)
2036            END DO
2037         ENDIF
2038         IF( nbondj == -1 .OR. (nbondj == 2 .AND. jperio /= 2) ) THEN
2039            DO jn = 1, 4        ! South or (jpnj = 1, not symmetric)
2040               z_bnds(jn,:,1,1) = plat_pnt(:,1)  ;  z_bnds(jn,:,1,2) = plon_pnt(:,1)
2041            END DO
2042         ENDIF
2043         IF( (nbondj == 1 .OR. nbondj == 2) .AND. jperio  < 3 ) THEN
2044            DO jn = 1, 4        ! (North or jpnj = 1), no north fold
2045               z_bnds(jn,:,nlcj,1) = plat_pnt(:,nlcj)  ;  z_bnds(jn,:,nlcj,2) = plon_pnt(:,nlcj)
2046            END DO
2047         ENDIF
[5363]2048      ENDIF
[9019]2049      !
2050      IF( (nbondj == 1 .OR. nbondj == 2) .AND. jperio >= 3 ) THEN    ! Rotate cells at the north fold
[5363]2051         DO jj = 1, jpj
2052            DO ji = 1, jpi
2053               IF( z_fld(ji,jj) == -1. ) THEN
2054                  z_rot(1,:) = z_bnds(3,ji,jj,:) ; z_rot(2,:) = z_bnds(4,ji,jj,:)
2055                  z_rot(3,:) = z_bnds(1,ji,jj,:) ; z_rot(4,:) = z_bnds(2,ji,jj,:)
2056                  z_bnds(:,ji,jj,:) = z_rot(:,:)
2057               ENDIF
2058            END DO
2059         END DO
[9019]2060      ELSE IF( nbondj == 2 .AND. jperio == 2 ) THEN                  ! Invert cells at the symmetric equator
[5363]2061         DO ji = 1, jpi
2062            z_rot(1:2,:) = z_bnds(3:4,ji,1,:)
2063            z_rot(3:4,:) = z_bnds(1:2,ji,1,:)
2064            z_bnds(:,ji,1,:) = z_rot(:,:)
2065         END DO
2066      ENDIF
[9019]2067      !
[5363]2068      CALL iom_set_domain_attr("grid_"//cdgrd, bounds_lat = RESHAPE(z_bnds(:,nldi:nlei,nldj:nlej,1),(/ 4,ni*nj /)),           &
[9019]2069          &                                    bounds_lon = RESHAPE(z_bnds(:,nldi:nlei,nldj:nlej,2),(/ 4,ni*nj /)), nvertex=4 )
2070      !
[5415]2071      DEALLOCATE( z_bnds, z_fld, z_rot ) 
[9019]2072      !
[5363]2073   END SUBROUTINE set_grid_bounds
2074
2075
[5385]2076   SUBROUTINE set_grid_znl( plat )
2077      !!----------------------------------------------------------------------
2078      !!                     ***  ROUTINE set_grid_znl  ***
2079      !!
2080      !! ** Purpose :   define grids for zonal mean
2081      !!
2082      !!----------------------------------------------------------------------
2083      REAL(wp), DIMENSION(jpi,jpj), INTENT(in) ::   plat
2084      !
[9019]2085      INTEGER  :: ni, nj, ix, iy
[5385]2086      REAL(wp), DIMENSION(:), ALLOCATABLE  ::   zlon
[9019]2087      !!----------------------------------------------------------------------
2088      !
2089      ni=nlei-nldi+1       ! define zonal mean domain (jpj*jpk)
2090      nj=nlej-nldj+1
2091      ALLOCATE( zlon(ni*nj) )       ;       zlon(:) = 0._wp
2092      !
[12276]2093!      CALL dom_ngb( -168.53, 65.03, ix, iy, 'T' ) !  i-line that passes through Bering Strait: Reference latitude (used in plots)
2094      CALL dom_ngb( 180., 90., ix, iy, 'T' ) !  i-line that passes near the North Pole : Reference latitude (used in plots)
[7646]2095      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]2096      CALL iom_set_domain_attr("gznl", data_dim=2, data_ibegin = 1-nldi, data_ni = jpi, data_jbegin = 1-nldj, data_nj = jpj)
2097      CALL iom_set_domain_attr("gznl", lonvalue = zlon,   &
2098         &                             latvalue = RESHAPE(plat(nldi:nlei, nldj:nlej),(/ ni*nj /))) 
[12276]2099      CALL iom_set_zoom_domain_attr("ptr", ibegin=ix-1, jbegin=0, ni=1, nj=jpjglo)
[5385]2100      !
2101      CALL iom_update_file_name('ptr')
2102      !
2103   END SUBROUTINE set_grid_znl
2104
[9019]2105
[1738]2106   SUBROUTINE set_scalar
2107      !!----------------------------------------------------------------------
[4148]2108      !!                     ***  ROUTINE set_scalar  ***
[1738]2109      !!
2110      !! ** Purpose :   define fake grids for scalar point
2111      !!
2112      !!----------------------------------------------------------------------
[5426]2113      REAL(wp), DIMENSION(1)   ::   zz = 1.
[1738]2114      !!----------------------------------------------------------------------
[9019]2115      !
[7646]2116      CALL iom_set_domain_attr('scalarpoint', ni_glo=jpnij, nj_glo=1, ibegin=narea-1, jbegin=0, ni=1, nj=1)
[5426]2117      CALL iom_set_domain_attr('scalarpoint', data_dim=2, data_ibegin = 1, data_ni = 1, data_jbegin = 1, data_nj = 1)
[9019]2118      !
2119      zz = REAL( narea, wp )
[5426]2120      CALL iom_set_domain_attr('scalarpoint', lonvalue=zz, latvalue=zz)
[9019]2121      !
[1738]2122   END SUBROUTINE set_scalar
2123
2124
[1725]2125   SUBROUTINE set_xmlatt
2126      !!----------------------------------------------------------------------
[4148]2127      !!                     ***  ROUTINE set_xmlatt  ***
[1725]2128      !!
2129      !! ** Purpose :   automatic definitions of some of the xml attributs...
2130      !!
2131      !!----------------------------------------------------------------------
2132      CHARACTER(len=1),DIMENSION( 3) ::   clgrd                    ! suffix name
[4148]2133      CHARACTER(len=256)             ::   clsuff                   ! suffix name
[1725]2134      CHARACTER(len=1)               ::   cl1                      ! 1 character
[5003]2135      CHARACTER(len=2)               ::   cl2                      ! 2 characters
2136      CHARACTER(len=3)               ::   cl3                      ! 3 characters
[4148]2137      INTEGER                        ::   ji, jg                   ! loop counters
[1725]2138      INTEGER                        ::   ix, iy                   ! i-,j- index
2139      REAL(wp)        ,DIMENSION(11) ::   zlontao                  ! longitudes of tao    moorings
2140      REAL(wp)        ,DIMENSION( 7) ::   zlattao                  ! latitudes  of tao    moorings
2141      REAL(wp)        ,DIMENSION( 4) ::   zlonrama                 ! longitudes of rama   moorings
2142      REAL(wp)        ,DIMENSION(11) ::   zlatrama                 ! latitudes  of rama   moorings
2143      REAL(wp)        ,DIMENSION( 3) ::   zlonpira                 ! longitudes of pirata moorings
2144      REAL(wp)        ,DIMENSION( 9) ::   zlatpira                 ! latitudes  of pirata moorings
[7646]2145      TYPE(xios_duration)            ::   f_op, f_of
[1725]2146      !!----------------------------------------------------------------------
2147      !
2148      ! frequency of the call of iom_put (attribut: freq_op)
[9019]2149      f_op%timestep = 1        ;  f_of%timestep =  0  ; CALL iom_set_field_attr('field_definition', freq_op=f_op, freq_offset=f_of)
2150      f_op%timestep = 2        ;  f_of%timestep =  0  ; CALL iom_set_field_attr('trendT_even'     , freq_op=f_op, freq_offset=f_of)
2151      f_op%timestep = 2        ;  f_of%timestep = -1  ; CALL iom_set_field_attr('trendT_odd'      , freq_op=f_op, freq_offset=f_of)
2152      f_op%timestep = nn_fsbc  ;  f_of%timestep =  0  ; CALL iom_set_field_attr('SBC'             , freq_op=f_op, freq_offset=f_of)
2153      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]2154      f_op%timestep = nn_fsbc  ;  f_of%timestep =  0  ; CALL iom_set_field_attr('ABL'             , freq_op=f_op, freq_offset=f_of)
[7646]2155
[1725]2156      ! output file names (attribut: name)
[4148]2157      DO ji = 1, 9
2158         WRITE(cl1,'(i1)') ji 
2159         CALL iom_update_file_name('file'//cl1)
[1725]2160      END DO
[4148]2161      DO ji = 1, 99
2162         WRITE(cl2,'(i2.2)') ji 
2163         CALL iom_update_file_name('file'//cl2)
2164      END DO
[5003]2165      DO ji = 1, 999
2166         WRITE(cl3,'(i3.3)') ji 
2167         CALL iom_update_file_name('file'//cl3)
2168      END DO
[1725]2169
2170      ! Zooms...
2171      clgrd = (/ 'T', 'U', 'W' /) 
2172      DO jg = 1, SIZE(clgrd)                                                                   ! grid type
2173         cl1 = clgrd(jg)
2174         ! Equatorial section (attributs: jbegin, ni, name_suffix)
2175         CALL dom_ngb( 0., 0., ix, iy, cl1 )
[9930]2176         CALL iom_set_zoom_domain_attr('Eq'//cl1, ibegin=0, jbegin=iy-1, ni=jpiglo, nj=1 )
[4148]2177         CALL iom_get_file_attr   ('Eq'//cl1, name_suffix = clsuff             )
2178         CALL iom_set_file_attr   ('Eq'//cl1, name_suffix = TRIM(clsuff)//'_Eq')
2179         CALL iom_update_file_name('Eq'//cl1)
[1725]2180      END DO
2181      ! TAO moorings (attributs: ibegin, jbegin, name_suffix)
2182      zlontao = (/ 137.0, 147.0, 156.0, 165.0, -180.0, -170.0, -155.0, -140.0, -125.0, -110.0, -95.0 /)
2183      zlattao = (/  -8.0,  -5.0,  -2.0,   0.0,    2.0,    5.0,    8.0 /)
2184      CALL set_mooring( zlontao, zlattao )
2185      ! RAMA moorings (attributs: ibegin, jbegin, name_suffix)
2186      zlonrama = (/  55.0,  67.0, 80.5, 90.0 /)
2187      zlatrama = (/ -16.0, -12.0, -8.0, -4.0, -1.5, 0.0, 1.5, 4.0, 8.0, 12.0, 15.0 /)
2188      CALL set_mooring( zlonrama, zlatrama )
2189      ! PIRATA moorings (attributs: ibegin, jbegin, name_suffix)
2190      zlonpira = (/ -38.0, -23.0, -10.0 /)
2191      zlatpira = (/ -19.0, -14.0,  -8.0, 0.0, 4.0, 8.0, 12.0, 15.0, 20.0 /)
2192      CALL set_mooring( zlonpira, zlatpira )
[9019]2193      !
[1725]2194   END SUBROUTINE set_xmlatt
2195
2196
[9019]2197   SUBROUTINE set_mooring( plon, plat )
[1725]2198      !!----------------------------------------------------------------------
[4148]2199      !!                     ***  ROUTINE set_mooring  ***
[1725]2200      !!
2201      !! ** Purpose :   automatic definitions of moorings xml attributs...
2202      !!
2203      !!----------------------------------------------------------------------
[9019]2204      REAL(wp), DIMENSION(:), INTENT(in) ::   plon, plat   ! longitudes/latitudes oft the mooring
[1725]2205      !
2206!!$      CHARACTER(len=1),DIMENSION(4) ::   clgrd = (/ 'T', 'U', 'V', 'W' /)   ! suffix name
2207      CHARACTER(len=1),DIMENSION(1) ::   clgrd = (/ 'T' /)        ! suffix name
[4148]2208      CHARACTER(len=256)            ::   clname                   ! file name
2209      CHARACTER(len=256)            ::   clsuff                   ! suffix name
[1725]2210      CHARACTER(len=1)              ::   cl1                      ! 1 character
2211      CHARACTER(len=6)              ::   clon,clat                ! name of longitude, latitude
2212      INTEGER                       ::   ji, jj, jg               ! loop counters
2213      INTEGER                       ::   ix, iy                   ! i-,j- index
2214      REAL(wp)                      ::   zlon, zlat
2215      !!----------------------------------------------------------------------
2216      DO jg = 1, SIZE(clgrd)
2217         cl1 = clgrd(jg)
2218         DO ji = 1, SIZE(plon)
2219            DO jj = 1, SIZE(plat)
2220               zlon = plon(ji)
2221               zlat = plat(jj)
2222               ! modifications for RAMA moorings
2223               IF( zlon ==  67. .AND. zlat ==  15. )   zlon =  65.
2224               IF( zlon ==  90. .AND. zlat <=  -4. )   zlon =  95.
2225               IF( zlon ==  95. .AND. zlat ==  -4. )   zlat =  -5.
2226               ! modifications for PIRATA moorings
2227               IF( zlon == -38. .AND. zlat == -19. )   zlon = -34.
2228               IF( zlon == -38. .AND. zlat == -14. )   zlon = -32.
2229               IF( zlon == -38. .AND. zlat ==  -8. )   zlon = -30.
2230               IF( zlon == -38. .AND. zlat ==   0. )   zlon = -35.
2231               IF( zlon == -23. .AND. zlat ==  20. )   zlat =  21.
2232               IF( zlon == -10. .AND. zlat == -14. )   zlat = -10.
2233               IF( zlon == -10. .AND. zlat ==  -8. )   zlat =  -6.
2234               IF( zlon == -10. .AND. zlat ==   4. ) THEN   ;   zlon = 0.   ;   zlat = 0.   ;   ENDIF
2235               CALL dom_ngb( zlon, zlat, ix, iy, cl1 )
2236               IF( zlon >= 0. ) THEN 
2237                  IF( zlon == REAL(NINT(zlon), wp) ) THEN   ;   WRITE(clon, '(i3,  a)') NINT( zlon), 'e'
2238                  ELSE                                      ;   WRITE(clon, '(f5.1,a)')       zlon , 'e'
2239                  ENDIF
2240               ELSE             
2241                  IF( zlon == REAL(NINT(zlon), wp) ) THEN   ;   WRITE(clon, '(i3,  a)') NINT(-zlon), 'w'
2242                  ELSE                                      ;   WRITE(clon, '(f5.1,a)')      -zlon , 'w'
2243                  ENDIF
2244               ENDIF
2245               IF( zlat >= 0. ) THEN 
2246                  IF( zlat == REAL(NINT(zlat), wp) ) THEN   ;   WRITE(clat, '(i2,  a)') NINT( zlat), 'n'
2247                  ELSE                                      ;   WRITE(clat, '(f4.1,a)')       zlat , 'n'
2248                  ENDIF
2249               ELSE             
2250                  IF( zlat == REAL(NINT(zlat), wp) ) THEN   ;   WRITE(clat, '(i2,  a)') NINT(-zlat), 's'
2251                  ELSE                                      ;   WRITE(clat, '(f4.1,a)')      -zlat , 's'
2252                  ENDIF
2253               ENDIF
2254               clname = TRIM(ADJUSTL(clat))//TRIM(ADJUSTL(clon))
[9930]2255               CALL iom_set_zoom_domain_attr(TRIM(clname)//cl1, ibegin= ix-1, jbegin= iy-1, ni=1, nj=1)
[7646]2256
[4148]2257               CALL iom_get_file_attr   (TRIM(clname)//cl1, name_suffix = clsuff                         )
2258               CALL iom_set_file_attr   (TRIM(clname)//cl1, name_suffix = TRIM(clsuff)//'_'//TRIM(clname))
2259               CALL iom_update_file_name(TRIM(clname)//cl1)
[1725]2260            END DO
2261         END DO
2262      END DO
2263     
2264   END SUBROUTINE set_mooring
2265
[4148]2266   
2267   SUBROUTINE iom_update_file_name( cdid )
2268      !!----------------------------------------------------------------------
2269      !!                     ***  ROUTINE iom_update_file_name  ***
2270      !!
2271      !! ** Purpose :   
2272      !!
2273      !!----------------------------------------------------------------------
2274      CHARACTER(LEN=*)          , INTENT(in) ::   cdid
2275      !
2276      CHARACTER(LEN=256) ::   clname
2277      CHARACTER(LEN=20)  ::   clfreq
2278      CHARACTER(LEN=20)  ::   cldate
2279      INTEGER            ::   idx
2280      INTEGER            ::   jn
2281      INTEGER            ::   itrlen
2282      INTEGER            ::   iyear, imonth, iday, isec
2283      REAL(wp)           ::   zsec
2284      LOGICAL            ::   llexist
[7646]2285      TYPE(xios_duration)   ::   output_freq 
[4148]2286      !!----------------------------------------------------------------------
[9019]2287      !
2288      DO jn = 1, 2
2289         !
[7646]2290         output_freq = xios_duration(0,0,0,0,0,0)
2291         IF( jn == 1 )   CALL iom_get_file_attr( cdid, name        = clname, output_freq = output_freq )
[4148]2292         IF( jn == 2 )   CALL iom_get_file_attr( cdid, name_suffix = clname )
[9019]2293         !
[4148]2294         IF ( TRIM(clname) /= '' ) THEN 
[9019]2295            !
[4148]2296            idx = INDEX(clname,'@expname@') + INDEX(clname,'@EXPNAME@')
2297            DO WHILE ( idx /= 0 ) 
2298               clname = clname(1:idx-1)//TRIM(cexper)//clname(idx+9:LEN_TRIM(clname))
2299               idx = INDEX(clname,'@expname@') + INDEX(clname,'@EXPNAME@')
2300            END DO
[9019]2301            !
[4148]2302            idx = INDEX(clname,'@freq@') + INDEX(clname,'@FREQ@')
2303            DO WHILE ( idx /= 0 ) 
[7646]2304              IF ( output_freq%timestep /= 0) THEN
2305                  WRITE(clfreq,'(I18,A2)')INT(output_freq%timestep),'ts' 
2306                  itrlen = LEN_TRIM(ADJUSTL(clfreq))
[7764]2307              ELSE IF ( output_freq%second /= 0 ) THEN
2308                  WRITE(clfreq,'(I19,A1)')INT(output_freq%second),'s' 
2309                  itrlen = LEN_TRIM(ADJUSTL(clfreq))
2310              ELSE IF ( output_freq%minute /= 0 ) THEN
[7768]2311                  WRITE(clfreq,'(I18,A2)')INT(output_freq%minute),'mi' 
[7764]2312                  itrlen = LEN_TRIM(ADJUSTL(clfreq))
[7646]2313              ELSE IF ( output_freq%hour /= 0 ) THEN
2314                  WRITE(clfreq,'(I19,A1)')INT(output_freq%hour),'h' 
2315                  itrlen = LEN_TRIM(ADJUSTL(clfreq))
2316              ELSE IF ( output_freq%day /= 0 ) THEN
2317                  WRITE(clfreq,'(I19,A1)')INT(output_freq%day),'d' 
2318                  itrlen = LEN_TRIM(ADJUSTL(clfreq))
2319              ELSE IF ( output_freq%month /= 0 ) THEN   
2320                  WRITE(clfreq,'(I19,A1)')INT(output_freq%month),'m' 
2321                  itrlen = LEN_TRIM(ADJUSTL(clfreq))
2322              ELSE IF ( output_freq%year /= 0 ) THEN   
2323                  WRITE(clfreq,'(I19,A1)')INT(output_freq%year),'y' 
2324                  itrlen = LEN_TRIM(ADJUSTL(clfreq))
2325              ELSE
[4148]2326                  CALL ctl_stop('error in the name of file id '//TRIM(cdid),   &
2327                     & ' attribute output_freq is undefined -> cannot replace @freq@ in '//TRIM(clname) )
[7646]2328              ENDIF
2329              clname = clname(1:idx-1)//TRIM(ADJUSTL(clfreq))//clname(idx+6:LEN_TRIM(clname))
2330              idx = INDEX(clname,'@freq@') + INDEX(clname,'@FREQ@')
[4148]2331            END DO
[9019]2332            !
[4148]2333            idx = INDEX(clname,'@startdate@') + INDEX(clname,'@STARTDATE@')
2334            DO WHILE ( idx /= 0 ) 
[12489]2335               cldate = iom_sdate( fjulday - rn_Dt / rday )
[4148]2336               clname = clname(1:idx-1)//TRIM(cldate)//clname(idx+11:LEN_TRIM(clname))
2337               idx = INDEX(clname,'@startdate@') + INDEX(clname,'@STARTDATE@')
2338            END DO
[9019]2339            !
[4148]2340            idx = INDEX(clname,'@startdatefull@') + INDEX(clname,'@STARTDATEFULL@')
2341            DO WHILE ( idx /= 0 ) 
[12489]2342               cldate = iom_sdate( fjulday - rn_Dt / rday, ldfull = .TRUE. )
[4148]2343               clname = clname(1:idx-1)//TRIM(cldate)//clname(idx+15:LEN_TRIM(clname))
2344               idx = INDEX(clname,'@startdatefull@') + INDEX(clname,'@STARTDATEFULL@')
2345            END DO
[9019]2346            !
[4148]2347            idx = INDEX(clname,'@enddate@') + INDEX(clname,'@ENDDATE@')
2348            DO WHILE ( idx /= 0 ) 
[12489]2349               cldate = iom_sdate( fjulday + rn_Dt / rday * REAL( nitend - nit000, wp ), ld24 = .TRUE. )
[4148]2350               clname = clname(1:idx-1)//TRIM(cldate)//clname(idx+9:LEN_TRIM(clname))
2351               idx = INDEX(clname,'@enddate@') + INDEX(clname,'@ENDDATE@')
2352            END DO
[9019]2353            !
[4148]2354            idx = INDEX(clname,'@enddatefull@') + INDEX(clname,'@ENDDATEFULL@')
2355            DO WHILE ( idx /= 0 ) 
[12489]2356               cldate = iom_sdate( fjulday + rn_Dt / rday * REAL( nitend - nit000, wp ), ld24 = .TRUE., ldfull = .TRUE. )
[4148]2357               clname = clname(1:idx-1)//TRIM(cldate)//clname(idx+13:LEN_TRIM(clname))
2358               idx = INDEX(clname,'@enddatefull@') + INDEX(clname,'@ENDDATEFULL@')
2359            END DO
[9019]2360            !
[12377]2361            IF( jn == 1 .AND. TRIM(Agrif_CFixed()) /= '0' )   clname = TRIM(Agrif_CFixed())//"_"//TRIM(clname)
[4148]2362            IF( jn == 1 )   CALL iom_set_file_attr( cdid, name        = clname )
2363            IF( jn == 2 )   CALL iom_set_file_attr( cdid, name_suffix = clname )
[9019]2364            !
[4148]2365         ENDIF
[9019]2366         !
[4148]2367      END DO
[9019]2368      !
[4148]2369   END SUBROUTINE iom_update_file_name
2370
2371
2372   FUNCTION iom_sdate( pjday, ld24, ldfull )
2373      !!----------------------------------------------------------------------
2374      !!                     ***  ROUTINE iom_sdate  ***
2375      !!
2376      !! ** Purpose :   send back the date corresponding to the given julian day
2377      !!----------------------------------------------------------------------
[9019]2378      REAL(wp), INTENT(in   )           ::   pjday    ! julian day
2379      LOGICAL , INTENT(in   ), OPTIONAL ::   ld24     ! true to force 24:00 instead of 00:00
2380      LOGICAL , INTENT(in   ), OPTIONAL ::   ldfull   ! true to get the compleate date: yyyymmdd_hh:mm:ss
[4148]2381      !
2382      CHARACTER(LEN=20) ::   iom_sdate
2383      CHARACTER(LEN=50) ::   clfmt                         !  format used to write the date
2384      INTEGER           ::   iyear, imonth, iday, ihour, iminute, isec
2385      REAL(wp)          ::   zsec
2386      LOGICAL           ::   ll24, llfull
[9019]2387      !!----------------------------------------------------------------------
[4148]2388      !
2389      IF( PRESENT(ld24) ) THEN   ;   ll24 = ld24
2390      ELSE                       ;   ll24 = .FALSE.
2391      ENDIF
[9019]2392      !
[4148]2393      IF( PRESENT(ldfull) ) THEN   ;   llfull = ldfull
2394      ELSE                         ;   llfull = .FALSE.
2395      ENDIF
[9019]2396      !
[4148]2397      CALL ju2ymds( pjday, iyear, imonth, iday, zsec )
2398      isec = NINT(zsec)
[9019]2399      !
[4148]2400      IF ( ll24 .AND. isec == 0 ) THEN   ! 00:00 of the next day -> move to 24:00 of the current day
2401         CALL ju2ymds( pjday - 1., iyear, imonth, iday, zsec )
2402         isec = 86400
2403      ENDIF
[9019]2404      !
[4148]2405      IF( iyear < 10000 ) THEN   ;   clfmt = "i4.4,2i2.2"                ! format used to write the date
2406      ELSE                       ;   WRITE(clfmt, "('i',i1,',2i2.2')") INT(LOG10(REAL(iyear,wp))) + 1
2407      ENDIF
[9019]2408      !
[5656]2409!$AGRIF_DO_NOT_TREAT     
[9019]2410      ! needed in the conv
[4148]2411      IF( llfull ) THEN
2412         clfmt = TRIM(clfmt)//",'_',i2.2,':',i2.2,':',i2.2"
2413         ihour   = isec / 3600
2414         isec    = MOD(isec, 3600)
2415         iminute = isec / 60
2416         isec    = MOD(isec, 60)
2417         WRITE(iom_sdate, '('//TRIM(clfmt)//')') iyear, imonth, iday, ihour, iminute, isec    ! date of the end of run
2418      ELSE
2419         WRITE(iom_sdate, '('//TRIM(clfmt)//')') iyear, imonth, iday                          ! date of the end of run
2420      ENDIF
[5656]2421!$AGRIF_END_DO_NOT_TREAT     
[9019]2422      !
[4148]2423   END FUNCTION iom_sdate
2424
[1359]2425#else
[9019]2426   !!----------------------------------------------------------------------
2427   !!   NOT 'key_iomput'                               a few dummy routines
2428   !!----------------------------------------------------------------------
[4152]2429   SUBROUTINE iom_setkt( kt, cdname )
2430      INTEGER         , INTENT(in)::   kt 
2431      CHARACTER(LEN=*), INTENT(in) ::   cdname
2432      IF( .FALSE. )   WRITE(numout,*) kt, cdname   ! useless test to avoid compilation warnings
[1359]2433   END SUBROUTINE iom_setkt
2434
[4152]2435   SUBROUTINE iom_context_finalize( cdname )
2436      CHARACTER(LEN=*), INTENT(in) ::   cdname
2437      IF( .FALSE. )   WRITE(numout,*)  cdname   ! useless test to avoid compilation warnings
2438   END SUBROUTINE iom_context_finalize
2439
[12377]2440   SUBROUTINE iom_update_file_name( cdid )
2441      CHARACTER(LEN=*), INTENT(in) ::   cdid
2442      IF( .FALSE. )   WRITE(numout,*)  cdid   ! useless test to avoid compilation warnings
2443   END SUBROUTINE iom_update_file_name
2444
[1359]2445#endif
[4689]2446
2447   LOGICAL FUNCTION iom_use( cdname )
2448      CHARACTER(LEN=*), INTENT(in) ::   cdname
2449#if defined key_iomput
2450      iom_use = xios_field_is_active( cdname )
2451#else
2452      iom_use = .FALSE.
2453#endif
2454   END FUNCTION iom_use
[11536]2455
2456   SUBROUTINE iom_miss_val( cdname, pmiss_val )
2457      CHARACTER(LEN=*), INTENT(in ) ::   cdname
2458      REAL(wp)        , INTENT(out) ::   pmiss_val   
2459#if defined key_iomput
2460      ! get missing value
2461      CALL xios_get_field_attr( cdname, default_value = pmiss_val )
2462#else
2463      IF( .FALSE. )   WRITE(numout,*) cdname, pmiss_val   ! useless test to avoid compilation warnings
[12377]2464      IF( .FALSE. )   pmiss_val = 0._wp                   ! useless assignment to avoid compilation warnings
[11536]2465#endif
2466   END SUBROUTINE iom_miss_val
2467 
[544]2468   !!======================================================================
2469END MODULE iom
Note: See TracBrowser for help on using the repository browser.