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

source: branches/2017/dev_r8126_ROBUST08_no_ghost/NEMOGCM/NEMO/OPA_SRC/IOM/iom.F90 @ 8979

Last change on this file since 8979 was 8979, checked in by acc, 6 years ago

Branch 2017/dev_r8126_ROBUST08_no_ghost. Merge in trunk changes up to rev 8864 in preparation for the merge. Sette tests OK except for AGRIF restartability but uncertain about the trunks status for this?

  • Property svn:keywords set to Id
File size: 97.0 KB
Line 
1MODULE iom
2   !!=====================================================================
3   !!                    ***  MODULE  iom ***
4   !! Input/Output manager :  Library to read input files
5   !!====================================================================
6   !! History :  2.0  ! 2005-12  (J. Belier) Original code
7   !!            2.0  ! 2006-02  (S. Masson) Adaptation to NEMO
8   !!            3.0  ! 2007-07  (D. Storkey) Changes to iom_gettime
9   !!            3.4  ! 2012-12  (R. Bourdalle-Badie and G. Reffray)  add C1D case 
10   !!            3.6  ! 2014-15  DIMG format removed
11   !!            3.6  ! 2015-15  (J. Harle) Added procedure to read REAL attributes
12   !!--------------------------------------------------------------------
13
14   !!--------------------------------------------------------------------
15   !!   iom_open       : open a file read only
16   !!   iom_close      : close a file or all files opened by iom
17   !!   iom_get        : read a field (interfaced to several routines)
18   !!   iom_gettime    : read the time axis cdvar in the file
19   !!   iom_varid      : get the id of a variable in a file
20   !!   iom_rstput     : write a field in a restart file (interfaced to several routines)
21   !!--------------------------------------------------------------------
22   USE dom_oce         ! ocean space and time domain
23   USE c1d             ! 1D vertical configuration
24   USE flo_oce         ! floats module declarations
25   USE lbclnk          ! lateal boundary condition / mpp exchanges
26   USE iom_def         ! iom variables definitions
27   USE iom_nf90        ! NetCDF format with native NetCDF library
28   USE in_out_manager  ! I/O manager
29   USE lib_mpp           ! MPP library
30#if defined key_iomput
31   USE sbc_oce, ONLY :   nn_fsbc         ! ocean space and time domain
32   USE trc_oce, ONLY :   nn_dttrc        !  !: frequency of step on passive tracers
33   USE icb_oce, ONLY :   nclasses, class_num       !  !: iceberg classes
34#if defined key_lim3
35   USE ice    , ONLY :   jpl
36#elif defined key_lim2
37   USE par_ice_2
38#endif
39   USE domngb          ! ocean space and time domain
40   USE phycst          ! physical constants
41   USE dianam          ! build name of file
42   USE xios
43# endif
44   USE ioipsl, ONLY :  ju2ymds    ! for calendar
45   USE crs             ! Grid coarsening
46
47   IMPLICIT NONE
48   PUBLIC   !   must be public to be able to access iom_def through iom
49   
50#if defined key_iomput
51   LOGICAL, PUBLIC, PARAMETER ::   lk_iomput = .TRUE.        !: iom_put flag
52#else
53   LOGICAL, PUBLIC, PARAMETER ::   lk_iomput = .FALSE.       !: iom_put flag
54#endif
55   PUBLIC iom_init, iom_swap, iom_open, iom_close, iom_setkt, iom_varid, iom_get
56   PUBLIC iom_getatt, iom_putatt, iom_gettime, iom_rstput, iom_put
57   PUBLIC iom_use, iom_context_finalize
58
59   PRIVATE iom_rp0d, iom_rp1d, iom_rp2d, iom_rp3d
60   PRIVATE iom_g0d, iom_g1d, iom_g2d, iom_g3d, iom_get_123d
61   PRIVATE iom_p1d, iom_p2d, iom_p3d
62#if defined key_iomput
63   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
64   PRIVATE set_grid, set_grid_bounds, set_scalar, set_xmlatt, set_mooring, iom_update_file_name, iom_sdate
65# endif
66
67   INTERFACE iom_get
68      MODULE PROCEDURE iom_g0d, iom_g1d, iom_g2d, iom_g3d
69   END INTERFACE
70   INTERFACE iom_getatt
71      MODULE PROCEDURE iom_g0d_iatt, iom_g0d_ratt, iom_g0d_catt
72   END INTERFACE
73   INTERFACE iom_putatt
74      MODULE PROCEDURE iom_p0d_iatt, iom_p0d_ratt, iom_p0d_catt
75   END INTERFACE
76   INTERFACE iom_rstput
77      MODULE PROCEDURE iom_rp0d, iom_rp1d, iom_rp2d, iom_rp3d
78   END INTERFACE
79  INTERFACE iom_put
80     MODULE PROCEDURE iom_p0d, iom_p1d, iom_p2d, iom_p3d
81  END INTERFACE
82
83   !!----------------------------------------------------------------------
84   !! NEMO/OPA 3.3 , NEMO Consortium (2010)
85   !! $Id$
86   !! Software governed by the CeCILL licence (NEMOGCM/NEMO_CeCILL.txt)
87   !!----------------------------------------------------------------------
88
89CONTAINS
90
91   SUBROUTINE iom_init( cdname ) 
92      !!----------------------------------------------------------------------
93      !!                     ***  ROUTINE   ***
94      !!
95      !! ** Purpose :   
96      !!
97      !!----------------------------------------------------------------------
98      CHARACTER(len=*), INTENT(in)  :: cdname
99#if defined key_iomput
100
101      TYPE(xios_duration) :: dtime    = xios_duration(0, 0, 0, 0, 0, 0)
102      TYPE(xios_date)     :: start_date
103      CHARACTER(len=10) :: clname
104      INTEGER           :: ji, jkmin
105      !
106      REAL(wp), ALLOCATABLE, DIMENSION(:,:) :: zt_bnds, zw_bnds
107      !!----------------------------------------------------------------------
108
109      ALLOCATE( zt_bnds(2,jpk), zw_bnds(2,jpk) )
110
111      clname = cdname
112      IF( TRIM(Agrif_CFixed()) /= '0' )   clname = TRIM(Agrif_CFixed())//"_"//TRIM(cdname)
113      CALL xios_context_initialize(TRIM(clname), mpi_comm_opa)
114      CALL iom_swap( cdname )
115
116
117      ! Calendar type is now defined in xml file
118      SELECT CASE ( nleapy )        ! Choose calendar for IOIPSL
119      CASE ( 1)   ; CALL xios_define_calendar( TYPE = "Gregorian", time_origin = xios_date(1900,01,01,00,00,00), &
120          &                                    start_date = xios_date(nyear,nmonth,nday,0,0,0) )
121      CASE ( 0)   ; CALL xios_define_calendar( TYPE = "NoLeap"   , time_origin = xios_date(1900,01,01,00,00,00), &
122          &                                    start_date = xios_date(nyear,nmonth,nday,0,0,0) )
123      CASE (30)   ; CALL xios_define_calendar( TYPE = "D360"     , time_origin = xios_date(1900,01,01,00,00,00), &
124          &                                    start_date = xios_date(nyear,nmonth,nday,0,0,0) )
125      END SELECT
126
127      ! horizontal grid definition
128      CALL set_scalar
129
130      IF( TRIM(cdname) == TRIM(cxios_context) ) THEN 
131         CALL set_grid( "T", glamt, gphit ) 
132         CALL set_grid( "U", glamu, gphiu )
133         CALL set_grid( "V", glamv, gphiv )
134         CALL set_grid( "W", glamt, gphit )
135         CALL set_grid_znl( gphit )
136         !
137         IF( ln_cfmeta ) THEN   ! Add additional grid metadata
138            CALL iom_set_domain_attr("grid_T", area = e1e2t(nldi:nlei, nldj:nlej))
139            CALL iom_set_domain_attr("grid_U", area = e1e2u(nldi:nlei, nldj:nlej))
140            CALL iom_set_domain_attr("grid_V", area = e1e2v(nldi:nlei, nldj:nlej))
141            CALL iom_set_domain_attr("grid_W", area = e1e2t(nldi:nlei, nldj:nlej))
142            CALL set_grid_bounds( "T", glamf, gphif, glamt, gphit )
143            CALL set_grid_bounds( "U", glamv, gphiv, glamu, gphiu )
144            CALL set_grid_bounds( "V", glamu, gphiu, glamv, gphiv )
145            CALL set_grid_bounds( "W", glamf, gphif, glamt, gphit )
146         ENDIF
147      ENDIF
148
149      IF( TRIM(cdname) == TRIM(cxios_context)//"_crs" ) THEN 
150         CALL dom_grid_crs   ! Save the parent grid information  & Switch to coarse grid domain
151         !
152         CALL set_grid( "T", glamt_crs, gphit_crs ) 
153         CALL set_grid( "U", glamu_crs, gphiu_crs ) 
154         CALL set_grid( "V", glamv_crs, gphiv_crs ) 
155         CALL set_grid( "W", glamt_crs, gphit_crs ) 
156         CALL set_grid_znl( gphit_crs )
157          !
158         CALL dom_grid_glo   ! Return to parent grid domain
159         !
160         IF( ln_cfmeta ) THEN   ! Add additional grid metadata
161            CALL iom_set_domain_attr("grid_T", area = e1e2t_crs(nldi:nlei, nldj:nlej))
162            CALL iom_set_domain_attr("grid_U", area = e1u_crs(nldi:nlei, nldj:nlej) * e2u_crs(nldi:nlei, nldj:nlej))
163            CALL iom_set_domain_attr("grid_V", area = e1v_crs(nldi:nlei, nldj:nlej) * e2v_crs(nldi:nlei, nldj:nlej))
164            CALL iom_set_domain_attr("grid_W", area = e1e2t_crs(nldi:nlei, nldj:nlej))
165            CALL set_grid_bounds( "T", glamf_crs, gphif_crs, glamt_crs, gphit_crs )
166            CALL set_grid_bounds( "U", glamv_crs, gphiv_crs, glamu_crs, gphiu_crs )
167            CALL set_grid_bounds( "V", glamu_crs, gphiu_crs, glamv_crs, gphiv_crs )
168            CALL set_grid_bounds( "W", glamf_crs, gphif_crs, glamt_crs, gphit_crs )
169         ENDIF
170      ENDIF
171
172      ! vertical grid definition
173      CALL iom_set_axis_attr( "deptht", gdept_1d )
174      CALL iom_set_axis_attr( "depthu", gdept_1d )
175      CALL iom_set_axis_attr( "depthv", gdept_1d )
176      CALL iom_set_axis_attr( "depthw", gdepw_1d )
177
178      ! Add vertical grid bounds
179      jkmin = MIN(2,jpk)  ! in case jpk=1 (i.e. sas2D)
180      zt_bnds(2,:        ) = gdept_1d(:)
181      zt_bnds(1,jkmin:jpk) = gdept_1d(1:jpkm1)
182      zt_bnds(1,1        ) = gdept_1d(1) - e3w_1d(1)
183      zw_bnds(1,:        ) = gdepw_1d(:)
184      zw_bnds(2,1:jpkm1  ) = gdepw_1d(jkmin:jpk)
185      zw_bnds(2,jpk:     ) = gdepw_1d(jpk) + e3t_1d(jpk)
186      CALL iom_set_axis_attr( "deptht", bounds=zt_bnds )
187      CALL iom_set_axis_attr( "depthu", bounds=zt_bnds )
188      CALL iom_set_axis_attr( "depthv", bounds=zt_bnds )
189      CALL iom_set_axis_attr( "depthw", bounds=zw_bnds )
190
191
192# if defined key_floats
193      CALL iom_set_axis_attr( "nfloat", (/ (REAL(ji,wp), ji=1,nfloat) /) )
194# endif
195#if defined key_lim3 || defined key_lim2
196      CALL iom_set_axis_attr( "ncatice", (/ (REAL(ji,wp), ji=1,jpl) /) )
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     
210      CALL xios_update_calendar(0)
211
212      DEALLOCATE( zt_bnds, zw_bnds )
213
214#endif
215     
216   END SUBROUTINE iom_init
217
218
219   SUBROUTINE iom_swap( cdname )
220      !!---------------------------------------------------------------------
221      !!                   ***  SUBROUTINE  iom_swap  ***
222      !!
223      !! ** Purpose :  swap context between different agrif grid for xmlio_server
224      !!---------------------------------------------------------------------
225      CHARACTER(len=*), INTENT(in) :: cdname
226#if defined key_iomput
227      TYPE(xios_context) :: nemo_hdl
228
229      IF( TRIM(Agrif_CFixed()) == '0' ) THEN
230        CALL xios_get_handle(TRIM(cdname),nemo_hdl)
231      ELSE
232        CALL xios_get_handle(TRIM(Agrif_CFixed())//"_"//TRIM(cdname),nemo_hdl)
233      ENDIF
234      !
235      CALL xios_set_current_context(nemo_hdl)
236#endif
237      !
238   END SUBROUTINE iom_swap
239
240
241   SUBROUTINE iom_open( cdname, kiomid, ldwrt, kdom, kiolib, ldstop, ldiof )
242      !!---------------------------------------------------------------------
243      !!                   ***  SUBROUTINE  iom_open  ***
244      !!
245      !! ** Purpose :  open an input file (return 0 if not found)
246      !!---------------------------------------------------------------------
247      CHARACTER(len=*), INTENT(in   )           ::   cdname   ! File name
248      INTEGER         , INTENT(  out)           ::   kiomid   ! iom identifier of the opened file
249      LOGICAL         , INTENT(in   ), OPTIONAL ::   ldwrt    ! open in write modeb          (default = .FALSE.)
250      INTEGER         , INTENT(in   ), OPTIONAL ::   kdom     ! Type of domain to be written (default = jpdom_local_noovlap)
251      INTEGER         , INTENT(in   ), OPTIONAL ::   kiolib   ! library used to open the file (default = jpnf90)
252      LOGICAL         , INTENT(in   ), OPTIONAL ::   ldstop   ! stop if open to read a non-existing file (default = .TRUE.)
253      LOGICAL         , INTENT(in   ), OPTIONAL ::   ldiof    ! Interp On the Fly, needed for AGRIF (default = .FALSE.)
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 )
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      !---------------------------------------------------------------------
675      !
676      clname = iom_file(kiomid)%name   !   esier to read
677      clinfo = '          iom_get_123d, file: '//trim(clname)//', var: '//trim(cdvar)
678      ! local definition of the domain ?
679      idom = kdom
680      ! do we read the overlap
681      ! ugly patch SM+JMM+RB to overwrite global definition in some cases
682      llnoov = (jpni * jpnj ) == jpnij .AND. .NOT. lk_agrif 
683      ! check kcount and kstart optionals parameters...
684      IF( PRESENT(kcount) .AND. (.NOT. PRESENT(kstart)) ) CALL ctl_stop(trim(clinfo), 'kcount present needs kstart present')
685      IF( PRESENT(kstart) .AND. (.NOT. PRESENT(kcount)) ) CALL ctl_stop(trim(clinfo), 'kstart present needs kcount present')
686      IF( PRESENT(kstart) .AND. idom /= jpdom_unknown .AND.  idom /= jpdom_autoglo_xy  ) &
687     &           CALL ctl_stop(trim(clinfo), 'kstart present needs kdom = jpdom_unknown or kdom = jpdom_autoglo_xy')
688
689      luse_jattr = .false.
690      IF( PRESENT(lrowattr) ) THEN
691         IF( lrowattr .AND. idom /= jpdom_data   ) CALL ctl_stop(trim(clinfo), 'lrowattr present and true needs kdom = jpdom_data')
692         IF( lrowattr .AND. idom == jpdom_data   ) luse_jattr = .true.
693      ENDIF
694      IF( luse_jattr ) THEN
695         SELECT CASE (iom_file(kiomid)%iolib)
696         CASE (jpnf90   )   
697             ! Ok
698         CASE DEFAULT   
699            CALL ctl_stop( TRIM(clinfo)//' accepted IO library is only jpnf90 (jpioipsl option has been removed)' )
700         END SELECT
701      ENDIF
702
703      ! Search for the variable in the data base (eventually actualize data)
704      istop = nstop
705      idvar = iom_varid( kiomid, cdvar )
706      !
707      IF( idvar > 0 ) THEN
708         ! to write iom_file(kiomid)%dimsz in a shorter way !
709         idimsz(:) = iom_file(kiomid)%dimsz(:, idvar) 
710         inbdim = iom_file(kiomid)%ndims(idvar)            ! number of dimensions in the file
711         idmspc = inbdim                                   ! number of spatial dimensions in the file
712         IF( iom_file(kiomid)%luld(idvar) )   idmspc = inbdim - 1
713         IF( idmspc > 3 )   CALL ctl_stop(trim(clinfo), 'the file has more than 3 spatial dimensions this case is not coded...') 
714         !
715         ! update idom definition...
716         ! Identify the domain in case of jpdom_auto(glo/dta) definition
717         IF( idom == jpdom_autoglo_xy ) THEN
718            ll_depth_spec = .TRUE.
719            idom = jpdom_autoglo
720         ELSE
721            ll_depth_spec = .FALSE.
722         ENDIF
723         IF( idom == jpdom_autoglo .OR. idom == jpdom_autodta ) THEN           
724            IF( idom == jpdom_autoglo ) THEN   ;   idom = jpdom_global 
725            ELSE                               ;   idom = jpdom_data
726            ENDIF
727            ind1 = INDEX( clname, '_', back = .TRUE. ) + 1
728            ind2 = INDEX( clname, '.', back = .TRUE. ) - 1
729            IF( ind2 > ind1 ) THEN   ;   IF( VERIFY( clname(ind1:ind2), '0123456789' ) == 0 )   idom = jpdom_local   ;   ENDIF
730         ENDIF
731         ! Identify the domain in case of jpdom_local definition
732         IF( idom == jpdom_local ) THEN
733            IF(     idimsz(1) == jpi               .AND. idimsz(2) == jpj               ) THEN   ;   idom = jpdom_local_full
734            ELSEIF( idimsz(1) == nlci              .AND. idimsz(2) == nlcj              ) THEN   ;   idom = jpdom_local_noextra
735            ELSEIF( idimsz(1) == (nlei - nldi + 1) .AND. idimsz(2) == (nlej - nldj + 1) ) THEN   ;   idom = jpdom_local_noovlap
736            ELSE   ;   CALL ctl_stop( trim(clinfo), 'impossible to identify the local domain' )
737            ENDIF
738         ENDIF
739         !
740         ! check the consistency between input array and data rank in the file
741         !
742         ! initializations
743         itime = 1
744         IF( PRESENT(ktime) ) itime = ktime
745
746         irankpv = 1 * COUNT( (/PRESENT(pv_r1d)/) ) + 2 * COUNT( (/PRESENT(pv_r2d)/) ) + 3 * COUNT( (/PRESENT(pv_r3d)/) )
747         WRITE(clrankpv, fmt='(i1)') irankpv
748         WRITE(cldmspc , fmt='(i1)') idmspc
749         !
750         IF(     idmspc <  irankpv ) THEN
751            CALL ctl_stop( TRIM(clinfo), 'The file has only '//cldmspc//' spatial dimension',   &
752               &                         'it is impossible to read a '//clrankpv//'D array from this file...' )
753         ELSEIF( idmspc == irankpv ) THEN
754            IF( PRESENT(pv_r1d) .AND. idom /= jpdom_unknown )   &
755               &   CALL ctl_stop( TRIM(clinfo), 'case not coded...You must use jpdom_unknown' )
756         ELSEIF( idmspc >  irankpv ) THEN
757               IF( PRESENT(pv_r2d) .AND. itime == 1 .AND. idimsz(3) == 1 .AND. idmspc == 3 ) THEN
758                  CALL ctl_warn( trim(clinfo), '2D array but 3 spatial dimensions for the data...'              ,   &
759                        &         'As the size of the z dimension is 1 and as we try to read the first record, ',   &
760                        &         'we accept this case, even if there is a possible mix-up between z and time dimension' )   
761                  idmspc = idmspc - 1
762               ELSE
763                  CALL ctl_stop( TRIM(clinfo), 'To keep iom lisibility, when reading a '//clrankpv//'D array,'         ,   &
764                     &                         'we do not accept data with '//cldmspc//' spatial dimensions',   &
765                     &                         'Use ncwa -a to suppress the unnecessary dimensions' )
766               ENDIF
767         ENDIF
768
769         !
770         ! definition of istart and icnt
771         !
772         icnt  (:) = 1
773         istart(:) = 1
774         istart(idmspc+1) = itime
775
776         IF( PRESENT(kstart) .AND. .NOT. ll_depth_spec ) THEN
777            istart(1:idmspc) = kstart(1:idmspc) ; icnt(1:idmspc) = kcount(1:idmspc)
778         ELSE
779            IF(idom == jpdom_unknown ) THEN
780               icnt(1:idmspc) = idimsz(1:idmspc)
781            ELSE
782               IF( .NOT. PRESENT(pv_r1d) ) THEN   !   not a 1D array
783                  IF(     idom == jpdom_data    ) THEN
784                     jstartrow = 1
785                     IF( luse_jattr ) THEN
786                        CALL iom_getatt(kiomid, 'open_ocean_jstart', jstartrow ) ! -999 is returned if the attribute is not found
787                        jstartrow = MAX(1,jstartrow)
788                     ENDIF
789                     istart(1:2) = (/ mig(1), mjg(1) + jstartrow - 1 /)  ! icnt(1:2) done below
790                  ELSEIF( idom == jpdom_global  ) THEN ; istart(1:2) = (/ nimpp , njmpp  /)  ! icnt(1:2) done below
791                  ENDIF
792                  ! we do not read the overlap                     -> we start to read at nldi, nldj
793! JMM + SM: ugly patch before getting the new version of lib_mpp)
794!                  IF( idom /= jpdom_local_noovlap )   istart(1:2) = istart(1:2) + (/ nldi - 1, nldj - 1 /)
795                  IF( llnoov .AND. idom /= jpdom_local_noovlap ) istart(1:2) = istart(1:2) + (/ nldi - 1, nldj - 1 /)
796                  ! we do not read the overlap and the extra-halos -> from nldi to nlei and from nldj to nlej
797! JMM + SM: ugly patch before getting the new version of lib_mpp)
798!                  icnt(1:2) = (/ nlei - nldi + 1, nlej - nldj + 1 /)
799                  IF( llnoov ) THEN   ;   icnt(1:2) = (/ nlei - nldi + 1, nlej - nldj + 1 /)
800                  ELSE                ;   icnt(1:2) = (/ nlci           , nlcj            /)
801                  ENDIF
802                  IF( PRESENT(pv_r3d) ) THEN
803                     IF( idom == jpdom_data ) THEN                                  ; icnt(3) = jpkglo
804                     ELSE IF( ll_depth_spec .AND. PRESENT(kstart) ) THEN            ; istart(3) = kstart(3); icnt(3) = kcount(3)
805                     ELSE                                                           ; icnt(3) = jpk
806                     ENDIF
807                  ENDIF
808               ENDIF
809            ENDIF
810         ENDIF
811
812         ! check that istart and icnt can be used with this file
813         !-
814         DO jl = 1, jpmax_dims
815            itmp = istart(jl)+icnt(jl)-1
816            IF( itmp > idimsz(jl) .AND. idimsz(jl) /= 0 ) THEN
817               WRITE( ctmp1, FMT="('(istart(', i1, ') + icnt(', i1, ') - 1) = ', i5)" ) jl, jl, itmp
818               WRITE( ctmp2, FMT="(' is larger than idimsz(', i1,') = ', i5)"         ) jl, idimsz(jl)
819               CALL ctl_stop( trim(clinfo), 'start and count too big regarding to the size of the data, ', ctmp1, ctmp2 )     
820            ENDIF
821         END DO
822
823         ! check that icnt matches the input array
824         !-     
825         IF( idom == jpdom_unknown ) THEN
826            IF( irankpv == 1 )        ishape(1:1) = SHAPE(pv_r1d)
827            IF( irankpv == 2 )        ishape(1:2) = SHAPE(pv_r2d)
828            IF( irankpv == 3 )        ishape(1:3) = SHAPE(pv_r3d)
829            ctmp1 = 'd'
830         ELSE
831            IF( irankpv == 2 ) THEN
832! JMM + SM: ugly patch before getting the new version of lib_mpp)
833!               ishape(1:2) = SHAPE(pv_r2d(nldi:nlei,nldj:nlej  ))   ;   ctmp1 = 'd(nldi:nlei,nldj:nlej)'
834               IF( llnoov ) THEN ; ishape(1:2)=SHAPE(pv_r2d(nldi:nlei,nldj:nlej  )) ; ctmp1='d(nldi:nlei,nldj:nlej)'
835               ELSE              ; ishape(1:2)=SHAPE(pv_r2d(1   :nlci,1   :nlcj  )) ; ctmp1='d(1:nlci,1:nlcj)'
836               ENDIF
837            ENDIF
838            IF( irankpv == 3 ) THEN 
839! JMM + SM: ugly patch before getting the new version of lib_mpp)
840!               ishape(1:3) = SHAPE(pv_r3d(nldi:nlei,nldj:nlej,:))   ;   ctmp1 = 'd(nldi:nlei,nldj:nlej,:)'
841               IF( llnoov ) THEN ; ishape(1:3)=SHAPE(pv_r3d(nldi:nlei,nldj:nlej,:)) ; ctmp1='d(nldi:nlei,nldj:nlej,:)'
842               ELSE              ; ishape(1:3)=SHAPE(pv_r3d(1   :nlci,1   :nlcj,:)) ; ctmp1='d(1:nlci,1:nlcj,:)'
843               ENDIF
844            ENDIF
845         ENDIF
846         
847         DO jl = 1, irankpv
848            WRITE( ctmp2, FMT="(', ', i1,'): ', i5,' /= icnt(', i1,'):', i5)" ) jl, ishape(jl), jl, icnt(jl)
849            IF( ishape(jl) /= icnt(jl) )   CALL ctl_stop( TRIM(clinfo), 'size(pv_r'//clrankpv//TRIM(ctmp1)//TRIM(ctmp2) )
850         END DO
851
852      ENDIF
853
854      ! read the data
855      !-     
856      IF( idvar > 0 .AND. istop == nstop ) THEN   ! no additional errors until this point...
857         !
858         ! find the right index of the array to be read
859! JMM + SM: ugly patch before getting the new version of lib_mpp)
860!         IF( idom /= jpdom_unknown ) THEN   ;   ix1 = nldi   ;   ix2 = nlei      ;   iy1 = nldj   ;   iy2 = nlej
861!         ELSE                               ;   ix1 = 1      ;   ix2 = icnt(1)   ;   iy1 = 1      ;   iy2 = icnt(2)
862!         ENDIF
863         IF( llnoov ) THEN
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         ELSE
868            IF( idom /= jpdom_unknown ) THEN   ;   ix1 = 1      ;   ix2 = nlci      ;   iy1 = 1      ;   iy2 = nlcj
869            ELSE                               ;   ix1 = 1      ;   ix2 = icnt(1)   ;   iy1 = 1      ;   iy2 = icnt(2)
870            ENDIF
871         ENDIF
872     
873         SELECT CASE (iom_file(kiomid)%iolib)
874         CASE (jpnf90   )   ;   CALL iom_nf90_get(    kiomid, idvar, inbdim, istart, icnt, ix1, ix2, iy1, iy2,   &
875            &                                         pv_r1d, pv_r2d, pv_r3d )
876         CASE DEFAULT
877            CALL ctl_stop( TRIM(clinfo)//' accepted IO library is only jpnf90 (jpioipsl option has been removed)' )
878         END SELECT
879
880         IF( istop == nstop ) THEN   ! no additional errors until this point...
881            IF(lwp) WRITE(numout,"(10x,' read ',a,' (rec: ',i6,') in ',a,' ok')") TRIM(cdvar), itime, TRIM(iom_file(kiomid)%name)
882         
883            !--- overlap areas and extra hallows (mpp)
884            IF(     PRESENT(pv_r2d) .AND. idom /= jpdom_unknown ) THEN
885               CALL lbc_lnk( pv_r2d,'Z',-999.,'no0' )
886            ELSEIF( PRESENT(pv_r3d) .AND. idom /= jpdom_unknown ) THEN
887               ! this if could be simplified with the new lbc_lnk that works with any size of the 3rd dimension
888               IF( icnt(3) == jpk ) THEN
889                  CALL lbc_lnk( pv_r3d,'Z',-999.,'no0' )
890               ELSE   ! put some arbitrary value (a call to lbc_lnk will be done later...)
891                  DO jj = nlcj+1, jpj   ;   pv_r3d(1:nlci, jj, :) = pv_r3d(1:nlci, nlej, :)   ;   END DO
892                  DO ji = nlci+1, jpi   ;   pv_r3d(ji    , : , :) = pv_r3d(nlei  , :   , :)   ;   END DO
893               ENDIF
894            ENDIF
895           
896            ! C1D case : always call lbc_lnk to replicate the central value over the whole 3X3 domain
897            IF( lk_c1d .AND. PRESENT(pv_r2d) )   CALL lbc_lnk( pv_r2d,'Z',1. )
898            IF( lk_c1d .AND. PRESENT(pv_r3d) )   CALL lbc_lnk( pv_r3d,'Z',1. )
899   
900            !--- Apply scale_factor and offset
901            zscf = iom_file(kiomid)%scf(idvar)      ! scale factor
902            zofs = iom_file(kiomid)%ofs(idvar)      ! offset
903            IF(     PRESENT(pv_r1d) ) THEN
904               IF( zscf /= 1. )   pv_r1d(:) = pv_r1d(:) * zscf 
905               IF( zofs /= 0. )   pv_r1d(:) = pv_r1d(:) + zofs
906            ELSEIF( PRESENT(pv_r2d) ) THEN
907               IF( zscf /= 1.)   pv_r2d(:,:) = pv_r2d(:,:) * zscf
908               IF( zofs /= 0.)   pv_r2d(:,:) = pv_r2d(:,:) + zofs
909            ELSEIF( PRESENT(pv_r3d) ) THEN
910               IF( zscf /= 1.)   pv_r3d(:,:,:) = pv_r3d(:,:,:) * zscf
911               IF( zofs /= 0.)   pv_r3d(:,:,:) = pv_r3d(:,:,:) + zofs
912            ENDIF
913            !
914         ENDIF
915         !
916      ENDIF
917      !
918   END SUBROUTINE iom_get_123d
919
920
921   SUBROUTINE iom_gettime( kiomid, ptime, cdvar, kntime, cdunits, cdcalendar )
922      !!--------------------------------------------------------------------
923      !!                   ***  SUBROUTINE iom_gettime  ***
924      !!
925      !! ** Purpose : read the time axis cdvar in the file
926      !!--------------------------------------------------------------------
927      INTEGER                    , INTENT(in   ) ::   kiomid     ! file Identifier
928      REAL(wp), DIMENSION(:)     , INTENT(  out) ::   ptime      ! the time axis
929      CHARACTER(len=*), OPTIONAL , INTENT(in   ) ::   cdvar      ! time axis name
930      INTEGER         , OPTIONAL , INTENT(  out) ::   kntime     ! number of times in file
931      CHARACTER(len=*), OPTIONAL , INTENT(  out) ::   cdunits    ! units attribute of time coordinate
932      CHARACTER(len=*), OPTIONAL , INTENT(  out) ::   cdcalendar ! calendar attribute of
933      !
934      INTEGER, DIMENSION(1) :: kdimsz
935      INTEGER            ::   idvar    ! id of the variable
936      CHARACTER(LEN=32)  ::   tname    ! local name of time coordinate
937      CHARACTER(LEN=100) ::   clinfo   ! info character
938      !---------------------------------------------------------------------
939      !
940      IF ( PRESENT(cdvar) ) THEN
941         tname = cdvar
942      ELSE
943         tname = iom_file(kiomid)%uldname
944      ENDIF
945      IF( kiomid > 0 ) THEN
946         clinfo = 'iom_gettime, file: '//trim(iom_file(kiomid)%name)//', var: '//trim(tname)
947         IF ( PRESENT(kntime) ) THEN
948            idvar  = iom_varid( kiomid, tname, kdimsz = kdimsz )
949            kntime = kdimsz(1)
950         ELSE
951            idvar = iom_varid( kiomid, tname )
952         ENDIF
953         !
954         ptime(:) = 0. ! default definition
955         IF( idvar > 0 ) THEN
956            IF( iom_file(kiomid)%ndims(idvar) == 1 ) THEN
957               IF( iom_file(kiomid)%luld(idvar) ) THEN
958                  IF( iom_file(kiomid)%dimsz(1,idvar) <= size(ptime) ) THEN
959                     SELECT CASE (iom_file(kiomid)%iolib)
960                     CASE (jpnf90   )   ;   CALL iom_nf90_gettime(   kiomid, idvar, ptime, cdunits, cdcalendar )
961                     CASE DEFAULT
962                        CALL ctl_stop( TRIM(clinfo)//' accepted IO library is only jpnf90 (jpioipsl option has been removed)' )
963                     END SELECT
964                  ELSE
965                     WRITE(ctmp1,*) 'error with the size of ptime ',size(ptime),iom_file(kiomid)%dimsz(1,idvar)
966                     CALL ctl_stop( trim(clinfo), trim(ctmp1) )
967                  ENDIF
968               ELSE
969                  CALL ctl_stop( trim(clinfo), 'variable dimension is not unlimited... use iom_get' )
970               ENDIF
971            ELSE
972               CALL ctl_stop( trim(clinfo), 'the variable has more than 1 dimension' )
973            ENDIF
974         ELSE
975            CALL ctl_stop( trim(clinfo), 'variable not found in '//iom_file(kiomid)%name )
976         ENDIF
977      ENDIF
978      !
979   END SUBROUTINE iom_gettime
980
981
982   !!----------------------------------------------------------------------
983   !!                   INTERFACE iom_getatt
984   !!----------------------------------------------------------------------
985   SUBROUTINE iom_g0d_iatt( kiomid, cdatt, pvar, cdvar )
986      INTEGER         , INTENT(in   )                 ::   kiomid    ! Identifier of the file
987      CHARACTER(len=*), INTENT(in   )                 ::   cdatt     ! Name of the attribute
988      INTEGER         , INTENT(  out)                 ::   pvar      ! read field
989      CHARACTER(len=*), INTENT(in   ), OPTIONAL       ::   cdvar     ! Name of the variable
990      !
991      IF( kiomid > 0 ) THEN
992         IF( iom_file(kiomid)%nfid > 0 ) THEN
993            SELECT CASE (iom_file(kiomid)%iolib)
994            CASE (jpnf90   )   ;   IF( PRESENT(cdvar) ) THEN
995                                      CALL iom_nf90_getatt( kiomid, cdatt, pvar, cdvar=cdvar )
996                                   ELSE
997                                      CALL iom_nf90_getatt( kiomid, cdatt, pvar )
998                                   ENDIF
999            CASE DEFAULT
1000               CALL ctl_stop( 'iom_g0d_iatt: accepted IO library is only jpnf90' )
1001            END SELECT
1002         ENDIF
1003      ENDIF
1004   END SUBROUTINE iom_g0d_iatt
1005
1006   SUBROUTINE iom_g0d_ratt( kiomid, cdatt, pvar, cdvar )
1007      INTEGER         , INTENT(in   )                 ::   kiomid    ! Identifier of the file
1008      CHARACTER(len=*), INTENT(in   )                 ::   cdatt     ! Name of the attribute
1009      REAL(wp)        , INTENT(  out)                 ::   pvar      ! written field
1010      CHARACTER(len=*), INTENT(in   ), OPTIONAL       ::   cdvar     ! Name of the variable
1011      !
1012      IF( kiomid > 0 ) THEN
1013         IF( iom_file(kiomid)%nfid > 0 ) THEN
1014            SELECT CASE (iom_file(kiomid)%iolib)
1015            CASE (jpnf90   )   ;   IF( PRESENT(cdvar) ) THEN
1016                                      CALL iom_nf90_getatt( kiomid, cdatt, pvar, cdvar=cdvar )
1017                                   ELSE
1018                                      CALL iom_nf90_getatt( kiomid, cdatt, pvar )
1019                                   ENDIF
1020            CASE DEFAULT   
1021               CALL ctl_stop( 'iom_g0d_ratt: accepted IO library is only jpnf90' )
1022            END SELECT
1023         ENDIF
1024      ENDIF
1025   END SUBROUTINE iom_g0d_ratt
1026
1027   SUBROUTINE iom_g0d_catt( kiomid, cdatt, pvar, cdvar )
1028      INTEGER         , INTENT(in   )                 ::   kiomid    ! Identifier of the file
1029      CHARACTER(len=*), INTENT(in   )                 ::   cdatt     ! Name of the attribute
1030      CHARACTER(len=*), INTENT(  out)                 ::   pvar      ! written field
1031      CHARACTER(len=*), INTENT(in   ), OPTIONAL       ::   cdvar     ! Name of the variable
1032      !
1033      IF( kiomid > 0 ) THEN
1034         IF( iom_file(kiomid)%nfid > 0 ) THEN
1035            SELECT CASE (iom_file(kiomid)%iolib)
1036            CASE (jpnf90   )   ;   IF( PRESENT(cdvar) ) THEN
1037                                      CALL iom_nf90_getatt( kiomid, cdatt, pvar, cdvar=cdvar )
1038                                   ELSE
1039                                      CALL iom_nf90_getatt( kiomid, cdatt, pvar )
1040                                   ENDIF
1041            CASE DEFAULT
1042               CALL ctl_stop( 'iom_g0d_ratt: accepted IO library is only jpnf90' )
1043            END SELECT
1044         ENDIF
1045      ENDIF
1046   END SUBROUTINE iom_g0d_catt
1047
1048   !!----------------------------------------------------------------------
1049   !!                   INTERFACE iom_putatt
1050   !!----------------------------------------------------------------------
1051   SUBROUTINE iom_p0d_iatt( kiomid, cdatt, pvar, cdvar )
1052      INTEGER         , INTENT(in   )                 ::   kiomid    ! Identifier of the file
1053      CHARACTER(len=*), INTENT(in   )                 ::   cdatt     ! Name of the attribute
1054      INTEGER         , INTENT(in   )                 ::   pvar      ! write field
1055      CHARACTER(len=*), INTENT(in   ), OPTIONAL       ::   cdvar     ! Name of the variable
1056      !
1057      IF( kiomid > 0 ) THEN
1058         IF( iom_file(kiomid)%nfid > 0 ) THEN
1059            SELECT CASE (iom_file(kiomid)%iolib)
1060            CASE (jpnf90   )   ;   IF( PRESENT(cdvar) ) THEN
1061                                      CALL iom_nf90_putatt( kiomid, cdatt, pvar, cdvar=cdvar )
1062                                   ELSE
1063                                      CALL iom_nf90_putatt( kiomid, cdatt, pvar )
1064                                   ENDIF
1065            CASE DEFAULT
1066               CALL ctl_stop( 'iom_p0d_iatt: accepted IO library is only jpnf90' )
1067            END SELECT
1068         ENDIF
1069      ENDIF
1070   END SUBROUTINE iom_p0d_iatt
1071
1072   SUBROUTINE iom_p0d_ratt( kiomid, cdatt, pvar, cdvar )
1073      INTEGER         , INTENT(in   )                 ::   kiomid    ! Identifier of the file
1074      CHARACTER(len=*), INTENT(in   )                 ::   cdatt     ! Name of the attribute
1075      REAL(wp)        , INTENT(in   )                 ::   pvar      ! write field
1076      CHARACTER(len=*), INTENT(in   ), OPTIONAL       ::   cdvar     ! Name of the variable
1077      !
1078      IF( kiomid > 0 ) THEN
1079         IF( iom_file(kiomid)%nfid > 0 ) THEN
1080            SELECT CASE (iom_file(kiomid)%iolib)
1081            CASE (jpnf90   )   ;   IF( PRESENT(cdvar) ) THEN
1082                                      CALL iom_nf90_putatt( kiomid, cdatt, pvar, cdvar=cdvar )
1083                                   ELSE
1084                                      CALL iom_nf90_putatt( kiomid, cdatt, pvar )
1085                                   ENDIF
1086            CASE DEFAULT   
1087               CALL ctl_stop( 'iom_p0d_ratt: accepted IO library is only jpnf90' )
1088            END SELECT
1089         ENDIF
1090      ENDIF
1091   END SUBROUTINE iom_p0d_ratt
1092
1093   SUBROUTINE iom_p0d_catt( kiomid, cdatt, pvar, cdvar )
1094      INTEGER         , INTENT(in   )                 ::   kiomid    ! Identifier of the file
1095      CHARACTER(len=*), INTENT(in   )                 ::   cdatt     ! Name of the attribute
1096      CHARACTER(len=*), INTENT(in   )                 ::   pvar      ! write field
1097      CHARACTER(len=*), INTENT(in   ), OPTIONAL       ::   cdvar     ! Name of the variable
1098      !
1099      IF( kiomid > 0 ) THEN
1100         IF( iom_file(kiomid)%nfid > 0 ) THEN
1101            SELECT CASE (iom_file(kiomid)%iolib)
1102            CASE (jpnf90   )   ;   IF( PRESENT(cdvar) ) THEN
1103                                      CALL iom_nf90_putatt( kiomid, cdatt, pvar, cdvar=cdvar )
1104                                   ELSE
1105                                      CALL iom_nf90_putatt( kiomid, cdatt, pvar )
1106                                   ENDIF
1107            CASE DEFAULT
1108               CALL ctl_stop( 'iom_p0d_ratt: accepted IO library is only jpnf90' )
1109            END SELECT
1110         ENDIF
1111      ENDIF
1112   END SUBROUTINE iom_p0d_catt
1113
1114   !!----------------------------------------------------------------------
1115   !!                   INTERFACE iom_rstput
1116   !!----------------------------------------------------------------------
1117   SUBROUTINE iom_rp0d( kt, kwrite, kiomid, cdvar, pvar, ktype )
1118      INTEGER         , INTENT(in)                         ::   kt       ! ocean time-step
1119      INTEGER         , INTENT(in)                         ::   kwrite   ! writing time-step
1120      INTEGER         , INTENT(in)                         ::   kiomid   ! Identifier of the file
1121      CHARACTER(len=*), INTENT(in)                         ::   cdvar    ! time axis name
1122      REAL(wp)        , INTENT(in)                         ::   pvar     ! written field
1123      INTEGER         , INTENT(in), OPTIONAL               ::   ktype    ! variable external type
1124      INTEGER :: ivid   ! variable id
1125      IF( kiomid > 0 ) THEN
1126         IF( iom_file(kiomid)%nfid > 0 ) THEN
1127            ivid = iom_varid( kiomid, cdvar, ldstop = .FALSE. )
1128            SELECT CASE (iom_file(kiomid)%iolib)
1129            CASE (jpnf90   )   ;   CALL iom_nf90_rstput(   kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r0d = pvar )
1130            CASE DEFAULT
1131               CALL ctl_stop( 'iom_rp0d: accepted IO library is only jpnf90 (jpioipsl option has been removed)' )
1132            END SELECT
1133         ENDIF
1134      ENDIF
1135   END SUBROUTINE iom_rp0d
1136
1137   SUBROUTINE iom_rp1d( kt, kwrite, kiomid, cdvar, pvar, ktype )
1138      INTEGER         , INTENT(in)                         ::   kt       ! ocean time-step
1139      INTEGER         , INTENT(in)                         ::   kwrite   ! writing time-step
1140      INTEGER         , INTENT(in)                         ::   kiomid   ! Identifier of the file
1141      CHARACTER(len=*), INTENT(in)                         ::   cdvar    ! time axis name
1142      REAL(wp)        , INTENT(in), DIMENSION(          :) ::   pvar     ! written field
1143      INTEGER         , INTENT(in), OPTIONAL               ::   ktype    ! variable external type
1144      INTEGER :: ivid   ! variable id
1145      IF( kiomid > 0 ) THEN
1146         IF( iom_file(kiomid)%nfid > 0 ) THEN
1147            ivid = iom_varid( kiomid, cdvar, ldstop = .FALSE. )
1148            SELECT CASE (iom_file(kiomid)%iolib)
1149            CASE (jpnf90   )   ;   CALL iom_nf90_rstput(   kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r1d = pvar )
1150            CASE DEFAULT
1151               CALL ctl_stop( 'iom_rp1d: accepted IO library is only jpnf90 (jpioipsl option has been removed)' )
1152            END SELECT
1153         ENDIF
1154      ENDIF
1155   END SUBROUTINE iom_rp1d
1156
1157   SUBROUTINE iom_rp2d( kt, kwrite, kiomid, cdvar, pvar, ktype )
1158      INTEGER         , INTENT(in)                         ::   kt       ! ocean time-step
1159      INTEGER         , INTENT(in)                         ::   kwrite   ! writing time-step
1160      INTEGER         , INTENT(in)                         ::   kiomid   ! Identifier of the file
1161      CHARACTER(len=*), INTENT(in)                         ::   cdvar    ! time axis name
1162      REAL(wp)        , INTENT(in), DIMENSION(:,    :    ) ::   pvar     ! written field
1163      INTEGER         , INTENT(in), OPTIONAL               ::   ktype    ! variable external type
1164      INTEGER :: ivid   ! variable id
1165      IF( kiomid > 0 ) THEN
1166         IF( iom_file(kiomid)%nfid > 0 ) THEN
1167            ivid = iom_varid( kiomid, cdvar, ldstop = .FALSE. )
1168            SELECT CASE (iom_file(kiomid)%iolib)
1169            CASE (jpnf90   )   ;   CALL iom_nf90_rstput(   kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r2d = pvar )
1170            CASE DEFAULT
1171               CALL ctl_stop( 'iom_rp2d: accepted IO library is only jpnf90 (jpioipsl option has been removed)' )
1172            END SELECT
1173         ENDIF
1174      ENDIF
1175   END SUBROUTINE iom_rp2d
1176
1177   SUBROUTINE iom_rp3d( kt, kwrite, kiomid, cdvar, pvar, ktype )
1178      INTEGER         , INTENT(in)                         ::   kt       ! ocean time-step
1179      INTEGER         , INTENT(in)                         ::   kwrite   ! writing time-step
1180      INTEGER         , INTENT(in)                         ::   kiomid   ! Identifier of the file
1181      CHARACTER(len=*), INTENT(in)                         ::   cdvar    ! time axis name
1182      REAL(wp)        , INTENT(in),       DIMENSION(:,:,:) ::   pvar     ! written field
1183      INTEGER         , INTENT(in), OPTIONAL               ::   ktype    ! variable external type
1184      INTEGER :: ivid   ! variable id
1185      IF( kiomid > 0 ) THEN
1186         IF( iom_file(kiomid)%nfid > 0 ) THEN
1187            ivid = iom_varid( kiomid, cdvar, ldstop = .FALSE. )
1188            SELECT CASE (iom_file(kiomid)%iolib)
1189            CASE (jpnf90   )   ;   CALL iom_nf90_rstput(   kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r3d = pvar )
1190            CASE DEFAULT
1191               CALL ctl_stop( 'iom_rp3d: accepted IO library is only jpnf90 (jpioipsl option has been removed)' )
1192            END SELECT
1193         ENDIF
1194      ENDIF
1195   END SUBROUTINE iom_rp3d
1196
1197
1198   !!----------------------------------------------------------------------
1199   !!                   INTERFACE iom_put
1200   !!----------------------------------------------------------------------
1201   SUBROUTINE iom_p0d( cdname, pfield0d )
1202      CHARACTER(LEN=*), INTENT(in) ::   cdname
1203      REAL(wp)        , INTENT(in) ::   pfield0d
1204      REAL(wp)        , DIMENSION(jpi,jpj) ::   zz     ! masson
1205#if defined key_iomput
1206      zz(:,:)=pfield0d
1207      CALL xios_send_field(cdname, zz)
1208      !CALL xios_send_field(cdname, (/pfield0d/))
1209#else
1210      IF( .FALSE. )   WRITE(numout,*) cdname, pfield0d   ! useless test to avoid compilation warnings
1211#endif
1212   END SUBROUTINE iom_p0d
1213
1214   SUBROUTINE iom_p1d( cdname, pfield1d )
1215      CHARACTER(LEN=*)          , INTENT(in) ::   cdname
1216      REAL(wp),     DIMENSION(:), INTENT(in) ::   pfield1d
1217#if defined key_iomput
1218      CALL xios_send_field( cdname, RESHAPE( (/pfield1d/), (/1,1,SIZE(pfield1d)/) ) )
1219#else
1220      IF( .FALSE. )   WRITE(numout,*) cdname, pfield1d   ! useless test to avoid compilation warnings
1221#endif
1222   END SUBROUTINE iom_p1d
1223
1224   SUBROUTINE iom_p2d( cdname, pfield2d )
1225      CHARACTER(LEN=*)            , INTENT(in) ::   cdname
1226      REAL(wp),     DIMENSION(:,:), INTENT(in) ::   pfield2d
1227#if defined key_iomput
1228      CALL xios_send_field(cdname, pfield2d)
1229#else
1230      IF( .FALSE. )   WRITE(numout,*) cdname, pfield2d   ! useless test to avoid compilation warnings
1231#endif
1232   END SUBROUTINE iom_p2d
1233
1234   SUBROUTINE iom_p3d( cdname, pfield3d )
1235      CHARACTER(LEN=*)                , INTENT(in) ::   cdname
1236      REAL(wp),       DIMENSION(:,:,:), INTENT(in) ::   pfield3d
1237#if defined key_iomput
1238      CALL xios_send_field(cdname, pfield3d)
1239#else
1240      IF( .FALSE. )   WRITE(numout,*) cdname, pfield3d   ! useless test to avoid compilation warnings
1241#endif
1242   END SUBROUTINE iom_p3d
1243   !!----------------------------------------------------------------------
1244
1245#if defined key_iomput
1246
1247   SUBROUTINE iom_set_domain_attr( cdid, ni_glo, nj_glo, ibegin, jbegin, ni, nj, zoom_ibegin, zoom_jbegin, zoom_ni, zoom_nj,   &
1248      &                                    data_dim, data_ibegin, data_ni, data_jbegin, data_nj, lonvalue, latvalue, mask,     &
1249      &                                    nvertex, bounds_lon, bounds_lat, area )
1250      CHARACTER(LEN=*)                   , INTENT(in) ::   cdid
1251      INTEGER                  , OPTIONAL, INTENT(in) ::   ni_glo, nj_glo, ibegin, jbegin, ni, nj
1252      INTEGER                  , OPTIONAL, INTENT(in) ::   data_dim, data_ibegin, data_ni, data_jbegin, data_nj
1253      INTEGER                  , OPTIONAL, INTENT(in) ::   zoom_ibegin, zoom_jbegin, zoom_ni, zoom_nj, nvertex
1254      REAL(wp), DIMENSION(:)   , OPTIONAL, INTENT(in) ::   lonvalue, latvalue
1255      REAL(wp), DIMENSION(:,:) , OPTIONAL, INTENT(in) ::   bounds_lon, bounds_lat, area
1256      LOGICAL, DIMENSION(:)   , OPTIONAL, INTENT(in) ::   mask
1257
1258
1259      IF ( xios_is_valid_domain     (cdid) ) THEN
1260         CALL xios_set_domain_attr     ( cdid, ni_glo=ni_glo, nj_glo=nj_glo, ibegin=ibegin, jbegin=jbegin, ni=ni, nj=nj,   &
1261            &    data_dim=data_dim, data_ibegin=data_ibegin, data_ni=data_ni, data_jbegin=data_jbegin, data_nj=data_nj ,   &
1262            &    lonvalue_1D=lonvalue, latvalue_1D=latvalue, mask_1D=mask, nvertex=nvertex, bounds_lon_1D=bounds_lon,      &
1263            &    bounds_lat_1D=bounds_lat, area=area, type='curvilinear')
1264     ENDIF
1265      IF ( xios_is_valid_domaingroup(cdid) ) THEN
1266         CALL xios_set_domaingroup_attr( cdid, ni_glo=ni_glo, nj_glo=nj_glo, ibegin=ibegin, jbegin=jbegin, ni=ni, nj=nj,   &
1267            &    data_dim=data_dim, data_ibegin=data_ibegin, data_ni=data_ni, data_jbegin=data_jbegin, data_nj=data_nj ,   &
1268            &    lonvalue_1D=lonvalue, latvalue_1D=latvalue, mask_1D=mask, nvertex=nvertex, bounds_lon_1D=bounds_lon,      &
1269            &    bounds_lat_1D=bounds_lat, area=area, type='curvilinear' )
1270      ENDIF
1271
1272      CALL xios_solve_inheritance()
1273
1274   END SUBROUTINE iom_set_domain_attr
1275
1276
1277   SUBROUTINE iom_set_zoom_domain_attr( cdid, ibegin, jbegin, ni, nj)
1278      CHARACTER(LEN=*)                   , INTENT(in) ::   cdid
1279      INTEGER                  , OPTIONAL, INTENT(in) ::   ibegin, jbegin, ni, nj
1280
1281      IF ( xios_is_valid_zoom_domain     (cdid) ) THEN
1282          CALL xios_set_zoom_domain_attr     ( cdid, ibegin=ibegin, jbegin=jbegin, ni=ni,    &
1283            &   nj=nj)
1284     ENDIF
1285   END SUBROUTINE iom_set_zoom_domain_attr
1286
1287
1288   SUBROUTINE iom_set_axis_attr( cdid, paxis, bounds )
1289      CHARACTER(LEN=*)      , INTENT(in) ::   cdid
1290      REAL(wp), DIMENSION(:)  , OPTIONAL, INTENT(in) ::   paxis
1291      REAL(wp), DIMENSION(:,:), OPTIONAL, INTENT(in) ::   bounds
1292
1293      IF ( PRESENT(paxis) ) THEN
1294         IF ( xios_is_valid_axis     (cdid) )   CALL xios_set_axis_attr     ( cdid, n_glo=SIZE(paxis), value=paxis )
1295         IF ( xios_is_valid_axisgroup(cdid) )   CALL xios_set_axisgroup_attr( cdid, n_glo=SIZE(paxis), value=paxis )
1296      ENDIF
1297      IF ( xios_is_valid_axis     (cdid) )   CALL xios_set_axis_attr     ( cdid, bounds=bounds )
1298      IF ( xios_is_valid_axisgroup(cdid) )   CALL xios_set_axisgroup_attr( cdid, bounds=bounds )
1299      CALL xios_solve_inheritance()
1300   END SUBROUTINE iom_set_axis_attr
1301
1302
1303   SUBROUTINE iom_set_field_attr( cdid, freq_op, freq_offset )
1304      CHARACTER(LEN=*)          , INTENT(in) ::   cdid
1305      TYPE(xios_duration),OPTIONAL , INTENT(in) ::   freq_op
1306      TYPE(xios_duration),OPTIONAL , INTENT(in) ::   freq_offset
1307      IF ( xios_is_valid_field     (cdid) )   CALL xios_set_field_attr       &
1308    &     ( cdid, freq_op=freq_op, freq_offset=freq_offset )
1309      IF ( xios_is_valid_fieldgroup(cdid) )   CALL xios_set_fieldgroup_attr  &
1310    &                    ( cdid, freq_op=freq_op, freq_offset=freq_offset )
1311      CALL xios_solve_inheritance()
1312   END SUBROUTINE iom_set_field_attr
1313
1314
1315   SUBROUTINE iom_set_file_attr( cdid, name, name_suffix )
1316      CHARACTER(LEN=*)          , INTENT(in) ::   cdid
1317      CHARACTER(LEN=*),OPTIONAL , INTENT(in) ::   name, name_suffix
1318      IF ( xios_is_valid_file     (cdid) )   CALL xios_set_file_attr     ( cdid, name=name, name_suffix=name_suffix )
1319      IF ( xios_is_valid_filegroup(cdid) )   CALL xios_set_filegroup_attr( cdid, name=name, name_suffix=name_suffix )
1320      CALL xios_solve_inheritance()
1321   END SUBROUTINE iom_set_file_attr
1322
1323
1324   SUBROUTINE iom_get_file_attr( cdid, name, name_suffix, output_freq )
1325      CHARACTER(LEN=*)          , INTENT(in ) ::   cdid
1326      CHARACTER(LEN=*),OPTIONAL , INTENT(out) ::   name, name_suffix
1327      TYPE(xios_duration), OPTIONAL , INTENT(out) :: output_freq
1328      LOGICAL                                 ::   llexist1,llexist2,llexist3
1329      !---------------------------------------------------------------------
1330      IF( PRESENT( name        ) )   name = ''          ! default values
1331      IF( PRESENT( name_suffix ) )   name_suffix = ''
1332      IF( PRESENT( output_freq ) )   output_freq = xios_duration(0,0,0,0,0,0)
1333      IF ( xios_is_valid_file     (cdid) ) THEN
1334         CALL xios_solve_inheritance()
1335         CALL xios_is_defined_file_attr     ( cdid, name = llexist1, name_suffix = llexist2, output_freq = llexist3)
1336         IF(llexist1)   CALL xios_get_file_attr     ( cdid, name = name )
1337         IF(llexist2)   CALL xios_get_file_attr     ( cdid, name_suffix = name_suffix )
1338         IF(llexist3)   CALL xios_get_file_attr     ( cdid, output_freq = output_freq )
1339      ENDIF
1340      IF ( xios_is_valid_filegroup(cdid) ) THEN
1341         CALL xios_solve_inheritance()
1342         CALL xios_is_defined_filegroup_attr( cdid, name = llexist1, name_suffix = llexist2, output_freq = llexist3)
1343         IF(llexist1)   CALL xios_get_filegroup_attr( cdid, name = name )
1344         IF(llexist2)   CALL xios_get_filegroup_attr( cdid, name_suffix = name_suffix )
1345         IF(llexist3)   CALL xios_get_filegroup_attr( cdid, output_freq = output_freq )
1346      ENDIF
1347   END SUBROUTINE iom_get_file_attr
1348
1349
1350   SUBROUTINE iom_set_grid_attr( cdid, mask )
1351      CHARACTER(LEN=*)                   , INTENT(in) ::   cdid
1352      LOGICAL, DIMENSION(:,:,:), OPTIONAL, INTENT(in) ::   mask
1353      IF ( xios_is_valid_grid     (cdid) )   CALL xios_set_grid_attr     ( cdid, mask_3D=mask )
1354      IF ( xios_is_valid_gridgroup(cdid) )   CALL xios_set_gridgroup_attr( cdid, mask_3D=mask )
1355      CALL xios_solve_inheritance()
1356   END SUBROUTINE iom_set_grid_attr
1357
1358   SUBROUTINE iom_setkt( kt, cdname )
1359      INTEGER         , INTENT(in) ::   kt 
1360      CHARACTER(LEN=*), INTENT(in) ::   cdname
1361      !     
1362      CALL iom_swap( cdname )   ! swap to cdname context
1363      CALL xios_update_calendar(kt)
1364      IF( cdname /= TRIM(cxios_context) ) CALL iom_swap( TRIM(cxios_context) )   ! return back to nemo context
1365      !
1366   END SUBROUTINE iom_setkt
1367
1368   SUBROUTINE iom_context_finalize( cdname )
1369      CHARACTER(LEN=*), INTENT(in) :: cdname
1370      !
1371      IF( xios_is_valid_context(cdname) ) THEN
1372         CALL iom_swap( cdname )   ! swap to cdname context
1373         CALL xios_context_finalize() ! finalize the context
1374         IF( cdname /= TRIM(cxios_context) ) CALL iom_swap( TRIM(cxios_context) )   ! return back to nemo context
1375      ENDIF
1376      !
1377   END SUBROUTINE iom_context_finalize
1378
1379
1380   SUBROUTINE set_grid( cdgrd, plon, plat )
1381      !!----------------------------------------------------------------------
1382      !!                     ***  ROUTINE set_grid  ***
1383      !!
1384      !! ** Purpose :   define horizontal grids
1385      !!
1386      !!----------------------------------------------------------------------
1387      CHARACTER(LEN=1)            , INTENT(in) ::   cdgrd
1388      REAL(wp), DIMENSION(jpi,jpj), INTENT(in) ::   plon
1389      REAL(wp), DIMENSION(jpi,jpj), INTENT(in) ::   plat
1390      !
1391      REAL(wp), DIMENSION(jpi,jpj,jpk) ::   zmask
1392      INTEGER  :: ni,nj
1393     
1394      ni=nlei-nldi+1 ; nj=nlej-nldj+1
1395
1396      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)
1397      CALL iom_set_domain_attr("grid_"//cdgrd, data_dim=2, data_ibegin = 1-nldi, data_ni = jpi, data_jbegin = 1-nldj, data_nj = jpj)
1398      CALL iom_set_domain_attr("grid_"//cdgrd, lonvalue = RESHAPE(plon(nldi:nlei, nldj:nlej),(/ ni*nj /)),   &
1399         &                                     latvalue = RESHAPE(plat(nldi:nlei, nldj:nlej),(/ ni*nj /))) 
1400
1401      IF ( ln_mskland ) THEN
1402         ! mask land points, keep values on coast line -> specific mask for U, V and W points
1403         SELECT CASE ( cdgrd )
1404         CASE('T')   ;   zmask(:,:,:)       = tmask(:,:,:)
1405         CASE('U')   ;   zmask(2:jpim1,:,:) = tmask(2:jpim1,:,:) + tmask(3:jpi,:,:)   ;   CALL lbc_lnk( zmask, 'U', 1. )
1406         CASE('V')   ;   zmask(:,2:jpjm1,:) = tmask(:,2:jpjm1,:) + tmask(:,3:jpj,:)   ;   CALL lbc_lnk( zmask, 'V', 1. )
1407         CASE('W')   ;   zmask(:,:,2:jpk  ) = tmask(:,:,1:jpkm1) + tmask(:,:,2:jpk)   ;   zmask(:,:,1) = tmask(:,:,1)
1408         END SELECT
1409         !
1410         CALL iom_set_domain_attr( "grid_"//cdgrd       , mask = RESHAPE(zmask(nldi:nlei,nldj:nlej,1),(/ni*nj    /)) /= 0. )
1411         CALL iom_set_grid_attr  ( "grid_"//cdgrd//"_3D", mask = RESHAPE(zmask(nldi:nlei,nldj:nlej,:),(/ni,nj,jpk/)) /= 0. )
1412      ENDIF
1413     
1414   END SUBROUTINE set_grid
1415
1416
1417   SUBROUTINE set_grid_bounds( cdgrd, plon_cnr, plat_cnr, plon_pnt, plat_pnt )
1418      !!----------------------------------------------------------------------
1419      !!                   ***  ROUTINE set_grid_bounds  ***
1420      !!
1421      !! ** Purpose :   define horizontal grid corners
1422      !!
1423      !!----------------------------------------------------------------------
1424      CHARACTER(LEN=1) , INTENT(in) :: cdgrd
1425      !
1426      REAL(wp), DIMENSION(jpi,jpj), INTENT(in)           :: plon_cnr, plat_cnr  ! Lat/lon coordinates of a contiguous vertex of cell (i,j)
1427      REAL(wp), DIMENSION(jpi,jpj), OPTIONAL, INTENT(in) :: plon_pnt, plat_pnt  ! Lat/lon coordinates of the point of cell (i,j)
1428      !
1429      REAL(wp), ALLOCATABLE, DIMENSION(:,:,:,:)   :: z_bnds      ! Lat/lon coordinates of the vertices of cell (i,j)
1430      REAL(wp), ALLOCATABLE, DIMENSION(:,:)       :: z_fld       ! Working array to determine where to rotate cells
1431      REAL(wp), ALLOCATABLE, DIMENSION(:,:)       :: z_rot       ! Lat/lon working array for rotation of cells
1432      !
1433      INTEGER :: icnr, jcnr                                      ! Offset such that the vertex coordinate (i+icnr,j+jcnr)
1434      !                                                          ! represents the bottom-left corner of cell (i,j)
1435      INTEGER :: ji, jj, jn, ni, nj
1436
1437      ALLOCATE( z_bnds(4,jpi,jpj,2), z_fld(jpi,jpj), z_rot(4,2)  )
1438
1439      ! Offset of coordinate representing bottom-left corner
1440      SELECT CASE ( TRIM(cdgrd) )
1441         CASE ('T', 'W')
1442            icnr = -1 ; jcnr = -1
1443         CASE ('U')
1444            icnr =  0 ; jcnr = -1
1445         CASE ('V')
1446            icnr = -1 ; jcnr =  0
1447      END SELECT
1448
1449      ni = nlei-nldi+1 ; nj = nlej-nldj+1  ! Dimensions of subdomain interior
1450
1451      z_fld(:,:) = 1._wp
1452      CALL lbc_lnk( z_fld, cdgrd, -1. )    ! Working array for location of northfold
1453
1454      ! Cell vertices that can be defined
1455      DO jj = 2, jpjm1
1456         DO ji = 2, jpim1
1457            z_bnds(1,ji,jj,1) = plat_cnr(ji+icnr,  jj+jcnr  ) ! Bottom-left
1458            z_bnds(2,ji,jj,1) = plat_cnr(ji+icnr+1,jj+jcnr  ) ! Bottom-right
1459            z_bnds(3,ji,jj,1) = plat_cnr(ji+icnr+1,jj+jcnr+1) ! Top-right
1460            z_bnds(4,ji,jj,1) = plat_cnr(ji+icnr,  jj+jcnr+1) ! Top-left
1461            z_bnds(1,ji,jj,2) = plon_cnr(ji+icnr,  jj+jcnr  ) ! Bottom-left
1462            z_bnds(2,ji,jj,2) = plon_cnr(ji+icnr+1,jj+jcnr  ) ! Bottom-right
1463            z_bnds(3,ji,jj,2) = plon_cnr(ji+icnr+1,jj+jcnr+1) ! Top-right
1464            z_bnds(4,ji,jj,2) = plon_cnr(ji+icnr,  jj+jcnr+1) ! Top-left
1465         END DO
1466      END DO
1467
1468      ! Cell vertices on boundries
1469      DO jn = 1, 4
1470         CALL lbc_lnk( z_bnds(jn,:,:,1), cdgrd, 1., pval=999._wp )
1471         CALL lbc_lnk( z_bnds(jn,:,:,2), cdgrd, 1., pval=999._wp )
1472      END DO
1473
1474      ! Zero-size cells at closed boundaries if cell points provided,
1475      ! otherwise they are closed cells with unrealistic bounds
1476      IF( PRESENT(plon_pnt) .AND. PRESENT(plat_pnt) ) THEN
1477         IF( (nbondi == -1 .OR. nbondi == 2) .AND. .NOT. (jperio == 1 .OR. jperio == 4 .OR. jperio == 6) ) THEN
1478            DO jn = 1, 4        ! (West or jpni = 1), closed E-W
1479               z_bnds(jn,1,:,1) = plat_pnt(1,:)  ;  z_bnds(jn,1,:,2) = plon_pnt(1,:)
1480            END DO
1481         ENDIF
1482         IF( (nbondi == 1 .OR. nbondi == 2) .AND. .NOT. (jperio == 1 .OR. jperio == 4 .OR. jperio == 6) ) THEN
1483            DO jn = 1, 4        ! (East or jpni = 1), closed E-W
1484               z_bnds(jn,nlci,:,1) = plat_pnt(nlci,:)  ;  z_bnds(jn,nlci,:,2) = plon_pnt(nlci,:)
1485            END DO
1486         ENDIF
1487         IF( nbondj == -1 .OR. (nbondj == 2 .AND. jperio /= 2) ) THEN
1488            DO jn = 1, 4        ! South or (jpnj = 1, not symmetric)
1489               z_bnds(jn,:,1,1) = plat_pnt(:,1)  ;  z_bnds(jn,:,1,2) = plon_pnt(:,1)
1490            END DO
1491         ENDIF
1492         IF( (nbondj == 1 .OR. nbondj == 2) .AND. jperio  < 3 ) THEN
1493            DO jn = 1, 4        ! (North or jpnj = 1), no north fold
1494               z_bnds(jn,:,nlcj,1) = plat_pnt(:,nlcj)  ;  z_bnds(jn,:,nlcj,2) = plon_pnt(:,nlcj)
1495            END DO
1496         ENDIF
1497      ENDIF
1498
1499      ! Rotate cells at the north fold
1500      IF( (nbondj == 1 .OR. nbondj == 2) .AND. jperio >= 3 ) THEN
1501         DO jj = 1, jpj
1502            DO ji = 1, jpi
1503               IF( z_fld(ji,jj) == -1. ) THEN
1504                  z_rot(1,:) = z_bnds(3,ji,jj,:) ; z_rot(2,:) = z_bnds(4,ji,jj,:)
1505                  z_rot(3,:) = z_bnds(1,ji,jj,:) ; z_rot(4,:) = z_bnds(2,ji,jj,:)
1506                  z_bnds(:,ji,jj,:) = z_rot(:,:)
1507               ENDIF
1508            END DO
1509         END DO
1510
1511      ! Invert cells at the symmetric equator
1512      ELSE IF( nbondj == 2 .AND. jperio == 2 ) THEN
1513         DO ji = 1, jpi
1514            z_rot(1:2,:) = z_bnds(3:4,ji,1,:)
1515            z_rot(3:4,:) = z_bnds(1:2,ji,1,:)
1516            z_bnds(:,ji,1,:) = z_rot(:,:)
1517         END DO
1518      ENDIF
1519
1520      CALL iom_set_domain_attr("grid_"//cdgrd, bounds_lat = RESHAPE(z_bnds(:,nldi:nlei,nldj:nlej,1),(/ 4,ni*nj /)),           &
1521                                               bounds_lon = RESHAPE(z_bnds(:,nldi:nlei,nldj:nlej,2),(/ 4,ni*nj /)), nvertex=4 )
1522
1523      DEALLOCATE( z_bnds, z_fld, z_rot ) 
1524
1525   END SUBROUTINE set_grid_bounds
1526
1527
1528   SUBROUTINE set_grid_znl( plat )
1529      !!----------------------------------------------------------------------
1530      !!                     ***  ROUTINE set_grid_znl  ***
1531      !!
1532      !! ** Purpose :   define grids for zonal mean
1533      !!
1534      !!----------------------------------------------------------------------
1535      REAL(wp), DIMENSION(jpi,jpj), INTENT(in) ::   plat
1536      !
1537      REAL(wp), DIMENSION(:), ALLOCATABLE  ::   zlon
1538      INTEGER  :: ni,nj, ix, iy
1539
1540     
1541      ni=nlei-nldi+1 ; nj=nlej-nldj+1            ! define zonal mean domain (jpj*jpk)
1542      ALLOCATE( zlon(ni*nj) )       ;       zlon(:) = 0.
1543
1544      CALL dom_ngb( -168.53, 65.03, ix, iy, 'T' ) !  i-line that passes through Bering Strait: Reference latitude (used in plots)
1545      CALL iom_set_domain_attr("gznl", ni_glo=jpiglo, nj_glo=jpjglo, ibegin=nimpp+nldi-2, jbegin=njmpp+nldj-2, ni=ni, nj=nj)
1546      CALL iom_set_domain_attr("gznl", data_dim=2, data_ibegin = 1-nldi, data_ni = jpi, data_jbegin = 1-nldj, data_nj = jpj)
1547      CALL iom_set_domain_attr("gznl", lonvalue = zlon,   &
1548         &                             latvalue = RESHAPE(plat(nldi:nlei, nldj:nlej),(/ ni*nj /))) 
1549      CALL iom_set_zoom_domain_attr ("ptr", ibegin=ix-1, jbegin=0, ni=1, nj=jpjglo)
1550      !
1551      CALL iom_update_file_name('ptr')
1552      !
1553   END SUBROUTINE set_grid_znl
1554
1555   SUBROUTINE set_scalar
1556      !!----------------------------------------------------------------------
1557      !!                     ***  ROUTINE set_scalar  ***
1558      !!
1559      !! ** Purpose :   define fake grids for scalar point
1560      !!
1561      !!----------------------------------------------------------------------
1562      REAL(wp), DIMENSION(1)   ::   zz = 1.
1563      !!----------------------------------------------------------------------
1564
1565      CALL iom_set_domain_attr('scalarpoint', ni_glo=jpnij, nj_glo=1, ibegin=narea-1, jbegin=0, ni=1, nj=1)
1566      CALL iom_set_domain_attr('scalarpoint', data_dim=2, data_ibegin = 1, data_ni = 1, data_jbegin = 1, data_nj = 1)
1567     
1568      zz=REAL(narea,wp)
1569      CALL iom_set_domain_attr('scalarpoint', lonvalue=zz, latvalue=zz)
1570
1571   END SUBROUTINE set_scalar
1572
1573
1574   SUBROUTINE set_xmlatt
1575      !!----------------------------------------------------------------------
1576      !!                     ***  ROUTINE set_xmlatt  ***
1577      !!
1578      !! ** Purpose :   automatic definitions of some of the xml attributs...
1579      !!
1580      !!----------------------------------------------------------------------
1581      CHARACTER(len=1),DIMENSION( 3) ::   clgrd                    ! suffix name
1582      CHARACTER(len=256)             ::   clsuff                   ! suffix name
1583      CHARACTER(len=1)               ::   cl1                      ! 1 character
1584      CHARACTER(len=2)               ::   cl2                      ! 2 characters
1585      CHARACTER(len=3)               ::   cl3                      ! 3 characters
1586      INTEGER                        ::   ji, jg                   ! loop counters
1587      INTEGER                        ::   ix, iy                   ! i-,j- index
1588      REAL(wp)        ,DIMENSION(11) ::   zlontao                  ! longitudes of tao    moorings
1589      REAL(wp)        ,DIMENSION( 7) ::   zlattao                  ! latitudes  of tao    moorings
1590      REAL(wp)        ,DIMENSION( 4) ::   zlonrama                 ! longitudes of rama   moorings
1591      REAL(wp)        ,DIMENSION(11) ::   zlatrama                 ! latitudes  of rama   moorings
1592      REAL(wp)        ,DIMENSION( 3) ::   zlonpira                 ! longitudes of pirata moorings
1593      REAL(wp)        ,DIMENSION( 9) ::   zlatpira                 ! latitudes  of pirata moorings
1594      TYPE(xios_duration)            ::   f_op, f_of
1595      !!----------------------------------------------------------------------
1596      !
1597      ! frequency of the call of iom_put (attribut: freq_op)
1598      f_op%timestep = 1        ;  f_of%timestep = 0  ; CALL iom_set_field_attr('field_definition', freq_op=f_op, freq_offset=f_of)
1599      f_op%timestep = 2        ;  f_of%timestep = 0  ; CALL iom_set_field_attr('trendT_even'     , freq_op=f_op, freq_offset=f_of)
1600      f_op%timestep = 2        ;  f_of%timestep = -1 ; CALL iom_set_field_attr('trendT_odd'      , freq_op=f_op, freq_offset=f_of)
1601      f_op%timestep = nn_fsbc  ;  f_of%timestep = 0  ; CALL iom_set_field_attr('SBC'             , freq_op=f_op, freq_offset=f_of)
1602      f_op%timestep = nn_fsbc  ;  f_of%timestep = 0  ; CALL iom_set_field_attr('SBC_scalar'      , freq_op=f_op, freq_offset=f_of)
1603      f_op%timestep = nn_dttrc ;  f_of%timestep = 0  ; CALL iom_set_field_attr('ptrc_T'          , freq_op=f_op, freq_offset=f_of)
1604      f_op%timestep = nn_dttrc ;  f_of%timestep = 0  ; CALL iom_set_field_attr('diad_T'          , freq_op=f_op, freq_offset=f_of)
1605
1606      ! output file names (attribut: name)
1607      DO ji = 1, 9
1608         WRITE(cl1,'(i1)') ji 
1609         CALL iom_update_file_name('file'//cl1)
1610      END DO
1611      DO ji = 1, 99
1612         WRITE(cl2,'(i2.2)') ji 
1613         CALL iom_update_file_name('file'//cl2)
1614      END DO
1615      DO ji = 1, 999
1616         WRITE(cl3,'(i3.3)') ji 
1617         CALL iom_update_file_name('file'//cl3)
1618      END DO
1619
1620      ! Zooms...
1621      clgrd = (/ 'T', 'U', 'W' /) 
1622      DO jg = 1, SIZE(clgrd)                                                                   ! grid type
1623         cl1 = clgrd(jg)
1624         ! Equatorial section (attributs: jbegin, ni, name_suffix)
1625         CALL dom_ngb( 0., 0., ix, iy, cl1 )
1626         CALL iom_set_zoom_domain_attr ('Eq'//cl1, jbegin=iy-1, ni=jpiglo)
1627         CALL iom_get_file_attr   ('Eq'//cl1, name_suffix = clsuff             )
1628         CALL iom_set_file_attr   ('Eq'//cl1, name_suffix = TRIM(clsuff)//'_Eq')
1629         CALL iom_update_file_name('Eq'//cl1)
1630      END DO
1631      ! TAO moorings (attributs: ibegin, jbegin, name_suffix)
1632      zlontao = (/ 137.0, 147.0, 156.0, 165.0, -180.0, -170.0, -155.0, -140.0, -125.0, -110.0, -95.0 /)
1633      zlattao = (/  -8.0,  -5.0,  -2.0,   0.0,    2.0,    5.0,    8.0 /)
1634      CALL set_mooring( zlontao, zlattao )
1635      ! RAMA moorings (attributs: ibegin, jbegin, name_suffix)
1636      zlonrama = (/  55.0,  67.0, 80.5, 90.0 /)
1637      zlatrama = (/ -16.0, -12.0, -8.0, -4.0, -1.5, 0.0, 1.5, 4.0, 8.0, 12.0, 15.0 /)
1638      CALL set_mooring( zlonrama, zlatrama )
1639      ! PIRATA moorings (attributs: ibegin, jbegin, name_suffix)
1640      zlonpira = (/ -38.0, -23.0, -10.0 /)
1641      zlatpira = (/ -19.0, -14.0,  -8.0, 0.0, 4.0, 8.0, 12.0, 15.0, 20.0 /)
1642      CALL set_mooring( zlonpira, zlatpira )
1643
1644     
1645   END SUBROUTINE set_xmlatt
1646
1647
1648   SUBROUTINE set_mooring( plon, plat)
1649      !!----------------------------------------------------------------------
1650      !!                     ***  ROUTINE set_mooring  ***
1651      !!
1652      !! ** Purpose :   automatic definitions of moorings xml attributs...
1653      !!
1654      !!----------------------------------------------------------------------
1655      REAL(wp), DIMENSION(:), INTENT(in) ::  plon, plat           ! longitudes/latitudes oft the mooring
1656      !
1657!!$      CHARACTER(len=1),DIMENSION(4) ::   clgrd = (/ 'T', 'U', 'V', 'W' /)   ! suffix name
1658      CHARACTER(len=1),DIMENSION(1) ::   clgrd = (/ 'T' /)        ! suffix name
1659      CHARACTER(len=256)            ::   clname                   ! file name
1660      CHARACTER(len=256)            ::   clsuff                   ! suffix name
1661      CHARACTER(len=1)              ::   cl1                      ! 1 character
1662      CHARACTER(len=6)              ::   clon,clat                ! name of longitude, latitude
1663      INTEGER                       ::   ji, jj, jg               ! loop counters
1664      INTEGER                       ::   ix, iy                   ! i-,j- index
1665      REAL(wp)                      ::   zlon, zlat
1666      !!----------------------------------------------------------------------
1667      DO jg = 1, SIZE(clgrd)
1668         cl1 = clgrd(jg)
1669         DO ji = 1, SIZE(plon)
1670            DO jj = 1, SIZE(plat)
1671               zlon = plon(ji)
1672               zlat = plat(jj)
1673               ! modifications for RAMA moorings
1674               IF( zlon ==  67. .AND. zlat ==  15. )   zlon =  65.
1675               IF( zlon ==  90. .AND. zlat <=  -4. )   zlon =  95.
1676               IF( zlon ==  95. .AND. zlat ==  -4. )   zlat =  -5.
1677               ! modifications for PIRATA moorings
1678               IF( zlon == -38. .AND. zlat == -19. )   zlon = -34.
1679               IF( zlon == -38. .AND. zlat == -14. )   zlon = -32.
1680               IF( zlon == -38. .AND. zlat ==  -8. )   zlon = -30.
1681               IF( zlon == -38. .AND. zlat ==   0. )   zlon = -35.
1682               IF( zlon == -23. .AND. zlat ==  20. )   zlat =  21.
1683               IF( zlon == -10. .AND. zlat == -14. )   zlat = -10.
1684               IF( zlon == -10. .AND. zlat ==  -8. )   zlat =  -6.
1685               IF( zlon == -10. .AND. zlat ==   4. ) THEN   ;   zlon = 0.   ;   zlat = 0.   ;   ENDIF
1686               CALL dom_ngb( zlon, zlat, ix, iy, cl1 )
1687               IF( zlon >= 0. ) THEN 
1688                  IF( zlon == REAL(NINT(zlon), wp) ) THEN   ;   WRITE(clon, '(i3,  a)') NINT( zlon), 'e'
1689                  ELSE                                      ;   WRITE(clon, '(f5.1,a)')       zlon , 'e'
1690                  ENDIF
1691               ELSE             
1692                  IF( zlon == REAL(NINT(zlon), wp) ) THEN   ;   WRITE(clon, '(i3,  a)') NINT(-zlon), 'w'
1693                  ELSE                                      ;   WRITE(clon, '(f5.1,a)')      -zlon , 'w'
1694                  ENDIF
1695               ENDIF
1696               IF( zlat >= 0. ) THEN 
1697                  IF( zlat == REAL(NINT(zlat), wp) ) THEN   ;   WRITE(clat, '(i2,  a)') NINT( zlat), 'n'
1698                  ELSE                                      ;   WRITE(clat, '(f4.1,a)')       zlat , 'n'
1699                  ENDIF
1700               ELSE             
1701                  IF( zlat == REAL(NINT(zlat), wp) ) THEN   ;   WRITE(clat, '(i2,  a)') NINT(-zlat), 's'
1702                  ELSE                                      ;   WRITE(clat, '(f4.1,a)')      -zlat , 's'
1703                  ENDIF
1704               ENDIF
1705               clname = TRIM(ADJUSTL(clat))//TRIM(ADJUSTL(clon))
1706               CALL iom_set_zoom_domain_attr  (TRIM(clname)//cl1, ibegin= ix-1, jbegin= iy-1)
1707
1708               CALL iom_get_file_attr   (TRIM(clname)//cl1, name_suffix = clsuff                         )
1709               CALL iom_set_file_attr   (TRIM(clname)//cl1, name_suffix = TRIM(clsuff)//'_'//TRIM(clname))
1710               CALL iom_update_file_name(TRIM(clname)//cl1)
1711            END DO
1712         END DO
1713      END DO
1714     
1715   END SUBROUTINE set_mooring
1716
1717   
1718   SUBROUTINE iom_update_file_name( cdid )
1719      !!----------------------------------------------------------------------
1720      !!                     ***  ROUTINE iom_update_file_name  ***
1721      !!
1722      !! ** Purpose :   
1723      !!
1724      !!----------------------------------------------------------------------
1725      CHARACTER(LEN=*)          , INTENT(in) ::   cdid
1726      !
1727      CHARACTER(LEN=256) ::   clname
1728      CHARACTER(LEN=20)  ::   clfreq
1729      CHARACTER(LEN=20)  ::   cldate
1730      INTEGER            ::   idx
1731      INTEGER            ::   jn
1732      INTEGER            ::   itrlen
1733      INTEGER            ::   iyear, imonth, iday, isec
1734      REAL(wp)           ::   zsec
1735      LOGICAL            ::   llexist
1736      TYPE(xios_duration)   ::   output_freq 
1737      !!----------------------------------------------------------------------
1738
1739      DO jn = 1,2
1740
1741         output_freq = xios_duration(0,0,0,0,0,0)
1742         IF( jn == 1 )   CALL iom_get_file_attr( cdid, name        = clname, output_freq = output_freq )
1743         IF( jn == 2 )   CALL iom_get_file_attr( cdid, name_suffix = clname )
1744
1745         IF ( TRIM(clname) /= '' ) THEN
1746
1747            idx = INDEX(clname,'@expname@') + INDEX(clname,'@EXPNAME@')
1748            DO WHILE ( idx /= 0 ) 
1749               clname = clname(1:idx-1)//TRIM(cexper)//clname(idx+9:LEN_TRIM(clname))
1750               idx = INDEX(clname,'@expname@') + INDEX(clname,'@EXPNAME@')
1751            END DO
1752
1753            idx = INDEX(clname,'@freq@') + INDEX(clname,'@FREQ@')
1754            DO WHILE ( idx /= 0 ) 
1755              IF ( output_freq%timestep /= 0) THEN
1756                  WRITE(clfreq,'(I18,A2)')INT(output_freq%timestep),'ts' 
1757                  itrlen = LEN_TRIM(ADJUSTL(clfreq))
1758              ELSE IF ( output_freq%second /= 0 ) THEN
1759                  WRITE(clfreq,'(I19,A1)')INT(output_freq%second),'s' 
1760                  itrlen = LEN_TRIM(ADJUSTL(clfreq))
1761              ELSE IF ( output_freq%minute /= 0 ) THEN
1762                  WRITE(clfreq,'(I18,A2)')INT(output_freq%minute),'mi' 
1763                  itrlen = LEN_TRIM(ADJUSTL(clfreq))
1764              ELSE IF ( output_freq%hour /= 0 ) THEN
1765                  WRITE(clfreq,'(I19,A1)')INT(output_freq%hour),'h' 
1766                  itrlen = LEN_TRIM(ADJUSTL(clfreq))
1767              ELSE IF ( output_freq%day /= 0 ) THEN
1768                  WRITE(clfreq,'(I19,A1)')INT(output_freq%day),'d' 
1769                  itrlen = LEN_TRIM(ADJUSTL(clfreq))
1770              ELSE IF ( output_freq%month /= 0 ) THEN   
1771                  WRITE(clfreq,'(I19,A1)')INT(output_freq%month),'m' 
1772                  itrlen = LEN_TRIM(ADJUSTL(clfreq))
1773              ELSE IF ( output_freq%year /= 0 ) THEN   
1774                  WRITE(clfreq,'(I19,A1)')INT(output_freq%year),'y' 
1775                  itrlen = LEN_TRIM(ADJUSTL(clfreq))
1776              ELSE
1777                  CALL ctl_stop('error in the name of file id '//TRIM(cdid),   &
1778                     & ' attribute output_freq is undefined -> cannot replace @freq@ in '//TRIM(clname) )
1779              ENDIF
1780              clname = clname(1:idx-1)//TRIM(ADJUSTL(clfreq))//clname(idx+6:LEN_TRIM(clname))
1781              idx = INDEX(clname,'@freq@') + INDEX(clname,'@FREQ@')
1782            END DO
1783
1784            idx = INDEX(clname,'@startdate@') + INDEX(clname,'@STARTDATE@')
1785            DO WHILE ( idx /= 0 ) 
1786               cldate = iom_sdate( fjulday - rdt / rday )
1787               clname = clname(1:idx-1)//TRIM(cldate)//clname(idx+11:LEN_TRIM(clname))
1788               idx = INDEX(clname,'@startdate@') + INDEX(clname,'@STARTDATE@')
1789            END DO
1790
1791            idx = INDEX(clname,'@startdatefull@') + INDEX(clname,'@STARTDATEFULL@')
1792            DO WHILE ( idx /= 0 ) 
1793               cldate = iom_sdate( fjulday - rdt / rday, ldfull = .TRUE. )
1794               clname = clname(1:idx-1)//TRIM(cldate)//clname(idx+15:LEN_TRIM(clname))
1795               idx = INDEX(clname,'@startdatefull@') + INDEX(clname,'@STARTDATEFULL@')
1796            END DO
1797
1798            idx = INDEX(clname,'@enddate@') + INDEX(clname,'@ENDDATE@')
1799            DO WHILE ( idx /= 0 ) 
1800               cldate = iom_sdate( fjulday + rdt / rday * REAL( nitend - nit000, wp ), ld24 = .TRUE. )
1801               clname = clname(1:idx-1)//TRIM(cldate)//clname(idx+9:LEN_TRIM(clname))
1802               idx = INDEX(clname,'@enddate@') + INDEX(clname,'@ENDDATE@')
1803            END DO
1804
1805            idx = INDEX(clname,'@enddatefull@') + INDEX(clname,'@ENDDATEFULL@')
1806            DO WHILE ( idx /= 0 ) 
1807               cldate = iom_sdate( fjulday + rdt / rday * REAL( nitend - nit000, wp ), ld24 = .TRUE., ldfull = .TRUE. )
1808               clname = clname(1:idx-1)//TRIM(cldate)//clname(idx+13:LEN_TRIM(clname))
1809               idx = INDEX(clname,'@enddatefull@') + INDEX(clname,'@ENDDATEFULL@')
1810            END DO
1811
1812            IF( jn == 1 .AND. TRIM(Agrif_CFixed()) /= '0' )   clname = TRIM(Agrif_CFixed())//"_"//TRIM(clname)
1813            IF( jn == 1 )   CALL iom_set_file_attr( cdid, name        = clname )
1814            IF( jn == 2 )   CALL iom_set_file_attr( cdid, name_suffix = clname )
1815
1816         ENDIF
1817
1818      END DO
1819
1820   END SUBROUTINE iom_update_file_name
1821
1822
1823   FUNCTION iom_sdate( pjday, ld24, ldfull )
1824      !!----------------------------------------------------------------------
1825      !!                     ***  ROUTINE iom_sdate  ***
1826      !!
1827      !! ** Purpose :   send back the date corresponding to the given julian day
1828      !!
1829      !!----------------------------------------------------------------------
1830      REAL(wp), INTENT(in   )           ::   pjday         ! julian day
1831      LOGICAL , INTENT(in   ), OPTIONAL ::   ld24          ! true to force 24:00 instead of 00:00
1832      LOGICAL , INTENT(in   ), OPTIONAL ::   ldfull        ! true to get the compleate date: yyyymmdd_hh:mm:ss
1833      !
1834      CHARACTER(LEN=20) ::   iom_sdate
1835      CHARACTER(LEN=50) ::   clfmt                         !  format used to write the date
1836      INTEGER           ::   iyear, imonth, iday, ihour, iminute, isec
1837      REAL(wp)          ::   zsec
1838      LOGICAL           ::   ll24, llfull
1839      !
1840      IF( PRESENT(ld24) ) THEN   ;   ll24 = ld24
1841      ELSE                       ;   ll24 = .FALSE.
1842      ENDIF
1843
1844      IF( PRESENT(ldfull) ) THEN   ;   llfull = ldfull
1845      ELSE                         ;   llfull = .FALSE.
1846      ENDIF
1847
1848      CALL ju2ymds( pjday, iyear, imonth, iday, zsec )
1849      isec = NINT(zsec)
1850
1851      IF ( ll24 .AND. isec == 0 ) THEN   ! 00:00 of the next day -> move to 24:00 of the current day
1852         CALL ju2ymds( pjday - 1., iyear, imonth, iday, zsec )
1853         isec = 86400
1854      ENDIF
1855
1856      IF( iyear < 10000 ) THEN   ;   clfmt = "i4.4,2i2.2"                ! format used to write the date
1857      ELSE                       ;   WRITE(clfmt, "('i',i1,',2i2.2')") INT(LOG10(REAL(iyear,wp))) + 1
1858      ENDIF
1859     
1860!$AGRIF_DO_NOT_TREAT     
1861! Should be fixed in the conv
1862      IF( llfull ) THEN
1863         clfmt = TRIM(clfmt)//",'_',i2.2,':',i2.2,':',i2.2"
1864         ihour   = isec / 3600
1865         isec    = MOD(isec, 3600)
1866         iminute = isec / 60
1867         isec    = MOD(isec, 60)
1868         WRITE(iom_sdate, '('//TRIM(clfmt)//')') iyear, imonth, iday, ihour, iminute, isec    ! date of the end of run
1869      ELSE
1870         WRITE(iom_sdate, '('//TRIM(clfmt)//')') iyear, imonth, iday                          ! date of the end of run
1871      ENDIF
1872!$AGRIF_END_DO_NOT_TREAT     
1873
1874   END FUNCTION iom_sdate
1875
1876#else
1877
1878
1879   SUBROUTINE iom_setkt( kt, cdname )
1880      INTEGER         , INTENT(in)::   kt 
1881      CHARACTER(LEN=*), INTENT(in) ::   cdname
1882      IF( .FALSE. )   WRITE(numout,*) kt, cdname   ! useless test to avoid compilation warnings
1883   END SUBROUTINE iom_setkt
1884
1885   SUBROUTINE iom_context_finalize( cdname )
1886      CHARACTER(LEN=*), INTENT(in) ::   cdname
1887      IF( .FALSE. )   WRITE(numout,*)  cdname   ! useless test to avoid compilation warnings
1888   END SUBROUTINE iom_context_finalize
1889
1890#endif
1891
1892   LOGICAL FUNCTION iom_use( cdname )
1893      CHARACTER(LEN=*), INTENT(in) ::   cdname
1894#if defined key_iomput
1895      iom_use = xios_field_is_active( cdname )
1896#else
1897      iom_use = .FALSE.
1898#endif
1899   END FUNCTION iom_use
1900   
1901   !!======================================================================
1902END MODULE iom
Note: See TracBrowser for help on using the repository browser.