source: trunk/LMDZ.MARS/libf/phymars/wstats.F90 @ 1769

Last change on this file since 1769 was 1689, checked in by emillour, 8 years ago

Mars GCM:
Fixing a big bug (dating from revision 1528) in wstats.
EM

File size: 14.1 KB
RevLine 
[38]1subroutine wstats(ngrid,nom,titre,unite,dim,px)
2
[1532]3use statto_mod, only: istats,istime,count
[1130]4use mod_phys_lmdz_para, only : is_mpi_root, is_master, gather, klon_mpi_begin
[1528]5use mod_grid_phy_lmdz, only : klon_glo, Grid1Dto2D_glo, &
6                              nbp_lon, nbp_lat, nbp_lev
[38]7implicit none
8
[1528]9include "netcdf.inc"
[38]10
11integer,intent(in) :: ngrid
12character (len=*),intent(in) :: nom,titre,unite
13integer,intent(in) :: dim
[1528]14real,intent(in) :: px(ngrid,nbp_lev)
[1532]15real,allocatable,save :: mean3d(:,:,:),sd3d(:,:,:),dx3(:,:,:)
16real,allocatable,save :: mean2d(:,:),sd2d(:,:),dx2(:,:)
[38]17character (len=50) :: namebis
18character (len=50), save :: firstvar
[1532]19!$OMP THREADPRIVATE(firstvar)
[1130]20integer :: ierr,varid,nbdim,nid
[38]21integer :: meanid,sdid
[1130]22integer, dimension(4)  :: id,start,sizes
[38]23logical, save :: firstcall=.TRUE.
24integer :: l,i,j,ig0
[1130]25integer,save :: indx
[38]26
27integer, save :: step=0
[1532]28!$OMP THREADPRIVATE(firstcall,indx,step)
[38]29
[1130]30! Added to work in parallel mode
31#ifdef CPP_PARA
[1528]32real px3_glop(klon_glo,nbp_lev) ! to store a 3D data set on global physics grid
33real px3_glo(nbp_lon,nbp_lat,nbp_lev) ! to store a global 3D data set on global lonxlat grid
[1130]34real px2_glop(klon_glo) ! to store a 2D data set on global physics grid
[1528]35real px2_glo(nbp_lon,nbp_lat) ! to store a 2D data set on global lonxlat grid
[1130]36real px2(ngrid)
[1528]37real px3(ngrid,nbp_lev)
[1130]38#else
39! When not running in parallel mode:
[1528]40real px3_glop(ngrid,nbp_lev) ! to store a 3D data set on global physics grid
41real px3_glo(nbp_lon,nbp_lat,nbp_lev) ! to store a global 3D data set on global lonxlat grid
[1130]42real px2_glop(ngrid) ! to store a 2D data set on global physics grid
[1528]43real px2_glo(nbp_lon,nbp_lat) ! to store a 2D data set on global lonxlat grid
[1130]44#endif
45
46! 1. Initialization (creation of stats.nc file)
[38]47if (firstcall) then
48   firstcall=.false.
[1130]49   firstvar=trim((nom))
[38]50   call inistats(ierr)
[1532]51   if (klon_glo>1) then ! general case, 3D GCM
52     allocate(mean3d(nbp_lon+1,nbp_lat,nbp_lev))
53     allocate(sd3d(nbp_lon+1,nbp_lat,nbp_lev))
54     allocate(dx3(nbp_lon+1,nbp_lat,nbp_lev))
55     allocate(mean2d(nbp_lon+1,nbp_lat))
56     allocate(sd2d(nbp_lon+1,nbp_lat))
57     allocate(dx2(nbp_lon+1,nbp_lat))
58   else ! 1D model
59     allocate(mean3d(1,1,nbp_lev))
60     allocate(sd3d(1,1,nbp_lev))
61     allocate(dx3(1,1,nbp_lev))
62     allocate(mean2d(1,1))
63     allocate(sd2d(1,1))
64     allocate(dx2(1,1))
65   endif
[38]66endif
67
[1130]68if (firstvar==nom) then ! If we're back to the first variable, increment time counter
[38]69      step = step + 1
70endif
71
72if (mod(step,istats).ne.0) then
[1130]73  ! if its not time to write to file, exit
[38]74   RETURN
75endif
76
[1130]77! collect fields on a global physics grid
78#ifdef CPP_PARA
79 if (dim.eq.3) then
[1528]80  px3(1:ngrid,1:nbp_lev)=px(1:ngrid,1:nbp_lev)
[1130]81  ! Gather fieds on a "global" (without redundant longitude) array
82  call Gather(px3,px3_glop)
83!$OMP MASTER
84  if (is_mpi_root) then
85    call Grid1Dto2D_glo(px3_glop,px3_glo)
86    ! copy dx3_glo() to dx3(:) and add redundant longitude
[1528]87    dx3(1:nbp_lon,:,:)=px3_glo(1:nbp_lon,:,:)
88    dx3(nbp_lon+1,:,:)=dx3(1,:,:)
[1130]89  endif
90!$OMP END MASTER
91!$OMP BARRIER
92 else ! dim.eq.2
93  ! Gather fieds on a "global" (without redundant longitude) array
94  px2(:)=px(:,1)
95  call Gather(px2,px2_glop)
96!$OMP MASTER
97          if (is_mpi_root) then
98            call Grid1Dto2D_glo(px2_glop,px2_glo)
99            ! copy px2_glo() to dx2(:) and add redundant longitude
[1528]100            dx2(1:nbp_lon,:)=px2_glo(1:nbp_lon,:)
101            dx2(nbp_lon+1,:)=dx2(1,:)
[1130]102          endif
103!$OMP END MASTER
104!$OMP BARRIER
105 endif
106#else
107  if (dim.eq.3) then
[1528]108    px3_glop(:,1:nbp_lev)=px(:,1:nbp_lev)
[1130]109!  Passage variable physique -->  variable dynamique
[1528]110    DO l=1,nbp_lev
111      DO i=1,nbp_lon
[1130]112         px3_glo(i,1,l)=px(1,l)
[1528]113         px3_glo(i,nbp_lat,l)=px(ngrid,l)
[1130]114      ENDDO
[1528]115      DO j=2,nbp_lat-1
116         ig0= 1+(j-2)*nbp_lon
117         DO i=1,nbp_lon
[1130]118            px3_glo(i,j,l)=px(ig0+i,l)
119         ENDDO
120      ENDDO
121    ENDDO
122  else ! dim.eq.2
123    px2_glop(:)=px(:,1)
124!    Passage variable physique -->  physique dynamique
[1528]125   DO i=1,nbp_lon
[1130]126     px2_glo(i,1)=px(1,1)
[1528]127     px2_glo(i,nbp_lat)=px(ngrid,1)
[1130]128   ENDDO
[1528]129   DO j=2,nbp_lat-1
130     ig0= 1+(j-2)*nbp_lon
131     DO i=1,nbp_lon
[1130]132        px2_glo(i,j)=px(ig0+i,1)
133     ENDDO
134   ENDDO
135  endif
136#endif
137
138! 2. Write field to file
139
140if (is_master) then
141! only master needs do this
142
[38]143ierr = NF_OPEN("stats.nc",NF_WRITE,nid)
144
145namebis=trim(nom)
[1130]146
147! test: check if that variable already exists in file
[38]148ierr= NF_INQ_VARID(nid,namebis,meanid)
149
150if (ierr.ne.NF_NOERR) then
[1130]151  ! variable not in file, create/define it
[38]152   if (firstvar==nom) then
[1130]153      indx=1
154      count(:)=0
[38]155   endif
156
157
158!declaration de la variable
159
160! choix du nom des coordonnees
161   ierr= NF_INQ_DIMID(nid,"longitude",id(1))
162   ierr= NF_INQ_DIMID(nid,"latitude",id(2))
163   if (dim.eq.3) then
164      ierr= NF_INQ_DIMID(nid,"altitude",id(3))
165      ierr= NF_INQ_DIMID(nid,"Time",id(4))
166      nbdim=4
167   else if (dim==2) then
168      ierr= NF_INQ_DIMID(nid,"Time",id(3))
169      nbdim=3
170   endif
171
172   write (*,*) "====================="
173   write (*,*) "STATS: creation de ",nom
174   namebis=trim(nom)
[1560]175   call def_var_stats(nid,namebis,titre,unite,nbdim,id,meanid,ierr)
[1130]176   if (dim.eq.3) then
177     call inivar(nid,meanid,size(px3_glop,1),dim,indx,px3_glop,ierr)
178   else ! dim.eq.2
179     call inivar(nid,meanid,size(px2_glop,1),dim,indx,px2_glop,ierr)
180   endif
[38]181   namebis=trim(nom)//"_sd"
[1560]182   call def_var_stats(nid,namebis,trim(titre)//" total standard deviation over the season",unite,nbdim,id,sdid,ierr)
[1130]183   if (dim.eq.3) then
184     call inivar(nid,sdid,size(px3_glop,1),dim,indx,px3_glop,ierr)
185   else ! dim.eq.2
186     call inivar(nid,sdid,size(px2_glop,1),dim,indx,px2_glop,ierr)
187   endif
[38]188
189   ierr= NF_CLOSE(nid)
190   return
191
192else
[1130]193   ! variable found in file
[38]194   namebis=trim(nom)//"_sd"
195   ierr= NF_INQ_VARID(nid,namebis,sdid)
196
197endif
198
199if (firstvar==nom) then
[1130]200   count(indx)=count(int(indx))+1
201   indx=indx+1
202   if (indx>istime) then
203      indx=1
[38]204   endif
205endif
206
[1130]207if (count(indx)==0) then
208   ! very first time we write the variable to file
[38]209   if (dim.eq.3) then
[1130]210      start=(/1,1,1,indx/)
[1532]211      if (klon_glo>1) then !general case
212        sizes=(/nbp_lon+1,nbp_lat,nbp_lev,1/)
213      else
214        sizes=(/1,1,nbp_lev,1/)
215      endif
[1130]216      mean3d(:,:,:)=0
217      sd3d(:,:,:)=0
[38]218   else if (dim.eq.2) then
[1130]219      start=(/1,1,indx,0/)
[1532]220      if (klon_glo>1) then !general case
[1689]221        sizes=(/nbp_lon+1,nbp_lat,1,0/)
[1532]222      else
223        sizes=(/1,1,1,0/)
224      endif
[1130]225      mean2d(:,:)=0
226      sd2d(:,:)=0
[38]227   endif
228else
[1130]229   ! load values from file
[38]230   if (dim.eq.3) then
[1130]231      start=(/1,1,1,indx/)
[1532]232      if (klon_glo>1) then !general case
233        sizes=(/nbp_lon+1,nbp_lat,nbp_lev,1/)
234      else ! 1D model case
235        sizes=(/1,1,nbp_lev,1/)
236      endif
[38]237#ifdef NC_DOUBLE
[1130]238      ierr = NF_GET_VARA_DOUBLE(nid,meanid,start,sizes,mean3d)
239      ierr = NF_GET_VARA_DOUBLE(nid,sdid,start,sizes,sd3d)
[38]240#else
[1130]241      ierr = NF_GET_VARA_REAL(nid,meanid,start,sizes,mean3d)
242      ierr = NF_GET_VARA_REAL(nid,sdid,start,sizes,sd3d)
[38]243#endif
244      if (ierr.ne.NF_NOERR) then
[1532]245         write (*,*) "wstats error reading :",trim(nom)
[38]246         write (*,*) NF_STRERROR(ierr)
247         stop ""
248      endif
249
250   else if (dim.eq.2) then
[1130]251      start=(/1,1,indx,0/)
[1532]252      if (klon_glo>1) then ! general case
253        sizes=(/nbp_lon+1,nbp_lat,1,0/)
254      else
255        sizes=(/1,1,1,0/)
256      endif
[38]257#ifdef NC_DOUBLE
[1130]258      ierr = NF_GET_VARA_DOUBLE(nid,meanid,start,sizes,mean2d)
259      ierr = NF_GET_VARA_DOUBLE(nid,sdid,start,sizes,sd2d)
[38]260#else
[1130]261      ierr = NF_GET_VARA_REAL(nid,meanid,start,sizes,mean2d)
262      ierr = NF_GET_VARA_REAL(nid,sdid,start,sizes,sd2d)
[38]263#endif
264      if (ierr.ne.NF_NOERR) then
[1532]265         write (*,*) "wstats error reading :",trim(nom)
[38]266         write (*,*) NF_STRERROR(ierr)
267         stop ""
268      endif
269   endif
[1130]270endif ! of if (count(indx)==0)
[38]271
[1130]272
273! 2.1. Build dx* (data on lon-lat grid, with redundant longitude)
274
[38]275if (dim.eq.3) then
[1528]276  dx3(1:nbp_lon,:,:)=px3_glo(:,:,:)
[1532]277  IF (klon_glo>1) THEN ! in 3D, add redundant longitude point
278    dx3(nbp_lon+1,:,:)=dx3(1,:,:)
279  ENDIF
[1130]280else ! dim.eq.2
[1528]281  dx2(1:nbp_lon,:)=px2_glo(:,:)
[1532]282  IF (klon_glo>1) THEN ! in 3D, add redundant longitude point
283    dx2(nbp_lon+1,:)=dx2(1,:)
284  ENDIF
[1130]285endif
[38]286
287
[1130]288! 2.2. Add current values to previously stored sums
[38]289
[1130]290if (dim.eq.3) then
[38]291
[1130]292   mean3d(:,:,:)=mean3d(:,:,:)+dx3(:,:,:)
293   sd3d(:,:,:)=sd3d(:,:,:)+dx3(:,:,:)**2
294
[38]295#ifdef NC_DOUBLE
[1130]296   ierr = NF_PUT_VARA_DOUBLE(nid,meanid,start,sizes,mean3d)
297   ierr = NF_PUT_VARA_DOUBLE(nid,sdid,start,sizes,sd3d)
[38]298#else
[1130]299   ierr = NF_PUT_VARA_REAL(nid,meanid,start,sizes,mean3d)
300   ierr = NF_PUT_VARA_REAL(nid,sdid,start,sizes,sd3d)
[38]301#endif
[1532]302  if (ierr.ne.NF_NOERR) then
303     write (*,*) "wstats error writing :",trim(nom)
304     write (*,*) NF_STRERROR(ierr)
305     stop ""
306  endif
[38]307
308else if (dim.eq.2) then
309
310   mean2d(:,:)= mean2d(:,:)+dx2(:,:)
[1130]311   sd2d(:,:)=sd2d(:,:)+dx2(:,:)**2
[38]312
313#ifdef NC_DOUBLE
[1130]314   ierr = NF_PUT_VARA_DOUBLE(nid,meanid,start,sizes,mean2d)
315   ierr = NF_PUT_VARA_DOUBLE(nid,sdid,start,sizes,sd2d)
[38]316#else
[1130]317   ierr = NF_PUT_VARA_REAL(nid,meanid,start,sizes,mean2d)
318   ierr = NF_PUT_VARA_REAL(nid,sdid,start,sizes,sd2d)
[38]319#endif
[1532]320  if (ierr.ne.NF_NOERR) then
321     write (*,*) "wstats error writing :",trim(nom)
322     write(*,*) "start:",start
323     write(*,*) "sizes:",sizes
324     write(*,*) "mean2d:",mean2d
325     write(*,*) "sd2d:",sd2d
326     write (*,*) NF_STRERROR(ierr)
327     stop ""
328  endif
[38]329
[1130]330endif ! of if (dim.eq.3) elseif (dim.eq.2)
[38]331
[1130]332  ierr= NF_CLOSE(nid)
333endif ! of if (is_master)
[38]334
335end subroutine wstats
336
337!======================================================
[1130]338subroutine inivar(nid,varid,ngrid,dim,indx,px,ierr)
[1532]339use mod_grid_phy_lmdz, only : nbp_lon, nbp_lat, nbp_lev, klon_glo
[38]340
341implicit none
342
343include "netcdf.inc"
344
[1130]345integer, intent(in) :: nid,varid,dim,indx,ngrid
[1528]346real, dimension(ngrid,nbp_lev), intent(in) :: px
[38]347integer, intent(out) :: ierr
348
349integer :: l,i,j,ig0
[1130]350integer, dimension(4) :: start,sizes
[1528]351real, dimension(nbp_lon+1,nbp_lat,nbp_lev) :: dx3
352real, dimension(nbp_lon+1,nbp_lat) :: dx2
[1532]353real :: dx3_1d(nbp_lev) ! for 1D outputs
354real :: dx2_1d ! for 1D outputs
[38]355
356if (dim.eq.3) then
357
[1130]358   start=(/1,1,1,indx/)
[1532]359   if (klon_glo>1) then ! general 3D case
360     sizes=(/nbp_lon+1,nbp_lat,nbp_lev,1/)
361   else
362     sizes=(/1,1,nbp_lev,1/)
363   endif
[38]364
365!  Passage variable physique -->  variable dynamique
366
[1532]367   if (klon_glo>1) then ! general case
368    DO l=1,nbp_lev
[1528]369      DO i=1,nbp_lon+1
[38]370         dx3(i,1,l)=px(1,l)
[1528]371         dx3(i,nbp_lat,l)=px(ngrid,l)
[38]372      ENDDO
[1528]373      DO j=2,nbp_lat-1
374         ig0= 1+(j-2)*nbp_lon
375         DO i=1,nbp_lon
[38]376            dx3(i,j,l)=px(ig0+i,l)
377         ENDDO
[1528]378         dx3(nbp_lon+1,j,l)=dx3(1,j,l)
[38]379      ENDDO
[1532]380    ENDDO
381   else ! 1D model case
382     dx3_1d(1:nbp_lev)=px(1,1:nbp_lev)
383   endif
[38]384
385#ifdef NC_DOUBLE
[1532]386   if (klon_glo>1) then
387     ierr = NF_PUT_VARA_DOUBLE(nid,varid,start,sizes,dx3)
388   else
389     ierr = NF_PUT_VARA_DOUBLE(nid,varid,start,sizes,dx3_1d)
390   endif
[38]391#else
[1532]392   if (klon_glo>1) then
393     ierr = NF_PUT_VARA_REAL(nid,varid,start,sizes,dx3)
394   else
395     ierr = NF_PUT_VARA_REAL(nid,varid,start,sizes,dx3_1d)
396   endif
[38]397#endif
[1532]398  if (ierr.ne.NF_NOERR) then
399     write (*,*) "inivar error writing variable"
400     write (*,*) NF_STRERROR(ierr)
401     stop ""
402  endif
[38]403
404else if (dim.eq.2) then
405
[1130]406      start=(/1,1,indx,0/)
[1532]407      if (klon_glo>1) then ! general 3D case
408        sizes=(/nbp_lon+1,nbp_lat,1,0/)
409      else
410        sizes=(/1,1,1,0/)
411      endif
[38]412
413!    Passage variable physique -->  physique dynamique
414
[1532]415  if (klon_glo>1) then ! general case
[1528]416  DO i=1,nbp_lon+1
[38]417     dx2(i,1)=px(1,1)
[1528]418     dx2(i,nbp_lat)=px(ngrid,1)
[38]419  ENDDO
[1528]420  DO j=2,nbp_lat-1
421     ig0= 1+(j-2)*nbp_lon
422     DO i=1,nbp_lon
[38]423        dx2(i,j)=px(ig0+i,1)
424     ENDDO
[1528]425     dx2(nbp_lon+1,j)=dx2(1,j)
[38]426  ENDDO
[1532]427  else ! 1D model case
428    dx2_1d=px(1,1)
429  endif
430 
[38]431#ifdef NC_DOUBLE
[1532]432   if (klon_glo>1) then
433     ierr = NF_PUT_VARA_DOUBLE(nid,varid,start,sizes,dx2)
434   else
435     ierr = NF_PUT_VARA_DOUBLE(nid,varid,start,sizes,dx2_1d)
436   endif
[38]437#else
[1532]438   if (klon_glo>1) then
439     ierr = NF_PUT_VARA_REAL(nid,varid,start,sizes,dx2)
440   else
441     ierr = NF_PUT_VARA_REAL(nid,varid,start,sizes,dx2_1d)
442   endif
[38]443#endif
[1532]444  if (ierr.ne.NF_NOERR) then
445     write (*,*) "inivar error writing variable"
446     write (*,*) NF_STRERROR(ierr)
447     stop ""
448  endif
[38]449
450endif
451
452end subroutine inivar
453
454!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
455
456subroutine def_var_stats(nid,name,title,units,nbdim,dimids,nvarid,ierr)
457
458! This subroutine defines variable 'name' in a (pre-existing and opened)
459! NetCDF file (known from its NetCDF ID 'nid').
460! The number of dimensions 'nbdim' of the variable, as well as the IDs of
461! corresponding dimensions must be set (in array 'dimids').
462! Upon successfull definition of the variable, 'nvarid' contains the
463! NetCDF ID of the variable.
464! The variables' attributes 'title' (Note that 'long_name' would be more
465! appropriate) and 'units' are also set.
466
467implicit none
468
[1528]469include "netcdf.inc"
[38]470
471integer,intent(in) :: nid ! NetCDF file ID
472character(len=*),intent(in) :: name ! the variable's name
473character(len=*),intent(in) :: title ! 'title' attribute of variable
474character(len=*),intent(in) :: units ! 'units' attribute of variable
475integer,intent(in) :: nbdim ! number of dimensions of the variable
476integer,dimension(nbdim),intent(in) :: dimids ! NetCDF IDs of the dimensions
477                                              ! the variable is defined along
478integer,intent(out) :: nvarid ! NetCDF ID of the variable
479integer,intent(out) :: ierr ! returned NetCDF staus code
480
481! 1. Switch to NetCDF define mode
482ierr=NF_REDEF(nid)
483
484! 2. Define the variable
485#ifdef NC_DOUBLE
486ierr = NF_DEF_VAR (nid,adjustl(name),NF_DOUBLE,nbdim,dimids,nvarid)
487#else
488ierr = NF_DEF_VAR (nid,adjustl(name),NF_FLOAT,nbdim,dimids,nvarid)
489#endif
490if(ierr/=NF_NOERR) then
491   write(*,*) "def_var_stats: Failed defining variable "//trim(name)
492   write(*,*) NF_STRERROR(ierr)
493   stop ""
494endif
495
496! 3. Write attributes
497ierr=NF_PUT_ATT_TEXT(nid,nvarid,"title",&
498                     len_trim(adjustl(title)),adjustl(title))
499if(ierr/=NF_NOERR) then
500   write(*,*) "def_var_stats: Failed writing title attribute for "//trim(name)
501   write(*,*) NF_STRERROR(ierr)
502   stop ""
503endif
504
505ierr=NF_PUT_ATT_TEXT(nid,nvarid,"units",&
506                     len_trim(adjustl(units)),adjustl(units))
507if(ierr/=NF_NOERR) then
508   write(*,*) "def_var_stats: Failed writing units attribute for "//trim(name)
509   write(*,*) NF_STRERROR(ierr)
510   stop ""
511endif
512
513! 4. Switch out of NetCDF define mode
514ierr = NF_ENDDEF(nid)
515
516end subroutine def_var_stats
517
Note: See TracBrowser for help on using the repository browser.