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

Last change on this file since 8442 was 8295, checked in by andmirek, 7 years ago

fix restart variable definition: see change 8293

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