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

source: NEMO/branches/2019/dev_r11943_MERGE_2019/src/OCE/IOM/iom.F90 @ 12252

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

rev12240_dev_r11943_MERGE_2019: same as [12251], merge trunk 12072:12248, all sette tests ok, GYRE_PISCES, AMM12, ISOMIP, VORTEX intentical to 12236

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