source: LMDZ5/branches/testing/libf/phylmd/iophy.F90 @ 5423

Last change on this file since 5423 was 2870, checked in by Laurent Fairhead, 8 years ago

Merged trunk changes r2842:2865 into testing branch

  • Property copyright set to
    Name of program: LMDZ
    Creation date: 1984
    Version: LMDZ5
    License: CeCILL version 2
    Holder: Laboratoire de m\'et\'eorologie dynamique, CNRS, UMR 8539
    See the license file in the root directory
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 45.3 KB
Line 
1!
2! $Id: iophy.F90 2870 2017-05-04 07:31:05Z evignon $
3!
4MODULE iophy
5
6! abd  REAL,private,allocatable,DIMENSION(:),save :: io_lat
7! abd  REAL,private,allocatable,DIMENSION(:),save :: io_lon
8  REAL,ALLOCATABLE,DIMENSION(:),SAVE :: io_lat
9  REAL,ALLOCATABLE,DIMENSION(:),SAVE :: io_lon
10  INTEGER, SAVE :: phys_domain_id
11  INTEGER, SAVE :: npstn
12  INTEGER, ALLOCATABLE, DIMENSION(:), SAVE :: nptabij
13  INTEGER, SAVE :: itau_iophy
14
15!$OMP THREADPRIVATE(itau_iophy)
16
17#ifdef CPP_XIOS
18  INTERFACE histwrite_phy
19!#ifdef CPP_XIOSnew
20    MODULE PROCEDURE histwrite2d_phy,histwrite3d_phy,histwrite2d_phy_old,histwrite3d_phy_old,histwrite2d_xios,histwrite3d_xios,histwrite0d_xios
21!#else
22!    MODULE PROCEDURE histwrite2d_phy,histwrite3d_phy,histwrite2d_phy_old,histwrite3d_phy_old,histwrite2d_xios,histwrite3d_xios
23!#endif
24
25  END INTERFACE
26#else
27  INTERFACE histwrite_phy
28    MODULE PROCEDURE histwrite2d_phy,histwrite3d_phy,histwrite2d_phy_old,histwrite3d_phy_old
29  END INTERFACE
30#endif
31
32  INTERFACE histbeg_phy_all
33    MODULE PROCEDURE histbeg_phy,histbeg_phyxios,histbeg_phy_points
34  END INTERFACE
35
36
37CONTAINS
38
39! ug Routine pour définir itau_iophy depuis phys_output_write_mod:
40  SUBROUTINE set_itau_iophy(ito)
41      IMPLICIT NONE
42      INTEGER, INTENT(IN) :: ito
43      itau_iophy = ito
44  END SUBROUTINE
45
46  SUBROUTINE init_iophy_new(rlat,rlon)
47  USE dimphy, ONLY: klon
48  USE mod_phys_lmdz_para, ONLY: gather, bcast, &
49                                jj_nb, jj_begin, jj_end, ii_begin, ii_end, &
50                                mpi_size, mpi_rank, klon_mpi, &
51                                is_sequential, is_south_pole_dyn
52  USE mod_grid_phy_lmdz, ONLY: nbp_lon, nbp_lat, klon_glo
53  USE print_control_mod, ONLY: prt_level,lunout
54#ifdef CPP_IOIPSL
55  USE ioipsl, ONLY: flio_dom_set
56#endif
57#ifdef CPP_XIOS
58  USE wxios, ONLY: wxios_domain_param
59#endif
60  IMPLICIT NONE
61    REAL,DIMENSION(klon),INTENT(IN) :: rlon
62    REAL,DIMENSION(klon),INTENT(IN) :: rlat
63
64    REAL,DIMENSION(klon_glo)        :: rlat_glo
65    REAL,DIMENSION(klon_glo)        :: rlon_glo
66   
67    INTEGER,DIMENSION(2) :: ddid
68    INTEGER,DIMENSION(2) :: dsg
69    INTEGER,DIMENSION(2) :: dsl
70    INTEGER,DIMENSION(2) :: dpf
71    INTEGER,DIMENSION(2) :: dpl
72    INTEGER,DIMENSION(2) :: dhs
73    INTEGER,DIMENSION(2) :: dhe
74    INTEGER :: i   
75    INTEGER :: data_ibegin, data_iend
76
77    CALL gather(rlat,rlat_glo)
78    CALL bcast(rlat_glo)
79    CALL gather(rlon,rlon_glo)
80    CALL bcast(rlon_glo)
81   
82!$OMP MASTER 
83    ALLOCATE(io_lat(nbp_lat))
84    IF (klon_glo == 1) THEN
85      io_lat(1)=rlat_glo(1)
86    ELSE
87      io_lat(1)=rlat_glo(1)
88      io_lat(nbp_lat)=rlat_glo(klon_glo)
89      DO i=2,nbp_lat-1
90        io_lat(i)=rlat_glo(2+(i-2)*nbp_lon)
91      ENDDO
92    ENDIF
93
94    ALLOCATE(io_lon(nbp_lon))
95    IF (klon_glo == 1) THEN
96      io_lon(1)=rlon_glo(1)
97    ELSE
98      io_lon(1:nbp_lon)=rlon_glo(2:nbp_lon+1)
99    ENDIF
100
101!! (I) dtnb   : total number of domains
102!! (I) dnb    : domain number
103!! (I) did(:) : distributed dimensions identifiers
104!!              (up to 5 dimensions are supported)
105!! (I) dsg(:) : total number of points for each dimension
106!! (I) dsl(:) : local number of points for each dimension
107!! (I) dpf(:) : position of first local point for each dimension
108!! (I) dpl(:) : position of last local point for each dimension
109!! (I) dhs(:) : start halo size for each dimension
110!! (I) dhe(:) : end halo size for each dimension
111!! (C) cdnm   : Model domain definition name.
112!!              The names actually supported are :
113!!              "BOX", "APPLE", "ORANGE".
114!!              These names are case insensitive.
115
116    ddid=(/ 1,2 /)
117    dsg=(/ nbp_lon, nbp_lat /)
118    dsl=(/ nbp_lon, jj_nb /)
119    dpf=(/ 1,jj_begin /)
120    dpl=(/ nbp_lon, jj_end /)
121    dhs=(/ ii_begin-1,0 /)
122    IF (mpi_rank==mpi_size-1) THEN
123      dhe=(/0,0/)
124    ELSE
125      dhe=(/ nbp_lon-ii_end,0 /) 
126    ENDIF
127
128#ifndef CPP_IOIPSL_NO_OUTPUT   
129    CALL flio_dom_set(mpi_size,mpi_rank,ddid,dsg,dsl,dpf,dpl,dhs,dhe, &
130                      'APPLE',phys_domain_id)
131#endif
132#ifdef CPP_XIOS
133    ! Set values for the mask:
134    IF (mpi_rank == 0) THEN
135        data_ibegin = 0
136    ELSE
137        data_ibegin = ii_begin - 1
138    ENDIF
139
140    IF (mpi_rank == mpi_size-1) THEN
141        data_iend = nbp_lon
142    ELSE
143        data_iend = ii_end + 1
144    ENDIF
145
146    IF (prt_level>=10) THEN
147      write(lunout,*) "init_iophy_new: mpirank=",mpi_rank," iibegin=",ii_begin , " ii_end=",ii_end," jjbegin=",jj_begin," jj_nb=",jj_nb," jj_end=",jj_end
148      write(lunout,*) "init_iophy_new: mpirank=",mpi_rank," nbp_lon=",nbp_lon," nbp_lat=",nbp_lat
149      write(lunout,*) "init_iophy_new: mpirank=",mpi_rank," data_ibegin=",data_ibegin," data_iend=",data_iend
150      write(lunout,*) "init_iophy_new: mpirank=",mpi_rank," data_ibegin=",data_ibegin," data_iend=",data_iend
151      write(lunout,*) "init_iophy_new: mpirank=",mpi_rank," is_south_pole=",is_south_pole_dyn
152    ENDIF
153
154    ! Initialize the XIOS domain coreesponding to this process:
155    CALL wxios_domain_param("dom_glo", is_sequential, nbp_lon, jj_nb, nbp_lon, nbp_lat, &
156                            1, nbp_lon, ii_begin, ii_end, jj_begin, jj_end,             &
157                            klon_mpi+2*(nbp_lon-1), data_ibegin, data_iend,             &
158                            io_lat, io_lon,is_south_pole_dyn,mpi_rank)
159#endif
160!$OMP END MASTER
161     
162  END SUBROUTINE init_iophy_new
163
164  SUBROUTINE init_iophy(lat,lon)
165  USE mod_phys_lmdz_para, ONLY: jj_begin, jj_end, ii_begin, ii_end, jj_nb, &
166                                mpi_size, mpi_rank
167  USE ioipsl, ONLY: flio_dom_set
168  USE mod_grid_phy_lmdz, ONLY : nbp_lon, nbp_lat
169  IMPLICIT NONE
170    REAL,DIMENSION(nbp_lon),INTENT(IN) :: lon
171    REAL,DIMENSION(nbp_lat),INTENT(IN) :: lat
172
173    INTEGER,DIMENSION(2) :: ddid
174    INTEGER,DIMENSION(2) :: dsg
175    INTEGER,DIMENSION(2) :: dsl
176    INTEGER,DIMENSION(2) :: dpf
177    INTEGER,DIMENSION(2) :: dpl
178    INTEGER,DIMENSION(2) :: dhs
179    INTEGER,DIMENSION(2) :: dhe
180
181!$OMP MASTER 
182    ALLOCATE(io_lat(nbp_lat))
183    io_lat(:)=lat(:)
184    ALLOCATE(io_lon(nbp_lon))
185    io_lon(:)=lon(:)
186   
187    ddid=(/ 1,2 /)
188    dsg=(/ nbp_lon, nbp_lat /)
189    dsl=(/ nbp_lon, jj_nb /)
190    dpf=(/ 1,jj_begin /)
191    dpl=(/ nbp_lon, jj_end /)
192    dhs=(/ ii_begin-1,0 /)
193    IF (mpi_rank==mpi_size-1) THEN
194      dhe=(/0,0/)
195    ELSE
196      dhe=(/ nbp_lon-ii_end,0 /) 
197    ENDIF
198   
199#ifndef CPP_IOIPSL_NO_OUTPUT
200    call flio_dom_set(mpi_size,mpi_rank,ddid,dsg,dsl,dpf,dpl,dhs,dhe, &
201                      'APPLE',phys_domain_id)
202#endif
203!$OMP END MASTER
204     
205  END SUBROUTINE init_iophy
206
207 SUBROUTINE histbeg_phyxios(name,itau0,zjulian,dtime,ffreq,lev,nhori,nid_day)
208!  USE dimphy
209  USE mod_phys_lmdz_para, ONLY: is_sequential, is_using_mpi, is_mpi_root, &
210                                jj_begin, jj_end, jj_nb
211  USE mod_grid_phy_lmdz, ONLY : nbp_lon, nbp_lat
212  USE ioipsl, ONLY: histbeg
213#ifdef CPP_XIOS
214  USE wxios, ONLY: wxios_add_file
215#endif
216  IMPLICIT NONE
217  include 'clesphys.h'
218   
219    CHARACTER*(*), INTENT(IN) :: name
220    INTEGER, INTENT(IN) :: itau0
221    REAL,INTENT(IN) :: zjulian
222    REAL,INTENT(IN) :: dtime
223    CHARACTER(LEN=*), INTENT(IN) :: ffreq
224    INTEGER,INTENT(IN) :: lev
225    INTEGER,INTENT(OUT) :: nhori
226    INTEGER,INTENT(OUT) :: nid_day
227
228!$OMP MASTER   
229    IF (is_sequential) THEN
230      call histbeg(name,nbp_lon,io_lon, jj_nb,io_lat(jj_begin:jj_end), &
231                   1,nbp_lon,1,jj_nb,itau0, zjulian, dtime, nhori, nid_day)
232    ELSE
233      call histbeg(name,nbp_lon,io_lon, jj_nb,io_lat(jj_begin:jj_end), &
234                   1,nbp_lon,1,jj_nb,itau0, zjulian, dtime, nhori, nid_day,phys_domain_id)
235    ENDIF
236
237#ifdef CPP_XIOS
238    ! ug OMP en chantier...
239    IF((.NOT. is_using_mpi) .OR. is_mpi_root) THEN
240        ! ug Création du fichier
241      IF (.not. ok_all_xml) THEN
242        CALL wxios_add_file(name, ffreq, lev)
243      ENDIF
244    ENDIF
245#endif
246!$OMP END MASTER
247 
248  END SUBROUTINE histbeg_phyxios
249 
250  SUBROUTINE histbeg_phy(name,itau0,zjulian,dtime,nhori,nid_day)
251
252  USE mod_phys_lmdz_para, ONLY: jj_begin, jj_end, jj_nb, is_sequential
253  USE mod_grid_phy_lmdz, ONLY : nbp_lon, nbp_lat
254  USE ioipsl, ONLY: histbeg
255
256  IMPLICIT NONE
257   
258    CHARACTER*(*), INTENT(IN) :: name
259    INTEGER, INTENT(IN) :: itau0
260    REAL,INTENT(IN) :: zjulian
261    REAL,INTENT(IN) :: dtime
262    INTEGER,INTENT(OUT) :: nhori
263    INTEGER,INTENT(OUT) :: nid_day
264
265!$OMP MASTER   
266#ifndef CPP_IOIPSL_NO_OUTPUT
267    IF (is_sequential) THEN
268      call histbeg(name,nbp_lon,io_lon, jj_nb,io_lat(jj_begin:jj_end), &
269                   1,nbp_lon,1,jj_nb,itau0, zjulian, dtime, nhori, nid_day)
270    ELSE
271      call histbeg(name,nbp_lon,io_lon, jj_nb,io_lat(jj_begin:jj_end), &
272                   1,nbp_lon,1,jj_nb,itau0, zjulian, dtime, nhori, nid_day,phys_domain_id)
273    ENDIF
274#endif
275!$OMP END MASTER
276 
277  END SUBROUTINE histbeg_phy
278
279
280  SUBROUTINE histbeg_phy_points(rlon,rlat,pim,tabij,ipt,jpt, &
281             plon,plat,plon_bounds,plat_bounds, &
282             nname,itau0,zjulian,dtime,nnhori,nnid_day)
283  USE dimphy, ONLY: klon
284  USE mod_phys_lmdz_para, ONLY: gather, bcast, &
285                                is_sequential, klon_mpi_begin, klon_mpi_end, &
286                                mpi_rank
287  USE mod_grid_phy_lmdz, ONLY: klon_glo, nbp_lon, nbp_lat
288  USE ioipsl, ONLY: histbeg
289
290  IMPLICIT NONE
291
292    REAL,DIMENSION(klon),INTENT(IN) :: rlon
293    REAL,DIMENSION(klon),INTENT(IN) :: rlat
294    INTEGER, INTENT(IN) :: itau0
295    REAL,INTENT(IN) :: zjulian
296    REAL,INTENT(IN) :: dtime
297    INTEGER, INTENT(IN) :: pim
298    INTEGER, intent(out) :: nnhori
299    CHARACTER(len=20), INTENT(IN) :: nname
300    INTEGER, INTENT(OUT) :: nnid_day
301    INTEGER :: i
302    REAL,DIMENSION(klon_glo)        :: rlat_glo
303    REAL,DIMENSION(klon_glo)        :: rlon_glo
304    INTEGER, DIMENSION(pim), INTENT(IN)  :: tabij
305    REAL,DIMENSION(pim), INTENT(IN) :: plat, plon
306    INTEGER,DIMENSION(pim), INTENT(IN) :: ipt, jpt
307    REAL,DIMENSION(pim,2), intent(out) :: plat_bounds, plon_bounds
308
309    INTEGER, SAVE :: tabprocbeg, tabprocend
310!$OMP THREADPRIVATE(tabprocbeg, tabprocend)
311    INTEGER :: ip
312    INTEGER, PARAMETER :: nip=1
313    INTEGER :: npproc
314    REAL, allocatable, DIMENSION(:) :: npplat, npplon
315    REAL, allocatable, DIMENSION(:,:) :: npplat_bounds, npplon_bounds
316    REAL, DIMENSION(nbp_lon,nbp_lat) :: zx_lon, zx_lat
317
318    CALL gather(rlat,rlat_glo)
319    CALL bcast(rlat_glo)
320    CALL gather(rlon,rlon_glo)
321    CALL bcast(rlon_glo)
322
323!$OMP MASTER
324    DO i=1,pim
325
326!    print*,'CFMIP_iophy i tabij lon lat',i,tabij(i),plon(i),plat(i)
327
328     plon_bounds(i,1)=rlon_glo(tabij(i)-1)
329     plon_bounds(i,2)=rlon_glo(tabij(i)+1)
330     IF (plon_bounds(i,2).LE.0..AND.plon_bounds(i,1).GE.0.) THEN
331      IF (rlon_glo(tabij(i)).GE.0.) THEN
332       plon_bounds(i,2)=-1*plon_bounds(i,2)
333      ENDIF
334     ENDIF
335     IF (plon_bounds(i,2).GE.0..AND.plon_bounds(i,1).LE.0.) THEN
336      IF (rlon_glo(tabij(i)).LE.0.) THEN
337       plon_bounds(i,2)=-1*plon_bounds(i,2)
338      ENDIF
339     ENDIF
340!
341     IF ( tabij(i).LE.nbp_lon) THEN
342      plat_bounds(i,1)=rlat_glo(tabij(i))
343     ELSE
344      plat_bounds(i,1)=rlat_glo(tabij(i)-nbp_lon)
345     ENDIF
346     plat_bounds(i,2)=rlat_glo(tabij(i)+nbp_lon)
347!
348!    print*,'CFMIP_iophy point i lon lon_bds',i,plon_bounds(i,1),rlon_glo(tabij(i)),plon_bounds(i,2)
349!    print*,'CFMIP_iophy point i lat lat_bds',i,plat_bounds(i,1),rlat_glo(tabij(i)),plat_bounds(i,2)
350!
351    ENDDO
352    if (is_sequential) then
353
354     npstn=pim
355     IF(.NOT. ALLOCATED(nptabij)) THEN
356      ALLOCATE(nptabij(pim))
357     ENDIF
358     DO i=1,pim
359      nptabij(i)=tabij(i)
360     ENDDO
361
362       CALL gr_fi_ecrit(1,klon,nbp_lon,nbp_lat,rlon_glo,zx_lon)
363       IF ((nbp_lon*nbp_lat).GT.1) THEN
364       DO i = 1, nbp_lon
365         zx_lon(i,1) = rlon_glo(i+1)
366         zx_lon(i,nbp_lat) = rlon_glo(i+1)
367       ENDDO
368       ENDIF
369       CALL gr_fi_ecrit(1,klon,nbp_lon,nbp_lat,rlat_glo,zx_lat)
370
371    DO i=1,pim
372!    print*,'CFMIP_iophy i tabij lon lat',i,tabij(i),plon(i),plat(i)
373
374     plon_bounds(i,1)=zx_lon(ipt(i)-1,jpt(i))
375     plon_bounds(i,2)=zx_lon(ipt(i)+1,jpt(i))
376
377     IF (ipt(i).EQ.1) THEN
378      plon_bounds(i,1)=zx_lon(nbp_lon,jpt(i))
379      plon_bounds(i,2)=360.+zx_lon(ipt(i)+1,jpt(i))
380     ENDIF
381 
382     IF (ipt(i).EQ.nbp_lon) THEN
383      plon_bounds(i,2)=360.+zx_lon(1,jpt(i))
384     ENDIF
385
386     plat_bounds(i,1)=zx_lat(ipt(i),jpt(i)-1)
387     plat_bounds(i,2)=zx_lat(ipt(i),jpt(i)+1)
388
389     IF (jpt(i).EQ.1) THEN
390      plat_bounds(i,1)=zx_lat(ipt(i),1)+0.001
391      plat_bounds(i,2)=zx_lat(ipt(i),1)-0.001
392     ENDIF
393 
394     IF (jpt(i).EQ.nbp_lat) THEN
395      plat_bounds(i,1)=zx_lat(ipt(i),nbp_lat)+0.001
396      plat_bounds(i,2)=zx_lat(ipt(i),nbp_lat)-0.001
397     ENDIF
398!
399!    print*,'CFMIP_iophy point i lon lon_bds',i,plon_bounds(i,1),rlon(tabij(i)),plon_bounds(i,2)
400!    print*,'CFMIP_iophy point i lat lat_bds',i,plat_bounds(i,1),rlat(tabij(i)),plat_bounds(i,2)
401!
402    ENDDO
403
404#ifndef CPP_IOIPSL_NO_OUTPUT
405     call histbeg(nname,pim,plon,plon_bounds, &
406                           plat,plat_bounds, &
407                           itau0, zjulian, dtime, nnhori, nnid_day)
408#endif
409    ELSE
410     npproc=0
411     DO ip=1, pim
412      tabprocbeg=klon_mpi_begin
413      tabprocend=klon_mpi_end
414      IF(tabij(ip).GE.tabprocbeg.AND.tabij(ip).LE.tabprocend) THEN
415       npproc=npproc+1
416       npstn=npproc
417      ENDIF
418     ENDDO
419!    print*,'CFMIP_iophy mpi_rank npstn',mpi_rank,npstn
420     IF(.NOT. ALLOCATED(nptabij)) THEN
421      ALLOCATE(nptabij(npstn))
422      ALLOCATE(npplon(npstn), npplat(npstn))
423      ALLOCATE(npplon_bounds(npstn,2), npplat_bounds(npstn,2))
424     ENDIF
425     npproc=0
426     DO ip=1, pim
427      IF(tabij(ip).GE.tabprocbeg.AND.tabij(ip).LE.tabprocend) THEN
428       npproc=npproc+1
429       nptabij(npproc)=tabij(ip)
430!      print*,'mpi_rank npproc ip plon plat tabij=',mpi_rank,npproc,ip, &
431!      plon(ip),plat(ip),tabij(ip)
432       npplon(npproc)=plon(ip)
433       npplat(npproc)=plat(ip)
434       npplon_bounds(npproc,1)=plon_bounds(ip,1)
435       npplon_bounds(npproc,2)=plon_bounds(ip,2)
436       npplat_bounds(npproc,1)=plat_bounds(ip,1)
437       npplat_bounds(npproc,2)=plat_bounds(ip,2)
438!!!
439!!! print qui sert a reordonner les points stations selon l'ordre CFMIP
440!!! ne pas enlever
441        print*,'iophy_mpi rank ip lon lat',mpi_rank,ip,plon(ip),plat(ip)
442!!!
443      ENDIF
444     ENDDO
445#ifndef CPP_IOIPSL_NO_OUTPUT
446     call histbeg(nname,npstn,npplon,npplon_bounds, &
447                            npplat,npplat_bounds, &
448                            itau0,zjulian,dtime,nnhori,nnid_day,phys_domain_id)
449#endif
450    ENDIF
451!$OMP END MASTER
452
453  END SUBROUTINE histbeg_phy_points
454
455
456  SUBROUTINE histdef2d_old (iff,lpoint,flag_var,nomvar,titrevar,unitvar)
457
458    USE ioipsl, ONLY: histdef
459    USE mod_phys_lmdz_para, ONLY: jj_nb
460    USE phys_output_var_mod, ONLY: type_ecri, zoutm, zdtime_moy, lev_files, &
461                                   nid_files, nhorim, swaero_diag, dryaod_diag, nfiles
462    USE mod_grid_phy_lmdz, ONLY : nbp_lon, nbp_lat
463    USE aero_mod, ONLY : naero_tot, name_aero_tau
464
465    IMPLICIT NONE
466
467    INCLUDE "clesphys.h"
468
469    INTEGER                          :: iff
470    INTEGER                          :: naero
471    LOGICAL                          :: lpoint
472    INTEGER, DIMENSION(nfiles)       :: flag_var
473    CHARACTER(LEN=20)                :: nomvar
474    CHARACTER(LEN=*)                 :: titrevar
475    CHARACTER(LEN=*)                 :: unitvar
476
477    REAL zstophym
478
479    IF (type_ecri(iff)=='inst(X)'.OR.type_ecri(iff)=='once') THEN
480       zstophym=zoutm(iff)
481    ELSE
482       zstophym=zdtime_moy
483    ENDIF
484
485    ! Appel a la lecture des noms et niveau d'ecriture des variables dans output.def
486    CALL conf_physoutputs(nomvar,flag_var)
487
488    IF(.NOT.lpoint) THEN 
489       IF ( flag_var(iff)<=lev_files(iff) ) THEN
490          CALL histdef (nid_files(iff),nomvar,titrevar,unitvar, &
491               nbp_lon,jj_nb,nhorim(iff), 1,1,1, -99, 32, &
492               type_ecri(iff), zstophym,zoutm(iff))               
493       ENDIF
494    ELSE
495       IF ( flag_var(iff)<=lev_files(iff) ) THEN
496          CALL histdef (nid_files(iff),nomvar,titrevar,unitvar, &
497               npstn,1,nhorim(iff), 1,1,1, -99, 32, &
498               type_ecri(iff), zstophym,zoutm(iff))               
499       ENDIF
500    ENDIF
501
502    ! Set swaero_diag=true if at least one of the concerned variables are defined
503    IF (nomvar=='topswad' .OR. nomvar=='topswad0' .OR. nomvar=='solswad' .OR. nomvar=='solswad0' .OR. &
504        nomvar=='toplwad' .OR. nomvar=='toplwad0' .OR. nomvar=='sollwad' .OR. nomvar=='sollwad0' .OR. &
505        nomvar=='topswai' .OR. nomvar=='solswai' ) THEN
506       IF  ( flag_var(iff)<=lev_files(iff) ) swaero_diag=.TRUE.
507    ENDIF
508
509    ! Set dryaod_diag=true if at least one of the concerned variables are defined
510    IF (nomvar=='dryod550aer') THEN
511      IF  ( flag_var(iff)<=lev_files(iff) ) dryaod_diag=.TRUE.
512    ENDIF
513    DO naero = 1, naero_tot-1
514      IF (nomvar=='dryod550_'//name_aero_tau(naero)) THEN
515        IF  ( flag_var(iff)<=lev_files(iff) ) dryaod_diag=.TRUE.
516      ENDIF
517    ENDDO
518
519  END SUBROUTINE histdef2d_old
520
521  SUBROUTINE histdef3d_old (iff,lpoint,flag_var,nomvar,titrevar,unitvar)
522
523    USE ioipsl, ONLY: histdef
524    USE dimphy, ONLY: klev
525    USE mod_phys_lmdz_para, ONLY: jj_nb
526    USE phys_output_var_mod, ONLY: type_ecri, zoutm, lev_files, nid_files, &
527                                   nhorim, zdtime_moy, levmin, levmax, &
528                                   nvertm, nfiles
529    USE mod_grid_phy_lmdz, ONLY : nbp_lon, nbp_lat
530    IMPLICIT NONE
531
532    INCLUDE "clesphys.h"
533
534    INTEGER                          :: iff
535    LOGICAL                          :: lpoint
536    INTEGER, DIMENSION(nfiles)       :: flag_var
537    CHARACTER(LEN=20)                 :: nomvar
538    CHARACTER(LEN=*)                 :: titrevar
539    CHARACTER(LEN=*)                 :: unitvar
540
541    REAL zstophym
542
543    ! Appel a la lecture des noms et niveau d'ecriture des variables dans output.def
544    CALL conf_physoutputs(nomvar,flag_var)
545
546    IF (type_ecri(iff)=='inst(X)'.OR.type_ecri(iff)=='once') THEN
547       zstophym=zoutm(iff)
548    ELSE
549       zstophym=zdtime_moy
550    ENDIF
551
552    IF(.NOT.lpoint) THEN
553       IF ( flag_var(iff)<=lev_files(iff) ) THEN
554          CALL histdef (nid_files(iff), nomvar, titrevar, unitvar, &
555               nbp_lon, jj_nb, nhorim(iff), klev, levmin(iff), &
556               levmax(iff)-levmin(iff)+1, nvertm(iff), 32, type_ecri(iff), &
557               zstophym, zoutm(iff))
558       ENDIF
559    ELSE
560       IF ( flag_var(iff)<=lev_files(iff) ) THEN
561          CALL histdef (nid_files(iff), nomvar, titrevar, unitvar, &
562               npstn,1,nhorim(iff), klev, levmin(iff), &
563               levmax(iff)-levmin(iff)+1, nvertm(iff), 32, &
564               type_ecri(iff), zstophym,zoutm(iff))
565       ENDIF
566    ENDIF
567  END SUBROUTINE histdef3d_old
568
569  SUBROUTINE histdef2d (iff,var)
570
571    USE ioipsl, ONLY: histdef
572    USE mod_phys_lmdz_para, ONLY: jj_nb
573    USE phys_output_var_mod, ONLY: ctrl_out, type_ecri_files, zoutm, zdtime_moy, &
574                                   clef_stations, phys_out_filenames, lev_files, &
575                                   nid_files, nhorim, swaero_diag, dryaod_diag
576    USE print_control_mod, ONLY: prt_level,lunout
577    USE mod_grid_phy_lmdz, ONLY : nbp_lon, nbp_lat
578    USE aero_mod, ONLY : naero_tot, name_aero_tau
579#ifdef CPP_XIOS
580    USE wxios, ONLY: wxios_add_field_to_file
581#endif
582    IMPLICIT NONE
583
584    INCLUDE "clesphys.h"
585
586    INTEGER                          :: iff
587    INTEGER                          :: naero
588    TYPE(ctrl_out)                   :: var
589
590    REAL zstophym
591    CHARACTER(LEN=20) :: typeecrit
592
593    ! ug On récupère le type écrit de la structure:
594    !       Assez moche, à refaire si meilleure méthode...
595    IF (INDEX(var%type_ecrit(iff), "once") > 0) THEN
596       typeecrit = 'once'
597    ELSE IF(INDEX(var%type_ecrit(iff), "t_min") > 0) THEN
598       typeecrit = 't_min(X)'
599    ELSE IF(INDEX(var%type_ecrit(iff), "t_max") > 0) THEN
600       typeecrit = 't_max(X)'
601    ELSE IF(INDEX(var%type_ecrit(iff), "inst") > 0) THEN
602       typeecrit = 'inst(X)'
603    ELSE
604       typeecrit = type_ecri_files(iff)
605    ENDIF
606
607    IF (typeecrit=='inst(X)'.OR.typeecrit=='once') THEN
608       zstophym=zoutm(iff)
609    ELSE
610       zstophym=zdtime_moy
611    ENDIF
612
613    ! Appel a la lecture des noms et niveau d'ecriture des variables dans output.def
614    CALL conf_physoutputs(var%name, var%flag)
615
616    IF(.NOT.clef_stations(iff)) THEN 
617
618#ifdef CPP_XIOS
619      IF (.not. ok_all_xml) THEN
620        IF ( var%flag(iff)<=lev_files(iff) ) THEN
621          CALL wxios_add_field_to_file(var%name, 2, iff, phys_out_filenames(iff), &
622          var%description, var%unit, var%flag(iff), typeecrit)
623          IF (prt_level >= 10) THEN
624            WRITE(lunout,*) 'histdef2d: call wxios_add_field_to_file var%name iff: ', &
625                            trim(var%name),iff
626          ENDIF
627        ENDIF
628      ENDIF
629#endif
630#ifndef CPP_IOIPSL_NO_OUTPUT
631
632       IF ( var%flag(iff)<=lev_files(iff) ) THEN
633          CALL histdef (nid_files(iff), var%name, var%description, var%unit, &
634               nbp_lon,jj_nb,nhorim(iff), 1,1,1, -99, 32, &
635               typeecrit, zstophym,zoutm(iff))               
636       ENDIF
637    ELSE
638       IF ( var%flag(iff)<=lev_files(iff)) THEN
639          CALL histdef (nid_files(iff), var%name, var%description, var%unit, &
640               npstn,1,nhorim(iff), 1,1,1, -99, 32, &
641               typeecrit, zstophym,zoutm(iff))               
642       ENDIF
643#endif
644    ENDIF
645
646    ! Set swaero_diag=true if at least one of the concerned variables are defined
647    !--OB 30/05/2016 use wider set of variables
648    IF ( var%name=='topswad' .OR. var%name=='topswad0' .OR. var%name=='solswad' .OR. var%name=='solswad0' .OR. &
649         var%name=='topswai' .OR. var%name=='solswai'  .OR. ( iflag_rrtm==1 .AND. (                            &
650         var%name=='toplwad' .OR. var%name=='toplwad0' .OR. var%name=='sollwad' .OR. var%name=='sollwad0' .OR. &
651         var%name=='toplwai' .OR. var%name=='sollwai'  ) ) ) THEN
652       IF  ( var%flag(iff)<=lev_files(iff) ) swaero_diag=.TRUE.
653    ENDIF
654
655    ! set dryaod_dry=true if at least one of the concerned variables are defined
656    IF (var%name=='dryod550aer') THEN
657      IF  ( var%flag(iff)<=lev_files(iff) ) dryaod_diag=.TRUE.
658    ENDIF
659    !
660    DO naero = 1, naero_tot-1
661      IF (var%name=='dryod550_'//name_aero_tau(naero)) THEN
662        IF  ( var%flag(iff)<=lev_files(iff) ) dryaod_diag=.TRUE.
663      ENDIF
664    ENDDO
665  END SUBROUTINE histdef2d
666
667  SUBROUTINE histdef3d (iff,var)
668
669    USE ioipsl, ONLY: histdef
670    USE dimphy, ONLY: klev
671    USE mod_phys_lmdz_para, ONLY: jj_nb
672    USE phys_output_var_mod, ONLY: ctrl_out, type_ecri_files, zoutm, zdtime_moy, &
673                                   clef_stations, phys_out_filenames, lev_files, &
674                                   nid_files, nhorim, swaero_diag, dryaod_diag, levmin, &
675                                   levmax, nvertm
676    USE print_control_mod, ONLY: prt_level,lunout
677    USE mod_grid_phy_lmdz, ONLY : nbp_lon, nbp_lat
678#ifdef CPP_XIOS
679    USE wxios, ONLY: wxios_add_field_to_file
680#endif
681    IMPLICIT NONE
682
683    INCLUDE "clesphys.h"
684
685    INTEGER                          :: iff
686    TYPE(ctrl_out)                   :: var
687
688    REAL zstophym
689    CHARACTER(LEN=20) :: typeecrit
690
691    ! ug On récupère le type écrit de la structure:
692    !       Assez moche, à refaire si meilleure méthode...
693    IF (INDEX(var%type_ecrit(iff), "once") > 0) THEN
694       typeecrit = 'once'
695    ELSE IF(INDEX(var%type_ecrit(iff), "t_min") > 0) THEN
696       typeecrit = 't_min(X)'
697    ELSE IF(INDEX(var%type_ecrit(iff), "t_max") > 0) THEN
698       typeecrit = 't_max(X)'
699    ELSE IF(INDEX(var%type_ecrit(iff), "inst") > 0) THEN
700       typeecrit = 'inst(X)'
701    ELSE
702       typeecrit = type_ecri_files(iff)
703    ENDIF
704
705
706    ! Appel a la lecture des noms et niveau d'ecriture des variables dans output.def
707    CALL conf_physoutputs(var%name,var%flag)
708
709    IF (typeecrit=='inst(X)'.OR.typeecrit=='once') THEN
710       zstophym=zoutm(iff)
711    ELSE
712       zstophym=zdtime_moy
713    ENDIF
714
715    IF(.NOT.clef_stations(iff)) THEN
716
717#ifdef CPP_XIOS
718       IF (.not. ok_all_xml) THEN
719         IF ( var%flag(iff)<=lev_files(iff) ) THEN
720         CALL wxios_add_field_to_file(var%name, 3, iff, phys_out_filenames(iff), &
721         var%description, var%unit, var%flag(iff), typeecrit)
722           IF (prt_level >= 10) THEN
723             WRITE(lunout,*) 'histdef3d: call wxios_add_field_to_file var%name iff: ', &
724                             trim(var%name),iff
725           ENDIF
726         ENDIF
727       ENDIF
728#endif
729#ifndef CPP_IOIPSL_NO_OUTPUT
730
731       IF ( var%flag(iff)<=lev_files(iff) ) THEN
732          CALL histdef (nid_files(iff), var%name, var%description, var%unit, &
733               nbp_lon, jj_nb, nhorim(iff), klev, levmin(iff), &
734               levmax(iff)-levmin(iff)+1, nvertm(iff), 32, typeecrit, &
735               zstophym, zoutm(iff))
736       ENDIF
737    ELSE
738       IF ( var%flag(iff)<=lev_files(iff)) THEN
739          CALL histdef (nid_files(iff), var%name, var%description, var%unit, &
740               npstn,1,nhorim(iff), klev, levmin(iff), &
741               levmax(iff)-levmin(iff)+1, nvertm(iff), 32, &
742               typeecrit, zstophym,zoutm(iff))
743       ENDIF
744#endif
745    ENDIF
746  END SUBROUTINE histdef3d
747
748  SUBROUTINE conf_physoutputs(nam_var,flag_var)
749!!! Lecture des noms et niveau de sortie des variables dans output.def
750    !   en utilisant les routines getin de IOIPSL 
751    USE ioipsl, ONLY: getin
752    USE phys_output_var_mod, ONLY: nfiles
753    USE print_control_mod, ONLY: prt_level,lunout
754    IMPLICIT NONE
755
756    CHARACTER(LEN=20)                :: nam_var
757    INTEGER, DIMENSION(nfiles)      :: flag_var
758
759    IF(prt_level>10) WRITE(lunout,*)'Avant getin: nam_var flag_var ',nam_var,flag_var(:)
760    CALL getin('flag_'//nam_var,flag_var)
761    CALL getin('name_'//nam_var,nam_var)
762    IF(prt_level>10) WRITE(lunout,*)'Apres getin: nam_var flag_var ',nam_var,flag_var(:)
763
764  END SUBROUTINE conf_physoutputs
765
766 
767  SUBROUTINE histwrite2d_phy_old(nid,lpoint,name,itau,field)
768  USE dimphy, ONLY: klon
769  USE mod_phys_lmdz_para, ONLY: Gather_omp, grid1Dto2D_mpi, &
770                                is_sequential, klon_mpi_begin, klon_mpi_end, &
771                                jj_nb, klon_mpi
772  USE ioipsl, ONLY: histwrite
773  USE print_control_mod, ONLY: prt_level,lunout
774  USE mod_grid_phy_lmdz, ONLY : nbp_lon, nbp_lat
775  IMPLICIT NONE
776   
777    INTEGER,INTENT(IN) :: nid
778    LOGICAL,INTENT(IN) :: lpoint
779    CHARACTER*(*), INTENT(IN) :: name
780    INTEGER, INTENT(IN) :: itau
781    REAL,DIMENSION(:),INTENT(IN) :: field
782    REAL,DIMENSION(klon_mpi) :: buffer_omp
783    INTEGER, allocatable, DIMENSION(:) :: index2d
784    REAL :: Field2d(nbp_lon,jj_nb)
785
786    INTEGER :: ip
787    REAL,ALLOCATABLE,DIMENSION(:) :: fieldok
788
789    IF (size(field)/=klon) CALL abort_physic('iophy::histwrite2d','Field first DIMENSION not equal to klon',1)
790   
791    CALL Gather_omp(field,buffer_omp)   
792!$OMP MASTER
793    CALL grid1Dto2D_mpi(buffer_omp,Field2d)
794    if(.NOT.lpoint) THEN
795     ALLOCATE(index2d(nbp_lon*jj_nb))
796     ALLOCATE(fieldok(nbp_lon*jj_nb))
797     IF (prt_level >= 10) write(lunout,*)'Sending ',name,' to IOIPSL'
798     CALL histwrite(nid,name,itau,Field2d,nbp_lon*jj_nb,index2d)
799     IF (prt_level >= 10) write(lunout,*)'Finished sending ',name,' to IOIPSL'
800    ELSE
801     ALLOCATE(fieldok(npstn))
802     ALLOCATE(index2d(npstn))
803
804     IF (is_sequential) THEN
805!     klon_mpi_begin=1
806!     klon_mpi_end=klon
807      DO ip=1, npstn
808       fieldok(ip)=buffer_omp(nptabij(ip))
809      ENDDO
810     ELSE
811      DO ip=1, npstn
812!     print*,'histwrite2d is_sequential npstn ip name nptabij',npstn,ip,name,nptabij(ip)
813       IF(nptabij(ip).GE.klon_mpi_begin.AND. &
814          nptabij(ip).LE.klon_mpi_end) THEN
815         fieldok(ip)=buffer_omp(nptabij(ip)-klon_mpi_begin+1)
816       ENDIF
817      ENDDO
818     ENDIF
819     IF (prt_level >= 10) write(lunout,*)'Sending ',name,' to IOIPSL'
820     CALL histwrite(nid,name,itau,fieldok,npstn,index2d)
821     IF (prt_level >= 10) write(lunout,*)'Finished sending ',name,' to IOIPSL'
822!
823    ENDIF
824    DEALLOCATE(index2d)
825    DEALLOCATE(fieldok)
826!$OMP END MASTER   
827
828 
829  END SUBROUTINE histwrite2d_phy_old
830
831  SUBROUTINE histwrite3d_phy_old(nid,lpoint,name,itau,field)
832  USE dimphy, ONLY: klon
833  USE mod_phys_lmdz_para, ONLY: Gather_omp, grid1Dto2D_mpi, &
834                                is_sequential, klon_mpi_begin, klon_mpi_end, &
835                                jj_nb, klon_mpi
836  USE mod_grid_phy_lmdz, ONLY : nbp_lon, nbp_lat
837  USE ioipsl, ONLY: histwrite
838  USE print_control_mod, ONLY: prt_level,lunout
839  IMPLICIT NONE
840   
841    INTEGER,INTENT(IN) :: nid
842    LOGICAL,INTENT(IN) :: lpoint
843    CHARACTER*(*), INTENT(IN) :: name
844    INTEGER, INTENT(IN) :: itau
845    REAL,DIMENSION(:,:),INTENT(IN) :: field  ! --> field(klon,:)
846    REAL,DIMENSION(klon_mpi,size(field,2)) :: buffer_omp
847    REAL :: Field3d(nbp_lon,jj_nb,size(field,2))
848    INTEGER :: ip, n, nlev
849    INTEGER, ALLOCATABLE, DIMENSION(:) :: index3d
850    REAL,allocatable, DIMENSION(:,:) :: fieldok
851
852
853    IF (size(field,1)/=klon) CALL abort_physic('iophy::histwrite3d','Field first DIMENSION not equal to klon',1)
854    nlev=size(field,2)
855
856    CALL Gather_omp(field,buffer_omp)
857!$OMP MASTER
858    CALL grid1Dto2D_mpi(buffer_omp,field3d)
859    IF (.NOT.lpoint) THEN
860     ALLOCATE(index3d(nbp_lon*jj_nb*nlev))
861     ALLOCATE(fieldok(nbp_lon*jj_nb,nlev))
862     IF (prt_level >= 10) write(lunout,*)'Sending ',name,' to IOIPSL'
863     CALL histwrite(nid,name,itau,Field3d,nbp_lon*jj_nb*nlev,index3d)
864     IF (prt_level >= 10) write(lunout,*)'Finished sending ',name,' to IOIPSL'
865   ELSE
866      nlev=size(field,2)
867      ALLOCATE(index3d(npstn*nlev))
868      ALLOCATE(fieldok(npstn,nlev))
869
870      IF (is_sequential) THEN
871!      klon_mpi_begin=1
872!      klon_mpi_end=klon
873       DO n=1, nlev
874       DO ip=1, npstn
875        fieldok(ip,n)=buffer_omp(nptabij(ip),n)
876       ENDDO
877       ENDDO
878      ELSE
879       DO n=1, nlev
880       DO ip=1, npstn
881        IF(nptabij(ip).GE.klon_mpi_begin.AND. &
882         nptabij(ip).LE.klon_mpi_end) THEN
883         fieldok(ip,n)=buffer_omp(nptabij(ip)-klon_mpi_begin+1,n)
884        ENDIF
885       ENDDO
886       ENDDO
887      ENDIF
888      IF (prt_level >= 10) write(lunout,*)'Sending ',name,' to IOIPSL'
889      CALL histwrite(nid,name,itau,fieldok,npstn*nlev,index3d)
890      IF (prt_level >= 10) write(lunout,*)'Finished sending ',name,' to IOIPSL'
891    ENDIF
892  DEALLOCATE(index3d)
893  DEALLOCATE(fieldok)
894!$OMP END MASTER   
895
896  END SUBROUTINE histwrite3d_phy_old
897
898
899
900
901! ug NOUVELLE VERSION DES WRITE AVEC LA BOUCLE DO RENTREE
902  SUBROUTINE histwrite2d_phy(var,field, STD_iff)
903  USE dimphy, ONLY: klon
904  USE mod_phys_lmdz_para, ONLY: gather_omp, grid1dto2d_mpi, &
905                                jj_nb, klon_mpi, klon_mpi_begin, &
906                                klon_mpi_end, is_sequential
907  USE ioipsl, ONLY: histwrite
908  USE phys_output_var_mod, ONLY: ctrl_out, clef_files, lev_files, &
909                                 nfiles, vars_defined, clef_stations, &
910                                 nid_files
911  USE print_control_mod, ONLY: prt_level,lunout
912  USE mod_grid_phy_lmdz, ONLY : nbp_lon, nbp_lat
913#ifdef CPP_XIOS
914  USE xios, ONLY: xios_send_field
915#endif
916
917
918  IMPLICIT NONE
919  include 'clesphys.h'
920
921    TYPE(ctrl_out), INTENT(IN) :: var
922    REAL, DIMENSION(:), INTENT(IN) :: field
923    INTEGER, INTENT(IN), OPTIONAL :: STD_iff ! ug RUSTINE POUR LES STD LEVS.....
924     
925    INTEGER :: iff, iff_beg, iff_end
926    LOGICAL, SAVE  :: firstx
927!$OMP THREADPRIVATE(firstx)
928
929    REAL,DIMENSION(klon_mpi) :: buffer_omp
930    INTEGER, allocatable, DIMENSION(:) :: index2d
931    REAL :: Field2d(nbp_lon,jj_nb)
932
933    INTEGER :: ip
934    REAL, ALLOCATABLE, DIMENSION(:) :: fieldok
935
936    IF (prt_level >= 10) THEN
937      WRITE(lunout,*)'Begin histwrite2d_phy for ',trim(var%name)
938    ENDIF
939! ug RUSTINE POUR LES STD LEVS.....
940      IF (PRESENT(STD_iff)) THEN
941            iff_beg = STD_iff
942            iff_end = STD_iff
943      ELSE
944            iff_beg = 1
945            iff_end = nfiles
946      ENDIF
947
948  ! On regarde si on est dans la phase de définition ou d'écriture:
949  IF (.NOT.vars_defined) THEN
950!$OMP MASTER
951      !Si phase de définition.... on définit
952      IF (.not. ok_all_xml) THEN
953      IF (prt_level >= 10) THEN
954      WRITE (lunout,*)"histwrite2d_phy: .not.vars_defined ; time to define ", trim(var%name)
955      ENDIF
956      DO iff=iff_beg, iff_end
957         IF (clef_files(iff)) THEN
958            CALL histdef2d(iff, var)
959         ENDIF
960      ENDDO
961      ENDIF
962!$OMP END MASTER
963  ELSE
964
965    !Et sinon on.... écrit
966    IF (SIZE(field)/=klon) CALL abort_physic('iophy::histwrite2d_phy','Field first DIMENSION not equal to klon',1)
967   
968    IF (prt_level >= 10) THEn
969      WRITE (lunout,*)"histwrite2d_phy: .not.vars_defined ; time to gather and write ", trim(var%name)
970    ENDIF
971   
972    CALL Gather_omp(field,buffer_omp)
973!$OMP MASTER
974    CALL grid1Dto2D_mpi(buffer_omp,Field2d)
975
976! La boucle sur les fichiers:
977      firstx=.true.
978
979      IF (ok_all_xml) THEN
980#ifdef CPP_XIOS
981          IF (prt_level >= 10) THEN
982             write(lunout,*)'Dans iophy histwrite2D,var%name ', trim(var%name)                       
983          ENDIF
984          CALL xios_send_field(var%name, Field2d)
985          IF (prt_level >= 10) THEN
986             WRITE (lunout,*)'Dans iophy histwrite2D,var%name apres xios_send ', trim(var%name)                       
987          ENDIF
988#else
989        CALL abort_physic ('iophy','cannot have ok_all_xml = .T. without CPP_XIOS defined' ,1)
990#endif
991      ELSE 
992        DO iff=iff_beg, iff_end
993            IF (var%flag(iff) <= lev_files(iff) .AND. clef_files(iff)) THEN
994
995#ifdef CPP_XIOS
996               IF (firstx) THEN
997                  IF (prt_level >= 10) THEN
998                     WRITE (lunout,*)'Dans iophy histwrite2D,iff,var%name ', iff,trim(var%name)                       
999                     WRITE (lunout,*)"histwrite2d_phy:.NOT.clef_stations(iff)and iff==iff_beg, call xios_send_field"
1000                  ENDIF
1001                  CALL xios_send_field(var%name, Field2d)
1002                  firstx=.false.
1003               ENDIF
1004#endif
1005
1006                  IF (.NOT.clef_stations(iff)) THEN
1007                        ALLOCATE(index2d(nbp_lon*jj_nb))
1008                        ALLOCATE(fieldok(nbp_lon*jj_nb))
1009#ifndef CPP_IOIPSL_NO_OUTPUT
1010                        CALL histwrite(nid_files(iff),var%name,itau_iophy,Field2d,nbp_lon*jj_nb,index2d)
1011#endif
1012!#ifdef CPP_XIOS
1013!                        IF (iff == iff_beg) THEN
1014!                          if (prt_level >= 10) then
1015!                            write(lunout,*)"histwrite2d_phy: .NOT.clef_stations(iff) and iff==iff_beg, call xios_send_field"
1016!                          endif
1017!                          CALL xios_send_field(var%name, Field2d)
1018!                        ENDIF
1019!#endif
1020                  ELSE
1021                        ALLOCATE(fieldok(npstn))
1022                        ALLOCATE(index2d(npstn))
1023
1024                        IF (is_sequential) THEN
1025                          DO ip=1, npstn
1026                            fieldok(ip)=buffer_omp(nptabij(ip))
1027                          ENDDO
1028                        ELSE
1029                              DO ip=1, npstn
1030                                write(lunout,*)'histwrite2d_phy is_sequential npstn ip namenptabij',npstn,ip,var%name,nptabij(ip)
1031                                     IF(nptabij(ip).GE.klon_mpi_begin.AND. &
1032                                        nptabij(ip).LE.klon_mpi_end) THEN
1033                                       fieldok(ip)=buffer_omp(nptabij(ip)-klon_mpi_begin+1)
1034                                     ENDIF
1035                              ENDDO
1036                       ENDIF ! of IF (is_sequential)
1037#ifndef CPP_IOIPSL_NO_OUTPUT
1038                       IF (prt_level >= 10) THEn
1039                         write(lunout,*)"histwrite2d_phy: clef_stations(iff) and iff==iff_beg, call wxios_write_2D"
1040                       ENDIF
1041                       CALL histwrite(nid_files(iff),var%name,itau_iophy,fieldok,npstn,index2d)
1042#endif
1043                  ENDIF ! of IF(.NOT.clef_stations(iff))
1044                 
1045                DEALLOCATE(index2d)
1046                DEALLOCATE(fieldok)
1047            ENDIF !levfiles
1048        ENDDO ! of DO iff=iff_beg, iff_end
1049      ENDIF
1050!$OMP END MASTER   
1051  ENDIF ! vars_defined
1052  IF (prt_level >= 10) WRITE(lunout,*)'End histwrite2d_phy ',trim(var%name)
1053  END SUBROUTINE histwrite2d_phy
1054
1055
1056! ug NOUVELLE VERSION DES WRITE AVEC LA BOUCLE DO RENTREE
1057  SUBROUTINE histwrite3d_phy(var, field, STD_iff)
1058  USE dimphy, ONLY: klon, klev
1059  USE mod_phys_lmdz_para, ONLY: gather_omp, grid1dto2d_mpi, &
1060                                jj_nb, klon_mpi, klon_mpi_begin, &
1061                                klon_mpi_end, is_sequential
1062  USE ioipsl, ONLY: histwrite
1063  USE phys_output_var_mod, ONLY: ctrl_out, clef_files, lev_files, &
1064                                 nfiles, vars_defined, clef_stations, &
1065                                 nid_files
1066  USE mod_grid_phy_lmdz, ONLY : nbp_lon, nbp_lat
1067#ifdef CPP_XIOS
1068  USE xios, ONLY: xios_send_field
1069#endif
1070  USE print_control_mod, ONLY: prt_level,lunout
1071
1072  IMPLICIT NONE
1073  include 'clesphys.h'
1074
1075    TYPE(ctrl_out), INTENT(IN) :: var
1076    REAL, DIMENSION(:,:), INTENT(IN) :: field ! --> field(klon,:)
1077    INTEGER, INTENT(IN), OPTIONAL :: STD_iff ! ug RUSTINE POUR LES STD LEVS.....
1078     
1079    INTEGER :: iff, iff_beg, iff_end
1080    LOGICAL, SAVE  :: firstx
1081!$OMP THREADPRIVATE(firstx)
1082    REAL,DIMENSION(klon_mpi,SIZE(field,2)) :: buffer_omp
1083    REAL :: Field3d(nbp_lon,jj_nb,SIZE(field,2))
1084    INTEGER :: ip, n, nlev, nlevx
1085    INTEGER, ALLOCATABLE, DIMENSION(:) :: index3d
1086    REAL,ALLOCATABLE, DIMENSION(:,:) :: fieldok
1087
1088  IF (prt_level >= 10) write(lunout,*)'Begin histrwrite3d ',var%name
1089
1090! ug RUSTINE POUR LES STD LEVS.....
1091      IF (PRESENT(STD_iff)) THEN
1092            iff_beg = STD_iff
1093            iff_end = STD_iff
1094      ELSE
1095            iff_beg = 1
1096            iff_end = nfiles
1097      ENDIF
1098
1099  ! On regarde si on est dans la phase de définition ou d'écriture:
1100  IF(.NOT.vars_defined) THEN
1101      !Si phase de définition.... on définit
1102!$OMP MASTER
1103      DO iff=iff_beg, iff_end
1104        IF (clef_files(iff)) THEN
1105          CALL histdef3d(iff, var)
1106        ENDIF
1107      ENDDO
1108!$OMP END MASTER
1109  ELSE
1110    !Et sinon on.... écrit
1111    IF (SIZE(field,1)/=klon) CALL abort_physic('iophy::histwrite3d','Field first DIMENSION not equal to klon',1)
1112    nlev=SIZE(field,2)
1113    IF (nlev.EQ.klev+1) THEN
1114        nlevx=klev
1115    ELSE
1116        nlevx=nlev
1117    ENDIF
1118
1119    CALL Gather_omp(field,buffer_omp)
1120!$OMP MASTER
1121    CALL grid1Dto2D_mpi(buffer_omp,field3d)
1122
1123
1124! BOUCLE SUR LES FICHIERS
1125     firstx=.true.
1126
1127      IF (ok_all_xml) THEN
1128#ifdef CPP_XIOS
1129          IF (prt_level >= 10) THEN
1130             write(lunout,*)'Dans iophy histwrite3D,var%name ',&
1131                             trim(var%name)                       
1132          ENDIF
1133          CALL xios_send_field(var%name, Field3d(:,:,1:nlevx))
1134#else
1135        CALL abort_physic ('iophy','cannot have ok_all_xml = .T. without CPP_XIOS defined' ,1)
1136#endif
1137      ELSE 
1138
1139
1140     DO iff=iff_beg, iff_end
1141            IF (var%flag(iff) <= lev_files(iff) .AND. clef_files(iff)) THEN
1142#ifdef CPP_XIOS
1143              IF (firstx) THEN
1144                IF (prt_level >= 10) THEn
1145                  WRITE (lunout,*)'Dans iophy, histwrite3D iff nlev klev firstx', &
1146                                  iff,nlev,klev, firstx                       
1147                  WRITE (lunout,*)'histwrite3d_phy: call xios_send_field for ', &
1148                                  trim(var%name), ' with iim jjm nlevx = ', &
1149                                  nbp_lon,jj_nb,nlevx
1150                ENDIF
1151                CALL xios_send_field(var%name, Field3d(:,:,1:nlevx))
1152                            firstx=.false.
1153              ENDIF
1154#endif
1155                IF (.NOT.clef_stations(iff)) THEN
1156                        ALLOCATE(index3d(nbp_lon*jj_nb*nlev))
1157                        ALLOCATE(fieldok(nbp_lon*jj_nb,nlev))
1158
1159#ifndef CPP_IOIPSL_NO_OUTPUT
1160                        CALL histwrite(nid_files(iff),var%name,itau_iophy,Field3d,nbp_lon*jj_nb*nlev,index3d)
1161#endif
1162
1163!#ifdef CPP_XIOS
1164!                        IF (iff == 1) THEN
1165!                              CALL xios_send_field(var%name, Field3d(:,:,1:klev))
1166!                        ENDIF
1167!#endif
1168!                       
1169                ELSE
1170                        nlev=size(field,2)
1171                        ALLOCATE(index3d(npstn*nlev))
1172                        ALLOCATE(fieldok(npstn,nlev))
1173
1174                        IF (is_sequential) THEN
1175                              DO n=1, nlev
1176                                    DO ip=1, npstn
1177                                          fieldok(ip,n)=buffer_omp(nptabij(ip),n)
1178                                    ENDDO
1179                              ENDDO
1180                        ELSE
1181                              DO n=1, nlev
1182                                    DO ip=1, npstn
1183                                                IF(nptabij(ip).GE.klon_mpi_begin.AND. &
1184                                                      nptabij(ip).LE.klon_mpi_end) THEN
1185                                                fieldok(ip,n)=buffer_omp(nptabij(ip)-klon_mpi_begin+1,n)
1186                                          ENDIF
1187                                    ENDDO
1188                              ENDDO
1189                        ENDIF
1190#ifndef CPP_IOIPSL_NO_OUTPUT
1191                        CALL histwrite(nid_files(iff),var%name,itau_iophy,fieldok,npstn*nlev,index3d)
1192#endif
1193                  ENDIF
1194                  DEALLOCATE(index3d)
1195                  DEALLOCATE(fieldok)
1196            ENDIF
1197      ENDDO
1198      ENDIF
1199!$OMP END MASTER   
1200  ENDIF ! vars_defined
1201  IF (prt_level >= 10) write(lunout,*)'End histrwrite3d ',var%name
1202  END SUBROUTINE histwrite3d_phy
1203 
1204
1205! VERSION DES HISTWRITE DEDIEES AU TOUT-XIOS-XML DEJA UTILISEE DANS PHYDEV
1206#ifdef CPP_XIOS
1207  SUBROUTINE histwrite2d_xios(field_name,field)
1208  USE dimphy, ONLY: klon
1209  USE mod_phys_lmdz_para, ONLY: gather_omp, grid1Dto2D_mpi, &
1210                                is_sequential, klon_mpi_begin, klon_mpi_end, &
1211                                jj_nb, klon_mpi
1212  USE mod_grid_phy_lmdz, ONLY : nbp_lon, nbp_lat
1213  USE xios, ONLY: xios_send_field
1214  USE print_control_mod, ONLY: prt_level,lunout
1215
1216  IMPLICIT NONE
1217
1218    CHARACTER(LEN=*), INTENT(IN) :: field_name
1219    REAL, DIMENSION(:), INTENT(IN) :: field
1220     
1221    REAL,DIMENSION(klon_mpi) :: buffer_omp
1222    INTEGER, allocatable, DIMENSION(:) :: index2d
1223    REAL :: Field2d(nbp_lon,jj_nb)
1224
1225    INTEGER :: ip
1226    REAL, ALLOCATABLE, DIMENSION(:) :: fieldok
1227
1228    IF (prt_level >= 10) WRITE(lunout,*)'Begin histrwrite2d_xios ',field_name
1229
1230    !Et sinon on.... écrit
1231    IF (SIZE(field)/=klon) CALL abort_physic('iophy::histwrite2d_xios','Field first DIMENSION not equal to klon',1)
1232   
1233    CALL Gather_omp(field,buffer_omp)   
1234!$OMP MASTER
1235    CALL grid1Dto2D_mpi(buffer_omp,Field2d)
1236   
1237!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1238!ATTENTION, STATIONS PAS GEREES !
1239!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1240    !IF(.NOT.clef_stations(iff)) THEN
1241    IF (.TRUE.) THEN
1242        ALLOCATE(index2d(nbp_lon*jj_nb))
1243        ALLOCATE(fieldok(nbp_lon*jj_nb))
1244
1245
1246        CALL xios_send_field(field_name, Field2d)
1247
1248    ELSE
1249        ALLOCATE(fieldok(npstn))
1250        ALLOCATE(index2d(npstn))
1251
1252        IF (is_sequential) THEN
1253            DO ip=1, npstn
1254                fieldok(ip)=buffer_omp(nptabij(ip))
1255            ENDDO
1256        ELSE
1257            DO ip=1, npstn
1258                PRINT*,'histwrite2d_xios is_sequential npstn ip namenptabij',npstn,ip,field_name,nptabij(ip)
1259                IF(nptabij(ip).GE.klon_mpi_begin.AND. &
1260                nptabij(ip).LE.klon_mpi_end) THEN
1261                    fieldok(ip)=buffer_omp(nptabij(ip)-klon_mpi_begin+1)
1262                ENDIF
1263            ENDDO
1264        ENDIF
1265
1266    ENDIF
1267                 
1268    DEALLOCATE(index2d)
1269    DEALLOCATE(fieldok)
1270!$OMP END MASTER   
1271
1272  IF (prt_level >= 10) WRITE(lunout,*)'End histrwrite2d_xios ',field_name
1273  END SUBROUTINE histwrite2d_xios
1274
1275
1276! ug NOUVELLE VERSION DES WRITE AVEC LA BOUCLE DO RENTREE
1277  SUBROUTINE histwrite3d_xios(field_name, field)
1278  USE dimphy, ONLY: klon, klev
1279  USE mod_phys_lmdz_para, ONLY: gather_omp, grid1Dto2D_mpi, &
1280                                is_sequential, klon_mpi_begin, klon_mpi_end, &
1281                                jj_nb, klon_mpi
1282  USE xios, ONLY: xios_send_field
1283  USE mod_grid_phy_lmdz, ONLY : nbp_lon, nbp_lat
1284  USE print_control_mod, ONLY: prt_level,lunout
1285
1286  IMPLICIT NONE
1287
1288    CHARACTER(LEN=*), INTENT(IN) :: field_name
1289    REAL, DIMENSION(:,:), INTENT(IN) :: field ! --> field(klon,:)
1290
1291    REAL,DIMENSION(klon_mpi,SIZE(field,2)) :: buffer_omp
1292    REAL :: Field3d(nbp_lon,jj_nb,SIZE(field,2))
1293    INTEGER :: ip, n, nlev
1294    INTEGER, ALLOCATABLE, DIMENSION(:) :: index3d
1295    REAL,ALLOCATABLE, DIMENSION(:,:) :: fieldok
1296
1297  IF (prt_level >= 10) write(lunout,*)'Begin histrwrite3d_xios ',field_name
1298
1299    !Et on.... écrit
1300    IF (SIZE(field,1)/=klon) CALL abort_physic('iophy::histwrite3d','Field first DIMENSION not equal to klon',1)
1301    nlev=SIZE(field,2)
1302
1303
1304    CALL Gather_omp(field,buffer_omp)
1305!$OMP MASTER
1306    CALL grid1Dto2D_mpi(buffer_omp,field3d)
1307
1308!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1309!ATTENTION, STATIONS PAS GEREES !
1310!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1311    !IF (.NOT.clef_stations(iff)) THEN
1312    IF(.TRUE.)THEN
1313        ALLOCATE(index3d(nbp_lon*jj_nb*nlev))
1314        ALLOCATE(fieldok(nbp_lon*jj_nb,nlev))
1315        CALL xios_send_field(field_name, Field3d(:,:,1:nlev))
1316                       
1317    ELSE
1318        nlev=size(field,2)
1319        ALLOCATE(index3d(npstn*nlev))
1320        ALLOCATE(fieldok(npstn,nlev))
1321
1322        IF (is_sequential) THEN
1323            DO n=1, nlev
1324                DO ip=1, npstn
1325                    fieldok(ip,n)=buffer_omp(nptabij(ip),n)
1326                ENDDO
1327            ENDDO
1328        ELSE
1329            DO n=1, nlev
1330                DO ip=1, npstn
1331                    IF(nptabij(ip).GE.klon_mpi_begin.AND. &
1332                    nptabij(ip).LE.klon_mpi_end) THEN
1333                        fieldok(ip,n)=buffer_omp(nptabij(ip)-klon_mpi_begin+1,n)
1334                    ENDIF
1335                ENDDO
1336            ENDDO
1337        ENDIF
1338    ENDIF
1339    DEALLOCATE(index3d)
1340    DEALLOCATE(fieldok)
1341!$OMP END MASTER   
1342
1343  IF (prt_level >= 10) write(lunout,*)'End histrwrite3d_xios ',field_name
1344  END SUBROUTINE histwrite3d_xios
1345
1346#ifdef CPP_XIOS
1347  SUBROUTINE histwrite0d_xios(field_name, field)
1348  USE xios, ONLY: xios_send_field
1349  IMPLICIT NONE
1350
1351    CHARACTER(LEN=*), INTENT(IN) :: field_name
1352    REAL, INTENT(IN) :: field ! --> scalar
1353
1354!$OMP MASTER
1355   CALL xios_send_field(field_name, field)
1356!$OMP END MASTER
1357
1358  END SUBROUTINE histwrite0d_xios
1359#endif
1360
1361#endif
1362end module iophy
Note: See TracBrowser for help on using the repository browser.