New URL for NEMO forge!   http://forge.nemo-ocean.eu

Since March 2022 along with NEMO 4.2 release, the code development moved to a self-hosted GitLab.
This present forge is now archived and remained online for history.
iom.F90 in NEMO/branches/2020/dev_r40_AGRIF_RW/src/OCE/IOM – NEMO

source: NEMO/branches/2020/dev_r40_AGRIF_RW/src/OCE/IOM/iom.F90 @ 12897

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

Ticket #2460: Changes in iom.F90

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