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/NERC/dev_r6998_ORCHESTRA/NEMOGCM/CONFIG/ORCHESTRA/MY_SRC – NEMO

source: branches/NERC/dev_r6998_ORCHESTRA/NEMOGCM/CONFIG/ORCHESTRA/MY_SRC/iom.F90 @ 7029

Last change on this file since 7029 was 7029, checked in by jamesharle, 8 years ago

Adding ORCHESTRA configuration
Merging with branches/2016/dev_r5549_BDY_ZEROGRAD
Merging with branches/2016/dev_r5840_BDY_MSK
Merging with branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP

File size: 97.7 KB
Line 
1MODULE iom
2   !!=====================================================================
3   !!                    ***  MODULE  iom ***
4   !! Input/Output manager :  Library to read input files
5   !!====================================================================
6   !! History :  2.0  ! 2005-12  (J. Belier) Original code
7   !!            2.0  ! 2006-02  (S. Masson) Adaptation to NEMO
8   !!            3.0  ! 2007-07  (D. Storkey) Changes to iom_gettime
9   !!            3.4  ! 2012-12  (R. Bourdalle-Badie and G. Reffray)  add C1D case 
10   !!            3.6  ! 2014-15  DIMG format removed
11   !!            3.6  ! 2015-15  (J. Harle) Added procedure to read REAL attributes
12   !!--------------------------------------------------------------------
13
14   !!--------------------------------------------------------------------
15   !!   iom_open       : open a file read only
16   !!   iom_close      : close a file or all files opened by iom
17   !!   iom_get        : read a field (interfaced to several routines)
18   !!   iom_gettime    : read the time axis cdvar in the file
19   !!   iom_varid      : get the id of a variable in a file
20   !!   iom_rstput     : write a field in a restart file (interfaced to several routines)
21   !!--------------------------------------------------------------------
22   USE dom_oce         ! ocean space and time domain
23   USE c1d             ! 1D vertical configuration
24   USE flo_oce         ! floats module declarations
25   USE lbclnk          ! lateal boundary condition / mpp exchanges
26   USE iom_def         ! iom variables definitions
27   USE iom_nf90        ! NetCDF format with native NetCDF library
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#if defined key_lim3
35   USE ice    , ONLY :   jpl
36#elif defined key_lim2
37   USE par_ice_2
38#endif
39   USE domngb          ! ocean space and time domain
40   USE phycst          ! physical constants
41   USE dianam          ! build name of file
42   USE xios
43# endif
44   USE ioipsl, ONLY :  ju2ymds    ! for calendar
45   USE crs             ! Grid coarsening
46
47   IMPLICIT NONE
48   PUBLIC   !   must be public to be able to access iom_def through iom
49   
50#if defined key_iomput
51   LOGICAL, PUBLIC, PARAMETER ::   lk_iomput = .TRUE.        !: iom_put flag
52#else
53   LOGICAL, PUBLIC, PARAMETER ::   lk_iomput = .FALSE.       !: iom_put flag
54#endif
55   PUBLIC iom_init, iom_swap, iom_open, iom_close, iom_setkt, iom_varid, iom_get, iom_gettime, iom_rstput, iom_put
56   PUBLIC iom_getatt, iom_use, iom_context_finalize
57
58   PRIVATE iom_rp0d, iom_rp1d, iom_rp2d, iom_rp3d
59   PRIVATE iom_g0d, iom_g1d, iom_g2d, iom_g3d, iom_get_123d
60   PRIVATE iom_p1d, iom_p2d, iom_p3d
61#if defined key_iomput
62   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
63   PRIVATE set_grid, set_grid_bounds, set_scalar, set_xmlatt, set_mooring, iom_update_file_name, iom_sdate
64# endif
65
66   INTERFACE iom_get
67      MODULE PROCEDURE iom_g0d, iom_g1d, iom_g2d, iom_g3d
68   END INTERFACE
69   INTERFACE iom_getatt
70      MODULE PROCEDURE iom_g0d_intatt, iom_g0d_ratt
71   END INTERFACE
72   INTERFACE iom_rstput
73      MODULE PROCEDURE iom_rp0d, iom_rp1d, iom_rp2d, iom_rp3d
74   END INTERFACE
75  INTERFACE iom_put
76     MODULE PROCEDURE iom_p0d, iom_p1d, iom_p2d, iom_p3d
77  END INTERFACE
78
79   !!----------------------------------------------------------------------
80   !! NEMO/OPA 3.3 , NEMO Consortium (2010)
81   !! $Id: iom.F90 6519 2016-05-11 10:50:34Z timgraham $
82   !! Software governed by the CeCILL licence (NEMOGCM/NEMO_CeCILL.txt)
83   !!----------------------------------------------------------------------
84
85CONTAINS
86
87   SUBROUTINE iom_init( cdname ) 
88      !!----------------------------------------------------------------------
89      !!                     ***  ROUTINE   ***
90      !!
91      !! ** Purpose :   
92      !!
93      !!----------------------------------------------------------------------
94      CHARACTER(len=*), INTENT(in)  :: cdname
95#if defined key_iomput
96#if ! defined key_xios2
97      TYPE(xios_time)     :: dtime    = xios_time(0, 0, 0, 0, 0, 0)
98      CHARACTER(len=19)   :: cldate 
99#else
100      TYPE(xios_duration) :: dtime    = xios_duration(0, 0, 0, 0, 0, 0)
101      TYPE(xios_date)     :: start_date
102#endif
103      CHARACTER(len=10)   :: clname
104      INTEGER             :: ji
105      !
106      REAL(wp), ALLOCATABLE, DIMENSION(:,:) :: z_bnds
107      !!----------------------------------------------------------------------
108#if ! defined key_xios2
109      ALLOCATE( z_bnds(jpk,2) )
110#else
111      ALLOCATE( z_bnds(2,jpk) )
112#endif
113
114      clname = cdname
115      IF( TRIM(Agrif_CFixed()) /= '0' )   clname = TRIM(Agrif_CFixed())//"_"//TRIM(cdname)
116      CALL xios_context_initialize(TRIM(clname), mpi_comm_opa)
117      CALL iom_swap( cdname )
118
119      ! calendar parameters
120#if ! defined key_xios2
121      SELECT CASE ( nleapy )        ! Choose calendar for IOIPSL
122      CASE ( 1)   ;   CALL xios_set_context_attr(TRIM(clname), calendar_type= "Gregorian")
123      CASE ( 0)   ;   CALL xios_set_context_attr(TRIM(clname), calendar_type= "NoLeap")
124      CASE (30)   ;   CALL xios_set_context_attr(TRIM(clname), calendar_type= "D360")
125      END SELECT
126      WRITE(cldate,"(i4.4,'-',i2.2,'-',i2.2,' ',i2.2,':',i2.2,':00')") nyear,nmonth,nday,nhour,nminute
127      CALL xios_set_context_attr(TRIM(clname), start_date=cldate )
128#else
129      ! Calendar type is now defined in xml file
130      SELECT CASE ( nleapy )        ! Choose calendar for IOIPSL
131      CASE ( 1)   ; CALL xios_define_calendar( TYPE = "Gregorian", time_origin = xios_date(1900,01,01,00,00,00), &
132          &                                    start_date = xios_date(nyear,nmonth,nday,0,0,0) )
133      CASE ( 0)   ; CALL xios_define_calendar( TYPE = "NoLeap"   , time_origin = xios_date(1900,01,01,00,00,00), &
134          &                                    start_date = xios_date(nyear,nmonth,nday,0,0,0) )
135      CASE (30)   ; CALL xios_define_calendar( TYPE = "D360"     , time_origin = xios_date(1900,01,01,00,00,00), &
136          &                                    start_date = xios_date(nyear,nmonth,nday,0,0,0) )
137      END SELECT
138#endif
139      ! horizontal grid definition
140      CALL set_scalar
141
142      IF( TRIM(cdname) == TRIM(cxios_context) ) THEN 
143         CALL set_grid( "T", glamt, gphit ) 
144         CALL set_grid( "U", glamu, gphiu )
145         CALL set_grid( "V", glamv, gphiv )
146         CALL set_grid( "W", glamt, gphit )
147         CALL set_grid_znl( gphit )
148         !
149         IF( ln_cfmeta ) THEN   ! Add additional grid metadata
150            CALL iom_set_domain_attr("grid_T", area = e1e2t(nldi:nlei, nldj:nlej))
151            CALL iom_set_domain_attr("grid_U", area = e1e2u(nldi:nlei, nldj:nlej))
152            CALL iom_set_domain_attr("grid_V", area = e1e2v(nldi:nlei, nldj:nlej))
153            CALL iom_set_domain_attr("grid_W", area = e1e2t(nldi:nlei, nldj:nlej))
154            CALL set_grid_bounds( "T", glamf, gphif, glamt, gphit )
155            CALL set_grid_bounds( "U", glamv, gphiv, glamu, gphiu )
156            CALL set_grid_bounds( "V", glamu, gphiu, glamv, gphiv )
157            CALL set_grid_bounds( "W", glamf, gphif, glamt, gphit )
158         ENDIF
159      ENDIF
160
161      IF( TRIM(cdname) == TRIM(cxios_context)//"_crs" ) THEN 
162         CALL dom_grid_crs   ! Save the parent grid information  & Switch to coarse grid domain
163         !
164         CALL set_grid( "T", glamt_crs, gphit_crs ) 
165         CALL set_grid( "U", glamu_crs, gphiu_crs ) 
166         CALL set_grid( "V", glamv_crs, gphiv_crs ) 
167         CALL set_grid( "W", glamt_crs, gphit_crs ) 
168         CALL set_grid_znl( gphit_crs )
169          !
170         CALL dom_grid_glo   ! Return to parent grid domain
171         !
172         IF( ln_cfmeta ) THEN   ! Add additional grid metadata
173            CALL iom_set_domain_attr("grid_T", area = e1e2t_crs(nldi:nlei, nldj:nlej))
174            CALL iom_set_domain_attr("grid_U", area = e1u_crs(nldi:nlei, nldj:nlej) * e2u_crs(nldi:nlei, nldj:nlej))
175            CALL iom_set_domain_attr("grid_V", area = e1v_crs(nldi:nlei, nldj:nlej) * e2v_crs(nldi:nlei, nldj:nlej))
176            CALL iom_set_domain_attr("grid_W", area = e1e2t_crs(nldi:nlei, nldj:nlej))
177            CALL set_grid_bounds( "T", glamf_crs, gphif_crs, glamt_crs, gphit_crs )
178            CALL set_grid_bounds( "U", glamv_crs, gphiv_crs, glamu_crs, gphiu_crs )
179            CALL set_grid_bounds( "V", glamu_crs, gphiu_crs, glamv_crs, gphiv_crs )
180            CALL set_grid_bounds( "W", glamf_crs, gphif_crs, glamt_crs, gphit_crs )
181         ENDIF
182      ENDIF
183
184      ! vertical grid definition
185      CALL iom_set_axis_attr( "deptht", gdept_1d )
186      CALL iom_set_axis_attr( "depthu", gdept_1d )
187      CALL iom_set_axis_attr( "depthv", gdept_1d )
188      CALL iom_set_axis_attr( "depthw", gdepw_1d )
189
190      ! Add vertical grid bounds
191#if ! defined key_xios2
192      z_bnds(:      ,1) = gdepw_1d(:)
193      z_bnds(1:jpkm1,2) = gdepw_1d(2:jpk)
194      z_bnds(jpk:   ,2) = gdepw_1d(jpk) + e3t_1d(jpk)
195#else
196      z_bnds(1      ,:) = gdepw_1d(:)
197      z_bnds(2,1:jpkm1) = gdepw_1d(2:jpk)
198      z_bnds(2,jpk:   ) = gdepw_1d(jpk) + e3t_1d(jpk)
199#endif
200
201      CALL iom_set_axis_attr( "deptht", bounds=z_bnds )
202      CALL iom_set_axis_attr( "depthu", bounds=z_bnds )
203      CALL iom_set_axis_attr( "depthv", bounds=z_bnds )
204
205#if ! defined key_xios2
206      z_bnds(:    ,2)  = gdept_1d(:)
207      z_bnds(2:jpk,1)  = gdept_1d(1:jpkm1)
208      z_bnds(1    ,1)  = gdept_1d(1) - e3w_1d(1)
209#else
210      z_bnds(2,:    )  = gdept_1d(:)
211      z_bnds(1,2:jpk)  = gdept_1d(1:jpkm1)
212      z_bnds(1,1    )  = gdept_1d(1) - e3w_1d(1)
213#endif
214      CALL iom_set_axis_attr( "depthw", bounds=z_bnds )
215
216# if defined key_floats
217      CALL iom_set_axis_attr( "nfloat", (/ (REAL(ji,wp), ji=1,nfloat) /) )
218# endif
219#if defined key_lim3 || defined key_lim2
220      CALL iom_set_axis_attr( "ncatice", (/ (REAL(ji,wp), ji=1,jpl) /) )
221#endif
222      CALL iom_set_axis_attr( "icbcla", class_num )
223      CALL iom_set_axis_attr( "iax_20C", (/ REAL(20,wp) /) )
224      CALL iom_set_axis_attr( "iax_28C", (/ REAL(28,wp) /) )
225     
226      ! automatic definitions of some of the xml attributs
227      CALL set_xmlatt
228
229      ! end file definition
230      dtime%second = rdt
231      CALL xios_set_timestep(dtime)
232      CALL xios_close_context_definition()
233     
234      CALL xios_update_calendar(0)
235
236      DEALLOCATE( z_bnds )
237
238#endif
239     
240   END SUBROUTINE iom_init
241
242
243   SUBROUTINE iom_swap( cdname )
244      !!---------------------------------------------------------------------
245      !!                   ***  SUBROUTINE  iom_swap  ***
246      !!
247      !! ** Purpose :  swap context between different agrif grid for xmlio_server
248      !!---------------------------------------------------------------------
249      CHARACTER(len=*), INTENT(in) :: cdname
250#if defined key_iomput
251      TYPE(xios_context) :: nemo_hdl
252
253      IF( TRIM(Agrif_CFixed()) == '0' ) THEN
254        CALL xios_get_handle(TRIM(cdname),nemo_hdl)
255      ELSE
256        CALL xios_get_handle(TRIM(Agrif_CFixed())//"_"//TRIM(cdname),nemo_hdl)
257      ENDIF
258      !
259      CALL xios_set_current_context(nemo_hdl)
260#endif
261      !
262   END SUBROUTINE iom_swap
263
264
265   SUBROUTINE iom_open( cdname, kiomid, ldwrt, kdom, kiolib, ldstop, ldiof )
266      !!---------------------------------------------------------------------
267      !!                   ***  SUBROUTINE  iom_open  ***
268      !!
269      !! ** Purpose :  open an input file (return 0 if not found)
270      !!---------------------------------------------------------------------
271      CHARACTER(len=*), INTENT(in   )           ::   cdname   ! File name
272      INTEGER         , INTENT(  out)           ::   kiomid   ! iom identifier of the opened file
273      LOGICAL         , INTENT(in   ), OPTIONAL ::   ldwrt    ! open in write modeb          (default = .FALSE.)
274      INTEGER         , INTENT(in   ), OPTIONAL ::   kdom     ! Type of domain to be written (default = jpdom_local_noovlap)
275      INTEGER         , INTENT(in   ), OPTIONAL ::   kiolib   ! library used to open the file (default = jpnf90)
276      LOGICAL         , INTENT(in   ), OPTIONAL ::   ldstop   ! stop if open to read a non-existing file (default = .TRUE.)
277      LOGICAL         , INTENT(in   ), OPTIONAL ::   ldiof    ! Interp On the Fly, needed for AGRIF (default = .FALSE.)
278
279      CHARACTER(LEN=256)    ::   clname    ! the name of the file based on cdname [[+clcpu]+clcpu]
280      CHARACTER(LEN=256)    ::   cltmpn    ! tempory name to store clname (in writting mode)
281      CHARACTER(LEN=10)     ::   clsuffix  ! ".nc"
282      CHARACTER(LEN=15)     ::   clcpu     ! the cpu number (max jpmax_digits digits)
283      CHARACTER(LEN=256)    ::   clinfo    ! info character
284      LOGICAL               ::   llok      ! check the existence
285      LOGICAL               ::   llwrt     ! local definition of ldwrt
286      LOGICAL               ::   llnoov    ! local definition to read overlap
287      LOGICAL               ::   llstop    ! local definition of ldstop
288      LOGICAL               ::   lliof     ! local definition of ldiof
289      INTEGER               ::   iolib     ! library do we use to open the file
290      INTEGER               ::   icnt      ! counter for digits in clcpu (max = jpmax_digits)
291      INTEGER               ::   iln, ils  ! lengths of character
292      INTEGER               ::   idom      ! type of domain
293      INTEGER               ::   istop     !
294      INTEGER, DIMENSION(2,5) ::   idompar ! domain parameters:
295      ! local number of points for x,y dimensions
296      ! position of first local point for x,y dimensions
297      ! position of last local point for x,y dimensions
298      ! start halo size for x,y dimensions
299      ! end halo size for x,y dimensions
300      !---------------------------------------------------------------------
301      ! Initializations and control
302      ! =============
303      kiomid = -1
304      clinfo = '                    iom_open ~~~  '
305      istop = nstop
306      ! if iom_open is called for the first time: initialize iom_file(:)%nfid to 0
307      ! (could be done when defining iom_file in f95 but not in f90)
308      IF( Agrif_Root() ) THEN
309         IF( iom_open_init == 0 ) THEN
310            iom_file(:)%nfid = 0
311            iom_open_init = 1
312         ENDIF
313      ENDIF
314      ! do we read or write the file?
315      IF( PRESENT(ldwrt) ) THEN   ;   llwrt = ldwrt
316      ELSE                        ;   llwrt = .FALSE.
317      ENDIF
318      ! do we call ctl_stop if we try to open a non-existing file in read mode?
319      IF( PRESENT(ldstop) ) THEN   ;   llstop = ldstop
320      ELSE                         ;   llstop = .TRUE.
321      ENDIF
322      ! what library do we use to open the file?
323      IF( PRESENT(kiolib) ) THEN   ;   iolib = kiolib
324      ELSE                         ;   iolib = jpnf90
325      ENDIF
326      ! are we using interpolation on the fly?
327      IF( PRESENT(ldiof) ) THEN   ;   lliof = ldiof
328      ELSE                        ;   lliof = .FALSE.
329      ENDIF
330      ! do we read the overlap
331      ! ugly patch SM+JMM+RB to overwrite global definition in some cases
332      llnoov = (jpni * jpnj ) == jpnij .AND. .NOT. lk_agrif 
333      ! create the file name by added, if needed, TRIM(Agrif_CFixed()) and TRIM(clsuffix)
334      ! =============
335      clname   = trim(cdname)
336      IF ( .NOT. Agrif_Root() .AND. .NOT. lliof ) THEN
337         iln    = INDEX(clname,'/') 
338         cltmpn = clname(1:iln)
339         clname = clname(iln+1:LEN_TRIM(clname))
340         clname=TRIM(cltmpn)//TRIM(Agrif_CFixed())//'_'//TRIM(clname)
341      ENDIF
342      ! which suffix should we use?
343      SELECT CASE (iolib)
344      CASE (jpnf90   ) ;   clsuffix = '.nc'
345      CASE DEFAULT     ;   clsuffix = ''
346         CALL ctl_stop( TRIM(clinfo), 'accepted IO library is only jpnf90 (jpioipsl option has been removed) ' )
347      END SELECT
348      ! Add the suffix if needed
349      iln = LEN_TRIM(clname)
350      ils = LEN_TRIM(clsuffix)
351      IF( iln <= ils .OR. INDEX( TRIM(clname), TRIM(clsuffix), back = .TRUE. ) /= iln - ils + 1 )   &
352         &   clname = TRIM(clname)//TRIM(clsuffix)
353      cltmpn = clname   ! store this name
354      ! try to find if the file to be opened already exist
355      ! =============
356      INQUIRE( FILE = clname, EXIST = llok )
357      IF( .NOT.llok ) THEN
358         ! we try to add the cpu number to the name
359         WRITE(clcpu,*) narea-1
360
361         clcpu  = TRIM(ADJUSTL(clcpu))
362         iln = INDEX(clname,TRIM(clsuffix), back = .TRUE.)
363         clname = clname(1:iln-1)//'_'//TRIM(clcpu)//TRIM(clsuffix)
364         icnt = 0
365         INQUIRE( FILE = clname, EXIST = llok ) 
366         ! we try different formats for the cpu number by adding 0
367         DO WHILE( .NOT.llok .AND. icnt < jpmax_digits )
368            clcpu  = "0"//trim(clcpu)
369            clname = clname(1:iln-1)//'_'//TRIM(clcpu)//TRIM(clsuffix)
370            INQUIRE( FILE = clname, EXIST = llok )
371            icnt = icnt + 1
372         END DO
373      ENDIF
374      IF( llwrt ) THEN
375         ! check the domain definition
376! JMM + SM: ugly patch before getting the new version of lib_mpp)
377!         idom = jpdom_local_noovlap   ! default definition
378         IF( llnoov ) THEN   ;   idom = jpdom_local_noovlap   ! default definition
379         ELSE                ;   idom = jpdom_local_full      ! default definition
380         ENDIF
381         IF( PRESENT(kdom) )   idom = kdom
382         ! create the domain informations
383         ! =============
384         SELECT CASE (idom)
385         CASE (jpdom_local_full)
386            idompar(:,1) = (/ jpi             , jpj              /)
387            idompar(:,2) = (/ nimpp           , njmpp            /)
388            idompar(:,3) = (/ nimpp + jpi - 1 , njmpp + jpj - 1  /)
389            idompar(:,4) = (/ nldi - 1        , nldj - 1         /)
390            idompar(:,5) = (/ jpi - nlei      , jpj - nlej       /)
391         CASE (jpdom_local_noextra)
392            idompar(:,1) = (/ nlci            , nlcj             /)
393            idompar(:,2) = (/ nimpp           , njmpp            /)
394            idompar(:,3) = (/ nimpp + nlci - 1, njmpp + nlcj - 1 /)
395            idompar(:,4) = (/ nldi - 1        , nldj - 1         /)
396            idompar(:,5) = (/ nlci - nlei     , nlcj - nlej      /)
397         CASE (jpdom_local_noovlap)
398            idompar(:,1) = (/ nlei  - nldi + 1, nlej  - nldj + 1 /)
399            idompar(:,2) = (/ nimpp + nldi - 1, njmpp + nldj - 1 /)
400            idompar(:,3) = (/ nimpp + nlei - 1, njmpp + nlej - 1 /)
401            idompar(:,4) = (/ 0               , 0                /)
402            idompar(:,5) = (/ 0               , 0                /)
403         CASE DEFAULT
404            CALL ctl_stop( TRIM(clinfo), 'wrong value of kdom, only jpdom_local* cases are accepted' )
405         END SELECT
406      ENDIF
407      ! Open the NetCDF file
408      ! =============
409      ! do we have some free file identifier?
410      IF( MINVAL(iom_file(:)%nfid) /= 0 )   &
411         &   CALL ctl_stop( TRIM(clinfo), 'No more free file identifier', 'increase jpmax_files in iom_def' )
412      ! if no file was found...
413      IF( .NOT. llok ) THEN
414         IF( .NOT. llwrt ) THEN   ! we are in read mode
415            IF( llstop ) THEN   ;   CALL ctl_stop( TRIM(clinfo), 'File '//TRIM(cltmpn)//'* not found' )
416            ELSE                ;   istop = nstop + 1   ! make sure that istop /= nstop so we don't open the file
417            ENDIF
418         ELSE                     ! we are in write mode so we
419            clname = cltmpn       ! get back the file name without the cpu number
420         ENDIF
421      ELSE
422         IF( llwrt .AND. .NOT. ln_clobber ) THEN   ! we stop as we want to write in a new file
423            CALL ctl_stop( TRIM(clinfo), 'We want to write in a new file but '//TRIM(clname)//' already exists...' )
424            istop = nstop + 1                      ! make sure that istop /= nstop so we don't open the file
425         ELSEIF( llwrt ) THEN     ! the file exists and we are in write mode with permission to
426            clname = cltmpn       ! overwrite so get back the file name without the cpu number
427         ENDIF
428      ENDIF
429      IF( istop == nstop ) THEN   ! no error within this routine
430         SELECT CASE (iolib)
431         CASE (jpnf90   )   ;   CALL iom_nf90_open(    clname, kiomid, llwrt, llok, idompar )
432         CASE DEFAULT
433            CALL ctl_stop( TRIM(clinfo)//' accepted IO library is only jpnf90 (jpioipsl option has been removed) ' )
434         END SELECT
435      ENDIF
436      !
437   END SUBROUTINE iom_open
438
439
440   SUBROUTINE iom_close( kiomid )
441      !!--------------------------------------------------------------------
442      !!                   ***  SUBROUTINE  iom_close  ***
443      !!
444      !! ** Purpose : close an input file, or all files opened by iom
445      !!--------------------------------------------------------------------
446      INTEGER, INTENT(inout), OPTIONAL ::   kiomid   ! iom identifier of the file to be closed
447      !                                              ! return 0 when file is properly closed
448      !                                              ! No argument: all files opened by iom are closed
449
450      INTEGER ::   jf         ! dummy loop indices
451      INTEGER ::   i_s, i_e   ! temporary integer
452      CHARACTER(LEN=100)    ::   clinfo    ! info character
453      !---------------------------------------------------------------------
454      !
455      clinfo = '                    iom_close ~~~  '
456      IF( PRESENT(kiomid) ) THEN
457         i_s = kiomid
458         i_e = kiomid
459      ELSE
460         i_s = 1
461         i_e = jpmax_files
462      ENDIF
463
464      IF( i_s > 0 ) THEN
465         DO jf = i_s, i_e
466            IF( iom_file(jf)%nfid > 0 ) THEN
467               SELECT CASE (iom_file(jf)%iolib)
468               CASE (jpnf90   )   ;   CALL iom_nf90_close(    jf )
469               CASE DEFAULT
470                  CALL ctl_stop( TRIM(clinfo)//' accepted IO library is only jpnf90 (jpioipsl option has been removed)' )
471               END SELECT
472               iom_file(jf)%nfid       = 0          ! free the id
473               IF( PRESENT(kiomid) )   kiomid = 0   ! return 0 as id to specify that the file was closed
474               IF(lwp) WRITE(numout,*) TRIM(clinfo)//' close file: '//TRIM(iom_file(jf)%name)//' ok'
475            ELSEIF( PRESENT(kiomid) ) THEN
476               WRITE(ctmp1,*) '--->',  kiomid
477               CALL ctl_stop( TRIM(clinfo)//' Invalid file identifier', ctmp1 )
478            ENDIF
479         END DO
480      ENDIF
481      !   
482   END SUBROUTINE iom_close
483
484
485   FUNCTION iom_varid ( kiomid, cdvar, kdimsz, kndims, ldstop ) 
486      !!-----------------------------------------------------------------------
487      !!                  ***  FUNCTION  iom_varid  ***
488      !!
489      !! ** Purpose : get the id of a variable in a file (return 0 if not found)
490      !!-----------------------------------------------------------------------
491      INTEGER              , INTENT(in   )           ::   kiomid   ! file Identifier
492      CHARACTER(len=*)     , INTENT(in   )           ::   cdvar    ! name of the variable
493      INTEGER, DIMENSION(:), INTENT(  out), OPTIONAL ::   kdimsz   ! size of the dimensions
494      INTEGER,               INTENT(  out), OPTIONAL ::   kndims   ! size of the dimensions
495      LOGICAL              , INTENT(in   ), OPTIONAL ::   ldstop   ! stop if looking for non-existing variable (default = .TRUE.)
496      !
497      INTEGER                        ::   iom_varid, iiv, i_nvd
498      LOGICAL                        ::   ll_fnd
499      CHARACTER(LEN=100)             ::   clinfo                   ! info character
500      LOGICAL                        ::   llstop                   ! local definition of ldstop
501      !!-----------------------------------------------------------------------
502      iom_varid = 0                         ! default definition
503      ! do we call ctl_stop if we look for non-existing variable?
504      IF( PRESENT(ldstop) ) THEN   ;   llstop = ldstop
505      ELSE                         ;   llstop = .TRUE.
506      ENDIF
507      !
508      IF( kiomid > 0 ) THEN
509         clinfo = 'iom_varid, file: '//trim(iom_file(kiomid)%name)//', var: '//trim(cdvar)
510         IF( iom_file(kiomid)%nfid == 0 ) THEN
511            CALL ctl_stop( trim(clinfo), 'the file is not open' )
512         ELSE
513            ll_fnd  = .FALSE.
514            iiv = 0
515            !
516            DO WHILE ( .NOT.ll_fnd .AND. iiv < iom_file(kiomid)%nvars )
517               iiv = iiv + 1
518               ll_fnd  = ( TRIM(cdvar) == TRIM(iom_file(kiomid)%cn_var(iiv)) )
519            END DO
520            !
521            IF( .NOT.ll_fnd ) THEN
522               iiv = iiv + 1
523               IF( iiv <= jpmax_vars ) THEN
524                  SELECT CASE (iom_file(kiomid)%iolib)
525                  CASE (jpnf90   )   ;   iom_varid = iom_nf90_varid  ( kiomid, cdvar, iiv, kdimsz, kndims )
526                  CASE DEFAULT
527                     CALL ctl_stop( TRIM(clinfo)//' accepted IO library is only jpnf90 (jpioipsl option has been removed)' )
528                  END SELECT
529               ELSE
530                  CALL ctl_stop( trim(clinfo), 'Too many variables in the file '//iom_file(kiomid)%name,   &
531                        &                         'increase the parameter jpmax_vars')
532               ENDIF
533               IF( llstop .AND. iom_varid == -1 )   CALL ctl_stop( TRIM(clinfo)//' not found' ) 
534            ELSE
535               iom_varid = iiv
536               IF( PRESENT(kdimsz) ) THEN
537                  i_nvd = iom_file(kiomid)%ndims(iiv)
538                  IF( i_nvd == size(kdimsz) ) THEN
539                     kdimsz(:) = iom_file(kiomid)%dimsz(1:i_nvd,iiv)
540                  ELSE
541                     WRITE(ctmp1,*) i_nvd, size(kdimsz)
542                     CALL ctl_stop( trim(clinfo), 'error in kdimsz size'//trim(ctmp1) )
543                  ENDIF
544               ENDIF
545               IF( PRESENT(kndims) )  kndims = iom_file(kiomid)%ndims(iiv)
546            ENDIF
547         ENDIF
548      ENDIF
549      !
550   END FUNCTION iom_varid
551
552
553   !!----------------------------------------------------------------------
554   !!                   INTERFACE iom_get
555   !!----------------------------------------------------------------------
556   SUBROUTINE iom_g0d( kiomid, cdvar, pvar, ktime )
557      INTEGER         , INTENT(in   )                 ::   kiomid    ! Identifier of the file
558      CHARACTER(len=*), INTENT(in   )                 ::   cdvar     ! Name of the variable
559      REAL(wp)        , INTENT(  out)                 ::   pvar      ! read field
560      INTEGER         , INTENT(in   ),     OPTIONAL   ::   ktime     ! record number
561      !
562      INTEGER                                         ::   idvar     ! variable id
563      INTEGER                                         ::   idmspc    ! number of spatial dimensions
564      INTEGER         , DIMENSION(1)                  ::   itime     ! record number
565      CHARACTER(LEN=100)                              ::   clinfo    ! info character
566      CHARACTER(LEN=100)                              ::   clname    ! file name
567      CHARACTER(LEN=1)                                ::   cldmspc   !
568      !
569      itime = 1
570      IF( PRESENT(ktime) ) itime = ktime
571      !
572      clname = iom_file(kiomid)%name
573      clinfo = '          iom_g0d, file: '//trim(clname)//', var: '//trim(cdvar)
574      !
575      IF( kiomid > 0 ) THEN
576         idvar = iom_varid( kiomid, cdvar )
577         IF( iom_file(kiomid)%nfid > 0 .AND. idvar > 0 ) THEN
578            idmspc = iom_file ( kiomid )%ndims( idvar )
579            IF( iom_file(kiomid)%luld(idvar) )  idmspc = idmspc - 1
580            WRITE(cldmspc , fmt='(i1)') idmspc
581            IF( idmspc > 0 )  CALL ctl_stop( TRIM(clinfo), 'When reading to a 0D array, we do not accept data', &
582                                 &                         'with 1 or more spatial dimensions: '//cldmspc//' were found.' , &
583                                 &                         'Use ncwa -a to suppress the unnecessary dimensions' )
584            SELECT CASE (iom_file(kiomid)%iolib)
585            CASE (jpnf90   )   ;   CALL iom_nf90_get(    kiomid, idvar, pvar, itime )
586            CASE DEFAULT
587               CALL ctl_stop( 'iom_g0d: accepted IO library is only jpnf90 (jpioipsl option has been removed)' )
588            END SELECT
589         ENDIF
590      ENDIF
591   END SUBROUTINE iom_g0d
592
593   SUBROUTINE iom_g1d( kiomid, kdom, cdvar, pvar, ktime, kstart, kcount )
594      INTEGER         , INTENT(in   )                         ::   kiomid    ! Identifier of the file
595      INTEGER         , INTENT(in   )                         ::   kdom      ! Type of domain to be read
596      CHARACTER(len=*), INTENT(in   )                         ::   cdvar     ! Name of the variable
597      REAL(wp)        , INTENT(  out), DIMENSION(:)           ::   pvar      ! read field
598      INTEGER         , INTENT(in   )              , OPTIONAL ::   ktime     ! record number
599      INTEGER         , INTENT(in   ), DIMENSION(1), OPTIONAL ::   kstart    ! start axis position of the reading
600      INTEGER         , INTENT(in   ), DIMENSION(1), OPTIONAL ::   kcount    ! number of points in each axis
601      !
602      IF( kiomid > 0 ) THEN
603         IF( iom_file(kiomid)%nfid > 0 ) CALL iom_get_123d( kiomid, kdom       , cdvar        , pv_r1d=pvar,   &
604              &                                                     ktime=ktime, kstart=kstart, kcount=kcount )
605      ENDIF
606   END SUBROUTINE iom_g1d
607
608   SUBROUTINE iom_g2d( kiomid, kdom, cdvar, pvar, ktime, kstart, kcount, lrowattr )
609      INTEGER         , INTENT(in   )                           ::   kiomid    ! Identifier of the file
610      INTEGER         , INTENT(in   )                           ::   kdom      ! Type of domain to be read
611      CHARACTER(len=*), INTENT(in   )                           ::   cdvar     ! Name of the variable
612      REAL(wp)        , INTENT(  out), DIMENSION(:,:)           ::   pvar      ! read field
613      INTEGER         , INTENT(in   )                , OPTIONAL ::   ktime     ! record number
614      INTEGER         , INTENT(in   ), DIMENSION(2)  , OPTIONAL ::   kstart    ! start axis position of the reading
615      INTEGER         , INTENT(in   ), DIMENSION(2)  , OPTIONAL ::   kcount    ! number of points in each axis
616      LOGICAL         , INTENT(in   )                , OPTIONAL ::   lrowattr  ! logical flag telling iom_get to
617                                                                               ! look for and use a file attribute
618                                                                               ! called open_ocean_jstart to set the start
619                                                                               ! value for the 2nd dimension (netcdf only)
620      !
621      IF( kiomid > 0 ) THEN
622         IF( iom_file(kiomid)%nfid > 0 ) CALL iom_get_123d( kiomid, kdom       , cdvar        , pv_r2d=pvar,   &
623              &                                                     ktime=ktime, kstart=kstart, kcount=kcount, &
624              &                                                     lrowattr=lrowattr )
625      ENDIF
626   END SUBROUTINE iom_g2d
627
628   SUBROUTINE iom_g3d( kiomid, kdom, cdvar, pvar, ktime, kstart, kcount, lrowattr )
629      INTEGER         , INTENT(in   )                             ::   kiomid    ! Identifier of the file
630      INTEGER         , INTENT(in   )                             ::   kdom      ! Type of domain to be read
631      CHARACTER(len=*), INTENT(in   )                             ::   cdvar     ! Name of the variable
632      REAL(wp)        , INTENT(  out), DIMENSION(:,:,:)           ::   pvar      ! read field
633      INTEGER         , INTENT(in   )                  , OPTIONAL ::   ktime     ! record number
634      INTEGER         , INTENT(in   ), DIMENSION(3)    , OPTIONAL ::   kstart    ! start axis position of the reading
635      INTEGER         , INTENT(in   ), DIMENSION(3)    , OPTIONAL ::   kcount    ! number of points in each axis
636      LOGICAL         , INTENT(in   )                  , OPTIONAL ::   lrowattr  ! logical flag telling iom_get to
637                                                                                 ! look for and use a file attribute
638                                                                                 ! called open_ocean_jstart to set the start
639                                                                                 ! value for the 2nd dimension (netcdf only)
640      !
641      IF( kiomid > 0 ) THEN
642         IF( iom_file(kiomid)%nfid > 0 ) CALL iom_get_123d( kiomid, kdom       , cdvar        , pv_r3d=pvar,   &
643              &                                                     ktime=ktime, kstart=kstart, kcount=kcount, &
644              &                                                     lrowattr=lrowattr )
645      ENDIF
646   END SUBROUTINE iom_g3d
647   !!----------------------------------------------------------------------
648
649   SUBROUTINE iom_get_123d( kiomid, kdom  , cdvar ,   &
650         &                  pv_r1d, pv_r2d, pv_r3d,   &
651         &                  ktime , kstart, kcount,   &
652         &                  lrowattr                )
653      !!-----------------------------------------------------------------------
654      !!                  ***  ROUTINE  iom_get_123d  ***
655      !!
656      !! ** Purpose : read a 1D/2D/3D variable
657      !!
658      !! ** Method : read ONE record at each CALL
659      !!-----------------------------------------------------------------------
660      INTEGER                    , INTENT(in   )           ::   kiomid     ! Identifier of the file
661      INTEGER                    , INTENT(in   )           ::   kdom       ! Type of domain to be read
662      CHARACTER(len=*)           , INTENT(in   )           ::   cdvar      ! Name of the variable
663      REAL(wp), DIMENSION(:)     , INTENT(  out), OPTIONAL ::   pv_r1d     ! read field (1D case)
664      REAL(wp), DIMENSION(:,:)   , INTENT(  out), OPTIONAL ::   pv_r2d     ! read field (2D case)
665      REAL(wp), DIMENSION(:,:,:) , INTENT(  out), OPTIONAL ::   pv_r3d     ! read field (3D case)
666      INTEGER                    , INTENT(in   ), OPTIONAL ::   ktime      ! record number
667      INTEGER , DIMENSION(:)     , INTENT(in   ), OPTIONAL ::   kstart     ! start position of the reading in each axis
668      INTEGER , DIMENSION(:)     , INTENT(in   ), OPTIONAL ::   kcount     ! number of points to be read in each axis
669      LOGICAL                    , INTENT(in   ), OPTIONAL ::   lrowattr   ! logical flag telling iom_get to
670                                                                           ! look for and use a file attribute
671                                                                           ! called open_ocean_jstart to set the start
672                                                                           ! value for the 2nd dimension (netcdf only)
673      !
674      LOGICAL                        ::   llnoov      ! local definition to read overlap
675      LOGICAL                        ::   luse_jattr  ! local definition to read open_ocean_jstart file attribute
676      INTEGER                        ::   jstartrow   ! start point for 2nd dimension optionally set by file attribute
677      INTEGER                        ::   jl          ! loop on number of dimension
678      INTEGER                        ::   idom        ! type of domain
679      INTEGER                        ::   idvar       ! id of the variable
680      INTEGER                        ::   inbdim      ! number of dimensions of the variable
681      INTEGER                        ::   idmspc      ! number of spatial dimensions
682      INTEGER                        ::   itime       ! record number
683      INTEGER                        ::   istop       ! temporary value of nstop
684      INTEGER                        ::   ix1, ix2, iy1, iy2   ! subdomain indexes
685      INTEGER                        ::   ji, jj      ! loop counters
686      INTEGER                        ::   irankpv     !
687      INTEGER                        ::   ind1, ind2  ! substring index
688      INTEGER, DIMENSION(jpmax_dims) ::   istart      ! starting point to read for each axis
689      INTEGER, DIMENSION(jpmax_dims) ::   icnt        ! number of value to read along each axis
690      INTEGER, DIMENSION(jpmax_dims) ::   idimsz      ! size of the dimensions of the variable
691      INTEGER, DIMENSION(jpmax_dims) ::   ishape      ! size of the dimensions of the variable
692      REAL(wp)                       ::   zscf, zofs  ! sacle_factor and add_offset
693      INTEGER                        ::   itmp        ! temporary integer
694      CHARACTER(LEN=256)             ::   clinfo      ! info character
695      CHARACTER(LEN=256)             ::   clname      ! file name
696      CHARACTER(LEN=1)               ::   clrankpv, cldmspc      !
697      LOGICAL                        ::   ll_depth_spec ! T => if kstart, kcount present then *only* use values for 3rd spatial dimension.
698      !---------------------------------------------------------------------
699      !
700      clname = iom_file(kiomid)%name   !   esier to read
701      clinfo = '          iom_get_123d, file: '//trim(clname)//', var: '//trim(cdvar)
702      ! local definition of the domain ?
703      idom = kdom
704      ! do we read the overlap
705      ! ugly patch SM+JMM+RB to overwrite global definition in some cases
706      llnoov = (jpni * jpnj ) == jpnij .AND. .NOT. lk_agrif 
707      ! check kcount and kstart optionals parameters...
708      IF( PRESENT(kcount) .AND. (.NOT. PRESENT(kstart)) ) CALL ctl_stop(trim(clinfo), 'kcount present needs kstart present')
709      IF( PRESENT(kstart) .AND. (.NOT. PRESENT(kcount)) ) CALL ctl_stop(trim(clinfo), 'kstart present needs kcount present')
710      IF( PRESENT(kstart) .AND. idom /= jpdom_unknown .AND.  idom /= jpdom_autoglo_xy  ) &
711     &           CALL ctl_stop(trim(clinfo), 'kstart present needs kdom = jpdom_unknown or kdom = jpdom_autoglo_xy')
712
713      luse_jattr = .false.
714      IF( PRESENT(lrowattr) ) THEN
715         IF( lrowattr .AND. idom /= jpdom_data   ) CALL ctl_stop(trim(clinfo), 'lrowattr present and true needs kdom = jpdom_data')
716         IF( lrowattr .AND. idom == jpdom_data   ) luse_jattr = .true.
717      ENDIF
718      IF( luse_jattr ) THEN
719         SELECT CASE (iom_file(kiomid)%iolib)
720         CASE (jpnf90   )   
721             ! Ok
722         CASE DEFAULT   
723            CALL ctl_stop( TRIM(clinfo)//' accepted IO library is only jpnf90 (jpioipsl option has been removed)' )
724         END SELECT
725      ENDIF
726
727      ! Search for the variable in the data base (eventually actualize data)
728      istop = nstop
729      idvar = iom_varid( kiomid, cdvar )
730      !
731      IF( idvar > 0 ) THEN
732         ! to write iom_file(kiomid)%dimsz in a shorter way !
733         idimsz(:) = iom_file(kiomid)%dimsz(:, idvar) 
734         inbdim = iom_file(kiomid)%ndims(idvar)            ! number of dimensions in the file
735         idmspc = inbdim                                   ! number of spatial dimensions in the file
736         IF( iom_file(kiomid)%luld(idvar) )   idmspc = inbdim - 1
737         IF( idmspc > 3 )   CALL ctl_stop(trim(clinfo), 'the file has more than 3 spatial dimensions this case is not coded...') 
738         !
739         ! update idom definition...
740         ! Identify the domain in case of jpdom_auto(glo/dta) definition
741         IF( idom == jpdom_autoglo_xy ) THEN
742            ll_depth_spec = .TRUE.
743            idom = jpdom_autoglo
744         ELSE
745            ll_depth_spec = .FALSE.
746         ENDIF
747         IF( idom == jpdom_autoglo .OR. idom == jpdom_autodta ) THEN           
748            IF( idom == jpdom_autoglo ) THEN   ;   idom = jpdom_global 
749            ELSE                               ;   idom = jpdom_data
750            ENDIF
751            ind1 = INDEX( clname, '_', back = .TRUE. ) + 1
752            ind2 = INDEX( clname, '.', back = .TRUE. ) - 1
753            IF( ind2 > ind1 ) THEN   ;   IF( VERIFY( clname(ind1:ind2), '0123456789' ) == 0 )   idom = jpdom_local   ;   ENDIF
754         ENDIF
755         ! Identify the domain in case of jpdom_local definition
756         IF( idom == jpdom_local ) THEN
757            IF(     idimsz(1) == jpi               .AND. idimsz(2) == jpj               ) THEN   ;   idom = jpdom_local_full
758            ELSEIF( idimsz(1) == nlci              .AND. idimsz(2) == nlcj              ) THEN   ;   idom = jpdom_local_noextra
759            ELSEIF( idimsz(1) == (nlei - nldi + 1) .AND. idimsz(2) == (nlej - nldj + 1) ) THEN   ;   idom = jpdom_local_noovlap
760            ELSE   ;   CALL ctl_stop( trim(clinfo), 'impossible to identify the local domain' )
761            ENDIF
762         ENDIF
763         !
764         ! check the consistency between input array and data rank in the file
765         !
766         ! initializations
767         itime = 1
768         IF( PRESENT(ktime) ) itime = ktime
769
770         irankpv = 1 * COUNT( (/PRESENT(pv_r1d)/) ) + 2 * COUNT( (/PRESENT(pv_r2d)/) ) + 3 * COUNT( (/PRESENT(pv_r3d)/) )
771         WRITE(clrankpv, fmt='(i1)') irankpv
772         WRITE(cldmspc , fmt='(i1)') idmspc
773         !
774         IF(     idmspc <  irankpv ) THEN
775            CALL ctl_stop( TRIM(clinfo), 'The file has only '//cldmspc//' spatial dimension',   &
776               &                         'it is impossible to read a '//clrankpv//'D array from this file...' )
777         ELSEIF( idmspc == irankpv ) THEN
778            IF( PRESENT(pv_r1d) .AND. idom /= jpdom_unknown )   &
779               &   CALL ctl_stop( TRIM(clinfo), 'case not coded...You must use jpdom_unknown' )
780         ELSEIF( idmspc >  irankpv ) THEN
781               IF( PRESENT(pv_r2d) .AND. itime == 1 .AND. idimsz(3) == 1 .AND. idmspc == 3 ) THEN
782                  CALL ctl_warn( trim(clinfo), '2D array but 3 spatial dimensions for the data...'              ,   &
783                        &         'As the size of the z dimension is 1 and as we try to read the first record, ',   &
784                        &         'we accept this case, even if there is a possible mix-up between z and time dimension' )   
785                  idmspc = idmspc - 1
786               ELSE
787                  CALL ctl_stop( TRIM(clinfo), 'To keep iom lisibility, when reading a '//clrankpv//'D array,'         ,   &
788                     &                         'we do not accept data with '//cldmspc//' spatial dimensions',   &
789                     &                         'Use ncwa -a to suppress the unnecessary dimensions' )
790               ENDIF
791         ENDIF
792
793         !
794         ! definition of istart and icnt
795         !
796         icnt  (:) = 1
797         istart(:) = 1
798         istart(idmspc+1) = itime
799
800         IF( PRESENT(kstart) .AND. .NOT. ll_depth_spec ) THEN ; istart(1:idmspc) = kstart(1:idmspc) ; icnt(1:idmspc) = kcount(1:idmspc)
801         ELSE
802            IF(           idom == jpdom_unknown ) THEN                                                ; icnt(1:idmspc) = idimsz(1:idmspc)
803            ELSE
804               IF( .NOT. PRESENT(pv_r1d) ) THEN   !   not a 1D array
805                  IF(     idom == jpdom_data    ) THEN
806                     jstartrow = 1
807                     IF( luse_jattr ) THEN
808                        CALL iom_getatt(kiomid, 'open_ocean_jstart', jstartrow ) ! -999 is returned if the attribute is not found
809                        jstartrow = MAX(1,jstartrow)
810                     ENDIF
811                     istart(1:2) = (/ mig(1), mjg(1) + jstartrow - 1 /)  ! icnt(1:2) done below
812                  ELSEIF( idom == jpdom_global  ) THEN ; istart(1:2) = (/ nimpp , njmpp  /)  ! icnt(1:2) done below
813                  ENDIF
814                  ! we do not read the overlap                     -> we start to read at nldi, nldj
815! JMM + SM: ugly patch before getting the new version of lib_mpp)
816!                  IF( idom /= jpdom_local_noovlap )   istart(1:2) = istart(1:2) + (/ nldi - 1, nldj - 1 /)
817                  IF( llnoov .AND. idom /= jpdom_local_noovlap ) istart(1:2) = istart(1:2) + (/ nldi - 1, nldj - 1 /)
818                  ! we do not read the overlap and the extra-halos -> from nldi to nlei and from nldj to nlej
819! JMM + SM: ugly patch before getting the new version of lib_mpp)
820!                  icnt(1:2) = (/ nlei - nldi + 1, nlej - nldj + 1 /)
821                  IF( llnoov ) THEN   ;   icnt(1:2) = (/ nlei - nldi + 1, nlej - nldj + 1 /)
822                  ELSE                ;   icnt(1:2) = (/ nlci           , nlcj            /)
823                  ENDIF
824                  IF( PRESENT(pv_r3d) ) THEN
825                     IF( idom == jpdom_data ) THEN                                  ; icnt(3) = jpkdta
826                     ELSE IF( ll_depth_spec .AND. PRESENT(kstart) ) THEN            ; istart(3) = kstart(3); icnt(3) = kcount(3)
827                     ELSE                                                           ; icnt(3) = jpk
828                     ENDIF
829                  ENDIF
830               ENDIF
831            ENDIF
832         ENDIF
833
834         ! check that istart and icnt can be used with this file
835         !-
836         DO jl = 1, jpmax_dims
837            itmp = istart(jl)+icnt(jl)-1
838            IF( itmp > idimsz(jl) .AND. idimsz(jl) /= 0 ) THEN
839               WRITE( ctmp1, FMT="('(istart(', i1, ') + icnt(', i1, ') - 1) = ', i5)" ) jl, jl, itmp
840               WRITE( ctmp2, FMT="(' is larger than idimsz(', i1,') = ', i5)"         ) jl, idimsz(jl)
841               CALL ctl_stop( trim(clinfo), 'start and count too big regarding to the size of the data, ', ctmp1, ctmp2 )     
842            ENDIF
843         END DO
844
845         ! check that icnt matches the input array
846         !-     
847         IF( idom == jpdom_unknown ) THEN
848            IF( irankpv == 1 )        ishape(1:1) = SHAPE(pv_r1d)
849            IF( irankpv == 2 )        ishape(1:2) = SHAPE(pv_r2d)
850            IF( irankpv == 3 )        ishape(1:3) = SHAPE(pv_r3d)
851            ctmp1 = 'd'
852         ELSE
853            IF( irankpv == 2 ) THEN
854! JMM + SM: ugly patch before getting the new version of lib_mpp)
855!               ishape(1:2) = SHAPE(pv_r2d(nldi:nlei,nldj:nlej  ))   ;   ctmp1 = 'd(nldi:nlei,nldj:nlej)'
856               IF( llnoov ) THEN ; ishape(1:2)=SHAPE(pv_r2d(nldi:nlei,nldj:nlej  )) ; ctmp1='d(nldi:nlei,nldj:nlej)'
857               ELSE              ; ishape(1:2)=SHAPE(pv_r2d(1   :nlci,1   :nlcj  )) ; ctmp1='d(1:nlci,1:nlcj)'
858               ENDIF
859            ENDIF
860            IF( irankpv == 3 ) THEN 
861! JMM + SM: ugly patch before getting the new version of lib_mpp)
862!               ishape(1:3) = SHAPE(pv_r3d(nldi:nlei,nldj:nlej,:))   ;   ctmp1 = 'd(nldi:nlei,nldj:nlej,:)'
863               IF( llnoov ) THEN ; ishape(1:3)=SHAPE(pv_r3d(nldi:nlei,nldj:nlej,:)) ; ctmp1='d(nldi:nlei,nldj:nlej,:)'
864               ELSE              ; ishape(1:3)=SHAPE(pv_r3d(1   :nlci,1   :nlcj,:)) ; ctmp1='d(1:nlci,1:nlcj,:)'
865               ENDIF
866            ENDIF
867         ENDIF
868         
869         DO jl = 1, irankpv
870            WRITE( ctmp2, FMT="(', ', i1,'): ', i5,' /= icnt(', i1,'):', i5)" ) jl, ishape(jl), jl, icnt(jl)
871            IF( ishape(jl) /= icnt(jl) )   CALL ctl_stop( TRIM(clinfo), 'size(pv_r'//clrankpv//TRIM(ctmp1)//TRIM(ctmp2) )
872         END DO
873
874      ENDIF
875
876      ! read the data
877      !-     
878      IF( idvar > 0 .AND. istop == nstop ) THEN   ! no additional errors until this point...
879         !
880         ! find the right index of the array to be read
881! JMM + SM: ugly patch before getting the new version of lib_mpp)
882!         IF( idom /= jpdom_unknown ) THEN   ;   ix1 = nldi   ;   ix2 = nlei      ;   iy1 = nldj   ;   iy2 = nlej
883!         ELSE                               ;   ix1 = 1      ;   ix2 = icnt(1)   ;   iy1 = 1      ;   iy2 = icnt(2)
884!         ENDIF
885         IF( llnoov ) THEN
886            IF( idom /= jpdom_unknown ) THEN   ;   ix1 = nldi   ;   ix2 = nlei      ;   iy1 = nldj   ;   iy2 = nlej
887            ELSE                               ;   ix1 = 1      ;   ix2 = icnt(1)   ;   iy1 = 1      ;   iy2 = icnt(2)
888            ENDIF
889         ELSE
890            IF( idom /= jpdom_unknown ) THEN   ;   ix1 = 1      ;   ix2 = nlci      ;   iy1 = 1      ;   iy2 = nlcj
891            ELSE                               ;   ix1 = 1      ;   ix2 = icnt(1)   ;   iy1 = 1      ;   iy2 = icnt(2)
892            ENDIF
893         ENDIF
894     
895         SELECT CASE (iom_file(kiomid)%iolib)
896         CASE (jpnf90   )   ;   CALL iom_nf90_get(    kiomid, idvar, inbdim, istart, icnt, ix1, ix2, iy1, iy2,   &
897            &                                         pv_r1d, pv_r2d, pv_r3d )
898         CASE DEFAULT
899            CALL ctl_stop( TRIM(clinfo)//' accepted IO library is only jpnf90 (jpioipsl option has been removed)' )
900         END SELECT
901
902         IF( istop == nstop ) THEN   ! no additional errors until this point...
903            IF(lwp) WRITE(numout,"(10x,' read ',a,' (rec: ',i6,') in ',a,' ok')") TRIM(cdvar), itime, TRIM(iom_file(kiomid)%name)
904         
905            !--- overlap areas and extra hallows (mpp)
906            IF(     PRESENT(pv_r2d) .AND. idom /= jpdom_unknown ) THEN
907               CALL lbc_lnk( pv_r2d,'Z',-999.,'no0' )
908            ELSEIF( PRESENT(pv_r3d) .AND. idom /= jpdom_unknown ) THEN
909               ! this if could be simplified with the new lbc_lnk that works with any size of the 3rd dimension
910               IF( icnt(3) == jpk ) THEN
911                  CALL lbc_lnk( pv_r3d,'Z',-999.,'no0' )
912               ELSE   ! put some arbitrary value (a call to lbc_lnk will be done later...)
913                  DO jj = nlcj+1, jpj   ;   pv_r3d(1:nlci, jj, :) = pv_r3d(1:nlci, nlej, :)   ;   END DO
914                  DO ji = nlci+1, jpi   ;   pv_r3d(ji    , : , :) = pv_r3d(nlei  , :   , :)   ;   END DO
915               ENDIF
916            ENDIF
917           
918            ! C1D case : always call lbc_lnk to replicate the central value over the whole 3X3 domain
919            IF( lk_c1d .AND. PRESENT(pv_r2d) )   CALL lbc_lnk( pv_r2d,'Z',1. )
920            IF( lk_c1d .AND. PRESENT(pv_r3d) )   CALL lbc_lnk( pv_r3d,'Z',1. )
921   
922            !--- Apply scale_factor and offset
923            zscf = iom_file(kiomid)%scf(idvar)      ! scale factor
924            zofs = iom_file(kiomid)%ofs(idvar)      ! offset
925            IF(     PRESENT(pv_r1d) ) THEN
926               IF( zscf /= 1. )   pv_r1d(:) = pv_r1d(:) * zscf 
927               IF( zofs /= 0. )   pv_r1d(:) = pv_r1d(:) + zofs
928            ELSEIF( PRESENT(pv_r2d) ) THEN
929               IF( zscf /= 1.)   pv_r2d(:,:) = pv_r2d(:,:) * zscf
930               IF( zofs /= 0.)   pv_r2d(:,:) = pv_r2d(:,:) + zofs
931            ELSEIF( PRESENT(pv_r3d) ) THEN
932               IF( zscf /= 1.)   pv_r3d(:,:,:) = pv_r3d(:,:,:) * zscf
933               IF( zofs /= 0.)   pv_r3d(:,:,:) = pv_r3d(:,:,:) + zofs
934            ENDIF
935            !
936         ENDIF
937         !
938      ENDIF
939      !
940   END SUBROUTINE iom_get_123d
941
942
943   SUBROUTINE iom_gettime( kiomid, ptime, cdvar, kntime, cdunits, cdcalendar )
944      !!--------------------------------------------------------------------
945      !!                   ***  SUBROUTINE iom_gettime  ***
946      !!
947      !! ** Purpose : read the time axis cdvar in the file
948      !!--------------------------------------------------------------------
949      INTEGER                    , INTENT(in   ) ::   kiomid     ! file Identifier
950      REAL(wp), DIMENSION(:)     , INTENT(  out) ::   ptime      ! the time axis
951      CHARACTER(len=*), OPTIONAL , INTENT(in   ) ::   cdvar      ! time axis name
952      INTEGER         , OPTIONAL , INTENT(  out) ::   kntime     ! number of times in file
953      CHARACTER(len=*), OPTIONAL , INTENT(  out) ::   cdunits    ! units attribute of time coordinate
954      CHARACTER(len=*), OPTIONAL , INTENT(  out) ::   cdcalendar ! calendar attribute of
955      !
956      INTEGER, DIMENSION(1) :: kdimsz
957      INTEGER            ::   idvar    ! id of the variable
958      CHARACTER(LEN=32)  ::   tname    ! local name of time coordinate
959      CHARACTER(LEN=100) ::   clinfo   ! info character
960      !---------------------------------------------------------------------
961      !
962      IF ( PRESENT(cdvar) ) THEN
963         tname = cdvar
964      ELSE
965         tname = iom_file(kiomid)%uldname
966      ENDIF
967      IF( kiomid > 0 ) THEN
968         clinfo = 'iom_gettime, file: '//trim(iom_file(kiomid)%name)//', var: '//trim(tname)
969         IF ( PRESENT(kntime) ) THEN
970            idvar  = iom_varid( kiomid, tname, kdimsz = kdimsz )
971            kntime = kdimsz(1)
972         ELSE
973            idvar = iom_varid( kiomid, tname )
974         ENDIF
975         !
976         ptime(:) = 0. ! default definition
977         IF( idvar > 0 ) THEN
978            IF( iom_file(kiomid)%ndims(idvar) == 1 ) THEN
979               IF( iom_file(kiomid)%luld(idvar) ) THEN
980                  IF( iom_file(kiomid)%dimsz(1,idvar) <= size(ptime) ) THEN
981                     SELECT CASE (iom_file(kiomid)%iolib)
982                     CASE (jpnf90   )   ;   CALL iom_nf90_gettime(   kiomid, idvar, ptime, cdunits, cdcalendar )
983                     CASE DEFAULT
984                        CALL ctl_stop( TRIM(clinfo)//' accepted IO library is only jpnf90 (jpioipsl option has been removed)' )
985                     END SELECT
986                  ELSE
987                     WRITE(ctmp1,*) 'error with the size of ptime ',size(ptime),iom_file(kiomid)%dimsz(1,idvar)
988                     CALL ctl_stop( trim(clinfo), trim(ctmp1) )
989                  ENDIF
990               ELSE
991                  CALL ctl_stop( trim(clinfo), 'variable dimension is not unlimited... use iom_get' )
992               ENDIF
993            ELSE
994               CALL ctl_stop( trim(clinfo), 'the variable has more than 1 dimension' )
995            ENDIF
996         ELSE
997            CALL ctl_stop( trim(clinfo), 'variable not found in '//iom_file(kiomid)%name )
998         ENDIF
999      ENDIF
1000      !
1001   END SUBROUTINE iom_gettime
1002
1003
1004   !!----------------------------------------------------------------------
1005   !!                   INTERFACE iom_getatt
1006   !!----------------------------------------------------------------------
1007   SUBROUTINE iom_g0d_intatt( kiomid, cdatt, pvar )
1008      INTEGER         , INTENT(in   )                 ::   kiomid    ! Identifier of the file
1009      CHARACTER(len=*), INTENT(in   )                 ::   cdatt     ! Name of the attribute
1010      INTEGER         , INTENT(  out)                 ::   pvar      ! read field
1011      !
1012      IF( kiomid > 0 ) THEN
1013         IF( iom_file(kiomid)%nfid > 0 ) THEN
1014            SELECT CASE (iom_file(kiomid)%iolib)
1015            CASE (jpnf90   )   ;   CALL iom_nf90_getatt( kiomid, cdatt, pv_i0d=pvar )
1016            CASE DEFAULT
1017               CALL ctl_stop( 'iom_g0d_att: accepted IO library is only jpnf90' )
1018            END SELECT
1019         ENDIF
1020      ENDIF
1021   END SUBROUTINE iom_g0d_intatt
1022
1023   SUBROUTINE iom_g0d_ratt( kiomid, cdatt, pvar, cdvar )
1024      INTEGER         , INTENT(in   )                 ::   kiomid    ! Identifier of the file
1025      CHARACTER(len=*), INTENT(in   )                 ::   cdatt     ! Name of the attribute
1026      REAL(wp)        , INTENT(  out)                 ::   pvar      ! written field
1027      CHARACTER(len=*), INTENT(in   ), OPTIONAL       ::   cdvar     ! Name of the variable
1028      !
1029      IF( kiomid > 0 ) THEN
1030         IF( iom_file(kiomid)%nfid > 0 ) THEN
1031            SELECT CASE (iom_file(kiomid)%iolib)
1032            CASE (jpnf90   )   ;   IF( PRESENT(cdvar) ) THEN
1033                                      CALL iom_nf90_getatt( kiomid, cdatt, pv_r0d=pvar, cdvar=cdvar )
1034                                   ELSE
1035                                      CALL iom_nf90_getatt( kiomid, cdatt, pv_r0d=pvar )
1036                                   ENDIF
1037            CASE DEFAULT   
1038               CALL ctl_stop( 'iom_g0d_att: accepted IO library is only jpnf90' )
1039            END SELECT
1040         ENDIF
1041      ENDIF
1042   END SUBROUTINE iom_g0d_ratt
1043
1044   !!----------------------------------------------------------------------
1045   !!                   INTERFACE iom_rstput
1046   !!----------------------------------------------------------------------
1047   SUBROUTINE iom_rp0d( kt, kwrite, kiomid, cdvar, pvar, ktype )
1048      INTEGER         , INTENT(in)                         ::   kt       ! ocean time-step
1049      INTEGER         , INTENT(in)                         ::   kwrite   ! writing time-step
1050      INTEGER         , INTENT(in)                         ::   kiomid   ! Identifier of the file
1051      CHARACTER(len=*), INTENT(in)                         ::   cdvar    ! time axis name
1052      REAL(wp)        , INTENT(in)                         ::   pvar     ! written field
1053      INTEGER         , INTENT(in), OPTIONAL               ::   ktype    ! variable external type
1054      INTEGER :: ivid   ! variable id
1055      IF( kiomid > 0 ) THEN
1056         IF( iom_file(kiomid)%nfid > 0 ) THEN
1057            ivid = iom_varid( kiomid, cdvar, ldstop = .FALSE. )
1058            SELECT CASE (iom_file(kiomid)%iolib)
1059            CASE (jpnf90   )   ;   CALL iom_nf90_rstput(   kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r0d = pvar )
1060            CASE DEFAULT
1061               CALL ctl_stop( 'iom_rp0d: accepted IO library is only jpnf90 (jpioipsl option has been removed)' )
1062            END SELECT
1063         ENDIF
1064      ENDIF
1065   END SUBROUTINE iom_rp0d
1066
1067   SUBROUTINE iom_rp1d( kt, kwrite, kiomid, cdvar, pvar, ktype )
1068      INTEGER         , INTENT(in)                         ::   kt       ! ocean time-step
1069      INTEGER         , INTENT(in)                         ::   kwrite   ! writing time-step
1070      INTEGER         , INTENT(in)                         ::   kiomid   ! Identifier of the file
1071      CHARACTER(len=*), INTENT(in)                         ::   cdvar    ! time axis name
1072      REAL(wp)        , INTENT(in), DIMENSION(          :) ::   pvar     ! written field
1073      INTEGER         , INTENT(in), OPTIONAL               ::   ktype    ! variable external type
1074      INTEGER :: ivid   ! variable id
1075      IF( kiomid > 0 ) THEN
1076         IF( iom_file(kiomid)%nfid > 0 ) THEN
1077            ivid = iom_varid( kiomid, cdvar, ldstop = .FALSE. )
1078            SELECT CASE (iom_file(kiomid)%iolib)
1079            CASE (jpnf90   )   ;   CALL iom_nf90_rstput(   kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r1d = pvar )
1080            CASE DEFAULT
1081               CALL ctl_stop( 'iom_rp1d: accepted IO library is only jpnf90 (jpioipsl option has been removed)' )
1082            END SELECT
1083         ENDIF
1084      ENDIF
1085   END SUBROUTINE iom_rp1d
1086
1087   SUBROUTINE iom_rp2d( kt, kwrite, kiomid, cdvar, pvar, ktype )
1088      INTEGER         , INTENT(in)                         ::   kt       ! ocean time-step
1089      INTEGER         , INTENT(in)                         ::   kwrite   ! writing time-step
1090      INTEGER         , INTENT(in)                         ::   kiomid   ! Identifier of the file
1091      CHARACTER(len=*), INTENT(in)                         ::   cdvar    ! time axis name
1092      REAL(wp)        , INTENT(in), DIMENSION(:,    :    ) ::   pvar     ! written field
1093      INTEGER         , INTENT(in), OPTIONAL               ::   ktype    ! variable external type
1094      INTEGER :: ivid   ! variable id
1095      IF( kiomid > 0 ) THEN
1096         IF( iom_file(kiomid)%nfid > 0 ) THEN
1097            ivid = iom_varid( kiomid, cdvar, ldstop = .FALSE. )
1098            SELECT CASE (iom_file(kiomid)%iolib)
1099            CASE (jpnf90   )   ;   CALL iom_nf90_rstput(   kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r2d = pvar )
1100            CASE DEFAULT
1101               CALL ctl_stop( 'iom_rp2d: accepted IO library is only jpnf90 (jpioipsl option has been removed)' )
1102            END SELECT
1103         ENDIF
1104      ENDIF
1105   END SUBROUTINE iom_rp2d
1106
1107   SUBROUTINE iom_rp3d( kt, kwrite, kiomid, cdvar, pvar, ktype )
1108      INTEGER         , INTENT(in)                         ::   kt       ! ocean time-step
1109      INTEGER         , INTENT(in)                         ::   kwrite   ! writing time-step
1110      INTEGER         , INTENT(in)                         ::   kiomid   ! Identifier of the file
1111      CHARACTER(len=*), INTENT(in)                         ::   cdvar    ! time axis name
1112      REAL(wp)        , INTENT(in),       DIMENSION(:,:,:) ::   pvar     ! written field
1113      INTEGER         , INTENT(in), OPTIONAL               ::   ktype    ! variable external type
1114      INTEGER :: ivid   ! variable id
1115      IF( kiomid > 0 ) THEN
1116         IF( iom_file(kiomid)%nfid > 0 ) THEN
1117            ivid = iom_varid( kiomid, cdvar, ldstop = .FALSE. )
1118            SELECT CASE (iom_file(kiomid)%iolib)
1119            CASE (jpnf90   )   ;   CALL iom_nf90_rstput(   kt, kwrite, kiomid, cdvar, ivid, ktype, pv_r3d = pvar )
1120            CASE DEFAULT
1121               CALL ctl_stop( 'iom_rp3d: accepted IO library is only jpnf90 (jpioipsl option has been removed)' )
1122            END SELECT
1123         ENDIF
1124      ENDIF
1125   END SUBROUTINE iom_rp3d
1126
1127
1128   !!----------------------------------------------------------------------
1129   !!                   INTERFACE iom_put
1130   !!----------------------------------------------------------------------
1131   SUBROUTINE iom_p0d( cdname, pfield0d )
1132      CHARACTER(LEN=*), INTENT(in) ::   cdname
1133      REAL(wp)        , INTENT(in) ::   pfield0d
1134      REAL(wp)        , DIMENSION(jpi,jpj) ::   zz     ! masson
1135#if defined key_iomput
1136      zz(:,:)=pfield0d
1137      CALL xios_send_field(cdname, zz)
1138      !CALL xios_send_field(cdname, (/pfield0d/))
1139#else
1140      IF( .FALSE. )   WRITE(numout,*) cdname, pfield0d   ! useless test to avoid compilation warnings
1141#endif
1142   END SUBROUTINE iom_p0d
1143
1144   SUBROUTINE iom_p1d( cdname, pfield1d )
1145      CHARACTER(LEN=*)          , INTENT(in) ::   cdname
1146      REAL(wp),     DIMENSION(:), INTENT(in) ::   pfield1d
1147#if defined key_iomput
1148      CALL xios_send_field( cdname, RESHAPE( (/pfield1d/), (/1,1,SIZE(pfield1d)/) ) )
1149#else
1150      IF( .FALSE. )   WRITE(numout,*) cdname, pfield1d   ! useless test to avoid compilation warnings
1151#endif
1152   END SUBROUTINE iom_p1d
1153
1154   SUBROUTINE iom_p2d( cdname, pfield2d )
1155      CHARACTER(LEN=*)            , INTENT(in) ::   cdname
1156      REAL(wp),     DIMENSION(:,:), INTENT(in) ::   pfield2d
1157#if defined key_iomput
1158      CALL xios_send_field(cdname, pfield2d)
1159#else
1160      IF( .FALSE. )   WRITE(numout,*) cdname, pfield2d   ! useless test to avoid compilation warnings
1161#endif
1162   END SUBROUTINE iom_p2d
1163
1164   SUBROUTINE iom_p3d( cdname, pfield3d )
1165      CHARACTER(LEN=*)                , INTENT(in) ::   cdname
1166      REAL(wp),       DIMENSION(:,:,:), INTENT(in) ::   pfield3d
1167#if defined key_iomput
1168      CALL xios_send_field(cdname, pfield3d)
1169#else
1170      IF( .FALSE. )   WRITE(numout,*) cdname, pfield3d   ! useless test to avoid compilation warnings
1171#endif
1172   END SUBROUTINE iom_p3d
1173   !!----------------------------------------------------------------------
1174
1175#if defined key_iomput
1176
1177   SUBROUTINE iom_set_domain_attr( cdid, ni_glo, nj_glo, ibegin, jbegin, ni, nj, zoom_ibegin, zoom_jbegin, zoom_ni, zoom_nj,   &
1178      &                                    data_dim, data_ibegin, data_ni, data_jbegin, data_nj, lonvalue, latvalue, mask,     &
1179      &                                    nvertex, bounds_lon, bounds_lat, area )
1180      CHARACTER(LEN=*)                   , INTENT(in) ::   cdid
1181      INTEGER                  , OPTIONAL, INTENT(in) ::   ni_glo, nj_glo, ibegin, jbegin, ni, nj
1182      INTEGER                  , OPTIONAL, INTENT(in) ::   data_dim, data_ibegin, data_ni, data_jbegin, data_nj
1183      INTEGER                  , OPTIONAL, INTENT(in) ::   zoom_ibegin, zoom_jbegin, zoom_ni, zoom_nj, nvertex
1184      REAL(wp), DIMENSION(:)   , OPTIONAL, INTENT(in) ::   lonvalue, latvalue
1185      REAL(wp), DIMENSION(:,:) , OPTIONAL, INTENT(in) ::   bounds_lon, bounds_lat, area
1186#if ! defined key_xios2
1187     LOGICAL,  DIMENSION(:,:) , OPTIONAL, INTENT(in) ::   mask
1188#else
1189      LOGICAL,  DIMENSION(:) , OPTIONAL, INTENT(in) ::   mask
1190#endif
1191
1192#if ! defined key_xios2
1193      IF ( xios_is_valid_domain     (cdid) ) THEN
1194         CALL xios_set_domain_attr     ( cdid, ni_glo=ni_glo, nj_glo=nj_glo, ibegin=ibegin, jbegin=jbegin, ni=ni, nj=nj,   &
1195            &    data_dim=data_dim, data_ibegin=data_ibegin, data_ni=data_ni, data_jbegin=data_jbegin, data_nj=data_nj ,   &
1196            &    zoom_ibegin=zoom_ibegin, zoom_jbegin=zoom_jbegin, zoom_ni=zoom_ni, zoom_nj=zoom_nj,                       &
1197            &    lonvalue=lonvalue, latvalue=latvalue, mask=mask, nvertex=nvertex, bounds_lon=bounds_lon,                  &
1198            &    bounds_lat=bounds_lat, area=area )
1199      ENDIF
1200
1201      IF ( xios_is_valid_domaingroup(cdid) ) THEN
1202         CALL xios_set_domaingroup_attr( cdid, ni_glo=ni_glo, nj_glo=nj_glo, ibegin=ibegin, jbegin=jbegin, ni=ni, nj=nj,   &
1203            &    data_dim=data_dim, data_ibegin=data_ibegin, data_ni=data_ni, data_jbegin=data_jbegin, data_nj=data_nj ,   &
1204            &    zoom_ibegin=zoom_ibegin, zoom_jbegin=zoom_jbegin, zoom_ni=zoom_ni, zoom_nj=zoom_nj,                       &
1205            &    lonvalue=lonvalue, latvalue=latvalue, mask=mask, nvertex=nvertex, bounds_lon=bounds_lon,                  &
1206            &    bounds_lat=bounds_lat, area=area )
1207      ENDIF
1208
1209#else
1210      IF ( xios_is_valid_domain     (cdid) ) THEN
1211         CALL xios_set_domain_attr     ( cdid, ni_glo=ni_glo, nj_glo=nj_glo, ibegin=ibegin, jbegin=jbegin, ni=ni, nj=nj,   &
1212            &    data_dim=data_dim, data_ibegin=data_ibegin, data_ni=data_ni, data_jbegin=data_jbegin, data_nj=data_nj ,   &
1213            &    lonvalue_1D=lonvalue, latvalue_1D=latvalue, mask_1D=mask, nvertex=nvertex, bounds_lon_1D=bounds_lon,                  &
1214            &    bounds_lat_1D=bounds_lat, area=area, type='curvilinear')
1215     ENDIF
1216      IF ( xios_is_valid_domaingroup(cdid) ) THEN
1217         CALL xios_set_domaingroup_attr( cdid, ni_glo=ni_glo, nj_glo=nj_glo, ibegin=ibegin, jbegin=jbegin, ni=ni, nj=nj,   &
1218            &    data_dim=data_dim, data_ibegin=data_ibegin, data_ni=data_ni, data_jbegin=data_jbegin, data_nj=data_nj ,   &
1219            &    lonvalue_1D=lonvalue, latvalue_1D=latvalue, mask_1D=mask, nvertex=nvertex, bounds_lon_1D=bounds_lon,                  &
1220            &    bounds_lat_1D=bounds_lat, area=area, type='curvilinear' )
1221      ENDIF
1222#endif
1223      CALL xios_solve_inheritance()
1224
1225   END SUBROUTINE iom_set_domain_attr
1226
1227#if defined key_xios2
1228  SUBROUTINE iom_set_zoom_domain_attr( cdid, ibegin, jbegin, ni, nj)
1229     CHARACTER(LEN=*)                   , INTENT(in) ::   cdid
1230     INTEGER                  , OPTIONAL, INTENT(in) ::   ibegin, jbegin, ni, nj
1231
1232     IF ( xios_is_valid_zoom_domain     (cdid) ) THEN
1233         CALL xios_set_zoom_domain_attr     ( cdid, ibegin=ibegin, jbegin=jbegin, ni=ni,    &
1234           &   nj=nj)
1235    ENDIF
1236  END SUBROUTINE iom_set_zoom_domain_attr
1237#endif
1238
1239
1240   SUBROUTINE iom_set_axis_attr( cdid, paxis, bounds )
1241      CHARACTER(LEN=*)      , INTENT(in) ::   cdid
1242      REAL(wp), DIMENSION(:)  , OPTIONAL, INTENT(in) ::   paxis
1243      REAL(wp), DIMENSION(:,:), OPTIONAL, INTENT(in) ::   bounds
1244      IF ( PRESENT(paxis) ) THEN
1245#if ! defined key_xios2
1246         IF ( xios_is_valid_axis     (cdid) )   CALL xios_set_axis_attr     ( cdid, size=SIZE(paxis), value=paxis )
1247         IF ( xios_is_valid_axisgroup(cdid) )   CALL xios_set_axisgroup_attr( cdid, size=SIZE(paxis), value=paxis )
1248#else
1249         IF ( xios_is_valid_axis     (cdid) )   CALL xios_set_axis_attr     ( cdid, n_glo=SIZE(paxis), value=paxis )
1250         IF ( xios_is_valid_axisgroup(cdid) )   CALL xios_set_axisgroup_attr( cdid, n_glo=SIZE(paxis), value=paxis )
1251#endif
1252      ENDIF
1253      IF ( xios_is_valid_axis     (cdid) )   CALL xios_set_axis_attr     ( cdid, bounds=bounds )
1254      IF ( xios_is_valid_axisgroup(cdid) )   CALL xios_set_axisgroup_attr( cdid, bounds=bounds )
1255      CALL xios_solve_inheritance()
1256   END SUBROUTINE iom_set_axis_attr
1257
1258
1259   SUBROUTINE iom_set_field_attr( cdid, freq_op, freq_offset )
1260      CHARACTER(LEN=*)          , INTENT(in) ::   cdid
1261#if ! defined key_xios2
1262      CHARACTER(LEN=*)   ,OPTIONAL , INTENT(in) ::   freq_op
1263      CHARACTER(LEN=*)   ,OPTIONAL , INTENT(in) ::   freq_offset
1264#else
1265      TYPE(xios_duration),OPTIONAL , INTENT(in) ::   freq_op
1266      TYPE(xios_duration),OPTIONAL , INTENT(in) ::   freq_offset
1267#endif
1268      IF ( xios_is_valid_field     (cdid) )   CALL xios_set_field_attr       &
1269    &     ( cdid, freq_op=freq_op, freq_offset=freq_offset )
1270      IF ( xios_is_valid_fieldgroup(cdid) )   CALL xios_set_fieldgroup_attr  &
1271    &                    ( cdid, freq_op=freq_op, freq_offset=freq_offset )
1272      CALL xios_solve_inheritance()
1273   END SUBROUTINE iom_set_field_attr
1274
1275
1276   SUBROUTINE iom_set_file_attr( cdid, name, name_suffix )
1277      CHARACTER(LEN=*)          , INTENT(in) ::   cdid
1278      CHARACTER(LEN=*),OPTIONAL , INTENT(in) ::   name, name_suffix
1279      IF ( xios_is_valid_file     (cdid) )   CALL xios_set_file_attr     ( cdid, name=name, name_suffix=name_suffix )
1280      IF ( xios_is_valid_filegroup(cdid) )   CALL xios_set_filegroup_attr( cdid, name=name, name_suffix=name_suffix )
1281      CALL xios_solve_inheritance()
1282   END SUBROUTINE iom_set_file_attr
1283
1284
1285   SUBROUTINE iom_get_file_attr( cdid, name, name_suffix, output_freq )
1286      CHARACTER(LEN=*)          , INTENT(in ) ::   cdid
1287      CHARACTER(LEN=*),OPTIONAL , INTENT(out) ::   name, name_suffix
1288#if ! defined key_xios2
1289      CHARACTER(LEN=*),OPTIONAL , INTENT(out) ::    output_freq
1290#else
1291      TYPE(xios_duration)   ,OPTIONAL , INTENT(out) :: output_freq
1292#endif 
1293      LOGICAL                                 ::   llexist1,llexist2,llexist3
1294      !---------------------------------------------------------------------
1295      IF( PRESENT( name        ) )   name = ''          ! default values
1296      IF( PRESENT( name_suffix ) )   name_suffix = ''
1297#if ! defined key_xios2
1298      IF( PRESENT( output_freq ) )   output_freq = ''
1299#else
1300      IF( PRESENT( output_freq ) )   output_freq = xios_duration(0,0,0,0,0,0)
1301#endif
1302      IF ( xios_is_valid_file     (cdid) ) THEN
1303         CALL xios_solve_inheritance()
1304         CALL xios_is_defined_file_attr     ( cdid, name = llexist1, name_suffix = llexist2, output_freq = llexist3)
1305         IF(llexist1)   CALL xios_get_file_attr     ( cdid, name = name )
1306         IF(llexist2)   CALL xios_get_file_attr     ( cdid, name_suffix = name_suffix )
1307         IF(llexist3)   CALL xios_get_file_attr     ( cdid, output_freq = output_freq )
1308      ENDIF
1309      IF ( xios_is_valid_filegroup(cdid) ) THEN
1310         CALL xios_solve_inheritance()
1311         CALL xios_is_defined_filegroup_attr( cdid, name = llexist1, name_suffix = llexist2, output_freq = llexist3)
1312         IF(llexist1)   CALL xios_get_filegroup_attr( cdid, name = name )
1313         IF(llexist2)   CALL xios_get_filegroup_attr( cdid, name_suffix = name_suffix )
1314         IF(llexist3)   CALL xios_get_filegroup_attr( cdid, output_freq = output_freq )
1315      ENDIF
1316   END SUBROUTINE iom_get_file_attr
1317
1318
1319   SUBROUTINE iom_set_grid_attr( cdid, mask )
1320      CHARACTER(LEN=*)                   , INTENT(in) ::   cdid
1321      LOGICAL, DIMENSION(:,:,:), OPTIONAL, INTENT(in) ::   mask
1322#if ! defined key_xios2
1323      IF ( xios_is_valid_grid     (cdid) )   CALL xios_set_grid_attr     ( cdid, mask=mask )
1324      IF ( xios_is_valid_gridgroup(cdid) )   CALL xios_set_gridgroup_attr( cdid, mask=mask )
1325#else
1326      IF ( xios_is_valid_grid     (cdid) )   CALL xios_set_grid_attr     ( cdid, mask_3D=mask )
1327      IF ( xios_is_valid_gridgroup(cdid) )   CALL xios_set_gridgroup_attr( cdid, mask_3D=mask )
1328#endif
1329      CALL xios_solve_inheritance()
1330   END SUBROUTINE iom_set_grid_attr
1331
1332   SUBROUTINE iom_setkt( kt, cdname )
1333      INTEGER         , INTENT(in) ::   kt 
1334      CHARACTER(LEN=*), INTENT(in) ::   cdname
1335      !     
1336      CALL iom_swap( cdname )   ! swap to cdname context
1337      CALL xios_update_calendar(kt)
1338      IF( cdname /= TRIM(cxios_context) ) CALL iom_swap( TRIM(cxios_context) )   ! return back to nemo context
1339      !
1340   END SUBROUTINE iom_setkt
1341
1342   SUBROUTINE iom_context_finalize( cdname )
1343      CHARACTER(LEN=*), INTENT(in) :: cdname
1344      !
1345      IF( xios_is_valid_context(cdname) ) THEN
1346         CALL iom_swap( cdname )   ! swap to cdname context
1347         CALL xios_context_finalize() ! finalize the context
1348         IF( cdname /= TRIM(cxios_context) ) CALL iom_swap( TRIM(cxios_context) )   ! return back to nemo context
1349      ENDIF
1350      !
1351   END SUBROUTINE iom_context_finalize
1352
1353
1354   SUBROUTINE set_grid( cdgrd, plon, plat )
1355      !!----------------------------------------------------------------------
1356      !!                     ***  ROUTINE set_grid  ***
1357      !!
1358      !! ** Purpose :   define horizontal grids
1359      !!
1360      !!----------------------------------------------------------------------
1361      CHARACTER(LEN=1)            , INTENT(in) ::   cdgrd
1362      REAL(wp), DIMENSION(jpi,jpj), INTENT(in) ::   plon
1363      REAL(wp), DIMENSION(jpi,jpj), INTENT(in) ::   plat
1364      !
1365      REAL(wp), DIMENSION(jpi,jpj,jpk) ::   zmask
1366      INTEGER  :: ni,nj
1367     
1368      ni=nlei-nldi+1 ; nj=nlej-nldj+1
1369
1370#if ! defined key_xios2
1371     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)
1372#else
1373     CALL iom_set_domain_attr("grid_"//cdgrd, ni_glo=jpiglo, nj_glo=jpjglo, ibegin=nimpp+nldi-2, jbegin=njmpp+nldj-2, ni=ni, nj=nj)
1374#endif     
1375      CALL iom_set_domain_attr("grid_"//cdgrd, data_dim=2, data_ibegin = 1-nldi, data_ni = jpi, data_jbegin = 1-nldj, data_nj = jpj)
1376      CALL iom_set_domain_attr("grid_"//cdgrd, lonvalue = RESHAPE(plon(nldi:nlei, nldj:nlej),(/ ni*nj /)),   &
1377         &                                     latvalue = RESHAPE(plat(nldi:nlei, nldj:nlej),(/ ni*nj /))) 
1378
1379      IF ( ln_mskland ) THEN
1380         ! mask land points, keep values on coast line -> specific mask for U, V and W points
1381         SELECT CASE ( cdgrd )
1382         CASE('T')   ;   zmask(:,:,:)       = tmask(:,:,:)
1383         CASE('U')   ;   zmask(2:jpim1,:,:) = tmask(2:jpim1,:,:) + tmask(3:jpi,:,:)   ;   CALL lbc_lnk( zmask, 'U', 1. )
1384         CASE('V')   ;   zmask(:,2:jpjm1,:) = tmask(:,2:jpjm1,:) + tmask(:,3:jpj,:)   ;   CALL lbc_lnk( zmask, 'V', 1. )
1385         CASE('W')   ;   zmask(:,:,2:jpk  ) = tmask(:,:,1:jpkm1) + tmask(:,:,2:jpk)   ;   zmask(:,:,1) = tmask(:,:,1)
1386         END SELECT
1387         !
1388#if ! defined key_xios2
1389         CALL iom_set_domain_attr( "grid_"//cdgrd       , mask = RESHAPE(zmask(nldi:nlei,nldj:nlej,1),(/ni,nj    /)) /= 0. )
1390#else
1391         CALL iom_set_domain_attr( "grid_"//cdgrd       , mask = RESHAPE(zmask(nldi:nlei,nldj:nlej,1),(/ni*nj    /)) /= 0. )
1392#endif 
1393         CALL iom_set_grid_attr  ( "grid_"//cdgrd//"_3D", mask = RESHAPE(zmask(nldi:nlei,nldj:nlej,:),(/ni,nj,jpk/)) /= 0. )
1394      ENDIF
1395     
1396   END SUBROUTINE set_grid
1397
1398
1399   SUBROUTINE set_grid_bounds( cdgrd, plon_cnr, plat_cnr, plon_pnt, plat_pnt )
1400      !!----------------------------------------------------------------------
1401      !!                   ***  ROUTINE set_grid_bounds  ***
1402      !!
1403      !! ** Purpose :   define horizontal grid corners
1404      !!
1405      !!----------------------------------------------------------------------
1406      CHARACTER(LEN=1) , INTENT(in) :: cdgrd
1407      !
1408      REAL(wp), DIMENSION(jpi,jpj), INTENT(in)           :: plon_cnr, plat_cnr  ! Lat/lon coordinates of a contiguous vertex of cell (i,j)
1409      REAL(wp), DIMENSION(jpi,jpj), OPTIONAL, INTENT(in) :: plon_pnt, plat_pnt  ! Lat/lon coordinates of the point of cell (i,j)
1410      !
1411      REAL(wp), ALLOCATABLE, DIMENSION(:,:,:,:)   :: z_bnds      ! Lat/lon coordinates of the vertices of cell (i,j)
1412      REAL(wp), ALLOCATABLE, DIMENSION(:,:)       :: z_fld       ! Working array to determine where to rotate cells
1413      REAL(wp), ALLOCATABLE, DIMENSION(:,:)       :: z_rot       ! Lat/lon working array for rotation of cells
1414      !
1415      INTEGER :: icnr, jcnr                                      ! Offset such that the vertex coordinate (i+icnr,j+jcnr)
1416      !                                                          ! represents the bottom-left corner of cell (i,j)
1417      INTEGER :: ji, jj, jn, ni, nj
1418
1419      ALLOCATE( z_bnds(4,jpi,jpj,2), z_fld(jpi,jpj), z_rot(4,2)  )
1420
1421      ! Offset of coordinate representing bottom-left corner
1422      SELECT CASE ( TRIM(cdgrd) )
1423         CASE ('T', 'W')
1424            icnr = -1 ; jcnr = -1
1425         CASE ('U')
1426            icnr =  0 ; jcnr = -1
1427         CASE ('V')
1428            icnr = -1 ; jcnr =  0
1429      END SELECT
1430
1431      ni = nlei-nldi+1 ; nj = nlej-nldj+1  ! Dimensions of subdomain interior
1432
1433      z_fld(:,:) = 1._wp
1434      CALL lbc_lnk( z_fld, cdgrd, -1. )    ! Working array for location of northfold
1435
1436      ! Cell vertices that can be defined
1437      DO jj = 2, jpjm1
1438         DO ji = 2, jpim1
1439            z_bnds(1,ji,jj,1) = plat_cnr(ji+icnr,  jj+jcnr  ) ! Bottom-left
1440            z_bnds(2,ji,jj,1) = plat_cnr(ji+icnr+1,jj+jcnr  ) ! Bottom-right
1441            z_bnds(3,ji,jj,1) = plat_cnr(ji+icnr+1,jj+jcnr+1) ! Top-right
1442            z_bnds(4,ji,jj,1) = plat_cnr(ji+icnr,  jj+jcnr+1) ! Top-left
1443            z_bnds(1,ji,jj,2) = plon_cnr(ji+icnr,  jj+jcnr  ) ! Bottom-left
1444            z_bnds(2,ji,jj,2) = plon_cnr(ji+icnr+1,jj+jcnr  ) ! Bottom-right
1445            z_bnds(3,ji,jj,2) = plon_cnr(ji+icnr+1,jj+jcnr+1) ! Top-right
1446            z_bnds(4,ji,jj,2) = plon_cnr(ji+icnr,  jj+jcnr+1) ! Top-left
1447         END DO
1448      END DO
1449
1450      ! Cell vertices on boundries
1451      DO jn = 1, 4
1452         CALL lbc_lnk( z_bnds(jn,:,:,1), cdgrd, 1., pval=999._wp )
1453         CALL lbc_lnk( z_bnds(jn,:,:,2), cdgrd, 1., pval=999._wp )
1454      END DO
1455
1456      ! Zero-size cells at closed boundaries if cell points provided,
1457      ! otherwise they are closed cells with unrealistic bounds
1458      IF( PRESENT(plon_pnt) .AND. PRESENT(plat_pnt) ) THEN
1459         IF( (nbondi == -1 .OR. nbondi == 2) .AND. .NOT. (jperio == 1 .OR. jperio == 4 .OR. jperio == 6) ) THEN
1460            DO jn = 1, 4        ! (West or jpni = 1), closed E-W
1461               z_bnds(jn,1,:,1) = plat_pnt(1,:)  ;  z_bnds(jn,1,:,2) = plon_pnt(1,:)
1462            END DO
1463         ENDIF
1464         IF( (nbondi == 1 .OR. nbondi == 2) .AND. .NOT. (jperio == 1 .OR. jperio == 4 .OR. jperio == 6) ) THEN
1465            DO jn = 1, 4        ! (East or jpni = 1), closed E-W
1466               z_bnds(jn,nlci,:,1) = plat_pnt(nlci,:)  ;  z_bnds(jn,nlci,:,2) = plon_pnt(nlci,:)
1467            END DO
1468         ENDIF
1469         IF( nbondj == -1 .OR. (nbondj == 2 .AND. jperio /= 2) ) THEN
1470            DO jn = 1, 4        ! South or (jpnj = 1, not symmetric)
1471               z_bnds(jn,:,1,1) = plat_pnt(:,1)  ;  z_bnds(jn,:,1,2) = plon_pnt(:,1)
1472            END DO
1473         ENDIF
1474         IF( (nbondj == 1 .OR. nbondj == 2) .AND. jperio  < 3 ) THEN
1475            DO jn = 1, 4        ! (North or jpnj = 1), no north fold
1476               z_bnds(jn,:,nlcj,1) = plat_pnt(:,nlcj)  ;  z_bnds(jn,:,nlcj,2) = plon_pnt(:,nlcj)
1477            END DO
1478         ENDIF
1479      ENDIF
1480
1481      ! Rotate cells at the north fold
1482      IF( (nbondj == 1 .OR. nbondj == 2) .AND. jperio >= 3 ) THEN
1483         DO jj = 1, jpj
1484            DO ji = 1, jpi
1485               IF( z_fld(ji,jj) == -1. ) THEN
1486                  z_rot(1,:) = z_bnds(3,ji,jj,:) ; z_rot(2,:) = z_bnds(4,ji,jj,:)
1487                  z_rot(3,:) = z_bnds(1,ji,jj,:) ; z_rot(4,:) = z_bnds(2,ji,jj,:)
1488                  z_bnds(:,ji,jj,:) = z_rot(:,:)
1489               ENDIF
1490            END DO
1491         END DO
1492
1493      ! Invert cells at the symmetric equator
1494      ELSE IF( nbondj == 2 .AND. jperio == 2 ) THEN
1495         DO ji = 1, jpi
1496            z_rot(1:2,:) = z_bnds(3:4,ji,1,:)
1497            z_rot(3:4,:) = z_bnds(1:2,ji,1,:)
1498            z_bnds(:,ji,1,:) = z_rot(:,:)
1499         END DO
1500      ENDIF
1501
1502      CALL iom_set_domain_attr("grid_"//cdgrd, bounds_lat = RESHAPE(z_bnds(:,nldi:nlei,nldj:nlej,1),(/ 4,ni*nj /)),           &
1503                                               bounds_lon = RESHAPE(z_bnds(:,nldi:nlei,nldj:nlej,2),(/ 4,ni*nj /)), nvertex=4 )
1504
1505      DEALLOCATE( z_bnds, z_fld, z_rot ) 
1506
1507   END SUBROUTINE set_grid_bounds
1508
1509
1510   SUBROUTINE set_grid_znl( plat )
1511      !!----------------------------------------------------------------------
1512      !!                     ***  ROUTINE set_grid_znl  ***
1513      !!
1514      !! ** Purpose :   define grids for zonal mean
1515      !!
1516      !!----------------------------------------------------------------------
1517      REAL(wp), DIMENSION(jpi,jpj), INTENT(in) ::   plat
1518      !
1519      REAL(wp), DIMENSION(:), ALLOCATABLE  ::   zlon
1520      INTEGER  :: ni,nj, ix, iy
1521
1522     
1523      ni=nlei-nldi+1 ; nj=nlej-nldj+1            ! define zonal mean domain (jpj*jpk)
1524      ALLOCATE( zlon(ni*nj) )       ;       zlon(:) = 0.
1525
1526      CALL dom_ngb( 180., 90., ix, iy, 'T' ) !  i-line that passes near the North Pole : Reference latitude (used in plots)
1527#if ! defined key_xios2
1528      CALL iom_set_domain_attr("gznl", ni_glo=jpiglo, nj_glo=jpjglo, ibegin=nimpp+nldi-1, jbegin=njmpp+nldj-1, ni=ni, nj=nj)
1529      CALL iom_set_domain_attr("gznl", data_dim=2, data_ibegin = 1-nldi, data_ni = jpi, data_jbegin = 1-nldj, data_nj = jpj)
1530      CALL iom_set_domain_attr("gznl", lonvalue = zlon,   &
1531         &                             latvalue = RESHAPE(plat(nldi:nlei, nldj:nlej),(/ ni*nj /))) 
1532      !
1533      CALL dom_ngb( 180., 90., ix, iy, 'T' ) !  i-line that passes near the North Pole : Reference latitude (used in plots)
1534      CALL iom_set_domain_attr ('ptr', zoom_ibegin=ix, zoom_nj=jpjglo)
1535#else
1536! Pas teste : attention aux indices !
1537      CALL iom_set_domain_attr("gznl", ni_glo=jpiglo, nj_glo=jpjglo, ibegin=nimpp+nldi-2, jbegin=njmpp+nldj-2, ni=ni, nj=nj)
1538      CALL iom_set_domain_attr("gznl", data_dim=2, data_ibegin = 1-nldi, data_ni = jpi, data_jbegin = 1-nldj, data_nj = jpj)
1539      CALL iom_set_domain_attr("gznl", lonvalue = zlon,   &
1540         &                             latvalue = RESHAPE(plat(nldi:nlei, nldj:nlej),(/ ni*nj /))) 
1541       CALL iom_set_zoom_domain_attr ("ptr", ibegin=ix-1, jbegin=0, ni=1, nj=jpjglo)
1542#endif
1543
1544      CALL iom_update_file_name('ptr')
1545      !
1546   END SUBROUTINE set_grid_znl
1547
1548   SUBROUTINE set_scalar
1549      !!----------------------------------------------------------------------
1550      !!                     ***  ROUTINE set_scalar  ***
1551      !!
1552      !! ** Purpose :   define fake grids for scalar point
1553      !!
1554      !!----------------------------------------------------------------------
1555      REAL(wp), DIMENSION(1)   ::   zz = 1.
1556      !!----------------------------------------------------------------------
1557#if ! defined key_xios2
1558      CALL iom_set_domain_attr('scalarpoint', ni_glo=jpnij, nj_glo=1, ibegin=narea, jbegin=1, ni=1, nj=1)
1559#else
1560      CALL iom_set_domain_attr('scalarpoint', ni_glo=jpnij, nj_glo=1, ibegin=narea-1, jbegin=0, ni=1, nj=1)
1561#endif
1562      CALL iom_set_domain_attr('scalarpoint', data_dim=2, data_ibegin = 1, data_ni = 1, data_jbegin = 1, data_nj = 1)
1563     
1564      zz=REAL(narea,wp)
1565      CALL iom_set_domain_attr('scalarpoint', lonvalue=zz, latvalue=zz)
1566
1567   END SUBROUTINE set_scalar
1568
1569
1570   SUBROUTINE set_xmlatt
1571      !!----------------------------------------------------------------------
1572      !!                     ***  ROUTINE set_xmlatt  ***
1573      !!
1574      !! ** Purpose :   automatic definitions of some of the xml attributs...
1575      !!
1576      !!----------------------------------------------------------------------
1577      CHARACTER(len=1),DIMENSION( 3) ::   clgrd                    ! suffix name
1578      CHARACTER(len=256)             ::   clsuff                   ! suffix name
1579      CHARACTER(len=1)               ::   cl1                      ! 1 character
1580      CHARACTER(len=2)               ::   cl2                      ! 2 characters
1581      CHARACTER(len=3)               ::   cl3                      ! 3 characters
1582      INTEGER                        ::   ji, jg                   ! loop counters
1583      INTEGER                        ::   ix, iy                   ! i-,j- index
1584      REAL(wp)        ,DIMENSION(11) ::   zlontao                  ! longitudes of tao    moorings
1585      REAL(wp)        ,DIMENSION( 7) ::   zlattao                  ! latitudes  of tao    moorings
1586      REAL(wp)        ,DIMENSION( 4) ::   zlonrama                 ! longitudes of rama   moorings
1587      REAL(wp)        ,DIMENSION(11) ::   zlatrama                 ! latitudes  of rama   moorings
1588      REAL(wp)        ,DIMENSION( 3) ::   zlonpira                 ! longitudes of pirata moorings
1589      REAL(wp)        ,DIMENSION( 9) ::   zlatpira                 ! latitudes  of pirata moorings
1590#if  defined key_xios2
1591      TYPE(xios_duration)            ::   f_op, f_of
1592#endif
1593 
1594      !!----------------------------------------------------------------------
1595      !
1596      ! frequency of the call of iom_put (attribut: freq_op)
1597#if ! defined key_xios2
1598      WRITE(cl1,'(i1)')        1   ;   CALL iom_set_field_attr('field_definition', freq_op=cl1//'ts', freq_offset='0ts')
1599      WRITE(cl1,'(i1)')  nn_fsbc   ;   CALL iom_set_field_attr('SBC'             , freq_op=cl1//'ts', freq_offset='0ts')
1600      WRITE(cl1,'(i1)')  nn_fsbc   ;   CALL iom_set_field_attr('SBC_scalar'      , freq_op=cl1//'ts', freq_offset='0ts')
1601      WRITE(cl1,'(i1)') nn_dttrc   ;   CALL iom_set_field_attr('ptrc_T'          , freq_op=cl1//'ts', freq_offset='0ts')
1602      WRITE(cl1,'(i1)') nn_dttrc   ;   CALL iom_set_field_attr('diad_T'          , freq_op=cl1//'ts', freq_offset='0ts')
1603#else
1604      f_op%timestep = 1        ;  f_of%timestep = 0  ; CALL iom_set_field_attr('field_definition', freq_op=f_op, freq_offset=f_of)
1605      f_op%timestep = nn_fsbc  ;  f_of%timestep = 0  ; CALL iom_set_field_attr('SBC'             , freq_op=f_op, freq_offset=f_of)
1606      f_op%timestep = nn_fsbc  ;  f_of%timestep = 0  ; CALL iom_set_field_attr('SBC_scalar'      , freq_op=f_op, freq_offset=f_of)
1607      f_op%timestep = nn_dttrc ;  f_of%timestep = 0  ; CALL iom_set_field_attr('ptrc_T'          , freq_op=f_op, freq_offset=f_of)
1608      f_op%timestep = nn_dttrc ;  f_of%timestep = 0  ; CALL iom_set_field_attr('diad_T'          , freq_op=f_op, freq_offset=f_of)
1609#endif
1610       
1611      ! output file names (attribut: name)
1612      DO ji = 1, 9
1613         WRITE(cl1,'(i1)') ji 
1614         CALL iom_update_file_name('file'//cl1)
1615      END DO
1616      DO ji = 1, 99
1617         WRITE(cl2,'(i2.2)') ji 
1618         CALL iom_update_file_name('file'//cl2)
1619      END DO
1620      DO ji = 1, 999
1621         WRITE(cl3,'(i3.3)') ji 
1622         CALL iom_update_file_name('file'//cl3)
1623      END DO
1624
1625      ! Zooms...
1626      clgrd = (/ 'T', 'U', 'W' /) 
1627      DO jg = 1, SIZE(clgrd)                                                                   ! grid type
1628         cl1 = clgrd(jg)
1629         ! Equatorial section (attributs: jbegin, ni, name_suffix)
1630         CALL dom_ngb( 0., 0., ix, iy, cl1 )
1631#if ! defined key_xios2
1632         CALL iom_set_domain_attr ('Eq'//cl1, zoom_jbegin=iy, zoom_ni=jpiglo)
1633#else
1634         CALL iom_set_zoom_domain_attr ('Eq'//cl1, jbegin=iy-1, ni=jpiglo)
1635#endif
1636         CALL iom_get_file_attr   ('Eq'//cl1, name_suffix = clsuff             )
1637         CALL iom_set_file_attr   ('Eq'//cl1, name_suffix = TRIM(clsuff)//'_Eq')
1638         CALL iom_update_file_name('Eq'//cl1)
1639      END DO
1640      ! TAO moorings (attributs: ibegin, jbegin, name_suffix)
1641      zlontao = (/ 137.0, 147.0, 156.0, 165.0, -180.0, -170.0, -155.0, -140.0, -125.0, -110.0, -95.0 /)
1642      zlattao = (/  -8.0,  -5.0,  -2.0,   0.0,    2.0,    5.0,    8.0 /)
1643      CALL set_mooring( zlontao, zlattao )
1644      ! RAMA moorings (attributs: ibegin, jbegin, name_suffix)
1645      zlonrama = (/  55.0,  67.0, 80.5, 90.0 /)
1646      zlatrama = (/ -16.0, -12.0, -8.0, -4.0, -1.5, 0.0, 1.5, 4.0, 8.0, 12.0, 15.0 /)
1647      CALL set_mooring( zlonrama, zlatrama )
1648      ! PIRATA moorings (attributs: ibegin, jbegin, name_suffix)
1649      zlonpira = (/ -38.0, -23.0, -10.0 /)
1650      zlatpira = (/ -19.0, -14.0,  -8.0, 0.0, 4.0, 8.0, 12.0, 15.0, 20.0 /)
1651      CALL set_mooring( zlonpira, zlatpira )
1652
1653     
1654   END SUBROUTINE set_xmlatt
1655
1656
1657   SUBROUTINE set_mooring( plon, plat)
1658      !!----------------------------------------------------------------------
1659      !!                     ***  ROUTINE set_mooring  ***
1660      !!
1661      !! ** Purpose :   automatic definitions of moorings xml attributs...
1662      !!
1663      !!----------------------------------------------------------------------
1664      REAL(wp), DIMENSION(:), INTENT(in) ::  plon, plat           ! longitudes/latitudes oft the mooring
1665      !
1666!!$      CHARACTER(len=1),DIMENSION(4) ::   clgrd = (/ 'T', 'U', 'V', 'W' /)   ! suffix name
1667      CHARACTER(len=1),DIMENSION(1) ::   clgrd = (/ 'T' /)        ! suffix name
1668      CHARACTER(len=256)            ::   clname                   ! file name
1669      CHARACTER(len=256)            ::   clsuff                   ! suffix name
1670      CHARACTER(len=1)              ::   cl1                      ! 1 character
1671      CHARACTER(len=6)              ::   clon,clat                ! name of longitude, latitude
1672      INTEGER                       ::   ji, jj, jg               ! loop counters
1673      INTEGER                       ::   ix, iy                   ! i-,j- index
1674      REAL(wp)                      ::   zlon, zlat
1675      !!----------------------------------------------------------------------
1676      DO jg = 1, SIZE(clgrd)
1677         cl1 = clgrd(jg)
1678         DO ji = 1, SIZE(plon)
1679            DO jj = 1, SIZE(plat)
1680               zlon = plon(ji)
1681               zlat = plat(jj)
1682               ! modifications for RAMA moorings
1683               IF( zlon ==  67. .AND. zlat ==  15. )   zlon =  65.
1684               IF( zlon ==  90. .AND. zlat <=  -4. )   zlon =  95.
1685               IF( zlon ==  95. .AND. zlat ==  -4. )   zlat =  -5.
1686               ! modifications for PIRATA moorings
1687               IF( zlon == -38. .AND. zlat == -19. )   zlon = -34.
1688               IF( zlon == -38. .AND. zlat == -14. )   zlon = -32.
1689               IF( zlon == -38. .AND. zlat ==  -8. )   zlon = -30.
1690               IF( zlon == -38. .AND. zlat ==   0. )   zlon = -35.
1691               IF( zlon == -23. .AND. zlat ==  20. )   zlat =  21.
1692               IF( zlon == -10. .AND. zlat == -14. )   zlat = -10.
1693               IF( zlon == -10. .AND. zlat ==  -8. )   zlat =  -6.
1694               IF( zlon == -10. .AND. zlat ==   4. ) THEN   ;   zlon = 0.   ;   zlat = 0.   ;   ENDIF
1695               CALL dom_ngb( zlon, zlat, ix, iy, cl1 )
1696               IF( zlon >= 0. ) THEN 
1697                  IF( zlon == REAL(NINT(zlon), wp) ) THEN   ;   WRITE(clon, '(i3,  a)') NINT( zlon), 'e'
1698                  ELSE                                      ;   WRITE(clon, '(f5.1,a)')       zlon , 'e'
1699                  ENDIF
1700               ELSE             
1701                  IF( zlon == REAL(NINT(zlon), wp) ) THEN   ;   WRITE(clon, '(i3,  a)') NINT(-zlon), 'w'
1702                  ELSE                                      ;   WRITE(clon, '(f5.1,a)')      -zlon , 'w'
1703                  ENDIF
1704               ENDIF
1705               IF( zlat >= 0. ) THEN 
1706                  IF( zlat == REAL(NINT(zlat), wp) ) THEN   ;   WRITE(clat, '(i2,  a)') NINT( zlat), 'n'
1707                  ELSE                                      ;   WRITE(clat, '(f4.1,a)')       zlat , 'n'
1708                  ENDIF
1709               ELSE             
1710                  IF( zlat == REAL(NINT(zlat), wp) ) THEN   ;   WRITE(clat, '(i2,  a)') NINT(-zlat), 's'
1711                  ELSE                                      ;   WRITE(clat, '(f4.1,a)')      -zlat , 's'
1712                  ENDIF
1713               ENDIF
1714               clname = TRIM(ADJUSTL(clat))//TRIM(ADJUSTL(clon))
1715#if ! defined key_xios2
1716               CALL iom_set_domain_attr (TRIM(clname)//cl1, zoom_ibegin= ix, zoom_jbegin= iy)
1717#else
1718               CALL iom_set_zoom_domain_attr  (TRIM(clname)//cl1, ibegin= ix-1, jbegin= iy-1)
1719#endif
1720               CALL iom_get_file_attr   (TRIM(clname)//cl1, name_suffix = clsuff                         )
1721               CALL iom_set_file_attr   (TRIM(clname)//cl1, name_suffix = TRIM(clsuff)//'_'//TRIM(clname))
1722               CALL iom_update_file_name(TRIM(clname)//cl1)
1723            END DO
1724         END DO
1725      END DO
1726     
1727   END SUBROUTINE set_mooring
1728
1729   
1730   SUBROUTINE iom_update_file_name( cdid )
1731      !!----------------------------------------------------------------------
1732      !!                     ***  ROUTINE iom_update_file_name  ***
1733      !!
1734      !! ** Purpose :   
1735      !!
1736      !!----------------------------------------------------------------------
1737      CHARACTER(LEN=*)          , INTENT(in) ::   cdid
1738      !
1739      CHARACTER(LEN=256) ::   clname
1740      CHARACTER(LEN=20)  ::   clfreq
1741      CHARACTER(LEN=20)  ::   cldate
1742      INTEGER            ::   idx
1743      INTEGER            ::   jn
1744      INTEGER            ::   itrlen
1745      INTEGER            ::   iyear, imonth, iday, isec
1746      REAL(wp)           ::   zsec
1747      LOGICAL            ::   llexist
1748#if  defined key_xios2
1749      TYPE(xios_duration)   ::   output_freq 
1750#endif     
1751      !!----------------------------------------------------------------------
1752
1753      DO jn = 1,2
1754#if ! defined key_xios2
1755         IF( jn == 1 )   CALL iom_get_file_attr( cdid, name        = clname, output_freq = clfreq )
1756#else
1757         output_freq = xios_duration(0,0,0,0,0,0)
1758         IF( jn == 1 )   CALL iom_get_file_attr( cdid, name        = clname, output_freq = output_freq )
1759#endif
1760         IF( jn == 2 )   CALL iom_get_file_attr( cdid, name_suffix = clname )
1761
1762         IF ( TRIM(clname) /= '' ) THEN
1763
1764            idx = INDEX(clname,'@expname@') + INDEX(clname,'@EXPNAME@')
1765            DO WHILE ( idx /= 0 ) 
1766               clname = clname(1:idx-1)//TRIM(cexper)//clname(idx+9:LEN_TRIM(clname))
1767               idx = INDEX(clname,'@expname@') + INDEX(clname,'@EXPNAME@')
1768            END DO
1769
1770#if ! defined key_xios2
1771            idx = INDEX(clname,'@freq@') + INDEX(clname,'@FREQ@')
1772            DO WHILE ( idx /= 0 ) 
1773               IF ( TRIM(clfreq) /= '' ) THEN
1774                  itrlen = LEN_TRIM(clfreq)
1775                  IF ( clfreq(itrlen-1:itrlen) == 'mo' ) clfreq = clfreq(1:itrlen-1)
1776                  clname = clname(1:idx-1)//TRIM(clfreq)//clname(idx+6:LEN_TRIM(clname))
1777               ELSE
1778                  CALL ctl_stop('error in the name of file id '//TRIM(cdid),   &
1779                     & ' attribute output_freq is undefined -> cannot replace @freq@ in '//TRIM(clname) )
1780               ENDIF
1781               idx = INDEX(clname,'@freq@') + INDEX(clname,'@FREQ@')
1782            END DO
1783#else
1784            idx = INDEX(clname,'@freq@') + INDEX(clname,'@FREQ@')
1785            DO WHILE ( idx /= 0 ) 
1786              IF ( output_freq%timestep /= 0) THEN
1787                  WRITE(clfreq,'(I18,A2)')INT(output_freq%timestep),'ts' 
1788                  itrlen = LEN_TRIM(ADJUSTL(clfreq))
1789              ELSE IF ( output_freq%hour /= 0 ) THEN
1790                  WRITE(clfreq,'(I19,A1)')INT(output_freq%hour),'h' 
1791                  itrlen = LEN_TRIM(ADJUSTL(clfreq))
1792              ELSE IF ( output_freq%day /= 0 ) THEN
1793                  WRITE(clfreq,'(I19,A1)')INT(output_freq%day),'d' 
1794                  itrlen = LEN_TRIM(ADJUSTL(clfreq))
1795              ELSE IF ( output_freq%month /= 0 ) THEN   
1796                  WRITE(clfreq,'(I19,A1)')INT(output_freq%month),'m' 
1797                  itrlen = LEN_TRIM(ADJUSTL(clfreq))
1798              ELSE IF ( output_freq%year /= 0 ) THEN   
1799                  WRITE(clfreq,'(I19,A1)')INT(output_freq%year),'y' 
1800                  itrlen = LEN_TRIM(ADJUSTL(clfreq))
1801              ELSE
1802                  CALL ctl_stop('error in the name of file id '//TRIM(cdid),   &
1803                     & ' attribute output_freq is undefined -> cannot replace @freq@ in '//TRIM(clname) )
1804              ENDIF
1805              clname = clname(1:idx-1)//TRIM(ADJUSTL(clfreq))//clname(idx+6:LEN_TRIM(clname))
1806              idx = INDEX(clname,'@freq@') + INDEX(clname,'@FREQ@')
1807            END DO
1808#endif
1809            idx = INDEX(clname,'@startdate@') + INDEX(clname,'@STARTDATE@')
1810            DO WHILE ( idx /= 0 ) 
1811               cldate = iom_sdate( fjulday - rdt / rday )
1812               clname = clname(1:idx-1)//TRIM(cldate)//clname(idx+11:LEN_TRIM(clname))
1813               idx = INDEX(clname,'@startdate@') + INDEX(clname,'@STARTDATE@')
1814            END DO
1815
1816            idx = INDEX(clname,'@startdatefull@') + INDEX(clname,'@STARTDATEFULL@')
1817            DO WHILE ( idx /= 0 ) 
1818               cldate = iom_sdate( fjulday - rdt / rday, ldfull = .TRUE. )
1819               clname = clname(1:idx-1)//TRIM(cldate)//clname(idx+15:LEN_TRIM(clname))
1820               idx = INDEX(clname,'@startdatefull@') + INDEX(clname,'@STARTDATEFULL@')
1821            END DO
1822
1823            idx = INDEX(clname,'@enddate@') + INDEX(clname,'@ENDDATE@')
1824            DO WHILE ( idx /= 0 ) 
1825               cldate = iom_sdate( fjulday + rdt / rday * REAL( nitend - nit000, wp ), ld24 = .TRUE. )
1826               clname = clname(1:idx-1)//TRIM(cldate)//clname(idx+9:LEN_TRIM(clname))
1827               idx = INDEX(clname,'@enddate@') + INDEX(clname,'@ENDDATE@')
1828            END DO
1829
1830            idx = INDEX(clname,'@enddatefull@') + INDEX(clname,'@ENDDATEFULL@')
1831            DO WHILE ( idx /= 0 ) 
1832               cldate = iom_sdate( fjulday + rdt / rday * REAL( nitend - nit000, wp ), ld24 = .TRUE., ldfull = .TRUE. )
1833               clname = clname(1:idx-1)//TRIM(cldate)//clname(idx+13:LEN_TRIM(clname))
1834               idx = INDEX(clname,'@enddatefull@') + INDEX(clname,'@ENDDATEFULL@')
1835            END DO
1836
1837            IF( jn == 1 .AND. TRIM(Agrif_CFixed()) /= '0' )   clname = TRIM(Agrif_CFixed())//"_"//TRIM(clname)
1838            IF( jn == 1 )   CALL iom_set_file_attr( cdid, name        = clname )
1839            IF( jn == 2 )   CALL iom_set_file_attr( cdid, name_suffix = clname )
1840
1841         ENDIF
1842
1843      END DO
1844
1845   END SUBROUTINE iom_update_file_name
1846
1847
1848   FUNCTION iom_sdate( pjday, ld24, ldfull )
1849      !!----------------------------------------------------------------------
1850      !!                     ***  ROUTINE iom_sdate  ***
1851      !!
1852      !! ** Purpose :   send back the date corresponding to the given julian day
1853      !!
1854      !!----------------------------------------------------------------------
1855      REAL(wp), INTENT(in   )           ::   pjday         ! julian day
1856      LOGICAL , INTENT(in   ), OPTIONAL ::   ld24          ! true to force 24:00 instead of 00:00
1857      LOGICAL , INTENT(in   ), OPTIONAL ::   ldfull        ! true to get the compleate date: yyyymmdd_hh:mm:ss
1858      !
1859      CHARACTER(LEN=20) ::   iom_sdate
1860      CHARACTER(LEN=50) ::   clfmt                         !  format used to write the date
1861      INTEGER           ::   iyear, imonth, iday, ihour, iminute, isec
1862      REAL(wp)          ::   zsec
1863      LOGICAL           ::   ll24, llfull
1864      !
1865      IF( PRESENT(ld24) ) THEN   ;   ll24 = ld24
1866      ELSE                       ;   ll24 = .FALSE.
1867      ENDIF
1868
1869      IF( PRESENT(ldfull) ) THEN   ;   llfull = ldfull
1870      ELSE                         ;   llfull = .FALSE.
1871      ENDIF
1872
1873      CALL ju2ymds( pjday, iyear, imonth, iday, zsec )
1874      isec = NINT(zsec)
1875
1876      IF ( ll24 .AND. isec == 0 ) THEN   ! 00:00 of the next day -> move to 24:00 of the current day
1877         CALL ju2ymds( pjday - 1., iyear, imonth, iday, zsec )
1878         isec = 86400
1879      ENDIF
1880
1881      IF( iyear < 10000 ) THEN   ;   clfmt = "i4.4,2i2.2"                ! format used to write the date
1882      ELSE                       ;   WRITE(clfmt, "('i',i1,',2i2.2')") INT(LOG10(REAL(iyear,wp))) + 1
1883      ENDIF
1884     
1885!$AGRIF_DO_NOT_TREAT     
1886! Should be fixed in the conv
1887      IF( llfull ) THEN
1888         clfmt = TRIM(clfmt)//",'_',i2.2,':',i2.2,':',i2.2"
1889         ihour   = isec / 3600
1890         isec    = MOD(isec, 3600)
1891         iminute = isec / 60
1892         isec    = MOD(isec, 60)
1893         WRITE(iom_sdate, '('//TRIM(clfmt)//')') iyear, imonth, iday, ihour, iminute, isec    ! date of the end of run
1894      ELSE
1895         WRITE(iom_sdate, '('//TRIM(clfmt)//')') iyear, imonth, iday                          ! date of the end of run
1896      ENDIF
1897!$AGRIF_END_DO_NOT_TREAT     
1898
1899   END FUNCTION iom_sdate
1900
1901#else
1902
1903
1904   SUBROUTINE iom_setkt( kt, cdname )
1905      INTEGER         , INTENT(in)::   kt 
1906      CHARACTER(LEN=*), INTENT(in) ::   cdname
1907      IF( .FALSE. )   WRITE(numout,*) kt, cdname   ! useless test to avoid compilation warnings
1908   END SUBROUTINE iom_setkt
1909
1910   SUBROUTINE iom_context_finalize( cdname )
1911      CHARACTER(LEN=*), INTENT(in) ::   cdname
1912      IF( .FALSE. )   WRITE(numout,*)  cdname   ! useless test to avoid compilation warnings
1913   END SUBROUTINE iom_context_finalize
1914
1915#endif
1916
1917   LOGICAL FUNCTION iom_use( cdname )
1918      CHARACTER(LEN=*), INTENT(in) ::   cdname
1919#if defined key_iomput
1920      iom_use = xios_field_is_active( cdname )
1921#else
1922      iom_use = .FALSE.
1923#endif
1924   END FUNCTION iom_use
1925   
1926   !!======================================================================
1927END MODULE iom
Note: See TracBrowser for help on using the repository browser.