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

source: branches/2014/dev_CNRS0_NOC1_LDF/NEMOGCM/NEMO/OPA_SRC/IOM/iom.F90 @ 4616

Last change on this file since 4616 was 4616, checked in by gm, 10 years ago

#1260 : see the associated wiki page for explanation

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