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

source: branches/2017/dev_r8183_ICEMODEL/NEMOGCM/NEMO/OPA_SRC/IOM/iom.F90 @ 8239

Last change on this file since 8239 was 8239, checked in by clem, 7 years ago

merge with v3_6_CMIP6_ice_diagnostics@r8238

  • Property svn:keywords set to Id
File size: 96.9 KB
Line 
1MODULE iom
2   !!=====================================================================
3   !!                    ***  MODULE  iom ***
4   !! Input/Output manager :  Library to read input files
5   !!====================================================================
6   !! History :  2.0  ! 2005-12  (J. Belier) Original code
7   !!            2.0  ! 2006-02  (S. Masson) Adaptation to NEMO
8   !!            3.0  ! 2007-07  (D. Storkey) Changes to iom_gettime
9   !!            3.4  ! 2012-12  (R. Bourdalle-Badie and G. Reffray)  add C1D case 
10   !!            3.6  ! 2014-15  DIMG format removed
11   !!            3.6  ! 2015-15  (J. Harle) Added procedure to read REAL attributes
12   !!--------------------------------------------------------------------
13
14   !!--------------------------------------------------------------------
15   !!   iom_open       : open a file read only
16   !!   iom_close      : close a file or all files opened by iom
17   !!   iom_get        : read a field (interfaced to several routines)
18   !!   iom_gettime    : read the time axis cdvar in the file
19   !!   iom_varid      : get the id of a variable in a file
20   !!   iom_rstput     : write a field in a restart file (interfaced to several routines)
21   !!--------------------------------------------------------------------
22   USE dom_oce         ! ocean space and time domain
23   USE c1d             ! 1D vertical configuration
24   USE flo_oce         ! floats module declarations
25   USE lbclnk          ! lateal boundary condition / mpp exchanges
26   USE iom_def         ! iom variables definitions
27   USE iom_nf90        ! NetCDF format with native NetCDF library
28   USE in_out_manager  ! I/O manager
29   USE lib_mpp           ! MPP library
30#if defined key_iomput
31   USE sbc_oce, ONLY :   nn_fsbc         ! ocean space and time domain
32   USE trc_oce, ONLY :   nn_dttrc        !  !: frequency of step on passive tracers
33   USE icb_oce, ONLY :   nclasses, class_num       !  !: iceberg classes
34#if defined key_lim3
35   USE ice    , ONLY :   jpl
36#elif defined key_lim2
37   USE par_ice_2
38#endif
39   USE domngb          ! ocean space and time domain
40   USE phycst          ! physical constants
41   USE dianam          ! build name of file
42   USE xios
43# 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      ! SIMIP diagnostics (4 main arctic straits)
198      CALL iom_set_axis_attr( "nstrait", (/ (REAL(ji,wp), ji=1,4) /) )
199#endif
200      CALL iom_set_axis_attr( "icbcla", class_num )
201      CALL iom_set_axis_attr( "iax_20C", (/ REAL(20,wp) /) )
202      CALL iom_set_axis_attr( "iax_28C", (/ REAL(28,wp) /) )
203     
204      ! automatic definitions of some of the xml attributs
205      CALL set_xmlatt
206
207      ! end file definition
208      dtime%second = rdt
209      CALL xios_set_timestep(dtime)
210      CALL xios_close_context_definition()
211     
212      CALL xios_update_calendar(0)
213
214      DEALLOCATE( zt_bnds, zw_bnds )
215
216#endif
217     
218   END SUBROUTINE iom_init
219
220
221   SUBROUTINE iom_swap( cdname )
222      !!---------------------------------------------------------------------
223      !!                   ***  SUBROUTINE  iom_swap  ***
224      !!
225      !! ** Purpose :  swap context between different agrif grid for xmlio_server
226      !!---------------------------------------------------------------------
227      CHARACTER(len=*), INTENT(in) :: cdname
228#if defined key_iomput
229      TYPE(xios_context) :: nemo_hdl
230
231      IF( TRIM(Agrif_CFixed()) == '0' ) THEN
232        CALL xios_get_handle(TRIM(cdname),nemo_hdl)
233      ELSE
234        CALL xios_get_handle(TRIM(Agrif_CFixed())//"_"//TRIM(cdname),nemo_hdl)
235      ENDIF
236      !
237      CALL xios_set_current_context(nemo_hdl)
238#endif
239      !
240   END SUBROUTINE iom_swap
241
242
243   SUBROUTINE iom_open( cdname, kiomid, ldwrt, kdom, kiolib, ldstop, ldiof )
244      !!---------------------------------------------------------------------
245      !!                   ***  SUBROUTINE  iom_open  ***
246      !!
247      !! ** Purpose :  open an input file (return 0 if not found)
248      !!---------------------------------------------------------------------
249      CHARACTER(len=*), INTENT(in   )           ::   cdname   ! File name
250      INTEGER         , INTENT(  out)           ::   kiomid   ! iom identifier of the opened file
251      LOGICAL         , INTENT(in   ), OPTIONAL ::   ldwrt    ! open in write modeb          (default = .FALSE.)
252      INTEGER         , INTENT(in   ), OPTIONAL ::   kdom     ! Type of domain to be written (default = jpdom_local_noovlap)
253      INTEGER         , INTENT(in   ), OPTIONAL ::   kiolib   ! library used to open the file (default = jpnf90)
254      LOGICAL         , INTENT(in   ), OPTIONAL ::   ldstop   ! stop if open to read a non-existing file (default = .TRUE.)
255      LOGICAL         , INTENT(in   ), OPTIONAL ::   ldiof    ! Interp On the Fly, needed for AGRIF (default = .FALSE.)
256
257      CHARACTER(LEN=256)    ::   clname    ! the name of the file based on cdname [[+clcpu]+clcpu]
258      CHARACTER(LEN=256)    ::   cltmpn    ! tempory name to store clname (in writting mode)
259      CHARACTER(LEN=10)     ::   clsuffix  ! ".nc"
260      CHARACTER(LEN=15)     ::   clcpu     ! the cpu number (max jpmax_digits digits)
261      CHARACTER(LEN=256)    ::   clinfo    ! info character
262      LOGICAL               ::   llok      ! check the existence
263      LOGICAL               ::   llwrt     ! local definition of ldwrt
264      LOGICAL               ::   llnoov    ! local definition to read overlap
265      LOGICAL               ::   llstop    ! local definition of ldstop
266      LOGICAL               ::   lliof     ! local definition of ldiof
267      INTEGER               ::   iolib     ! library do we use to open the file
268      INTEGER               ::   icnt      ! counter for digits in clcpu (max = jpmax_digits)
269      INTEGER               ::   iln, ils  ! lengths of character
270      INTEGER               ::   idom      ! type of domain
271      INTEGER               ::   istop     !
272      INTEGER, DIMENSION(2,5) ::   idompar ! domain parameters:
273      ! local number of points for x,y dimensions
274      ! position of first local point for x,y dimensions
275      ! position of last local point for x,y dimensions
276      ! start halo size for x,y dimensions
277      ! end halo size for x,y dimensions
278      !---------------------------------------------------------------------
279      ! Initializations and control
280      ! =============
281      kiomid = -1
282      clinfo = '                    iom_open ~~~  '
283      istop = nstop
284      ! if iom_open is called for the first time: initialize iom_file(:)%nfid to 0
285      ! (could be done when defining iom_file in f95 but not in f90)
286      IF( Agrif_Root() ) THEN
287         IF( iom_open_init == 0 ) THEN
288            iom_file(:)%nfid = 0
289            iom_open_init = 1
290         ENDIF
291      ENDIF
292      ! do we read or write the file?
293      IF( PRESENT(ldwrt) ) THEN   ;   llwrt = ldwrt
294      ELSE                        ;   llwrt = .FALSE.
295      ENDIF
296      ! do we call ctl_stop if we try to open a non-existing file in read mode?
297      IF( PRESENT(ldstop) ) THEN   ;   llstop = ldstop
298      ELSE                         ;   llstop = .TRUE.
299      ENDIF
300      ! what library do we use to open the file?
301      IF( PRESENT(kiolib) ) THEN   ;   iolib = kiolib
302      ELSE                         ;   iolib = jpnf90
303      ENDIF
304      ! are we using interpolation on the fly?
305      IF( PRESENT(ldiof) ) THEN   ;   lliof = ldiof
306      ELSE                        ;   lliof = .FALSE.
307      ENDIF
308      ! do we read the overlap
309      ! ugly patch SM+JMM+RB to overwrite global definition in some cases
310      llnoov = (jpni * jpnj ) == jpnij .AND. .NOT. lk_agrif 
311      ! create the file name by added, if needed, TRIM(Agrif_CFixed()) and TRIM(clsuffix)
312      ! =============
313      clname   = trim(cdname)
314      IF ( .NOT. Agrif_Root() .AND. .NOT. lliof ) THEN
315         iln    = INDEX(clname,'/') 
316         cltmpn = clname(1:iln)
317         clname = clname(iln+1:LEN_TRIM(clname))
318         clname=TRIM(cltmpn)//TRIM(Agrif_CFixed())//'_'//TRIM(clname)
319      ENDIF
320      ! which suffix should we use?
321      SELECT CASE (iolib)
322      CASE (jpnf90   ) ;   clsuffix = '.nc'
323      CASE DEFAULT     ;   clsuffix = ''
324         CALL ctl_stop( TRIM(clinfo), 'accepted IO library is only jpnf90 (jpioipsl option has been removed) ' )
325      END SELECT
326      ! Add the suffix if needed
327      iln = LEN_TRIM(clname)
328      ils = LEN_TRIM(clsuffix)
329      IF( iln <= ils .OR. INDEX( TRIM(clname), TRIM(clsuffix), back = .TRUE. ) /= iln - ils + 1 )   &
330         &   clname = TRIM(clname)//TRIM(clsuffix)
331      cltmpn = clname   ! store this name
332      ! try to find if the file to be opened already exist
333      ! =============
334      INQUIRE( FILE = clname, EXIST = llok )
335      IF( .NOT.llok ) THEN
336         ! we try to add the cpu number to the name
337         WRITE(clcpu,*) narea-1
338
339         clcpu  = TRIM(ADJUSTL(clcpu))
340         iln = INDEX(clname,TRIM(clsuffix), back = .TRUE.)
341         clname = clname(1:iln-1)//'_'//TRIM(clcpu)//TRIM(clsuffix)
342         icnt = 0
343         INQUIRE( FILE = clname, EXIST = llok ) 
344         ! we try different formats for the cpu number by adding 0
345         DO WHILE( .NOT.llok .AND. icnt < jpmax_digits )
346            clcpu  = "0"//trim(clcpu)
347            clname = clname(1:iln-1)//'_'//TRIM(clcpu)//TRIM(clsuffix)
348            INQUIRE( FILE = clname, EXIST = llok )
349            icnt = icnt + 1
350         END DO
351      ENDIF
352      IF( llwrt ) THEN
353         ! check the domain definition
354! JMM + SM: ugly patch before getting the new version of lib_mpp)
355!         idom = jpdom_local_noovlap   ! default definition
356         IF( llnoov ) THEN   ;   idom = jpdom_local_noovlap   ! default definition
357         ELSE                ;   idom = jpdom_local_full      ! default definition
358         ENDIF
359         IF( PRESENT(kdom) )   idom = kdom
360         ! create the domain informations
361         ! =============
362         SELECT CASE (idom)
363         CASE (jpdom_local_full)
364            idompar(:,1) = (/ jpi             , jpj              /)
365            idompar(:,2) = (/ nimpp           , njmpp            /)
366            idompar(:,3) = (/ nimpp + jpi - 1 , njmpp + jpj - 1  /)
367            idompar(:,4) = (/ nldi - 1        , nldj - 1         /)
368            idompar(:,5) = (/ jpi - nlei      , jpj - nlej       /)
369         CASE (jpdom_local_noextra)
370            idompar(:,1) = (/ nlci            , nlcj             /)
371            idompar(:,2) = (/ nimpp           , njmpp            /)
372            idompar(:,3) = (/ nimpp + nlci - 1, njmpp + nlcj - 1 /)
373            idompar(:,4) = (/ nldi - 1        , nldj - 1         /)
374            idompar(:,5) = (/ nlci - nlei     , nlcj - nlej      /)
375         CASE (jpdom_local_noovlap)
376            idompar(:,1) = (/ nlei  - nldi + 1, nlej  - nldj + 1 /)
377            idompar(:,2) = (/ nimpp + nldi - 1, njmpp + nldj - 1 /)
378            idompar(:,3) = (/ nimpp + nlei - 1, njmpp + nlej - 1 /)
379            idompar(:,4) = (/ 0               , 0                /)
380            idompar(:,5) = (/ 0               , 0                /)
381         CASE DEFAULT
382            CALL ctl_stop( TRIM(clinfo), 'wrong value of kdom, only jpdom_local* cases are accepted' )
383         END SELECT
384      ENDIF
385      ! Open the NetCDF file
386      ! =============
387      ! do we have some free file identifier?
388      IF( MINVAL(iom_file(:)%nfid) /= 0 )   &
389         &   CALL ctl_stop( TRIM(clinfo), 'No more free file identifier', 'increase jpmax_files in iom_def' )
390      ! if no file was found...
391      IF( .NOT. llok ) THEN
392         IF( .NOT. llwrt ) THEN   ! we are in read mode
393            IF( llstop ) THEN   ;   CALL ctl_stop( TRIM(clinfo), 'File '//TRIM(cltmpn)//'* not found' )
394            ELSE                ;   istop = nstop + 1   ! make sure that istop /= nstop so we don't open the file
395            ENDIF
396         ELSE                     ! we are in write mode so we
397            clname = cltmpn       ! get back the file name without the cpu number
398         ENDIF
399      ELSE
400         IF( llwrt .AND. .NOT. ln_clobber ) THEN   ! we stop as we want to write in a new file
401            CALL ctl_stop( TRIM(clinfo), 'We want to write in a new file but '//TRIM(clname)//' already exists...' )
402            istop = nstop + 1                      ! make sure that istop /= nstop so we don't open the file
403         ELSEIF( llwrt ) THEN     ! the file exists and we are in write mode with permission to
404            clname = cltmpn       ! overwrite so get back the file name without the cpu number
405         ENDIF
406      ENDIF
407      IF( istop == nstop ) THEN   ! no error within this routine
408         SELECT CASE (iolib)
409         CASE (jpnf90   )   ;   CALL iom_nf90_open(    clname, kiomid, llwrt, llok, idompar )
410         CASE DEFAULT
411            CALL ctl_stop( TRIM(clinfo)//' accepted IO library is only jpnf90 (jpioipsl option has been removed) ' )
412         END SELECT
413      ENDIF
414      !
415   END SUBROUTINE iom_open
416
417
418   SUBROUTINE iom_close( kiomid )
419      !!--------------------------------------------------------------------
420      !!                   ***  SUBROUTINE  iom_close  ***
421      !!
422      !! ** Purpose : close an input file, or all files opened by iom
423      !!--------------------------------------------------------------------
424      INTEGER, INTENT(inout), OPTIONAL ::   kiomid   ! iom identifier of the file to be closed
425      !                                              ! return 0 when file is properly closed
426      !                                              ! No argument: all files opened by iom are closed
427
428      INTEGER ::   jf         ! dummy loop indices
429      INTEGER ::   i_s, i_e   ! temporary integer
430      CHARACTER(LEN=100)    ::   clinfo    ! info character
431      !---------------------------------------------------------------------
432      !
433      clinfo = '                    iom_close ~~~  '
434      IF( PRESENT(kiomid) ) THEN
435         i_s = kiomid
436         i_e = kiomid
437      ELSE
438         i_s = 1
439         i_e = jpmax_files
440      ENDIF
441
442      IF( i_s > 0 ) THEN
443         DO jf = i_s, i_e
444            IF( iom_file(jf)%nfid > 0 ) THEN
445               SELECT CASE (iom_file(jf)%iolib)
446               CASE (jpnf90   )   ;   CALL iom_nf90_close(    jf )
447               CASE DEFAULT
448                  CALL ctl_stop( TRIM(clinfo)//' accepted IO library is only jpnf90 (jpioipsl option has been removed)' )
449               END SELECT
450               iom_file(jf)%nfid       = 0          ! free the id
451               IF( PRESENT(kiomid) )   kiomid = 0   ! return 0 as id to specify that the file was closed
452               IF(lwp) WRITE(numout,*) TRIM(clinfo)//' close file: '//TRIM(iom_file(jf)%name)//' ok'
453            ELSEIF( PRESENT(kiomid) ) THEN
454               WRITE(ctmp1,*) '--->',  kiomid
455               CALL ctl_stop( TRIM(clinfo)//' Invalid file identifier', ctmp1 )
456            ENDIF
457         END DO
458      ENDIF
459      !   
460   END SUBROUTINE iom_close
461
462
463   FUNCTION iom_varid ( kiomid, cdvar, kdimsz, kndims, ldstop ) 
464      !!-----------------------------------------------------------------------
465      !!                  ***  FUNCTION  iom_varid  ***
466      !!
467      !! ** Purpose : get the id of a variable in a file (return 0 if not found)
468      !!-----------------------------------------------------------------------
469      INTEGER              , INTENT(in   )           ::   kiomid   ! file Identifier
470      CHARACTER(len=*)     , INTENT(in   )           ::   cdvar    ! name of the variable
471      INTEGER, DIMENSION(:), INTENT(  out), OPTIONAL ::   kdimsz   ! size of the dimensions
472      INTEGER,               INTENT(  out), OPTIONAL ::   kndims   ! size of the dimensions
473      LOGICAL              , INTENT(in   ), OPTIONAL ::   ldstop   ! stop if looking for non-existing variable (default = .TRUE.)
474      !
475      INTEGER                        ::   iom_varid, iiv, i_nvd
476      LOGICAL                        ::   ll_fnd
477      CHARACTER(LEN=100)             ::   clinfo                   ! info character
478      LOGICAL                        ::   llstop                   ! local definition of ldstop
479      !!-----------------------------------------------------------------------
480      iom_varid = 0                         ! default definition
481      ! do we call ctl_stop if we look for non-existing variable?
482      IF( PRESENT(ldstop) ) THEN   ;   llstop = ldstop
483      ELSE                         ;   llstop = .TRUE.
484      ENDIF
485      !
486      IF( kiomid > 0 ) THEN
487         clinfo = 'iom_varid, file: '//trim(iom_file(kiomid)%name)//', var: '//trim(cdvar)
488         IF( iom_file(kiomid)%nfid == 0 ) THEN
489            CALL ctl_stop( trim(clinfo), 'the file is not open' )
490         ELSE
491            ll_fnd  = .FALSE.
492            iiv = 0
493            !
494            DO WHILE ( .NOT.ll_fnd .AND. iiv < iom_file(kiomid)%nvars )
495               iiv = iiv + 1
496               ll_fnd  = ( TRIM(cdvar) == TRIM(iom_file(kiomid)%cn_var(iiv)) )
497            END DO
498            !
499            IF( .NOT.ll_fnd ) THEN
500               iiv = iiv + 1
501               IF( iiv <= jpmax_vars ) THEN
502                  SELECT CASE (iom_file(kiomid)%iolib)
503                  CASE (jpnf90   )   ;   iom_varid = iom_nf90_varid  ( kiomid, cdvar, iiv, kdimsz, kndims )
504                  CASE DEFAULT
505                     CALL ctl_stop( TRIM(clinfo)//' accepted IO library is only jpnf90 (jpioipsl option has been removed)' )
506                  END SELECT
507               ELSE
508                  CALL ctl_stop( trim(clinfo), 'Too many variables in the file '//iom_file(kiomid)%name,   &
509                        &                         'increase the parameter jpmax_vars')
510               ENDIF
511               IF( llstop .AND. iom_varid == -1 )   CALL ctl_stop( TRIM(clinfo)//' not found' ) 
512            ELSE
513               iom_varid = iiv
514               IF( PRESENT(kdimsz) ) THEN
515                  i_nvd = iom_file(kiomid)%ndims(iiv)
516                  IF( i_nvd == size(kdimsz) ) THEN
517                     kdimsz(:) = iom_file(kiomid)%dimsz(1:i_nvd,iiv)
518                  ELSE
519                     WRITE(ctmp1,*) i_nvd, size(kdimsz)
520                     CALL ctl_stop( trim(clinfo), 'error in kdimsz size'//trim(ctmp1) )
521                  ENDIF
522               ENDIF
523               IF( PRESENT(kndims) )  kndims = iom_file(kiomid)%ndims(iiv)
524            ENDIF
525         ENDIF
526      ENDIF
527      !
528   END FUNCTION iom_varid
529
530
531   !!----------------------------------------------------------------------
532   !!                   INTERFACE iom_get
533   !!----------------------------------------------------------------------
534   SUBROUTINE iom_g0d( kiomid, cdvar, pvar, ktime )
535      INTEGER         , INTENT(in   )                 ::   kiomid    ! Identifier of the file
536      CHARACTER(len=*), INTENT(in   )                 ::   cdvar     ! Name of the variable
537      REAL(wp)        , INTENT(  out)                 ::   pvar      ! read field
538      INTEGER         , INTENT(in   ),     OPTIONAL   ::   ktime     ! record number
539      !
540      INTEGER                                         ::   idvar     ! variable id
541      INTEGER                                         ::   idmspc    ! number of spatial dimensions
542      INTEGER         , DIMENSION(1)                  ::   itime     ! record number
543      CHARACTER(LEN=100)                              ::   clinfo    ! info character
544      CHARACTER(LEN=100)                              ::   clname    ! file name
545      CHARACTER(LEN=1)                                ::   cldmspc   !
546      !
547      itime = 1
548      IF( PRESENT(ktime) ) itime = ktime
549      !
550      clname = iom_file(kiomid)%name
551      clinfo = '          iom_g0d, file: '//trim(clname)//', var: '//trim(cdvar)
552      !
553      IF( kiomid > 0 ) THEN
554         idvar = iom_varid( kiomid, cdvar )
555         IF( iom_file(kiomid)%nfid > 0 .AND. idvar > 0 ) THEN
556            idmspc = iom_file ( kiomid )%ndims( idvar )
557            IF( iom_file(kiomid)%luld(idvar) )  idmspc = idmspc - 1
558            WRITE(cldmspc , fmt='(i1)') idmspc
559            IF( idmspc > 0 )  CALL ctl_stop( TRIM(clinfo), 'When reading to a 0D array, we do not accept data', &
560                                 &                         'with 1 or more spatial dimensions: '//cldmspc//' were found.' , &
561                                 &                         'Use ncwa -a to suppress the unnecessary dimensions' )
562            SELECT CASE (iom_file(kiomid)%iolib)
563            CASE (jpnf90   )   ;   CALL iom_nf90_get(    kiomid, idvar, pvar, itime )
564            CASE DEFAULT
565               CALL ctl_stop( 'iom_g0d: accepted IO library is only jpnf90 (jpioipsl option has been removed)' )
566            END SELECT
567         ENDIF
568      ENDIF
569   END SUBROUTINE iom_g0d
570
571   SUBROUTINE iom_g1d( kiomid, kdom, cdvar, pvar, ktime, kstart, kcount )
572      INTEGER         , INTENT(in   )                         ::   kiomid    ! Identifier of the file
573      INTEGER         , INTENT(in   )                         ::   kdom      ! Type of domain to be read
574      CHARACTER(len=*), INTENT(in   )                         ::   cdvar     ! Name of the variable
575      REAL(wp)        , INTENT(  out), DIMENSION(:)           ::   pvar      ! read field
576      INTEGER         , INTENT(in   )              , OPTIONAL ::   ktime     ! record number
577      INTEGER         , INTENT(in   ), DIMENSION(1), OPTIONAL ::   kstart    ! start axis position of the reading
578      INTEGER         , INTENT(in   ), DIMENSION(1), OPTIONAL ::   kcount    ! number of points in each axis
579      !
580      IF( kiomid > 0 ) THEN
581         IF( iom_file(kiomid)%nfid > 0 ) CALL iom_get_123d( kiomid, kdom       , cdvar        , pv_r1d=pvar,   &
582              &                                                     ktime=ktime, kstart=kstart, kcount=kcount )
583      ENDIF
584   END SUBROUTINE iom_g1d
585
586   SUBROUTINE iom_g2d( kiomid, kdom, cdvar, pvar, ktime, kstart, kcount, lrowattr )
587      INTEGER         , INTENT(in   )                           ::   kiomid    ! Identifier of the file
588      INTEGER         , INTENT(in   )                           ::   kdom      ! Type of domain to be read
589      CHARACTER(len=*), INTENT(in   )                           ::   cdvar     ! Name of the variable
590      REAL(wp)        , INTENT(  out), DIMENSION(:,:)           ::   pvar      ! read field
591      INTEGER         , INTENT(in   )                , OPTIONAL ::   ktime     ! record number
592      INTEGER         , INTENT(in   ), DIMENSION(2)  , OPTIONAL ::   kstart    ! start axis position of the reading
593      INTEGER         , INTENT(in   ), DIMENSION(2)  , OPTIONAL ::   kcount    ! number of points in each axis
594      LOGICAL         , INTENT(in   )                , OPTIONAL ::   lrowattr  ! logical flag telling iom_get to
595                                                                               ! look for and use a file attribute
596                                                                               ! called open_ocean_jstart to set the start
597                                                                               ! value for the 2nd dimension (netcdf only)
598      !
599      IF( kiomid > 0 ) THEN
600         IF( iom_file(kiomid)%nfid > 0 ) CALL iom_get_123d( kiomid, kdom       , cdvar        , pv_r2d=pvar,   &
601              &                                                     ktime=ktime, kstart=kstart, kcount=kcount, &
602              &                                                     lrowattr=lrowattr )
603      ENDIF
604   END SUBROUTINE iom_g2d
605
606   SUBROUTINE iom_g3d( kiomid, kdom, cdvar, pvar, ktime, kstart, kcount, lrowattr )
607      INTEGER         , INTENT(in   )                             ::   kiomid    ! Identifier of the file
608      INTEGER         , INTENT(in   )                             ::   kdom      ! Type of domain to be read
609      CHARACTER(len=*), INTENT(in   )                             ::   cdvar     ! Name of the variable
610      REAL(wp)        , INTENT(  out), DIMENSION(:,:,:)           ::   pvar      ! read field
611      INTEGER         , INTENT(in   )                  , OPTIONAL ::   ktime     ! record number
612      INTEGER         , INTENT(in   ), DIMENSION(3)    , OPTIONAL ::   kstart    ! start axis position of the reading
613      INTEGER         , INTENT(in   ), DIMENSION(3)    , OPTIONAL ::   kcount    ! number of points in each axis
614      LOGICAL         , INTENT(in   )                  , OPTIONAL ::   lrowattr  ! logical flag telling iom_get to
615                                                                                 ! look for and use a file attribute
616                                                                                 ! called open_ocean_jstart to set the start
617                                                                                 ! value for the 2nd dimension (netcdf only)
618      !
619      IF( kiomid > 0 ) THEN
620         IF( iom_file(kiomid)%nfid > 0 ) CALL iom_get_123d( kiomid, kdom       , cdvar        , pv_r3d=pvar,   &
621              &                                                     ktime=ktime, kstart=kstart, kcount=kcount, &
622              &                                                     lrowattr=lrowattr )
623      ENDIF
624   END SUBROUTINE iom_g3d
625   !!----------------------------------------------------------------------
626
627   SUBROUTINE iom_get_123d( kiomid, kdom  , cdvar ,   &
628         &                  pv_r1d, pv_r2d, pv_r3d,   &
629         &                  ktime , kstart, kcount,   &
630         &                  lrowattr                )
631      !!-----------------------------------------------------------------------
632      !!                  ***  ROUTINE  iom_get_123d  ***
633      !!
634      !! ** Purpose : read a 1D/2D/3D variable
635      !!
636      !! ** Method : read ONE record at each CALL
637      !!-----------------------------------------------------------------------
638      INTEGER                    , INTENT(in   )           ::   kiomid     ! Identifier of the file
639      INTEGER                    , INTENT(in   )           ::   kdom       ! Type of domain to be read
640      CHARACTER(len=*)           , INTENT(in   )           ::   cdvar      ! Name of the variable
641      REAL(wp), DIMENSION(:)     , INTENT(  out), OPTIONAL ::   pv_r1d     ! read field (1D case)
642      REAL(wp), DIMENSION(:,:)   , INTENT(  out), OPTIONAL ::   pv_r2d     ! read field (2D case)
643      REAL(wp), DIMENSION(:,:,:) , INTENT(  out), OPTIONAL ::   pv_r3d     ! read field (3D case)
644      INTEGER                    , INTENT(in   ), OPTIONAL ::   ktime      ! record number
645      INTEGER , DIMENSION(:)     , INTENT(in   ), OPTIONAL ::   kstart     ! start position of the reading in each axis
646      INTEGER , DIMENSION(:)     , INTENT(in   ), OPTIONAL ::   kcount     ! number of points to be read in each axis
647      LOGICAL                    , INTENT(in   ), OPTIONAL ::   lrowattr   ! logical flag telling iom_get to
648                                                                           ! look for and use a file attribute
649                                                                           ! called open_ocean_jstart to set the start
650                                                                           ! value for the 2nd dimension (netcdf only)
651      !
652      LOGICAL                        ::   llnoov      ! local definition to read overlap
653      LOGICAL                        ::   luse_jattr  ! local definition to read open_ocean_jstart file attribute
654      INTEGER                        ::   jstartrow   ! start point for 2nd dimension optionally set by file attribute
655      INTEGER                        ::   jl          ! loop on number of dimension
656      INTEGER                        ::   idom        ! type of domain
657      INTEGER                        ::   idvar       ! id of the variable
658      INTEGER                        ::   inbdim      ! number of dimensions of the variable
659      INTEGER                        ::   idmspc      ! number of spatial dimensions
660      INTEGER                        ::   itime       ! record number
661      INTEGER                        ::   istop       ! temporary value of nstop
662      INTEGER                        ::   ix1, ix2, iy1, iy2   ! subdomain indexes
663      INTEGER                        ::   ji, jj      ! loop counters
664      INTEGER                        ::   irankpv     !
665      INTEGER                        ::   ind1, ind2  ! substring index
666      INTEGER, DIMENSION(jpmax_dims) ::   istart      ! starting point to read for each axis
667      INTEGER, DIMENSION(jpmax_dims) ::   icnt        ! number of value to read along each axis
668      INTEGER, DIMENSION(jpmax_dims) ::   idimsz      ! size of the dimensions of the variable
669      INTEGER, DIMENSION(jpmax_dims) ::   ishape      ! size of the dimensions of the variable
670      REAL(wp)                       ::   zscf, zofs  ! sacle_factor and add_offset
671      INTEGER                        ::   itmp        ! temporary integer
672      CHARACTER(LEN=256)             ::   clinfo      ! info character
673      CHARACTER(LEN=256)             ::   clname      ! file name
674      CHARACTER(LEN=1)               ::   clrankpv, cldmspc      !
675      LOGICAL                        ::   ll_depth_spec ! T => if kstart, kcount present then *only* use values for 3rd spatial dimension.
676      !---------------------------------------------------------------------
677      !
678      clname = iom_file(kiomid)%name   !   esier to read
679      clinfo = '          iom_get_123d, file: '//trim(clname)//', var: '//trim(cdvar)
680      ! local definition of the domain ?
681      idom = kdom
682      ! do we read the overlap
683      ! ugly patch SM+JMM+RB to overwrite global definition in some cases
684      llnoov = (jpni * jpnj ) == jpnij .AND. .NOT. lk_agrif 
685      ! check kcount and kstart optionals parameters...
686      IF( PRESENT(kcount) .AND. (.NOT. PRESENT(kstart)) ) CALL ctl_stop(trim(clinfo), 'kcount present needs kstart present')
687      IF( PRESENT(kstart) .AND. (.NOT. PRESENT(kcount)) ) CALL ctl_stop(trim(clinfo), 'kstart present needs kcount present')
688      IF( PRESENT(kstart) .AND. idom /= jpdom_unknown .AND.  idom /= jpdom_autoglo_xy  ) &
689     &           CALL ctl_stop(trim(clinfo), 'kstart present needs kdom = jpdom_unknown or kdom = jpdom_autoglo_xy')
690
691      luse_jattr = .false.
692      IF( PRESENT(lrowattr) ) THEN
693         IF( lrowattr .AND. idom /= jpdom_data   ) CALL ctl_stop(trim(clinfo), 'lrowattr present and true needs kdom = jpdom_data')
694         IF( lrowattr .AND. idom == jpdom_data   ) luse_jattr = .true.
695      ENDIF
696      IF( luse_jattr ) THEN
697         SELECT CASE (iom_file(kiomid)%iolib)
698         CASE (jpnf90   )   
699             ! Ok
700         CASE DEFAULT   
701            CALL ctl_stop( TRIM(clinfo)//' accepted IO library is only jpnf90 (jpioipsl option has been removed)' )
702         END SELECT
703      ENDIF
704
705      ! Search for the variable in the data base (eventually actualize data)
706      istop = nstop
707      idvar = iom_varid( kiomid, cdvar )
708      !
709      IF( idvar > 0 ) THEN
710         ! to write iom_file(kiomid)%dimsz in a shorter way !
711         idimsz(:) = iom_file(kiomid)%dimsz(:, idvar) 
712         inbdim = iom_file(kiomid)%ndims(idvar)            ! number of dimensions in the file
713         idmspc = inbdim                                   ! number of spatial dimensions in the file
714         IF( iom_file(kiomid)%luld(idvar) )   idmspc = inbdim - 1
715         IF( idmspc > 3 )   CALL ctl_stop(trim(clinfo), 'the file has more than 3 spatial dimensions this case is not coded...') 
716         !
717         ! update idom definition...
718         ! Identify the domain in case of jpdom_auto(glo/dta) definition
719         IF( idom == jpdom_autoglo_xy ) THEN
720            ll_depth_spec = .TRUE.
721            idom = jpdom_autoglo
722         ELSE
723            ll_depth_spec = .FALSE.
724         ENDIF
725         IF( idom == jpdom_autoglo .OR. idom == jpdom_autodta ) THEN           
726            IF( idom == jpdom_autoglo ) THEN   ;   idom = jpdom_global 
727            ELSE                               ;   idom = jpdom_data
728            ENDIF
729            ind1 = INDEX( clname, '_', back = .TRUE. ) + 1
730            ind2 = INDEX( clname, '.', back = .TRUE. ) - 1
731            IF( ind2 > ind1 ) THEN   ;   IF( VERIFY( clname(ind1:ind2), '0123456789' ) == 0 )   idom = jpdom_local   ;   ENDIF
732         ENDIF
733         ! Identify the domain in case of jpdom_local definition
734         IF( idom == jpdom_local ) THEN
735            IF(     idimsz(1) == jpi               .AND. idimsz(2) == jpj               ) THEN   ;   idom = jpdom_local_full
736            ELSEIF( idimsz(1) == nlci              .AND. idimsz(2) == nlcj              ) THEN   ;   idom = jpdom_local_noextra
737            ELSEIF( idimsz(1) == (nlei - nldi + 1) .AND. idimsz(2) == (nlej - nldj + 1) ) THEN   ;   idom = jpdom_local_noovlap
738            ELSE   ;   CALL ctl_stop( trim(clinfo), 'impossible to identify the local domain' )
739            ENDIF
740         ENDIF
741         !
742         ! check the consistency between input array and data rank in the file
743         !
744         ! initializations
745         itime = 1
746         IF( PRESENT(ktime) ) itime = ktime
747
748         irankpv = 1 * COUNT( (/PRESENT(pv_r1d)/) ) + 2 * COUNT( (/PRESENT(pv_r2d)/) ) + 3 * COUNT( (/PRESENT(pv_r3d)/) )
749         WRITE(clrankpv, fmt='(i1)') irankpv
750         WRITE(cldmspc , fmt='(i1)') idmspc
751         !
752         IF(     idmspc <  irankpv ) THEN
753            CALL ctl_stop( TRIM(clinfo), 'The file has only '//cldmspc//' spatial dimension',   &
754               &                         'it is impossible to read a '//clrankpv//'D array from this file...' )
755         ELSEIF( idmspc == irankpv ) THEN
756            IF( PRESENT(pv_r1d) .AND. idom /= jpdom_unknown )   &
757               &   CALL ctl_stop( TRIM(clinfo), 'case not coded...You must use jpdom_unknown' )
758         ELSEIF( idmspc >  irankpv ) THEN
759               IF( PRESENT(pv_r2d) .AND. itime == 1 .AND. idimsz(3) == 1 .AND. idmspc == 3 ) THEN
760                  CALL ctl_warn( trim(clinfo), '2D array but 3 spatial dimensions for the data...'              ,   &
761                        &         'As the size of the z dimension is 1 and as we try to read the first record, ',   &
762                        &         'we accept this case, even if there is a possible mix-up between z and time dimension' )   
763                  idmspc = idmspc - 1
764               ELSE
765                  CALL ctl_stop( TRIM(clinfo), 'To keep iom lisibility, when reading a '//clrankpv//'D array,'         ,   &
766                     &                         'we do not accept data with '//cldmspc//' spatial dimensions',   &
767                     &                         'Use ncwa -a to suppress the unnecessary dimensions' )
768               ENDIF
769         ENDIF
770
771         !
772         ! definition of istart and icnt
773         !
774         icnt  (:) = 1
775         istart(:) = 1
776         istart(idmspc+1) = itime
777
778         IF( PRESENT(kstart) .AND. .NOT. ll_depth_spec ) THEN ; istart(1:idmspc) = kstart(1:idmspc) ; icnt(1:idmspc) = kcount(1:idmspc)
779         ELSE
780            IF(           idom == jpdom_unknown ) THEN                                                ; 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( 180., 90., ix, iy, 'T' ) !  i-line that passes near the North Pole : 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 = nn_fsbc  ;  f_of%timestep = 0  ; CALL iom_set_field_attr('SBC'             , freq_op=f_op, freq_offset=f_of)
1600      f_op%timestep = nn_fsbc  ;  f_of%timestep = 0  ; CALL iom_set_field_attr('SBC_scalar'      , freq_op=f_op, freq_offset=f_of)
1601      f_op%timestep = nn_dttrc ;  f_of%timestep = 0  ; CALL iom_set_field_attr('ptrc_T'          , freq_op=f_op, freq_offset=f_of)
1602      f_op%timestep = nn_dttrc ;  f_of%timestep = 0  ; CALL iom_set_field_attr('diad_T'          , freq_op=f_op, freq_offset=f_of)
1603
1604      ! output file names (attribut: name)
1605      DO ji = 1, 9
1606         WRITE(cl1,'(i1)') ji 
1607         CALL iom_update_file_name('file'//cl1)
1608      END DO
1609      DO ji = 1, 99
1610         WRITE(cl2,'(i2.2)') ji 
1611         CALL iom_update_file_name('file'//cl2)
1612      END DO
1613      DO ji = 1, 999
1614         WRITE(cl3,'(i3.3)') ji 
1615         CALL iom_update_file_name('file'//cl3)
1616      END DO
1617
1618      ! Zooms...
1619      clgrd = (/ 'T', 'U', 'W' /) 
1620      DO jg = 1, SIZE(clgrd)                                                                   ! grid type
1621         cl1 = clgrd(jg)
1622         ! Equatorial section (attributs: jbegin, ni, name_suffix)
1623         CALL dom_ngb( 0., 0., ix, iy, cl1 )
1624         CALL iom_set_zoom_domain_attr ('Eq'//cl1, jbegin=iy-1, ni=jpiglo)
1625         CALL iom_get_file_attr   ('Eq'//cl1, name_suffix = clsuff             )
1626         CALL iom_set_file_attr   ('Eq'//cl1, name_suffix = TRIM(clsuff)//'_Eq')
1627         CALL iom_update_file_name('Eq'//cl1)
1628      END DO
1629      ! TAO moorings (attributs: ibegin, jbegin, name_suffix)
1630      zlontao = (/ 137.0, 147.0, 156.0, 165.0, -180.0, -170.0, -155.0, -140.0, -125.0, -110.0, -95.0 /)
1631      zlattao = (/  -8.0,  -5.0,  -2.0,   0.0,    2.0,    5.0,    8.0 /)
1632      CALL set_mooring( zlontao, zlattao )
1633      ! RAMA moorings (attributs: ibegin, jbegin, name_suffix)
1634      zlonrama = (/  55.0,  67.0, 80.5, 90.0 /)
1635      zlatrama = (/ -16.0, -12.0, -8.0, -4.0, -1.5, 0.0, 1.5, 4.0, 8.0, 12.0, 15.0 /)
1636      CALL set_mooring( zlonrama, zlatrama )
1637      ! PIRATA moorings (attributs: ibegin, jbegin, name_suffix)
1638      zlonpira = (/ -38.0, -23.0, -10.0 /)
1639      zlatpira = (/ -19.0, -14.0,  -8.0, 0.0, 4.0, 8.0, 12.0, 15.0, 20.0 /)
1640      CALL set_mooring( zlonpira, zlatpira )
1641
1642     
1643   END SUBROUTINE set_xmlatt
1644
1645
1646   SUBROUTINE set_mooring( plon, plat)
1647      !!----------------------------------------------------------------------
1648      !!                     ***  ROUTINE set_mooring  ***
1649      !!
1650      !! ** Purpose :   automatic definitions of moorings xml attributs...
1651      !!
1652      !!----------------------------------------------------------------------
1653      REAL(wp), DIMENSION(:), INTENT(in) ::  plon, plat           ! longitudes/latitudes oft the mooring
1654      !
1655!!$      CHARACTER(len=1),DIMENSION(4) ::   clgrd = (/ 'T', 'U', 'V', 'W' /)   ! suffix name
1656      CHARACTER(len=1),DIMENSION(1) ::   clgrd = (/ 'T' /)        ! suffix name
1657      CHARACTER(len=256)            ::   clname                   ! file name
1658      CHARACTER(len=256)            ::   clsuff                   ! suffix name
1659      CHARACTER(len=1)              ::   cl1                      ! 1 character
1660      CHARACTER(len=6)              ::   clon,clat                ! name of longitude, latitude
1661      INTEGER                       ::   ji, jj, jg               ! loop counters
1662      INTEGER                       ::   ix, iy                   ! i-,j- index
1663      REAL(wp)                      ::   zlon, zlat
1664      !!----------------------------------------------------------------------
1665      DO jg = 1, SIZE(clgrd)
1666         cl1 = clgrd(jg)
1667         DO ji = 1, SIZE(plon)
1668            DO jj = 1, SIZE(plat)
1669               zlon = plon(ji)
1670               zlat = plat(jj)
1671               ! modifications for RAMA moorings
1672               IF( zlon ==  67. .AND. zlat ==  15. )   zlon =  65.
1673               IF( zlon ==  90. .AND. zlat <=  -4. )   zlon =  95.
1674               IF( zlon ==  95. .AND. zlat ==  -4. )   zlat =  -5.
1675               ! modifications for PIRATA moorings
1676               IF( zlon == -38. .AND. zlat == -19. )   zlon = -34.
1677               IF( zlon == -38. .AND. zlat == -14. )   zlon = -32.
1678               IF( zlon == -38. .AND. zlat ==  -8. )   zlon = -30.
1679               IF( zlon == -38. .AND. zlat ==   0. )   zlon = -35.
1680               IF( zlon == -23. .AND. zlat ==  20. )   zlat =  21.
1681               IF( zlon == -10. .AND. zlat == -14. )   zlat = -10.
1682               IF( zlon == -10. .AND. zlat ==  -8. )   zlat =  -6.
1683               IF( zlon == -10. .AND. zlat ==   4. ) THEN   ;   zlon = 0.   ;   zlat = 0.   ;   ENDIF
1684               CALL dom_ngb( zlon, zlat, ix, iy, cl1 )
1685               IF( zlon >= 0. ) THEN 
1686                  IF( zlon == REAL(NINT(zlon), wp) ) THEN   ;   WRITE(clon, '(i3,  a)') NINT( zlon), 'e'
1687                  ELSE                                      ;   WRITE(clon, '(f5.1,a)')       zlon , 'e'
1688                  ENDIF
1689               ELSE             
1690                  IF( zlon == REAL(NINT(zlon), wp) ) THEN   ;   WRITE(clon, '(i3,  a)') NINT(-zlon), 'w'
1691                  ELSE                                      ;   WRITE(clon, '(f5.1,a)')      -zlon , 'w'
1692                  ENDIF
1693               ENDIF
1694               IF( zlat >= 0. ) THEN 
1695                  IF( zlat == REAL(NINT(zlat), wp) ) THEN   ;   WRITE(clat, '(i2,  a)') NINT( zlat), 'n'
1696                  ELSE                                      ;   WRITE(clat, '(f4.1,a)')       zlat , 'n'
1697                  ENDIF
1698               ELSE             
1699                  IF( zlat == REAL(NINT(zlat), wp) ) THEN   ;   WRITE(clat, '(i2,  a)') NINT(-zlat), 's'
1700                  ELSE                                      ;   WRITE(clat, '(f4.1,a)')      -zlat , 's'
1701                  ENDIF
1702               ENDIF
1703               clname = TRIM(ADJUSTL(clat))//TRIM(ADJUSTL(clon))
1704               CALL iom_set_zoom_domain_attr  (TRIM(clname)//cl1, ibegin= ix-1, jbegin= iy-1)
1705
1706               CALL iom_get_file_attr   (TRIM(clname)//cl1, name_suffix = clsuff                         )
1707               CALL iom_set_file_attr   (TRIM(clname)//cl1, name_suffix = TRIM(clsuff)//'_'//TRIM(clname))
1708               CALL iom_update_file_name(TRIM(clname)//cl1)
1709            END DO
1710         END DO
1711      END DO
1712     
1713   END SUBROUTINE set_mooring
1714
1715   
1716   SUBROUTINE iom_update_file_name( cdid )
1717      !!----------------------------------------------------------------------
1718      !!                     ***  ROUTINE iom_update_file_name  ***
1719      !!
1720      !! ** Purpose :   
1721      !!
1722      !!----------------------------------------------------------------------
1723      CHARACTER(LEN=*)          , INTENT(in) ::   cdid
1724      !
1725      CHARACTER(LEN=256) ::   clname
1726      CHARACTER(LEN=20)  ::   clfreq
1727      CHARACTER(LEN=20)  ::   cldate
1728      INTEGER            ::   idx
1729      INTEGER            ::   jn
1730      INTEGER            ::   itrlen
1731      INTEGER            ::   iyear, imonth, iday, isec
1732      REAL(wp)           ::   zsec
1733      LOGICAL            ::   llexist
1734      TYPE(xios_duration)   ::   output_freq 
1735      !!----------------------------------------------------------------------
1736
1737      DO jn = 1,2
1738
1739         output_freq = xios_duration(0,0,0,0,0,0)
1740         IF( jn == 1 )   CALL iom_get_file_attr( cdid, name        = clname, output_freq = output_freq )
1741         IF( jn == 2 )   CALL iom_get_file_attr( cdid, name_suffix = clname )
1742
1743         IF ( TRIM(clname) /= '' ) THEN
1744
1745            idx = INDEX(clname,'@expname@') + INDEX(clname,'@EXPNAME@')
1746            DO WHILE ( idx /= 0 ) 
1747               clname = clname(1:idx-1)//TRIM(cexper)//clname(idx+9:LEN_TRIM(clname))
1748               idx = INDEX(clname,'@expname@') + INDEX(clname,'@EXPNAME@')
1749            END DO
1750
1751            idx = INDEX(clname,'@freq@') + INDEX(clname,'@FREQ@')
1752            DO WHILE ( idx /= 0 ) 
1753              IF ( output_freq%timestep /= 0) THEN
1754                  WRITE(clfreq,'(I18,A2)')INT(output_freq%timestep),'ts' 
1755                  itrlen = LEN_TRIM(ADJUSTL(clfreq))
1756              ELSE IF ( output_freq%second /= 0 ) THEN
1757                  WRITE(clfreq,'(I19,A1)')INT(output_freq%second),'s' 
1758                  itrlen = LEN_TRIM(ADJUSTL(clfreq))
1759              ELSE IF ( output_freq%minute /= 0 ) THEN
1760                  WRITE(clfreq,'(I18,A2)')INT(output_freq%minute),'mi' 
1761                  itrlen = LEN_TRIM(ADJUSTL(clfreq))
1762              ELSE IF ( output_freq%hour /= 0 ) THEN
1763                  WRITE(clfreq,'(I19,A1)')INT(output_freq%hour),'h' 
1764                  itrlen = LEN_TRIM(ADJUSTL(clfreq))
1765              ELSE IF ( output_freq%day /= 0 ) THEN
1766                  WRITE(clfreq,'(I19,A1)')INT(output_freq%day),'d' 
1767                  itrlen = LEN_TRIM(ADJUSTL(clfreq))
1768              ELSE IF ( output_freq%month /= 0 ) THEN   
1769                  WRITE(clfreq,'(I19,A1)')INT(output_freq%month),'m' 
1770                  itrlen = LEN_TRIM(ADJUSTL(clfreq))
1771              ELSE IF ( output_freq%year /= 0 ) THEN   
1772                  WRITE(clfreq,'(I19,A1)')INT(output_freq%year),'y' 
1773                  itrlen = LEN_TRIM(ADJUSTL(clfreq))
1774              ELSE
1775                  CALL ctl_stop('error in the name of file id '//TRIM(cdid),   &
1776                     & ' attribute output_freq is undefined -> cannot replace @freq@ in '//TRIM(clname) )
1777              ENDIF
1778              clname = clname(1:idx-1)//TRIM(ADJUSTL(clfreq))//clname(idx+6:LEN_TRIM(clname))
1779              idx = INDEX(clname,'@freq@') + INDEX(clname,'@FREQ@')
1780            END DO
1781
1782            idx = INDEX(clname,'@startdate@') + INDEX(clname,'@STARTDATE@')
1783            DO WHILE ( idx /= 0 ) 
1784               cldate = iom_sdate( fjulday - rdt / rday )
1785               clname = clname(1:idx-1)//TRIM(cldate)//clname(idx+11:LEN_TRIM(clname))
1786               idx = INDEX(clname,'@startdate@') + INDEX(clname,'@STARTDATE@')
1787            END DO
1788
1789            idx = INDEX(clname,'@startdatefull@') + INDEX(clname,'@STARTDATEFULL@')
1790            DO WHILE ( idx /= 0 ) 
1791               cldate = iom_sdate( fjulday - rdt / rday, ldfull = .TRUE. )
1792               clname = clname(1:idx-1)//TRIM(cldate)//clname(idx+15:LEN_TRIM(clname))
1793               idx = INDEX(clname,'@startdatefull@') + INDEX(clname,'@STARTDATEFULL@')
1794            END DO
1795
1796            idx = INDEX(clname,'@enddate@') + INDEX(clname,'@ENDDATE@')
1797            DO WHILE ( idx /= 0 ) 
1798               cldate = iom_sdate( fjulday + rdt / rday * REAL( nitend - nit000, wp ), ld24 = .TRUE. )
1799               clname = clname(1:idx-1)//TRIM(cldate)//clname(idx+9:LEN_TRIM(clname))
1800               idx = INDEX(clname,'@enddate@') + INDEX(clname,'@ENDDATE@')
1801            END DO
1802
1803            idx = INDEX(clname,'@enddatefull@') + INDEX(clname,'@ENDDATEFULL@')
1804            DO WHILE ( idx /= 0 ) 
1805               cldate = iom_sdate( fjulday + rdt / rday * REAL( nitend - nit000, wp ), ld24 = .TRUE., ldfull = .TRUE. )
1806               clname = clname(1:idx-1)//TRIM(cldate)//clname(idx+13:LEN_TRIM(clname))
1807               idx = INDEX(clname,'@enddatefull@') + INDEX(clname,'@ENDDATEFULL@')
1808            END DO
1809
1810            IF( jn == 1 .AND. TRIM(Agrif_CFixed()) /= '0' )   clname = TRIM(Agrif_CFixed())//"_"//TRIM(clname)
1811            IF( jn == 1 )   CALL iom_set_file_attr( cdid, name        = clname )
1812            IF( jn == 2 )   CALL iom_set_file_attr( cdid, name_suffix = clname )
1813
1814         ENDIF
1815
1816      END DO
1817
1818   END SUBROUTINE iom_update_file_name
1819
1820
1821   FUNCTION iom_sdate( pjday, ld24, ldfull )
1822      !!----------------------------------------------------------------------
1823      !!                     ***  ROUTINE iom_sdate  ***
1824      !!
1825      !! ** Purpose :   send back the date corresponding to the given julian day
1826      !!
1827      !!----------------------------------------------------------------------
1828      REAL(wp), INTENT(in   )           ::   pjday         ! julian day
1829      LOGICAL , INTENT(in   ), OPTIONAL ::   ld24          ! true to force 24:00 instead of 00:00
1830      LOGICAL , INTENT(in   ), OPTIONAL ::   ldfull        ! true to get the compleate date: yyyymmdd_hh:mm:ss
1831      !
1832      CHARACTER(LEN=20) ::   iom_sdate
1833      CHARACTER(LEN=50) ::   clfmt                         !  format used to write the date
1834      INTEGER           ::   iyear, imonth, iday, ihour, iminute, isec
1835      REAL(wp)          ::   zsec
1836      LOGICAL           ::   ll24, llfull
1837      !
1838      IF( PRESENT(ld24) ) THEN   ;   ll24 = ld24
1839      ELSE                       ;   ll24 = .FALSE.
1840      ENDIF
1841
1842      IF( PRESENT(ldfull) ) THEN   ;   llfull = ldfull
1843      ELSE                         ;   llfull = .FALSE.
1844      ENDIF
1845
1846      CALL ju2ymds( pjday, iyear, imonth, iday, zsec )
1847      isec = NINT(zsec)
1848
1849      IF ( ll24 .AND. isec == 0 ) THEN   ! 00:00 of the next day -> move to 24:00 of the current day
1850         CALL ju2ymds( pjday - 1., iyear, imonth, iday, zsec )
1851         isec = 86400
1852      ENDIF
1853
1854      IF( iyear < 10000 ) THEN   ;   clfmt = "i4.4,2i2.2"                ! format used to write the date
1855      ELSE                       ;   WRITE(clfmt, "('i',i1,',2i2.2')") INT(LOG10(REAL(iyear,wp))) + 1
1856      ENDIF
1857     
1858!$AGRIF_DO_NOT_TREAT     
1859! Should be fixed in the conv
1860      IF( llfull ) THEN
1861         clfmt = TRIM(clfmt)//",'_',i2.2,':',i2.2,':',i2.2"
1862         ihour   = isec / 3600
1863         isec    = MOD(isec, 3600)
1864         iminute = isec / 60
1865         isec    = MOD(isec, 60)
1866         WRITE(iom_sdate, '('//TRIM(clfmt)//')') iyear, imonth, iday, ihour, iminute, isec    ! date of the end of run
1867      ELSE
1868         WRITE(iom_sdate, '('//TRIM(clfmt)//')') iyear, imonth, iday                          ! date of the end of run
1869      ENDIF
1870!$AGRIF_END_DO_NOT_TREAT     
1871
1872   END FUNCTION iom_sdate
1873
1874#else
1875
1876
1877   SUBROUTINE iom_setkt( kt, cdname )
1878      INTEGER         , INTENT(in)::   kt 
1879      CHARACTER(LEN=*), INTENT(in) ::   cdname
1880      IF( .FALSE. )   WRITE(numout,*) kt, cdname   ! useless test to avoid compilation warnings
1881   END SUBROUTINE iom_setkt
1882
1883   SUBROUTINE iom_context_finalize( cdname )
1884      CHARACTER(LEN=*), INTENT(in) ::   cdname
1885      IF( .FALSE. )   WRITE(numout,*)  cdname   ! useless test to avoid compilation warnings
1886   END SUBROUTINE iom_context_finalize
1887
1888#endif
1889
1890   LOGICAL FUNCTION iom_use( cdname )
1891      CHARACTER(LEN=*), INTENT(in) ::   cdname
1892#if defined key_iomput
1893      iom_use = xios_field_is_active( cdname )
1894#else
1895      iom_use = .FALSE.
1896#endif
1897   END FUNCTION iom_use
1898   
1899   !!======================================================================
1900END MODULE iom
Note: See TracBrowser for help on using the repository browser.