My Project
 All Classes Files Functions Variables Macros
mkstat.F90
Go to the documentation of this file.
1 subroutine mkstats(ierr)
2 
3 
4 !
5 ! This program writes a stats.nc file from sums and sums of squares
6 ! to means and standard deviations and also writes netcdf style
7 ! file so that the data can be viewed easily. The data file is
8 ! overwritten in place.
9 ! SRL 21 May 1996
10 ! Yann W. july 2003
11 
12 
13 implicit none
14 
15 #include "dimensions.h"
16 #include "statto.h"
17 #include "netcdf.inc"
18 
19 integer,parameter :: iip1=iim+1
20 integer,parameter :: jjp1=jjm+1
21 integer :: ierr,nid,nbvar,i,ndims,lt,nvarid
22 integer, dimension(4) :: id,varid,start,size
23 integer, dimension(5) :: dimids
24 character (len=50) :: name,nameout,units,title
25 real, dimension(iip1,jjp1,llm) :: sum3d,square3d,mean3d,sd3d
26 real, dimension(iip1,jjp1) :: sum2d,square2d,mean2d,sd2d
27 real, dimension(istime) :: time
28 real, dimension(jjp1) :: lat
29 real, dimension(iip1) :: lon
30 real, dimension(llm) :: alt
31 logical :: lcopy=.true.
32 !integer :: latid,lonid,altid,timeid
33 integer :: meanid,sdid
34 !integer, dimension(4) :: dimout
35 
36 ! Incrementation of count for the last step, which is not done in wstats
37 count(istime)=count(istime)+1
38 
39 ierr = nf_open("stats.nc",nf_write,nid)
40 
41 ! We catch the id of dimensions of the stats file
42 
43 ierr= nf_inq_dimid(nid,"latitude",id(1))
44 ierr= nf_inq_dimid(nid,"longitude",id(2))
45 ierr= nf_inq_dimid(nid,"altitude",id(3))
46 ierr= nf_inq_dimid(nid,"Time",id(4))
47 
48 ierr= nf_inq_varid(nid,"latitude",varid(1))
49 ierr= nf_inq_varid(nid,"longitude",varid(2))
50 ierr= nf_inq_varid(nid,"altitude",varid(3))
51 ierr= nf_inq_varid(nid,"Time",varid(4))
52 
53 ! Time initialisation
54 
55 do i=1,istime
56  time(i)=i*24./istime
57 #ifdef NC_DOUBLE
58  ierr= nf_put_vara_double(nid,varid(4),i,1,time(i))
59 #else
60  ierr= nf_put_vara_real(nid,varid(4),i,1,time(i))
61 #endif
62 enddo
63 
64 ! We catche the values of the variables
65 
66 #ifdef NC_DOUBLE
67  ierr = nf_get_var_double(nid,varid(1),lat)
68  ierr = nf_get_var_double(nid,varid(2),lon)
69  ierr = nf_get_var_double(nid,varid(3),alt)
70 #else
71  ierr = nf_get_var_real(nid,varid(1),lat)
72  ierr = nf_get_var_real(nid,varid(2),lon)
73  ierr = nf_get_var_real(nid,varid(3),alt)
74 #endif
75 
76 ! We catch the number of variables in the stats file
77 ierr = nf_inq_nvars(nid,nbvar)
78 
79 ! to catche the "real" number of variables (without the "additionnal variables")
80 nbvar=(nbvar-4)/2
81 
82 do i=1,nbvar
83  varid=(i-1)*2+5
84 
85  ! What's the variable's name?
86  ierr=nf_inq_varname(nid,varid,name)
87  write(*,*) "OK variable ",name
88  ! Its units?
89  units=" "
90  ierr=nf_get_att_text(nid,varid,"units",units)
91  ! Its title?
92  title=" "
93  ierr=nf_get_att_text(nid,varid,"title",title)
94  ! Its number of dimensions?
95  ierr=nf_inq_varndims(nid,varid,ndims)
96  ! Its values?
97 
98  if(ndims==4) then ! lat, lon, alt & time
99 
100 ! dimout(1)=lonid
101 ! dimout(2)=latid
102 ! dimout(3)=altid
103 ! dimout(4)=timeid
104 
105  size=(/iip1,jjp1,llm,1/)
106  do lt=1,istime
107  start=(/1,1,1,lt/)
108  ! Extraction of the "source" variables
109 #ifdef NC_DOUBLE
110  ierr = nf_get_vara_double(nid,varid,start,size,sum3d)
111  ierr = nf_get_vara_double(nid,varid+1,start,size,square3d)
112 #else
113  ierr = nf_get_vara_real(nid,varid,start,size,sum3d)
114  ierr = nf_get_vara_real(nid,varid+1,start,size,square3d)
115 #endif
116  ! Calculation of these variables
117  mean3d=sum3d/count(lt)
118  sd3d=sqrt(max(0.,square3d/count(lt)-mean3d**2))
119  ! Writing of the variables
120 #ifdef NC_DOUBLE
121  ierr = nf_put_vara_double(nid,varid,start,size,mean3d)
122  ierr = nf_put_vara_double(nid,varid+1,start,size,sd3d)
123 #else
124  ierr = nf_put_vara_real(nid,varid,start,size,mean3d)
125  ierr = nf_put_vara_real(nid,varid+1,start,size,sd3d)
126 #endif
127  enddo
128 
129  else if (ndims.eq.3) then
130 
131 ! dimout(1)=lonid
132 ! dimout(2)=latid
133 ! dimout(3)=timeid
134 
135  size=(/iip1,jjp1,1,0/)
136  do lt=1,istime
137  start=(/1,1,lt,0/)
138  ! Extraction of the "source" variables
139 #ifdef NC_DOUBLE
140  ierr = nf_get_vara_double(nid,varid,start,size,sum2d)
141  ierr = nf_get_vara_double(nid,varid+1,start,size,square2d)
142 #else
143  ierr = nf_get_vara_real(nid,varid,start,size,sum2d)
144  ierr = nf_get_vara_real(nid,varid+1,start,size,square2d)
145 #endif
146  ! Calculation of these variables
147  mean2d=sum2d/count(lt)
148  sd2d=sqrt(max(0.,square2d/count(lt)-mean2d**2))
149  ! Writing of the variables
150 #ifdef NC_DOUBLE
151  ierr = nf_put_vara_double(nid,varid,start,size,mean2d)
152  ierr = nf_put_vara_double(nid,varid+1,start,size,sd2d)
153 #else
154  ierr = nf_put_vara_real(nid,varid,start,size,mean2d)
155  ierr = nf_put_vara_real(nid,varid+1,start,size,sd2d)
156 #endif
157  enddo
158 
159  endif
160 enddo
161 
162 ierr= nf_close(nid)
163 
164 end