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

source: branches/2017/dev_r8600_xios_read_write/NEMOGCM/NEMO/OPA_SRC/IOM/iom.F90 @ 8792

Last change on this file since 8792 was 8792, checked in by andmirek, 6 years ago

#1953 and #1962 rename rst_fields to rst_rfields

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