New URL for NEMO forge!   http://forge.nemo-ocean.eu

Since March 2022 along with NEMO 4.2 release, the code development moved to a self-hosted GitLab.
This present forge is now archived and remained online for history.
iom.F90 in branches/UKMO/test_moci_test_suite/NEMOGCM/NEMO/OPA_SRC/IOM – NEMO

source: branches/UKMO/test_moci_test_suite/NEMOGCM/NEMO/OPA_SRC/IOM/iom.F90 @ 8257

Last change on this file since 8257 was 8257, checked in by andmirek, 8 years ago

few improvements and fixe to run GO6

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