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

source: branches/2017/dev_r7881_ENHANCE09_RK3/NEMOGCM/NEMO/OPA_SRC/IOM/iom.F90 @ 8850

Last change on this file since 8850 was 8850, checked in by gm, 6 years ago

#1911 (ENHANCE-09): PART I.3 - phasing with trunk: tracer trends output (see #1877 trunk change from 86666 to 8698)

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