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

source: NEMO/branches/2020/r12377_ticket2386/src/OCE/IOM/iom.F90 @ 12515

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

Ticket #2386 : changes in code to make XIOS restart read/write working in reference configurations

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