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

Last change on this file since 308 was 38, checked in by emillour, 14 years ago

Ajout du modè Martien (mon LMDZ.MARS.BETA, du 28/01/2011) dans le rértoire mars, pour pouvoir suivre plus facilement les modifs.
EM

File size: 8.5 KB
Line 
1subroutine wstats(ngrid,nom,titre,unite,dim,px)
2
3implicit none
4
5#include "dimensions.h"
6#include "dimphys.h"
7#include "statto.h"
8#include "netcdf.inc"
9
10integer,intent(in) :: ngrid
11character (len=*),intent(in) :: nom,titre,unite
12integer,intent(in) :: dim
13real, dimension(ngrid,llm),intent(in) :: px
14integer,parameter :: iip1=iim+1
15integer,parameter :: jjp1=jjm+1
16real, dimension(iip1,jjp1,llm) :: mean3d,sd3d,dx3
17real, dimension(iip1,jjp1) :: mean2d,sd2d,dx2
18character (len=50) :: namebis
19character (len=50), save :: firstvar
20integer :: ierr,varid,nbdim,nid
21integer :: meanid,sdid
22integer, dimension(4)  :: id,start,size
23logical, save :: firstcall=.TRUE.
24integer :: l,i,j,ig0
25integer,save :: index
26
27integer, save :: step=0
28
29
30if (firstcall) then
31   firstcall=.false.
32   firstvar=trim((nom))
33   call inistats(ierr)
34endif
35
36if (firstvar==nom) then ! If we're back to the first variable
37      step = step + 1
38endif
39
40if (mod(step,istats).ne.0) then
41   RETURN
42endif
43
44ierr = NF_OPEN("stats.nc",NF_WRITE,nid)
45
46namebis=trim(nom)
47ierr= NF_INQ_VARID(nid,namebis,meanid)
48
49if (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
83else
84   namebis=trim(nom)//"_sd"
85   ierr= NF_INQ_VARID(nid,namebis,sdid)
86
87endif
88
89if (firstvar==nom) then
90   count(index)=count(int(index))+1
91   index=index+1
92   if (index>istime) then
93      index=1
94   endif
95endif
96
97if (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
109else
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
140endif
141
142if (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
175else 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
206endif
207
208ierr= NF_CLOSE(nid)
209
210end subroutine wstats
211
212!======================================================
213subroutine inivar(nid,varid,ngrid,dim,index,px,ierr)
214
215implicit none
216
217include "dimensions.h"
218include "dimphys.h"
219include "netcdf.inc"
220
221integer, intent(in) :: nid,varid,dim,index,ngrid
222real, dimension(ngrid,llm), intent(in) :: px
223integer, intent(out) :: ierr
224
225integer,parameter :: iip1=iim+1
226integer,parameter :: jjp1=jjm+1
227
228integer :: l,i,j,ig0
229integer, dimension(4) :: start,size
230real, dimension(iip1,jjp1,llm) :: dx3
231real, dimension(iip1,jjp1) :: dx2
232
233if (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
260else 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
285endif
286
287end subroutine inivar
288
289!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
290
291subroutine 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
302implicit none
303
304#include "netcdf.inc"
305
306integer,intent(in) :: nid ! NetCDF file ID
307character(len=*),intent(in) :: name ! the variable's name
308character(len=*),intent(in) :: title ! 'title' attribute of variable
309character(len=*),intent(in) :: units ! 'units' attribute of variable
310integer,intent(in) :: nbdim ! number of dimensions of the variable
311integer,dimension(nbdim),intent(in) :: dimids ! NetCDF IDs of the dimensions
312                                              ! the variable is defined along
313integer,intent(out) :: nvarid ! NetCDF ID of the variable
314integer,intent(out) :: ierr ! returned NetCDF staus code
315
316! 1. Switch to NetCDF define mode
317ierr=NF_REDEF(nid)
318
319! 2. Define the variable
320#ifdef NC_DOUBLE
321ierr = NF_DEF_VAR (nid,adjustl(name),NF_DOUBLE,nbdim,dimids,nvarid)
322#else
323ierr = NF_DEF_VAR (nid,adjustl(name),NF_FLOAT,nbdim,dimids,nvarid)
324#endif
325if(ierr/=NF_NOERR) then
326   write(*,*) "def_var_stats: Failed defining variable "//trim(name)
327   write(*,*) NF_STRERROR(ierr)
328   stop ""
329endif
330
331! 3. Write attributes
332ierr=NF_PUT_ATT_TEXT(nid,nvarid,"title",&
333                     len_trim(adjustl(title)),adjustl(title))
334if(ierr/=NF_NOERR) then
335   write(*,*) "def_var_stats: Failed writing title attribute for "//trim(name)
336   write(*,*) NF_STRERROR(ierr)
337   stop ""
338endif
339
340ierr=NF_PUT_ATT_TEXT(nid,nvarid,"units",&
341                     len_trim(adjustl(units)),adjustl(units))
342if(ierr/=NF_NOERR) then
343   write(*,*) "def_var_stats: Failed writing units attribute for "//trim(name)
344   write(*,*) NF_STRERROR(ierr)
345   stop ""
346endif
347
348! 4. Switch out of NetCDF define mode
349ierr = NF_ENDDEF(nid)
350
351end subroutine def_var_stats
352
Note: See TracBrowser for help on using the repository browser.