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

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

source: branches/2013/dev_LOCEAN_2013/NEMOGCM/NEMO/OPA_SRC/IOM/iom.F90 @ 4152

Last change on this file since 4152 was 4152, checked in by cetlod, 10 years ago

merge in dev_LOCEAN_2013 the 2nd development branch dev_r3940_CNRS4_IOCRS, see ticket #1169

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