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/2015/dev_r5003_MERCATOR6_CRS/NEMOGCM/NEMO/OPA_SRC/IOM – NEMO

source: branches/2015/dev_r5003_MERCATOR6_CRS/NEMOGCM/NEMO/OPA_SRC/IOM/iom.F90 @ 7217

Last change on this file since 7217 was 7217, checked in by cbricaud, 7 years ago

CRS branch: code cleaning

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