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 branches/UKMO/dev_merge_2017_GC_couple_pkg/NEMOGCM/NEMO/OPA_SRC/IOM – NEMO

source: branches/UKMO/dev_merge_2017_GC_couple_pkg/NEMOGCM/NEMO/OPA_SRC/IOM/iom.F90 @ 11257

Last change on this file since 11257 was 9677, checked in by dancopsey, 6 years ago

Strip out SVN keywords.

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