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_r13327_KERNEL-06_2_techene_e3/src/OCE/IOM – NEMO

source: NEMO/branches/2020/dev_r13327_KERNEL-06_2_techene_e3/src/OCE/IOM/iom.F90 @ 13998

Last change on this file since 13998 was 13998, checked in by techene, 3 years ago

branch updated with trunk 13787

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