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 @ 8785

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

#1953 and #1962 change in subroutine name and way temperature first level is read

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