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

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

#1962 change variable names to follow NEMO coding convention

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