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

source: NEMO/branches/2018/dev_r10164_HPC09_ESIWACE_PREP_MERGE/src/OCE/IOM/iom.F90 @ 10397

Last change on this file since 10397 was 10397, checked in by smasson, 5 years ago

dev_r10164_HPC09_ESIWACE_PREP_MERGE: action 5 introduce mpp_delay_sum, see #2133

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