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