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

source: trunk/NEMOGCM/NEMO/OPA_SRC/IOM/iom.F90 @ 4689

Last change on this file since 4689 was 4689, checked in by clem, 10 years ago

new version of LIM3 with perfect conservation of heat, see ticket #1352

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