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 @ 1793

Last change on this file since 1793 was 1793, checked in by rblod, 14 years ago

Adaptation to use iomput with AGRIF, see ticket #630

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