New URL for NEMO forge!   http://forge.nemo-ocean.eu

Since March 2022 along with NEMO 4.2 release, the code development moved to a self-hosted GitLab.
This present forge is now archived and remained online for history.
iom.F90 in trunk/NEMOGCM/NEMO/OPA_SRC/IOM – NEMO

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

Last change on this file since 2715 was 2715, checked in by rblod, 13 years ago

First attempt to put dynamic allocation on the trunk

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