-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreducets.f90
275 lines (251 loc) · 9.59 KB
/
reducets.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
! Compile (mistral)
! ifort reduceps.f90 -o reduceps `/sw/rhel6-x64/netcdf/netcdf_fortran-4.4.2-intel14/bin/nf-config --fflags --flibs`
! Compile (blizzard):
! xlf95_r -o reducets reducets.f90 `/sw/aix61/netcdf-4.2.1.1/bin/nf-config --fflags --flibs`
! Compile (thunder):
! ifort -o reducets reducets.f90 -L/sw/sles10-x64/netcdf-3.6.3-intel/lib -lnetcdf -I/sw/sles10-x64/netcdf-3.6.3-intel/include
!new path: /groups/ocean/software/netcdf4/gcc/4.4.1.1/
Program reduce
use netcdf
implicit none
! This program glues together the ts files from UCLA
! Compile with ifort -o reducets reducets.f90 -L/sw/sles9-x64/netcdf-3.6.2-intel/lib -lnetcdf
! -I/sw/sles9-x64/netcdf-3.6.2-intel/include on tornado
! Assume number of time steps nt smaller than 10000 and number of variables nv smaller than 35 ! otherwise need to be changes
integer, parameter :: nv=100,nma=8,nmi=3,nsu=28 ! Christopher: nma+1 (zc), nmi+1 (zb)
integer :: nt
character(100) stem,nm,nm2
character(200) outpath
character(20) pref,name,dimname,cnx,cny
character(4) xsuf,ysuf
character(1) dum1
character(2) dum2
integer nx,ny
integer status,ncid,ndim,nvar,ncidw,timeid,varid
integer flag(nv)
character(len=10) maxnms(nma)
character(len=10) minnms(nmi)
character(len=10) sumnms(nsu)
character(len=100) namout(nv),lname(nv),uname(nv)
real fval(nv)
real, dimension(:,:), allocatable :: var,varout
integer, dimension(:,:), allocatable :: cnt
integer i,j,k,kk,l, ntr
!0. Some preparations
!-------------------,cnx,cny-
!* maxnms contains the names where we take max, minnms where we take min, summns where we take !*sum; if nothing specified will take the mean; list can be extended if needed
!* Ask to get filenames
call get_command_argument(1,stem)
call get_command_argument(2,cnx)
call get_command_argument(3,cny)
call get_command_argument(4,outpath)
read (cnx,*) nx
read (cny,*) ny
!! print*,'Directory where files are'
! read*,dirin
! print*,'Files prefix'
! read*,pref
! print*,'Processors in x'
! read*,nx
! print*,'Processors in y'
! read*,ny
! Open the first file to check the amoutn of timesteps; allocate the arrays
nm=trim(stem)//".ts.00000000.nc"
print*,trim(nm)
status=nf90_open(nm,nf90_nowrite,ncid)
if (status.ne.nf90_noerr) print*,nf90_strerror(status)
status=nf90_inquire(ncid,unlimitedDimID=timeid)
status=nf90_inquire_dimension(ncid,timeid,len=nt)
status=nf90_close(ncid)
print *, 'Number of timesteps ', nt
allocate(var(nv,nt), varout(nv,nt), cnt(nv,nt))
varout(:,:)=0.
cnt(:,:)=0
var(:,:)=0.
maxnms(1)="cfl "
maxnms(2)="maxdiv "
maxnms(3)="wmax "
maxnms(4)="lmax "
maxnms(5)="bflxmx "
maxnms(6)="bflxrmx "
maxnms(7)="precip_m "
maxnms(8)="zc "
minnms(1)="bflxmn "
minnms(2)="bflxrmn "
minnms(3)="zb "
sumnms(1)="wr_cs1 "
sumnms(2)="wr_cs2 "
sumnms(3)="wv_cs1 "
sumnms(4)="wv_cs2 "
sumnms(5)="wt_cs1 "
sumnms(6)="wt_cs2 "
sumnms(7)="rt_cs1 "
sumnms(8)="rt_cs2 "
sumnms(9)="rl_cs1 "
sumnms(10)="rl_cs2 "
sumnms(11)="tv_cs1 "
sumnms(12)="tv_cs2 "
sumnms(13)="tl_cs1 "
sumnms(14)="tl_cs2 "
sumnms(15)="w_cs1 "
sumnms(16)="w_cs2 "
sumnms(17)="cnt_cs1 "
sumnms(18)="cnt_cs2 "
sumnms(19)="nrain "
sumnms(20)="nrcnt "
sumnms(21)="hst_d1 "
sumnms(22)="hst_d2 "
sumnms(23)="hst_d3 "
sumnms(24)="hst_d4 "
sumnms(25)="dissum "
sumnms(26)="discnt "
sumnms(27)="dissum2 "
sumnms(28)="discnt2 "
!1. Open each file and read variable
!----------------------------------
!*Loop over files and reconstruct filename
do j=1,ny
if (j-1.lt.10) then
write(dum1,'(I1.1)') j-1
ysuf="000"//dum1
else
write(dum2,'(I2.1)') j-1
ysuf="00"//dum2
end if
do i=1,nx
if (i-1.lt.10) then
write(dum1,'(I1.1)') i-1
xsuf="000"//dum1
else
write(dum2,'(I2.1)') i-1
xsuf="00"//dum2
end if
nm=trim(stem)//".ts."//ysuf//xsuf//".nc"
print*,trim(nm)
!*Open file
status=nf90_open(nm,nf90_nowrite,ncid)
if (status.ne.nf90_noerr) print*,nf90_strerror(status)
!*Get Variable names, dims, attributes. Do this only once at it is the same for all files
!*Also set the flag according to compute mean (0), max (2), min (3), sum (1)
if ((i.eq.1).and.(j.eq.1)) then
flag(:)=0
status=nf90_inquire(ncid,ndim,nvar)
if (status.ne.nf90_noerr) print*,nf90_strerror(status)
status=nf90_inquire_dimension(ncid,1,dimname,ntr)
if (status.ne.nf90_noerr) print*,nf90_strerror(status)
print*,'There are ',ntr,' time steps and ',nvar, 'variables in the file'
do k=1,nvar
status=nf90_inquire_variable(ncid,k,name)
if (status.ne.nf90_noerr) print*,nf90_strerror(status)
namout(k)=name
!*Assume the attribute names are longname and units
status=nf90_get_att(ncid,k,'longname',lname(k))
if (status.ne.nf90_noerr) print*,nf90_strerror(status)
status=nf90_get_att(ncid,k,'units',uname(k))
if (status.ne.nf90_noerr) print*,nf90_strerror(status)
if (status.ne.nf90_noerr) print*,nf90_strerror(status)
status=nf90_get_att(ncid,k,'_FillValue',fval(k))
if (status.ne.nf90_noerr) then
print*,nf90_strerror(status)
fval(k) = -999.
end if
do kk=1,nma
if (trim(name).eq.trim(maxnms(kk))) flag(k)=2
end do
do kk=1,nmi
if (trim(name).eq.trim(minnms(kk))) flag(k)=3
end do
do kk=1,nsu
if (trim(name).eq.trim(sumnms(kk))) flag(k)=1
end do
end do
end if
!*Read variable in var
do k=1,nvar
status=nf90_get_var(ncid,k,var(k,1:ntr))
if (status.ne.nf90_noerr) print*,nf90_strerror(status)
end do
!*Close file
status=nf90_close(ncid)
if (status.ne.nf90_noerr) print*,nf90_strerror(status)
!2. Compute max, min, sum or average
!----------------------------------
do k=1,nvar
if (flag(k).le.1) then
do l=1,ntr
if (var(k,l).gt.-999.) then
varout(k,l)=varout(k,l)+var(k,l)
cnt(k,l)=cnt(k,l)+1
end if
end do
else
if (flag(k).eq.2) then
do l=1,ntr
if ((varout(k,l).eq.0).or.(var(k,l).gt.varout(k,l))) varout(k,l)=var(k,l)
end do
else
! Christopher: correct bug
varout(k,:) = -999.
do l=1,ntr
!varout(k,l) = -999.
!if ((varout(k,l).eq.0).or.((var(k,l).gt.-999).and.(var(k,l).lt.varout(k,l)))) varout(k,l)=var(k,l)
if ((varout(k,l).eq.-999.).or.(var(k,l).lt.varout(k,l))) varout(k,l)=var(k,l)
end do
endif
end if
end do
end do
end do
!*End of looping over the files
do k=1,nvar
if (flag(k).eq.0) then
varout(k,:)=varout(k,:)/cnt(k,:)
where (cnt(k,:) == 0)
varout(k,:) = -999.
endwhere
end if
end do
!3 Write new variables in *.ts.nc
!---------------------------------
!* Enter define mode
nm2=trim(stem)//".ts.nc"
print*,nm2
status=nf90_create(nm2, nf90_CLOBBER,ncidw)
if (status.ne.nf90_noerr) print*,nf90_strerror(status)
!* Define dimenstions
status=nf90_def_dim(ncidw,'time',nf90_unlimited,timeid)
if (status.ne.nf90_noerr) print*,nf90_strerror(status)
!* Define and store variable
do k=1,nvar
!Christopher: change unit
!lmax (kg/kg --> g/kg)
if (trim(namout(k)).eq.'lmax') then
uname(k)='g/kg'
varout(k,1:ntr) = varout(k,1:ntr)*1000.
endif
!b_flux (m^2/s^3)
if (trim(namout(k)).eq.'b_flux') then
uname(k)='m^2/s^3'
endif
!Christopher: end
status=nf90_def_var(ncidw,trim(namout(k)),NF90_float,(/timeid/),varid)
if (status.ne.nf90_noerr) print*,nf90_strerror(status)
status=nf90_put_att(ncidw,varid,'longname',trim(lname(k)))
if (status.ne.nf90_noerr) print*,nf90_strerror(status)
status=nf90_put_att(ncidw,varid,'_FillValue',-999.0)
if (status.ne.nf90_noerr) print*,nf90_strerror(status)
status=nf90_put_att(ncidw,varid,'units',trim(uname(k)))
if (status.ne.nf90_noerr) print*,nf90_strerror(status)
status=nf90_enddef(ncid)
if (status.ne.nf90_noerr) print*,nf90_strerror(status)
status=nf90_put_var(ncidw,varid,varout(k,1:ntr))
if (status.ne.nf90_noerr) print*,nf90_strerror(status)
status=nf90_redef(ncid)
if (status.ne.nf90_noerr) print*,nf90_strerror(status)
end do
status=nf90_enddef(ncid)
if (status.ne.nf90_noerr) print*,nf90_strerror(status)
!* Close file
status=nf90_close(ncid)
if (status.ne.nf90_noerr) print*,nf90_strerror(status)
end