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

source: trunk/NEMO/OPA_SRC/IOM/iom.F90 @ 1743

Last change on this file since 1743 was 1743, checked in by cetlod, 14 years ago

Add the control of xml attributes for TOP models, see ticket:597

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 60.2 KB
Line 
1MODULE iom
2   !!=====================================================================
3   !!                    ***  MODULE  iom ***
4   !! Input/Output manager :  Library to read input files
5   !!====================================================================
6   !! History :  9.0  ! 05 12  (J. Belier) Original code
7   !!            9.0  ! 06 02  (S. Masson) Adaptation to NEMO
8   !!             "   ! 07 07  (D. Storkey) Changes to iom_gettime
9   !!--------------------------------------------------------------------
10   !!gm  caution add !DIR nec: improved performance to be checked as well as no result changes
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 in_out_manager  ! I/O manager
21   USE dom_oce         ! ocean space and time domain
22   USE lbclnk          ! lateal boundary condition / mpp exchanges
23   USE iom_def         ! iom variables definitions
24   USE iom_ioipsl      ! NetCDF format with IOIPSL library
25   USE iom_nf90        ! NetCDF format with native NetCDF library
26   USE iom_rstdimg     ! restarts access direct format "dimg" style...
27
28#if defined key_iomput
29   USE sbc_oce, ONLY :   nn_fsbc         ! ocean space and time domain
30   USE domngb          ! ocean space and time domain
31   USE phycst          ! physical constants
32   USE dianam          ! build name of file
33   USE mod_event_client
34   USE mod_attribut
35# endif
36
37   IMPLICIT NONE
38   PUBLIC   !   must be public to be able to access iom_def through iom
39   
40#if defined key_iomput
41   LOGICAL, PUBLIC, PARAMETER ::   lk_iomput = .TRUE.        !: iom_put flag
42#else
43   LOGICAL, PUBLIC, PARAMETER ::   lk_iomput = .FALSE.       !: iom_put flag
44#endif
45   PUBLIC iom_init, iom_open, iom_close, iom_setkt, iom_varid, iom_get, iom_gettime, iom_rstput, iom_put
46
47   PRIVATE iom_rp0d, iom_rp1d, iom_rp2d, iom_rp3d
48   PRIVATE iom_g0d, iom_g1d, iom_g2d, iom_g3d, iom_get_123d
49   PRIVATE iom_p2d, iom_p3d
50#if defined key_iomput
51   PRIVATE set_grid
52# endif
53
54   INTERFACE iom_get
55      MODULE PROCEDURE iom_g0d, iom_g1d, iom_g2d, iom_g3d
56   END INTERFACE
57   INTERFACE iom_rstput
58      MODULE PROCEDURE iom_rp0d, iom_rp1d, iom_rp2d, iom_rp3d
59   END INTERFACE
60  INTERFACE iom_put
61     MODULE PROCEDURE iom_p0d, iom_p2d, iom_p3d
62  END INTERFACE
63#if defined key_iomput
64   INTERFACE iom_setkt
65      MODULE PROCEDURE event__set_timestep
66   END INTERFACE
67# endif
68
69   !!----------------------------------------------------------------------
70   !!  OPA 9.0 , LOCEAN-IPSL (2006)
71   !! $Id$
72   !! Software governed by the CeCILL licence (modipsl/doc/NEMO_CeCILL.txt)
73   !!----------------------------------------------------------------------
74
75CONTAINS
76
77   SUBROUTINE iom_init
78      !!----------------------------------------------------------------------
79      !!                     ***  ROUTINE   ***
80      !!
81      !! ** Purpose :   
82      !!
83      !!----------------------------------------------------------------------
84#if defined key_iomput
85      REAL(wp) ::   ztmp
86      !!----------------------------------------------------------------------
87      ! read the xml file
88      CALL event__parse_xml_file( 'iodef.xml' )   ! <- to get from the nameliste (namrun)...
89
90      ! calendar parameters
91      SELECT CASE ( nleapy )        ! Choose calendar for IOIPSL
92      CASE ( 1)   ;   CALL event__set_calendar('gregorian')
93      CASE ( 0)   ;   CALL event__set_calendar('noleap'   )
94      CASE (30)   ;   CALL event__set_calendar('360d'     )
95      END SELECT
96      ztmp = fjulday - adatrj
97      IF( ABS(ztmp  - REAL(NINT(ztmp),wp)) < 0.1 / rday )   ztmp = REAL(NINT(ztmp),wp)   ! avoid truncation error
98      CALL event__set_time_parameters( nit000 - 1, ztmp, rdt )
99
100      ! horizontal grid definition
101      CALL set_scalar
102      CALL set_grid( "grid_T", glamt, gphit )
103      CALL set_grid( "grid_U", glamu, gphiu )
104      CALL set_grid( "grid_V", glamv, gphiv )
105      CALL set_grid( "grid_W", glamt, gphit )
106
107      ! vertical grid definition
108      CALL event__set_vert_axis( "deptht", gdept_0 )
109      CALL event__set_vert_axis( "depthu", gdept_0 )
110      CALL event__set_vert_axis( "depthv", gdept_0 )
111      CALL event__set_vert_axis( "depthw", gdepw_0 )
112     
113      ! automatic definitions of some of the xml attributs
114      CALL set_xmlatt
115
116      ! end file definition
117      CALL event__close_io_definition
118#endif
119
120   END SUBROUTINE iom_init
121
122
123   SUBROUTINE iom_open( cdname, kiomid, ldwrt, kdom, kiolib, ldstop, ldiof )
124      !!---------------------------------------------------------------------
125      !!                   ***  SUBROUTINE  iom_open  ***
126      !!
127      !! ** Purpose :  open an input file (return 0 if not found)
128      !!---------------------------------------------------------------------
129      CHARACTER(len=*), INTENT(in   )           ::   cdname   ! File name
130      INTEGER         , INTENT(  out)           ::   kiomid   ! iom identifier of the opened file
131      LOGICAL         , INTENT(in   ), OPTIONAL ::   ldwrt    ! open in write modeb          (default = .FALSE.)
132      INTEGER         , INTENT(in   ), OPTIONAL ::   kdom     ! Type of domain to be written (default = jpdom_local_noovlap)
133      INTEGER         , INTENT(in   ), OPTIONAL ::   kiolib   ! library used to open the file (default = jpnf90)
134      LOGICAL         , INTENT(in   ), OPTIONAL ::   ldstop   ! stop if open to read a non-existing file (default = .TRUE.)
135      LOGICAL         , INTENT(in   ), OPTIONAL ::   ldiof    ! Interp On the Fly, needed for AGRIF (default = .FALSE.)
136
137      CHARACTER(LEN=100)    ::   clname    ! the name of the file based on cdname [[+clcpu]+clcpu]
138      CHARACTER(LEN=100)    ::   cltmpn    ! tempory name to store clname (in writting mode)
139      CHARACTER(LEN=10)     ::   clsuffix  ! ".nc" or ".dimg"
140      CHARACTER(LEN=15)     ::   clcpu     ! the cpu number (max jpmax_digits digits)
141      CHARACTER(LEN=100)    ::   clinfo    ! info character
142      LOGICAL               ::   llok      ! check the existence
143      LOGICAL               ::   llwrt     ! local definition of ldwrt
144      LOGICAL               ::   llnoov    ! local definition to read overlap
145      LOGICAL               ::   llstop    ! local definition of ldstop
146      LOGICAL               ::   lliof     ! local definition of ldiof
147      INTEGER               ::   iolib     ! library do we use to open the file
148      INTEGER               ::   icnt      ! counter for digits in clcpu (max = jpmax_digits)
149      INTEGER               ::   iln, ils  ! lengths of character
150      INTEGER               ::   idom      ! type of domain
151      INTEGER               ::   istop     !
152      INTEGER, DIMENSION(2,5) ::   idompar ! domain parameters:
153      ! local number of points for x,y dimensions
154      ! position of first local point for x,y dimensions
155      ! position of last local point for x,y dimensions
156      ! start halo size for x,y dimensions
157      ! end halo size for x,y dimensions
158      !---------------------------------------------------------------------
159      ! Initializations and control
160      ! =============
161      kiomid = -1
162      clinfo = '                    iom_open ~~~  '
163      istop = nstop
164      ! if iom_open is called for the first time: initialize iom_file(:)%nfid to 0
165      ! (could be done when defining iom_file in f95 but not in f90)
166#if ! defined key_agrif
167      IF( iom_open_init == 0 ) THEN
168         iom_file(:)%nfid = 0
169         iom_open_init = 1
170      ENDIF
171#else
172      IF( Agrif_Root() ) THEN
173         IF( iom_open_init == 0 ) THEN
174            iom_file(:)%nfid = 0
175            iom_open_init = 1
176         ENDIF
177      ENDIF
178#endif
179      ! do we read or write the file?
180      IF( PRESENT(ldwrt) ) THEN   ;   llwrt = ldwrt
181      ELSE                        ;   llwrt = .FALSE.
182      ENDIF
183      ! do we call ctl_stop if we try to open a non-existing file in read mode?
184      IF( PRESENT(ldstop) ) THEN   ;   llstop = ldstop
185      ELSE                         ;   llstop = .TRUE.
186      ENDIF
187      ! what library do we use to open the file?
188      IF( PRESENT(kiolib) ) THEN   ;   iolib = kiolib
189      ELSE                         ;   iolib = jpnf90
190      ENDIF
191      ! are we using interpolation on the fly?
192      IF( PRESENT(ldiof) ) THEN   ;   lliof = ldiof
193      ELSE                        ;   lliof = .FALSE.
194      ENDIF
195      ! do we read the overlap
196      ! ugly patch SM+JMM+RB to overwrite global definition in some cases
197      llnoov = (jpni * jpnj ) == jpnij .AND. .NOT. lk_agrif 
198      ! create the file name by added, if needed, TRIM(Agrif_CFixed()) and TRIM(clsuffix)
199      ! =============
200      clname   = trim(cdname)
201#if defined key_agrif
202      IF ( .NOT. Agrif_Root() .AND. .NOT. lliof ) THEN
203         iln    = INDEX(clname,'/') 
204         cltmpn = clname(1:iln)
205         clname = clname(iln+1:LEN_TRIM(clname))
206         clname=TRIM(cltmpn)//TRIM(Agrif_CFixed())//'_'//TRIM(clname)
207      ENDIF
208#endif   
209      ! which suffix should we use?
210      SELECT CASE (iolib)
211      CASE (jpioipsl ) ;   clsuffix = '.nc'
212      CASE (jpnf90   ) ;   clsuffix = '.nc'
213      CASE (jprstdimg) ;   clsuffix = '.dimg'
214      CASE DEFAULT     ;   clsuffix = ''
215         CALL ctl_stop( TRIM(clinfo), 'accepted IO library are only jpioipsl, jpnf90 and jprstdimg' )
216      END SELECT
217      ! Add the suffix if needed
218      iln = LEN_TRIM(clname)
219      ils = LEN_TRIM(clsuffix)
220      IF( iln <= ils .OR. INDEX( TRIM(clname), TRIM(clsuffix), back = .TRUE. ) /= iln - ils + 1 )   &
221         &   clname = TRIM(clname)//TRIM(clsuffix)
222      cltmpn = clname   ! store this name
223      ! try to find if the file to be opened already exist
224      ! =============
225      INQUIRE( FILE = clname, EXIST = llok )
226      IF( .NOT.llok ) THEN
227         ! we try to add the cpu number to the name
228         IF( iolib == jprstdimg ) THEN   ;   WRITE(clcpu,*) narea
229         ELSE                            ;   WRITE(clcpu,*) narea-1
230         ENDIF
231         clcpu  = TRIM(ADJUSTL(clcpu))
232         iln = INDEX(clname,TRIM(clsuffix), back = .TRUE.)
233         clname = clname(1:iln-1)//'_'//TRIM(clcpu)//TRIM(clsuffix)
234         icnt = 0
235         INQUIRE( FILE = clname, EXIST = llok ) 
236         ! we try different formats for the cpu number by adding 0
237         DO WHILE( .NOT.llok .AND. icnt < jpmax_digits )
238            clcpu  = "0"//trim(clcpu)
239            clname = clname(1:iln-1)//'_'//TRIM(clcpu)//TRIM(clsuffix)
240            INQUIRE( FILE = clname, EXIST = llok )
241            icnt = icnt + 1
242         END DO
243      ENDIF
244      IF( llwrt ) THEN
245         ! check the domain definition
246! JMM + SM: ugly patch before getting the new version of lib_mpp)
247!         idom = jpdom_local_noovlap   ! default definition
248         IF( llnoov ) THEN   ;   idom = jpdom_local_noovlap   ! default definition
249         ELSE                ;   idom = jpdom_local_full      ! default definition
250         ENDIF
251         IF( PRESENT(kdom) )   idom = kdom
252         ! create the domain informations
253         ! =============
254         SELECT CASE (idom)
255         CASE (jpdom_local_full)
256            idompar(:,1) = (/ jpi             , jpj              /)
257            idompar(:,2) = (/ nimpp           , njmpp            /)
258            idompar(:,3) = (/ nimpp + jpi - 1 , njmpp + jpj - 1  /)
259            idompar(:,4) = (/ nldi - 1        , nldj - 1         /)
260            idompar(:,5) = (/ jpi - nlei      , jpj - nlej       /)
261         CASE (jpdom_local_noextra)
262            idompar(:,1) = (/ nlci            , nlcj             /)
263            idompar(:,2) = (/ nimpp           , njmpp            /)
264            idompar(:,3) = (/ nimpp + nlci - 1, njmpp + nlcj - 1 /)
265            idompar(:,4) = (/ nldi - 1        , nldj - 1         /)
266            idompar(:,5) = (/ nlci - nlei     , nlcj - nlej      /)
267         CASE (jpdom_local_noovlap)
268            idompar(:,1) = (/ nlei  - nldi + 1, nlej  - nldj + 1 /)
269            idompar(:,2) = (/ nimpp + nldi - 1, njmpp + nldj - 1 /)
270            idompar(:,3) = (/ nimpp + nlei - 1, njmpp + nlej - 1 /)
271            idompar(:,4) = (/ 0               , 0                /)
272            idompar(:,5) = (/ 0               , 0                /)
273         CASE DEFAULT
274            CALL ctl_stop( TRIM(clinfo), 'wrong value of kdom, only jpdom_local* cases are accepted' )
275         END SELECT
276      ENDIF
277      ! Open the NetCDF or RSTDIMG file
278      ! =============
279      ! do we have some free file identifier?
280      IF( MINVAL(iom_file(:)%nfid) /= 0 )   &
281         &   CALL ctl_stop( TRIM(clinfo), 'No more free file identifier', 'increase jpmax_files in iom_def' )
282      ! if no file was found...
283      IF( .NOT. llok ) THEN
284         IF( .NOT. llwrt ) THEN   ! we are in read mode
285            IF( llstop ) THEN   ;   CALL ctl_stop( TRIM(clinfo), 'File '//TRIM(cltmpn)//'* not found' )
286            ELSE                ;   istop = nstop + 1   ! make sure that istop /= nstop so we don't open the file
287            ENDIF
288         ELSE                     ! we are in write mode so we
289            clname = cltmpn       ! get back the file name without the cpu number
290         ENDIF
291      ENDIF
292      IF( istop == nstop ) THEN   ! no error within this routine
293         SELECT CASE (iolib)
294         CASE (jpioipsl )   ;   CALL iom_ioipsl_open(  clname, kiomid, llwrt, llok, idompar )
295         CASE (jpnf90   )   ;   CALL iom_nf90_open(    clname, kiomid, llwrt, llok, idompar )
296         CASE (jprstdimg)   ;   CALL iom_rstdimg_open( clname, kiomid, llwrt, llok, idompar )
297         CASE DEFAULT
298            CALL ctl_stop( TRIM(clinfo)//' accepted IO library are only jpioipsl, jpnf90 and jprstdimg' )
299         END SELECT
300      ENDIF
301      !
302   END SUBROUTINE iom_open
303
304
305   SUBROUTINE iom_close( kiomid )
306      !!--------------------------------------------------------------------
307      !!                   ***  SUBROUTINE  iom_close  ***
308      !!
309      !! ** Purpose : close an input file, or all files opened by iom
310      !!--------------------------------------------------------------------
311      INTEGER, INTENT(inout), OPTIONAL ::   kiomid   ! iom identifier of the file to be closed
312      !                                              ! return 0 when file is properly closed
313      !                                              ! No argument: all files opened by iom are closed
314
315      INTEGER ::   jf         ! dummy loop indices
316      INTEGER ::   i_s, i_e   ! temporary integer
317      CHARACTER(LEN=100)    ::   clinfo    ! info character
318      !---------------------------------------------------------------------
319      !
320      clinfo = '                    iom_close ~~~  '
321      IF( PRESENT(kiomid) ) THEN
322         i_s = kiomid
323         i_e = kiomid
324      ELSE
325         i_s = 1
326         i_e = jpmax_files
327#if defined key_iomput
328         CALL event__stop_ioserver
329#endif
330      ENDIF
331
332      IF( i_s > 0 ) THEN
333         DO jf = i_s, i_e
334            IF( iom_file(jf)%nfid > 0 ) THEN
335               SELECT CASE (iom_file(jf)%iolib)
336               CASE (jpioipsl )   ;   CALL iom_ioipsl_close(  jf )
337               CASE (jpnf90   )   ;   CALL iom_nf90_close(    jf )
338               CASE (jprstdimg)   ;   CALL iom_rstdimg_close( jf )
339               CASE DEFAULT
340                  CALL ctl_stop( TRIM(clinfo)//' accepted IO library are only jpioipsl, jpnf90 and jprstdimg' )
341               END SELECT
342               iom_file(jf)%nfid       = 0          ! free the id
343               IF( PRESENT(kiomid) )   kiomid = 0   ! return 0 as id to specify that the file was closed
344               IF(lwp) WRITE(numout,*) TRIM(clinfo)//' close file: '//TRIM(iom_file(jf)%name)//' ok'
345            ELSEIF( PRESENT(kiomid) ) THEN
346               WRITE(ctmp1,*) '--->',  kiomid
347               CALL ctl_stop( TRIM(clinfo)//' Invalid file identifier', ctmp1 )
348            ENDIF
349         END DO
350      ENDIF
351      !   
352   END SUBROUTINE iom_close
353
354
355   FUNCTION iom_varid ( kiomid, cdvar, kdimsz, ldstop ) 
356      !!-----------------------------------------------------------------------
357      !!                  ***  FUNCTION  iom_varid  ***
358      !!
359      !! ** Purpose : get the id of a variable in a file (return 0 if not found)
360      !!-----------------------------------------------------------------------
361      INTEGER              , INTENT(in   )           ::   kiomid   ! file Identifier
362      CHARACTER(len=*)     , INTENT(in   )           ::   cdvar    ! name of the variable
363      INTEGER, DIMENSION(:), INTENT(  out), OPTIONAL ::   kdimsz   ! size of the dimensions
364      LOGICAL              , INTENT(in   ), OPTIONAL ::   ldstop   ! stop if looking for non-existing variable (default = .TRUE.)
365      !
366      INTEGER                        ::   iom_varid, iiv, i_nvd
367      LOGICAL                        ::   ll_fnd
368      CHARACTER(LEN=100)             ::   clinfo                   ! info character
369      LOGICAL                        ::   llstop                   ! local definition of ldstop
370      !!-----------------------------------------------------------------------
371      iom_varid = 0                         ! default definition
372      ! do we call ctl_stop if we look for non-existing variable?
373      IF( PRESENT(ldstop) ) THEN   ;   llstop = ldstop
374      ELSE                         ;   llstop = .TRUE.
375      ENDIF
376      !
377      IF( kiomid > 0 ) THEN
378         clinfo = 'iom_varid, file: '//trim(iom_file(kiomid)%name)//', var: '//trim(cdvar)
379         IF( iom_file(kiomid)%nfid == 0 ) THEN
380            CALL ctl_stop( trim(clinfo), 'the file is not open' )
381         ELSE
382            ll_fnd  = .FALSE.
383            iiv = 0
384            !
385            DO WHILE ( .NOT.ll_fnd .AND. iiv < iom_file(kiomid)%nvars )
386               iiv = iiv + 1
387               ll_fnd  = ( TRIM(cdvar) == TRIM(iom_file(kiomid)%cn_var(iiv)) )
388            END DO
389            !
390            IF( .NOT.ll_fnd ) THEN
391               iiv = iiv + 1
392               IF( iiv <= jpmax_vars ) THEN
393                  SELECT CASE (iom_file(kiomid)%iolib)
394                  CASE (jpioipsl )   ;   iom_varid = iom_ioipsl_varid( kiomid, cdvar, iiv, kdimsz )
395                  CASE (jpnf90   )   ;   iom_varid = iom_nf90_varid  ( kiomid, cdvar, iiv, kdimsz )
396                  CASE (jprstdimg)   ;   iom_varid = -1   ! all variables are listed in iom_file
397                  CASE DEFAULT   
398                     CALL ctl_stop( TRIM(clinfo)//' accepted IO library are only jpioipsl, jpnf90 and jprstdimg' )
399                  END SELECT
400               ELSE
401                  CALL ctl_stop( trim(clinfo), 'Too many variables in the file '//iom_file(kiomid)%name,   &
402                        &                         'increase the parameter jpmax_vars')
403               ENDIF
404               IF( llstop .AND. iom_varid == -1 )   CALL ctl_stop( TRIM(clinfo)//' not found' ) 
405            ELSE
406               iom_varid = iiv
407               IF( PRESENT(kdimsz) ) THEN
408                  i_nvd = iom_file(kiomid)%ndims(iiv)
409                  IF( i_nvd == size(kdimsz) ) THEN
410                     kdimsz(:) = iom_file(kiomid)%dimsz(1:i_nvd,iiv)
411                  ELSE
412                     WRITE(ctmp1,*) i_nvd, size(kdimsz)
413                     CALL ctl_stop( trim(clinfo), 'error in kdimsz size'//trim(ctmp1) )
414                  ENDIF
415               ENDIF
416            ENDIF
417         ENDIF
418      ENDIF
419      !
420   END FUNCTION iom_varid
421
422
423   !!----------------------------------------------------------------------
424   !!                   INTERFACE iom_get
425   !!----------------------------------------------------------------------
426   SUBROUTINE iom_g0d( kiomid, cdvar, pvar )
427      INTEGER         , INTENT(in   )                 ::   kiomid    ! Identifier of the file
428      CHARACTER(len=*), INTENT(in   )                 ::   cdvar     ! Name of the variable
429      REAL(wp)        , INTENT(  out)                 ::   pvar      ! read field
430      !
431      INTEGER               :: idvar   ! variable id
432      !
433      IF( kiomid > 0 ) THEN
434         idvar = iom_varid( kiomid, cdvar )
435         IF( iom_file(kiomid)%nfid > 0 .AND. idvar > 0 ) THEN
436            SELECT CASE (iom_file(kiomid)%iolib)
437            CASE (jpioipsl )   ;   CALL iom_ioipsl_get(  kiomid, idvar, pvar )
438            CASE (jpnf90   )   ;   CALL iom_nf90_get(    kiomid, idvar, pvar )
439            CASE (jprstdimg)   ;   CALL iom_rstdimg_get( kiomid, idvar, pvar )
440            CASE DEFAULT   
441               CALL ctl_stop( 'iom_g0d: accepted IO library are only jpioipsl, jpnf90 and jprstdimg' )
442            END SELECT
443         ENDIF
444      ENDIF
445   END SUBROUTINE iom_g0d
446
447   SUBROUTINE iom_g1d( kiomid, kdom, cdvar, pvar, ktime, kstart, kcount )
448      INTEGER         , INTENT(in   )                         ::   kiomid    ! Identifier of the file
449      INTEGER         , INTENT(in   )                         ::   kdom      ! Type of domain to be read
450      CHARACTER(len=*), INTENT(in   )                         ::   cdvar     ! Name of the variable
451      REAL(wp)        , INTENT(  out), DIMENSION(:)           ::   pvar      ! read field
452      INTEGER         , INTENT(in   )              , OPTIONAL ::   ktime     ! record number
453      INTEGER         , INTENT(in   ), DIMENSION(1), OPTIONAL ::   kstart    ! start axis position of the reading
454      INTEGER         , INTENT(in   ), DIMENSION(1), OPTIONAL ::   kcount    ! number of points in each axis
455      !
456      IF( kiomid > 0 ) THEN
457         IF( iom_file(kiomid)%nfid > 0 ) CALL iom_get_123d( kiomid, kdom       , cdvar        , pv_r1d=pvar,   &
458              &                                                     ktime=ktime, kstart=kstart, kcount=kcount )
459      ENDIF
460   END SUBROUTINE iom_g1d
461
462   SUBROUTINE iom_g2d( kiomid, kdom, cdvar, pvar, ktime, kstart, kcount )
463      INTEGER         , INTENT(in   )                           ::   kiomid    ! Identifier of the file
464      INTEGER         , INTENT(in   )                           ::   kdom      ! Type of domain to be read
465      CHARACTER(len=*), INTENT(in   )                           ::   cdvar     ! Name of the variable
466      REAL(wp)        , INTENT(  out), DIMENSION(:,:)           ::   pvar      ! read field
467      INTEGER         , INTENT(in   )                , OPTIONAL ::   ktime     ! record number
468      INTEGER         , INTENT(in   ), DIMENSION(2)  , OPTIONAL ::   kstart    ! start axis position of the reading
469      INTEGER         , INTENT(in   ), DIMENSION(2)  , OPTIONAL ::   kcount    ! number of points in each axis
470      !
471      IF( kiomid > 0 ) THEN
472         IF( iom_file(kiomid)%nfid > 0 ) CALL iom_get_123d( kiomid, kdom       , cdvar        , pv_r2d=pvar,   &
473              &                                                     ktime=ktime, kstart=kstart, kcount=kcount )
474      ENDIF
475   END SUBROUTINE iom_g2d
476
477   SUBROUTINE iom_g3d( kiomid, kdom, cdvar, pvar, ktime, kstart, kcount )
478      INTEGER         , INTENT(in   )                             ::   kiomid    ! Identifier of the file
479      INTEGER         , INTENT(in   )                             ::   kdom      ! Type of domain to be read
480      CHARACTER(len=*), INTENT(in   )                             ::   cdvar     ! Name of the variable
481      REAL(wp)        , INTENT(  out), DIMENSION(:,:,:)           ::   pvar      ! read field
482      INTEGER         , INTENT(in   )                  , OPTIONAL ::   ktime     ! record number
483      INTEGER         , INTENT(in   ), DIMENSION(3)    , OPTIONAL ::   kstart    ! start axis position of the reading
484      INTEGER         , INTENT(in   ), DIMENSION(3)    , OPTIONAL ::   kcount    ! number of points in each axis
485      !
486      IF( kiomid > 0 ) THEN
487         IF( iom_file(kiomid)%nfid > 0 ) CALL iom_get_123d( kiomid, kdom       , cdvar        , pv_r3d=pvar,   &
488              &                                                     ktime=ktime, kstart=kstart, kcount=kcount )
489      ENDIF
490   END SUBROUTINE iom_g3d
491   !!----------------------------------------------------------------------
492
493   SUBROUTINE iom_get_123d( kiomid, kdom  , cdvar ,   &
494         &                  pv_r1d, pv_r2d, pv_r3d,   &
495         &                  ktime , kstart, kcount  )
496      !!-----------------------------------------------------------------------
497      !!                  ***  ROUTINE  iom_get_123d  ***
498      !!
499      !! ** Purpose : read a 1D/2D/3D variable
500      !!
501      !! ** Method : read ONE record at each CALL
502      !!-----------------------------------------------------------------------
503      INTEGER                    , INTENT(in   )           ::   kiomid     ! Identifier of the file
504      INTEGER                    , INTENT(in   )           ::   kdom       ! Type of domain to be read
505      CHARACTER(len=*)           , INTENT(in   )           ::   cdvar      ! Name of the variable
506      REAL(wp), DIMENSION(:)     , INTENT(  out), OPTIONAL ::   pv_r1d     ! read field (1D case)
507      REAL(wp), DIMENSION(:,:)   , INTENT(  out), OPTIONAL ::   pv_r2d     ! read field (2D case)
508      REAL(wp), DIMENSION(:,:,:) , INTENT(  out), OPTIONAL ::   pv_r3d     ! read field (3D case)
509      INTEGER                    , INTENT(in   ), OPTIONAL ::   ktime      ! record number
510      INTEGER , DIMENSION(:)     , INTENT(in   ), OPTIONAL ::   kstart     ! start position of the reading in each axis
511      INTEGER , DIMENSION(:)     , INTENT(in   ), OPTIONAL ::   kcount     ! number of points to be read in each axis
512      !
513      LOGICAL                        ::   llnoov      ! local definition to read overlap
514      INTEGER                        ::   jl          ! loop on number of dimension
515      INTEGER                        ::   idom        ! type of domain
516      INTEGER                        ::   idvar       ! id of the variable
517      INTEGER                        ::   inbdim      ! number of dimensions of the variable
518      INTEGER                        ::   idmspc      ! number of spatial dimensions
519      INTEGER                        ::   itime       ! record number
520      INTEGER                        ::   istop       ! temporary value of nstop
521      INTEGER                        ::   ix1, ix2, iy1, iy2   ! subdomain indexes
522      INTEGER                        ::   ji, jj      ! loop counters
523      INTEGER                        ::   irankpv       !
524      INTEGER                        ::   ind1, ind2  ! substring index
525      INTEGER, DIMENSION(jpmax_dims) ::   istart      ! starting point to read for each axis
526      INTEGER, DIMENSION(jpmax_dims) ::   icnt        ! number of value to read along each axis
527      INTEGER, DIMENSION(jpmax_dims) ::   idimsz      ! size of the dimensions of the variable
528      INTEGER, DIMENSION(jpmax_dims) ::   ishape      ! size of the dimensions of the variable
529      REAL(wp)                       ::   zscf, zofs  ! sacle_factor and add_offset
530      INTEGER                        ::   itmp        ! temporary integer
531      CHARACTER(LEN=100)             ::   clinfo      ! info character
532      CHARACTER(LEN=100)             ::   clname      ! file name
533      CHARACTER(LEN=1)               ::   clrankpv, cldmspc      !
534      !---------------------------------------------------------------------
535      !
536      clname = iom_file(kiomid)%name   !   esier to read
537      clinfo = '          iom_get_123d, file: '//trim(clname)//', var: '//trim(cdvar)
538      ! local definition of the domain ?
539      idom = kdom
540      ! do we read the overlap
541      ! ugly patch SM+JMM+RB to overwrite global definition in some cases
542      llnoov = (jpni * jpnj ) == jpnij .AND. .NOT. lk_agrif 
543      ! check kcount and kstart optionals parameters...
544      IF( PRESENT(kcount) .AND. (.NOT. PRESENT(kstart)) ) CALL ctl_stop(trim(clinfo), 'kcount present needs kstart present')
545      IF( PRESENT(kstart) .AND. (.NOT. PRESENT(kcount)) ) CALL ctl_stop(trim(clinfo), 'kstart present needs kcount present')
546      IF( PRESENT(kstart) .AND. idom /= jpdom_unknown   ) CALL ctl_stop(trim(clinfo), 'kstart present needs kdom = jpdom_unknown')
547
548      ! Search for the variable in the data base (eventually actualize data)
549      istop = nstop
550      idvar = iom_varid( kiomid, cdvar )
551      !
552      IF( idvar > 0 ) THEN
553         ! to write iom_file(kiomid)%dimsz in a shorter way !
554         idimsz(:) = iom_file(kiomid)%dimsz(:, idvar) 
555         inbdim = iom_file(kiomid)%ndims(idvar)            ! number of dimensions in the file
556         idmspc = inbdim                                   ! number of spatial dimensions in the file
557         IF( iom_file(kiomid)%luld(idvar) )   idmspc = inbdim - 1
558         IF( idmspc > 3 )   CALL ctl_stop(trim(clinfo), 'the file has more than 3 spatial dimensions this case is not coded...') 
559         !
560         ! update idom definition...
561         ! Identify the domain in case of jpdom_auto(glo/dta) definition
562         IF( idom == jpdom_autoglo .OR. idom == jpdom_autodta ) THEN           
563            IF( idom == jpdom_autoglo ) THEN   ;   idom = jpdom_global 
564            ELSE                               ;   idom = jpdom_data
565            ENDIF
566            ind1 = INDEX( clname, '_', back = .TRUE. ) + 1
567            ind2 = INDEX( clname, '.', back = .TRUE. ) - 1
568            IF( ind2 > ind1 ) THEN   ;   IF( VERIFY( clname(ind1:ind2), '0123456789' ) == 0 )   idom = jpdom_local   ;   ENDIF
569         ENDIF
570         ! Identify the domain in case of jpdom_local definition
571         IF( idom == jpdom_local ) THEN
572            IF(     idimsz(1) == jpi               .AND. idimsz(2) == jpj               ) THEN   ;   idom = jpdom_local_full
573            ELSEIF( idimsz(1) == nlci              .AND. idimsz(2) == nlcj              ) THEN   ;   idom = jpdom_local_noextra
574            ELSEIF( idimsz(1) == (nlei - nldi + 1) .AND. idimsz(2) == (nlej - nldj + 1) ) THEN   ;   idom = jpdom_local_noovlap
575            ELSE   ;   CALL ctl_stop( trim(clinfo), 'impossible to identify the local domain' )
576            ENDIF
577         ENDIF
578         !
579         ! check the consistency between input array and data rank in the file
580         !
581         ! initializations
582         itime = 1
583         IF( PRESENT(ktime) ) itime = ktime
584
585         irankpv = 1 * COUNT( (/PRESENT(pv_r1d)/) ) + 2 * COUNT( (/PRESENT(pv_r2d)/) ) + 3 * COUNT( (/PRESENT(pv_r3d)/) )
586         WRITE(clrankpv, fmt='(i1)') irankpv
587         WRITE(cldmspc , fmt='(i1)') idmspc
588         !
589         IF(     idmspc <  irankpv ) THEN
590            CALL ctl_stop( TRIM(clinfo), 'The file has only '//cldmspc//' spatial dimension',   &
591               &                         'it is impossible to read a '//clrankpv//'D array from this file...' )
592         ELSEIF( idmspc == irankpv ) THEN
593            IF( PRESENT(pv_r1d) .AND. idom /= jpdom_unknown )   &
594               &   CALL ctl_stop( TRIM(clinfo), 'case not coded...You must use jpdom_unknown' )
595         ELSEIF( idmspc >  irankpv ) THEN
596               IF( PRESENT(pv_r2d) .AND. itime == 1 .AND. idimsz(3) == 1 .AND. idmspc == 3 ) THEN
597                  CALL ctl_warn( trim(clinfo), '2D array but 3 spatial dimensions for the data...'              ,   &
598                        &         'As the size of the z dimension is 1 and as we try to read the first record, ',   &
599                        &         'we accept this case, even if there is a possible mix-up between z and time dimension' )   
600                  idmspc = idmspc - 1
601               ELSE
602                  CALL ctl_stop( TRIM(clinfo), 'To keep iom lisibility, when reading a '//clrankpv//'D array,'         ,   &
603                     &                         'we do not accept data with more than '//cldmspc//' spatial dimension',   &
604                     &                         'Use ncwa -a to suppress the unnecessary dimensions' )
605               ENDIF
606         ENDIF
607
608         !
609         ! definition of istart and icnt
610         !
611         icnt  (:) = 1
612         istart(:) = 1
613         istart(idmspc+1) = itime
614
615         IF(              PRESENT(kstart)       ) THEN ; istart(1:idmspc) = kstart(1:idmspc) ; icnt(1:idmspc) = kcount(1:idmspc)
616         ELSE
617            IF(           idom == jpdom_unknown ) THEN                                       ; icnt(1:idmspc) = idimsz(1:idmspc)
618            ELSE
619               IF( .NOT. PRESENT(pv_r1d) ) THEN   !   not a 1D array
620                  IF(     idom == jpdom_data    ) THEN ; istart(1:2) = (/ mig(1), mjg(1) /)  ! icnt(1:2) done bellow
621                  ELSEIF( idom == jpdom_global  ) THEN ; istart(1:2) = (/ nimpp , njmpp  /)  ! icnt(1:2) done bellow
622                  ENDIF
623                  ! we do not read the overlap                     -> we start to read at nldi, nldj
624! JMM + SM: ugly patch before getting the new version of lib_mpp)
625!                  IF( idom /= jpdom_local_noovlap )   istart(1:2) = istart(1:2) + (/ nldi - 1, nldj - 1 /)
626                  IF( llnoov .AND. idom /= jpdom_local_noovlap ) istart(1:2) = istart(1:2) + (/ nldi - 1, nldj - 1 /)
627                  ! we do not read the overlap and the extra-halos -> from nldi to nlei and from nldj to nlej
628! JMM + SM: ugly patch before getting the new version of lib_mpp)
629!                  icnt(1:2) = (/ nlei - nldi + 1, nlej - nldj + 1 /)
630                  IF( llnoov ) THEN   ;   icnt(1:2) = (/ nlei - nldi + 1, nlej - nldj + 1 /)
631                  ELSE                ;   icnt(1:2) = (/ nlci           , nlcj            /)
632                  ENDIF
633                  IF( PRESENT(pv_r3d) ) THEN
634                     IF( idom == jpdom_data ) THEN   ; icnt(3) = jpkdta
635                     ELSE                            ; icnt(3) = jpk
636                     ENDIF
637                  ENDIF
638               ENDIF
639            ENDIF
640         ENDIF
641
642         ! check that istart and icnt can be used with this file
643         !-
644         DO jl = 1, jpmax_dims
645            itmp = istart(jl)+icnt(jl)-1
646            IF( itmp > idimsz(jl) .AND. idimsz(jl) /= 0 ) THEN
647               WRITE( ctmp1, FMT="('(istart(', i1, ') + icnt(', i1, ') - 1) = ', i5)" ) jl, jl, itmp
648               WRITE( ctmp2, FMT="(' is larger than idimsz(', i1,') = ', i5)"         ) jl, idimsz(jl)
649               CALL ctl_stop( trim(clinfo), 'start and count too big regarding to the size of the data, ', ctmp1, ctmp2 )     
650            ENDIF
651         END DO
652
653         ! check that icnt matches the input array
654         !-     
655         IF( idom == jpdom_unknown ) THEN
656            IF( irankpv == 1 )        ishape(1:1) = SHAPE(pv_r1d)
657            IF( irankpv == 2 )        ishape(1:2) = SHAPE(pv_r2d)
658            IF( irankpv == 3 )        ishape(1:3) = SHAPE(pv_r3d)
659            ctmp1 = 'd'
660         ELSE
661            IF( irankpv == 2 ) THEN
662! JMM + SM: ugly patch before getting the new version of lib_mpp)
663!               ishape(1:2) = SHAPE(pv_r2d(nldi:nlei,nldj:nlej  ))   ;   ctmp1 = 'd(nldi:nlei,nldj:nlej)'
664               IF( llnoov ) THEN ; ishape(1:2)=SHAPE(pv_r2d(nldi:nlei,nldj:nlej  )) ; ctmp1='d(nldi:nlei,nldj:nlej)'
665               ELSE              ; ishape(1:2)=SHAPE(pv_r2d(1   :nlci,1   :nlcj  )) ; ctmp1='d(1:nlci,1:nlcj)'
666               ENDIF
667            ENDIF
668            IF( irankpv == 3 ) THEN 
669! JMM + SM: ugly patch before getting the new version of lib_mpp)
670!               ishape(1:3) = SHAPE(pv_r3d(nldi:nlei,nldj:nlej,:))   ;   ctmp1 = 'd(nldi:nlei,nldj:nlej,:)'
671               IF( llnoov ) THEN ; ishape(1:3)=SHAPE(pv_r3d(nldi:nlei,nldj:nlej,:)) ; ctmp1='d(nldi:nlei,nldj:nlej,:)'
672               ELSE              ; ishape(1:3)=SHAPE(pv_r3d(1   :nlci,1   :nlcj,:)) ; ctmp1='d(1:nlci,1:nlcj,:)'
673               ENDIF
674            ENDIF
675         ENDIF
676         
677         DO jl = 1, irankpv
678            WRITE( ctmp2, FMT="(', ', i1,'): ', i5,' /= icnt(', i1,'):', i5)" ) jl, ishape(jl), jl, icnt(jl)
679            IF( ishape(jl) /= icnt(jl) )   CALL ctl_stop( TRIM(clinfo), 'size(pv_r'//clrankpv//TRIM(ctmp1)//TRIM(ctmp2) )
680         END DO
681
682      ENDIF
683
684      ! read the data
685      !-     
686      IF( idvar > 0 .AND. istop == nstop ) THEN   ! no additional errors until this point...
687         !
688         ! find the right index of the array to be read
689! JMM + SM: ugly patch before getting the new version of lib_mpp)
690!         IF( idom /= jpdom_unknown ) THEN   ;   ix1 = nldi   ;   ix2 = nlei      ;   iy1 = nldj   ;   iy2 = nlej
691!         ELSE                               ;   ix1 = 1      ;   ix2 = icnt(1)   ;   iy1 = 1      ;   iy2 = icnt(2)
692!         ENDIF
693         IF( llnoov ) THEN
694            IF( idom /= jpdom_unknown ) THEN   ;   ix1 = nldi   ;   ix2 = nlei      ;   iy1 = nldj   ;   iy2 = nlej
695            ELSE                               ;   ix1 = 1      ;   ix2 = icnt(1)   ;   iy1 = 1      ;   iy2 = icnt(2)
696            ENDIF
697         ELSE
698            IF( idom /= jpdom_unknown ) THEN   ;   ix1 = 1      ;   ix2 = nlci      ;   iy1 = 1      ;   iy2 = nlcj
699            ELSE                               ;   ix1 = 1      ;   ix2 = icnt(1)   ;   iy1 = 1      ;   iy2 = icnt(2)
700            ENDIF
701         ENDIF
702     
703         SELECT CASE (iom_file(kiomid)%iolib)
704         CASE (jpioipsl )   ;   CALL iom_ioipsl_get(  kiomid, idvar, inbdim, istart, icnt, ix1, ix2, iy1, iy2,   &
705            &                                         pv_r1d, pv_r2d, pv_r3d )
706         CASE (jpnf90   )   ;   CALL iom_nf90_get(    kiomid, idvar, inbdim, istart, icnt, ix1, ix2, iy1, iy2,   &
707            &                                         pv_r1d, pv_r2d, pv_r3d )
708         CASE (jprstdimg)   ;   CALL iom_rstdimg_get( kiomid, idom, idvar, ix1, ix2, iy1, iy2,   &
709            &                                         pv_r1d, pv_r2d, pv_r3d )
710         CASE DEFAULT   
711            CALL ctl_stop( TRIM(clinfo)//' accepted IO library are only jpioipsl, jpnf90 and jprstdimg' )
712         END SELECT
713
714         IF( istop == nstop ) THEN   ! no additional errors until this point...
715            IF(lwp) WRITE(numout,"(10x,' read ',a,' (rec: ',i4,') in ',a,' ok')") TRIM(cdvar), itime, TRIM(iom_file(kiomid)%name)
716         
717            !--- overlap areas and extra hallows (mpp)
718            IF(     PRESENT(pv_r2d) .AND. idom /= jpdom_unknown ) THEN
719               CALL lbc_lnk( pv_r2d,'Z',-999.,'no0' )
720            ELSEIF( PRESENT(pv_r3d) .AND. idom /= jpdom_unknown ) THEN
721               ! this if could be simplified with the new lbc_lnk that works with any size of the 3rd dimension
722               IF( icnt(3) == jpk ) THEN
723                  CALL lbc_lnk( pv_r3d,'Z',-999.,'no0' )
724               ELSE   ! put some arbitrary value (a call to lbc_lnk will be done later...)
725                  DO jj = nlcj+1, jpj   ;   pv_r3d(1:nlci, jj, :) = pv_r3d(1:nlci, nlej, :)   ;   END DO
726                  DO ji = nlci+1, jpi   ;   pv_r3d(ji    , : , :) = pv_r3d(nlei  , :   , :)   ;   END DO
727               ENDIF
728            ENDIF
729           
730            !--- Apply scale_factor and offset
731            zscf = iom_file(kiomid)%scf(idvar)      ! scale factor
732            zofs = iom_file(kiomid)%ofs(idvar)      ! offset
733            IF(     PRESENT(pv_r1d) ) THEN
734               IF( zscf /= 1. )   pv_r1d(:) = pv_r1d(:) * zscf 
735               IF( zofs /= 0. )   pv_r1d(:) = pv_r1d(:) + zofs
736            ELSEIF( PRESENT(pv_r2d) ) THEN
737!CDIR COLLAPSE
738               IF( zscf /= 1.)   pv_r2d(:,:) = pv_r2d(:,:) * zscf
739!CDIR COLLAPSE
740               IF( zofs /= 0.)   pv_r2d(:,:) = pv_r2d(:,:) + zofs
741            ELSEIF( PRESENT(pv_r3d) ) THEN
742!CDIR COLLAPSE
743               IF( zscf /= 1.)   pv_r3d(:,:,:) = pv_r3d(:,:,:) * zscf
744!CDIR COLLAPSE
745               IF( zofs /= 0.)   pv_r3d(:,:,:) = pv_r3d(:,:,:) + zofs
746            ENDIF
747            !
748         ENDIF
749         !
750      ENDIF
751      !
752   END SUBROUTINE iom_get_123d
753
754
755   SUBROUTINE iom_gettime( kiomid, ptime, cdvar, kntime, cdunits, cdcalendar )
756      !!--------------------------------------------------------------------
757      !!                   ***  SUBROUTINE iom_gettime  ***
758      !!
759      !! ** Purpose : read the time axis cdvar in the file
760      !!--------------------------------------------------------------------
761      INTEGER                    , INTENT(in   ) ::   kiomid     ! file Identifier
762      REAL(wp), DIMENSION(:)     , INTENT(  out) ::   ptime      ! the time axis
763      CHARACTER(len=*), OPTIONAL , INTENT(in   ) ::   cdvar      ! time axis name
764      INTEGER         , OPTIONAL , INTENT(  out) ::   kntime     ! number of times in file
765      CHARACTER(len=*), OPTIONAL , INTENT(  out) ::   cdunits    ! units attribute of time coordinate
766      CHARACTER(len=*), OPTIONAL , INTENT(  out) ::   cdcalendar ! calendar attribute of
767      !
768      INTEGER, DIMENSION(1) :: kdimsz
769      INTEGER            ::   idvar    ! id of the variable
770      CHARACTER(LEN=32)  ::   tname    ! local name of time coordinate
771      CHARACTER(LEN=100) ::   clinfo   ! info character
772      !---------------------------------------------------------------------
773      !
774      IF ( PRESENT(cdvar) ) THEN
775         tname = cdvar
776      ELSE
777         tname = iom_file(kiomid)%uldname
778      ENDIF
779      IF( kiomid > 0 ) THEN
780         clinfo = 'iom_gettime, file: '//trim(iom_file(kiomid)%name)//', var: '//trim(tname)
781         IF ( PRESENT(kntime) ) THEN
782            idvar  = iom_varid( kiomid, tname, kdimsz = kdimsz )
783            kntime = kdimsz(1)
784         ELSE
785            idvar = iom_varid( kiomid, tname )
786         ENDIF
787         !
788         ptime(:) = 0. ! default definition
789         IF( idvar > 0 ) THEN
790            IF( iom_file(kiomid)%ndims(idvar) == 1 ) THEN
791               IF( iom_file(kiomid)%luld(idvar) ) THEN
792                  IF( iom_file(kiomid)%dimsz(1,idvar) == size(ptime) ) THEN
793                     SELECT CASE (iom_file(kiomid)%iolib)
794                     CASE (jpioipsl )   ;   CALL iom_ioipsl_gettime( kiomid, idvar, ptime, cdunits, cdcalendar )
795                     CASE (jpnf90   )   ;   CALL iom_nf90_gettime(   kiomid, idvar, ptime, cdunits, cdcalendar )
796                     CASE (jprstdimg)   ;   CALL ctl_stop( TRIM(clinfo)//' case IO library == jprstdimg not coded...' )
797                     CASE DEFAULT   
798                        CALL ctl_stop( TRIM(clinfo)//' accepted IO library are only jpioipsl, jpnf90 and jprstdimg' )
799                     END SELECT
800                  ELSE
801                     WRITE(ctmp1,*) 'error with the size of ptime ',size(ptime),iom_file(kiomid)%dimsz(1,idvar)
802                     CALL ctl_stop( trim(clinfo), trim(ctmp1) )
803                  ENDIF
804               ELSE
805                  CALL ctl_stop( trim(clinfo), 'variable dimension is not unlimited... use iom_get' )
806               ENDIF
807            ELSE
808               CALL ctl_stop( trim(clinfo), 'the variable has more than 1 dimension' )
809            ENDIF
810         ELSE
811            CALL ctl_stop( trim(clinfo), 'variable not found in '//iom_file(kiomid)%name )
812         ENDIF
813      ENDIF
814      !
815   END SUBROUTINE iom_gettime
816
817
818   !!----------------------------------------------------------------------
819   !!                   INTERFACE iom_rstput
820   !!----------------------------------------------------------------------
821   SUBROUTINE iom_rp0d( kt, kwrite, kiomid, cdvar, pvar, ktype )
822      INTEGER         , INTENT(in)                         ::   kt       ! ocean time-step
823      INTEGER         , INTENT(in)                         ::   kwrite   ! writing time-step
824      INTEGER         , INTENT(in)                         ::   kiomid   ! Identifier of the file
825      CHARACTER(len=*), INTENT(in)                         ::   cdvar    ! time axis name
826      REAL(wp)        , INTENT(in)                         ::   pvar     ! written field
827      INTEGER         , INTENT(in), OPTIONAL               ::   ktype    ! variable external type
828      INTEGER :: ivid   ! variable id
829      IF( kiomid > 0 ) THEN
830         IF( iom_file(kiomid)%nfid > 0 ) THEN
831            ivid = iom_varid( kiomid, cdvar, ldstop = .FALSE. )
832            SELECT CASE (iom_file(kiomid)%iolib)
833            CASE (jpioipsl )   ;   CALL iom_ioipsl_rstput( kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r0d = pvar )
834            CASE (jpnf90   )   ;   CALL iom_nf90_rstput(   kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r0d = pvar )
835            CASE (jprstdimg)   ;   IF( kt == kwrite )    CALL iom_rstdimg_rstput( kiomid, cdvar, ivid, pvar )
836            CASE DEFAULT     
837               CALL ctl_stop( 'iom_rp0d: accepted IO library are only jpioipsl, jpnf90 and jprstdimg' )
838            END SELECT
839         ENDIF
840      ENDIF
841   END SUBROUTINE iom_rp0d
842
843   SUBROUTINE iom_rp1d( kt, kwrite, kiomid, cdvar, pvar, ktype )
844      INTEGER         , INTENT(in)                         ::   kt       ! ocean time-step
845      INTEGER         , INTENT(in)                         ::   kwrite   ! writing time-step
846      INTEGER         , INTENT(in)                         ::   kiomid   ! Identifier of the file
847      CHARACTER(len=*), INTENT(in)                         ::   cdvar    ! time axis name
848      REAL(wp)        , INTENT(in), DIMENSION(        jpk) ::   pvar     ! written field
849      INTEGER         , INTENT(in), OPTIONAL               ::   ktype    ! variable external type
850      INTEGER :: ivid   ! variable id
851      IF( kiomid > 0 ) THEN
852         IF( iom_file(kiomid)%nfid > 0 ) THEN
853            ivid = iom_varid( kiomid, cdvar, ldstop = .FALSE. )
854            SELECT CASE (iom_file(kiomid)%iolib)
855            CASE (jpioipsl )   ;   CALL iom_ioipsl_rstput( kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r1d = pvar )
856            CASE (jpnf90   )   ;   CALL iom_nf90_rstput(   kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r1d = pvar )
857            CASE (jprstdimg)   ;   IF( kt == kwrite )    CALL iom_rstdimg_rstput( kiomid, cdvar, ivid, pv_r1d = pvar )
858            CASE DEFAULT     
859               CALL ctl_stop( 'iom_rp1d: accepted IO library are only jpioipsl, jpnf90 and jprstdimg' )
860            END SELECT
861         ENDIF
862      ENDIF
863   END SUBROUTINE iom_rp1d
864
865   SUBROUTINE iom_rp2d( kt, kwrite, kiomid, cdvar, pvar, ktype )
866      INTEGER         , INTENT(in)                         ::   kt       ! ocean time-step
867      INTEGER         , INTENT(in)                         ::   kwrite   ! writing time-step
868      INTEGER         , INTENT(in)                         ::   kiomid   ! Identifier of the file
869      CHARACTER(len=*), INTENT(in)                         ::   cdvar    ! time axis name
870      REAL(wp)        , INTENT(in), DIMENSION(jpi,jpj    ) ::   pvar     ! written field
871      INTEGER         , INTENT(in), OPTIONAL               ::   ktype    ! variable external type
872      INTEGER :: ivid   ! variable id
873      IF( kiomid > 0 ) THEN
874         IF( iom_file(kiomid)%nfid > 0 ) THEN
875            ivid = iom_varid( kiomid, cdvar, ldstop = .FALSE. )
876            SELECT CASE (iom_file(kiomid)%iolib)
877            CASE (jpioipsl )   ;   CALL iom_ioipsl_rstput( kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r2d = pvar )
878            CASE (jpnf90   )   ;   CALL iom_nf90_rstput(   kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r2d = pvar )
879            CASE (jprstdimg)   ;   IF( kt == kwrite )   CALL iom_rstdimg_rstput( kiomid, cdvar, ivid, pv_r2d = pvar ) 
880            CASE DEFAULT     
881               CALL ctl_stop( 'iom_rp2d: accepted IO library are only jpioipsl, jpnf90 and jprstdimg' )
882            END SELECT
883         ENDIF
884      ENDIF
885   END SUBROUTINE iom_rp2d
886
887   SUBROUTINE iom_rp3d( kt, kwrite, kiomid, cdvar, pvar, ktype )
888      INTEGER         , INTENT(in)                         ::   kt       ! ocean time-step
889      INTEGER         , INTENT(in)                         ::   kwrite   ! writing time-step
890      INTEGER         , INTENT(in)                         ::   kiomid   ! Identifier of the file
891      CHARACTER(len=*), INTENT(in)                         ::   cdvar    ! time axis name
892      REAL(wp)        , INTENT(in), DIMENSION(jpi,jpj,jpk) ::   pvar     ! written field
893      INTEGER         , INTENT(in), OPTIONAL               ::   ktype    ! variable external type
894      INTEGER :: ivid   ! variable id
895      IF( kiomid > 0 ) THEN
896         IF( iom_file(kiomid)%nfid > 0 ) THEN
897            ivid = iom_varid( kiomid, cdvar, ldstop = .FALSE. )
898            SELECT CASE (iom_file(kiomid)%iolib)
899            CASE (jpioipsl )   ;   CALL iom_ioipsl_rstput( kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r3d = pvar )
900            CASE (jpnf90   )   ;   CALL iom_nf90_rstput(   kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r3d = pvar )
901            CASE (jprstdimg)   ;   IF( kt == kwrite )   CALL iom_rstdimg_rstput( kiomid, cdvar, ivid, pv_r3d = pvar )
902            CASE DEFAULT     
903               CALL ctl_stop( 'iom_rp3d: accepted IO library are only jpioipsl and jprstdimg' )
904            END SELECT
905         ENDIF
906      ENDIF
907   END SUBROUTINE iom_rp3d
908
909
910   !!----------------------------------------------------------------------
911   !!                   INTERFACE iom_put
912   !!----------------------------------------------------------------------
913   SUBROUTINE iom_p0d( cdname, pfield0d )
914      CHARACTER(LEN=*), INTENT(in) ::   cdname
915      REAL(wp)        , INTENT(in) ::   pfield0d
916#if defined key_iomput
917      CALL event__write_field2D( cdname, RESHAPE( (/pfield0d/), (/1,1/) ) )
918#else
919      IF( .FALSE. )   WRITE(numout,*) cdname, pfield0d   ! useless test to avoid compilation warnings
920#endif
921   END SUBROUTINE iom_p0d
922
923   SUBROUTINE iom_p2d( cdname, pfield2d )
924      CHARACTER(LEN=*)            , INTENT(in) ::   cdname
925      REAL(wp), DIMENSION(jpi,jpj), INTENT(in) ::   pfield2d
926#if defined key_iomput
927      CALL event__write_field2D( cdname, pfield2d(nldi:nlei, nldj:nlej) )
928#else
929      IF( .FALSE. )   WRITE(numout,*) cdname, pfield2d   ! useless test to avoid compilation warnings
930#endif
931   END SUBROUTINE iom_p2d
932
933   SUBROUTINE iom_p3d( cdname, pfield3d )
934      CHARACTER(LEN=*)                , INTENT(in) ::   cdname
935      REAL(wp), DIMENSION(jpi,jpj,jpk), INTENT(in) ::   pfield3d
936#if defined key_iomput
937      CALL event__write_field3D( cdname, pfield3d(nldi:nlei, nldj:nlej, :) )
938#else
939      IF( .FALSE. )   WRITE(numout,*) cdname, pfield3d   ! useless test to avoid compilation warnings
940#endif
941   END SUBROUTINE iom_p3d
942   !!----------------------------------------------------------------------
943
944
945#if defined key_iomput
946
947   SUBROUTINE set_grid( cdname, plon, plat )
948      !!----------------------------------------------------------------------
949      !!                     ***  ROUTINE   ***
950      !!
951      !! ** Purpose :   define horizontal grids
952      !!
953      !!----------------------------------------------------------------------
954      CHARACTER(LEN=*)            , INTENT(in) ::   cdname
955      REAL(wp), DIMENSION(jpi,jpj), INTENT(in) ::   plon
956      REAL(wp), DIMENSION(jpi,jpj), INTENT(in) ::   plat
957
958      CALL event__set_grid_dimension( cdname, jpiglo, jpjglo)
959      CALL event__set_grid_domain( cdname, nlei-nldi+1, nlej-nldj+1, nimpp+nldi-1, njmpp+nldj-1, &
960         &                         plon(nldi:nlei, nldj:nlej), plat(nldi:nlei, nldj:nlej) )
961      CALL event__set_grid_type_nemo( cdname )
962
963   END SUBROUTINE set_grid
964
965
966   SUBROUTINE set_scalar
967      !!----------------------------------------------------------------------
968      !!                     ***  ROUTINE   ***
969      !!
970      !! ** Purpose :   define fake grids for scalar point
971      !!
972      !!----------------------------------------------------------------------
973      REAL(wp), DIMENSION(1,1) ::   zz = 1.
974      !!----------------------------------------------------------------------
975      CALL event__set_grid_dimension( 'scalarpoint', jpnij, 1)
976      CALL event__set_grid_domain   ( 'scalarpoint', 1, 1, narea, 1, zz, zz )
977      CALL event__set_grid_type_nemo( 'scalarpoint' )
978
979   END SUBROUTINE set_scalar
980
981
982   SUBROUTINE set_xmlatt
983      !!----------------------------------------------------------------------
984      !!                     ***  ROUTINE   ***
985      !!
986      !! ** Purpose :   automatic definitions of some of the xml attributs...
987      !!
988      !!----------------------------------------------------------------------
989      CHARACTER(len=6),DIMENSION( 8) ::   clsuff                   ! suffix name
990      CHARACTER(len=1),DIMENSION( 3) ::   clgrd                    ! suffix name
991      CHARACTER(len=50)              ::   clname                   ! file name
992      CHARACTER(len=1)               ::   cl1                      ! 1 character
993      CHARACTER(len=2)               ::   cl2                      ! 1 character
994      INTEGER                        ::   idt                      ! time-step in seconds
995      INTEGER                        ::   iddss, ihhss             ! number of seconds in 1 day, 1 hour and 1 year
996      INTEGER                        ::   iyymo                    ! number of months in 1 year
997      INTEGER                        ::   jg, jh, jd, jm, jy       ! loop counters
998      INTEGER                        ::   ix, iy                   ! i-,j- index
999      REAL(wp)        ,DIMENSION(11) ::   zlontao                  ! longitudes of tao    moorings
1000      REAL(wp)        ,DIMENSION( 7) ::   zlattao                  ! latitudes  of tao    moorings
1001      REAL(wp)        ,DIMENSION( 4) ::   zlonrama                 ! longitudes of rama   moorings
1002      REAL(wp)        ,DIMENSION(11) ::   zlatrama                 ! latitudes  of rama   moorings
1003      REAL(wp)        ,DIMENSION( 3) ::   zlonpira                 ! longitudes of pirata moorings
1004      REAL(wp)        ,DIMENSION( 9) ::   zlatpira                 ! latitudes  of pirata moorings
1005      !!----------------------------------------------------------------------
1006      !
1007      idt   = NINT( rdttra(1)     )
1008      iddss = NINT( rday          )                                         ! number of seconds in 1 day
1009      ihhss = NINT( rmmss * rhhmm )                                         ! number of seconds in 1 hour
1010      iyymo = NINT( raamo         )                                         ! number of months in 1 year
1011
1012      ! frequency of the call of iom_put (attribut: freq_op)
1013      CALL event__set_attribut( 'field_definition', attr( field__freq_op, idt           ) )    ! model time-step
1014      CALL event__set_attribut( 'SBC'             , attr( field__freq_op, idt * nn_fsbc ) )    ! SBC time-step
1015     
1016      ! output file names (attribut: name)
1017      clsuff(:) = (/ 'grid_T', 'grid_U', 'grid_V', 'grid_W', 'icemod', 'ptrc_T', 'diad_T', 'scalar' /)     
1018      DO jg = 1, SIZE(clsuff)                                                                  ! grid type
1019         DO jh = 1, 12                                                                         ! 1, 2, 3, 4, 6, 12 hours
1020            IF( MOD(12,jh) == 0 ) THEN
1021               WRITE(cl2,'(i2)') jh 
1022               CALL dia_nam( clname, jh * ihhss, clsuff(jg), ldfsec = .TRUE. )
1023               CALL event__set_attribut( TRIM(ADJUSTL(cl2))//'h_'//clsuff(jg), attr( file__name, TRIM(clname) ) )
1024            ENDIF
1025         END DO
1026         DO jd = 1, 5, 2                                                                       ! 1, 3, 5 days
1027            WRITE(cl1,'(i1)') jd 
1028            CALL dia_nam( clname, jd * iddss, clsuff(jg), ldfsec = .TRUE. )
1029            CALL event__set_attribut( cl1//'d_'//clsuff(jg), attr( file__name, TRIM(clname) ) )
1030         END DO
1031         DO jm = 1, 6                                                                          ! 1, 2, 3, 4, 6 months
1032            IF( MOD(6,jm) == 0 ) THEN
1033               WRITE(cl1,'(i1)') jm 
1034               CALL dia_nam( clname, -jm, clsuff(jg) )
1035               CALL event__set_attribut( cl1//'m_'//clsuff(jg), attr( file__name, TRIM(clname) ) )
1036            ENDIF
1037         END DO
1038         DO jy = 1, 10                                                                         ! 1, 2, 5, 10 years 
1039            IF( MOD(10,jy) == 0 ) THEN
1040               WRITE(cl2,'(i2)') jy 
1041               CALL dia_nam( clname, -jy * iyymo, clsuff(jg) )
1042               CALL event__set_attribut( TRIM(ADJUSTL(cl2))//'y_'//clsuff(jg), attr( file__name, TRIM(clname) ) )
1043            ENDIF
1044         END DO
1045      END DO
1046
1047      ! Zooms...
1048      clgrd = (/ 'T', 'U', 'W' /) 
1049      DO jg = 1, SIZE(clgrd)                                                                   ! grid type
1050         cl1 = clgrd(jg)
1051         ! Equatorial section (attributs: jbegin, ni, name_suffix)
1052         CALL dom_ngb( 0., 0., ix, iy, cl1 )
1053         CALL event__set_attribut( 'Eq'//cl1, attr( zoom__jbegin     , iy     ) )
1054         CALL event__set_attribut( 'Eq'//cl1, attr( zoom__ni         , jpiglo ) )
1055         CALL event__set_attribut( 'Eq'//cl1, attr( file__name_suffix, '_Eq'  ) )
1056      END DO
1057      ! TAO moorings (attributs: ibegin, jbegin, name_suffix)
1058      zlontao = (/ 137.0, 147.0, 156.0, 165.0, -180.0, -170.0, -155.0, -140.0, -125.0, -110.0, -95.0 /)
1059      zlattao = (/  -8.0,  -5.0,  -2.0,   0.0,    2.0,    5.0,    8.0 /)
1060      CALL set_mooring( zlontao, zlattao )
1061      ! RAMA moorings (attributs: ibegin, jbegin, name_suffix)
1062      zlonrama = (/  55.0,  67.0, 80.5, 90.0 /)
1063      zlatrama = (/ -16.0, -12.0, -8.0, -4.0, -1.5, 0.0, 1.5, 4.0, 8.0, 12.0, 15.0 /)
1064      CALL set_mooring( zlonrama, zlatrama )
1065      ! PIRATA moorings (attributs: ibegin, jbegin, name_suffix)
1066      zlonpira = (/ -38.0, -23.0, -10.0 /)
1067      zlatpira = (/ -19.0, -14.0,  -8.0, 0.0, 4.0, 8.0, 12.0, 15.0, 20.0 /)
1068      CALL set_mooring( zlonpira, zlatpira )
1069     
1070   END SUBROUTINE set_xmlatt
1071
1072
1073   SUBROUTINE set_mooring( plon, plat)
1074      !!----------------------------------------------------------------------
1075      !!                     ***  ROUTINE   ***
1076      !!
1077      !! ** Purpose :   automatic definitions of moorings xml attributs...
1078      !!
1079      !!----------------------------------------------------------------------
1080      REAL(wp), DIMENSION(:), INTENT(in) ::  plon, plat           ! longitudes/latitudes oft the mooring
1081      !
1082!!$      CHARACTER(len=1),DIMENSION(4) ::   clgrd = (/ 'T', 'U', 'V', 'W' /)   ! suffix name
1083      CHARACTER(len=1),DIMENSION(1) ::   clgrd = (/ 'T' /)        ! suffix name
1084      CHARACTER(len=50)             ::   clname                   ! file name
1085      CHARACTER(len=1)              ::   cl1                      ! 1 character
1086      CHARACTER(len=6)              ::   clon,clat                ! name of longitude, latitude
1087      INTEGER                       ::   ji, jj, jg               ! loop counters
1088      INTEGER                       ::   ix, iy                   ! i-,j- index
1089      REAL(wp)                      ::   zlon, zlat
1090      !!----------------------------------------------------------------------
1091      DO jg = 1, SIZE(clgrd)
1092         cl1 = clgrd(jg)
1093         DO ji = 1, SIZE(plon)
1094            DO jj = 1, SIZE(plat)
1095               zlon = plon(ji)
1096               zlat = plat(jj)
1097               ! modifications for RAMA moorings
1098               IF( zlon ==  67. .AND. zlat ==  15. )   zlon =  65.
1099               IF( zlon ==  90. .AND. zlat <=  -4. )   zlon =  95.
1100               IF( zlon ==  95. .AND. zlat ==  -4. )   zlat =  -5.
1101               ! modifications for PIRATA moorings
1102               IF( zlon == -38. .AND. zlat == -19. )   zlon = -34.
1103               IF( zlon == -38. .AND. zlat == -14. )   zlon = -32.
1104               IF( zlon == -38. .AND. zlat ==  -8. )   zlon = -30.
1105               IF( zlon == -38. .AND. zlat ==   0. )   zlon = -35.
1106               IF( zlon == -23. .AND. zlat ==  20. )   zlat =  21.
1107               IF( zlon == -10. .AND. zlat == -14. )   zlat = -10.
1108               IF( zlon == -10. .AND. zlat ==  -8. )   zlat =  -6.
1109               IF( zlon == -10. .AND. zlat ==   4. ) THEN   ;   zlon = 0.   ;   zlat = 0.   ;   ENDIF
1110               CALL dom_ngb( zlon, zlat, ix, iy, cl1 )
1111               IF( zlon >= 0. ) THEN 
1112                  IF( zlon == REAL(NINT(zlon), wp) ) THEN   ;   WRITE(clon, '(i3,  a)') NINT( zlon), 'e'
1113                  ELSE                                      ;   WRITE(clon, '(f5.1,a)')       zlon , 'e'
1114                  ENDIF
1115               ELSE             
1116                  IF( zlon == REAL(NINT(zlon), wp) ) THEN   ;   WRITE(clon, '(i3,  a)') NINT(-zlon), 'w'
1117                  ELSE                                      ;   WRITE(clon, '(f5.1,a)')      -zlon , 'w'
1118                  ENDIF
1119               ENDIF
1120               IF( zlat >= 0. ) THEN 
1121                  IF( zlat == REAL(NINT(zlat), wp) ) THEN   ;   WRITE(clat, '(i2,  a)') NINT( zlat), 'n'
1122                  ELSE                                      ;   WRITE(clat, '(f4.1,a)')       zlat , 'n'
1123                  ENDIF
1124               ELSE             
1125                  IF( zlat == REAL(NINT(zlat), wp) ) THEN   ;   WRITE(clat, '(i2,  a)') NINT(-zlat), 's'
1126                  ELSE                                      ;   WRITE(clat, '(f4.1,a)')      -zlat , 's'
1127                  ENDIF
1128               ENDIF
1129               clname = TRIM(ADJUSTL(clat))//TRIM(ADJUSTL(clon))
1130               CALL event__set_attribut( TRIM(clname)//cl1, attr( zoom__ibegin     , ix                ) )
1131               CALL event__set_attribut( TRIM(clname)//cl1, attr( zoom__jbegin     , iy                ) )
1132               CALL event__set_attribut( TRIM(clname)//cl1, attr( file__name_suffix, '_'//TRIM(clname) ) )     
1133            END DO
1134         END DO
1135      END DO
1136     
1137   END SUBROUTINE set_mooring
1138
1139#else
1140
1141   SUBROUTINE iom_setkt( kt )
1142      INTEGER, INTENT(in   )::   kt 
1143      IF( .FALSE. )   WRITE(numout,*) kt   ! useless test to avoid compilation warnings
1144   END SUBROUTINE iom_setkt
1145
1146#endif
1147
1148
1149   !!======================================================================
1150END MODULE iom
Note: See TracBrowser for help on using the repository browser.