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

source: branches/2013/dev_UKMO_2013/NEMOGCM/NEMO/OPA_SRC/IOM/iom.F90 @ 5103

Last change on this file since 5103 was 4170, checked in by rfurner, 11 years ago

merging changes from 3987:4144 dev_r3987_UKMO6_C1D

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