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

source: branches/2017/dev_r8600_xios_read_write_v2/NEMOGCM/NEMO/OPA_SRC/IOM/iom.F90 @ 8831

Last change on this file since 8831 was 8812, checked in by andmirek, 7 years ago

#1953 and #1962 enable restart read/write with XIOS in SAS

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