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

source: branches/UKMO/AMM15_v3_6_STABLE_package_reanalysis4/NEMOGCM/NEMO/OPA_SRC/IOM/iom.F90 @ 11639

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

regional mean and transport diagnostics added for reanalysis

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