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

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

change names of the new subroutines

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