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/UKMO/dev_r6436_linkOasisNoKeys/NEMOGCM/NEMO/OPA_SRC/IOM – NEMO

source: branches/UKMO/dev_r6436_linkOasisNoKeys/NEMOGCM/NEMO/OPA_SRC/IOM/iom.F90 @ 6880

Last change on this file since 6880 was 6880, checked in by stephenhaddad, 8 years ago

crum #160fixing compatibility between xios-1.0 and NEMO v3.6 STABLE branch.

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