1 | subroutine wstats(ngrid,nom,titre,unite,dim,px) |
---|
2 | |
---|
3 | implicit none |
---|
4 | |
---|
5 | #include "dimensions.h" |
---|
6 | #include "dimphys.h" |
---|
7 | #include "statto.h" |
---|
8 | #include "netcdf.inc" |
---|
9 | |
---|
10 | integer,intent(in) :: ngrid |
---|
11 | character (len=*),intent(in) :: nom,titre,unite |
---|
12 | integer,intent(in) :: dim |
---|
13 | real, dimension(ngrid,llm),intent(in) :: px |
---|
14 | integer,parameter :: iip1=iim+1 |
---|
15 | integer,parameter :: jjp1=jjm+1 |
---|
16 | real, dimension(iip1,jjp1,llm) :: mean3d,sd3d,dx3 |
---|
17 | real, dimension(iip1,jjp1) :: mean2d,sd2d,dx2 |
---|
18 | character (len=50) :: namebis |
---|
19 | character (len=50), save :: firstvar |
---|
20 | integer :: ierr,varid,nbdim,nid |
---|
21 | integer :: meanid,sdid |
---|
22 | integer, dimension(4) :: id,start,size |
---|
23 | logical, save :: firstcall=.TRUE. |
---|
24 | integer :: l,i,j,ig0 |
---|
25 | integer,save :: index |
---|
26 | |
---|
27 | integer, save :: step=0 |
---|
28 | |
---|
29 | |
---|
30 | if (firstcall) then |
---|
31 | firstcall=.false. |
---|
32 | firstvar=trim((nom)) |
---|
33 | call inistats(ierr) |
---|
34 | endif |
---|
35 | |
---|
36 | if (firstvar==nom) then ! If we're back to the first variable |
---|
37 | step = step + 1 |
---|
38 | endif |
---|
39 | |
---|
40 | if (mod(step,istats).ne.0) then |
---|
41 | RETURN |
---|
42 | endif |
---|
43 | |
---|
44 | ierr = NF_OPEN("stats.nc",NF_WRITE,nid) |
---|
45 | |
---|
46 | namebis=trim(nom) |
---|
47 | ierr= NF_INQ_VARID(nid,namebis,meanid) |
---|
48 | |
---|
49 | if (ierr.ne.NF_NOERR) then |
---|
50 | |
---|
51 | if (firstvar==nom) then |
---|
52 | index=1 |
---|
53 | count=0 |
---|
54 | endif |
---|
55 | |
---|
56 | |
---|
57 | !declaration de la variable |
---|
58 | |
---|
59 | ! choix du nom des coordonnees |
---|
60 | ierr= NF_INQ_DIMID(nid,"longitude",id(1)) |
---|
61 | ierr= NF_INQ_DIMID(nid,"latitude",id(2)) |
---|
62 | if (dim.eq.3) then |
---|
63 | ierr= NF_INQ_DIMID(nid,"altitude",id(3)) |
---|
64 | ierr= NF_INQ_DIMID(nid,"Time",id(4)) |
---|
65 | nbdim=4 |
---|
66 | else if (dim==2) then |
---|
67 | ierr= NF_INQ_DIMID(nid,"Time",id(3)) |
---|
68 | nbdim=3 |
---|
69 | endif |
---|
70 | |
---|
71 | write (*,*) "=====================" |
---|
72 | write (*,*) "STATS: creation de ",nom |
---|
73 | namebis=trim(nom) |
---|
74 | call def_var_stats(nid,namebis,titre,unite,nbdim,id,meanid,ierr) |
---|
75 | call inivar(nid,meanid,ngrid,dim,index,px,ierr) |
---|
76 | namebis=trim(nom)//"_sd" |
---|
77 | call def_var_stats(nid,namebis,trim(titre)//" total standard deviation over the season",unite,nbdim,id,sdid,ierr) |
---|
78 | call inivar(nid,sdid,ngrid,dim,index,px,ierr) |
---|
79 | |
---|
80 | ierr= NF_CLOSE(nid) |
---|
81 | return |
---|
82 | |
---|
83 | else |
---|
84 | namebis=trim(nom)//"_sd" |
---|
85 | ierr= NF_INQ_VARID(nid,namebis,sdid) |
---|
86 | |
---|
87 | endif |
---|
88 | |
---|
89 | if (firstvar==nom) then |
---|
90 | count(index)=count(int(index))+1 |
---|
91 | index=index+1 |
---|
92 | if (index>istime) then |
---|
93 | index=1 |
---|
94 | endif |
---|
95 | endif |
---|
96 | |
---|
97 | if (count(index)==0) then |
---|
98 | if (dim.eq.3) then |
---|
99 | start=(/1,1,1,index/) |
---|
100 | size=(/iip1,jjp1,llm,1/) |
---|
101 | mean3d=0 |
---|
102 | sd3d=0 |
---|
103 | else if (dim.eq.2) then |
---|
104 | start=(/1,1,index,0/) |
---|
105 | size=(/iip1,jjp1,1,0/) |
---|
106 | mean2d=0 |
---|
107 | sd2d=0 |
---|
108 | endif |
---|
109 | else |
---|
110 | if (dim.eq.3) then |
---|
111 | start=(/1,1,1,index/) |
---|
112 | size=(/iip1,jjp1,llm,1/) |
---|
113 | #ifdef NC_DOUBLE |
---|
114 | ierr = NF_GET_VARA_DOUBLE(nid,meanid,start,size,mean3d) |
---|
115 | ierr = NF_GET_VARA_DOUBLE(nid,sdid,start,size,sd3d) |
---|
116 | #else |
---|
117 | ierr = NF_GET_VARA_REAL(nid,meanid,start,size,mean3d) |
---|
118 | ierr = NF_GET_VARA_REAL(nid,sdid,start,size,sd3d) |
---|
119 | #endif |
---|
120 | if (ierr.ne.NF_NOERR) then |
---|
121 | write (*,*) NF_STRERROR(ierr) |
---|
122 | stop "" |
---|
123 | endif |
---|
124 | |
---|
125 | else if (dim.eq.2) then |
---|
126 | start=(/1,1,index,0/) |
---|
127 | size=(/iip1,jjp1,1,0/) |
---|
128 | #ifdef NC_DOUBLE |
---|
129 | ierr = NF_GET_VARA_DOUBLE(nid,meanid,start,size,mean2d) |
---|
130 | ierr = NF_GET_VARA_DOUBLE(nid,sdid,start,size,sd2d) |
---|
131 | #else |
---|
132 | ierr = NF_GET_VARA_REAL(nid,meanid,start,size,mean2d) |
---|
133 | ierr = NF_GET_VARA_REAL(nid,sdid,start,size,sd2d) |
---|
134 | #endif |
---|
135 | if (ierr.ne.NF_NOERR) then |
---|
136 | write (*,*) NF_STRERROR(ierr) |
---|
137 | stop "" |
---|
138 | endif |
---|
139 | endif |
---|
140 | endif |
---|
141 | |
---|
142 | if (dim.eq.3) then |
---|
143 | |
---|
144 | ! Passage variable physique --> variable dynamique |
---|
145 | |
---|
146 | DO l=1,llm |
---|
147 | DO i=1,iip1 |
---|
148 | dx3(i,1,l)=px(1,l) |
---|
149 | dx3(i,jjp1,l)=px(ngrid,l) |
---|
150 | ENDDO |
---|
151 | DO j=2,jjm |
---|
152 | ig0= 1+(j-2)*iim |
---|
153 | DO i=1,iim |
---|
154 | dx3(i,j,l)=px(ig0+i,l) |
---|
155 | ENDDO |
---|
156 | dx3(iip1,j,l)=dx3(1,j,l) |
---|
157 | ENDDO |
---|
158 | ENDDO |
---|
159 | |
---|
160 | mean3d(:,:,:)= mean3d(:,:,:)+dx3(:,:,:) |
---|
161 | sd3d(:,:,:)= sd3d(:,:,:)+dx3(:,:,:)**2 |
---|
162 | |
---|
163 | #ifdef NC_DOUBLE |
---|
164 | ierr = NF_PUT_VARA_DOUBLE(nid,meanid,start,size,mean3d) |
---|
165 | ierr = NF_PUT_VARA_DOUBLE(nid,sdid,start,size,sd3d) |
---|
166 | #else |
---|
167 | ierr = NF_PUT_VARA_REAL(nid,meanid,start,size,mean3d) |
---|
168 | ierr = NF_PUT_VARA_REAL(nid,sdid,start,size,sd3d) |
---|
169 | #endif |
---|
170 | if (ierr.ne.NF_NOERR) then |
---|
171 | write (*,*) NF_STRERROR(ierr) |
---|
172 | stop "" |
---|
173 | endif |
---|
174 | |
---|
175 | else if (dim.eq.2) then |
---|
176 | |
---|
177 | ! Passage variable physique --> physique dynamique |
---|
178 | |
---|
179 | DO i=1,iip1 |
---|
180 | dx2(i,1)=px(1,1) |
---|
181 | dx2(i,jjp1)=px(ngrid,1) |
---|
182 | ENDDO |
---|
183 | DO j=2,jjm |
---|
184 | ig0= 1+(j-2)*iim |
---|
185 | DO i=1,iim |
---|
186 | dx2(i,j)=px(ig0+i,1) |
---|
187 | ENDDO |
---|
188 | dx2(iip1,j)=dx2(1,j) |
---|
189 | ENDDO |
---|
190 | |
---|
191 | mean2d(:,:)= mean2d(:,:)+dx2(:,:) |
---|
192 | sd2d(:,:)= sd2d(:,:)+dx2(:,:)**2 |
---|
193 | |
---|
194 | #ifdef NC_DOUBLE |
---|
195 | ierr = NF_PUT_VARA_DOUBLE(nid,meanid,start,size,mean2d) |
---|
196 | ierr = NF_PUT_VARA_DOUBLE(nid,sdid,start,size,sd2d) |
---|
197 | #else |
---|
198 | ierr = NF_PUT_VARA_REAL(nid,meanid,start,size,mean2d) |
---|
199 | ierr = NF_PUT_VARA_REAL(nid,sdid,start,size,sd2d) |
---|
200 | #endif |
---|
201 | if (ierr.ne.NF_NOERR) then |
---|
202 | write (*,*) NF_STRERROR(ierr) |
---|
203 | stop "" |
---|
204 | endif |
---|
205 | |
---|
206 | endif |
---|
207 | |
---|
208 | ierr= NF_CLOSE(nid) |
---|
209 | |
---|
210 | end subroutine wstats |
---|
211 | |
---|
212 | !====================================================== |
---|
213 | subroutine inivar(nid,varid,ngrid,dim,index,px,ierr) |
---|
214 | |
---|
215 | implicit none |
---|
216 | |
---|
217 | include "dimensions.h" |
---|
218 | include "dimphys.h" |
---|
219 | include "netcdf.inc" |
---|
220 | |
---|
221 | integer, intent(in) :: nid,varid,dim,index,ngrid |
---|
222 | real, dimension(ngrid,llm), intent(in) :: px |
---|
223 | integer, intent(out) :: ierr |
---|
224 | |
---|
225 | integer,parameter :: iip1=iim+1 |
---|
226 | integer,parameter :: jjp1=jjm+1 |
---|
227 | |
---|
228 | integer :: l,i,j,ig0 |
---|
229 | integer, dimension(4) :: start,size |
---|
230 | real, dimension(iip1,jjp1,llm) :: dx3 |
---|
231 | real, dimension(iip1,jjp1) :: dx2 |
---|
232 | |
---|
233 | if (dim.eq.3) then |
---|
234 | |
---|
235 | start=(/1,1,1,index/) |
---|
236 | size=(/iip1,jjp1,llm,1/) |
---|
237 | |
---|
238 | ! Passage variable physique --> variable dynamique |
---|
239 | |
---|
240 | DO l=1,llm |
---|
241 | DO i=1,iip1 |
---|
242 | dx3(i,1,l)=px(1,l) |
---|
243 | dx3(i,jjp1,l)=px(ngrid,l) |
---|
244 | ENDDO |
---|
245 | DO j=2,jjm |
---|
246 | ig0= 1+(j-2)*iim |
---|
247 | DO i=1,iim |
---|
248 | dx3(i,j,l)=px(ig0+i,l) |
---|
249 | ENDDO |
---|
250 | dx3(iip1,j,l)=dx3(1,j,l) |
---|
251 | ENDDO |
---|
252 | ENDDO |
---|
253 | |
---|
254 | #ifdef NC_DOUBLE |
---|
255 | ierr = NF_PUT_VARA_DOUBLE(nid,varid,start,size,dx3) |
---|
256 | #else |
---|
257 | ierr = NF_PUT_VARA_REAL(nid,varid,start,size,dx3) |
---|
258 | #endif |
---|
259 | |
---|
260 | else if (dim.eq.2) then |
---|
261 | |
---|
262 | start=(/1,1,index,0/) |
---|
263 | size=(/iip1,jjp1,1,0/) |
---|
264 | |
---|
265 | ! Passage variable physique --> physique dynamique |
---|
266 | |
---|
267 | DO i=1,iip1 |
---|
268 | dx2(i,1)=px(1,1) |
---|
269 | dx2(i,jjp1)=px(ngrid,1) |
---|
270 | ENDDO |
---|
271 | DO j=2,jjm |
---|
272 | ig0= 1+(j-2)*iim |
---|
273 | DO i=1,iim |
---|
274 | dx2(i,j)=px(ig0+i,1) |
---|
275 | ENDDO |
---|
276 | dx2(iip1,j)=dx2(1,j) |
---|
277 | ENDDO |
---|
278 | |
---|
279 | #ifdef NC_DOUBLE |
---|
280 | ierr = NF_PUT_VARA_DOUBLE(nid,varid,start,size,dx2) |
---|
281 | #else |
---|
282 | ierr = NF_PUT_VARA_REAL(nid,varid,start,size,dx2) |
---|
283 | #endif |
---|
284 | |
---|
285 | endif |
---|
286 | |
---|
287 | end subroutine inivar |
---|
288 | |
---|
289 | !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
---|
290 | |
---|
291 | subroutine def_var_stats(nid,name,title,units,nbdim,dimids,nvarid,ierr) |
---|
292 | |
---|
293 | ! This subroutine defines variable 'name' in a (pre-existing and opened) |
---|
294 | ! NetCDF file (known from its NetCDF ID 'nid'). |
---|
295 | ! The number of dimensions 'nbdim' of the variable, as well as the IDs of |
---|
296 | ! corresponding dimensions must be set (in array 'dimids'). |
---|
297 | ! Upon successfull definition of the variable, 'nvarid' contains the |
---|
298 | ! NetCDF ID of the variable. |
---|
299 | ! The variables' attributes 'title' (Note that 'long_name' would be more |
---|
300 | ! appropriate) and 'units' are also set. |
---|
301 | |
---|
302 | implicit none |
---|
303 | |
---|
304 | #include "netcdf.inc" |
---|
305 | |
---|
306 | integer,intent(in) :: nid ! NetCDF file ID |
---|
307 | character(len=*),intent(in) :: name ! the variable's name |
---|
308 | character(len=*),intent(in) :: title ! 'title' attribute of variable |
---|
309 | character(len=*),intent(in) :: units ! 'units' attribute of variable |
---|
310 | integer,intent(in) :: nbdim ! number of dimensions of the variable |
---|
311 | integer,dimension(nbdim),intent(in) :: dimids ! NetCDF IDs of the dimensions |
---|
312 | ! the variable is defined along |
---|
313 | integer,intent(out) :: nvarid ! NetCDF ID of the variable |
---|
314 | integer,intent(out) :: ierr ! returned NetCDF staus code |
---|
315 | |
---|
316 | ! 1. Switch to NetCDF define mode |
---|
317 | ierr=NF_REDEF(nid) |
---|
318 | |
---|
319 | ! 2. Define the variable |
---|
320 | #ifdef NC_DOUBLE |
---|
321 | ierr = NF_DEF_VAR (nid,adjustl(name),NF_DOUBLE,nbdim,dimids,nvarid) |
---|
322 | #else |
---|
323 | ierr = NF_DEF_VAR (nid,adjustl(name),NF_FLOAT,nbdim,dimids,nvarid) |
---|
324 | #endif |
---|
325 | if(ierr/=NF_NOERR) then |
---|
326 | write(*,*) "def_var_stats: Failed defining variable "//trim(name) |
---|
327 | write(*,*) NF_STRERROR(ierr) |
---|
328 | stop "" |
---|
329 | endif |
---|
330 | |
---|
331 | ! 3. Write attributes |
---|
332 | ierr=NF_PUT_ATT_TEXT(nid,nvarid,"title",& |
---|
333 | len_trim(adjustl(title)),adjustl(title)) |
---|
334 | if(ierr/=NF_NOERR) then |
---|
335 | write(*,*) "def_var_stats: Failed writing title attribute for "//trim(name) |
---|
336 | write(*,*) NF_STRERROR(ierr) |
---|
337 | stop "" |
---|
338 | endif |
---|
339 | |
---|
340 | ierr=NF_PUT_ATT_TEXT(nid,nvarid,"units",& |
---|
341 | len_trim(adjustl(units)),adjustl(units)) |
---|
342 | if(ierr/=NF_NOERR) then |
---|
343 | write(*,*) "def_var_stats: Failed writing units attribute for "//trim(name) |
---|
344 | write(*,*) NF_STRERROR(ierr) |
---|
345 | stop "" |
---|
346 | endif |
---|
347 | |
---|
348 | ! 4. Switch out of NetCDF define mode |
---|
349 | ierr = NF_ENDDEF(nid) |
---|
350 | |
---|
351 | end subroutine def_var_stats |
---|
352 | |
---|