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

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

#1962 put back tabs, spaces and lines removed when implementiong XIOS write functionality

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