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

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

source: branches/2014/dev_r4650_UKMO14.5_SST_BIAS_CORRECTION/NEMOGCM/NEMO/OPA_SRC/IOM/iom.F90 @ 5965

Last change on this file since 5965 was 5965, checked in by timgraham, 8 years ago

Upgraded branch to r5518 of trunk (v3.6 stable revision)

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