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

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

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

Last change on this file since 4650 was 4650, checked in by acc, 10 years ago

#1325. Minor fix to iom.F90 to prevent incorrect filename generation and allow existing restart files to be correctly overwritten if ln_clobber is .true.

  • Property svn:keywords set to Id
File size: 77.6 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.3 , NEMO Consortium (2010)
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         ELSEIF( llwrt ) THEN     ! the file exists and we are in write mode with permission to
347            clname = cltmpn       ! overwrite so get back the file name without the cpu number
348         ENDIF
349      ENDIF
350      IF( istop == nstop ) THEN   ! no error within this routine
351         SELECT CASE (iolib)
352         CASE (jpioipsl )   ;   CALL iom_ioipsl_open(  clname, kiomid, llwrt, llok, idompar )
353         CASE (jpnf90   )   ;   CALL iom_nf90_open(    clname, kiomid, llwrt, llok, idompar )
354         CASE (jprstdimg)   ;   CALL iom_rstdimg_open( clname, kiomid, llwrt, llok, idompar )
355         CASE DEFAULT
356            CALL ctl_stop( TRIM(clinfo)//' accepted IO library are only jpioipsl, jpnf90 and jprstdimg' )
357         END SELECT
358      ENDIF
359      !
360   END SUBROUTINE iom_open
361
362
363   SUBROUTINE iom_close( kiomid )
364      !!--------------------------------------------------------------------
365      !!                   ***  SUBROUTINE  iom_close  ***
366      !!
367      !! ** Purpose : close an input file, or all files opened by iom
368      !!--------------------------------------------------------------------
369      INTEGER, INTENT(inout), OPTIONAL ::   kiomid   ! iom identifier of the file to be closed
370      !                                              ! return 0 when file is properly closed
371      !                                              ! No argument: all files opened by iom are closed
372
373      INTEGER ::   jf         ! dummy loop indices
374      INTEGER ::   i_s, i_e   ! temporary integer
375      CHARACTER(LEN=100)    ::   clinfo    ! info character
376      !---------------------------------------------------------------------
377      !
378      clinfo = '                    iom_close ~~~  '
379      IF( PRESENT(kiomid) ) THEN
380         i_s = kiomid
381         i_e = kiomid
382      ELSE
383         i_s = 1
384         i_e = jpmax_files
385      ENDIF
386
387      IF( i_s > 0 ) THEN
388         DO jf = i_s, i_e
389            IF( iom_file(jf)%nfid > 0 ) THEN
390               SELECT CASE (iom_file(jf)%iolib)
391               CASE (jpioipsl )   ;   CALL iom_ioipsl_close(  jf )
392               CASE (jpnf90   )   ;   CALL iom_nf90_close(    jf )
393               CASE (jprstdimg)   ;   CALL iom_rstdimg_close( jf )
394               CASE DEFAULT
395                  CALL ctl_stop( TRIM(clinfo)//' accepted IO library are only jpioipsl, jpnf90 and jprstdimg' )
396               END SELECT
397               iom_file(jf)%nfid       = 0          ! free the id
398               IF( PRESENT(kiomid) )   kiomid = 0   ! return 0 as id to specify that the file was closed
399               IF(lwp) WRITE(numout,*) TRIM(clinfo)//' close file: '//TRIM(iom_file(jf)%name)//' ok'
400            ELSEIF( PRESENT(kiomid) ) THEN
401               WRITE(ctmp1,*) '--->',  kiomid
402               CALL ctl_stop( TRIM(clinfo)//' Invalid file identifier', ctmp1 )
403            ENDIF
404         END DO
405      ENDIF
406      !   
407   END SUBROUTINE iom_close
408
409
410   FUNCTION iom_varid ( kiomid, cdvar, kdimsz, kndims, ldstop ) 
411      !!-----------------------------------------------------------------------
412      !!                  ***  FUNCTION  iom_varid  ***
413      !!
414      !! ** Purpose : get the id of a variable in a file (return 0 if not found)
415      !!-----------------------------------------------------------------------
416      INTEGER              , INTENT(in   )           ::   kiomid   ! file Identifier
417      CHARACTER(len=*)     , INTENT(in   )           ::   cdvar    ! name of the variable
418      INTEGER, DIMENSION(:), INTENT(  out), OPTIONAL ::   kdimsz   ! size of the dimensions
419      INTEGER,               INTENT(  out), OPTIONAL ::   kndims   ! size of the dimensions
420      LOGICAL              , INTENT(in   ), OPTIONAL ::   ldstop   ! stop if looking for non-existing variable (default = .TRUE.)
421      !
422      INTEGER                        ::   iom_varid, iiv, i_nvd
423      LOGICAL                        ::   ll_fnd
424      CHARACTER(LEN=100)             ::   clinfo                   ! info character
425      LOGICAL                        ::   llstop                   ! local definition of ldstop
426      !!-----------------------------------------------------------------------
427      iom_varid = 0                         ! default definition
428      ! do we call ctl_stop if we look for non-existing variable?
429      IF( PRESENT(ldstop) ) THEN   ;   llstop = ldstop
430      ELSE                         ;   llstop = .TRUE.
431      ENDIF
432      !
433      IF( kiomid > 0 ) THEN
434         clinfo = 'iom_varid, file: '//trim(iom_file(kiomid)%name)//', var: '//trim(cdvar)
435         IF( iom_file(kiomid)%nfid == 0 ) THEN
436            CALL ctl_stop( trim(clinfo), 'the file is not open' )
437         ELSE
438            ll_fnd  = .FALSE.
439            iiv = 0
440            !
441            DO WHILE ( .NOT.ll_fnd .AND. iiv < iom_file(kiomid)%nvars )
442               iiv = iiv + 1
443               ll_fnd  = ( TRIM(cdvar) == TRIM(iom_file(kiomid)%cn_var(iiv)) )
444            END DO
445            !
446            IF( .NOT.ll_fnd ) THEN
447               iiv = iiv + 1
448               IF( iiv <= jpmax_vars ) THEN
449                  SELECT CASE (iom_file(kiomid)%iolib)
450                  CASE (jpioipsl )   ;   iom_varid = iom_ioipsl_varid( kiomid, cdvar, iiv, kdimsz )
451                  CASE (jpnf90   )   ;   iom_varid = iom_nf90_varid  ( kiomid, cdvar, iiv, kdimsz, kndims )
452                  CASE (jprstdimg)   ;   iom_varid = -1   ! all variables are listed in iom_file
453                  CASE DEFAULT   
454                     CALL ctl_stop( TRIM(clinfo)//' accepted IO library are only jpioipsl, jpnf90 and jprstdimg' )
455                  END SELECT
456               ELSE
457                  CALL ctl_stop( trim(clinfo), 'Too many variables in the file '//iom_file(kiomid)%name,   &
458                        &                         'increase the parameter jpmax_vars')
459               ENDIF
460               IF( llstop .AND. iom_varid == -1 )   CALL ctl_stop( TRIM(clinfo)//' not found' ) 
461            ELSE
462               iom_varid = iiv
463               IF( PRESENT(kdimsz) ) THEN
464                  i_nvd = iom_file(kiomid)%ndims(iiv)
465                  IF( i_nvd == size(kdimsz) ) THEN
466                     kdimsz(:) = iom_file(kiomid)%dimsz(1:i_nvd,iiv)
467                  ELSE
468                     WRITE(ctmp1,*) i_nvd, size(kdimsz)
469                     CALL ctl_stop( trim(clinfo), 'error in kdimsz size'//trim(ctmp1) )
470                  ENDIF
471               ENDIF
472               IF( PRESENT(kndims) )  kndims = iom_file(kiomid)%ndims(iiv)
473            ENDIF
474         ENDIF
475      ENDIF
476      !
477   END FUNCTION iom_varid
478
479
480   !!----------------------------------------------------------------------
481   !!                   INTERFACE iom_get
482   !!----------------------------------------------------------------------
483   SUBROUTINE iom_g0d( kiomid, cdvar, pvar, ktime )
484      INTEGER         , INTENT(in   )                 ::   kiomid    ! Identifier of the file
485      CHARACTER(len=*), INTENT(in   )                 ::   cdvar     ! Name of the variable
486      REAL(wp)        , INTENT(  out)                 ::   pvar      ! read field
487      INTEGER         , INTENT(in   ),     OPTIONAL   ::   ktime     ! record number
488      !
489      INTEGER                                         ::   idvar     ! variable id
490      INTEGER                                         ::   idmspc    ! number of spatial dimensions
491      INTEGER         , DIMENSION(1)                  ::   itime     ! record number
492      CHARACTER(LEN=100)                              ::   clinfo    ! info character
493      CHARACTER(LEN=100)                              ::   clname    ! file name
494      CHARACTER(LEN=1)                                ::   cldmspc   !
495      !
496      itime = 1
497      IF( PRESENT(ktime) ) itime = ktime
498      !
499      clname = iom_file(kiomid)%name
500      clinfo = '          iom_g0d, file: '//trim(clname)//', var: '//trim(cdvar)
501      !
502      IF( kiomid > 0 ) THEN
503         idvar = iom_varid( kiomid, cdvar )
504         IF( iom_file(kiomid)%nfid > 0 .AND. idvar > 0 ) THEN
505            idmspc = iom_file ( kiomid )%ndims( idvar )
506            IF( iom_file(kiomid)%luld(idvar) )  idmspc = idmspc - 1
507            WRITE(cldmspc , fmt='(i1)') idmspc
508            IF( idmspc > 0 )  CALL ctl_stop( TRIM(clinfo), 'When reading to a 0D array, we do not accept data', &
509                                 &                         'with 1 or more spatial dimensions: '//cldmspc//' were found.' , &
510                                 &                         'Use ncwa -a to suppress the unnecessary dimensions' )
511            SELECT CASE (iom_file(kiomid)%iolib)
512            CASE (jpioipsl )   ;   CALL iom_ioipsl_get(  kiomid, idvar, pvar, itime )
513            CASE (jpnf90   )   ;   CALL iom_nf90_get(    kiomid, idvar, pvar, itime )
514            CASE (jprstdimg)   ;   CALL iom_rstdimg_get( kiomid, idvar, pvar )
515            CASE DEFAULT   
516               CALL ctl_stop( 'iom_g0d: accepted IO library are only jpioipsl, jpnf90 and jprstdimg' )
517            END SELECT
518         ENDIF
519      ENDIF
520   END SUBROUTINE iom_g0d
521
522   SUBROUTINE iom_g1d( kiomid, kdom, cdvar, pvar, ktime, kstart, kcount )
523      INTEGER         , INTENT(in   )                         ::   kiomid    ! Identifier of the file
524      INTEGER         , INTENT(in   )                         ::   kdom      ! Type of domain to be read
525      CHARACTER(len=*), INTENT(in   )                         ::   cdvar     ! Name of the variable
526      REAL(wp)        , INTENT(  out), DIMENSION(:)           ::   pvar      ! read field
527      INTEGER         , INTENT(in   )              , OPTIONAL ::   ktime     ! record number
528      INTEGER         , INTENT(in   ), DIMENSION(1), OPTIONAL ::   kstart    ! start axis position of the reading
529      INTEGER         , INTENT(in   ), DIMENSION(1), OPTIONAL ::   kcount    ! number of points in each axis
530      !
531      IF( kiomid > 0 ) THEN
532         IF( iom_file(kiomid)%nfid > 0 ) CALL iom_get_123d( kiomid, kdom       , cdvar        , pv_r1d=pvar,   &
533              &                                                     ktime=ktime, kstart=kstart, kcount=kcount )
534      ENDIF
535   END SUBROUTINE iom_g1d
536
537   SUBROUTINE iom_g2d( kiomid, kdom, cdvar, pvar, ktime, kstart, kcount )
538      INTEGER         , INTENT(in   )                           ::   kiomid    ! Identifier of the file
539      INTEGER         , INTENT(in   )                           ::   kdom      ! Type of domain to be read
540      CHARACTER(len=*), INTENT(in   )                           ::   cdvar     ! Name of the variable
541      REAL(wp)        , INTENT(  out), DIMENSION(:,:)           ::   pvar      ! read field
542      INTEGER         , INTENT(in   )                , OPTIONAL ::   ktime     ! record number
543      INTEGER         , INTENT(in   ), DIMENSION(2)  , OPTIONAL ::   kstart    ! start axis position of the reading
544      INTEGER         , INTENT(in   ), DIMENSION(2)  , OPTIONAL ::   kcount    ! number of points in each axis
545      !
546      IF( kiomid > 0 ) THEN
547         IF( iom_file(kiomid)%nfid > 0 ) CALL iom_get_123d( kiomid, kdom       , cdvar        , pv_r2d=pvar,   &
548              &                                                     ktime=ktime, kstart=kstart, kcount=kcount )
549      ENDIF
550   END SUBROUTINE iom_g2d
551
552   SUBROUTINE iom_g3d( kiomid, kdom, cdvar, pvar, ktime, kstart, kcount )
553      INTEGER         , INTENT(in   )                             ::   kiomid    ! Identifier of the file
554      INTEGER         , INTENT(in   )                             ::   kdom      ! Type of domain to be read
555      CHARACTER(len=*), INTENT(in   )                             ::   cdvar     ! Name of the variable
556      REAL(wp)        , INTENT(  out), DIMENSION(:,:,:)           ::   pvar      ! read field
557      INTEGER         , INTENT(in   )                  , OPTIONAL ::   ktime     ! record number
558      INTEGER         , INTENT(in   ), DIMENSION(3)    , OPTIONAL ::   kstart    ! start axis position of the reading
559      INTEGER         , INTENT(in   ), DIMENSION(3)    , OPTIONAL ::   kcount    ! number of points in each axis
560      !
561      IF( kiomid > 0 ) THEN
562         IF( iom_file(kiomid)%nfid > 0 ) CALL iom_get_123d( kiomid, kdom       , cdvar        , pv_r3d=pvar,   &
563              &                                                     ktime=ktime, kstart=kstart, kcount=kcount )
564      ENDIF
565   END SUBROUTINE iom_g3d
566   !!----------------------------------------------------------------------
567
568   SUBROUTINE iom_get_123d( kiomid, kdom  , cdvar ,   &
569         &                  pv_r1d, pv_r2d, pv_r3d,   &
570         &                  ktime , kstart, kcount  )
571      !!-----------------------------------------------------------------------
572      !!                  ***  ROUTINE  iom_get_123d  ***
573      !!
574      !! ** Purpose : read a 1D/2D/3D variable
575      !!
576      !! ** Method : read ONE record at each CALL
577      !!-----------------------------------------------------------------------
578      INTEGER                    , INTENT(in   )           ::   kiomid     ! Identifier of the file
579      INTEGER                    , INTENT(in   )           ::   kdom       ! Type of domain to be read
580      CHARACTER(len=*)           , INTENT(in   )           ::   cdvar      ! Name of the variable
581      REAL(wp), DIMENSION(:)     , INTENT(  out), OPTIONAL ::   pv_r1d     ! read field (1D case)
582      REAL(wp), DIMENSION(:,:)   , INTENT(  out), OPTIONAL ::   pv_r2d     ! read field (2D case)
583      REAL(wp), DIMENSION(:,:,:) , INTENT(  out), OPTIONAL ::   pv_r3d     ! read field (3D case)
584      INTEGER                    , INTENT(in   ), OPTIONAL ::   ktime      ! record number
585      INTEGER , DIMENSION(:)     , INTENT(in   ), OPTIONAL ::   kstart     ! start position of the reading in each axis
586      INTEGER , DIMENSION(:)     , INTENT(in   ), OPTIONAL ::   kcount     ! number of points to be read in each axis
587      !
588      LOGICAL                        ::   llnoov      ! local definition to read overlap
589      INTEGER                        ::   jl          ! loop on number of dimension
590      INTEGER                        ::   idom        ! type of domain
591      INTEGER                        ::   idvar       ! id of the variable
592      INTEGER                        ::   inbdim      ! number of dimensions of the variable
593      INTEGER                        ::   idmspc      ! number of spatial dimensions
594      INTEGER                        ::   itime       ! record number
595      INTEGER                        ::   istop       ! temporary value of nstop
596      INTEGER                        ::   ix1, ix2, iy1, iy2   ! subdomain indexes
597      INTEGER                        ::   ji, jj      ! loop counters
598      INTEGER                        ::   irankpv       !
599      INTEGER                        ::   ind1, ind2  ! substring index
600      INTEGER, DIMENSION(jpmax_dims) ::   istart      ! starting point to read for each axis
601      INTEGER, DIMENSION(jpmax_dims) ::   icnt        ! number of value to read along each axis
602      INTEGER, DIMENSION(jpmax_dims) ::   idimsz      ! size of the dimensions of the variable
603      INTEGER, DIMENSION(jpmax_dims) ::   ishape      ! size of the dimensions of the variable
604      REAL(wp)                       ::   zscf, zofs  ! sacle_factor and add_offset
605      INTEGER                        ::   itmp        ! temporary integer
606      CHARACTER(LEN=256)             ::   clinfo      ! info character
607      CHARACTER(LEN=256)             ::   clname      ! file name
608      CHARACTER(LEN=1)               ::   clrankpv, cldmspc      !
609      !---------------------------------------------------------------------
610      !
611      clname = iom_file(kiomid)%name   !   esier to read
612      clinfo = '          iom_get_123d, file: '//trim(clname)//', var: '//trim(cdvar)
613      ! local definition of the domain ?
614      idom = kdom
615      ! do we read the overlap
616      ! ugly patch SM+JMM+RB to overwrite global definition in some cases
617      llnoov = (jpni * jpnj ) == jpnij .AND. .NOT. lk_agrif 
618      ! check kcount and kstart optionals parameters...
619      IF( PRESENT(kcount) .AND. (.NOT. PRESENT(kstart)) ) CALL ctl_stop(trim(clinfo), 'kcount present needs kstart present')
620      IF( PRESENT(kstart) .AND. (.NOT. PRESENT(kcount)) ) CALL ctl_stop(trim(clinfo), 'kstart present needs kcount present')
621      IF( PRESENT(kstart) .AND. idom /= jpdom_unknown   ) CALL ctl_stop(trim(clinfo), 'kstart present needs kdom = jpdom_unknown')
622
623      ! Search for the variable in the data base (eventually actualize data)
624      istop = nstop
625      idvar = iom_varid( kiomid, cdvar )
626      !
627      IF( idvar > 0 ) THEN
628         ! to write iom_file(kiomid)%dimsz in a shorter way !
629         idimsz(:) = iom_file(kiomid)%dimsz(:, idvar) 
630         inbdim = iom_file(kiomid)%ndims(idvar)            ! number of dimensions in the file
631         idmspc = inbdim                                   ! number of spatial dimensions in the file
632         IF( iom_file(kiomid)%luld(idvar) )   idmspc = inbdim - 1
633         IF( idmspc > 3 )   CALL ctl_stop(trim(clinfo), 'the file has more than 3 spatial dimensions this case is not coded...') 
634         !
635         ! update idom definition...
636         ! Identify the domain in case of jpdom_auto(glo/dta) definition
637         IF( idom == jpdom_autoglo .OR. idom == jpdom_autodta ) THEN           
638            IF( idom == jpdom_autoglo ) THEN   ;   idom = jpdom_global 
639            ELSE                               ;   idom = jpdom_data
640            ENDIF
641            ind1 = INDEX( clname, '_', back = .TRUE. ) + 1
642            ind2 = INDEX( clname, '.', back = .TRUE. ) - 1
643            IF( ind2 > ind1 ) THEN   ;   IF( VERIFY( clname(ind1:ind2), '0123456789' ) == 0 )   idom = jpdom_local   ;   ENDIF
644         ENDIF
645         ! Identify the domain in case of jpdom_local definition
646         IF( idom == jpdom_local ) THEN
647            IF(     idimsz(1) == jpi               .AND. idimsz(2) == jpj               ) THEN   ;   idom = jpdom_local_full
648            ELSEIF( idimsz(1) == nlci              .AND. idimsz(2) == nlcj              ) THEN   ;   idom = jpdom_local_noextra
649            ELSEIF( idimsz(1) == (nlei - nldi + 1) .AND. idimsz(2) == (nlej - nldj + 1) ) THEN   ;   idom = jpdom_local_noovlap
650            ELSE   ;   CALL ctl_stop( trim(clinfo), 'impossible to identify the local domain' )
651            ENDIF
652         ENDIF
653         !
654         ! check the consistency between input array and data rank in the file
655         !
656         ! initializations
657         itime = 1
658         IF( PRESENT(ktime) ) itime = ktime
659
660         irankpv = 1 * COUNT( (/PRESENT(pv_r1d)/) ) + 2 * COUNT( (/PRESENT(pv_r2d)/) ) + 3 * COUNT( (/PRESENT(pv_r3d)/) )
661         WRITE(clrankpv, fmt='(i1)') irankpv
662         WRITE(cldmspc , fmt='(i1)') idmspc
663         !
664         IF(     idmspc <  irankpv ) THEN
665            CALL ctl_stop( TRIM(clinfo), 'The file has only '//cldmspc//' spatial dimension',   &
666               &                         'it is impossible to read a '//clrankpv//'D array from this file...' )
667         ELSEIF( idmspc == irankpv ) THEN
668            IF( PRESENT(pv_r1d) .AND. idom /= jpdom_unknown )   &
669               &   CALL ctl_stop( TRIM(clinfo), 'case not coded...You must use jpdom_unknown' )
670         ELSEIF( idmspc >  irankpv ) THEN
671               IF( PRESENT(pv_r2d) .AND. itime == 1 .AND. idimsz(3) == 1 .AND. idmspc == 3 ) THEN
672                  CALL ctl_warn( trim(clinfo), '2D array but 3 spatial dimensions for the data...'              ,   &
673                        &         'As the size of the z dimension is 1 and as we try to read the first record, ',   &
674                        &         'we accept this case, even if there is a possible mix-up between z and time dimension' )   
675                  idmspc = idmspc - 1
676               ELSE
677                  CALL ctl_stop( TRIM(clinfo), 'To keep iom lisibility, when reading a '//clrankpv//'D array,'         ,   &
678                     &                         'we do not accept data with '//cldmspc//' spatial dimensions',   &
679                     &                         'Use ncwa -a to suppress the unnecessary dimensions' )
680               ENDIF
681         ENDIF
682
683         !
684         ! definition of istart and icnt
685         !
686         icnt  (:) = 1
687         istart(:) = 1
688         istart(idmspc+1) = itime
689
690         IF(              PRESENT(kstart)       ) THEN ; istart(1:idmspc) = kstart(1:idmspc) ; icnt(1:idmspc) = kcount(1:idmspc)
691         ELSE
692            IF(           idom == jpdom_unknown ) THEN                                       ; icnt(1:idmspc) = idimsz(1:idmspc)
693            ELSE
694               IF( .NOT. PRESENT(pv_r1d) ) THEN   !   not a 1D array
695                  IF(     idom == jpdom_data    ) THEN ; istart(1:2) = (/ mig(1), mjg(1) /)  ! icnt(1:2) done bellow
696                  ELSEIF( idom == jpdom_global  ) THEN ; istart(1:2) = (/ nimpp , njmpp  /)  ! icnt(1:2) done bellow
697                  ENDIF
698                  ! we do not read the overlap                     -> we start to read at nldi, nldj
699! JMM + SM: ugly patch before getting the new version of lib_mpp)
700!                  IF( idom /= jpdom_local_noovlap )   istart(1:2) = istart(1:2) + (/ nldi - 1, nldj - 1 /)
701                  IF( llnoov .AND. idom /= jpdom_local_noovlap ) istart(1:2) = istart(1:2) + (/ nldi - 1, nldj - 1 /)
702                  ! we do not read the overlap and the extra-halos -> from nldi to nlei and from nldj to nlej
703! JMM + SM: ugly patch before getting the new version of lib_mpp)
704!                  icnt(1:2) = (/ nlei - nldi + 1, nlej - nldj + 1 /)
705                  IF( llnoov ) THEN   ;   icnt(1:2) = (/ nlei - nldi + 1, nlej - nldj + 1 /)
706                  ELSE                ;   icnt(1:2) = (/ nlci           , nlcj            /)
707                  ENDIF
708                  IF( PRESENT(pv_r3d) ) THEN
709                     IF( idom == jpdom_data ) THEN   ; icnt(3) = jpkdta
710                     ELSE                            ; icnt(3) = jpk
711                     ENDIF
712                  ENDIF
713               ENDIF
714            ENDIF
715         ENDIF
716
717         ! check that istart and icnt can be used with this file
718         !-
719         DO jl = 1, jpmax_dims
720            itmp = istart(jl)+icnt(jl)-1
721            IF( itmp > idimsz(jl) .AND. idimsz(jl) /= 0 ) THEN
722               WRITE( ctmp1, FMT="('(istart(', i1, ') + icnt(', i1, ') - 1) = ', i5)" ) jl, jl, itmp
723               WRITE( ctmp2, FMT="(' is larger than idimsz(', i1,') = ', i5)"         ) jl, idimsz(jl)
724               CALL ctl_stop( trim(clinfo), 'start and count too big regarding to the size of the data, ', ctmp1, ctmp2 )     
725            ENDIF
726         END DO
727
728         ! check that icnt matches the input array
729         !-     
730         IF( idom == jpdom_unknown ) THEN
731            IF( irankpv == 1 )        ishape(1:1) = SHAPE(pv_r1d)
732            IF( irankpv == 2 )        ishape(1:2) = SHAPE(pv_r2d)
733            IF( irankpv == 3 )        ishape(1:3) = SHAPE(pv_r3d)
734            ctmp1 = 'd'
735         ELSE
736            IF( irankpv == 2 ) THEN
737! JMM + SM: ugly patch before getting the new version of lib_mpp)
738!               ishape(1:2) = SHAPE(pv_r2d(nldi:nlei,nldj:nlej  ))   ;   ctmp1 = 'd(nldi:nlei,nldj:nlej)'
739               IF( llnoov ) THEN ; ishape(1:2)=SHAPE(pv_r2d(nldi:nlei,nldj:nlej  )) ; ctmp1='d(nldi:nlei,nldj:nlej)'
740               ELSE              ; ishape(1:2)=SHAPE(pv_r2d(1   :nlci,1   :nlcj  )) ; ctmp1='d(1:nlci,1:nlcj)'
741               ENDIF
742            ENDIF
743            IF( irankpv == 3 ) THEN 
744! JMM + SM: ugly patch before getting the new version of lib_mpp)
745!               ishape(1:3) = SHAPE(pv_r3d(nldi:nlei,nldj:nlej,:))   ;   ctmp1 = 'd(nldi:nlei,nldj:nlej,:)'
746               IF( llnoov ) THEN ; ishape(1:3)=SHAPE(pv_r3d(nldi:nlei,nldj:nlej,:)) ; ctmp1='d(nldi:nlei,nldj:nlej,:)'
747               ELSE              ; ishape(1:3)=SHAPE(pv_r3d(1   :nlci,1   :nlcj,:)) ; ctmp1='d(1:nlci,1:nlcj,:)'
748               ENDIF
749            ENDIF
750         ENDIF
751         
752         DO jl = 1, irankpv
753            WRITE( ctmp2, FMT="(', ', i1,'): ', i5,' /= icnt(', i1,'):', i5)" ) jl, ishape(jl), jl, icnt(jl)
754            IF( ishape(jl) /= icnt(jl) )   CALL ctl_stop( TRIM(clinfo), 'size(pv_r'//clrankpv//TRIM(ctmp1)//TRIM(ctmp2) )
755         END DO
756
757      ENDIF
758
759      ! read the data
760      !-     
761      IF( idvar > 0 .AND. istop == nstop ) THEN   ! no additional errors until this point...
762         !
763         ! find the right index of the array to be read
764! JMM + SM: ugly patch before getting the new version of lib_mpp)
765!         IF( idom /= jpdom_unknown ) THEN   ;   ix1 = nldi   ;   ix2 = nlei      ;   iy1 = nldj   ;   iy2 = nlej
766!         ELSE                               ;   ix1 = 1      ;   ix2 = icnt(1)   ;   iy1 = 1      ;   iy2 = icnt(2)
767!         ENDIF
768         IF( llnoov ) THEN
769            IF( idom /= jpdom_unknown ) THEN   ;   ix1 = nldi   ;   ix2 = nlei      ;   iy1 = nldj   ;   iy2 = nlej
770            ELSE                               ;   ix1 = 1      ;   ix2 = icnt(1)   ;   iy1 = 1      ;   iy2 = icnt(2)
771            ENDIF
772         ELSE
773            IF( idom /= jpdom_unknown ) THEN   ;   ix1 = 1      ;   ix2 = nlci      ;   iy1 = 1      ;   iy2 = nlcj
774            ELSE                               ;   ix1 = 1      ;   ix2 = icnt(1)   ;   iy1 = 1      ;   iy2 = icnt(2)
775            ENDIF
776         ENDIF
777     
778         SELECT CASE (iom_file(kiomid)%iolib)
779         CASE (jpioipsl )   ;   CALL iom_ioipsl_get(  kiomid, idvar, inbdim, istart, icnt, ix1, ix2, iy1, iy2,   &
780            &                                         pv_r1d, pv_r2d, pv_r3d )
781         CASE (jpnf90   )   ;   CALL iom_nf90_get(    kiomid, idvar, inbdim, istart, icnt, ix1, ix2, iy1, iy2,   &
782            &                                         pv_r1d, pv_r2d, pv_r3d )
783         CASE (jprstdimg)   ;   CALL iom_rstdimg_get( kiomid, idom, idvar, ix1, ix2, iy1, iy2,   &
784            &                                         pv_r1d, pv_r2d, pv_r3d )
785         CASE DEFAULT   
786            CALL ctl_stop( TRIM(clinfo)//' accepted IO library are only jpioipsl, jpnf90 and jprstdimg' )
787         END SELECT
788
789         IF( istop == nstop ) THEN   ! no additional errors until this point...
790            IF(lwp) WRITE(numout,"(10x,' read ',a,' (rec: ',i6,') in ',a,' ok')") TRIM(cdvar), itime, TRIM(iom_file(kiomid)%name)
791         
792            !--- overlap areas and extra hallows (mpp)
793            IF(     PRESENT(pv_r2d) .AND. idom /= jpdom_unknown ) THEN
794               CALL lbc_lnk( pv_r2d,'Z',-999.,'no0' )
795            ELSEIF( PRESENT(pv_r3d) .AND. idom /= jpdom_unknown ) THEN
796               ! this if could be simplified with the new lbc_lnk that works with any size of the 3rd dimension
797               IF( icnt(3) == jpk ) THEN
798                  CALL lbc_lnk( pv_r3d,'Z',-999.,'no0' )
799               ELSE   ! put some arbitrary value (a call to lbc_lnk will be done later...)
800                  DO jj = nlcj+1, jpj   ;   pv_r3d(1:nlci, jj, :) = pv_r3d(1:nlci, nlej, :)   ;   END DO
801                  DO ji = nlci+1, jpi   ;   pv_r3d(ji    , : , :) = pv_r3d(nlei  , :   , :)   ;   END DO
802               ENDIF
803            ENDIF
804           
805            ! C1D case : always call lbc_lnk to replicate the central value over the whole 3X3 domain
806            IF( lk_c1d .AND. PRESENT(pv_r2d) )   CALL lbc_lnk( pv_r2d,'Z',1. )
807            IF( lk_c1d .AND. PRESENT(pv_r3d) )   CALL lbc_lnk( pv_r3d,'Z',1. )
808   
809            !--- Apply scale_factor and offset
810            zscf = iom_file(kiomid)%scf(idvar)      ! scale factor
811            zofs = iom_file(kiomid)%ofs(idvar)      ! offset
812            IF(     PRESENT(pv_r1d) ) THEN
813               IF( zscf /= 1. )   pv_r1d(:) = pv_r1d(:) * zscf 
814               IF( zofs /= 0. )   pv_r1d(:) = pv_r1d(:) + zofs
815            ELSEIF( PRESENT(pv_r2d) ) THEN
816!CDIR COLLAPSE
817               IF( zscf /= 1.)   pv_r2d(:,:) = pv_r2d(:,:) * zscf
818!CDIR COLLAPSE
819               IF( zofs /= 0.)   pv_r2d(:,:) = pv_r2d(:,:) + zofs
820            ELSEIF( PRESENT(pv_r3d) ) THEN
821!CDIR COLLAPSE
822               IF( zscf /= 1.)   pv_r3d(:,:,:) = pv_r3d(:,:,:) * zscf
823!CDIR COLLAPSE
824               IF( zofs /= 0.)   pv_r3d(:,:,:) = pv_r3d(:,:,:) + zofs
825            ENDIF
826            !
827         ENDIF
828         !
829      ENDIF
830      !
831   END SUBROUTINE iom_get_123d
832
833
834   SUBROUTINE iom_gettime( kiomid, ptime, cdvar, kntime, cdunits, cdcalendar )
835      !!--------------------------------------------------------------------
836      !!                   ***  SUBROUTINE iom_gettime  ***
837      !!
838      !! ** Purpose : read the time axis cdvar in the file
839      !!--------------------------------------------------------------------
840      INTEGER                    , INTENT(in   ) ::   kiomid     ! file Identifier
841      REAL(wp), DIMENSION(:)     , INTENT(  out) ::   ptime      ! the time axis
842      CHARACTER(len=*), OPTIONAL , INTENT(in   ) ::   cdvar      ! time axis name
843      INTEGER         , OPTIONAL , INTENT(  out) ::   kntime     ! number of times in file
844      CHARACTER(len=*), OPTIONAL , INTENT(  out) ::   cdunits    ! units attribute of time coordinate
845      CHARACTER(len=*), OPTIONAL , INTENT(  out) ::   cdcalendar ! calendar attribute of
846      !
847      INTEGER, DIMENSION(1) :: kdimsz
848      INTEGER            ::   idvar    ! id of the variable
849      CHARACTER(LEN=32)  ::   tname    ! local name of time coordinate
850      CHARACTER(LEN=100) ::   clinfo   ! info character
851      !---------------------------------------------------------------------
852      !
853      IF ( PRESENT(cdvar) ) THEN
854         tname = cdvar
855      ELSE
856         tname = iom_file(kiomid)%uldname
857      ENDIF
858      IF( kiomid > 0 ) THEN
859         clinfo = 'iom_gettime, file: '//trim(iom_file(kiomid)%name)//', var: '//trim(tname)
860         IF ( PRESENT(kntime) ) THEN
861            idvar  = iom_varid( kiomid, tname, kdimsz = kdimsz )
862            kntime = kdimsz(1)
863         ELSE
864            idvar = iom_varid( kiomid, tname )
865         ENDIF
866         !
867         ptime(:) = 0. ! default definition
868         IF( idvar > 0 ) THEN
869            IF( iom_file(kiomid)%ndims(idvar) == 1 ) THEN
870               IF( iom_file(kiomid)%luld(idvar) ) THEN
871                  IF( iom_file(kiomid)%dimsz(1,idvar) <= size(ptime) ) THEN
872                     SELECT CASE (iom_file(kiomid)%iolib)
873                     CASE (jpioipsl )   ;   CALL iom_ioipsl_gettime( kiomid, idvar, ptime, cdunits, cdcalendar )
874                     CASE (jpnf90   )   ;   CALL iom_nf90_gettime(   kiomid, idvar, ptime, cdunits, cdcalendar )
875                     CASE (jprstdimg)   ;   CALL ctl_stop( TRIM(clinfo)//' case IO library == jprstdimg not coded...' )
876                     CASE DEFAULT   
877                        CALL ctl_stop( TRIM(clinfo)//' accepted IO library are only jpioipsl, jpnf90 and jprstdimg' )
878                     END SELECT
879                  ELSE
880                     WRITE(ctmp1,*) 'error with the size of ptime ',size(ptime),iom_file(kiomid)%dimsz(1,idvar)
881                     CALL ctl_stop( trim(clinfo), trim(ctmp1) )
882                  ENDIF
883               ELSE
884                  CALL ctl_stop( trim(clinfo), 'variable dimension is not unlimited... use iom_get' )
885               ENDIF
886            ELSE
887               CALL ctl_stop( trim(clinfo), 'the variable has more than 1 dimension' )
888            ENDIF
889         ELSE
890            CALL ctl_stop( trim(clinfo), 'variable not found in '//iom_file(kiomid)%name )
891         ENDIF
892      ENDIF
893      !
894   END SUBROUTINE iom_gettime
895
896
897   !!----------------------------------------------------------------------
898   !!                   INTERFACE iom_getatt
899   !!----------------------------------------------------------------------
900   SUBROUTINE iom_g0d_intatt( kiomid, cdatt, pvar )
901      INTEGER         , INTENT(in   )                 ::   kiomid    ! Identifier of the file
902      CHARACTER(len=*), INTENT(in   )                 ::   cdatt     ! Name of the attribute
903      INTEGER         , INTENT(  out)                 ::   pvar      ! read field
904      !
905      IF( kiomid > 0 ) THEN
906         IF( iom_file(kiomid)%nfid > 0 ) THEN
907            SELECT CASE (iom_file(kiomid)%iolib)
908            CASE (jpioipsl )   ;   CALL ctl_stop('iom_getatt: only nf90 available')
909            CASE (jpnf90   )   ;   CALL iom_nf90_getatt( kiomid, cdatt, pvar )
910            CASE (jprstdimg)   ;   CALL ctl_stop('iom_getatt: only nf90 available')
911            CASE DEFAULT   
912               CALL ctl_stop( 'iom_g0d_att: accepted IO library are only jpioipsl, jpnf90 and jprstdimg' )
913            END SELECT
914         ENDIF
915      ENDIF
916   END SUBROUTINE iom_g0d_intatt
917
918
919   !!----------------------------------------------------------------------
920   !!                   INTERFACE iom_rstput
921   !!----------------------------------------------------------------------
922   SUBROUTINE iom_rp0d( kt, kwrite, kiomid, cdvar, pvar, ktype )
923      INTEGER         , INTENT(in)                         ::   kt       ! ocean time-step
924      INTEGER         , INTENT(in)                         ::   kwrite   ! writing time-step
925      INTEGER         , INTENT(in)                         ::   kiomid   ! Identifier of the file
926      CHARACTER(len=*), INTENT(in)                         ::   cdvar    ! time axis name
927      REAL(wp)        , INTENT(in)                         ::   pvar     ! written field
928      INTEGER         , INTENT(in), OPTIONAL               ::   ktype    ! variable external type
929      INTEGER :: ivid   ! variable id
930      IF( kiomid > 0 ) THEN
931         IF( iom_file(kiomid)%nfid > 0 ) THEN
932            ivid = iom_varid( kiomid, cdvar, ldstop = .FALSE. )
933            SELECT CASE (iom_file(kiomid)%iolib)
934            CASE (jpioipsl )   ;   CALL iom_ioipsl_rstput( kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r0d = pvar )
935            CASE (jpnf90   )   ;   CALL iom_nf90_rstput(   kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r0d = pvar )
936            CASE (jprstdimg)   ;   IF( kt == kwrite )    CALL iom_rstdimg_rstput( kiomid, cdvar, ivid, pvar )
937            CASE DEFAULT     
938               CALL ctl_stop( 'iom_rp0d: accepted IO library are only jpioipsl, jpnf90 and jprstdimg' )
939            END SELECT
940         ENDIF
941      ENDIF
942   END SUBROUTINE iom_rp0d
943
944   SUBROUTINE iom_rp1d( kt, kwrite, kiomid, cdvar, pvar, ktype )
945      INTEGER         , INTENT(in)                         ::   kt       ! ocean time-step
946      INTEGER         , INTENT(in)                         ::   kwrite   ! writing time-step
947      INTEGER         , INTENT(in)                         ::   kiomid   ! Identifier of the file
948      CHARACTER(len=*), INTENT(in)                         ::   cdvar    ! time axis name
949      REAL(wp)        , INTENT(in), DIMENSION(          :) ::   pvar     ! written field
950      INTEGER         , INTENT(in), OPTIONAL               ::   ktype    ! variable external type
951      INTEGER :: ivid   ! variable id
952      IF( kiomid > 0 ) THEN
953         IF( iom_file(kiomid)%nfid > 0 ) THEN
954            ivid = iom_varid( kiomid, cdvar, ldstop = .FALSE. )
955            SELECT CASE (iom_file(kiomid)%iolib)
956            CASE (jpioipsl )   ;   CALL iom_ioipsl_rstput( kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r1d = pvar )
957            CASE (jpnf90   )   ;   CALL iom_nf90_rstput(   kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r1d = pvar )
958            CASE (jprstdimg)   ;   IF( kt == kwrite )    CALL iom_rstdimg_rstput( kiomid, cdvar, ivid, pv_r1d = pvar )
959            CASE DEFAULT     
960               CALL ctl_stop( 'iom_rp1d: accepted IO library are only jpioipsl, jpnf90 and jprstdimg' )
961            END SELECT
962         ENDIF
963      ENDIF
964   END SUBROUTINE iom_rp1d
965
966   SUBROUTINE iom_rp2d( kt, kwrite, kiomid, cdvar, pvar, ktype )
967      INTEGER         , INTENT(in)                         ::   kt       ! ocean time-step
968      INTEGER         , INTENT(in)                         ::   kwrite   ! writing time-step
969      INTEGER         , INTENT(in)                         ::   kiomid   ! Identifier of the file
970      CHARACTER(len=*), INTENT(in)                         ::   cdvar    ! time axis name
971      REAL(wp)        , INTENT(in), DIMENSION(:,    :    ) ::   pvar     ! written field
972      INTEGER         , INTENT(in), OPTIONAL               ::   ktype    ! variable external type
973      INTEGER :: ivid   ! variable id
974      IF( kiomid > 0 ) THEN
975         IF( iom_file(kiomid)%nfid > 0 ) THEN
976            ivid = iom_varid( kiomid, cdvar, ldstop = .FALSE. )
977            SELECT CASE (iom_file(kiomid)%iolib)
978            CASE (jpioipsl )   ;   CALL iom_ioipsl_rstput( kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r2d = pvar )
979            CASE (jpnf90   )   ;   CALL iom_nf90_rstput(   kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r2d = pvar )
980            CASE (jprstdimg)   ;   IF( kt == kwrite )   CALL iom_rstdimg_rstput( kiomid, cdvar, ivid, pv_r2d = pvar ) 
981            CASE DEFAULT     
982               CALL ctl_stop( 'iom_rp2d: accepted IO library are only jpioipsl, jpnf90 and jprstdimg' )
983            END SELECT
984         ENDIF
985      ENDIF
986   END SUBROUTINE iom_rp2d
987
988   SUBROUTINE iom_rp3d( kt, kwrite, kiomid, cdvar, pvar, ktype )
989      INTEGER         , INTENT(in)                         ::   kt       ! ocean time-step
990      INTEGER         , INTENT(in)                         ::   kwrite   ! writing time-step
991      INTEGER         , INTENT(in)                         ::   kiomid   ! Identifier of the file
992      CHARACTER(len=*), INTENT(in)                         ::   cdvar    ! time axis name
993      REAL(wp)        , INTENT(in),       DIMENSION(:,:,:) ::   pvar     ! written field
994      INTEGER         , INTENT(in), OPTIONAL               ::   ktype    ! variable external type
995      INTEGER :: ivid   ! variable id
996      IF( kiomid > 0 ) THEN
997         IF( iom_file(kiomid)%nfid > 0 ) THEN
998            ivid = iom_varid( kiomid, cdvar, ldstop = .FALSE. )
999            SELECT CASE (iom_file(kiomid)%iolib)
1000            CASE (jpioipsl )   ;   CALL iom_ioipsl_rstput( kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r3d = pvar )
1001            CASE (jpnf90   )   ;   CALL iom_nf90_rstput(   kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r3d = pvar )
1002            CASE (jprstdimg)   ;   IF( kt == kwrite )   CALL iom_rstdimg_rstput( kiomid, cdvar, ivid, pv_r3d = pvar )
1003            CASE DEFAULT     
1004               CALL ctl_stop( 'iom_rp3d: accepted IO library are only jpioipsl and jprstdimg' )
1005            END SELECT
1006         ENDIF
1007      ENDIF
1008   END SUBROUTINE iom_rp3d
1009
1010
1011   !!----------------------------------------------------------------------
1012   !!                   INTERFACE iom_put
1013   !!----------------------------------------------------------------------
1014   SUBROUTINE iom_p0d( cdname, pfield0d )
1015      CHARACTER(LEN=*), INTENT(in) ::   cdname
1016      REAL(wp)        , INTENT(in) ::   pfield0d
1017#if defined key_iomput
1018      CALL xios_send_field(cdname, (/pfield0d/))
1019#else
1020      IF( .FALSE. )   WRITE(numout,*) cdname, pfield0d   ! useless test to avoid compilation warnings
1021#endif
1022   END SUBROUTINE iom_p0d
1023
1024   SUBROUTINE iom_p1d( cdname, pfield1d )
1025      CHARACTER(LEN=*)          , INTENT(in) ::   cdname
1026      REAL(wp),     DIMENSION(:), INTENT(in) ::   pfield1d
1027#if defined key_iomput
1028      CALL xios_send_field( cdname, RESHAPE( (/pfield1d/), (/1,1,SIZE(pfield1d)/) ) )
1029#else
1030      IF( .FALSE. )   WRITE(numout,*) cdname, pfield1d   ! useless test to avoid compilation warnings
1031#endif
1032   END SUBROUTINE iom_p1d
1033
1034   SUBROUTINE iom_p2d( cdname, pfield2d )
1035      CHARACTER(LEN=*)            , INTENT(in) ::   cdname
1036      REAL(wp),     DIMENSION(:,:), INTENT(in) ::   pfield2d
1037#if defined key_iomput
1038      CALL xios_send_field(cdname, pfield2d)
1039#else
1040      IF( .FALSE. )   WRITE(numout,*) cdname, pfield2d   ! useless test to avoid compilation warnings
1041#endif
1042   END SUBROUTINE iom_p2d
1043
1044   SUBROUTINE iom_p3d( cdname, pfield3d )
1045      CHARACTER(LEN=*)                , INTENT(in) ::   cdname
1046      REAL(wp),       DIMENSION(:,:,:), INTENT(in) ::   pfield3d
1047#if defined key_iomput
1048      CALL xios_send_field(cdname, pfield3d)
1049#else
1050      IF( .FALSE. )   WRITE(numout,*) cdname, pfield3d   ! useless test to avoid compilation warnings
1051#endif
1052   END SUBROUTINE iom_p3d
1053   !!----------------------------------------------------------------------
1054
1055#if defined key_iomput
1056
1057   SUBROUTINE iom_set_domain_attr( cdid, ni_glo, nj_glo, ibegin, jbegin, ni, nj, zoom_ibegin, zoom_jbegin, zoom_ni, zoom_nj,   &
1058      &                                    data_dim, data_ibegin, data_ni, data_jbegin, data_nj, lonvalue, latvalue, mask )
1059      CHARACTER(LEN=*)                 , INTENT(in) ::   cdid
1060      INTEGER                , OPTIONAL, INTENT(in) ::   ni_glo, nj_glo, ibegin, jbegin, ni, nj
1061      INTEGER                , OPTIONAL, INTENT(in) ::   data_dim, data_ibegin, data_ni, data_jbegin, data_nj
1062      INTEGER                , OPTIONAL, INTENT(in) ::   zoom_ibegin, zoom_jbegin, zoom_ni, zoom_nj
1063      REAL(wp), DIMENSION(:) , OPTIONAL, INTENT(in) ::   lonvalue, latvalue
1064      LOGICAL, DIMENSION(:,:), OPTIONAL, INTENT(in) ::   mask
1065
1066      IF ( xios_is_valid_domain     (cdid) ) THEN
1067         CALL xios_set_domain_attr     ( cdid, ni_glo=ni_glo, nj_glo=nj_glo, ibegin=ibegin, jbegin=jbegin, ni=ni, nj=nj,   &
1068            &    data_dim=data_dim, data_ibegin=data_ibegin, data_ni=data_ni, data_jbegin=data_jbegin, data_nj=data_nj ,   &
1069            &    zoom_ibegin=zoom_ibegin, zoom_jbegin=zoom_jbegin, zoom_ni=zoom_ni, zoom_nj=zoom_nj,                       &
1070            &    lonvalue=lonvalue, latvalue=latvalue,mask=mask )
1071      ENDIF
1072
1073      IF ( xios_is_valid_domaingroup(cdid) ) THEN
1074         CALL xios_set_domaingroup_attr( cdid, ni_glo=ni_glo, nj_glo=nj_glo, ibegin=ibegin, jbegin=jbegin, ni=ni, nj=nj,   &
1075            &    data_dim=data_dim, data_ibegin=data_ibegin, data_ni=data_ni, data_jbegin=data_jbegin, data_nj=data_nj ,   &
1076            &    zoom_ibegin=zoom_ibegin, zoom_jbegin=zoom_jbegin, zoom_ni=zoom_ni, zoom_nj=zoom_nj,                       &
1077            &    lonvalue=lonvalue, latvalue=latvalue,mask=mask )
1078      ENDIF
1079      CALL xios_solve_inheritance()
1080
1081   END SUBROUTINE iom_set_domain_attr
1082
1083
1084   SUBROUTINE iom_set_axis_attr( cdid, paxis )
1085      CHARACTER(LEN=*)      , INTENT(in) ::   cdid
1086      REAL(wp), DIMENSION(:), INTENT(in) ::   paxis
1087      IF ( xios_is_valid_axis     (cdid) )   CALL xios_set_axis_attr     ( cdid, size=size(paxis),value=paxis )
1088      IF ( xios_is_valid_axisgroup(cdid) )   CALL xios_set_axisgroup_attr( cdid, size=size(paxis),value=paxis )
1089      CALL xios_solve_inheritance()
1090   END SUBROUTINE iom_set_axis_attr
1091
1092
1093   SUBROUTINE iom_set_field_attr( cdid, freq_op, freq_offset )
1094      CHARACTER(LEN=*)          , INTENT(in) ::   cdid
1095      CHARACTER(LEN=*),OPTIONAL , INTENT(in) ::   freq_op
1096      CHARACTER(LEN=*),OPTIONAL , INTENT(in) ::   freq_offset
1097      IF ( xios_is_valid_field     (cdid) )   CALL xios_set_field_attr     ( cdid, freq_op=freq_op, freq_offset=freq_offset )
1098      IF ( xios_is_valid_fieldgroup(cdid) )   CALL xios_set_fieldgroup_attr( cdid, freq_op=freq_op, freq_offset=freq_offset )
1099      CALL xios_solve_inheritance()
1100   END SUBROUTINE iom_set_field_attr
1101
1102
1103   SUBROUTINE iom_set_file_attr( cdid, name, name_suffix )
1104      CHARACTER(LEN=*)          , INTENT(in) ::   cdid
1105      CHARACTER(LEN=*),OPTIONAL , INTENT(in) ::   name, name_suffix
1106      IF ( xios_is_valid_file     (cdid) )   CALL xios_set_file_attr     ( cdid, name=name, name_suffix=name_suffix )
1107      IF ( xios_is_valid_filegroup(cdid) )   CALL xios_set_filegroup_attr( cdid, name=name, name_suffix=name_suffix )
1108      CALL xios_solve_inheritance()
1109   END SUBROUTINE iom_set_file_attr
1110
1111
1112   SUBROUTINE iom_get_file_attr( cdid, name, name_suffix, output_freq )
1113      CHARACTER(LEN=*)          , INTENT(in ) ::   cdid
1114      CHARACTER(LEN=*),OPTIONAL , INTENT(out) ::   name, name_suffix, output_freq
1115      LOGICAL                                 ::   llexist1,llexist2,llexist3
1116      !---------------------------------------------------------------------
1117      IF( PRESENT( name        ) )   name = ''          ! default values
1118      IF( PRESENT( name_suffix ) )   name_suffix = ''
1119      IF( PRESENT( output_freq ) )   output_freq = ''
1120      IF ( xios_is_valid_file     (cdid) ) THEN
1121         CALL xios_solve_inheritance()
1122         CALL xios_is_defined_file_attr     ( cdid, name = llexist1, name_suffix = llexist2, output_freq = llexist3)
1123         IF(llexist1)   CALL xios_get_file_attr     ( cdid, name = name )
1124         IF(llexist2)   CALL xios_get_file_attr     ( cdid, name_suffix = name_suffix )
1125         IF(llexist3)   CALL xios_get_file_attr     ( cdid, output_freq = output_freq )
1126      ENDIF
1127      IF ( xios_is_valid_filegroup(cdid) ) THEN
1128         CALL xios_solve_inheritance()
1129         CALL xios_is_defined_filegroup_attr( cdid, name = llexist1, name_suffix = llexist2, output_freq = llexist3)
1130         IF(llexist1)   CALL xios_get_filegroup_attr( cdid, name = name )
1131         IF(llexist2)   CALL xios_get_filegroup_attr( cdid, name_suffix = name_suffix )
1132         IF(llexist3)   CALL xios_get_filegroup_attr( cdid, output_freq = output_freq )
1133      ENDIF
1134   END SUBROUTINE iom_get_file_attr
1135
1136
1137   SUBROUTINE iom_set_grid_attr( cdid, mask )
1138      CHARACTER(LEN=*)                   , INTENT(in) ::   cdid
1139      LOGICAL, DIMENSION(:,:,:), OPTIONAL, INTENT(in) ::   mask
1140      IF ( xios_is_valid_grid     (cdid) )   CALL xios_set_grid_attr     ( cdid, mask=mask )
1141      IF ( xios_is_valid_gridgroup(cdid) )   CALL xios_set_gridgroup_attr( cdid, mask=mask )
1142      CALL xios_solve_inheritance()
1143   END SUBROUTINE iom_set_grid_attr
1144
1145   SUBROUTINE iom_setkt( kt, cdname )
1146      INTEGER         , INTENT(in) ::   kt 
1147      CHARACTER(LEN=*), INTENT(in) ::   cdname
1148      !     
1149      CALL iom_swap( cdname )   ! swap to cdname context
1150      CALL xios_update_calendar(kt)
1151      IF( cdname /= "nemo" ) CALL iom_swap( "nemo" )   ! return back to nemo context
1152      !
1153   END SUBROUTINE iom_setkt
1154
1155   SUBROUTINE iom_context_finalize( cdname )
1156      CHARACTER(LEN=*), INTENT(in) :: cdname
1157      !     
1158      CALL iom_swap( cdname )   ! swap to cdname context
1159      CALL xios_context_finalize() ! finalize the context
1160      IF( cdname /= "nemo" ) CALL iom_swap( "nemo" )   ! return back to nemo context
1161      !
1162   END SUBROUTINE iom_context_finalize
1163
1164
1165   SUBROUTINE set_grid( cdgrd, plon, plat )
1166      !!----------------------------------------------------------------------
1167      !!                     ***  ROUTINE set_grid  ***
1168      !!
1169      !! ** Purpose :   define horizontal grids
1170      !!
1171      !!----------------------------------------------------------------------
1172      CHARACTER(LEN=1)            , INTENT(in) ::   cdgrd
1173      REAL(wp), DIMENSION(jpi,jpj), INTENT(in) ::   plon
1174      REAL(wp), DIMENSION(jpi,jpj), INTENT(in) ::   plat
1175      !
1176      REAL(wp), DIMENSION(jpi,jpj,jpk) ::   zmask
1177      INTEGER  :: ni,nj
1178     
1179      ni=nlei-nldi+1 ; nj=nlej-nldj+1
1180
1181      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)
1182      CALL iom_set_domain_attr("grid_"//cdgrd, data_dim=2, data_ibegin = 1-nldi, data_ni = jpi, data_jbegin = 1-nldj, data_nj = jpj)
1183      CALL iom_set_domain_attr("grid_"//cdgrd, lonvalue = RESHAPE(plon(nldi:nlei, nldj:nlej),(/ ni*nj /)),   &
1184         &                                     latvalue = RESHAPE(plat(nldi:nlei, nldj:nlej),(/ ni*nj /))) 
1185
1186      IF ( ln_mskland ) THEN
1187         ! mask land points, keep values on coast line -> specific mask for U, V and W points
1188         SELECT CASE ( cdgrd )
1189         CASE('T')   ;   zmask(:,:,:)       = tmask(:,:,:)
1190         CASE('U')   ;   zmask(2:jpim1,:,:) = tmask(2:jpim1,:,:) + tmask(3:jpi,:,:)   ;   CALL lbc_lnk( zmask, 'U', 1. )
1191         CASE('V')   ;   zmask(:,2:jpjm1,:) = tmask(:,2:jpjm1,:) + tmask(:,3:jpi,:)   ;   CALL lbc_lnk( zmask, 'V', 1. )
1192         CASE('W')   ;   zmask(:,:,2:jpk  ) = tmask(:,:,1:jpkm1) + tmask(:,:,2:jpk)   ;   zmask(:,:,1) = tmask(:,:,1)
1193         END SELECT
1194         !
1195         CALL iom_set_domain_attr( "grid_"//cdgrd       , mask = RESHAPE(zmask(nldi:nlei,nldj:nlej,1),(/ni,nj    /)) /= 0. )
1196         CALL iom_set_grid_attr  ( "grid_"//cdgrd//"_3D", mask = RESHAPE(zmask(nldi:nlei,nldj:nlej,:),(/ni,nj,jpk/)) /= 0. )
1197      ENDIF
1198     
1199   END SUBROUTINE set_grid
1200
1201
1202   SUBROUTINE set_scalar
1203      !!----------------------------------------------------------------------
1204      !!                     ***  ROUTINE set_scalar  ***
1205      !!
1206      !! ** Purpose :   define fake grids for scalar point
1207      !!
1208      !!----------------------------------------------------------------------
1209      REAL(wp), DIMENSION(1,1) ::   zz = 1.
1210      !!----------------------------------------------------------------------
1211      CALL iom_set_domain_attr('scalarpoint', ni_glo=jpnij, nj_glo=1, ibegin=narea, jbegin=1, ni=1, nj=1)
1212      CALL iom_set_domain_attr('scalarpoint', data_dim=1)
1213      CALL iom_set_domain_attr('scalarpoint', lonvalue=(/ zz /), latvalue=(/ zz /))
1214
1215   END SUBROUTINE set_scalar
1216
1217
1218   SUBROUTINE set_xmlatt
1219      !!----------------------------------------------------------------------
1220      !!                     ***  ROUTINE set_xmlatt  ***
1221      !!
1222      !! ** Purpose :   automatic definitions of some of the xml attributs...
1223      !!
1224      !!----------------------------------------------------------------------
1225      CHARACTER(len=1),DIMENSION( 3) ::   clgrd                    ! suffix name
1226      CHARACTER(len=256)             ::   clsuff                   ! suffix name
1227      CHARACTER(len=1)               ::   cl1                      ! 1 character
1228      CHARACTER(len=2)               ::   cl2                      ! 1 character
1229      INTEGER                        ::   ji, jg                   ! loop counters
1230      INTEGER                        ::   ix, iy                   ! i-,j- index
1231      REAL(wp)        ,DIMENSION(11) ::   zlontao                  ! longitudes of tao    moorings
1232      REAL(wp)        ,DIMENSION( 7) ::   zlattao                  ! latitudes  of tao    moorings
1233      REAL(wp)        ,DIMENSION( 4) ::   zlonrama                 ! longitudes of rama   moorings
1234      REAL(wp)        ,DIMENSION(11) ::   zlatrama                 ! latitudes  of rama   moorings
1235      REAL(wp)        ,DIMENSION( 3) ::   zlonpira                 ! longitudes of pirata moorings
1236      REAL(wp)        ,DIMENSION( 9) ::   zlatpira                 ! latitudes  of pirata moorings
1237      !!----------------------------------------------------------------------
1238      !
1239      ! frequency of the call of iom_put (attribut: freq_op)
1240      WRITE(cl1,'(i1)')        1   ;   CALL iom_set_field_attr('field_definition', freq_op = cl1//'ts', freq_offset='0ts')
1241      WRITE(cl1,'(i1)')  nn_fsbc   ;   CALL iom_set_field_attr('SBC'             , freq_op = cl1//'ts', freq_offset='0ts')
1242      WRITE(cl1,'(i1)')  nn_fsbc   ;   CALL iom_set_field_attr('SBC_scalar'      , freq_op = cl1//'ts', freq_offset='0ts')
1243      WRITE(cl1,'(i1)') nn_dttrc   ;   CALL iom_set_field_attr('ptrc_T'          , freq_op = cl1//'ts', freq_offset='0ts')
1244      WRITE(cl1,'(i1)') nn_dttrc   ;   CALL iom_set_field_attr('diad_T'          , freq_op = cl1//'ts', freq_offset='0ts')
1245       
1246      ! output file names (attribut: name)
1247      DO ji = 1, 9
1248         WRITE(cl1,'(i1)') ji 
1249         CALL iom_update_file_name('file'//cl1)
1250      END DO
1251      DO ji = 1, 99
1252         WRITE(cl2,'(i2.2)') ji 
1253         CALL iom_update_file_name('file'//cl2)
1254      END DO
1255
1256      ! Zooms...
1257      clgrd = (/ 'T', 'U', 'W' /) 
1258      DO jg = 1, SIZE(clgrd)                                                                   ! grid type
1259         cl1 = clgrd(jg)
1260         ! Equatorial section (attributs: jbegin, ni, name_suffix)
1261         CALL dom_ngb( 0., 0., ix, iy, cl1 )
1262         CALL iom_set_domain_attr ('Eq'//cl1, zoom_jbegin=iy, zoom_ni=jpiglo)
1263         CALL iom_get_file_attr   ('Eq'//cl1, name_suffix = clsuff             )
1264         CALL iom_set_file_attr   ('Eq'//cl1, name_suffix = TRIM(clsuff)//'_Eq')
1265         CALL iom_update_file_name('Eq'//cl1)
1266      END DO
1267      ! TAO moorings (attributs: ibegin, jbegin, name_suffix)
1268      zlontao = (/ 137.0, 147.0, 156.0, 165.0, -180.0, -170.0, -155.0, -140.0, -125.0, -110.0, -95.0 /)
1269      zlattao = (/  -8.0,  -5.0,  -2.0,   0.0,    2.0,    5.0,    8.0 /)
1270      CALL set_mooring( zlontao, zlattao )
1271      ! RAMA moorings (attributs: ibegin, jbegin, name_suffix)
1272      zlonrama = (/  55.0,  67.0, 80.5, 90.0 /)
1273      zlatrama = (/ -16.0, -12.0, -8.0, -4.0, -1.5, 0.0, 1.5, 4.0, 8.0, 12.0, 15.0 /)
1274      CALL set_mooring( zlonrama, zlatrama )
1275      ! PIRATA moorings (attributs: ibegin, jbegin, name_suffix)
1276      zlonpira = (/ -38.0, -23.0, -10.0 /)
1277      zlatpira = (/ -19.0, -14.0,  -8.0, 0.0, 4.0, 8.0, 12.0, 15.0, 20.0 /)
1278      CALL set_mooring( zlonpira, zlatpira )
1279     
1280   END SUBROUTINE set_xmlatt
1281
1282
1283   SUBROUTINE set_mooring( plon, plat)
1284      !!----------------------------------------------------------------------
1285      !!                     ***  ROUTINE set_mooring  ***
1286      !!
1287      !! ** Purpose :   automatic definitions of moorings xml attributs...
1288      !!
1289      !!----------------------------------------------------------------------
1290      REAL(wp), DIMENSION(:), INTENT(in) ::  plon, plat           ! longitudes/latitudes oft the mooring
1291      !
1292!!$      CHARACTER(len=1),DIMENSION(4) ::   clgrd = (/ 'T', 'U', 'V', 'W' /)   ! suffix name
1293      CHARACTER(len=1),DIMENSION(1) ::   clgrd = (/ 'T' /)        ! suffix name
1294      CHARACTER(len=256)            ::   clname                   ! file name
1295      CHARACTER(len=256)            ::   clsuff                   ! suffix name
1296      CHARACTER(len=1)              ::   cl1                      ! 1 character
1297      CHARACTER(len=6)              ::   clon,clat                ! name of longitude, latitude
1298      INTEGER                       ::   ji, jj, jg               ! loop counters
1299      INTEGER                       ::   ix, iy                   ! i-,j- index
1300      REAL(wp)                      ::   zlon, zlat
1301      !!----------------------------------------------------------------------
1302      DO jg = 1, SIZE(clgrd)
1303         cl1 = clgrd(jg)
1304         DO ji = 1, SIZE(plon)
1305            DO jj = 1, SIZE(plat)
1306               zlon = plon(ji)
1307               zlat = plat(jj)
1308               ! modifications for RAMA moorings
1309               IF( zlon ==  67. .AND. zlat ==  15. )   zlon =  65.
1310               IF( zlon ==  90. .AND. zlat <=  -4. )   zlon =  95.
1311               IF( zlon ==  95. .AND. zlat ==  -4. )   zlat =  -5.
1312               ! modifications for PIRATA moorings
1313               IF( zlon == -38. .AND. zlat == -19. )   zlon = -34.
1314               IF( zlon == -38. .AND. zlat == -14. )   zlon = -32.
1315               IF( zlon == -38. .AND. zlat ==  -8. )   zlon = -30.
1316               IF( zlon == -38. .AND. zlat ==   0. )   zlon = -35.
1317               IF( zlon == -23. .AND. zlat ==  20. )   zlat =  21.
1318               IF( zlon == -10. .AND. zlat == -14. )   zlat = -10.
1319               IF( zlon == -10. .AND. zlat ==  -8. )   zlat =  -6.
1320               IF( zlon == -10. .AND. zlat ==   4. ) THEN   ;   zlon = 0.   ;   zlat = 0.   ;   ENDIF
1321               CALL dom_ngb( zlon, zlat, ix, iy, cl1 )
1322               IF( zlon >= 0. ) THEN 
1323                  IF( zlon == REAL(NINT(zlon), wp) ) THEN   ;   WRITE(clon, '(i3,  a)') NINT( zlon), 'e'
1324                  ELSE                                      ;   WRITE(clon, '(f5.1,a)')       zlon , 'e'
1325                  ENDIF
1326               ELSE             
1327                  IF( zlon == REAL(NINT(zlon), wp) ) THEN   ;   WRITE(clon, '(i3,  a)') NINT(-zlon), 'w'
1328                  ELSE                                      ;   WRITE(clon, '(f5.1,a)')      -zlon , 'w'
1329                  ENDIF
1330               ENDIF
1331               IF( zlat >= 0. ) THEN 
1332                  IF( zlat == REAL(NINT(zlat), wp) ) THEN   ;   WRITE(clat, '(i2,  a)') NINT( zlat), 'n'
1333                  ELSE                                      ;   WRITE(clat, '(f4.1,a)')       zlat , 'n'
1334                  ENDIF
1335               ELSE             
1336                  IF( zlat == REAL(NINT(zlat), wp) ) THEN   ;   WRITE(clat, '(i2,  a)') NINT(-zlat), 's'
1337                  ELSE                                      ;   WRITE(clat, '(f4.1,a)')      -zlat , 's'
1338                  ENDIF
1339               ENDIF
1340               clname = TRIM(ADJUSTL(clat))//TRIM(ADJUSTL(clon))
1341               CALL iom_set_domain_attr (TRIM(clname)//cl1, zoom_ibegin= ix, zoom_jbegin= iy)
1342               CALL iom_get_file_attr   (TRIM(clname)//cl1, name_suffix = clsuff                         )
1343               CALL iom_set_file_attr   (TRIM(clname)//cl1, name_suffix = TRIM(clsuff)//'_'//TRIM(clname))
1344               CALL iom_update_file_name(TRIM(clname)//cl1)
1345            END DO
1346         END DO
1347      END DO
1348     
1349   END SUBROUTINE set_mooring
1350
1351   
1352   SUBROUTINE iom_update_file_name( cdid )
1353      !!----------------------------------------------------------------------
1354      !!                     ***  ROUTINE iom_update_file_name  ***
1355      !!
1356      !! ** Purpose :   
1357      !!
1358      !!----------------------------------------------------------------------
1359      CHARACTER(LEN=*)          , INTENT(in) ::   cdid
1360      !
1361      CHARACTER(LEN=256) ::   clname
1362      CHARACTER(LEN=20)  ::   clfreq
1363      CHARACTER(LEN=20)  ::   cldate
1364      INTEGER            ::   idx
1365      INTEGER            ::   jn
1366      INTEGER            ::   itrlen
1367      INTEGER            ::   iyear, imonth, iday, isec
1368      REAL(wp)           ::   zsec
1369      LOGICAL            ::   llexist
1370      !!----------------------------------------------------------------------
1371
1372      DO jn = 1,2
1373
1374         IF( jn == 1 )   CALL iom_get_file_attr( cdid, name        = clname, output_freq = clfreq )
1375         IF( jn == 2 )   CALL iom_get_file_attr( cdid, name_suffix = clname )
1376
1377         IF ( TRIM(clname) /= '' ) THEN
1378
1379            idx = INDEX(clname,'@expname@') + INDEX(clname,'@EXPNAME@')
1380            DO WHILE ( idx /= 0 ) 
1381               clname = clname(1:idx-1)//TRIM(cexper)//clname(idx+9:LEN_TRIM(clname))
1382               idx = INDEX(clname,'@expname@') + INDEX(clname,'@EXPNAME@')
1383            END DO
1384
1385            idx = INDEX(clname,'@freq@') + INDEX(clname,'@FREQ@')
1386            DO WHILE ( idx /= 0 ) 
1387               IF ( TRIM(clfreq) /= '' ) THEN
1388                  itrlen = LEN_TRIM(clfreq)
1389                  IF ( clfreq(itrlen-1:itrlen) == 'mo' ) clfreq = clfreq(1:itrlen-1)
1390                  clname = clname(1:idx-1)//TRIM(clfreq)//clname(idx+6:LEN_TRIM(clname))
1391               ELSE
1392                  CALL ctl_stop('error in the name of file id '//TRIM(cdid),   &
1393                     & ' attribute output_freq is undefined -> cannot replace @freq@ in '//TRIM(clname) )
1394               ENDIF
1395               idx = INDEX(clname,'@freq@') + INDEX(clname,'@FREQ@')
1396            END DO
1397
1398            idx = INDEX(clname,'@startdate@') + INDEX(clname,'@STARTDATE@')
1399            DO WHILE ( idx /= 0 ) 
1400               cldate = iom_sdate( fjulday - rdttra(1) / rday )
1401               clname = clname(1:idx-1)//TRIM(cldate)//clname(idx+11:LEN_TRIM(clname))
1402               idx = INDEX(clname,'@startdate@') + INDEX(clname,'@STARTDATE@')
1403            END DO
1404
1405            idx = INDEX(clname,'@startdatefull@') + INDEX(clname,'@STARTDATEFULL@')
1406            DO WHILE ( idx /= 0 ) 
1407               cldate = iom_sdate( fjulday - rdttra(1) / rday, ldfull = .TRUE. )
1408               clname = clname(1:idx-1)//TRIM(cldate)//clname(idx+15:LEN_TRIM(clname))
1409               idx = INDEX(clname,'@startdatefull@') + INDEX(clname,'@STARTDATEFULL@')
1410            END DO
1411
1412            idx = INDEX(clname,'@enddate@') + INDEX(clname,'@ENDDATE@')
1413            DO WHILE ( idx /= 0 ) 
1414               cldate = iom_sdate( fjulday + rdttra(1) / rday * REAL( nitend - nit000, wp ), ld24 = .TRUE. )
1415               clname = clname(1:idx-1)//TRIM(cldate)//clname(idx+9:LEN_TRIM(clname))
1416               idx = INDEX(clname,'@enddate@') + INDEX(clname,'@ENDDATE@')
1417            END DO
1418
1419            idx = INDEX(clname,'@enddatefull@') + INDEX(clname,'@ENDDATEFULL@')
1420            DO WHILE ( idx /= 0 ) 
1421               cldate = iom_sdate( fjulday + rdttra(1) / rday * REAL( nitend - nit000, wp ), ld24 = .TRUE., ldfull = .TRUE. )
1422               clname = clname(1:idx-1)//TRIM(cldate)//clname(idx+13:LEN_TRIM(clname))
1423               idx = INDEX(clname,'@enddatefull@') + INDEX(clname,'@ENDDATEFULL@')
1424            END DO
1425
1426            IF( jn == 1 )   CALL iom_set_file_attr( cdid, name        = clname )
1427            IF( jn == 2 )   CALL iom_set_file_attr( cdid, name_suffix = clname )
1428
1429         ENDIF
1430
1431      END DO
1432
1433   END SUBROUTINE iom_update_file_name
1434
1435
1436   FUNCTION iom_sdate( pjday, ld24, ldfull )
1437      !!----------------------------------------------------------------------
1438      !!                     ***  ROUTINE iom_sdate  ***
1439      !!
1440      !! ** Purpose :   send back the date corresponding to the given julian day
1441      !!
1442      !!----------------------------------------------------------------------
1443      REAL(wp), INTENT(in   )           ::   pjday         ! julian day
1444      LOGICAL , INTENT(in   ), OPTIONAL ::   ld24          ! true to force 24:00 instead of 00:00
1445      LOGICAL , INTENT(in   ), OPTIONAL ::   ldfull        ! true to get the compleate date: yyyymmdd_hh:mm:ss
1446      !
1447      CHARACTER(LEN=20) ::   iom_sdate
1448      CHARACTER(LEN=50) ::   clfmt                         !  format used to write the date
1449      INTEGER           ::   iyear, imonth, iday, ihour, iminute, isec
1450      REAL(wp)          ::   zsec
1451      LOGICAL           ::   ll24, llfull
1452      !
1453      IF( PRESENT(ld24) ) THEN   ;   ll24 = ld24
1454      ELSE                       ;   ll24 = .FALSE.
1455      ENDIF
1456
1457      IF( PRESENT(ldfull) ) THEN   ;   llfull = ldfull
1458      ELSE                         ;   llfull = .FALSE.
1459      ENDIF
1460
1461      CALL ju2ymds( pjday, iyear, imonth, iday, zsec )
1462      isec = NINT(zsec)
1463
1464      IF ( ll24 .AND. isec == 0 ) THEN   ! 00:00 of the next day -> move to 24:00 of the current day
1465         CALL ju2ymds( pjday - 1., iyear, imonth, iday, zsec )
1466         isec = 86400
1467      ENDIF
1468
1469      IF( iyear < 10000 ) THEN   ;   clfmt = "i4.4,2i2.2"                ! format used to write the date
1470      ELSE                       ;   WRITE(clfmt, "('i',i1,',2i2.2')") INT(LOG10(REAL(iyear,wp))) + 1
1471      ENDIF
1472     
1473      IF( llfull ) THEN
1474         clfmt = TRIM(clfmt)//",'_',i2.2,':',i2.2,':',i2.2"
1475         ihour   = isec / 3600
1476         isec    = MOD(isec, 3600)
1477         iminute = isec / 60
1478         isec    = MOD(isec, 60)
1479         WRITE(iom_sdate, '('//TRIM(clfmt)//')') iyear, imonth, iday, ihour, iminute, isec    ! date of the end of run
1480      ELSE
1481         WRITE(iom_sdate, '('//TRIM(clfmt)//')') iyear, imonth, iday                          ! date of the end of run
1482      ENDIF
1483
1484   END FUNCTION iom_sdate
1485
1486#else
1487
1488
1489   SUBROUTINE iom_setkt( kt, cdname )
1490      INTEGER         , INTENT(in)::   kt 
1491      CHARACTER(LEN=*), INTENT(in) ::   cdname
1492      IF( .FALSE. )   WRITE(numout,*) kt, cdname   ! useless test to avoid compilation warnings
1493   END SUBROUTINE iom_setkt
1494
1495   SUBROUTINE iom_context_finalize( cdname )
1496      CHARACTER(LEN=*), INTENT(in) ::   cdname
1497      IF( .FALSE. )   WRITE(numout,*)  cdname   ! useless test to avoid compilation warnings
1498   END SUBROUTINE iom_context_finalize
1499
1500#endif
1501   
1502   !!======================================================================
1503END MODULE iom
Note: See TracBrowser for help on using the repository browser.