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

source: branches/2017/dev_r7864_XIOS_defs/NEMOGCM/NEMO/OPA_SRC/IOM/iom.F90 @ 8669

Last change on this file since 8669 was 8669, checked in by andmirek, 6 years ago

fix error in iom_set_zoom_domain_attr

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