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

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

source: branches/UKMO/AMM15_v3_6_STABLE_package_reanalysis2/NEMOGCM/NEMO/OPA_SRC/IOM/iom.F90 @ 10958

Last change on this file since 10958 was 10958, checked in by rrenshaw, 5 years ago

added regional mean code to 3.6 branch for AMM7/15 suite

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