-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathdineof_utils.F90
291 lines (188 loc) · 7.03 KB
/
dineof_utils.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#include "ppdef.h"
module dineof_utils
contains
!---------------------------------------------------------
!----- subroutine valsvd ---------------------------------
!---------------------------------------------------------
! computes the missing data values from the EOF basis
!
!---------------------------------------------------------
subroutine valsvd(U,S,V,X,IEX,JEX,VAL,NN,IMISST)
implicit none
integer,intent(in) :: IMISST
real, intent(in) :: U(:,:),V(:,:),S(:,:)
real, intent(inout) :: X(:,:)
integer,intent(in) :: NN,IEX(:),JEX(:)
real,intent(out) :: VAL
integer :: K,t
do t=1,IMISST
VAL=0
DO K=1,NN
VAL=VAL+U(IEX(t),K)*S(K,1)*V(JEX(t),K)
ENDDO
X(IEX(t),JEX(t))=VAL
enddo
end subroutine
!cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
subroutine dindiff(x,B,alpha,numit)
use ufileformat
implicit none
real,intent(inout) :: B(:)
real,intent(in) :: x(:)
real, pointer :: BF(:),xe(:)
integer :: nsize,k,numit
real :: alpha,valex
nsize = size(B,1)
allocate(BF(size(B,1)+1))
allocate(xe(size(x,1)+1))
BF=0
! extended x
xe(1) = 1.5*x(1) - .5 * x(2)
xe(2:nsize) = (x(1:nsize-1) + x(2:nsize))/2
xe(nsize+1) = 1.5*x(nsize) - .5 * x(nsize-1)
do k=1,numit
! F(2:nsize) = alpha * (f(2:nsize) - f(1:nsize-1));
! f = f + (F(2:nsize+1) - F(1:nsize));
BF(2:nsize) = alpha * (B(2:nsize) - B(1:nsize-1))/(x(2:nsize) - x(1:nsize-1))
B = B + (BF(2:nsize+1) - BF(1:nsize))/ (xe(2:nsize+1) - xe(1:nsize))
end do
deallocate(BF,xe)
end subroutine dindiff
!cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
!---------------------------------------------------------
!----- subroutine writeMatrix ----------------------------
!---------------------------------------------------------
! reshapes matrix into 3D file, then writes results
! into separate files
!
!---------------------------------------------------------
subroutine writeMatrix(X,xmean,valex,resultfnames,maskfile,norma,imax,jmax,first,DirOutput,fileMean,fileStd)
use ufileformat
use initfile
implicit none
real,intent(in) :: X(:,:)
real,intent(in),optional :: fileMean(:),fileStd(:)
integer,intent(in) :: imax(:),jmax(:),first(:)
real,intent(in) :: xmean
real,intent(inout) :: valex
integer,intent(in) :: norma
character(len=200),intent(in) :: resultfnames(:),maskfile(:)
character(len=200),intent(in) :: DirOutput
real, allocatable :: sst(:,:,:)
real,pointer :: mask(:,:)
real, parameter :: valexc = 9999.
real :: mean,stddev,var
integer :: i,j,s,t,q,nbvars,NN,N
character(len=200) :: initfilename
N = size(X,2)
nbvars=size(resultfnames)
call getarg(1,initfilename)
!Reshape the matrix into a 3D matrix (two spatial dimensions and one temporal dimension)
!---------------------------------------------------------------------------------------
do q=1,nbvars
allocate(sst(imax(q),jmax(q),N))
if( presentInitValue(initfilename,'mask')) then
call uload(maskfile(q),mask,valex)
else
allocate(mask(size(sst,1),size(sst,2)))
mask = mask+1
endif
where (mask.eq.0) mask = valexc
do t=1,N
s = first(q)
do i=1,imax(q)
do j=1,jmax(q)
if(mask(i,j).ne.valexc) then
sst(i,j,t)=X(s,t);
if(norma.eq.1) then
sst(i,j,t)=(sst(i,j,t)*fileStd(q))+fileMean(q)
end if
s = s+1
else
sst(i,j,t)=valexc;
endif
enddo
enddo
enddo
NN = count(sst.ne.valexc)
mean = sum(sst,sst.ne.valexc)/NN
var = sum((sst-mean)**2,sst.ne.valexc)/NN
stddev = sqrt(var)
if(norma.eq.1) then
! -----------------------------------------
! some statistics about the filled matrices
! -----------------------------------------
write(stdout,*)
write(stdout,*)'mean ',mean
write(stdout,*)'Standard deviation',stddev,fileStd,fileMean
write(stdout,*)
end if
call usave(trim(resultfnames(q)),sst(:,:,:),valexc)
deallocate(sst,mask)
enddo
end subroutine writeMatrix
!---------------------------------------------------------
!----- subroutine writeSVD ----------------------------
!---------------------------------------------------------
! writes EOFs
!
!---------------------------------------------------------
subroutine writeSVD(DirOutput,eofvfname,eofsfname,s,v,valc,sumVarEnd)
use ufileformat
use netcdf
implicit none
character(len=200),intent(in) :: DirOutput,eofvfname,eofsfname
real,intent(in) :: s(:),v(:,:),valc(:),sumVarEnd
integer :: i,j,k,ncid,status
real, parameter :: valex = 9999.
real, parameter :: valexc = 9999.
real :: varex(size(s))
! call usave(trim(resultfnames(q)),sst(:,:,:),valexc)
status = nf90_create(path = trim(DirOutput)//'/DINEOF_diagnostics.nc', cmode = nf90_clobber, ncid = ncid)
if (status /= nf90_noerr) call handle_err(status)
status = nf90_close(ncid)
if (status /= nf90_noerr) call handle_err(status)
! Variance detailed
! _______________
varex= 100.0*s**2/sumVarEnd
call usave(trim(DirOutput)//'/DINEOF_diagnostics.nc#varEx',varex,valex)
write(stdout,*) 'Sum of the squares of the singular values of the ',size(s), 'EOFs retained', sum(varex)
!
! Singular values
! _______________
!call usave(trim(DirOutput)//'/DINEOF_diagnostics.nc#vlsng',s,valex)
call usave(eofsfname,s,valex)
!
! Temporal modes
! _______________
call usave(eofvfname,v,valex)
!---------------------------------------------------------------------------------------
! Valc: write expected error for each mode
! ----------------
call usave(trim(DirOutput)//'/DINEOF_diagnostics.nc#valc',valc,valexc)
!---------------------------------------------------------------------------------------
!stop
500 format(a72)
501 format(e10.4)
502 format(3e10.4)
503 format(300(e10.4,1X))
504 format(a5,i3,a3,f8.4,a2)
end subroutine writeSVD
subroutine clear_files(filenames)
use netcdf
character(len=200),intent(in) :: filenames
integer :: status,ncid
status = nf90_create(path = trim(filenames), cmode = nf90_clobber, ncid = ncid)
if (status /= nf90_noerr) call handle_err(status)
status = nf90_close(ncid)
if (status /= nf90_noerr) call handle_err(status)
end subroutine clear_files
subroutine handle_err(status)
use netcdf
integer, intent ( in) :: status
if(status /= nf90_noerr) then
print *, trim(nf90_strerror(status))
stop "Stopped"
end if
end subroutine handle_err
end module dineof_utils