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

source: branches/2015/dev_r5003_MERCATOR6_CRS/NEMOGCM/NEMO/OPA_SRC/IOM/iom.F90 @ 5105

Last change on this file since 5105 was 5105, checked in by cbricaud, 9 years ago

bug correction

  • Property svn:keywords set to Id
File size: 78.7 KB
Line 
1MODULE iom
2   !!=====================================================================
3   !!                    ***  MODULE  iom ***
4   !! Input/Output manager :  Library to read input files
5   !!====================================================================
6   !! History :  2.0  ! 2005-12  (J. Belier) Original code
7   !!            2.0  ! 2006-02  (S. Masson) Adaptation to NEMO
8   !!            3.0  ! 2007-07  (D. Storkey) Changes to iom_gettime
9   !!            3.4  ! 2012-12  (R. Bourdalle-Badie and G. Reffray)  add C1D case 
10   !!--------------------------------------------------------------------
11
12   !!--------------------------------------------------------------------
13   !!   iom_open       : open a file read only
14   !!   iom_close      : close a file or all files opened by iom
15   !!   iom_get        : read a field (interfaced to several routines)
16   !!   iom_gettime    : read the time axis cdvar in the file
17   !!   iom_varid      : get the id of a variable in a file
18   !!   iom_rstput     : write a field in a restart file (interfaced to several routines)
19   !!--------------------------------------------------------------------
20   USE dom_oce, ONLY : nimpp, njmpp, nlci, nlcj, nldi, nldj, nlei, nlej, & 
21                       mig, mjg, narea, &
22                       gphiv, gphif, &
23                       agrif_root, agrif_cfixed, lk_agrif, &
24                       rdt,rdttra, gdept_0, ln_crs, gdepw_0,  adatrj, fjulday
25
26   USE c1d             ! 1D vertical configuration
27   USE flo_oce         ! floats module declarations
28   USE lbclnk          ! lateal boundary condition / mpp exchanges
29   USE iom_def         ! iom variables definitions
30   USE iom_ioipsl      ! NetCDF format with IOIPSL library
31   USE iom_nf90        ! NetCDF format with native NetCDF library
32   USE iom_rstdimg     ! restarts access direct format "dimg" style...
33   USE in_out_manager  ! I/O manager
34   USE lib_mpp           ! MPP library
35#if defined key_iomput
36   USE sbc_oce, ONLY :   nn_fsbc         ! ocean space and time domain
37   USE trc_oce, ONLY :   nn_dttrc        !  !: frequency of step on passive tracers
38   USE icb_oce, ONLY :   nclasses, class_num       !  !: iceberg classes
39#if defined key_lim3
40   USE par_ice
41#elif defined key_lim2
42   USE par_ice_2
43#endif
44   USE domngb          ! ocean space and time domain
45   USE phycst          ! physical constants
46   USE dianam          ! build name of file
47   USE xios
48# endif
49   USE ioipsl, ONLY :  ju2ymds    ! for calendar
50   USE crs             ! Grid coarsening
51
52   IMPLICIT NONE
53   PUBLIC   !   must be public to be able to access iom_def through iom
54   
55#if defined key_iomput
56   LOGICAL, PUBLIC, PARAMETER ::   lk_iomput = .TRUE.        !: iom_put flag
57#else
58   LOGICAL, PUBLIC, PARAMETER ::   lk_iomput = .FALSE.       !: iom_put flag
59#endif
60   PUBLIC iom_init, iom_swap, iom_open, iom_close, iom_setkt, iom_varid, iom_get, iom_gettime, iom_rstput, iom_put
61   PUBLIC iom_getatt, iom_use, iom_context_finalize
62
63   PRIVATE iom_rp0d, iom_rp1d, iom_rp2d, iom_rp3d
64   PRIVATE iom_g0d, iom_g1d, iom_g2d, iom_g3d, iom_get_123d
65   PRIVATE iom_p1d, iom_p2d, iom_p3d
66#if defined key_iomput
67   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
68   PRIVATE set_grid, set_scalar, set_xmlatt, set_mooring, iom_update_file_name, iom_sdate
69# endif
70
71   INTERFACE iom_get
72      MODULE PROCEDURE iom_g0d, iom_g1d, iom_g2d, iom_g3d
73   END INTERFACE
74   INTERFACE iom_getatt
75      MODULE PROCEDURE iom_g0d_intatt
76   END INTERFACE
77   INTERFACE iom_rstput
78      MODULE PROCEDURE iom_rp0d, iom_rp1d, iom_rp2d, iom_rp3d
79   END INTERFACE
80  INTERFACE iom_put
81     MODULE PROCEDURE iom_p0d, iom_p1d, iom_p2d, iom_p3d
82  END INTERFACE
83
84   !!----------------------------------------------------------------------
85   !! NEMO/OPA 3.3 , NEMO Consortium (2010)
86   !! $Id$
87   !! Software governed by the CeCILL licence (NEMOGCM/NEMO_CeCILL.txt)
88   !!----------------------------------------------------------------------
89
90CONTAINS
91
92   SUBROUTINE iom_init( cdname ) 
93      !!----------------------------------------------------------------------
94      !!                     ***  ROUTINE   ***
95      !!
96      !! ** Purpose :   
97      !!
98      !!----------------------------------------------------------------------
99      CHARACTER(len=*), INTENT(in)  :: cdname
100#if defined key_iomput
101      TYPE(xios_time)   :: dtime    = xios_time(0, 0, 0, 0, 0, 0)
102      CHARACTER(len=19) :: cldate 
103      CHARACTER(len=10) :: clname
104      INTEGER           ::   ji
105      !!----------------------------------------------------------------------
106
107      clname = cdname
108      IF( TRIM(Agrif_CFixed()) /= '0' )   clname = TRIM(Agrif_CFixed())//"_"//TRIM(cdname)
109# if defined key_mpp_mpi
110      CALL xios_context_initialize(TRIM(clname), mpi_comm_opa)
111# else
112      CALL xios_context_initialize(TRIM(clname), 0)
113# endif
114      CALL iom_swap( cdname )
115
116      ! calendar parameters
117      SELECT CASE ( nleapy )        ! Choose calendar for IOIPSL
118      CASE ( 1)   ;   CALL xios_set_context_attr(TRIM(clname), calendar_type= "Gregorian")
119      CASE ( 0)   ;   CALL xios_set_context_attr(TRIM(clname), calendar_type= "NoLeap")
120      CASE (30)   ;   CALL xios_set_context_attr(TRIM(clname), calendar_type= "D360")
121      END SELECT
122      WRITE(cldate,"(i4.4,'-',i2.2,'-',i2.2,' 00:00:00')") nyear,nmonth,nday 
123      CALL xios_set_context_attr(TRIM(clname), start_date=cldate )
124
125      ! horizontal grid definition
126      CALL set_scalar
127
128      IF( TRIM(cdname) == "nemo" ) THEN 
129         CALL set_grid( "T", glamt, gphit ) 
130         CALL set_grid( "U", glamu, gphiu )
131         CALL set_grid( "V", glamv, gphiv )
132         CALL set_grid( "W", glamt, gphit )
133      ENDIF
134
135      IF( TRIM(cdname) == "nemo_crs" ) THEN 
136         CALL dom_grid_crs   ! Save the parent grid information  & Switch to coarse grid domain
137         !
138         CALL set_grid( "T", glamt_crs, gphit_crs ) 
139         CALL set_grid( "U", glamu_crs, gphiu_crs ) 
140         CALL set_grid( "V", glamv_crs, gphiv_crs ) 
141         CALL set_grid( "W", glamt_crs, gphit_crs ) 
142          !
143         CALL dom_grid_glo   ! Return to parent grid domain
144      ENDIF
145
146
147      ! vertical grid definition
148      CALL iom_set_axis_attr( "deptht", gdept_1d )
149      CALL iom_set_axis_attr( "depthu", gdept_1d )
150      CALL iom_set_axis_attr( "depthv", gdept_1d )
151      CALL iom_set_axis_attr( "depthw", gdepw_1d )
152# if defined key_floats
153      CALL iom_set_axis_attr( "nfloat", (/ (REAL(ji,wp), ji=1,nfloat) /) )
154# endif
155#if defined key_lim3 || defined key_lim2
156      CALL iom_set_axis_attr( "ncatice", (/ (REAL(ji,wp), ji=1,jpl) /) )
157#endif
158      CALL iom_set_axis_attr( "icbcla", class_num )
159     
160      ! automatic definitions of some of the xml attributs
161      CALL set_xmlatt
162
163      ! end file definition
164      dtime%second = rdt
165      CALL xios_set_timestep(dtime)
166      CALL xios_close_context_definition()
167     
168      CALL xios_update_calendar(0)
169#endif
170     
171   END SUBROUTINE iom_init
172
173
174   SUBROUTINE iom_swap( cdname )
175      !!---------------------------------------------------------------------
176      !!                   ***  SUBROUTINE  iom_swap  ***
177      !!
178      !! ** Purpose :  swap context between different agrif grid for xmlio_server
179      !!---------------------------------------------------------------------
180      CHARACTER(len=*), INTENT(in) :: cdname
181#if defined key_iomput
182      TYPE(xios_context) :: nemo_hdl
183
184      IF( TRIM(Agrif_CFixed()) == '0' ) THEN
185        CALL xios_get_handle(TRIM(cdname),nemo_hdl)
186      ELSE
187        CALL xios_get_handle(TRIM(Agrif_CFixed())//"_"//TRIM(cdname),nemo_hdl)
188      ENDIF
189      !
190      CALL xios_set_current_context(nemo_hdl)
191#endif
192      !
193   END SUBROUTINE iom_swap
194
195
196   SUBROUTINE iom_open( cdname, kiomid, ldwrt, kdom, kiolib, ldstop, ldiof )
197      !!---------------------------------------------------------------------
198      !!                   ***  SUBROUTINE  iom_open  ***
199      !!
200      !! ** Purpose :  open an input file (return 0 if not found)
201      !!---------------------------------------------------------------------
202      CHARACTER(len=*), INTENT(in   )           ::   cdname   ! File name
203      INTEGER         , INTENT(  out)           ::   kiomid   ! iom identifier of the opened file
204      LOGICAL         , INTENT(in   ), OPTIONAL ::   ldwrt    ! open in write modeb          (default = .FALSE.)
205      INTEGER         , INTENT(in   ), OPTIONAL ::   kdom     ! Type of domain to be written (default = jpdom_local_noovlap)
206      INTEGER         , INTENT(in   ), OPTIONAL ::   kiolib   ! library used to open the file (default = jpnf90)
207      LOGICAL         , INTENT(in   ), OPTIONAL ::   ldstop   ! stop if open to read a non-existing file (default = .TRUE.)
208      LOGICAL         , INTENT(in   ), OPTIONAL ::   ldiof    ! Interp On the Fly, needed for AGRIF (default = .FALSE.)
209
210      CHARACTER(LEN=256)    ::   clname    ! the name of the file based on cdname [[+clcpu]+clcpu]
211      CHARACTER(LEN=256)    ::   cltmpn    ! tempory name to store clname (in writting mode)
212      CHARACTER(LEN=10)     ::   clsuffix  ! ".nc" or ".dimg"
213      CHARACTER(LEN=15)     ::   clcpu     ! the cpu number (max jpmax_digits digits)
214      CHARACTER(LEN=256)    ::   clinfo    ! info character
215      LOGICAL               ::   llok      ! check the existence
216      LOGICAL               ::   llwrt     ! local definition of ldwrt
217      LOGICAL               ::   llnoov    ! local definition to read overlap
218      LOGICAL               ::   llstop    ! local definition of ldstop
219      LOGICAL               ::   lliof     ! local definition of ldiof
220      INTEGER               ::   iolib     ! library do we use to open the file
221      INTEGER               ::   icnt      ! counter for digits in clcpu (max = jpmax_digits)
222      INTEGER               ::   iln, ils  ! lengths of character
223      INTEGER               ::   idom      ! type of domain
224      INTEGER               ::   istop     !
225      INTEGER, DIMENSION(2,5) ::   idompar ! domain parameters:
226      ! local number of points for x,y dimensions
227      ! position of first local point for x,y dimensions
228      ! position of last local point for x,y dimensions
229      ! start halo size for x,y dimensions
230      ! end halo size for x,y dimensions
231      !---------------------------------------------------------------------
232      ! Initializations and control
233      ! =============
234      kiomid = -1
235      clinfo = '                    iom_open ~~~  '
236      istop = nstop
237      ! if iom_open is called for the first time: initialize iom_file(:)%nfid to 0
238      ! (could be done when defining iom_file in f95 but not in f90)
239      IF( Agrif_Root() ) THEN
240         IF( iom_open_init == 0 ) THEN
241            iom_file(:)%nfid = 0
242            iom_open_init = 1
243         ENDIF
244      ENDIF
245      ! do we read or write the file?
246      IF( PRESENT(ldwrt) ) THEN   ;   llwrt = ldwrt
247      ELSE                        ;   llwrt = .FALSE.
248      ENDIF
249      ! do we call ctl_stop if we try to open a non-existing file in read mode?
250      IF( PRESENT(ldstop) ) THEN   ;   llstop = ldstop
251      ELSE                         ;   llstop = .TRUE.
252      ENDIF
253      ! what library do we use to open the file?
254      IF( PRESENT(kiolib) ) THEN   ;   iolib = kiolib
255      ELSE                         ;   iolib = jpnf90
256      ENDIF
257      ! are we using interpolation on the fly?
258      IF( PRESENT(ldiof) ) THEN   ;   lliof = ldiof
259      ELSE                        ;   lliof = .FALSE.
260      ENDIF
261      ! do we read the overlap
262      ! ugly patch SM+JMM+RB to overwrite global definition in some cases
263      llnoov = (jpni * jpnj ) == jpnij .AND. .NOT. lk_agrif 
264      ! create the file name by added, if needed, TRIM(Agrif_CFixed()) and TRIM(clsuffix)
265      ! =============
266      clname   = trim(cdname)
267      IF ( .NOT. Agrif_Root() .AND. .NOT. lliof ) THEN
268         iln    = INDEX(clname,'/') 
269         cltmpn = clname(1:iln)
270         clname = clname(iln+1:LEN_TRIM(clname))
271         clname=TRIM(cltmpn)//TRIM(Agrif_CFixed())//'_'//TRIM(clname)
272      ENDIF
273      ! which suffix should we use?
274      SELECT CASE (iolib)
275      CASE (jpioipsl ) ;   clsuffix = '.nc'
276      CASE (jpnf90   ) ;   clsuffix = '.nc'
277      CASE (jprstdimg) ;   clsuffix = '.dimg'
278      CASE DEFAULT     ;   clsuffix = ''
279         CALL ctl_stop( TRIM(clinfo), 'accepted IO library are only jpioipsl, jpnf90 and jprstdimg' )
280      END SELECT
281      ! Add the suffix if needed
282      iln = LEN_TRIM(clname)
283      ils = LEN_TRIM(clsuffix)
284      IF( iln <= ils .OR. INDEX( TRIM(clname), TRIM(clsuffix), back = .TRUE. ) /= iln - ils + 1 )   &
285         &   clname = TRIM(clname)//TRIM(clsuffix)
286      cltmpn = clname   ! store this name
287      ! try to find if the file to be opened already exist
288      ! =============
289      INQUIRE( FILE = clname, EXIST = llok )
290      IF( .NOT.llok ) THEN
291         ! we try to add the cpu number to the name
292         IF( iolib == jprstdimg ) THEN   ;   WRITE(clcpu,*) narea
293         ELSE                            ;   WRITE(clcpu,*) narea-1
294         ENDIF
295         clcpu  = TRIM(ADJUSTL(clcpu))
296         iln = INDEX(clname,TRIM(clsuffix), back = .TRUE.)
297         clname = clname(1:iln-1)//'_'//TRIM(clcpu)//TRIM(clsuffix)
298         icnt = 0
299         INQUIRE( FILE = clname, EXIST = llok ) 
300         ! we try different formats for the cpu number by adding 0
301         DO WHILE( .NOT.llok .AND. icnt < jpmax_digits )
302            clcpu  = "0"//trim(clcpu)
303            clname = clname(1:iln-1)//'_'//TRIM(clcpu)//TRIM(clsuffix)
304            INQUIRE( FILE = clname, EXIST = llok )
305            icnt = icnt + 1
306         END DO
307      ENDIF
308      IF( llwrt ) THEN
309         ! check the domain definition
310! JMM + SM: ugly patch before getting the new version of lib_mpp)
311!         idom = jpdom_local_noovlap   ! default definition
312         IF( llnoov ) THEN   ;   idom = jpdom_local_noovlap   ! default definition
313         ELSE                ;   idom = jpdom_local_full      ! default definition
314         ENDIF
315         IF( PRESENT(kdom) )   idom = kdom
316         ! create the domain informations
317         ! =============
318         SELECT CASE (idom)
319         CASE (jpdom_local_full)
320            idompar(:,1) = (/ jpi             , jpj              /)
321            idompar(:,2) = (/ nimpp           , njmpp            /)
322            idompar(:,3) = (/ nimpp + jpi - 1 , njmpp + jpj - 1  /)
323            idompar(:,4) = (/ nldi - 1        , nldj - 1         /)
324            idompar(:,5) = (/ jpi - nlei      , jpj - nlej       /)
325         CASE (jpdom_local_noextra)
326            idompar(:,1) = (/ nlci            , nlcj             /)
327            idompar(:,2) = (/ nimpp           , njmpp            /)
328            idompar(:,3) = (/ nimpp + nlci - 1, njmpp + nlcj - 1 /)
329            idompar(:,4) = (/ nldi - 1        , nldj - 1         /)
330            idompar(:,5) = (/ nlci - nlei     , nlcj - nlej      /)
331         CASE (jpdom_local_noovlap)
332            idompar(:,1) = (/ nlei  - nldi + 1, nlej  - nldj + 1 /)
333            idompar(:,2) = (/ nimpp + nldi - 1, njmpp + nldj - 1 /)
334            idompar(:,3) = (/ nimpp + nlei - 1, njmpp + nlej - 1 /)
335            idompar(:,4) = (/ 0               , 0                /)
336            idompar(:,5) = (/ 0               , 0                /)
337         CASE DEFAULT
338            CALL ctl_stop( TRIM(clinfo), 'wrong value of kdom, only jpdom_local* cases are accepted' )
339         END SELECT
340      ENDIF
341      ! Open the NetCDF or RSTDIMG file
342      ! =============
343      ! do we have some free file identifier?
344      IF( MINVAL(iom_file(:)%nfid) /= 0 )   &
345         &   CALL ctl_stop( TRIM(clinfo), 'No more free file identifier', 'increase jpmax_files in iom_def' )
346      ! if no file was found...
347      IF( .NOT. llok ) THEN
348         IF( .NOT. llwrt ) THEN   ! we are in read mode
349            IF( llstop ) THEN   ;   CALL ctl_stop( TRIM(clinfo), 'File '//TRIM(cltmpn)//'* not found' )
350            ELSE                ;   istop = nstop + 1   ! make sure that istop /= nstop so we don't open the file
351            ENDIF
352         ELSE                     ! we are in write mode so we
353            clname = cltmpn       ! get back the file name without the cpu number
354         ENDIF
355      ELSE
356         IF( llwrt .AND. .NOT. ln_clobber ) THEN   ! we stop as we want to write in a new file
357            CALL ctl_stop( TRIM(clinfo), 'We want to write in a new file but '//TRIM(clname)//' already exists...' )
358            istop = nstop + 1                      ! make sure that istop /= nstop so we don't open the file
359         ELSEIF( llwrt ) THEN     ! the file exists and we are in write mode with permission to
360            clname = cltmpn       ! overwrite so get back the file name without the cpu number
361         ENDIF
362      ENDIF
363      IF( istop == nstop ) THEN   ! no error within this routine
364         SELECT CASE (iolib)
365         CASE (jpioipsl )   ;   CALL iom_ioipsl_open(  clname, kiomid, llwrt, llok, idompar )
366         CASE (jpnf90   )   ;   CALL iom_nf90_open(    clname, kiomid, llwrt, llok, idompar )
367         CASE (jprstdimg)   ;   CALL iom_rstdimg_open( clname, kiomid, llwrt, llok, idompar )
368         CASE DEFAULT
369            CALL ctl_stop( TRIM(clinfo)//' accepted IO library are only jpioipsl, jpnf90 and jprstdimg' )
370         END SELECT
371      ENDIF
372      !
373   END SUBROUTINE iom_open
374
375
376   SUBROUTINE iom_close( kiomid )
377      !!--------------------------------------------------------------------
378      !!                   ***  SUBROUTINE  iom_close  ***
379      !!
380      !! ** Purpose : close an input file, or all files opened by iom
381      !!--------------------------------------------------------------------
382      INTEGER, INTENT(inout), OPTIONAL ::   kiomid   ! iom identifier of the file to be closed
383      !                                              ! return 0 when file is properly closed
384      !                                              ! No argument: all files opened by iom are closed
385
386      INTEGER ::   jf         ! dummy loop indices
387      INTEGER ::   i_s, i_e   ! temporary integer
388      CHARACTER(LEN=100)    ::   clinfo    ! info character
389      !---------------------------------------------------------------------
390      !
391      clinfo = '                    iom_close ~~~  '
392      IF( PRESENT(kiomid) ) THEN
393         i_s = kiomid
394         i_e = kiomid
395      ELSE
396         i_s = 1
397         i_e = jpmax_files
398      ENDIF
399
400      IF( i_s > 0 ) THEN
401         DO jf = i_s, i_e
402            IF( iom_file(jf)%nfid > 0 ) THEN
403               SELECT CASE (iom_file(jf)%iolib)
404               CASE (jpioipsl )   ;   CALL iom_ioipsl_close(  jf )
405               CASE (jpnf90   )   ;   CALL iom_nf90_close(    jf )
406               CASE (jprstdimg)   ;   CALL iom_rstdimg_close( jf )
407               CASE DEFAULT
408                  CALL ctl_stop( TRIM(clinfo)//' accepted IO library are only jpioipsl, jpnf90 and jprstdimg' )
409               END SELECT
410               iom_file(jf)%nfid       = 0          ! free the id
411               IF( PRESENT(kiomid) )   kiomid = 0   ! return 0 as id to specify that the file was closed
412               IF(lwp) WRITE(numout,*) TRIM(clinfo)//' close file: '//TRIM(iom_file(jf)%name)//' ok'
413            ELSEIF( PRESENT(kiomid) ) THEN
414               WRITE(ctmp1,*) '--->',  kiomid
415               CALL ctl_stop( TRIM(clinfo)//' Invalid file identifier', ctmp1 )
416            ENDIF
417         END DO
418      ENDIF
419      !   
420   END SUBROUTINE iom_close
421
422
423   FUNCTION iom_varid ( kiomid, cdvar, kdimsz, kndims, ldstop ) 
424      !!-----------------------------------------------------------------------
425      !!                  ***  FUNCTION  iom_varid  ***
426      !!
427      !! ** Purpose : get the id of a variable in a file (return 0 if not found)
428      !!-----------------------------------------------------------------------
429      INTEGER              , INTENT(in   )           ::   kiomid   ! file Identifier
430      CHARACTER(len=*)     , INTENT(in   )           ::   cdvar    ! name of the variable
431      INTEGER, DIMENSION(:), INTENT(  out), OPTIONAL ::   kdimsz   ! size of the dimensions
432      INTEGER,               INTENT(  out), OPTIONAL ::   kndims   ! size of the dimensions
433      LOGICAL              , INTENT(in   ), OPTIONAL ::   ldstop   ! stop if looking for non-existing variable (default = .TRUE.)
434      !
435      INTEGER                        ::   iom_varid, iiv, i_nvd
436      LOGICAL                        ::   ll_fnd
437      CHARACTER(LEN=100)             ::   clinfo                   ! info character
438      LOGICAL                        ::   llstop                   ! local definition of ldstop
439      !!-----------------------------------------------------------------------
440      iom_varid = 0                         ! default definition
441      ! do we call ctl_stop if we look for non-existing variable?
442      IF( PRESENT(ldstop) ) THEN   ;   llstop = ldstop
443      ELSE                         ;   llstop = .TRUE.
444      ENDIF
445      !
446      IF( kiomid > 0 ) THEN
447         clinfo = 'iom_varid, file: '//trim(iom_file(kiomid)%name)//', var: '//trim(cdvar)
448         IF( iom_file(kiomid)%nfid == 0 ) THEN
449            CALL ctl_stop( trim(clinfo), 'the file is not open' )
450         ELSE
451            ll_fnd  = .FALSE.
452            iiv = 0
453            !
454            DO WHILE ( .NOT.ll_fnd .AND. iiv < iom_file(kiomid)%nvars )
455               iiv = iiv + 1
456               ll_fnd  = ( TRIM(cdvar) == TRIM(iom_file(kiomid)%cn_var(iiv)) )
457            END DO
458            !
459            IF( .NOT.ll_fnd ) THEN
460               iiv = iiv + 1
461               IF( iiv <= jpmax_vars ) THEN
462                  SELECT CASE (iom_file(kiomid)%iolib)
463                  CASE (jpioipsl )   ;   iom_varid = iom_ioipsl_varid( kiomid, cdvar, iiv, kdimsz )
464                  CASE (jpnf90   )   ;   iom_varid = iom_nf90_varid  ( kiomid, cdvar, iiv, kdimsz, kndims )
465                  CASE (jprstdimg)   ;   iom_varid = -1   ! all variables are listed in iom_file
466                  CASE DEFAULT   
467                     CALL ctl_stop( TRIM(clinfo)//' accepted IO library are only jpioipsl, jpnf90 and jprstdimg' )
468                  END SELECT
469               ELSE
470                  CALL ctl_stop( trim(clinfo), 'Too many variables in the file '//iom_file(kiomid)%name,   &
471                        &                         'increase the parameter jpmax_vars')
472               ENDIF
473               IF( llstop .AND. iom_varid == -1 )   CALL ctl_stop( TRIM(clinfo)//' not found' ) 
474            ELSE
475               iom_varid = iiv
476               IF( PRESENT(kdimsz) ) THEN
477                  i_nvd = iom_file(kiomid)%ndims(iiv)
478                  IF( i_nvd == size(kdimsz) ) THEN
479                     kdimsz(:) = iom_file(kiomid)%dimsz(1:i_nvd,iiv)
480                  ELSE
481                     WRITE(ctmp1,*) i_nvd, size(kdimsz)
482                     CALL ctl_stop( trim(clinfo), 'error in kdimsz size'//trim(ctmp1) )
483                  ENDIF
484               ENDIF
485               IF( PRESENT(kndims) )  kndims = iom_file(kiomid)%ndims(iiv)
486            ENDIF
487         ENDIF
488      ENDIF
489      !
490   END FUNCTION iom_varid
491
492
493   !!----------------------------------------------------------------------
494   !!                   INTERFACE iom_get
495   !!----------------------------------------------------------------------
496   SUBROUTINE iom_g0d( kiomid, cdvar, pvar, ktime )
497      INTEGER         , INTENT(in   )                 ::   kiomid    ! Identifier of the file
498      CHARACTER(len=*), INTENT(in   )                 ::   cdvar     ! Name of the variable
499      REAL(wp)        , INTENT(  out)                 ::   pvar      ! read field
500      INTEGER         , INTENT(in   ),     OPTIONAL   ::   ktime     ! record number
501      !
502      INTEGER                                         ::   idvar     ! variable id
503      INTEGER                                         ::   idmspc    ! number of spatial dimensions
504      INTEGER         , DIMENSION(1)                  ::   itime     ! record number
505      CHARACTER(LEN=100)                              ::   clinfo    ! info character
506      CHARACTER(LEN=100)                              ::   clname    ! file name
507      CHARACTER(LEN=1)                                ::   cldmspc   !
508      !
509      itime = 1
510      IF( PRESENT(ktime) ) itime = ktime
511      !
512      clname = iom_file(kiomid)%name
513      clinfo = '          iom_g0d, file: '//trim(clname)//', var: '//trim(cdvar)
514      !
515      IF( kiomid > 0 ) THEN
516         idvar = iom_varid( kiomid, cdvar )
517         IF( iom_file(kiomid)%nfid > 0 .AND. idvar > 0 ) THEN
518            idmspc = iom_file ( kiomid )%ndims( idvar )
519            IF( iom_file(kiomid)%luld(idvar) )  idmspc = idmspc - 1
520            WRITE(cldmspc , fmt='(i1)') idmspc
521            IF( idmspc > 0 )  CALL ctl_stop( TRIM(clinfo), 'When reading to a 0D array, we do not accept data', &
522                                 &                         'with 1 or more spatial dimensions: '//cldmspc//' were found.' , &
523                                 &                         'Use ncwa -a to suppress the unnecessary dimensions' )
524            SELECT CASE (iom_file(kiomid)%iolib)
525            CASE (jpioipsl )   ;   CALL iom_ioipsl_get(  kiomid, idvar, pvar, itime )
526            CASE (jpnf90   )   ;   CALL iom_nf90_get(    kiomid, idvar, pvar, itime )
527            CASE (jprstdimg)   ;   CALL iom_rstdimg_get( kiomid, idvar, pvar )
528            CASE DEFAULT   
529               CALL ctl_stop( 'iom_g0d: accepted IO library are only jpioipsl, jpnf90 and jprstdimg' )
530            END SELECT
531         ENDIF
532      ENDIF
533   END SUBROUTINE iom_g0d
534
535   SUBROUTINE iom_g1d( kiomid, kdom, cdvar, pvar, ktime, kstart, kcount )
536      INTEGER         , INTENT(in   )                         ::   kiomid    ! Identifier of the file
537      INTEGER         , INTENT(in   )                         ::   kdom      ! Type of domain to be read
538      CHARACTER(len=*), INTENT(in   )                         ::   cdvar     ! Name of the variable
539      REAL(wp)        , INTENT(  out), DIMENSION(:)           ::   pvar      ! read field
540      INTEGER         , INTENT(in   )              , OPTIONAL ::   ktime     ! record number
541      INTEGER         , INTENT(in   ), DIMENSION(1), OPTIONAL ::   kstart    ! start axis position of the reading
542      INTEGER         , INTENT(in   ), DIMENSION(1), OPTIONAL ::   kcount    ! number of points in each axis
543      !
544      IF( kiomid > 0 ) THEN
545         IF( iom_file(kiomid)%nfid > 0 ) CALL iom_get_123d( kiomid, kdom       , cdvar        , pv_r1d=pvar,   &
546              &                                                     ktime=ktime, kstart=kstart, kcount=kcount )
547      ENDIF
548   END SUBROUTINE iom_g1d
549
550   SUBROUTINE iom_g2d( kiomid, kdom, cdvar, pvar, ktime, kstart, kcount )
551      INTEGER         , INTENT(in   )                           ::   kiomid    ! Identifier of the file
552      INTEGER         , INTENT(in   )                           ::   kdom      ! Type of domain to be read
553      CHARACTER(len=*), INTENT(in   )                           ::   cdvar     ! Name of the variable
554      REAL(wp)        , INTENT(  out), DIMENSION(:,:)           ::   pvar      ! read field
555      INTEGER         , INTENT(in   )                , OPTIONAL ::   ktime     ! record number
556      INTEGER         , INTENT(in   ), DIMENSION(2)  , OPTIONAL ::   kstart    ! start axis position of the reading
557      INTEGER         , INTENT(in   ), DIMENSION(2)  , OPTIONAL ::   kcount    ! number of points in each axis
558      !
559      IF( kiomid > 0 ) THEN
560         IF( iom_file(kiomid)%nfid > 0 ) CALL iom_get_123d( kiomid, kdom       , cdvar        , pv_r2d=pvar,   &
561              &                                                     ktime=ktime, kstart=kstart, kcount=kcount )
562      ENDIF
563   END SUBROUTINE iom_g2d
564
565   SUBROUTINE iom_g3d( kiomid, kdom, cdvar, pvar, ktime, kstart, kcount )
566      INTEGER         , INTENT(in   )                             ::   kiomid    ! Identifier of the file
567      INTEGER         , INTENT(in   )                             ::   kdom      ! Type of domain to be read
568      CHARACTER(len=*), INTENT(in   )                             ::   cdvar     ! Name of the variable
569      REAL(wp)        , INTENT(  out), DIMENSION(:,:,:)           ::   pvar      ! read field
570      INTEGER         , INTENT(in   )                  , OPTIONAL ::   ktime     ! record number
571      INTEGER         , INTENT(in   ), DIMENSION(3)    , OPTIONAL ::   kstart    ! start axis position of the reading
572      INTEGER         , INTENT(in   ), DIMENSION(3)    , OPTIONAL ::   kcount    ! number of points in each axis
573      !
574      IF( kiomid > 0 ) THEN
575         IF( iom_file(kiomid)%nfid > 0 ) CALL iom_get_123d( kiomid, kdom       , cdvar        , pv_r3d=pvar,   &
576              &                                                     ktime=ktime, kstart=kstart, kcount=kcount )
577      ENDIF
578   END SUBROUTINE iom_g3d
579   !!----------------------------------------------------------------------
580
581   SUBROUTINE iom_get_123d( kiomid, kdom  , cdvar ,   &
582         &                  pv_r1d, pv_r2d, pv_r3d,   &
583         &                  ktime , kstart, kcount  )
584      !!-----------------------------------------------------------------------
585      !!                  ***  ROUTINE  iom_get_123d  ***
586      !!
587      !! ** Purpose : read a 1D/2D/3D variable
588      !!
589      !! ** Method : read ONE record at each CALL
590      !!-----------------------------------------------------------------------
591      INTEGER                    , INTENT(in   )           ::   kiomid     ! Identifier of the file
592      INTEGER                    , INTENT(in   )           ::   kdom       ! Type of domain to be read
593      CHARACTER(len=*)           , INTENT(in   )           ::   cdvar      ! Name of the variable
594      REAL(wp), DIMENSION(:)     , INTENT(  out), OPTIONAL ::   pv_r1d     ! read field (1D case)
595      REAL(wp), DIMENSION(:,:)   , INTENT(  out), OPTIONAL ::   pv_r2d     ! read field (2D case)
596      REAL(wp), DIMENSION(:,:,:) , INTENT(  out), OPTIONAL ::   pv_r3d     ! read field (3D case)
597      INTEGER                    , INTENT(in   ), OPTIONAL ::   ktime      ! record number
598      INTEGER , DIMENSION(:)     , INTENT(in   ), OPTIONAL ::   kstart     ! start position of the reading in each axis
599      INTEGER , DIMENSION(:)     , INTENT(in   ), OPTIONAL ::   kcount     ! number of points to be read in each axis
600      !
601      LOGICAL                        ::   llnoov      ! local definition to read overlap
602      INTEGER                        ::   jl          ! loop on number of dimension
603      INTEGER                        ::   idom        ! type of domain
604      INTEGER                        ::   idvar       ! id of the variable
605      INTEGER                        ::   inbdim      ! number of dimensions of the variable
606      INTEGER                        ::   idmspc      ! number of spatial dimensions
607      INTEGER                        ::   itime       ! record number
608      INTEGER                        ::   istop       ! temporary value of nstop
609      INTEGER                        ::   ix1, ix2, iy1, iy2   ! subdomain indexes
610      INTEGER                        ::   ji, jj      ! loop counters
611      INTEGER                        ::   irankpv       !
612      INTEGER                        ::   ind1, ind2  ! substring index
613      INTEGER, DIMENSION(jpmax_dims) ::   istart      ! starting point to read for each axis
614      INTEGER, DIMENSION(jpmax_dims) ::   icnt        ! number of value to read along each axis
615      INTEGER, DIMENSION(jpmax_dims) ::   idimsz      ! size of the dimensions of the variable
616      INTEGER, DIMENSION(jpmax_dims) ::   ishape      ! size of the dimensions of the variable
617      REAL(wp)                       ::   zscf, zofs  ! sacle_factor and add_offset
618      INTEGER                        ::   itmp        ! temporary integer
619      CHARACTER(LEN=256)             ::   clinfo      ! info character
620      CHARACTER(LEN=256)             ::   clname      ! file name
621      CHARACTER(LEN=1)               ::   clrankpv, cldmspc      !
622      !---------------------------------------------------------------------
623      !
624      clname = iom_file(kiomid)%name   !   esier to read
625      clinfo = '          iom_get_123d, file: '//trim(clname)//', var: '//trim(cdvar)
626      ! local definition of the domain ?
627      idom = kdom
628      ! do we read the overlap
629      ! ugly patch SM+JMM+RB to overwrite global definition in some cases
630      llnoov = (jpni * jpnj ) == jpnij .AND. .NOT. lk_agrif 
631      ! check kcount and kstart optionals parameters...
632      IF( PRESENT(kcount) .AND. (.NOT. PRESENT(kstart)) ) CALL ctl_stop(trim(clinfo), 'kcount present needs kstart present')
633      IF( PRESENT(kstart) .AND. (.NOT. PRESENT(kcount)) ) CALL ctl_stop(trim(clinfo), 'kstart present needs kcount present')
634      IF( PRESENT(kstart) .AND. idom /= jpdom_unknown   ) CALL ctl_stop(trim(clinfo), 'kstart present needs kdom = jpdom_unknown')
635
636      ! Search for the variable in the data base (eventually actualize data)
637      istop = nstop
638      idvar = iom_varid( kiomid, cdvar )
639      !
640      IF( idvar > 0 ) THEN
641         ! to write iom_file(kiomid)%dimsz in a shorter way !
642         idimsz(:) = iom_file(kiomid)%dimsz(:, idvar) 
643         inbdim = iom_file(kiomid)%ndims(idvar)            ! number of dimensions in the file
644         idmspc = inbdim                                   ! number of spatial dimensions in the file
645         IF( iom_file(kiomid)%luld(idvar) )   idmspc = inbdim - 1
646         IF( idmspc > 3 )   CALL ctl_stop(trim(clinfo), 'the file has more than 3 spatial dimensions this case is not coded...') 
647         !
648         ! update idom definition...
649         ! Identify the domain in case of jpdom_auto(glo/dta) definition
650         IF( idom == jpdom_autoglo .OR. idom == jpdom_autodta ) THEN           
651            IF( idom == jpdom_autoglo ) THEN   ;   idom = jpdom_global 
652            ELSE                               ;   idom = jpdom_data
653            ENDIF
654            ind1 = INDEX( clname, '_', back = .TRUE. ) + 1
655            ind2 = INDEX( clname, '.', back = .TRUE. ) - 1
656            IF( ind2 > ind1 ) THEN   ;   IF( VERIFY( clname(ind1:ind2), '0123456789' ) == 0 )   idom = jpdom_local   ;   ENDIF
657         ENDIF
658         ! Identify the domain in case of jpdom_local definition
659         IF( idom == jpdom_local ) THEN
660            IF(     idimsz(1) == jpi               .AND. idimsz(2) == jpj               ) THEN   ;   idom = jpdom_local_full
661            ELSEIF( idimsz(1) == nlci              .AND. idimsz(2) == nlcj              ) THEN   ;   idom = jpdom_local_noextra
662            ELSEIF( idimsz(1) == (nlei - nldi + 1) .AND. idimsz(2) == (nlej - nldj + 1) ) THEN   ;   idom = jpdom_local_noovlap
663            ELSE   ;   CALL ctl_stop( trim(clinfo), 'impossible to identify the local domain' )
664            ENDIF
665         ENDIF
666         !
667         ! check the consistency between input array and data rank in the file
668         !
669         ! initializations
670         itime = 1
671         IF( PRESENT(ktime) ) itime = ktime
672
673         irankpv = 1 * COUNT( (/PRESENT(pv_r1d)/) ) + 2 * COUNT( (/PRESENT(pv_r2d)/) ) + 3 * COUNT( (/PRESENT(pv_r3d)/) )
674         WRITE(clrankpv, fmt='(i1)') irankpv
675         WRITE(cldmspc , fmt='(i1)') idmspc
676         !
677         IF(     idmspc <  irankpv ) THEN
678            CALL ctl_stop( TRIM(clinfo), 'The file has only '//cldmspc//' spatial dimension',   &
679               &                         'it is impossible to read a '//clrankpv//'D array from this file...' )
680         ELSEIF( idmspc == irankpv ) THEN
681            IF( PRESENT(pv_r1d) .AND. idom /= jpdom_unknown )   &
682               &   CALL ctl_stop( TRIM(clinfo), 'case not coded...You must use jpdom_unknown' )
683         ELSEIF( idmspc >  irankpv ) THEN
684               IF( PRESENT(pv_r2d) .AND. itime == 1 .AND. idimsz(3) == 1 .AND. idmspc == 3 ) THEN
685                  CALL ctl_warn( trim(clinfo), '2D array but 3 spatial dimensions for the data...'              ,   &
686                        &         'As the size of the z dimension is 1 and as we try to read the first record, ',   &
687                        &         'we accept this case, even if there is a possible mix-up between z and time dimension' )   
688                  idmspc = idmspc - 1
689               ELSE
690                  CALL ctl_stop( TRIM(clinfo), 'To keep iom lisibility, when reading a '//clrankpv//'D array,'         ,   &
691                     &                         'we do not accept data with '//cldmspc//' spatial dimensions',   &
692                     &                         'Use ncwa -a to suppress the unnecessary dimensions' )
693               ENDIF
694         ENDIF
695
696         !
697         ! definition of istart and icnt
698         !
699         icnt  (:) = 1
700         istart(:) = 1
701         istart(idmspc+1) = itime
702
703         IF(              PRESENT(kstart)       ) THEN ; istart(1:idmspc) = kstart(1:idmspc) ; icnt(1:idmspc) = kcount(1:idmspc)
704         ELSE
705            IF(           idom == jpdom_unknown ) THEN                                       ; icnt(1:idmspc) = idimsz(1:idmspc)
706            ELSE
707               IF( .NOT. PRESENT(pv_r1d) ) THEN   !   not a 1D array
708                  IF(     idom == jpdom_data    ) THEN ; istart(1:2) = (/ mig(1), mjg(1) /)  ! icnt(1:2) done bellow
709                  ELSEIF( idom == jpdom_global  ) THEN ; istart(1:2) = (/ nimpp , njmpp  /)  ! icnt(1:2) done bellow
710                  ENDIF
711                  ! we do not read the overlap                     -> we start to read at nldi, nldj
712! JMM + SM: ugly patch before getting the new version of lib_mpp)
713!                  IF( idom /= jpdom_local_noovlap )   istart(1:2) = istart(1:2) + (/ nldi - 1, nldj - 1 /)
714                  IF( llnoov .AND. idom /= jpdom_local_noovlap ) istart(1:2) = istart(1:2) + (/ nldi - 1, nldj - 1 /)
715                  ! we do not read the overlap and the extra-halos -> from nldi to nlei and from nldj to nlej
716! JMM + SM: ugly patch before getting the new version of lib_mpp)
717!                  icnt(1:2) = (/ nlei - nldi + 1, nlej - nldj + 1 /)
718                  IF( llnoov ) THEN   ;   icnt(1:2) = (/ nlei - nldi + 1, nlej - nldj + 1 /)
719                  ELSE                ;   icnt(1:2) = (/ nlci           , nlcj            /)
720                  ENDIF
721                  IF( PRESENT(pv_r3d) ) THEN
722                     IF( idom == jpdom_data ) THEN   ; icnt(3) = jpkdta
723                     ELSE                            ; icnt(3) = jpk
724                     ENDIF
725                  ENDIF
726               ENDIF
727            ENDIF
728         ENDIF
729
730         ! check that istart and icnt can be used with this file
731         !-
732         DO jl = 1, jpmax_dims
733            itmp = istart(jl)+icnt(jl)-1
734            IF( itmp > idimsz(jl) .AND. idimsz(jl) /= 0 ) THEN
735               WRITE( ctmp1, FMT="('(istart(', i1, ') + icnt(', i1, ') - 1) = ', i5)" ) jl, jl, itmp
736               WRITE( ctmp2, FMT="(' is larger than idimsz(', i1,') = ', i5)"         ) jl, idimsz(jl)
737               CALL ctl_stop( trim(clinfo), 'start and count too big regarding to the size of the data, ', ctmp1, ctmp2 )     
738            ENDIF
739         END DO
740
741         ! check that icnt matches the input array
742         !-     
743         IF( idom == jpdom_unknown ) THEN
744            IF( irankpv == 1 )        ishape(1:1) = SHAPE(pv_r1d)
745            IF( irankpv == 2 )        ishape(1:2) = SHAPE(pv_r2d)
746            IF( irankpv == 3 )        ishape(1:3) = SHAPE(pv_r3d)
747            ctmp1 = 'd'
748         ELSE
749            IF( irankpv == 2 ) THEN
750! JMM + SM: ugly patch before getting the new version of lib_mpp)
751!               ishape(1:2) = SHAPE(pv_r2d(nldi:nlei,nldj:nlej  ))   ;   ctmp1 = 'd(nldi:nlei,nldj:nlej)'
752               IF( llnoov ) THEN ; ishape(1:2)=SHAPE(pv_r2d(nldi:nlei,nldj:nlej  )) ; ctmp1='d(nldi:nlei,nldj:nlej)'
753               ELSE              ; ishape(1:2)=SHAPE(pv_r2d(1   :nlci,1   :nlcj  )) ; ctmp1='d(1:nlci,1:nlcj)'
754               ENDIF
755            ENDIF
756            IF( irankpv == 3 ) THEN 
757! JMM + SM: ugly patch before getting the new version of lib_mpp)
758!               ishape(1:3) = SHAPE(pv_r3d(nldi:nlei,nldj:nlej,:))   ;   ctmp1 = 'd(nldi:nlei,nldj:nlej,:)'
759               IF( llnoov ) THEN ; ishape(1:3)=SHAPE(pv_r3d(nldi:nlei,nldj:nlej,:)) ; ctmp1='d(nldi:nlei,nldj:nlej,:)'
760               ELSE              ; ishape(1:3)=SHAPE(pv_r3d(1   :nlci,1   :nlcj,:)) ; ctmp1='d(1:nlci,1:nlcj,:)'
761               ENDIF
762            ENDIF
763         ENDIF
764         
765         DO jl = 1, irankpv
766            WRITE( ctmp2, FMT="(', ', i1,'): ', i5,' /= icnt(', i1,'):', i5)" ) jl, ishape(jl), jl, icnt(jl)
767            IF( ishape(jl) /= icnt(jl) )   CALL ctl_stop( TRIM(clinfo), 'size(pv_r'//clrankpv//TRIM(ctmp1)//TRIM(ctmp2) )
768         END DO
769
770      ENDIF
771
772      ! read the data
773      !-     
774      IF( idvar > 0 .AND. istop == nstop ) THEN   ! no additional errors until this point...
775         !
776         ! find the right index of the array to be read
777! JMM + SM: ugly patch before getting the new version of lib_mpp)
778!         IF( idom /= jpdom_unknown ) THEN   ;   ix1 = nldi   ;   ix2 = nlei      ;   iy1 = nldj   ;   iy2 = nlej
779!         ELSE                               ;   ix1 = 1      ;   ix2 = icnt(1)   ;   iy1 = 1      ;   iy2 = icnt(2)
780!         ENDIF
781         IF( llnoov ) THEN
782            IF( idom /= jpdom_unknown ) THEN   ;   ix1 = nldi   ;   ix2 = nlei      ;   iy1 = nldj   ;   iy2 = nlej
783            ELSE                               ;   ix1 = 1      ;   ix2 = icnt(1)   ;   iy1 = 1      ;   iy2 = icnt(2)
784            ENDIF
785         ELSE
786            IF( idom /= jpdom_unknown ) THEN   ;   ix1 = 1      ;   ix2 = nlci      ;   iy1 = 1      ;   iy2 = nlcj
787            ELSE                               ;   ix1 = 1      ;   ix2 = icnt(1)   ;   iy1 = 1      ;   iy2 = icnt(2)
788            ENDIF
789         ENDIF
790     
791         SELECT CASE (iom_file(kiomid)%iolib)
792         CASE (jpioipsl )   ;   CALL iom_ioipsl_get(  kiomid, idvar, inbdim, istart, icnt, ix1, ix2, iy1, iy2,   &
793            &                                         pv_r1d, pv_r2d, pv_r3d )
794         CASE (jpnf90   )   ;   CALL iom_nf90_get(    kiomid, idvar, inbdim, istart, icnt, ix1, ix2, iy1, iy2,   &
795            &                                         pv_r1d, pv_r2d, pv_r3d )
796         CASE (jprstdimg)   ;   CALL iom_rstdimg_get( kiomid, idom, idvar, ix1, ix2, iy1, iy2,   &
797            &                                         pv_r1d, pv_r2d, pv_r3d )
798         CASE DEFAULT   
799            CALL ctl_stop( TRIM(clinfo)//' accepted IO library are only jpioipsl, jpnf90 and jprstdimg' )
800         END SELECT
801
802         IF( istop == nstop ) THEN   ! no additional errors until this point...
803            IF(lwp) WRITE(numout,"(10x,' read ',a,' (rec: ',i6,') in ',a,' ok')") TRIM(cdvar), itime, TRIM(iom_file(kiomid)%name)
804         
805            !--- overlap areas and extra hallows (mpp)
806            IF(     PRESENT(pv_r2d) .AND. idom /= jpdom_unknown ) THEN
807               CALL lbc_lnk( pv_r2d,'Z',-999.,'no0' )
808            ELSEIF( PRESENT(pv_r3d) .AND. idom /= jpdom_unknown ) THEN
809               ! this if could be simplified with the new lbc_lnk that works with any size of the 3rd dimension
810               IF( icnt(3) == jpk ) THEN
811                  CALL lbc_lnk( pv_r3d,'Z',-999.,'no0' )
812               ELSE   ! put some arbitrary value (a call to lbc_lnk will be done later...)
813                  DO jj = nlcj+1, jpj   ;   pv_r3d(1:nlci, jj, :) = pv_r3d(1:nlci, nlej, :)   ;   END DO
814                  DO ji = nlci+1, jpi   ;   pv_r3d(ji    , : , :) = pv_r3d(nlei  , :   , :)   ;   END DO
815               ENDIF
816            ENDIF
817           
818            ! C1D case : always call lbc_lnk to replicate the central value over the whole 3X3 domain
819            IF( lk_c1d .AND. PRESENT(pv_r2d) )   CALL lbc_lnk( pv_r2d,'Z',1. )
820            IF( lk_c1d .AND. PRESENT(pv_r3d) )   CALL lbc_lnk( pv_r3d,'Z',1. )
821   
822            !--- Apply scale_factor and offset
823            zscf = iom_file(kiomid)%scf(idvar)      ! scale factor
824            zofs = iom_file(kiomid)%ofs(idvar)      ! offset
825            IF(     PRESENT(pv_r1d) ) THEN
826               IF( zscf /= 1. )   pv_r1d(:) = pv_r1d(:) * zscf 
827               IF( zofs /= 0. )   pv_r1d(:) = pv_r1d(:) + zofs
828            ELSEIF( PRESENT(pv_r2d) ) THEN
829!CDIR COLLAPSE
830               IF( zscf /= 1.)   pv_r2d(:,:) = pv_r2d(:,:) * zscf
831!CDIR COLLAPSE
832               IF( zofs /= 0.)   pv_r2d(:,:) = pv_r2d(:,:) + zofs
833            ELSEIF( PRESENT(pv_r3d) ) THEN
834!CDIR COLLAPSE
835               IF( zscf /= 1.)   pv_r3d(:,:,:) = pv_r3d(:,:,:) * zscf
836!CDIR COLLAPSE
837               IF( zofs /= 0.)   pv_r3d(:,:,:) = pv_r3d(:,:,:) + zofs
838            ENDIF
839            !
840         ENDIF
841         !
842      ENDIF
843      !
844   END SUBROUTINE iom_get_123d
845
846
847   SUBROUTINE iom_gettime( kiomid, ptime, cdvar, kntime, cdunits, cdcalendar )
848      !!--------------------------------------------------------------------
849      !!                   ***  SUBROUTINE iom_gettime  ***
850      !!
851      !! ** Purpose : read the time axis cdvar in the file
852      !!--------------------------------------------------------------------
853      INTEGER                    , INTENT(in   ) ::   kiomid     ! file Identifier
854      REAL(wp), DIMENSION(:)     , INTENT(  out) ::   ptime      ! the time axis
855      CHARACTER(len=*), OPTIONAL , INTENT(in   ) ::   cdvar      ! time axis name
856      INTEGER         , OPTIONAL , INTENT(  out) ::   kntime     ! number of times in file
857      CHARACTER(len=*), OPTIONAL , INTENT(  out) ::   cdunits    ! units attribute of time coordinate
858      CHARACTER(len=*), OPTIONAL , INTENT(  out) ::   cdcalendar ! calendar attribute of
859      !
860      INTEGER, DIMENSION(1) :: kdimsz
861      INTEGER            ::   idvar    ! id of the variable
862      CHARACTER(LEN=32)  ::   tname    ! local name of time coordinate
863      CHARACTER(LEN=100) ::   clinfo   ! info character
864      !---------------------------------------------------------------------
865      !
866      IF ( PRESENT(cdvar) ) THEN
867         tname = cdvar
868      ELSE
869         tname = iom_file(kiomid)%uldname
870      ENDIF
871      IF( kiomid > 0 ) THEN
872         clinfo = 'iom_gettime, file: '//trim(iom_file(kiomid)%name)//', var: '//trim(tname)
873         IF ( PRESENT(kntime) ) THEN
874            idvar  = iom_varid( kiomid, tname, kdimsz = kdimsz )
875            kntime = kdimsz(1)
876         ELSE
877            idvar = iom_varid( kiomid, tname )
878         ENDIF
879         !
880         ptime(:) = 0. ! default definition
881         IF( idvar > 0 ) THEN
882            IF( iom_file(kiomid)%ndims(idvar) == 1 ) THEN
883               IF( iom_file(kiomid)%luld(idvar) ) THEN
884                  IF( iom_file(kiomid)%dimsz(1,idvar) <= size(ptime) ) THEN
885                     SELECT CASE (iom_file(kiomid)%iolib)
886                     CASE (jpioipsl )   ;   CALL iom_ioipsl_gettime( kiomid, idvar, ptime, cdunits, cdcalendar )
887                     CASE (jpnf90   )   ;   CALL iom_nf90_gettime(   kiomid, idvar, ptime, cdunits, cdcalendar )
888                     CASE (jprstdimg)   ;   CALL ctl_stop( TRIM(clinfo)//' case IO library == jprstdimg not coded...' )
889                     CASE DEFAULT   
890                        CALL ctl_stop( TRIM(clinfo)//' accepted IO library are only jpioipsl, jpnf90 and jprstdimg' )
891                     END SELECT
892                  ELSE
893                     WRITE(ctmp1,*) 'error with the size of ptime ',size(ptime),iom_file(kiomid)%dimsz(1,idvar)
894                     CALL ctl_stop( trim(clinfo), trim(ctmp1) )
895                  ENDIF
896               ELSE
897                  CALL ctl_stop( trim(clinfo), 'variable dimension is not unlimited... use iom_get' )
898               ENDIF
899            ELSE
900               CALL ctl_stop( trim(clinfo), 'the variable has more than 1 dimension' )
901            ENDIF
902         ELSE
903            CALL ctl_stop( trim(clinfo), 'variable not found in '//iom_file(kiomid)%name )
904         ENDIF
905      ENDIF
906      !
907   END SUBROUTINE iom_gettime
908
909
910   !!----------------------------------------------------------------------
911   !!                   INTERFACE iom_getatt
912   !!----------------------------------------------------------------------
913   SUBROUTINE iom_g0d_intatt( kiomid, cdatt, pvar )
914      INTEGER         , INTENT(in   )                 ::   kiomid    ! Identifier of the file
915      CHARACTER(len=*), INTENT(in   )                 ::   cdatt     ! Name of the attribute
916      INTEGER         , INTENT(  out)                 ::   pvar      ! read field
917      !
918      IF( kiomid > 0 ) THEN
919         IF( iom_file(kiomid)%nfid > 0 ) THEN
920            SELECT CASE (iom_file(kiomid)%iolib)
921            CASE (jpioipsl )   ;   CALL ctl_stop('iom_getatt: only nf90 available')
922            CASE (jpnf90   )   ;   CALL iom_nf90_getatt( kiomid, cdatt, pvar )
923            CASE (jprstdimg)   ;   CALL ctl_stop('iom_getatt: only nf90 available')
924            CASE DEFAULT   
925               CALL ctl_stop( 'iom_g0d_att: accepted IO library are only jpioipsl, jpnf90 and jprstdimg' )
926            END SELECT
927         ENDIF
928      ENDIF
929   END SUBROUTINE iom_g0d_intatt
930
931
932   !!----------------------------------------------------------------------
933   !!                   INTERFACE iom_rstput
934   !!----------------------------------------------------------------------
935   SUBROUTINE iom_rp0d( kt, kwrite, kiomid, cdvar, pvar, ktype )
936      INTEGER         , INTENT(in)                         ::   kt       ! ocean time-step
937      INTEGER         , INTENT(in)                         ::   kwrite   ! writing time-step
938      INTEGER         , INTENT(in)                         ::   kiomid   ! Identifier of the file
939      CHARACTER(len=*), INTENT(in)                         ::   cdvar    ! time axis name
940      REAL(wp)        , INTENT(in)                         ::   pvar     ! written field
941      INTEGER         , INTENT(in), OPTIONAL               ::   ktype    ! variable external type
942      INTEGER :: ivid   ! variable id
943      IF( kiomid > 0 ) THEN
944         IF( iom_file(kiomid)%nfid > 0 ) THEN
945            ivid = iom_varid( kiomid, cdvar, ldstop = .FALSE. )
946            SELECT CASE (iom_file(kiomid)%iolib)
947            CASE (jpioipsl )   ;   CALL iom_ioipsl_rstput( kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r0d = pvar )
948            CASE (jpnf90   )   ;   CALL iom_nf90_rstput(   kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r0d = pvar )
949            CASE (jprstdimg)   ;   IF( kt == kwrite )    CALL iom_rstdimg_rstput( kiomid, cdvar, ivid, pvar )
950            CASE DEFAULT     
951               CALL ctl_stop( 'iom_rp0d: accepted IO library are only jpioipsl, jpnf90 and jprstdimg' )
952            END SELECT
953         ENDIF
954      ENDIF
955   END SUBROUTINE iom_rp0d
956
957   SUBROUTINE iom_rp1d( kt, kwrite, kiomid, cdvar, pvar, ktype )
958      INTEGER         , INTENT(in)                         ::   kt       ! ocean time-step
959      INTEGER         , INTENT(in)                         ::   kwrite   ! writing time-step
960      INTEGER         , INTENT(in)                         ::   kiomid   ! Identifier of the file
961      CHARACTER(len=*), INTENT(in)                         ::   cdvar    ! time axis name
962      REAL(wp)        , INTENT(in), DIMENSION(          :) ::   pvar     ! written field
963      INTEGER         , INTENT(in), OPTIONAL               ::   ktype    ! variable external type
964      INTEGER :: ivid   ! variable id
965      IF( kiomid > 0 ) THEN
966         IF( iom_file(kiomid)%nfid > 0 ) THEN
967            ivid = iom_varid( kiomid, cdvar, ldstop = .FALSE. )
968            SELECT CASE (iom_file(kiomid)%iolib)
969            CASE (jpioipsl )   ;   CALL iom_ioipsl_rstput( kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r1d = pvar )
970            CASE (jpnf90   )   ;   CALL iom_nf90_rstput(   kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r1d = pvar )
971            CASE (jprstdimg)   ;   IF( kt == kwrite )    CALL iom_rstdimg_rstput( kiomid, cdvar, ivid, pv_r1d = pvar )
972            CASE DEFAULT     
973               CALL ctl_stop( 'iom_rp1d: accepted IO library are only jpioipsl, jpnf90 and jprstdimg' )
974            END SELECT
975         ENDIF
976      ENDIF
977   END SUBROUTINE iom_rp1d
978
979   SUBROUTINE iom_rp2d( kt, kwrite, kiomid, cdvar, pvar, ktype )
980      INTEGER         , INTENT(in)                         ::   kt       ! ocean time-step
981      INTEGER         , INTENT(in)                         ::   kwrite   ! writing time-step
982      INTEGER         , INTENT(in)                         ::   kiomid   ! Identifier of the file
983      CHARACTER(len=*), INTENT(in)                         ::   cdvar    ! time axis name
984      REAL(wp)        , INTENT(in), DIMENSION(:,    :    ) ::   pvar     ! written field
985      INTEGER         , INTENT(in), OPTIONAL               ::   ktype    ! variable external type
986      INTEGER :: ivid   ! variable id
987      IF( kiomid > 0 ) THEN
988         IF( iom_file(kiomid)%nfid > 0 ) THEN
989            ivid = iom_varid( kiomid, cdvar, ldstop = .FALSE. )
990            SELECT CASE (iom_file(kiomid)%iolib)
991            CASE (jpioipsl )   ;   CALL iom_ioipsl_rstput( kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r2d = pvar )
992            CASE (jpnf90   )   ;   CALL iom_nf90_rstput(   kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r2d = pvar )
993            CASE (jprstdimg)   ;   IF( kt == kwrite )   CALL iom_rstdimg_rstput( kiomid, cdvar, ivid, pv_r2d = pvar ) 
994            CASE DEFAULT     
995               CALL ctl_stop( 'iom_rp2d: accepted IO library are only jpioipsl, jpnf90 and jprstdimg' )
996            END SELECT
997         ENDIF
998      ENDIF
999   END SUBROUTINE iom_rp2d
1000
1001   SUBROUTINE iom_rp3d( kt, kwrite, kiomid, cdvar, pvar, ktype )
1002      INTEGER         , INTENT(in)                         ::   kt       ! ocean time-step
1003      INTEGER         , INTENT(in)                         ::   kwrite   ! writing time-step
1004      INTEGER         , INTENT(in)                         ::   kiomid   ! Identifier of the file
1005      CHARACTER(len=*), INTENT(in)                         ::   cdvar    ! time axis name
1006      REAL(wp)        , INTENT(in),       DIMENSION(:,:,:) ::   pvar     ! written field
1007      INTEGER         , INTENT(in), OPTIONAL               ::   ktype    ! variable external type
1008      INTEGER :: ivid   ! variable id
1009      IF( kiomid > 0 ) THEN
1010         IF( iom_file(kiomid)%nfid > 0 ) THEN
1011            ivid = iom_varid( kiomid, cdvar, ldstop = .FALSE. )
1012            SELECT CASE (iom_file(kiomid)%iolib)
1013            CASE (jpioipsl )   ;   CALL iom_ioipsl_rstput( kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r3d = pvar )
1014            CASE (jpnf90   )   ;   CALL iom_nf90_rstput(   kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r3d = pvar )
1015            CASE (jprstdimg)   ;   IF( kt == kwrite )   CALL iom_rstdimg_rstput( kiomid, cdvar, ivid, pv_r3d = pvar )
1016            CASE DEFAULT     
1017               CALL ctl_stop( 'iom_rp3d: accepted IO library are only jpioipsl and jprstdimg' )
1018            END SELECT
1019         ENDIF
1020      ENDIF
1021   END SUBROUTINE iom_rp3d
1022
1023
1024   !!----------------------------------------------------------------------
1025   !!                   INTERFACE iom_put
1026   !!----------------------------------------------------------------------
1027   SUBROUTINE iom_p0d( cdname, pfield0d )
1028      CHARACTER(LEN=*), INTENT(in) ::   cdname
1029      REAL(wp)        , INTENT(in) ::   pfield0d
1030      REAL(wp)        , DIMENSION(jpi,jpj) ::   zz     ! masson
1031#if defined key_iomput
1032      zz(:,:)=pfield0d
1033      CALL xios_send_field(cdname, zz)
1034      !CALL xios_send_field(cdname, (/pfield0d/))
1035#else
1036      IF( .FALSE. )   WRITE(numout,*) cdname, pfield0d   ! useless test to avoid compilation warnings
1037#endif
1038   END SUBROUTINE iom_p0d
1039
1040   SUBROUTINE iom_p1d( cdname, pfield1d )
1041      CHARACTER(LEN=*)          , INTENT(in) ::   cdname
1042      REAL(wp),     DIMENSION(:), INTENT(in) ::   pfield1d
1043#if defined key_iomput
1044      CALL xios_send_field( cdname, RESHAPE( (/pfield1d/), (/1,1,SIZE(pfield1d)/) ) )
1045#else
1046      IF( .FALSE. )   WRITE(numout,*) cdname, pfield1d   ! useless test to avoid compilation warnings
1047#endif
1048   END SUBROUTINE iom_p1d
1049
1050   SUBROUTINE iom_p2d( cdname, pfield2d )
1051      CHARACTER(LEN=*)            , INTENT(in) ::   cdname
1052      REAL(wp),     DIMENSION(:,:), INTENT(in) ::   pfield2d
1053#if defined key_iomput
1054      CALL xios_send_field(cdname, pfield2d)
1055#else
1056      IF( .FALSE. )   WRITE(numout,*) cdname, pfield2d   ! useless test to avoid compilation warnings
1057#endif
1058   END SUBROUTINE iom_p2d
1059
1060   SUBROUTINE iom_p3d( cdname, pfield3d )
1061      CHARACTER(LEN=*)                , INTENT(in) ::   cdname
1062      REAL(wp),       DIMENSION(:,:,:), INTENT(in) ::   pfield3d
1063#if defined key_iomput
1064      CALL xios_send_field(cdname, pfield3d)
1065#else
1066      IF( .FALSE. )   WRITE(numout,*) cdname, pfield3d   ! useless test to avoid compilation warnings
1067#endif
1068   END SUBROUTINE iom_p3d
1069   !!----------------------------------------------------------------------
1070
1071#if defined key_iomput
1072
1073   SUBROUTINE iom_set_domain_attr( cdid, ni_glo, nj_glo, ibegin, jbegin, ni, nj, zoom_ibegin, zoom_jbegin, zoom_ni, zoom_nj,   &
1074      &                                    data_dim, data_ibegin, data_ni, data_jbegin, data_nj, lonvalue, latvalue, mask )
1075      CHARACTER(LEN=*)                 , INTENT(in) ::   cdid
1076      INTEGER                , OPTIONAL, INTENT(in) ::   ni_glo, nj_glo, ibegin, jbegin, ni, nj
1077      INTEGER                , OPTIONAL, INTENT(in) ::   data_dim, data_ibegin, data_ni, data_jbegin, data_nj
1078      INTEGER                , OPTIONAL, INTENT(in) ::   zoom_ibegin, zoom_jbegin, zoom_ni, zoom_nj
1079      REAL(wp), DIMENSION(:) , OPTIONAL, INTENT(in) ::   lonvalue, latvalue
1080      LOGICAL, DIMENSION(:,:), OPTIONAL, INTENT(in) ::   mask
1081
1082      IF ( xios_is_valid_domain     (cdid) ) THEN
1083         CALL xios_set_domain_attr     ( cdid, ni_glo=ni_glo, nj_glo=nj_glo, ibegin=ibegin, jbegin=jbegin, ni=ni, nj=nj,   &
1084            &    data_dim=data_dim, data_ibegin=data_ibegin, data_ni=data_ni, data_jbegin=data_jbegin, data_nj=data_nj ,   &
1085            &    zoom_ibegin=zoom_ibegin, zoom_jbegin=zoom_jbegin, zoom_ni=zoom_ni, zoom_nj=zoom_nj,                       &
1086            &    lonvalue=lonvalue, latvalue=latvalue,mask=mask )
1087      ENDIF
1088
1089      IF ( xios_is_valid_domaingroup(cdid) ) THEN
1090         CALL xios_set_domaingroup_attr( cdid, ni_glo=ni_glo, nj_glo=nj_glo, ibegin=ibegin, jbegin=jbegin, ni=ni, nj=nj,   &
1091            &    data_dim=data_dim, data_ibegin=data_ibegin, data_ni=data_ni, data_jbegin=data_jbegin, data_nj=data_nj ,   &
1092            &    zoom_ibegin=zoom_ibegin, zoom_jbegin=zoom_jbegin, zoom_ni=zoom_ni, zoom_nj=zoom_nj,                       &
1093            &    lonvalue=lonvalue, latvalue=latvalue,mask=mask )
1094      ENDIF
1095      CALL xios_solve_inheritance()
1096
1097   END SUBROUTINE iom_set_domain_attr
1098
1099
1100   SUBROUTINE iom_set_axis_attr( cdid, paxis )
1101      CHARACTER(LEN=*)      , INTENT(in) ::   cdid
1102      REAL(wp), DIMENSION(:), INTENT(in) ::   paxis
1103      IF ( xios_is_valid_axis     (cdid) )   CALL xios_set_axis_attr     ( cdid, size=size(paxis),value=paxis )
1104      IF ( xios_is_valid_axisgroup(cdid) )   CALL xios_set_axisgroup_attr( cdid, size=size(paxis),value=paxis )
1105      CALL xios_solve_inheritance()
1106   END SUBROUTINE iom_set_axis_attr
1107
1108
1109   SUBROUTINE iom_set_field_attr( cdid, freq_op, freq_offset )
1110      CHARACTER(LEN=*)          , INTENT(in) ::   cdid
1111      CHARACTER(LEN=*),OPTIONAL , INTENT(in) ::   freq_op
1112      CHARACTER(LEN=*),OPTIONAL , INTENT(in) ::   freq_offset
1113      IF ( xios_is_valid_field     (cdid) )   CALL xios_set_field_attr     ( cdid, freq_op=freq_op, freq_offset=freq_offset )
1114      IF ( xios_is_valid_fieldgroup(cdid) )   CALL xios_set_fieldgroup_attr( cdid, freq_op=freq_op, freq_offset=freq_offset )
1115      CALL xios_solve_inheritance()
1116   END SUBROUTINE iom_set_field_attr
1117
1118
1119   SUBROUTINE iom_set_file_attr( cdid, name, name_suffix )
1120      CHARACTER(LEN=*)          , INTENT(in) ::   cdid
1121      CHARACTER(LEN=*),OPTIONAL , INTENT(in) ::   name, name_suffix
1122      IF ( xios_is_valid_file     (cdid) )   CALL xios_set_file_attr     ( cdid, name=name, name_suffix=name_suffix )
1123      IF ( xios_is_valid_filegroup(cdid) )   CALL xios_set_filegroup_attr( cdid, name=name, name_suffix=name_suffix )
1124      CALL xios_solve_inheritance()
1125   END SUBROUTINE iom_set_file_attr
1126
1127
1128   SUBROUTINE iom_get_file_attr( cdid, name, name_suffix, output_freq )
1129      CHARACTER(LEN=*)          , INTENT(in ) ::   cdid
1130      CHARACTER(LEN=*),OPTIONAL , INTENT(out) ::   name, name_suffix, output_freq
1131      LOGICAL                                 ::   llexist1,llexist2,llexist3
1132      !---------------------------------------------------------------------
1133      IF( PRESENT( name        ) )   name = ''          ! default values
1134      IF( PRESENT( name_suffix ) )   name_suffix = ''
1135      IF( PRESENT( output_freq ) )   output_freq = ''
1136      IF ( xios_is_valid_file     (cdid) ) THEN
1137         CALL xios_solve_inheritance()
1138         CALL xios_is_defined_file_attr     ( cdid, name = llexist1, name_suffix = llexist2, output_freq = llexist3)
1139         IF(llexist1)   CALL xios_get_file_attr     ( cdid, name = name )
1140         IF(llexist2)   CALL xios_get_file_attr     ( cdid, name_suffix = name_suffix )
1141         IF(llexist3)   CALL xios_get_file_attr     ( cdid, output_freq = output_freq )
1142      ENDIF
1143      IF ( xios_is_valid_filegroup(cdid) ) THEN
1144         CALL xios_solve_inheritance()
1145         CALL xios_is_defined_filegroup_attr( cdid, name = llexist1, name_suffix = llexist2, output_freq = llexist3)
1146         IF(llexist1)   CALL xios_get_filegroup_attr( cdid, name = name )
1147         IF(llexist2)   CALL xios_get_filegroup_attr( cdid, name_suffix = name_suffix )
1148         IF(llexist3)   CALL xios_get_filegroup_attr( cdid, output_freq = output_freq )
1149      ENDIF
1150   END SUBROUTINE iom_get_file_attr
1151
1152
1153   SUBROUTINE iom_set_grid_attr( cdid, mask )
1154      CHARACTER(LEN=*)                   , INTENT(in) ::   cdid
1155      LOGICAL, DIMENSION(:,:,:), OPTIONAL, INTENT(in) ::   mask
1156      IF ( xios_is_valid_grid     (cdid) )   CALL xios_set_grid_attr     ( cdid, mask=mask )
1157      IF ( xios_is_valid_gridgroup(cdid) )   CALL xios_set_gridgroup_attr( cdid, mask=mask )
1158      CALL xios_solve_inheritance()
1159   END SUBROUTINE iom_set_grid_attr
1160
1161   SUBROUTINE iom_setkt( kt, cdname )
1162      INTEGER         , INTENT(in) ::   kt 
1163      CHARACTER(LEN=*), INTENT(in) ::   cdname
1164      !     
1165      CALL iom_swap( cdname )   ! swap to cdname context
1166      CALL xios_update_calendar(kt)
1167      IF( cdname /= "nemo" ) CALL iom_swap( "nemo" )   ! return back to nemo context
1168      !
1169   END SUBROUTINE iom_setkt
1170
1171   SUBROUTINE iom_context_finalize( cdname )
1172      CHARACTER(LEN=*), INTENT(in) :: cdname
1173      !
1174      IF( xios_is_valid_context(cdname) ) THEN
1175         CALL iom_swap( cdname )   ! swap to cdname context
1176         CALL xios_context_finalize() ! finalize the context
1177         IF( cdname /= "nemo" ) CALL iom_swap( "nemo" )   ! return back to nemo context
1178      ENDIF
1179      !
1180   END SUBROUTINE iom_context_finalize
1181
1182
1183   SUBROUTINE set_grid( cdgrd, plon, plat )
1184      !!----------------------------------------------------------------------
1185      !!                     ***  ROUTINE set_grid  ***
1186      !!
1187      !! ** Purpose :   define horizontal grids
1188      !!
1189      !!----------------------------------------------------------------------
1190      CHARACTER(LEN=1)            , INTENT(in) ::   cdgrd
1191      REAL(wp), DIMENSION(jpi,jpj), INTENT(in) ::   plon
1192      REAL(wp), DIMENSION(jpi,jpj), INTENT(in) ::   plat
1193      !
1194      REAL(wp), DIMENSION(jpi,jpj,jpk) ::   zmask
1195      INTEGER  :: ni,nj
1196     
1197      ni=nlei-nldi+1 ; nj=nlej-nldj+1
1198
1199      CALL iom_set_domain_attr("grid_"//cdgrd, ni_glo=jpiglo, nj_glo=jpjglo, ibegin=nimpp+nldi-1, jbegin=njmpp+nldj-1, ni=ni, nj=nj)
1200      CALL iom_set_domain_attr("grid_"//cdgrd, data_dim=2, data_ibegin = 1-nldi, data_ni = jpi, data_jbegin = 1-nldj, data_nj = jpj)
1201      CALL iom_set_domain_attr("grid_"//cdgrd, lonvalue = RESHAPE(plon(nldi:nlei, nldj:nlej),(/ ni*nj /)),   &
1202         &                                     latvalue = RESHAPE(plat(nldi:nlei, nldj:nlej),(/ ni*nj /))) 
1203
1204      IF ( ln_mskland ) THEN
1205         ! mask land points, keep values on coast line -> specific mask for U, V and W points
1206         SELECT CASE ( cdgrd )
1207         CASE('T')   ;   zmask(:,:,:)       = tmask(:,:,:)
1208         CASE('U')   ;   zmask(2:jpim1,:,:) = tmask(2:jpim1,:,:) + tmask(3:jpi,:,:)   ;   CALL lbc_lnk( zmask, 'U', 1. )
1209         CASE('V')   ;   zmask(:,2:jpjm1,:) = tmask(:,2:jpjm1,:) + tmask(:,3:jpi,:)   ;   CALL lbc_lnk( zmask, 'V', 1. )
1210         CASE('W')   ;   zmask(:,:,2:jpk  ) = tmask(:,:,1:jpkm1) + tmask(:,:,2:jpk)   ;   zmask(:,:,1) = tmask(:,:,1)
1211         END SELECT
1212         !
1213         CALL iom_set_domain_attr( "grid_"//cdgrd       , mask = RESHAPE(zmask(nldi:nlei,nldj:nlej,1),(/ni,nj    /)) /= 0. )
1214         CALL iom_set_grid_attr  ( "grid_"//cdgrd//"_3D", mask = RESHAPE(zmask(nldi:nlei,nldj:nlej,:),(/ni,nj,jpk/)) /= 0. )
1215      ENDIF
1216     
1217   END SUBROUTINE set_grid
1218
1219
1220   SUBROUTINE set_scalar
1221      !!----------------------------------------------------------------------
1222      !!                     ***  ROUTINE set_scalar  ***
1223      !!
1224      !! ** Purpose :   define fake grids for scalar point
1225      !!
1226      !!----------------------------------------------------------------------
1227      REAL(wp), DIMENSION(1) ::   zz = 1.
1228      !!----------------------------------------------------------------------
1229      CALL iom_set_domain_attr('scalarpoint', ni_glo=jpnij, nj_glo=1, ibegin=narea, jbegin=1, ni=1, nj=1)
1230      CALL iom_set_domain_attr('scalarpoint', data_dim=2, data_ibegin = 1, data_ni = 1, data_jbegin = 1, data_nj = 1)
1231      zz=REAL(narea,wp)
1232      CALL iom_set_domain_attr('scalarpoint', lonvalue=zz, latvalue=zz)
1233
1234   END SUBROUTINE set_scalar
1235
1236
1237   SUBROUTINE set_xmlatt
1238      !!----------------------------------------------------------------------
1239      !!                     ***  ROUTINE set_xmlatt  ***
1240      !!
1241      !! ** Purpose :   automatic definitions of some of the xml attributs...
1242      !!
1243      !!----------------------------------------------------------------------
1244      CHARACTER(len=1),DIMENSION( 3) ::   clgrd                    ! suffix name
1245      CHARACTER(len=256)             ::   clsuff                   ! suffix name
1246      CHARACTER(len=1)               ::   cl1                      ! 1 character
1247      CHARACTER(len=2)               ::   cl2                      ! 2 characters
1248      CHARACTER(len=3)               ::   cl3                      ! 3 characters
1249      INTEGER                        ::   ji, jg                   ! loop counters
1250      INTEGER                        ::   ix, iy                   ! i-,j- index
1251      REAL(wp)        ,DIMENSION(11) ::   zlontao                  ! longitudes of tao    moorings
1252      REAL(wp)        ,DIMENSION( 7) ::   zlattao                  ! latitudes  of tao    moorings
1253      REAL(wp)        ,DIMENSION( 4) ::   zlonrama                 ! longitudes of rama   moorings
1254      REAL(wp)        ,DIMENSION(11) ::   zlatrama                 ! latitudes  of rama   moorings
1255      REAL(wp)        ,DIMENSION( 3) ::   zlonpira                 ! longitudes of pirata moorings
1256      REAL(wp)        ,DIMENSION( 9) ::   zlatpira                 ! latitudes  of pirata moorings
1257      !!----------------------------------------------------------------------
1258      !
1259      ! frequency of the call of iom_put (attribut: freq_op)
1260      WRITE(cl1,'(i1)')        1   ;   CALL iom_set_field_attr('field_definition', freq_op = cl1//'ts', freq_offset='0ts')
1261      WRITE(cl1,'(i1)')  nn_fsbc   ;   CALL iom_set_field_attr('SBC'             , freq_op = cl1//'ts', freq_offset='0ts')
1262      WRITE(cl1,'(i1)')  nn_fsbc   ;   CALL iom_set_field_attr('SBC_scalar'      , freq_op = cl1//'ts', freq_offset='0ts')
1263      WRITE(cl1,'(i1)') nn_dttrc   ;   CALL iom_set_field_attr('ptrc_T'          , freq_op = cl1//'ts', freq_offset='0ts')
1264      WRITE(cl1,'(i1)') nn_dttrc   ;   CALL iom_set_field_attr('diad_T'          , freq_op = cl1//'ts', freq_offset='0ts')
1265       
1266      ! output file names (attribut: name)
1267      DO ji = 1, 9
1268         WRITE(cl1,'(i1)') ji 
1269         CALL iom_update_file_name('file'//cl1)
1270      END DO
1271      DO ji = 1, 99
1272         WRITE(cl2,'(i2.2)') ji 
1273         CALL iom_update_file_name('file'//cl2)
1274      END DO
1275      DO ji = 1, 999
1276         WRITE(cl3,'(i3.3)') ji 
1277         CALL iom_update_file_name('file'//cl3)
1278      END DO
1279
1280      ! Zooms...
1281      clgrd = (/ 'T', 'U', 'W' /) 
1282      DO jg = 1, SIZE(clgrd)                                                                   ! grid type
1283         cl1 = clgrd(jg)
1284         ! Equatorial section (attributs: jbegin, ni, name_suffix)
1285         CALL dom_ngb( 0., 0., ix, iy, cl1 )
1286         CALL iom_set_domain_attr ('Eq'//cl1, zoom_jbegin=iy, zoom_ni=jpiglo)
1287         CALL iom_get_file_attr   ('Eq'//cl1, name_suffix = clsuff             )
1288         CALL iom_set_file_attr   ('Eq'//cl1, name_suffix = TRIM(clsuff)//'_Eq')
1289         CALL iom_update_file_name('Eq'//cl1)
1290      END DO
1291      ! TAO moorings (attributs: ibegin, jbegin, name_suffix)
1292      zlontao = (/ 137.0, 147.0, 156.0, 165.0, -180.0, -170.0, -155.0, -140.0, -125.0, -110.0, -95.0 /)
1293      zlattao = (/  -8.0,  -5.0,  -2.0,   0.0,    2.0,    5.0,    8.0 /)
1294      CALL set_mooring( zlontao, zlattao )
1295      ! RAMA moorings (attributs: ibegin, jbegin, name_suffix)
1296      zlonrama = (/  55.0,  67.0, 80.5, 90.0 /)
1297      zlatrama = (/ -16.0, -12.0, -8.0, -4.0, -1.5, 0.0, 1.5, 4.0, 8.0, 12.0, 15.0 /)
1298      CALL set_mooring( zlonrama, zlatrama )
1299      ! PIRATA moorings (attributs: ibegin, jbegin, name_suffix)
1300      zlonpira = (/ -38.0, -23.0, -10.0 /)
1301      zlatpira = (/ -19.0, -14.0,  -8.0, 0.0, 4.0, 8.0, 12.0, 15.0, 20.0 /)
1302      CALL set_mooring( zlonpira, zlatpira )
1303     
1304   END SUBROUTINE set_xmlatt
1305
1306
1307   SUBROUTINE set_mooring( plon, plat)
1308      !!----------------------------------------------------------------------
1309      !!                     ***  ROUTINE set_mooring  ***
1310      !!
1311      !! ** Purpose :   automatic definitions of moorings xml attributs...
1312      !!
1313      !!----------------------------------------------------------------------
1314      REAL(wp), DIMENSION(:), INTENT(in) ::  plon, plat           ! longitudes/latitudes oft the mooring
1315      !
1316!!$      CHARACTER(len=1),DIMENSION(4) ::   clgrd = (/ 'T', 'U', 'V', 'W' /)   ! suffix name
1317      CHARACTER(len=1),DIMENSION(1) ::   clgrd = (/ 'T' /)        ! suffix name
1318      CHARACTER(len=256)            ::   clname                   ! file name
1319      CHARACTER(len=256)            ::   clsuff                   ! suffix name
1320      CHARACTER(len=1)              ::   cl1                      ! 1 character
1321      CHARACTER(len=6)              ::   clon,clat                ! name of longitude, latitude
1322      INTEGER                       ::   ji, jj, jg               ! loop counters
1323      INTEGER                       ::   ix, iy                   ! i-,j- index
1324      REAL(wp)                      ::   zlon, zlat
1325      !!----------------------------------------------------------------------
1326      DO jg = 1, SIZE(clgrd)
1327         cl1 = clgrd(jg)
1328         DO ji = 1, SIZE(plon)
1329            DO jj = 1, SIZE(plat)
1330               zlon = plon(ji)
1331               zlat = plat(jj)
1332               ! modifications for RAMA moorings
1333               IF( zlon ==  67. .AND. zlat ==  15. )   zlon =  65.
1334               IF( zlon ==  90. .AND. zlat <=  -4. )   zlon =  95.
1335               IF( zlon ==  95. .AND. zlat ==  -4. )   zlat =  -5.
1336               ! modifications for PIRATA moorings
1337               IF( zlon == -38. .AND. zlat == -19. )   zlon = -34.
1338               IF( zlon == -38. .AND. zlat == -14. )   zlon = -32.
1339               IF( zlon == -38. .AND. zlat ==  -8. )   zlon = -30.
1340               IF( zlon == -38. .AND. zlat ==   0. )   zlon = -35.
1341               IF( zlon == -23. .AND. zlat ==  20. )   zlat =  21.
1342               IF( zlon == -10. .AND. zlat == -14. )   zlat = -10.
1343               IF( zlon == -10. .AND. zlat ==  -8. )   zlat =  -6.
1344               IF( zlon == -10. .AND. zlat ==   4. ) THEN   ;   zlon = 0.   ;   zlat = 0.   ;   ENDIF
1345               CALL dom_ngb( zlon, zlat, ix, iy, cl1 )
1346               IF( zlon >= 0. ) THEN 
1347                  IF( zlon == REAL(NINT(zlon), wp) ) THEN   ;   WRITE(clon, '(i3,  a)') NINT( zlon), 'e'
1348                  ELSE                                      ;   WRITE(clon, '(f5.1,a)')       zlon , 'e'
1349                  ENDIF
1350               ELSE             
1351                  IF( zlon == REAL(NINT(zlon), wp) ) THEN   ;   WRITE(clon, '(i3,  a)') NINT(-zlon), 'w'
1352                  ELSE                                      ;   WRITE(clon, '(f5.1,a)')      -zlon , 'w'
1353                  ENDIF
1354               ENDIF
1355               IF( zlat >= 0. ) THEN 
1356                  IF( zlat == REAL(NINT(zlat), wp) ) THEN   ;   WRITE(clat, '(i2,  a)') NINT( zlat), 'n'
1357                  ELSE                                      ;   WRITE(clat, '(f4.1,a)')       zlat , 'n'
1358                  ENDIF
1359               ELSE             
1360                  IF( zlat == REAL(NINT(zlat), wp) ) THEN   ;   WRITE(clat, '(i2,  a)') NINT(-zlat), 's'
1361                  ELSE                                      ;   WRITE(clat, '(f4.1,a)')      -zlat , 's'
1362                  ENDIF
1363               ENDIF
1364               clname = TRIM(ADJUSTL(clat))//TRIM(ADJUSTL(clon))
1365               CALL iom_set_domain_attr (TRIM(clname)//cl1, zoom_ibegin= ix, zoom_jbegin= iy)
1366               CALL iom_get_file_attr   (TRIM(clname)//cl1, name_suffix = clsuff                         )
1367               CALL iom_set_file_attr   (TRIM(clname)//cl1, name_suffix = TRIM(clsuff)//'_'//TRIM(clname))
1368               CALL iom_update_file_name(TRIM(clname)//cl1)
1369            END DO
1370         END DO
1371      END DO
1372     
1373   END SUBROUTINE set_mooring
1374
1375   
1376   SUBROUTINE iom_update_file_name( cdid )
1377      !!----------------------------------------------------------------------
1378      !!                     ***  ROUTINE iom_update_file_name  ***
1379      !!
1380      !! ** Purpose :   
1381      !!
1382      !!----------------------------------------------------------------------
1383      CHARACTER(LEN=*)          , INTENT(in) ::   cdid
1384      !
1385      CHARACTER(LEN=256) ::   clname
1386      CHARACTER(LEN=20)  ::   clfreq
1387      CHARACTER(LEN=20)  ::   cldate
1388      INTEGER            ::   idx
1389      INTEGER            ::   jn
1390      INTEGER            ::   itrlen
1391      INTEGER            ::   iyear, imonth, iday, isec
1392      REAL(wp)           ::   zsec
1393      LOGICAL            ::   llexist
1394      !!----------------------------------------------------------------------
1395
1396      DO jn = 1,2
1397
1398         IF( jn == 1 )   CALL iom_get_file_attr( cdid, name        = clname, output_freq = clfreq )
1399         IF( jn == 2 )   CALL iom_get_file_attr( cdid, name_suffix = clname )
1400
1401         IF ( TRIM(clname) /= '' ) THEN
1402
1403            idx = INDEX(clname,'@expname@') + INDEX(clname,'@EXPNAME@')
1404            DO WHILE ( idx /= 0 ) 
1405               clname = clname(1:idx-1)//TRIM(cexper)//clname(idx+9:LEN_TRIM(clname))
1406               idx = INDEX(clname,'@expname@') + INDEX(clname,'@EXPNAME@')
1407            END DO
1408
1409            idx = INDEX(clname,'@freq@') + INDEX(clname,'@FREQ@')
1410            DO WHILE ( idx /= 0 ) 
1411               IF ( TRIM(clfreq) /= '' ) THEN
1412                  itrlen = LEN_TRIM(clfreq)
1413                  IF ( clfreq(itrlen-1:itrlen) == 'mo' ) clfreq = clfreq(1:itrlen-1)
1414                  clname = clname(1:idx-1)//TRIM(clfreq)//clname(idx+6:LEN_TRIM(clname))
1415               ELSE
1416                  CALL ctl_stop('error in the name of file id '//TRIM(cdid),   &
1417                     & ' attribute output_freq is undefined -> cannot replace @freq@ in '//TRIM(clname) )
1418               ENDIF
1419               idx = INDEX(clname,'@freq@') + INDEX(clname,'@FREQ@')
1420            END DO
1421
1422            idx = INDEX(clname,'@startdate@') + INDEX(clname,'@STARTDATE@')
1423            DO WHILE ( idx /= 0 ) 
1424               cldate = iom_sdate( fjulday - rdttra(1) / rday )
1425               clname = clname(1:idx-1)//TRIM(cldate)//clname(idx+11:LEN_TRIM(clname))
1426               idx = INDEX(clname,'@startdate@') + INDEX(clname,'@STARTDATE@')
1427            END DO
1428
1429            idx = INDEX(clname,'@startdatefull@') + INDEX(clname,'@STARTDATEFULL@')
1430            DO WHILE ( idx /= 0 ) 
1431               cldate = iom_sdate( fjulday - rdttra(1) / rday, ldfull = .TRUE. )
1432               clname = clname(1:idx-1)//TRIM(cldate)//clname(idx+15:LEN_TRIM(clname))
1433               idx = INDEX(clname,'@startdatefull@') + INDEX(clname,'@STARTDATEFULL@')
1434            END DO
1435
1436            idx = INDEX(clname,'@enddate@') + INDEX(clname,'@ENDDATE@')
1437            DO WHILE ( idx /= 0 ) 
1438               cldate = iom_sdate( fjulday + rdttra(1) / rday * REAL( nitend - nit000, wp ), ld24 = .TRUE. )
1439               clname = clname(1:idx-1)//TRIM(cldate)//clname(idx+9:LEN_TRIM(clname))
1440               idx = INDEX(clname,'@enddate@') + INDEX(clname,'@ENDDATE@')
1441            END DO
1442
1443            idx = INDEX(clname,'@enddatefull@') + INDEX(clname,'@ENDDATEFULL@')
1444            DO WHILE ( idx /= 0 ) 
1445               cldate = iom_sdate( fjulday + rdttra(1) / rday * REAL( nitend - nit000, wp ), ld24 = .TRUE., ldfull = .TRUE. )
1446               clname = clname(1:idx-1)//TRIM(cldate)//clname(idx+13:LEN_TRIM(clname))
1447               idx = INDEX(clname,'@enddatefull@') + INDEX(clname,'@ENDDATEFULL@')
1448            END DO
1449
1450            IF( jn == 1 )   CALL iom_set_file_attr( cdid, name        = clname )
1451            IF( jn == 2 )   CALL iom_set_file_attr( cdid, name_suffix = clname )
1452
1453         ENDIF
1454
1455      END DO
1456
1457   END SUBROUTINE iom_update_file_name
1458
1459
1460   FUNCTION iom_sdate( pjday, ld24, ldfull )
1461      !!----------------------------------------------------------------------
1462      !!                     ***  ROUTINE iom_sdate  ***
1463      !!
1464      !! ** Purpose :   send back the date corresponding to the given julian day
1465      !!
1466      !!----------------------------------------------------------------------
1467      REAL(wp), INTENT(in   )           ::   pjday         ! julian day
1468      LOGICAL , INTENT(in   ), OPTIONAL ::   ld24          ! true to force 24:00 instead of 00:00
1469      LOGICAL , INTENT(in   ), OPTIONAL ::   ldfull        ! true to get the compleate date: yyyymmdd_hh:mm:ss
1470      !
1471      CHARACTER(LEN=20) ::   iom_sdate
1472      CHARACTER(LEN=50) ::   clfmt                         !  format used to write the date
1473      INTEGER           ::   iyear, imonth, iday, ihour, iminute, isec
1474      REAL(wp)          ::   zsec
1475      LOGICAL           ::   ll24, llfull
1476      !
1477      IF( PRESENT(ld24) ) THEN   ;   ll24 = ld24
1478      ELSE                       ;   ll24 = .FALSE.
1479      ENDIF
1480
1481      IF( PRESENT(ldfull) ) THEN   ;   llfull = ldfull
1482      ELSE                         ;   llfull = .FALSE.
1483      ENDIF
1484
1485      CALL ju2ymds( pjday, iyear, imonth, iday, zsec )
1486      isec = NINT(zsec)
1487
1488      IF ( ll24 .AND. isec == 0 ) THEN   ! 00:00 of the next day -> move to 24:00 of the current day
1489         CALL ju2ymds( pjday - 1., iyear, imonth, iday, zsec )
1490         isec = 86400
1491      ENDIF
1492
1493      IF( iyear < 10000 ) THEN   ;   clfmt = "i4.4,2i2.2"                ! format used to write the date
1494      ELSE                       ;   WRITE(clfmt, "('i',i1,',2i2.2')") INT(LOG10(REAL(iyear,wp))) + 1
1495      ENDIF
1496     
1497      IF( llfull ) THEN
1498         clfmt = TRIM(clfmt)//",'_',i2.2,':',i2.2,':',i2.2"
1499         ihour   = isec / 3600
1500         isec    = MOD(isec, 3600)
1501         iminute = isec / 60
1502         isec    = MOD(isec, 60)
1503         WRITE(iom_sdate, '('//TRIM(clfmt)//')') iyear, imonth, iday, ihour, iminute, isec    ! date of the end of run
1504      ELSE
1505         WRITE(iom_sdate, '('//TRIM(clfmt)//')') iyear, imonth, iday                          ! date of the end of run
1506      ENDIF
1507
1508   END FUNCTION iom_sdate
1509
1510#else
1511
1512
1513   SUBROUTINE iom_setkt( kt, cdname )
1514      INTEGER         , INTENT(in)::   kt 
1515      CHARACTER(LEN=*), INTENT(in) ::   cdname
1516      IF( .FALSE. )   WRITE(numout,*) kt, cdname   ! useless test to avoid compilation warnings
1517   END SUBROUTINE iom_setkt
1518
1519   SUBROUTINE iom_context_finalize( cdname )
1520      CHARACTER(LEN=*), INTENT(in) ::   cdname
1521      IF( .FALSE. )   WRITE(numout,*)  cdname   ! useless test to avoid compilation warnings
1522   END SUBROUTINE iom_context_finalize
1523
1524#endif
1525
1526   LOGICAL FUNCTION iom_use( cdname )
1527      CHARACTER(LEN=*), INTENT(in) ::   cdname
1528#if defined key_iomput
1529      iom_use = xios_field_is_active( cdname )
1530#else
1531      iom_use = .FALSE.
1532#endif
1533   END FUNCTION iom_use
1534   
1535   !!======================================================================
1536END MODULE iom
Note: See TracBrowser for help on using the repository browser.