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

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

ticket #2386: update trunk@12493 to have AGRIF sette working

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