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

source: branches/UKMO/dev_r7573_xios_write/NEMOGCM/NEMO/OPA_SRC/IOM/iom.F90 @ 8079

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

#1882 a first working version with XIOS writing restart file. Works with MO suite u-am389

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