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

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

source: branches/2017/dev_r8600_xios_write/NEMOGCM/NEMO/OPA_SRC/IOM/iom.F90 @ 8761

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

add ln_diurnal for XIOS definitions

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