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

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

source: branches/UKMO/dev_r5518_25hr_mean_assim_bkg/NEMOGCM/NEMO/OPA_SRC/IOM/iom.F90 @ 6758

Last change on this file since 6758 was 6758, checked in by kingr, 8 years ago

Merged branches/UKMO/nemo_v3_6_STABLE_copy@6237

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