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/2017/dev_merge_2017/NEMOGCM/NEMO/OPA_SRC/IOM – NEMO

source: branches/2017/dev_merge_2017/NEMOGCM/NEMO/OPA_SRC/IOM/iom.F90 @ 9367

Last change on this file since 9367 was 9367, checked in by mathiot, 6 years ago

Add restart read/write via XIOS capability (#1953 and #1962 and twiki: 2017WP/Met_Office-1_Mirek_XIOSread). WARNING: need to upgrade XIOS to r1296 to compile

  • Property svn:keywords set to Id
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      ! create the file name by added, if needed, TRIM(Agrif_CFixed()) and TRIM(clsuffix)
663      ! =============
664      clname   = trim(cdname)
665      IF ( .NOT. Agrif_Root() .AND. .NOT. lliof ) THEN
666         iln    = INDEX(clname,'/') 
667         cltmpn = clname(1:iln)
668         clname = clname(iln+1:LEN_TRIM(clname))
669         clname=TRIM(cltmpn)//TRIM(Agrif_CFixed())//'_'//TRIM(clname)
670      ENDIF
671      ! which suffix should we use?
672      SELECT CASE (iolib)
673      CASE (jpnf90   ) ;   clsuffix = '.nc'
674      CASE DEFAULT     ;   clsuffix = ''
675         CALL ctl_stop( TRIM(clinfo), 'accepted IO library is only jpnf90 (jpioipsl option has been removed) ' )
676      END SELECT
677      ! Add the suffix if needed
678      iln = LEN_TRIM(clname)
679      ils = LEN_TRIM(clsuffix)
680      IF( iln <= ils .OR. INDEX( TRIM(clname), TRIM(clsuffix), back = .TRUE. ) /= iln - ils + 1 )   &
681         &   clname = TRIM(clname)//TRIM(clsuffix)
682      cltmpn = clname   ! store this name
683      ! try to find if the file to be opened already exist
684      ! =============
685      INQUIRE( FILE = clname, EXIST = llok )
686      IF( .NOT.llok ) THEN
687         ! we try to add the cpu number to the name
688         WRITE(clcpu,*) narea-1
689
690         clcpu  = TRIM(ADJUSTL(clcpu))
691         iln = INDEX(clname,TRIM(clsuffix), back = .TRUE.)
692         clname = clname(1:iln-1)//'_'//TRIM(clcpu)//TRIM(clsuffix)
693         icnt = 0
694         INQUIRE( FILE = clname, EXIST = llok ) 
695         ! we try different formats for the cpu number by adding 0
696         DO WHILE( .NOT.llok .AND. icnt < jpmax_digits )
697            clcpu  = "0"//trim(clcpu)
698            clname = clname(1:iln-1)//'_'//TRIM(clcpu)//TRIM(clsuffix)
699            INQUIRE( FILE = clname, EXIST = llok )
700            icnt = icnt + 1
701         END DO
702      ELSE
703         lxios_sini = .TRUE.
704      ENDIF
705      IF( llwrt ) THEN
706         ! check the domain definition
707! JMM + SM: ugly patch before getting the new version of lib_mpp)
708!         idom = jpdom_local_noovlap   ! default definition
709         IF( llnoov ) THEN   ;   idom = jpdom_local_noovlap   ! default definition
710         ELSE                ;   idom = jpdom_local_full      ! default definition
711         ENDIF
712         IF( PRESENT(kdom) )   idom = kdom
713         ! create the domain informations
714         ! =============
715         SELECT CASE (idom)
716         CASE (jpdom_local_full)
717            idompar(:,1) = (/ jpi             , jpj              /)
718            idompar(:,2) = (/ nimpp           , njmpp            /)
719            idompar(:,3) = (/ nimpp + jpi - 1 , njmpp + jpj - 1  /)
720            idompar(:,4) = (/ nldi - 1        , nldj - 1         /)
721            idompar(:,5) = (/ jpi - nlei      , jpj - nlej       /)
722         CASE (jpdom_local_noextra)
723            idompar(:,1) = (/ nlci            , nlcj             /)
724            idompar(:,2) = (/ nimpp           , njmpp            /)
725            idompar(:,3) = (/ nimpp + nlci - 1, njmpp + nlcj - 1 /)
726            idompar(:,4) = (/ nldi - 1        , nldj - 1         /)
727            idompar(:,5) = (/ nlci - nlei     , nlcj - nlej      /)
728         CASE (jpdom_local_noovlap)
729            idompar(:,1) = (/ nlei  - nldi + 1, nlej  - nldj + 1 /)
730            idompar(:,2) = (/ nimpp + nldi - 1, njmpp + nldj - 1 /)
731            idompar(:,3) = (/ nimpp + nlei - 1, njmpp + nlej - 1 /)
732            idompar(:,4) = (/ 0               , 0                /)
733            idompar(:,5) = (/ 0               , 0                /)
734         CASE DEFAULT
735            CALL ctl_stop( TRIM(clinfo), 'wrong value of kdom, only jpdom_local* cases are accepted' )
736         END SELECT
737      ENDIF
738      ! Open the NetCDF file
739      ! =============
740      ! do we have some free file identifier?
741      IF( MINVAL(iom_file(:)%nfid) /= 0 )   &
742         &   CALL ctl_stop( TRIM(clinfo), 'No more free file identifier', 'increase jpmax_files in iom_def' )
743      ! if no file was found...
744      IF( .NOT. llok ) THEN
745         IF( .NOT. llwrt ) THEN   ! we are in read mode
746            IF( llstop ) THEN   ;   CALL ctl_stop( TRIM(clinfo), 'File '//TRIM(cltmpn)//'* not found' )
747            ELSE                ;   istop = nstop + 1   ! make sure that istop /= nstop so we don't open the file
748            ENDIF
749         ELSE                     ! we are in write mode so we
750            clname = cltmpn       ! get back the file name without the cpu number
751         ENDIF
752      ELSE
753         IF( llwrt .AND. .NOT. ln_clobber ) THEN   ! we stop as we want to write in a new file
754            CALL ctl_stop( TRIM(clinfo), 'We want to write in a new file but '//TRIM(clname)//' already exists...' )
755            istop = nstop + 1                      ! make sure that istop /= nstop so we don't open the file
756         ELSEIF( llwrt ) THEN     ! the file exists and we are in write mode with permission to
757            clname = cltmpn       ! overwrite so get back the file name without the cpu number
758         ENDIF
759      ENDIF
760      IF( istop == nstop ) THEN   ! no error within this routine
761         SELECT CASE (iolib)
762         CASE (jpnf90   )   ;   CALL iom_nf90_open(    clname, kiomid, llwrt, llok, idompar, kdlev = kdlev )
763         CASE DEFAULT
764            CALL ctl_stop( TRIM(clinfo)//' accepted IO library is only jpnf90 (jpioipsl option has been removed) ' )
765         END SELECT
766      ENDIF
767      !
768   END SUBROUTINE iom_open
769
770
771   SUBROUTINE iom_close( kiomid )
772      !!--------------------------------------------------------------------
773      !!                   ***  SUBROUTINE  iom_close  ***
774      !!
775      !! ** Purpose : close an input file, or all files opened by iom
776      !!--------------------------------------------------------------------
777      INTEGER, INTENT(inout), OPTIONAL ::   kiomid   ! iom identifier of the file to be closed
778      !                                              ! return 0 when file is properly closed
779      !                                              ! No argument: all files opened by iom are closed
780
781      INTEGER ::   jf         ! dummy loop indices
782      INTEGER ::   i_s, i_e   ! temporary integer
783      CHARACTER(LEN=100)    ::   clinfo    ! info character
784      !---------------------------------------------------------------------
785      !
786      clinfo = '                    iom_close ~~~  '
787      IF( PRESENT(kiomid) ) THEN
788         i_s = kiomid
789         i_e = kiomid
790      ELSE
791         i_s = 1
792         i_e = jpmax_files
793      ENDIF
794
795      IF( i_s > 0 ) THEN
796         DO jf = i_s, i_e
797            IF( iom_file(jf)%nfid > 0 ) THEN
798               SELECT CASE (iom_file(jf)%iolib)
799               CASE (jpnf90   )   ;   CALL iom_nf90_close(    jf )
800               CASE DEFAULT
801                  CALL ctl_stop( TRIM(clinfo)//' accepted IO library is only jpnf90 (jpioipsl option has been removed)' )
802               END SELECT
803               iom_file(jf)%nfid       = 0          ! free the id
804               IF( PRESENT(kiomid) )   kiomid = 0   ! return 0 as id to specify that the file was closed
805               IF(lwp) WRITE(numout,*) TRIM(clinfo)//' close file: '//TRIM(iom_file(jf)%name)//' ok'
806            ELSEIF( PRESENT(kiomid) ) THEN
807               WRITE(ctmp1,*) '--->',  kiomid
808               CALL ctl_stop( TRIM(clinfo)//' Invalid file identifier', ctmp1 )
809            ENDIF
810         END DO
811      ENDIF
812      !   
813   END SUBROUTINE iom_close
814
815
816   FUNCTION iom_varid ( kiomid, cdvar, kdimsz, kndims, ldstop ) 
817      !!-----------------------------------------------------------------------
818      !!                  ***  FUNCTION  iom_varid  ***
819      !!
820      !! ** Purpose : get the id of a variable in a file (return 0 if not found)
821      !!-----------------------------------------------------------------------
822      INTEGER              , INTENT(in   )           ::   kiomid   ! file Identifier
823      CHARACTER(len=*)     , INTENT(in   )           ::   cdvar    ! name of the variable
824      INTEGER, DIMENSION(:), INTENT(  out), OPTIONAL ::   kdimsz   ! size of the dimensions
825      INTEGER,               INTENT(  out), OPTIONAL ::   kndims   ! size of the dimensions
826      LOGICAL              , INTENT(in   ), OPTIONAL ::   ldstop   ! stop if looking for non-existing variable (default = .TRUE.)
827      !
828      INTEGER                        ::   iom_varid, iiv, i_nvd
829      LOGICAL                        ::   ll_fnd
830      CHARACTER(LEN=100)             ::   clinfo                   ! info character
831      LOGICAL                        ::   llstop                   ! local definition of ldstop
832      !!-----------------------------------------------------------------------
833      iom_varid = 0                         ! default definition
834      ! do we call ctl_stop if we look for non-existing variable?
835      IF( PRESENT(ldstop) ) THEN   ;   llstop = ldstop
836      ELSE                         ;   llstop = .TRUE.
837      ENDIF
838      !
839      IF( kiomid > 0 ) THEN
840         clinfo = 'iom_varid, file: '//trim(iom_file(kiomid)%name)//', var: '//trim(cdvar)
841         IF( iom_file(kiomid)%nfid == 0 ) THEN
842            CALL ctl_stop( trim(clinfo), 'the file is not open' )
843         ELSE
844            ll_fnd  = .FALSE.
845            iiv = 0
846            !
847            DO WHILE ( .NOT.ll_fnd .AND. iiv < iom_file(kiomid)%nvars )
848               iiv = iiv + 1
849               ll_fnd  = ( TRIM(cdvar) == TRIM(iom_file(kiomid)%cn_var(iiv)) )
850            END DO
851            !
852            IF( .NOT.ll_fnd ) THEN
853               iiv = iiv + 1
854               IF( iiv <= jpmax_vars ) THEN
855                  SELECT CASE (iom_file(kiomid)%iolib)
856                  CASE (jpnf90   )   ;   iom_varid = iom_nf90_varid  ( kiomid, cdvar, iiv, kdimsz, kndims )
857                  CASE DEFAULT
858                     CALL ctl_stop( TRIM(clinfo)//' accepted IO library is only jpnf90 (jpioipsl option has been removed)' )
859                  END SELECT
860               ELSE
861                  CALL ctl_stop( trim(clinfo), 'Too many variables in the file '//iom_file(kiomid)%name,   &
862                        &                         'increase the parameter jpmax_vars')
863               ENDIF
864               IF( llstop .AND. iom_varid == -1 )   CALL ctl_stop( TRIM(clinfo)//' not found' ) 
865            ELSE
866               iom_varid = iiv
867               IF( PRESENT(kdimsz) ) THEN
868                  i_nvd = iom_file(kiomid)%ndims(iiv)
869                  IF( i_nvd == size(kdimsz) ) THEN
870                     kdimsz(:) = iom_file(kiomid)%dimsz(1:i_nvd,iiv)
871                  ELSE
872                     WRITE(ctmp1,*) i_nvd, size(kdimsz)
873                     CALL ctl_stop( trim(clinfo), 'error in kdimsz size'//trim(ctmp1) )
874                  ENDIF
875               ENDIF
876               IF( PRESENT(kndims) )  kndims = iom_file(kiomid)%ndims(iiv)
877            ENDIF
878         ENDIF
879      ENDIF
880      !
881   END FUNCTION iom_varid
882
883
884   !!----------------------------------------------------------------------
885   !!                   INTERFACE iom_get
886   !!----------------------------------------------------------------------
887   SUBROUTINE iom_g0d( kiomid, cdvar, pvar, ktime, ldxios )
888      INTEGER         , INTENT(in   )                 ::   kiomid    ! Identifier of the file
889      CHARACTER(len=*), INTENT(in   )                 ::   cdvar     ! Name of the variable
890      REAL(wp)        , INTENT(  out)                 ::   pvar      ! read field
891      INTEGER         , INTENT(in   ),     OPTIONAL   ::   ktime     ! record number
892      LOGICAL         , INTENT(in   ),     OPTIONAL   ::   ldxios    ! use xios to read restart
893      !
894      INTEGER                                         ::   idvar     ! variable id
895      INTEGER                                         ::   idmspc    ! number of spatial dimensions
896      INTEGER         , DIMENSION(1)                  ::   itime     ! record number
897      CHARACTER(LEN=100)                              ::   clinfo    ! info character
898      CHARACTER(LEN=100)                              ::   clname    ! file name
899      CHARACTER(LEN=1)                                ::   cldmspc   !
900      LOGICAL                                         ::   llxios
901      !
902      llxios = .FALSE.
903      IF( PRESENT(ldxios) ) llxios = ldxios
904
905      IF(.NOT.llxios) THEN  ! read data using default library
906         itime = 1
907         IF( PRESENT(ktime) ) itime = ktime
908         !
909         clname = iom_file(kiomid)%name
910         clinfo = '          iom_g0d, file: '//trim(clname)//', var: '//trim(cdvar)
911         !
912         IF( kiomid > 0 ) THEN
913            idvar = iom_varid( kiomid, cdvar )
914            IF( iom_file(kiomid)%nfid > 0 .AND. idvar > 0 ) THEN
915               idmspc = iom_file ( kiomid )%ndims( idvar )
916               IF( iom_file(kiomid)%luld(idvar) )  idmspc = idmspc - 1
917               WRITE(cldmspc , fmt='(i1)') idmspc
918               IF( idmspc > 0 )  CALL ctl_stop( TRIM(clinfo), 'When reading to a 0D array, we do not accept data', &
919                                    &                         'with 1 or more spatial dimensions: '//cldmspc//' were found.' , &
920                                    &                         'Use ncwa -a to suppress the unnecessary dimensions' )
921               SELECT CASE (iom_file(kiomid)%iolib)
922               CASE (jpnf90   )   ;   CALL iom_nf90_get(    kiomid, idvar, pvar, itime )
923               CASE DEFAULT
924                  CALL ctl_stop( 'iom_g0d: accepted IO library is only jpnf90 (jpioipsl option has been removed)' )
925               END SELECT
926            ENDIF
927         ENDIF
928      ELSE
929         IF(lwp) WRITE(numout,*) 'XIOS RST READ (0D): ', trim(cdvar)
930         CALL iom_swap( TRIM(crxios_context) )
931         CALL xios_recv_field( trim(cdvar), pvar)
932         CALL iom_swap( TRIM(cxios_context) )
933      ENDIF
934   END SUBROUTINE iom_g0d
935
936   SUBROUTINE iom_g1d( kiomid, kdom, cdvar, pvar, ktime, kstart, kcount, ldxios )
937      INTEGER         , INTENT(in   )                         ::   kiomid    ! Identifier of the file
938      INTEGER         , INTENT(in   )                         ::   kdom      ! Type of domain to be read
939      CHARACTER(len=*), INTENT(in   )                         ::   cdvar     ! Name of the variable
940      REAL(wp)        , INTENT(  out), DIMENSION(:)           ::   pvar      ! read field
941      INTEGER         , INTENT(in   )              , OPTIONAL ::   ktime     ! record number
942      INTEGER         , INTENT(in   ), DIMENSION(1), OPTIONAL ::   kstart    ! start axis position of the reading
943      INTEGER         , INTENT(in   ), DIMENSION(1), OPTIONAL ::   kcount    ! number of points in each axis
944      LOGICAL         , INTENT(in   ),               OPTIONAL ::   ldxios    ! read data using XIOS
945      !
946      IF( kiomid > 0 ) THEN
947         IF( iom_file(kiomid)%nfid > 0 ) CALL iom_get_123d( kiomid, kdom       , cdvar        , pv_r1d=pvar,   &
948              &                                                     ktime=ktime, kstart=kstart, kcount=kcount, &
949              &                                                     ldxios=ldxios )
950      ENDIF
951   END SUBROUTINE iom_g1d
952
953   SUBROUTINE iom_g2d( kiomid, kdom, cdvar, pvar, ktime, kstart, kcount, lrowattr, ldxios)
954      INTEGER         , INTENT(in   )                           ::   kiomid    ! Identifier of the file
955      INTEGER         , INTENT(in   )                           ::   kdom      ! Type of domain to be read
956      CHARACTER(len=*), INTENT(in   )                           ::   cdvar     ! Name of the variable
957      REAL(wp)        , INTENT(  out), DIMENSION(:,:)           ::   pvar      ! read field
958      INTEGER         , INTENT(in   )                , OPTIONAL ::   ktime     ! record number
959      INTEGER         , INTENT(in   ), DIMENSION(2)  , OPTIONAL ::   kstart    ! start axis position of the reading
960      INTEGER         , INTENT(in   ), DIMENSION(2)  , OPTIONAL ::   kcount    ! number of points in each axis
961      LOGICAL         , INTENT(in   )                , OPTIONAL ::   lrowattr  ! logical flag telling iom_get to
962                                                                               ! look for and use a file attribute
963                                                                               ! called open_ocean_jstart to set the start
964                                                                               ! value for the 2nd dimension (netcdf only)
965      LOGICAL         , INTENT(in   ),               OPTIONAL ::   ldxios      ! read data using XIOS
966      !
967      IF( kiomid > 0 ) THEN
968         IF( iom_file(kiomid)%nfid > 0 ) CALL iom_get_123d( kiomid, kdom       , cdvar        , pv_r2d=pvar,   &
969              &                                                     ktime=ktime, kstart=kstart, kcount=kcount, &
970              &                                                     lrowattr=lrowattr,  ldxios=ldxios)
971      ENDIF
972   END SUBROUTINE iom_g2d
973
974   SUBROUTINE iom_g3d( kiomid, kdom, cdvar, pvar, ktime, kstart, kcount, lrowattr, ldxios )
975      INTEGER         , INTENT(in   )                             ::   kiomid    ! Identifier of the file
976      INTEGER         , INTENT(in   )                             ::   kdom      ! Type of domain to be read
977      CHARACTER(len=*), INTENT(in   )                             ::   cdvar     ! Name of the variable
978      REAL(wp)        , INTENT(  out), DIMENSION(:,:,:)           ::   pvar      ! read field
979      INTEGER         , INTENT(in   )                  , OPTIONAL ::   ktime     ! record number
980      INTEGER         , INTENT(in   ), DIMENSION(3)    , OPTIONAL ::   kstart    ! start axis position of the reading
981      INTEGER         , INTENT(in   ), DIMENSION(3)    , OPTIONAL ::   kcount    ! number of points in each axis
982      LOGICAL         , INTENT(in   )                  , OPTIONAL ::   lrowattr  ! logical flag telling iom_get to
983                                                                                 ! look for and use a file attribute
984                                                                                 ! called open_ocean_jstart to set the start
985                                                                                 ! value for the 2nd dimension (netcdf only)
986      LOGICAL         , INTENT(in   ),               OPTIONAL ::   ldxios        ! read data using XIOS
987      !
988      IF( kiomid > 0 ) THEN
989         IF( iom_file(kiomid)%nfid > 0 ) CALL iom_get_123d( kiomid, kdom       , cdvar        , pv_r3d=pvar,   &
990              &                                                     ktime=ktime, kstart=kstart, kcount=kcount, &
991              &                                                     lrowattr=lrowattr, ldxios=ldxios )
992      ENDIF
993   END SUBROUTINE iom_g3d
994   !!----------------------------------------------------------------------
995
996   SUBROUTINE iom_get_123d( kiomid, kdom  , cdvar ,   &
997         &                  pv_r1d, pv_r2d, pv_r3d,   &
998         &                  ktime , kstart, kcount,   &
999         &                  lrowattr, ldxios        )
1000      !!-----------------------------------------------------------------------
1001      !!                  ***  ROUTINE  iom_get_123d  ***
1002      !!
1003      !! ** Purpose : read a 1D/2D/3D variable
1004      !!
1005      !! ** Method : read ONE record at each CALL
1006      !!-----------------------------------------------------------------------
1007      INTEGER                    , INTENT(in   )           ::   kiomid     ! Identifier of the file
1008      INTEGER                    , INTENT(in   )           ::   kdom       ! Type of domain to be read
1009      CHARACTER(len=*)           , INTENT(in   )           ::   cdvar      ! Name of the variable
1010      REAL(wp), DIMENSION(:)     , INTENT(  out), OPTIONAL ::   pv_r1d     ! read field (1D case)
1011      REAL(wp), DIMENSION(:,:)   , INTENT(  out), OPTIONAL ::   pv_r2d     ! read field (2D case)
1012      REAL(wp), DIMENSION(:,:,:) , INTENT(  out), OPTIONAL ::   pv_r3d     ! read field (3D case)
1013      INTEGER                    , INTENT(in   ), OPTIONAL ::   ktime      ! record number
1014      INTEGER , DIMENSION(:)     , INTENT(in   ), OPTIONAL ::   kstart     ! start position of the reading in each axis
1015      INTEGER , DIMENSION(:)     , INTENT(in   ), OPTIONAL ::   kcount     ! number of points to be read in each axis
1016      LOGICAL                    , INTENT(in   ), OPTIONAL ::   lrowattr   ! logical flag telling iom_get to
1017                                                                           ! look for and use a file attribute
1018                                                                           ! called open_ocean_jstart to set the start
1019                                                                           ! value for the 2nd dimension (netcdf only)
1020      LOGICAL                    , INTENT(in   ), OPTIONAL ::   ldxios     ! use XIOS to read restart
1021      !
1022      LOGICAL                        ::   llxios       ! local definition for XIOS read
1023      LOGICAL                        ::   llnoov      ! local definition to read overlap
1024      LOGICAL                        ::   luse_jattr  ! local definition to read open_ocean_jstart file attribute
1025      INTEGER                        ::   jstartrow   ! start point for 2nd dimension optionally set by file attribute
1026      INTEGER                        ::   jl          ! loop on number of dimension
1027      INTEGER                        ::   idom        ! type of domain
1028      INTEGER                        ::   idvar       ! id of the variable
1029      INTEGER                        ::   inbdim      ! number of dimensions of the variable
1030      INTEGER                        ::   idmspc      ! number of spatial dimensions
1031      INTEGER                        ::   itime       ! record number
1032      INTEGER                        ::   istop       ! temporary value of nstop
1033      INTEGER                        ::   ix1, ix2, iy1, iy2   ! subdomain indexes
1034      INTEGER                        ::   ji, jj      ! loop counters
1035      INTEGER                        ::   irankpv     !
1036      INTEGER                        ::   ind1, ind2  ! substring index
1037      INTEGER, DIMENSION(jpmax_dims) ::   istart      ! starting point to read for each axis
1038      INTEGER, DIMENSION(jpmax_dims) ::   icnt        ! number of value to read along each axis
1039      INTEGER, DIMENSION(jpmax_dims) ::   idimsz      ! size of the dimensions of the variable
1040      INTEGER, DIMENSION(jpmax_dims) ::   ishape      ! size of the dimensions of the variable
1041      REAL(wp)                       ::   zscf, zofs  ! sacle_factor and add_offset
1042      INTEGER                        ::   itmp        ! temporary integer
1043      CHARACTER(LEN=256)             ::   clinfo      ! info character
1044      CHARACTER(LEN=256)             ::   clname      ! file name
1045      CHARACTER(LEN=1)               ::   clrankpv, cldmspc      !
1046      LOGICAL                        ::   ll_depth_spec ! T => if kstart, kcount present then *only* use values for 3rd spatial dimension.
1047      INTEGER                        ::   inlev       ! number of levels for 3D data
1048      REAL(wp)                       ::   gma, gmi
1049      !---------------------------------------------------------------------
1050      !
1051      inlev = -1
1052      IF( PRESENT(pv_r3d) )   inlev = SIZE(pv_r3d, 3)
1053      !
1054      llxios = .FALSE.
1055      if(PRESENT(ldxios)) llxios = ldxios
1056      idvar = iom_varid( kiomid, cdvar ) 
1057      idom = kdom
1058      !
1059      IF(.NOT.llxios) THEN
1060         clname = iom_file(kiomid)%name   !   esier to read
1061         clinfo = '          iom_get_123d, file: '//trim(clname)//', var: '//trim(cdvar)
1062         ! local definition of the domain ?
1063         ! do we read the overlap
1064         ! ugly patch SM+JMM+RB to overwrite global definition in some cases
1065         llnoov = (jpni * jpnj ) == jpnij .AND. .NOT. lk_agrif 
1066         ! check kcount and kstart optionals parameters...
1067         IF( PRESENT(kcount) .AND. (.NOT. PRESENT(kstart)) ) CALL ctl_stop(trim(clinfo), 'kcount present needs kstart present')
1068         IF( PRESENT(kstart) .AND. (.NOT. PRESENT(kcount)) ) CALL ctl_stop(trim(clinfo), 'kstart present needs kcount present')
1069         IF( PRESENT(kstart) .AND. idom /= jpdom_unknown .AND.  idom /= jpdom_autoglo_xy  ) &
1070     &          CALL ctl_stop(trim(clinfo), 'kstart present needs kdom = jpdom_unknown or kdom = jpdom_autoglo_xy')
1071
1072         luse_jattr = .false.
1073         IF( PRESENT(lrowattr) ) THEN
1074            IF( lrowattr .AND. idom /= jpdom_data   ) CALL ctl_stop(trim(clinfo), 'lrowattr present and true needs kdom = jpdom_data')
1075            IF( lrowattr .AND. idom == jpdom_data   ) luse_jattr = .true.
1076         ENDIF
1077         IF( luse_jattr ) THEN
1078            SELECT CASE (iom_file(kiomid)%iolib)
1079            CASE (jpnf90   )   
1080                ! Ok
1081            CASE DEFAULT   
1082               CALL ctl_stop( TRIM(clinfo)//' accepted IO library is only jpnf90 (jpioipsl option has been removed)' )
1083            END SELECT
1084         ENDIF
1085
1086         ! Search for the variable in the data base (eventually actualize data)
1087         istop = nstop
1088         !
1089         IF( idvar > 0 ) THEN
1090            ! to write iom_file(kiomid)%dimsz in a shorter way !
1091            idimsz(:) = iom_file(kiomid)%dimsz(:, idvar) 
1092            inbdim = iom_file(kiomid)%ndims(idvar)            ! number of dimensions in the file
1093            idmspc = inbdim                                   ! number of spatial dimensions in the file
1094            IF( iom_file(kiomid)%luld(idvar) )   idmspc = inbdim - 1
1095            IF( idmspc > 3 )   CALL ctl_stop(trim(clinfo), 'the file has more than 3 spatial dimensions this case is not coded...') 
1096            !
1097            ! update idom definition...
1098            ! Identify the domain in case of jpdom_auto(glo/dta) definition
1099            IF( idom == jpdom_autoglo_xy ) THEN
1100               ll_depth_spec = .TRUE.
1101               idom = jpdom_autoglo
1102            ELSE
1103               ll_depth_spec = .FALSE.
1104            ENDIF
1105            IF( idom == jpdom_autoglo .OR. idom == jpdom_autodta ) THEN           
1106               IF( idom == jpdom_autoglo ) THEN   ;   idom = jpdom_global 
1107               ELSE                               ;   idom = jpdom_data
1108               ENDIF
1109               ind1 = INDEX( clname, '_', back = .TRUE. ) + 1
1110               ind2 = INDEX( clname, '.', back = .TRUE. ) - 1
1111               IF( ind2 > ind1 ) THEN   ;   IF( VERIFY( clname(ind1:ind2), '0123456789' ) == 0 )   idom = jpdom_local   ;   ENDIF
1112            ENDIF
1113            ! Identify the domain in case of jpdom_local definition
1114            IF( idom == jpdom_local ) THEN
1115               IF(     idimsz(1) == jpi               .AND. idimsz(2) == jpj               ) THEN   ;   idom = jpdom_local_full
1116               ELSEIF( idimsz(1) == nlci              .AND. idimsz(2) == nlcj              ) THEN   ;   idom = jpdom_local_noextra
1117               ELSEIF( idimsz(1) == (nlei - nldi + 1) .AND. idimsz(2) == (nlej - nldj + 1) ) THEN   ;   idom = jpdom_local_noovlap
1118               ELSE   ;   CALL ctl_stop( trim(clinfo), 'impossible to identify the local domain' )
1119               ENDIF
1120            ENDIF
1121            !
1122            ! check the consistency between input array and data rank in the file
1123            !
1124            ! initializations
1125            itime = 1
1126            IF( PRESENT(ktime) ) itime = ktime
1127            !
1128            irankpv = 1 * COUNT( (/PRESENT(pv_r1d)/) ) + 2 * COUNT( (/PRESENT(pv_r2d)/) ) + 3 * COUNT( (/PRESENT(pv_r3d)/) )
1129            WRITE(clrankpv, fmt='(i1)') irankpv
1130            WRITE(cldmspc , fmt='(i1)') idmspc
1131            !
1132            IF(     idmspc <  irankpv ) THEN
1133               CALL ctl_stop( TRIM(clinfo), 'The file has only '//cldmspc//' spatial dimension',   &
1134                  &                         'it is impossible to read a '//clrankpv//'D array from this file...' )
1135            ELSEIF( idmspc == irankpv ) THEN
1136               IF( PRESENT(pv_r1d) .AND. idom /= jpdom_unknown )   &
1137                  &   CALL ctl_stop( TRIM(clinfo), 'case not coded...You must use jpdom_unknown' )
1138            ELSEIF( idmspc >  irankpv ) THEN
1139                  IF( PRESENT(pv_r2d) .AND. itime == 1 .AND. idimsz(3) == 1 .AND. idmspc == 3 ) THEN
1140                     CALL ctl_warn( trim(clinfo), '2D array but 3 spatial dimensions for the data...'              ,   &
1141                           &         'As the size of the z dimension is 1 and as we try to read the first record, ',   &
1142                           &         'we accept this case, even if there is a possible mix-up between z and time dimension' )   
1143                     idmspc = idmspc - 1
1144                  ELSE
1145                     CALL ctl_stop( TRIM(clinfo), 'To keep iom lisibility, when reading a '//clrankpv//'D array,'         ,   &
1146                        &                         'we do not accept data with '//cldmspc//' spatial dimensions',   &
1147                        &                         'Use ncwa -a to suppress the unnecessary dimensions' )
1148                  ENDIF
1149            ENDIF
1150            !
1151            ! definition of istart and icnt
1152            !
1153            icnt  (:) = 1
1154            istart(:) = 1
1155            istart(idmspc+1) = itime
1156   
1157            IF( PRESENT(kstart) .AND. .NOT. ll_depth_spec ) THEN
1158               istart(1:idmspc) = kstart(1:idmspc) 
1159               icnt  (1:idmspc) = kcount(1:idmspc)
1160            ELSE
1161               IF(idom == jpdom_unknown ) THEN
1162                  icnt(1:idmspc) = idimsz(1:idmspc)
1163               ELSE
1164                  IF( .NOT. PRESENT(pv_r1d) ) THEN   !   not a 1D array
1165                     IF(     idom == jpdom_data    ) THEN
1166                        jstartrow = 1
1167                        IF( luse_jattr ) THEN
1168                           CALL iom_getatt(kiomid, 'open_ocean_jstart', jstartrow ) ! -999 is returned if the attribute is not found
1169                           jstartrow = MAX(1,jstartrow)
1170                        ENDIF
1171                        istart(1:2) = (/ mig(1), mjg(1) + jstartrow - 1 /)  ! icnt(1:2) done below
1172                     ELSEIF( idom == jpdom_global  ) THEN ; istart(1:2) = (/ nimpp , njmpp  /)  ! icnt(1:2) done below
1173                     ENDIF
1174                     ! we do not read the overlap                     -> we start to read at nldi, nldj
1175! JMM + SM: ugly patch before getting the new version of lib_mpp)
1176!                  IF( idom /= jpdom_local_noovlap )   istart(1:2) = istart(1:2) + (/ nldi - 1, nldj - 1 /)
1177                     IF( llnoov .AND. idom /= jpdom_local_noovlap ) istart(1:2) = istart(1:2) + (/ nldi - 1, nldj - 1 /)
1178                  ! we do not read the overlap and the extra-halos -> from nldi to nlei and from nldj to nlej
1179! JMM + SM: ugly patch before getting the new version of lib_mpp)
1180!                  icnt(1:2) = (/ nlei - nldi + 1, nlej - nldj + 1 /)
1181                     IF( llnoov ) THEN   ;   icnt(1:2) = (/ nlei - nldi + 1, nlej - nldj + 1 /)
1182                     ELSE                ;   icnt(1:2) = (/ nlci           , nlcj            /)
1183                     ENDIF
1184                     IF( PRESENT(pv_r3d) ) THEN
1185                        IF( idom == jpdom_data ) THEN                        ;                               icnt(3) = inlev
1186                        ELSEIF( ll_depth_spec .AND. PRESENT(kstart) ) THEN   ;   istart(3) = kstart(3)   ;   icnt(3) = kcount(3)
1187                        ELSE                                                 ;                               icnt(3) = inlev
1188                        ENDIF
1189                     ENDIF
1190                  ENDIF
1191               ENDIF
1192            ENDIF
1193
1194            ! check that istart and icnt can be used with this file
1195            !-
1196            DO jl = 1, jpmax_dims
1197               itmp = istart(jl)+icnt(jl)-1
1198               IF( itmp > idimsz(jl) .AND. idimsz(jl) /= 0 ) THEN
1199                  WRITE( ctmp1, FMT="('(istart(', i1, ') + icnt(', i1, ') - 1) = ', i5)" ) jl, jl, itmp
1200                  WRITE( ctmp2, FMT="(' is larger than idimsz(', i1,') = ', i5)"         ) jl, idimsz(jl)
1201                  CALL ctl_stop( trim(clinfo), 'start and count too big regarding to the size of the data, ', ctmp1, ctmp2 )     
1202               ENDIF
1203            END DO
1204
1205            ! check that icnt matches the input array
1206            !-     
1207            IF( idom == jpdom_unknown ) THEN
1208               IF( irankpv == 1 )        ishape(1:1) = SHAPE(pv_r1d)
1209               IF( irankpv == 2 )        ishape(1:2) = SHAPE(pv_r2d)
1210               IF( irankpv == 3 )        ishape(1:3) = SHAPE(pv_r3d)
1211               ctmp1 = 'd'
1212            ELSE
1213               IF( irankpv == 2 ) THEN
1214! JMM + SM: ugly patch before getting the new version of lib_mpp)
1215!               ishape(1:2) = SHAPE(pv_r2d(nldi:nlei,nldj:nlej  ))   ;   ctmp1 = 'd(nldi:nlei,nldj:nlej)'
1216                  IF( llnoov ) THEN ; ishape(1:2)=SHAPE(pv_r2d(nldi:nlei,nldj:nlej  )) ; ctmp1='d(nldi:nlei,nldj:nlej)'
1217                  ELSE              ; ishape(1:2)=SHAPE(pv_r2d(1   :nlci,1   :nlcj  )) ; ctmp1='d(1:nlci,1:nlcj)'
1218                  ENDIF
1219               ENDIF
1220               IF( irankpv == 3 ) THEN 
1221! JMM + SM: ugly patch before getting the new version of lib_mpp)
1222!               ishape(1:3) = SHAPE(pv_r3d(nldi:nlei,nldj:nlej,:))   ;   ctmp1 = 'd(nldi:nlei,nldj:nlej,:)'
1223                  IF( llnoov ) THEN ; ishape(1:3)=SHAPE(pv_r3d(nldi:nlei,nldj:nlej,:)) ; ctmp1='d(nldi:nlei,nldj:nlej,:)'
1224                  ELSE              ; ishape(1:3)=SHAPE(pv_r3d(1   :nlci,1   :nlcj,:)) ; ctmp1='d(1:nlci,1:nlcj,:)'
1225                  ENDIF
1226               ENDIF
1227            ENDIF
1228         
1229            DO jl = 1, irankpv
1230               WRITE( ctmp2, FMT="(', ', i1,'): ', i5,' /= icnt(', i1,'):', i5)" ) jl, ishape(jl), jl, icnt(jl)
1231               IF( ishape(jl) /= icnt(jl) )   CALL ctl_stop( TRIM(clinfo), 'size(pv_r'//clrankpv//TRIM(ctmp1)//TRIM(ctmp2) )
1232            END DO
1233
1234         ENDIF
1235
1236         ! read the data
1237         !-     
1238         IF( idvar > 0 .AND. istop == nstop ) THEN   ! no additional errors until this point...
1239            !
1240         ! find the right index of the array to be read
1241! JMM + SM: ugly patch before getting the new version of lib_mpp)
1242!         IF( idom /= jpdom_unknown ) THEN   ;   ix1 = nldi   ;   ix2 = nlei      ;   iy1 = nldj   ;   iy2 = nlej
1243!         ELSE                               ;   ix1 = 1      ;   ix2 = icnt(1)   ;   iy1 = 1      ;   iy2 = icnt(2)
1244!         ENDIF
1245            IF( llnoov ) THEN
1246               IF( idom /= jpdom_unknown ) THEN   ;   ix1 = nldi   ;   ix2 = nlei      ;   iy1 = nldj   ;   iy2 = nlej
1247               ELSE                               ;   ix1 = 1      ;   ix2 = icnt(1)   ;   iy1 = 1      ;   iy2 = icnt(2)
1248               ENDIF
1249            ELSE
1250               IF( idom /= jpdom_unknown ) THEN   ;   ix1 = 1      ;   ix2 = nlci      ;   iy1 = 1      ;   iy2 = nlcj
1251               ELSE                               ;   ix1 = 1      ;   ix2 = icnt(1)   ;   iy1 = 1      ;   iy2 = icnt(2)
1252               ENDIF
1253            ENDIF
1254     
1255            SELECT CASE (iom_file(kiomid)%iolib)
1256            CASE (jpnf90   )   ;   CALL iom_nf90_get(    kiomid, idvar, inbdim, istart, icnt, ix1, ix2, iy1, iy2,   &
1257               &                                         pv_r1d, pv_r2d, pv_r3d )
1258            CASE DEFAULT
1259               CALL ctl_stop( TRIM(clinfo)//' accepted IO library is only jpnf90 (jpioipsl option has been removed)' )
1260            END SELECT
1261
1262            IF( istop == nstop ) THEN   ! no additional errors until this point...
1263               IF(lwp) WRITE(numout,"(10x,' read ',a,' (rec: ',i6,') in ',a,' ok')") TRIM(cdvar), itime, TRIM(iom_file(kiomid)%name)
1264             
1265               !--- overlap areas and extra hallows (mpp)
1266               IF(     PRESENT(pv_r2d) .AND. idom /= jpdom_unknown ) THEN
1267                  CALL lbc_lnk( pv_r2d,'Z',-999.,'no0' )
1268               ELSEIF( PRESENT(pv_r3d) .AND. idom /= jpdom_unknown ) THEN
1269                  ! this if could be simplified with the new lbc_lnk that works with any size of the 3rd dimension
1270                  IF( icnt(3) == inlev ) THEN
1271                     CALL lbc_lnk( pv_r3d,'Z',-999.,'no0' )
1272                  ELSE   ! put some arbitrary value (a call to lbc_lnk will be done later...)
1273                     DO jj = nlcj+1, jpj   ;   pv_r3d(1:nlci, jj, :) = pv_r3d(1:nlci, nlej, :)   ;   END DO
1274                     DO ji = nlci+1, jpi   ;   pv_r3d(ji    , : , :) = pv_r3d(nlei  , :   , :)   ;   END DO
1275                  ENDIF
1276               ENDIF
1277               !
1278            ELSE
1279               ! return if istop == nstop is false
1280               RETURN
1281            ENDIF
1282         ELSE
1283            ! return if statment idvar > 0 .AND. istop == nstop is false
1284            RETURN
1285         ENDIF
1286         !
1287      ELSE        ! read using XIOS. Only if KEY_IOMPUT is defined
1288#if defined key_iomput
1289!would be good to be able to check which context is active and swap only if current is not restart
1290         CALL iom_swap( TRIM(crxios_context) ) 
1291         IF( PRESENT(pv_r3d) ) THEN
1292            if(lwp) write(numout,*) 'XIOS RST READ (3D): ',trim(cdvar)
1293            CALL xios_recv_field( trim(cdvar), pv_r3d)
1294            IF(idom /= jpdom_unknown ) then
1295                CALL lbc_lnk( pv_r3d,'Z',-999.,'no0' )
1296            ENDIF
1297         ELSEIF( PRESENT(pv_r2d) ) THEN
1298            if(lwp) write(numout,*) 'XIOS RST READ (2D): ', trim(cdvar)
1299            CALL xios_recv_field( trim(cdvar), pv_r2d)
1300            IF(idom /= jpdom_unknown ) THEN
1301                CALL lbc_lnk(pv_r2d,'Z',-999.,'no0')
1302            ENDIF
1303         ELSEIF( PRESENT(pv_r1d) ) THEN
1304            if(lwp) write(numout,*) 'XIOS RST READ (1D): ', trim(cdvar)
1305            CALL xios_recv_field( trim(cdvar), pv_r1d)
1306         ENDIF
1307         CALL iom_swap( TRIM(cxios_context) )
1308#else
1309         istop = istop + 1 
1310         clinfo = 'Can not use XIOS in iom_get_123d, file: '//trim(clname)//', var:'//trim(cdvar)
1311#endif
1312      ENDIF
1313!some final adjustments
1314      ! C1D case : always call lbc_lnk to replicate the central value over the whole 3X3 domain
1315      IF( lk_c1d .AND. PRESENT(pv_r2d) )   CALL lbc_lnk( pv_r2d,'Z',1. )
1316      IF( lk_c1d .AND. PRESENT(pv_r3d) )   CALL lbc_lnk( pv_r3d,'Z',1. )
1317
1318      !--- Apply scale_factor and offset
1319      zscf = iom_file(kiomid)%scf(idvar)      ! scale factor
1320      zofs = iom_file(kiomid)%ofs(idvar)      ! offset
1321      IF(     PRESENT(pv_r1d) ) THEN
1322         IF( zscf /= 1. )   pv_r1d(:) = pv_r1d(:) * zscf 
1323         IF( zofs /= 0. )   pv_r1d(:) = pv_r1d(:) + zofs
1324      ELSEIF( PRESENT(pv_r2d) ) THEN
1325         IF( zscf /= 1.)   pv_r2d(:,:) = pv_r2d(:,:) * zscf
1326         IF( zofs /= 0.)   pv_r2d(:,:) = pv_r2d(:,:) + zofs
1327      ELSEIF( PRESENT(pv_r3d) ) THEN
1328         IF( zscf /= 1.)   pv_r3d(:,:,:) = pv_r3d(:,:,:) * zscf
1329         IF( zofs /= 0.)   pv_r3d(:,:,:) = pv_r3d(:,:,:) + zofs
1330      ENDIF
1331      !
1332   END SUBROUTINE iom_get_123d
1333
1334
1335   SUBROUTINE iom_gettime( kiomid, ptime, cdvar, kntime, cdunits, cdcalendar )
1336      !!--------------------------------------------------------------------
1337      !!                   ***  SUBROUTINE iom_gettime  ***
1338      !!
1339      !! ** Purpose : read the time axis cdvar in the file
1340      !!--------------------------------------------------------------------
1341      INTEGER                    , INTENT(in   ) ::   kiomid     ! file Identifier
1342      REAL(wp), DIMENSION(:)     , INTENT(  out) ::   ptime      ! the time axis
1343      CHARACTER(len=*), OPTIONAL , INTENT(in   ) ::   cdvar      ! time axis name
1344      INTEGER         , OPTIONAL , INTENT(  out) ::   kntime     ! number of times in file
1345      CHARACTER(len=*), OPTIONAL , INTENT(  out) ::   cdunits    ! units attribute of time coordinate
1346      CHARACTER(len=*), OPTIONAL , INTENT(  out) ::   cdcalendar ! calendar attribute of
1347      !
1348      INTEGER, DIMENSION(1) :: kdimsz
1349      INTEGER            ::   idvar    ! id of the variable
1350      CHARACTER(LEN=32)  ::   tname    ! local name of time coordinate
1351      CHARACTER(LEN=100) ::   clinfo   ! info character
1352      !---------------------------------------------------------------------
1353      !
1354      IF ( PRESENT(cdvar) ) THEN
1355         tname = cdvar
1356      ELSE
1357         tname = iom_file(kiomid)%uldname
1358      ENDIF
1359      IF( kiomid > 0 ) THEN
1360         clinfo = 'iom_gettime, file: '//trim(iom_file(kiomid)%name)//', var: '//trim(tname)
1361         IF ( PRESENT(kntime) ) THEN
1362            idvar  = iom_varid( kiomid, tname, kdimsz = kdimsz )
1363            kntime = kdimsz(1)
1364         ELSE
1365            idvar = iom_varid( kiomid, tname )
1366         ENDIF
1367         !
1368         ptime(:) = 0. ! default definition
1369         IF( idvar > 0 ) THEN
1370            IF( iom_file(kiomid)%ndims(idvar) == 1 ) THEN
1371               IF( iom_file(kiomid)%luld(idvar) ) THEN
1372                  IF( iom_file(kiomid)%dimsz(1,idvar) <= size(ptime) ) THEN
1373                     SELECT CASE (iom_file(kiomid)%iolib)
1374                     CASE (jpnf90   )   ;   CALL iom_nf90_gettime(   kiomid, idvar, ptime, cdunits, cdcalendar )
1375                     CASE DEFAULT
1376                        CALL ctl_stop( TRIM(clinfo)//' accepted IO library is only jpnf90 (jpioipsl option has been removed)' )
1377                     END SELECT
1378                  ELSE
1379                     WRITE(ctmp1,*) 'error with the size of ptime ',size(ptime),iom_file(kiomid)%dimsz(1,idvar)
1380                     CALL ctl_stop( trim(clinfo), trim(ctmp1) )
1381                  ENDIF
1382               ELSE
1383                  CALL ctl_stop( trim(clinfo), 'variable dimension is not unlimited... use iom_get' )
1384               ENDIF
1385            ELSE
1386               CALL ctl_stop( trim(clinfo), 'the variable has more than 1 dimension' )
1387            ENDIF
1388         ELSE
1389            CALL ctl_stop( trim(clinfo), 'variable not found in '//iom_file(kiomid)%name )
1390         ENDIF
1391      ENDIF
1392      !
1393   END SUBROUTINE iom_gettime
1394
1395
1396   !!----------------------------------------------------------------------
1397   !!                   INTERFACE iom_getatt
1398   !!----------------------------------------------------------------------
1399   SUBROUTINE iom_g0d_iatt( kiomid, cdatt, pvar, cdvar )
1400      INTEGER         , INTENT(in   )                 ::   kiomid    ! Identifier of the file
1401      CHARACTER(len=*), INTENT(in   )                 ::   cdatt     ! Name of the attribute
1402      INTEGER         , INTENT(  out)                 ::   pvar      ! read field
1403      CHARACTER(len=*), INTENT(in   ), OPTIONAL       ::   cdvar     ! Name of the variable
1404      !
1405      IF( kiomid > 0 ) THEN
1406         IF( iom_file(kiomid)%nfid > 0 ) THEN
1407            SELECT CASE (iom_file(kiomid)%iolib)
1408            CASE (jpnf90   )   ;   IF( PRESENT(cdvar) ) THEN
1409                                      CALL iom_nf90_getatt( kiomid, cdatt, pvar, cdvar=cdvar )
1410                                   ELSE
1411                                      CALL iom_nf90_getatt( kiomid, cdatt, pvar )
1412                                   ENDIF
1413            CASE DEFAULT
1414               CALL ctl_stop( 'iom_g0d_iatt: accepted IO library is only jpnf90' )
1415            END SELECT
1416         ENDIF
1417      ENDIF
1418   END SUBROUTINE iom_g0d_iatt
1419
1420   SUBROUTINE iom_g0d_ratt( kiomid, cdatt, pvar, cdvar )
1421      INTEGER         , INTENT(in   )                 ::   kiomid    ! Identifier of the file
1422      CHARACTER(len=*), INTENT(in   )                 ::   cdatt     ! Name of the attribute
1423      REAL(wp)        , INTENT(  out)                 ::   pvar      ! written field
1424      CHARACTER(len=*), INTENT(in   ), OPTIONAL       ::   cdvar     ! Name of the variable
1425      !
1426      IF( kiomid > 0 ) THEN
1427         IF( iom_file(kiomid)%nfid > 0 ) THEN
1428            SELECT CASE (iom_file(kiomid)%iolib)
1429            CASE (jpnf90   )   ;   IF( PRESENT(cdvar) ) THEN
1430                                      CALL iom_nf90_getatt( kiomid, cdatt, pvar, cdvar=cdvar )
1431                                   ELSE
1432                                      CALL iom_nf90_getatt( kiomid, cdatt, pvar )
1433                                   ENDIF
1434            CASE DEFAULT   
1435               CALL ctl_stop( 'iom_g0d_ratt: accepted IO library is only jpnf90' )
1436            END SELECT
1437         ENDIF
1438      ENDIF
1439   END SUBROUTINE iom_g0d_ratt
1440
1441   SUBROUTINE iom_g0d_catt( kiomid, cdatt, pvar, cdvar )
1442      INTEGER         , INTENT(in   )                 ::   kiomid    ! Identifier of the file
1443      CHARACTER(len=*), INTENT(in   )                 ::   cdatt     ! Name of the attribute
1444      CHARACTER(len=*), INTENT(  out)                 ::   pvar      ! written field
1445      CHARACTER(len=*), INTENT(in   ), OPTIONAL       ::   cdvar     ! Name of the variable
1446      !
1447      IF( kiomid > 0 ) THEN
1448         IF( iom_file(kiomid)%nfid > 0 ) THEN
1449            SELECT CASE (iom_file(kiomid)%iolib)
1450            CASE (jpnf90   )   ;   IF( PRESENT(cdvar) ) THEN
1451                                      CALL iom_nf90_getatt( kiomid, cdatt, pvar, cdvar=cdvar )
1452                                   ELSE
1453                                      CALL iom_nf90_getatt( kiomid, cdatt, pvar )
1454                                   ENDIF
1455            CASE DEFAULT
1456               CALL ctl_stop( 'iom_g0d_ratt: accepted IO library is only jpnf90' )
1457            END SELECT
1458         ENDIF
1459      ENDIF
1460   END SUBROUTINE iom_g0d_catt
1461
1462   !!----------------------------------------------------------------------
1463   !!                   INTERFACE iom_putatt
1464   !!----------------------------------------------------------------------
1465   SUBROUTINE iom_p0d_iatt( kiomid, cdatt, pvar, cdvar )
1466      INTEGER         , INTENT(in   )                 ::   kiomid    ! Identifier of the file
1467      CHARACTER(len=*), INTENT(in   )                 ::   cdatt     ! Name of the attribute
1468      INTEGER         , INTENT(in   )                 ::   pvar      ! write field
1469      CHARACTER(len=*), INTENT(in   ), OPTIONAL       ::   cdvar     ! Name of the variable
1470      !
1471      IF( kiomid > 0 ) THEN
1472         IF( iom_file(kiomid)%nfid > 0 ) THEN
1473            SELECT CASE (iom_file(kiomid)%iolib)
1474            CASE (jpnf90   )   ;   IF( PRESENT(cdvar) ) THEN
1475                                      CALL iom_nf90_putatt( kiomid, cdatt, pvar, cdvar=cdvar )
1476                                   ELSE
1477                                      CALL iom_nf90_putatt( kiomid, cdatt, pvar )
1478                                   ENDIF
1479            CASE DEFAULT
1480               CALL ctl_stop( 'iom_p0d_iatt: accepted IO library is only jpnf90' )
1481            END SELECT
1482         ENDIF
1483      ENDIF
1484   END SUBROUTINE iom_p0d_iatt
1485
1486   SUBROUTINE iom_p0d_ratt( kiomid, cdatt, pvar, cdvar )
1487      INTEGER         , INTENT(in   )                 ::   kiomid    ! Identifier of the file
1488      CHARACTER(len=*), INTENT(in   )                 ::   cdatt     ! Name of the attribute
1489      REAL(wp)        , INTENT(in   )                 ::   pvar      ! write field
1490      CHARACTER(len=*), INTENT(in   ), OPTIONAL       ::   cdvar     ! Name of the variable
1491      !
1492      IF( kiomid > 0 ) THEN
1493         IF( iom_file(kiomid)%nfid > 0 ) THEN
1494            SELECT CASE (iom_file(kiomid)%iolib)
1495            CASE (jpnf90   )   ;   IF( PRESENT(cdvar) ) THEN
1496                                      CALL iom_nf90_putatt( kiomid, cdatt, pvar, cdvar=cdvar )
1497                                   ELSE
1498                                      CALL iom_nf90_putatt( kiomid, cdatt, pvar )
1499                                   ENDIF
1500            CASE DEFAULT   
1501               CALL ctl_stop( 'iom_p0d_ratt: accepted IO library is only jpnf90' )
1502            END SELECT
1503         ENDIF
1504      ENDIF
1505   END SUBROUTINE iom_p0d_ratt
1506
1507   SUBROUTINE iom_p0d_catt( kiomid, cdatt, pvar, cdvar )
1508      INTEGER         , INTENT(in   )                 ::   kiomid    ! Identifier of the file
1509      CHARACTER(len=*), INTENT(in   )                 ::   cdatt     ! Name of the attribute
1510      CHARACTER(len=*), INTENT(in   )                 ::   pvar      ! write field
1511      CHARACTER(len=*), INTENT(in   ), OPTIONAL       ::   cdvar     ! Name of the variable
1512      !
1513      IF( kiomid > 0 ) THEN
1514         IF( iom_file(kiomid)%nfid > 0 ) THEN
1515            SELECT CASE (iom_file(kiomid)%iolib)
1516            CASE (jpnf90   )   ;   IF( PRESENT(cdvar) ) THEN
1517                                      CALL iom_nf90_putatt( kiomid, cdatt, pvar, cdvar=cdvar )
1518                                   ELSE
1519                                      CALL iom_nf90_putatt( kiomid, cdatt, pvar )
1520                                   ENDIF
1521            CASE DEFAULT
1522               CALL ctl_stop( 'iom_p0d_ratt: accepted IO library is only jpnf90' )
1523            END SELECT
1524         ENDIF
1525      ENDIF
1526   END SUBROUTINE iom_p0d_catt
1527
1528   !!----------------------------------------------------------------------
1529   !!                   INTERFACE iom_rstput
1530   !!----------------------------------------------------------------------
1531   SUBROUTINE iom_rp0d( kt, kwrite, kiomid, cdvar, pvar, ktype, ldxios )
1532      INTEGER         , INTENT(in)                         ::   kt       ! ocean time-step
1533      INTEGER         , INTENT(in)                         ::   kwrite   ! writing time-step
1534      INTEGER         , INTENT(in)                         ::   kiomid   ! Identifier of the file
1535      CHARACTER(len=*), INTENT(in)                         ::   cdvar    ! time axis name
1536      REAL(wp)        , INTENT(in)                         ::   pvar     ! written field
1537      INTEGER         , INTENT(in), OPTIONAL               ::   ktype    ! variable external type
1538      LOGICAL, OPTIONAL :: ldxios   ! xios write flag
1539      LOGICAL :: llx                ! local xios write flag
1540      INTEGER :: ivid   ! variable id
1541
1542      llx = .FALSE.
1543      IF(PRESENT(ldxios)) llx = ldxios
1544      IF( llx ) THEN
1545#ifdef key_iomput
1546      IF( kt == kwrite ) THEN
1547          IF(lwp) write(numout,*) 'RESTART: write (XIOS 0D) ',trim(cdvar)
1548          CALL xios_send_field(trim(cdvar), pvar)
1549      ENDIF
1550#endif
1551      ELSE
1552         IF( kiomid > 0 ) THEN
1553            IF( iom_file(kiomid)%nfid > 0 ) THEN
1554               ivid = iom_varid( kiomid, cdvar, ldstop = .FALSE. )
1555               SELECT CASE (iom_file(kiomid)%iolib)
1556               CASE (jpnf90   )   ;   CALL iom_nf90_rstput(   kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r0d = pvar )
1557               CASE DEFAULT     
1558                  CALL ctl_stop( 'iom_rp0d: accepted IO library is only jpnf90 (jpioipsl option has been removed)' )
1559               END SELECT
1560            ENDIF
1561         ENDIF
1562      ENDIF
1563   END SUBROUTINE iom_rp0d
1564
1565   SUBROUTINE iom_rp1d( kt, kwrite, kiomid, cdvar, pvar, ktype, ldxios )
1566      INTEGER         , INTENT(in)                         ::   kt       ! ocean time-step
1567      INTEGER         , INTENT(in)                         ::   kwrite   ! writing time-step
1568      INTEGER         , INTENT(in)                         ::   kiomid   ! Identifier of the file
1569      CHARACTER(len=*), INTENT(in)                         ::   cdvar    ! time axis name
1570      REAL(wp)        , INTENT(in), DIMENSION(          :) ::   pvar     ! written field
1571      INTEGER         , INTENT(in), OPTIONAL               ::   ktype    ! variable external type
1572      LOGICAL, OPTIONAL                                    ::   ldxios   ! xios write flag
1573      LOGICAL :: llx                ! local xios write flag
1574      INTEGER :: ivid   ! variable id
1575
1576      llx = .FALSE.
1577      IF(PRESENT(ldxios)) llx = ldxios
1578      IF( llx ) THEN
1579#ifdef key_iomput
1580      IF( kt == kwrite ) THEN
1581         IF(lwp) write(numout,*) 'RESTART: write (XIOS 1D) ',trim(cdvar)
1582         CALL xios_send_field(trim(cdvar), pvar)
1583      ENDIF
1584#endif
1585      ELSE
1586         IF( kiomid > 0 ) THEN
1587            IF( iom_file(kiomid)%nfid > 0 ) THEN
1588               ivid = iom_varid( kiomid, cdvar, ldstop = .FALSE. )
1589               SELECT CASE (iom_file(kiomid)%iolib)
1590               CASE (jpnf90   )   ;   CALL iom_nf90_rstput(   kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r1d = pvar )
1591               CASE DEFAULT     
1592                  CALL ctl_stop( 'iom_rp1d: accepted IO library is only jpnf90 (jpioipsl option has been removed)' )
1593               END SELECT
1594            ENDIF
1595         ENDIF
1596      ENDIF
1597   END SUBROUTINE iom_rp1d
1598
1599   SUBROUTINE iom_rp2d( kt, kwrite, kiomid, cdvar, pvar, ktype, ldxios )
1600      INTEGER         , INTENT(in)                         ::   kt       ! ocean time-step
1601      INTEGER         , INTENT(in)                         ::   kwrite   ! writing time-step
1602      INTEGER         , INTENT(in)                         ::   kiomid   ! Identifier of the file
1603      CHARACTER(len=*), INTENT(in)                         ::   cdvar    ! time axis name
1604      REAL(wp)        , INTENT(in), DIMENSION(:,    :    ) ::   pvar     ! written field
1605      INTEGER         , INTENT(in), OPTIONAL               ::   ktype    ! variable external type
1606      LOGICAL, OPTIONAL :: ldxios   ! xios write flag
1607      LOGICAL :: llx
1608      INTEGER :: ivid   ! variable id
1609
1610      llx = .FALSE.
1611      IF(PRESENT(ldxios)) llx = ldxios
1612      IF( llx ) THEN
1613#ifdef key_iomput
1614      IF( kt == kwrite ) THEN
1615         IF(lwp) write(numout,*) 'RESTART: write (XIOS 2D) ',trim(cdvar)
1616         CALL xios_send_field(trim(cdvar), pvar)
1617      ENDIF
1618#endif
1619      ELSE
1620         IF( kiomid > 0 ) THEN
1621            IF( iom_file(kiomid)%nfid > 0 ) THEN
1622               ivid = iom_varid( kiomid, cdvar, ldstop = .FALSE. )
1623               SELECT CASE (iom_file(kiomid)%iolib)
1624               CASE (jpnf90   )   ;   CALL iom_nf90_rstput(   kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r2d = pvar )
1625               CASE DEFAULT     
1626                  CALL ctl_stop( 'iom_rp2d: accepted IO library is only jpnf90 (jpioipsl option has been removed)' )
1627               END SELECT
1628            ENDIF
1629         ENDIF
1630      ENDIF
1631   END SUBROUTINE iom_rp2d
1632
1633   SUBROUTINE iom_rp3d( kt, kwrite, kiomid, cdvar, pvar, ktype, ldxios )
1634      INTEGER         , INTENT(in)                         ::   kt       ! ocean time-step
1635      INTEGER         , INTENT(in)                         ::   kwrite   ! writing time-step
1636      INTEGER         , INTENT(in)                         ::   kiomid   ! Identifier of the file
1637      CHARACTER(len=*), INTENT(in)                         ::   cdvar    ! time axis name
1638      REAL(wp)        , INTENT(in),       DIMENSION(:,:,:) ::   pvar     ! written field
1639      INTEGER         , INTENT(in), OPTIONAL               ::   ktype    ! variable external type
1640      LOGICAL, OPTIONAL :: ldxios   ! xios write flag
1641      LOGICAL :: llx                 ! local xios write flag
1642      INTEGER :: ivid   ! variable id
1643
1644      llx = .FALSE.
1645      IF(PRESENT(ldxios)) llx = ldxios
1646      IF( llx ) THEN
1647#ifdef key_iomput
1648      IF( kt == kwrite ) THEN
1649         IF(lwp) write(numout,*) 'RESTART: write (XIOS 3D) ',trim(cdvar)
1650         CALL xios_send_field(trim(cdvar), pvar)
1651      ENDIF
1652#endif
1653      ELSE
1654         IF( kiomid > 0 ) THEN
1655            IF( iom_file(kiomid)%nfid > 0 ) THEN
1656               ivid = iom_varid( kiomid, cdvar, ldstop = .FALSE. )
1657               SELECT CASE (iom_file(kiomid)%iolib)
1658               CASE (jpnf90   )   ;   CALL iom_nf90_rstput(   kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r3d = pvar )
1659               CASE DEFAULT     
1660                  CALL ctl_stop( 'iom_rp3d: accepted IO library is only jpnf90 (jpioipsl option has been removed)' )
1661               END SELECT
1662            ENDIF
1663         ENDIF
1664      ENDIF
1665   END SUBROUTINE iom_rp3d
1666
1667
1668   !!----------------------------------------------------------------------
1669   !!                   INTERFACE iom_put
1670   !!----------------------------------------------------------------------
1671   SUBROUTINE iom_p0d( cdname, pfield0d )
1672      CHARACTER(LEN=*), INTENT(in) ::   cdname
1673      REAL(wp)        , INTENT(in) ::   pfield0d
1674      REAL(wp)        , DIMENSION(jpi,jpj) ::   zz     ! masson
1675#if defined key_iomput
1676      zz(:,:)=pfield0d
1677      CALL xios_send_field(cdname, zz)
1678      !CALL xios_send_field(cdname, (/pfield0d/))
1679#else
1680      IF( .FALSE. )   WRITE(numout,*) cdname, pfield0d   ! useless test to avoid compilation warnings
1681#endif
1682   END SUBROUTINE iom_p0d
1683
1684   SUBROUTINE iom_p1d( cdname, pfield1d )
1685      CHARACTER(LEN=*)          , INTENT(in) ::   cdname
1686      REAL(wp),     DIMENSION(:), INTENT(in) ::   pfield1d
1687#if defined key_iomput
1688      CALL xios_send_field( cdname, RESHAPE( (/pfield1d/), (/1,1,SIZE(pfield1d)/) ) )
1689#else
1690      IF( .FALSE. )   WRITE(numout,*) cdname, pfield1d   ! useless test to avoid compilation warnings
1691#endif
1692   END SUBROUTINE iom_p1d
1693
1694   SUBROUTINE iom_p2d( cdname, pfield2d )
1695      CHARACTER(LEN=*)            , INTENT(in) ::   cdname
1696      REAL(wp),     DIMENSION(:,:), INTENT(in) ::   pfield2d
1697#if defined key_iomput
1698      CALL xios_send_field(cdname, pfield2d)
1699#else
1700      IF( .FALSE. )   WRITE(numout,*) cdname, pfield2d   ! useless test to avoid compilation warnings
1701#endif
1702   END SUBROUTINE iom_p2d
1703
1704   SUBROUTINE iom_p3d( cdname, pfield3d )
1705      CHARACTER(LEN=*)                , INTENT(in) ::   cdname
1706      REAL(wp),       DIMENSION(:,:,:), INTENT(in) ::   pfield3d
1707#if defined key_iomput
1708      CALL xios_send_field( cdname, pfield3d )
1709#else
1710      IF( .FALSE. )   WRITE(numout,*) cdname, pfield3d   ! useless test to avoid compilation warnings
1711#endif
1712   END SUBROUTINE iom_p3d
1713
1714#if defined key_iomput
1715   !!----------------------------------------------------------------------
1716   !!   'key_iomput'                                         IOM  interface
1717   !!----------------------------------------------------------------------
1718
1719   SUBROUTINE iom_set_domain_attr( cdid, ni_glo, nj_glo, ibegin, jbegin, ni, nj, zoom_ibegin, zoom_jbegin, zoom_ni, zoom_nj,   &
1720      &                                    data_dim, data_ibegin, data_ni, data_jbegin, data_nj, lonvalue, latvalue, mask,     &
1721      &                                    nvertex, bounds_lon, bounds_lat, area )
1722      !!----------------------------------------------------------------------
1723      !!----------------------------------------------------------------------
1724      CHARACTER(LEN=*)                  , INTENT(in) ::   cdid
1725      INTEGER                 , OPTIONAL, INTENT(in) ::   ni_glo, nj_glo, ibegin, jbegin, ni, nj
1726      INTEGER                 , OPTIONAL, INTENT(in) ::   data_dim, data_ibegin, data_ni, data_jbegin, data_nj
1727      INTEGER                 , OPTIONAL, INTENT(in) ::   zoom_ibegin, zoom_jbegin, zoom_ni, zoom_nj, nvertex
1728      REAL(wp), DIMENSION(:)  , OPTIONAL, INTENT(in) ::   lonvalue, latvalue
1729      REAL(wp), DIMENSION(:,:), OPTIONAL, INTENT(in) ::   bounds_lon, bounds_lat, area
1730      LOGICAL , DIMENSION(:)  , OPTIONAL, INTENT(in) ::   mask
1731      !!----------------------------------------------------------------------
1732      !
1733      IF( xios_is_valid_domain     (cdid) ) THEN
1734         CALL xios_set_domain_attr     ( cdid, ni_glo=ni_glo, nj_glo=nj_glo, ibegin=ibegin, jbegin=jbegin, ni=ni, nj=nj,   &
1735            &    data_dim=data_dim, data_ibegin=data_ibegin, data_ni=data_ni, data_jbegin=data_jbegin, data_nj=data_nj ,   &
1736            &    lonvalue_1D=lonvalue, latvalue_1D=latvalue, mask_1D=mask, nvertex=nvertex, bounds_lon_1D=bounds_lon,      &
1737            &    bounds_lat_1D=bounds_lat, area=area, type='curvilinear')
1738      ENDIF
1739      IF( xios_is_valid_domaingroup(cdid) ) THEN
1740         CALL xios_set_domaingroup_attr( cdid, ni_glo=ni_glo, nj_glo=nj_glo, ibegin=ibegin, jbegin=jbegin, ni=ni, nj=nj,   &
1741            &    data_dim=data_dim, data_ibegin=data_ibegin, data_ni=data_ni, data_jbegin=data_jbegin, data_nj=data_nj ,   &
1742            &    lonvalue_1D=lonvalue, latvalue_1D=latvalue, mask_1D=mask, nvertex=nvertex, bounds_lon_1D=bounds_lon,      &
1743            &    bounds_lat_1D=bounds_lat, area=area, type='curvilinear' )
1744      ENDIF
1745      !
1746      CALL xios_solve_inheritance()
1747      !
1748   END SUBROUTINE iom_set_domain_attr
1749
1750
1751   SUBROUTINE iom_set_zoom_domain_attr( cdid, ibegin, jbegin, ni, nj )
1752      !!----------------------------------------------------------------------
1753      !!----------------------------------------------------------------------
1754      CHARACTER(LEN=*)          , INTENT(in) ::   cdid
1755      INTEGER         , OPTIONAL, INTENT(in) ::   ibegin, jbegin, ni, nj
1756      !!----------------------------------------------------------------------
1757      IF( xios_is_valid_zoom_domain(cdid) )   CALL xios_set_zoom_domain_attr( cdid, ibegin=ibegin, jbegin=jbegin, ni=ni, nj=nj )
1758   END SUBROUTINE iom_set_zoom_domain_attr
1759
1760
1761   SUBROUTINE iom_set_axis_attr( cdid, paxis, bounds )
1762      !!----------------------------------------------------------------------
1763      !!----------------------------------------------------------------------
1764      CHARACTER(LEN=*)      , INTENT(in) ::   cdid
1765      REAL(wp), DIMENSION(:)  , OPTIONAL, INTENT(in) ::   paxis
1766      REAL(wp), DIMENSION(:,:), OPTIONAL, INTENT(in) ::   bounds
1767      !!----------------------------------------------------------------------
1768      IF( PRESENT(paxis) ) THEN
1769         IF( xios_is_valid_axis     (cdid) )   CALL xios_set_axis_attr     ( cdid, n_glo=SIZE(paxis), value=paxis )
1770         IF( xios_is_valid_axisgroup(cdid) )   CALL xios_set_axisgroup_attr( cdid, n_glo=SIZE(paxis), value=paxis )
1771      ENDIF
1772      IF( xios_is_valid_axis     (cdid) )   CALL xios_set_axis_attr     ( cdid, bounds=bounds )
1773      IF( xios_is_valid_axisgroup(cdid) )   CALL xios_set_axisgroup_attr( cdid, bounds=bounds )
1774      CALL xios_solve_inheritance()
1775   END SUBROUTINE iom_set_axis_attr
1776
1777
1778   SUBROUTINE iom_set_field_attr( cdid, freq_op, freq_offset )
1779      !!----------------------------------------------------------------------
1780      !!----------------------------------------------------------------------
1781      CHARACTER(LEN=*)             , INTENT(in) ::   cdid
1782      TYPE(xios_duration), OPTIONAL, INTENT(in) ::   freq_op
1783      TYPE(xios_duration), OPTIONAL, INTENT(in) ::   freq_offset
1784      !!----------------------------------------------------------------------
1785      IF( xios_is_valid_field     (cdid) )   CALL xios_set_field_attr     ( cdid, freq_op=freq_op, freq_offset=freq_offset )
1786      IF( xios_is_valid_fieldgroup(cdid) )   CALL xios_set_fieldgroup_attr( cdid, freq_op=freq_op, freq_offset=freq_offset )
1787      CALL xios_solve_inheritance()
1788   END SUBROUTINE iom_set_field_attr
1789
1790
1791   SUBROUTINE iom_set_file_attr( cdid, name, name_suffix )
1792      !!----------------------------------------------------------------------
1793      !!----------------------------------------------------------------------
1794      CHARACTER(LEN=*)          , INTENT(in) ::   cdid
1795      CHARACTER(LEN=*),OPTIONAL , INTENT(in) ::   name, name_suffix
1796      !!----------------------------------------------------------------------
1797      IF( xios_is_valid_file     (cdid) )   CALL xios_set_file_attr     ( cdid, name=name, name_suffix=name_suffix )
1798      IF( xios_is_valid_filegroup(cdid) )   CALL xios_set_filegroup_attr( cdid, name=name, name_suffix=name_suffix )
1799      CALL xios_solve_inheritance()
1800   END SUBROUTINE iom_set_file_attr
1801
1802
1803   SUBROUTINE iom_get_file_attr( cdid, name, name_suffix, output_freq )
1804      !!----------------------------------------------------------------------
1805      !!----------------------------------------------------------------------
1806      CHARACTER(LEN=*)          , INTENT(in ) ::   cdid
1807      CHARACTER(LEN=*),OPTIONAL , INTENT(out) ::   name, name_suffix
1808      TYPE(xios_duration), OPTIONAL , INTENT(out) :: output_freq
1809      LOGICAL                                 ::   llexist1,llexist2,llexist3
1810      !---------------------------------------------------------------------
1811      IF( PRESENT( name        ) )   name = ''          ! default values
1812      IF( PRESENT( name_suffix ) )   name_suffix = ''
1813      IF( PRESENT( output_freq ) )   output_freq = xios_duration(0,0,0,0,0,0)
1814      IF( xios_is_valid_file     (cdid) ) THEN
1815         CALL xios_solve_inheritance()
1816         CALL xios_is_defined_file_attr     ( cdid, name = llexist1, name_suffix = llexist2, output_freq = llexist3)
1817         IF(llexist1)   CALL xios_get_file_attr     ( cdid, name = name )
1818         IF(llexist2)   CALL xios_get_file_attr     ( cdid, name_suffix = name_suffix )
1819         IF(llexist3)   CALL xios_get_file_attr     ( cdid, output_freq = output_freq )
1820      ENDIF
1821      IF( xios_is_valid_filegroup(cdid) ) THEN
1822         CALL xios_solve_inheritance()
1823         CALL xios_is_defined_filegroup_attr( cdid, name = llexist1, name_suffix = llexist2, output_freq = llexist3)
1824         IF(llexist1)   CALL xios_get_filegroup_attr( cdid, name = name )
1825         IF(llexist2)   CALL xios_get_filegroup_attr( cdid, name_suffix = name_suffix )
1826         IF(llexist3)   CALL xios_get_filegroup_attr( cdid, output_freq = output_freq )
1827      ENDIF
1828   END SUBROUTINE iom_get_file_attr
1829
1830
1831   SUBROUTINE iom_set_grid_attr( cdid, mask )
1832      !!----------------------------------------------------------------------
1833      !!----------------------------------------------------------------------
1834      CHARACTER(LEN=*)                   , INTENT(in) ::   cdid
1835      LOGICAL, DIMENSION(:,:,:), OPTIONAL, INTENT(in) ::   mask
1836      !!----------------------------------------------------------------------
1837      IF( xios_is_valid_grid     (cdid) )   CALL xios_set_grid_attr     ( cdid, mask_3D=mask )
1838      IF( xios_is_valid_gridgroup(cdid) )   CALL xios_set_gridgroup_attr( cdid, mask_3D=mask )
1839      CALL xios_solve_inheritance()
1840   END SUBROUTINE iom_set_grid_attr
1841
1842   SUBROUTINE iom_setkt( kt, cdname )
1843      !!----------------------------------------------------------------------
1844      !!----------------------------------------------------------------------
1845      INTEGER         , INTENT(in) ::   kt 
1846      CHARACTER(LEN=*), INTENT(in) ::   cdname
1847      !!----------------------------------------------------------------------
1848      CALL iom_swap( cdname )   ! swap to cdname context
1849      CALL xios_update_calendar(kt)
1850      IF( cdname /= TRIM(cxios_context) )   CALL iom_swap( TRIM(cxios_context) )   ! return back to nemo context
1851   END SUBROUTINE iom_setkt
1852
1853   SUBROUTINE iom_context_finalize( cdname )
1854      !!----------------------------------------------------------------------
1855      !!----------------------------------------------------------------------
1856      CHARACTER(LEN=*), INTENT(in) :: cdname
1857      CHARACTER(LEN=120)           :: clname
1858      !!----------------------------------------------------------------------
1859      clname = cdname
1860      IF( TRIM(Agrif_CFixed()) .NE. '0' ) clname = TRIM(Agrif_CFixed())//"_"//clname 
1861      IF( xios_is_valid_context(clname) ) THEN
1862         CALL iom_swap( cdname )   ! swap to cdname context
1863         CALL xios_context_finalize() ! finalize the context
1864         IF( cdname /= TRIM(cxios_context) ) CALL iom_swap( TRIM(cxios_context) )   ! return back to nemo context
1865      ENDIF
1866      !
1867   END SUBROUTINE iom_context_finalize
1868
1869
1870   SUBROUTINE set_grid( cdgrd, plon, plat, ldxios )
1871      !!----------------------------------------------------------------------
1872      !!                     ***  ROUTINE set_grid  ***
1873      !!
1874      !! ** Purpose :   define horizontal grids
1875      !!----------------------------------------------------------------------
1876      CHARACTER(LEN=1)            , INTENT(in) ::   cdgrd
1877      REAL(wp), DIMENSION(jpi,jpj), INTENT(in) ::   plon
1878      REAL(wp), DIMENSION(jpi,jpj), INTENT(in) ::   plat
1879      !
1880      INTEGER  :: ni, nj
1881      REAL(wp), DIMENSION(jpi,jpj,jpk) ::   zmask
1882      LOGICAL, INTENT(IN) :: ldxios
1883      !!----------------------------------------------------------------------
1884      !
1885      ni = nlei-nldi+1
1886      nj = nlej-nldj+1
1887      !
1888      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)
1889      CALL iom_set_domain_attr("grid_"//cdgrd, data_dim=2, data_ibegin = 1-nldi, data_ni = jpi, data_jbegin = 1-nldj, data_nj = jpj)
1890      CALL iom_set_domain_attr("grid_"//cdgrd, lonvalue = RESHAPE(plon(nldi:nlei, nldj:nlej),(/ ni*nj /)),   &
1891         &                                     latvalue = RESHAPE(plat(nldi:nlei, nldj:nlej),(/ ni*nj /))) 
1892      !
1893      IF ( ln_mskland .AND. (.NOT.ldxios) ) THEN
1894         ! mask land points, keep values on coast line -> specific mask for U, V and W points
1895         SELECT CASE ( cdgrd )
1896         CASE('T')   ;   zmask(:,:,:)       = tmask(:,:,:)
1897         CASE('U')   ;   zmask(2:jpim1,:,:) = tmask(2:jpim1,:,:) + tmask(3:jpi,:,:)   ;   CALL lbc_lnk( zmask, 'U', 1. )
1898         CASE('V')   ;   zmask(:,2:jpjm1,:) = tmask(:,2:jpjm1,:) + tmask(:,3:jpj,:)   ;   CALL lbc_lnk( zmask, 'V', 1. )
1899         CASE('W')   ;   zmask(:,:,2:jpk  ) = tmask(:,:,1:jpkm1) + tmask(:,:,2:jpk)   ;   zmask(:,:,1) = tmask(:,:,1)
1900         END SELECT
1901         !
1902         CALL iom_set_domain_attr( "grid_"//cdgrd       , mask = RESHAPE(zmask(nldi:nlei,nldj:nlej,1),(/ni*nj    /)) /= 0. )
1903         CALL iom_set_grid_attr  ( "grid_"//cdgrd//"_3D", mask = RESHAPE(zmask(nldi:nlei,nldj:nlej,:),(/ni,nj,jpk/)) /= 0. )
1904      ENDIF
1905      !
1906   END SUBROUTINE set_grid
1907
1908
1909   SUBROUTINE set_grid_bounds( cdgrd, plon_cnr, plat_cnr, plon_pnt, plat_pnt )
1910      !!----------------------------------------------------------------------
1911      !!                   ***  ROUTINE set_grid_bounds  ***
1912      !!
1913      !! ** Purpose :   define horizontal grid corners
1914      !!
1915      !!----------------------------------------------------------------------
1916      CHARACTER(LEN=1)                      , INTENT(in) :: cdgrd
1917      REAL(wp), DIMENSION(jpi,jpj)          , INTENT(in) :: plon_cnr, plat_cnr  ! Lat/lon coord. of a contiguous vertex of cell (i,j)
1918      REAL(wp), DIMENSION(jpi,jpj), OPTIONAL, INTENT(in) :: plon_pnt, plat_pnt  ! Lat/lon coord. of the point of cell (i,j)
1919      !
1920      INTEGER :: ji, jj, jn, ni, nj
1921      INTEGER :: icnr, jcnr                                    ! Offset such that the vertex coordinate (i+icnr,j+jcnr)
1922      !                                                        ! represents the bottom-left corner of cell (i,j)
1923      REAL(wp), ALLOCATABLE, DIMENSION(:,:,:,:) :: z_bnds      ! Lat/lon coordinates of the vertices of cell (i,j)
1924      REAL(wp), ALLOCATABLE, DIMENSION(:,:)     :: z_fld       ! Working array to determine where to rotate cells
1925      REAL(wp), ALLOCATABLE, DIMENSION(:,:)     :: z_rot       ! Lat/lon working array for rotation of cells
1926      !!----------------------------------------------------------------------
1927      !
1928      ALLOCATE( z_bnds(4,jpi,jpj,2), z_fld(jpi,jpj), z_rot(4,2)  )
1929      !
1930      ! Offset of coordinate representing bottom-left corner
1931      SELECT CASE ( TRIM(cdgrd) )
1932      CASE ('T', 'W')   ;   icnr = -1   ;   jcnr = -1
1933      CASE ('U')        ;   icnr =  0   ;   jcnr = -1
1934      CASE ('V')        ;   icnr = -1   ;   jcnr =  0
1935      END SELECT
1936      !
1937      ni = nlei-nldi+1   ! Dimensions of subdomain interior
1938      nj = nlej-nldj+1
1939      !
1940      z_fld(:,:) = 1._wp
1941      CALL lbc_lnk( z_fld, cdgrd, -1. )    ! Working array for location of northfold
1942      !
1943      ! Cell vertices that can be defined
1944      DO jj = 2, jpjm1
1945         DO ji = 2, jpim1
1946            z_bnds(1,ji,jj,1) = plat_cnr(ji+icnr,  jj+jcnr  ) ! Bottom-left
1947            z_bnds(2,ji,jj,1) = plat_cnr(ji+icnr+1,jj+jcnr  ) ! Bottom-right
1948            z_bnds(3,ji,jj,1) = plat_cnr(ji+icnr+1,jj+jcnr+1) ! Top-right
1949            z_bnds(4,ji,jj,1) = plat_cnr(ji+icnr,  jj+jcnr+1) ! Top-left
1950            z_bnds(1,ji,jj,2) = plon_cnr(ji+icnr,  jj+jcnr  ) ! Bottom-left
1951            z_bnds(2,ji,jj,2) = plon_cnr(ji+icnr+1,jj+jcnr  ) ! Bottom-right
1952            z_bnds(3,ji,jj,2) = plon_cnr(ji+icnr+1,jj+jcnr+1) ! Top-right
1953            z_bnds(4,ji,jj,2) = plon_cnr(ji+icnr,  jj+jcnr+1) ! Top-left
1954         END DO
1955      END DO
1956      !
1957      ! Cell vertices on boundries
1958      DO jn = 1, 4
1959         CALL lbc_lnk( z_bnds(jn,:,:,1), cdgrd, 1., pval=999._wp )
1960         CALL lbc_lnk( z_bnds(jn,:,:,2), cdgrd, 1., pval=999._wp )
1961      END DO
1962      !
1963      ! Zero-size cells at closed boundaries if cell points provided,
1964      ! otherwise they are closed cells with unrealistic bounds
1965      IF( PRESENT(plon_pnt) .AND. PRESENT(plat_pnt) ) THEN
1966         IF( (nbondi == -1 .OR. nbondi == 2) .AND. .NOT. (jperio == 1 .OR. jperio == 4 .OR. jperio == 6) ) THEN
1967            DO jn = 1, 4        ! (West or jpni = 1), closed E-W
1968               z_bnds(jn,1,:,1) = plat_pnt(1,:)  ;  z_bnds(jn,1,:,2) = plon_pnt(1,:)
1969            END DO
1970         ENDIF
1971         IF( (nbondi == 1 .OR. nbondi == 2) .AND. .NOT. (jperio == 1 .OR. jperio == 4 .OR. jperio == 6) ) THEN
1972            DO jn = 1, 4        ! (East or jpni = 1), closed E-W
1973               z_bnds(jn,nlci,:,1) = plat_pnt(nlci,:)  ;  z_bnds(jn,nlci,:,2) = plon_pnt(nlci,:)
1974            END DO
1975         ENDIF
1976         IF( nbondj == -1 .OR. (nbondj == 2 .AND. jperio /= 2) ) THEN
1977            DO jn = 1, 4        ! South or (jpnj = 1, not symmetric)
1978               z_bnds(jn,:,1,1) = plat_pnt(:,1)  ;  z_bnds(jn,:,1,2) = plon_pnt(:,1)
1979            END DO
1980         ENDIF
1981         IF( (nbondj == 1 .OR. nbondj == 2) .AND. jperio  < 3 ) THEN
1982            DO jn = 1, 4        ! (North or jpnj = 1), no north fold
1983               z_bnds(jn,:,nlcj,1) = plat_pnt(:,nlcj)  ;  z_bnds(jn,:,nlcj,2) = plon_pnt(:,nlcj)
1984            END DO
1985         ENDIF
1986      ENDIF
1987      !
1988      IF( (nbondj == 1 .OR. nbondj == 2) .AND. jperio >= 3 ) THEN    ! Rotate cells at the north fold
1989         DO jj = 1, jpj
1990            DO ji = 1, jpi
1991               IF( z_fld(ji,jj) == -1. ) THEN
1992                  z_rot(1,:) = z_bnds(3,ji,jj,:) ; z_rot(2,:) = z_bnds(4,ji,jj,:)
1993                  z_rot(3,:) = z_bnds(1,ji,jj,:) ; z_rot(4,:) = z_bnds(2,ji,jj,:)
1994                  z_bnds(:,ji,jj,:) = z_rot(:,:)
1995               ENDIF
1996            END DO
1997         END DO
1998      ELSE IF( nbondj == 2 .AND. jperio == 2 ) THEN                  ! Invert cells at the symmetric equator
1999         DO ji = 1, jpi
2000            z_rot(1:2,:) = z_bnds(3:4,ji,1,:)
2001            z_rot(3:4,:) = z_bnds(1:2,ji,1,:)
2002            z_bnds(:,ji,1,:) = z_rot(:,:)
2003         END DO
2004      ENDIF
2005      !
2006      CALL iom_set_domain_attr("grid_"//cdgrd, bounds_lat = RESHAPE(z_bnds(:,nldi:nlei,nldj:nlej,1),(/ 4,ni*nj /)),           &
2007          &                                    bounds_lon = RESHAPE(z_bnds(:,nldi:nlei,nldj:nlej,2),(/ 4,ni*nj /)), nvertex=4 )
2008      !
2009      DEALLOCATE( z_bnds, z_fld, z_rot ) 
2010      !
2011   END SUBROUTINE set_grid_bounds
2012
2013
2014   SUBROUTINE set_grid_znl( plat )
2015      !!----------------------------------------------------------------------
2016      !!                     ***  ROUTINE set_grid_znl  ***
2017      !!
2018      !! ** Purpose :   define grids for zonal mean
2019      !!
2020      !!----------------------------------------------------------------------
2021      REAL(wp), DIMENSION(jpi,jpj), INTENT(in) ::   plat
2022      !
2023      INTEGER  :: ni, nj, ix, iy
2024      REAL(wp), DIMENSION(:), ALLOCATABLE  ::   zlon
2025      !!----------------------------------------------------------------------
2026      !
2027      ni=nlei-nldi+1       ! define zonal mean domain (jpj*jpk)
2028      nj=nlej-nldj+1
2029      ALLOCATE( zlon(ni*nj) )       ;       zlon(:) = 0._wp
2030      !
2031      CALL dom_ngb( -168.53, 65.03, ix, iy, 'T' ) !  i-line that passes through Bering Strait: Reference latitude (used in plots)
2032!      CALL dom_ngb( 180., 90., ix, iy, 'T' ) !  i-line that passes near the North Pole : Reference latitude (used in plots)
2033      CALL iom_set_domain_attr("gznl", ni_glo=jpiglo, nj_glo=jpjglo, ibegin=nimpp+nldi-2, jbegin=njmpp+nldj-2, ni=ni, nj=nj)
2034      CALL iom_set_domain_attr("gznl", data_dim=2, data_ibegin = 1-nldi, data_ni = jpi, data_jbegin = 1-nldj, data_nj = jpj)
2035      CALL iom_set_domain_attr("gznl", lonvalue = zlon,   &
2036         &                             latvalue = RESHAPE(plat(nldi:nlei, nldj:nlej),(/ ni*nj /))) 
2037      CALL iom_set_zoom_domain_attr ("ptr", ibegin=ix-1, jbegin=0, ni=1, nj=jpjglo)
2038      !
2039      CALL iom_update_file_name('ptr')
2040      !
2041   END SUBROUTINE set_grid_znl
2042
2043
2044   SUBROUTINE set_scalar
2045      !!----------------------------------------------------------------------
2046      !!                     ***  ROUTINE set_scalar  ***
2047      !!
2048      !! ** Purpose :   define fake grids for scalar point
2049      !!
2050      !!----------------------------------------------------------------------
2051      REAL(wp), DIMENSION(1)   ::   zz = 1.
2052      !!----------------------------------------------------------------------
2053      !
2054      CALL iom_set_domain_attr('scalarpoint', ni_glo=jpnij, nj_glo=1, ibegin=narea-1, jbegin=0, ni=1, nj=1)
2055      CALL iom_set_domain_attr('scalarpoint', data_dim=2, data_ibegin = 1, data_ni = 1, data_jbegin = 1, data_nj = 1)
2056      !
2057      zz = REAL( narea, wp )
2058      CALL iom_set_domain_attr('scalarpoint', lonvalue=zz, latvalue=zz)
2059      !
2060   END SUBROUTINE set_scalar
2061
2062
2063   SUBROUTINE set_xmlatt
2064      !!----------------------------------------------------------------------
2065      !!                     ***  ROUTINE set_xmlatt  ***
2066      !!
2067      !! ** Purpose :   automatic definitions of some of the xml attributs...
2068      !!
2069      !!----------------------------------------------------------------------
2070      CHARACTER(len=1),DIMENSION( 3) ::   clgrd                    ! suffix name
2071      CHARACTER(len=256)             ::   clsuff                   ! suffix name
2072      CHARACTER(len=1)               ::   cl1                      ! 1 character
2073      CHARACTER(len=2)               ::   cl2                      ! 2 characters
2074      CHARACTER(len=3)               ::   cl3                      ! 3 characters
2075      INTEGER                        ::   ji, jg                   ! loop counters
2076      INTEGER                        ::   ix, iy                   ! i-,j- index
2077      REAL(wp)        ,DIMENSION(11) ::   zlontao                  ! longitudes of tao    moorings
2078      REAL(wp)        ,DIMENSION( 7) ::   zlattao                  ! latitudes  of tao    moorings
2079      REAL(wp)        ,DIMENSION( 4) ::   zlonrama                 ! longitudes of rama   moorings
2080      REAL(wp)        ,DIMENSION(11) ::   zlatrama                 ! latitudes  of rama   moorings
2081      REAL(wp)        ,DIMENSION( 3) ::   zlonpira                 ! longitudes of pirata moorings
2082      REAL(wp)        ,DIMENSION( 9) ::   zlatpira                 ! latitudes  of pirata moorings
2083      TYPE(xios_duration)            ::   f_op, f_of
2084      !!----------------------------------------------------------------------
2085      !
2086      ! frequency of the call of iom_put (attribut: freq_op)
2087      f_op%timestep = 1        ;  f_of%timestep =  0  ; CALL iom_set_field_attr('field_definition', freq_op=f_op, freq_offset=f_of)
2088      f_op%timestep = 2        ;  f_of%timestep =  0  ; CALL iom_set_field_attr('trendT_even'     , freq_op=f_op, freq_offset=f_of)
2089      f_op%timestep = 2        ;  f_of%timestep = -1  ; CALL iom_set_field_attr('trendT_odd'      , freq_op=f_op, freq_offset=f_of)
2090      f_op%timestep = nn_fsbc  ;  f_of%timestep =  0  ; CALL iom_set_field_attr('SBC'             , freq_op=f_op, freq_offset=f_of)
2091      f_op%timestep = nn_fsbc  ;  f_of%timestep =  0  ; CALL iom_set_field_attr('SBC_scalar'      , freq_op=f_op, freq_offset=f_of)
2092      f_op%timestep = nn_dttrc ;  f_of%timestep =  0  ; CALL iom_set_field_attr('ptrc_T'          , freq_op=f_op, freq_offset=f_of)
2093      f_op%timestep = nn_dttrc ;  f_of%timestep =  0  ; CALL iom_set_field_attr('diad_T'          , freq_op=f_op, freq_offset=f_of)
2094
2095      ! output file names (attribut: name)
2096      DO ji = 1, 9
2097         WRITE(cl1,'(i1)') ji 
2098         CALL iom_update_file_name('file'//cl1)
2099      END DO
2100      DO ji = 1, 99
2101         WRITE(cl2,'(i2.2)') ji 
2102         CALL iom_update_file_name('file'//cl2)
2103      END DO
2104      DO ji = 1, 999
2105         WRITE(cl3,'(i3.3)') ji 
2106         CALL iom_update_file_name('file'//cl3)
2107      END DO
2108
2109      ! Zooms...
2110      clgrd = (/ 'T', 'U', 'W' /) 
2111      DO jg = 1, SIZE(clgrd)                                                                   ! grid type
2112         cl1 = clgrd(jg)
2113         ! Equatorial section (attributs: jbegin, ni, name_suffix)
2114         CALL dom_ngb( 0., 0., ix, iy, cl1 )
2115         CALL iom_set_zoom_domain_attr ('Eq'//cl1, jbegin=iy-1, ni=jpiglo)
2116         CALL iom_get_file_attr   ('Eq'//cl1, name_suffix = clsuff             )
2117         CALL iom_set_file_attr   ('Eq'//cl1, name_suffix = TRIM(clsuff)//'_Eq')
2118         CALL iom_update_file_name('Eq'//cl1)
2119      END DO
2120      ! TAO moorings (attributs: ibegin, jbegin, name_suffix)
2121      zlontao = (/ 137.0, 147.0, 156.0, 165.0, -180.0, -170.0, -155.0, -140.0, -125.0, -110.0, -95.0 /)
2122      zlattao = (/  -8.0,  -5.0,  -2.0,   0.0,    2.0,    5.0,    8.0 /)
2123      CALL set_mooring( zlontao, zlattao )
2124      ! RAMA moorings (attributs: ibegin, jbegin, name_suffix)
2125      zlonrama = (/  55.0,  67.0, 80.5, 90.0 /)
2126      zlatrama = (/ -16.0, -12.0, -8.0, -4.0, -1.5, 0.0, 1.5, 4.0, 8.0, 12.0, 15.0 /)
2127      CALL set_mooring( zlonrama, zlatrama )
2128      ! PIRATA moorings (attributs: ibegin, jbegin, name_suffix)
2129      zlonpira = (/ -38.0, -23.0, -10.0 /)
2130      zlatpira = (/ -19.0, -14.0,  -8.0, 0.0, 4.0, 8.0, 12.0, 15.0, 20.0 /)
2131      CALL set_mooring( zlonpira, zlatpira )
2132      !
2133   END SUBROUTINE set_xmlatt
2134
2135
2136   SUBROUTINE set_mooring( plon, plat )
2137      !!----------------------------------------------------------------------
2138      !!                     ***  ROUTINE set_mooring  ***
2139      !!
2140      !! ** Purpose :   automatic definitions of moorings xml attributs...
2141      !!
2142      !!----------------------------------------------------------------------
2143      REAL(wp), DIMENSION(:), INTENT(in) ::   plon, plat   ! longitudes/latitudes oft the mooring
2144      !
2145!!$      CHARACTER(len=1),DIMENSION(4) ::   clgrd = (/ 'T', 'U', 'V', 'W' /)   ! suffix name
2146      CHARACTER(len=1),DIMENSION(1) ::   clgrd = (/ 'T' /)        ! suffix name
2147      CHARACTER(len=256)            ::   clname                   ! file name
2148      CHARACTER(len=256)            ::   clsuff                   ! suffix name
2149      CHARACTER(len=1)              ::   cl1                      ! 1 character
2150      CHARACTER(len=6)              ::   clon,clat                ! name of longitude, latitude
2151      INTEGER                       ::   ji, jj, jg               ! loop counters
2152      INTEGER                       ::   ix, iy                   ! i-,j- index
2153      REAL(wp)                      ::   zlon, zlat
2154      !!----------------------------------------------------------------------
2155      DO jg = 1, SIZE(clgrd)
2156         cl1 = clgrd(jg)
2157         DO ji = 1, SIZE(plon)
2158            DO jj = 1, SIZE(plat)
2159               zlon = plon(ji)
2160               zlat = plat(jj)
2161               ! modifications for RAMA moorings
2162               IF( zlon ==  67. .AND. zlat ==  15. )   zlon =  65.
2163               IF( zlon ==  90. .AND. zlat <=  -4. )   zlon =  95.
2164               IF( zlon ==  95. .AND. zlat ==  -4. )   zlat =  -5.
2165               ! modifications for PIRATA moorings
2166               IF( zlon == -38. .AND. zlat == -19. )   zlon = -34.
2167               IF( zlon == -38. .AND. zlat == -14. )   zlon = -32.
2168               IF( zlon == -38. .AND. zlat ==  -8. )   zlon = -30.
2169               IF( zlon == -38. .AND. zlat ==   0. )   zlon = -35.
2170               IF( zlon == -23. .AND. zlat ==  20. )   zlat =  21.
2171               IF( zlon == -10. .AND. zlat == -14. )   zlat = -10.
2172               IF( zlon == -10. .AND. zlat ==  -8. )   zlat =  -6.
2173               IF( zlon == -10. .AND. zlat ==   4. ) THEN   ;   zlon = 0.   ;   zlat = 0.   ;   ENDIF
2174               CALL dom_ngb( zlon, zlat, ix, iy, cl1 )
2175               IF( zlon >= 0. ) THEN 
2176                  IF( zlon == REAL(NINT(zlon), wp) ) THEN   ;   WRITE(clon, '(i3,  a)') NINT( zlon), 'e'
2177                  ELSE                                      ;   WRITE(clon, '(f5.1,a)')       zlon , 'e'
2178                  ENDIF
2179               ELSE             
2180                  IF( zlon == REAL(NINT(zlon), wp) ) THEN   ;   WRITE(clon, '(i3,  a)') NINT(-zlon), 'w'
2181                  ELSE                                      ;   WRITE(clon, '(f5.1,a)')      -zlon , 'w'
2182                  ENDIF
2183               ENDIF
2184               IF( zlat >= 0. ) THEN 
2185                  IF( zlat == REAL(NINT(zlat), wp) ) THEN   ;   WRITE(clat, '(i2,  a)') NINT( zlat), 'n'
2186                  ELSE                                      ;   WRITE(clat, '(f4.1,a)')       zlat , 'n'
2187                  ENDIF
2188               ELSE             
2189                  IF( zlat == REAL(NINT(zlat), wp) ) THEN   ;   WRITE(clat, '(i2,  a)') NINT(-zlat), 's'
2190                  ELSE                                      ;   WRITE(clat, '(f4.1,a)')      -zlat , 's'
2191                  ENDIF
2192               ENDIF
2193               clname = TRIM(ADJUSTL(clat))//TRIM(ADJUSTL(clon))
2194               CALL iom_set_zoom_domain_attr  (TRIM(clname)//cl1, ibegin= ix-1, jbegin= iy-1)
2195
2196               CALL iom_get_file_attr   (TRIM(clname)//cl1, name_suffix = clsuff                         )
2197               CALL iom_set_file_attr   (TRIM(clname)//cl1, name_suffix = TRIM(clsuff)//'_'//TRIM(clname))
2198               CALL iom_update_file_name(TRIM(clname)//cl1)
2199            END DO
2200         END DO
2201      END DO
2202     
2203   END SUBROUTINE set_mooring
2204
2205   
2206   SUBROUTINE iom_update_file_name( cdid )
2207      !!----------------------------------------------------------------------
2208      !!                     ***  ROUTINE iom_update_file_name  ***
2209      !!
2210      !! ** Purpose :   
2211      !!
2212      !!----------------------------------------------------------------------
2213      CHARACTER(LEN=*)          , INTENT(in) ::   cdid
2214      !
2215      CHARACTER(LEN=256) ::   clname
2216      CHARACTER(LEN=20)  ::   clfreq
2217      CHARACTER(LEN=20)  ::   cldate
2218      INTEGER            ::   idx
2219      INTEGER            ::   jn
2220      INTEGER            ::   itrlen
2221      INTEGER            ::   iyear, imonth, iday, isec
2222      REAL(wp)           ::   zsec
2223      LOGICAL            ::   llexist
2224      TYPE(xios_duration)   ::   output_freq 
2225      !!----------------------------------------------------------------------
2226      !
2227      DO jn = 1, 2
2228         !
2229         output_freq = xios_duration(0,0,0,0,0,0)
2230         IF( jn == 1 )   CALL iom_get_file_attr( cdid, name        = clname, output_freq = output_freq )
2231         IF( jn == 2 )   CALL iom_get_file_attr( cdid, name_suffix = clname )
2232         !
2233         IF ( TRIM(clname) /= '' ) THEN 
2234            !
2235            idx = INDEX(clname,'@expname@') + INDEX(clname,'@EXPNAME@')
2236            DO WHILE ( idx /= 0 ) 
2237               clname = clname(1:idx-1)//TRIM(cexper)//clname(idx+9:LEN_TRIM(clname))
2238               idx = INDEX(clname,'@expname@') + INDEX(clname,'@EXPNAME@')
2239            END DO
2240            !
2241            idx = INDEX(clname,'@freq@') + INDEX(clname,'@FREQ@')
2242            DO WHILE ( idx /= 0 ) 
2243              IF ( output_freq%timestep /= 0) THEN
2244                  WRITE(clfreq,'(I18,A2)')INT(output_freq%timestep),'ts' 
2245                  itrlen = LEN_TRIM(ADJUSTL(clfreq))
2246              ELSE IF ( output_freq%second /= 0 ) THEN
2247                  WRITE(clfreq,'(I19,A1)')INT(output_freq%second),'s' 
2248                  itrlen = LEN_TRIM(ADJUSTL(clfreq))
2249              ELSE IF ( output_freq%minute /= 0 ) THEN
2250                  WRITE(clfreq,'(I18,A2)')INT(output_freq%minute),'mi' 
2251                  itrlen = LEN_TRIM(ADJUSTL(clfreq))
2252              ELSE IF ( output_freq%hour /= 0 ) THEN
2253                  WRITE(clfreq,'(I19,A1)')INT(output_freq%hour),'h' 
2254                  itrlen = LEN_TRIM(ADJUSTL(clfreq))
2255              ELSE IF ( output_freq%day /= 0 ) THEN
2256                  WRITE(clfreq,'(I19,A1)')INT(output_freq%day),'d' 
2257                  itrlen = LEN_TRIM(ADJUSTL(clfreq))
2258              ELSE IF ( output_freq%month /= 0 ) THEN   
2259                  WRITE(clfreq,'(I19,A1)')INT(output_freq%month),'m' 
2260                  itrlen = LEN_TRIM(ADJUSTL(clfreq))
2261              ELSE IF ( output_freq%year /= 0 ) THEN   
2262                  WRITE(clfreq,'(I19,A1)')INT(output_freq%year),'y' 
2263                  itrlen = LEN_TRIM(ADJUSTL(clfreq))
2264              ELSE
2265                  CALL ctl_stop('error in the name of file id '//TRIM(cdid),   &
2266                     & ' attribute output_freq is undefined -> cannot replace @freq@ in '//TRIM(clname) )
2267              ENDIF
2268              clname = clname(1:idx-1)//TRIM(ADJUSTL(clfreq))//clname(idx+6:LEN_TRIM(clname))
2269              idx = INDEX(clname,'@freq@') + INDEX(clname,'@FREQ@')
2270            END DO
2271            !
2272            idx = INDEX(clname,'@startdate@') + INDEX(clname,'@STARTDATE@')
2273            DO WHILE ( idx /= 0 ) 
2274               cldate = iom_sdate( fjulday - rdt / rday )
2275               clname = clname(1:idx-1)//TRIM(cldate)//clname(idx+11:LEN_TRIM(clname))
2276               idx = INDEX(clname,'@startdate@') + INDEX(clname,'@STARTDATE@')
2277            END DO
2278            !
2279            idx = INDEX(clname,'@startdatefull@') + INDEX(clname,'@STARTDATEFULL@')
2280            DO WHILE ( idx /= 0 ) 
2281               cldate = iom_sdate( fjulday - rdt / rday, ldfull = .TRUE. )
2282               clname = clname(1:idx-1)//TRIM(cldate)//clname(idx+15:LEN_TRIM(clname))
2283               idx = INDEX(clname,'@startdatefull@') + INDEX(clname,'@STARTDATEFULL@')
2284            END DO
2285            !
2286            idx = INDEX(clname,'@enddate@') + INDEX(clname,'@ENDDATE@')
2287            DO WHILE ( idx /= 0 ) 
2288               cldate = iom_sdate( fjulday + rdt / rday * REAL( nitend - nit000, wp ), ld24 = .TRUE. )
2289               clname = clname(1:idx-1)//TRIM(cldate)//clname(idx+9:LEN_TRIM(clname))
2290               idx = INDEX(clname,'@enddate@') + INDEX(clname,'@ENDDATE@')
2291            END DO
2292            !
2293            idx = INDEX(clname,'@enddatefull@') + INDEX(clname,'@ENDDATEFULL@')
2294            DO WHILE ( idx /= 0 ) 
2295               cldate = iom_sdate( fjulday + rdt / rday * REAL( nitend - nit000, wp ), ld24 = .TRUE., ldfull = .TRUE. )
2296               clname = clname(1:idx-1)//TRIM(cldate)//clname(idx+13:LEN_TRIM(clname))
2297               idx = INDEX(clname,'@enddatefull@') + INDEX(clname,'@ENDDATEFULL@')
2298            END DO
2299            !
2300            IF( jn == 1 .AND. TRIM(Agrif_CFixed()) /= '0' )   clname = TRIM(Agrif_CFixed())//"_"//TRIM(clname)
2301            IF( jn == 1 )   CALL iom_set_file_attr( cdid, name        = clname )
2302            IF( jn == 2 )   CALL iom_set_file_attr( cdid, name_suffix = clname )
2303            !
2304         ENDIF
2305         !
2306      END DO
2307      !
2308   END SUBROUTINE iom_update_file_name
2309
2310
2311   FUNCTION iom_sdate( pjday, ld24, ldfull )
2312      !!----------------------------------------------------------------------
2313      !!                     ***  ROUTINE iom_sdate  ***
2314      !!
2315      !! ** Purpose :   send back the date corresponding to the given julian day
2316      !!----------------------------------------------------------------------
2317      REAL(wp), INTENT(in   )           ::   pjday    ! julian day
2318      LOGICAL , INTENT(in   ), OPTIONAL ::   ld24     ! true to force 24:00 instead of 00:00
2319      LOGICAL , INTENT(in   ), OPTIONAL ::   ldfull   ! true to get the compleate date: yyyymmdd_hh:mm:ss
2320      !
2321      CHARACTER(LEN=20) ::   iom_sdate
2322      CHARACTER(LEN=50) ::   clfmt                         !  format used to write the date
2323      INTEGER           ::   iyear, imonth, iday, ihour, iminute, isec
2324      REAL(wp)          ::   zsec
2325      LOGICAL           ::   ll24, llfull
2326      !!----------------------------------------------------------------------
2327      !
2328      IF( PRESENT(ld24) ) THEN   ;   ll24 = ld24
2329      ELSE                       ;   ll24 = .FALSE.
2330      ENDIF
2331      !
2332      IF( PRESENT(ldfull) ) THEN   ;   llfull = ldfull
2333      ELSE                         ;   llfull = .FALSE.
2334      ENDIF
2335      !
2336      CALL ju2ymds( pjday, iyear, imonth, iday, zsec )
2337      isec = NINT(zsec)
2338      !
2339      IF ( ll24 .AND. isec == 0 ) THEN   ! 00:00 of the next day -> move to 24:00 of the current day
2340         CALL ju2ymds( pjday - 1., iyear, imonth, iday, zsec )
2341         isec = 86400
2342      ENDIF
2343      !
2344      IF( iyear < 10000 ) THEN   ;   clfmt = "i4.4,2i2.2"                ! format used to write the date
2345      ELSE                       ;   WRITE(clfmt, "('i',i1,',2i2.2')") INT(LOG10(REAL(iyear,wp))) + 1
2346      ENDIF
2347      !
2348!$AGRIF_DO_NOT_TREAT     
2349      ! needed in the conv
2350      IF( llfull ) THEN
2351         clfmt = TRIM(clfmt)//",'_',i2.2,':',i2.2,':',i2.2"
2352         ihour   = isec / 3600
2353         isec    = MOD(isec, 3600)
2354         iminute = isec / 60
2355         isec    = MOD(isec, 60)
2356         WRITE(iom_sdate, '('//TRIM(clfmt)//')') iyear, imonth, iday, ihour, iminute, isec    ! date of the end of run
2357      ELSE
2358         WRITE(iom_sdate, '('//TRIM(clfmt)//')') iyear, imonth, iday                          ! date of the end of run
2359      ENDIF
2360!$AGRIF_END_DO_NOT_TREAT     
2361      !
2362   END FUNCTION iom_sdate
2363
2364#else
2365   !!----------------------------------------------------------------------
2366   !!   NOT 'key_iomput'                               a few dummy routines
2367   !!----------------------------------------------------------------------
2368
2369   SUBROUTINE iom_setkt( kt, cdname )
2370      INTEGER         , INTENT(in)::   kt 
2371      CHARACTER(LEN=*), INTENT(in) ::   cdname
2372      IF( .FALSE. )   WRITE(numout,*) kt, cdname   ! useless test to avoid compilation warnings
2373   END SUBROUTINE iom_setkt
2374
2375   SUBROUTINE iom_context_finalize( cdname )
2376      CHARACTER(LEN=*), INTENT(in) ::   cdname
2377      IF( .FALSE. )   WRITE(numout,*)  cdname   ! useless test to avoid compilation warnings
2378   END SUBROUTINE iom_context_finalize
2379
2380#endif
2381
2382   LOGICAL FUNCTION iom_use( cdname )
2383      !!----------------------------------------------------------------------
2384      !!----------------------------------------------------------------------
2385      CHARACTER(LEN=*), INTENT(in) ::   cdname
2386      !!----------------------------------------------------------------------
2387#if defined key_iomput
2388      iom_use = xios_field_is_active( cdname )
2389#else
2390      iom_use = .FALSE.
2391#endif
2392   END FUNCTION iom_use
2393   
2394   !!======================================================================
2395END MODULE iom
Note: See TracBrowser for help on using the repository browser.