-
Notifications
You must be signed in to change notification settings - Fork 0
/
LandscapeHelper.jl
177 lines (167 loc) · 4.69 KB
/
LandscapeHelper.jl
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
function GetData(LandscapeNo, repl,NoiseSeries)
file="inData/Landscapes/L"*string(LandscapeNo)*"/XY"*string(repl)*".h5"
XY=h5open(file,"r") do file
read(file,"XY")
end
Ext=h5open(file,"r") do file
read(file,"Extent")
end
file="inData/Noise/N"*string(NoiseSeries)*".h5"
N=h5open(file,"r") do file
read(file,"N")
end
return XY,N,Ext
end
function GetData(p::par)
file=p.inData*"/Landscapes/L"*string(p.NoLandscape)*"/XY"*string(p.repl)*".h5"
p.XY=h5open(file,"r") do file
read(file,"XY")
end
p.NoSites=size(p.XY,2)
Ext=h5open(file,"r") do file
read(file,"Extent")
end
p.extent=Ext[1]
file=p.inData*"/Noise/N"*string(p.NoNoise)*".h5"
p.noise=h5open(file,"r") do file
read(file,"N")
end
p.tempGrad=20-((p.XY[2,:]/p.extent)*2-1.0)*p.tempSlope*p.extent
connectivity(p)
D=pairwise(Euclidean(),p.XY,p.XY)
D=D+eye(D)*maximum(D)
p.sDist=minimum(p.XY[2,:])-mean(minimum(D,1))
return p
end
function makeXYLandscape(LandscapeNo::Int64,repl::Int64,NoSites::Int64,Extent::Float64,_save::Int64)
if NoSites>0
XY=rand(Float64,2,NoSites).*Extent #XY coordinates in a 0:1 range
else
NoSites=abs(NoSites)
XY=[zeros(NoSites)'; collect(linspace(0,Extent,NoSites))'] # for making 1D landscapes
end
if _save==1
if isdir("inData/Landscapes/L"*string(LandscapeNo))
else
mkdir("inData/Landscapes/L"*string(LandscapeNo))
end
file="inData/Landscapes/L"*string(LandscapeNo)*"/XY"*string(repl)*".h5"
A=h5open(file,"w") do file
write(file,"XY", XY)
write(file,"Extent", Extent)
end
else
return XY
end
end
function makeNoise(NoiseNo::Int64,Tend::Int64,_save::Int64)
# No evidence of year to year autocorrelation
# http://journals.ametsoc.org/doi/pdf/10.1175/1520-0493%281977%29105%3C0009%3AEOTAAS%3E2.0.CO%3B2
# Generally normal distributed year to year anomalies
# http://www.giss.nasa.gov/research/briefs/hansen_17/
# average of 0.5 st.d.
# http://www.nature.com/nature/journal/v500/n7462/pdf/nature12310.pdf
N=randn(1,Tend)*0.5 #
if _save==1
if isdir("inData/Noise")
else
mkdir("inData/Noise")
end
file="inData/Noise/N"*string(NoiseNo)*".h5"
A=h5open(file,"w") do file
write(file,"N", N)
end
else
return N
end
end
function getP()
getP(1,1,1,600,1,0.8)
end
function getP(NoLandscape::Int64,repl::Int64,NoNoise::Int64,Tend::Int64,Poisson::Int64,alpha::Float64)
NoSpecies=100
XY,N,extent=GetData(NoLandscape,repl,NoNoise)
NoSites=size(XY,2)
r=0.1
m=0.05*r
ext=1.0e-10 #one seed per total plant biomass of wetland
reprod=0.05
dispersalAlpha=20.0
dispersalC=0.5
LDDED=0.0
LDDVD=0.0
overWinter=0.3
seedPerBiomass=1000*100*100
TWidth=90.0
z=collect(linspace(0,40,NoSpecies))
sDist=0.
C=connectivity(XY,dispersalAlpha,dispersalC)
CCstart=200
CCamp=5.0
CCk=75.0
tempSlope=1.0/100000.0
tempGrad=20-((XY[2,:]/extent)*2-1.0)*tempSlope*extent
inData="inData"
outData="outData"
p=par(NoSpecies,NoSites,NoLandscape,Tend,repl,NoNoise,Poisson,r,m,ext,alpha,reprod,dispersalAlpha,dispersalC,LDDED,LDDVD,overWinter,seedPerBiomass,extent,TWidth,z,XY,sDist,C,CCstart,CCamp,CCk,tempSlope,tempGrad,N,inData,outData)
end
function moments(X,p)
T=size(X,1)
S=size(X,2)
N=size(X,3)
M=zeros(Float64,T,N,5)
X[isnan(X)]=0.0
for t=1:T
for n=1:N
M[t,n,1]=sum(X[t,:,n])
M[t,n,2]=sum(p.z.*X[t,:,n]')./M[t,n,1]
M[t,n,3]=sum(p.z.*(M[t,n,2]'-p.z).^2)./M[t,n,1]
M[t,n,4]=sum(p.z.*(M[t,n,2]'-p.z).^3)./M[t,n,1]
M[t,n,5]=sum(p.z.*(M[t,n,2]'-p.z).^4)./M[t,n,1]
end
end
return M
end
function extractX(X,a::Int64)
for i=1:size(X,1)
for j=1:size(X,2)
for k=1:size(X,3)
end
end
end
end
function Base.show(io::IO, E::par) # funciton to display par type
N=fieldnames(E)
println()
println("Simulation parameters")
println("--------------------------------")
for i in 1:length(N)
if (typeof(E.(N[i]))==Int64 || typeof(E.(N[i]))==Float64)
println(io,N[i]," (",E.(N[i]),")")
else
println(io,N[i]," (",typeof(E.(N[i])),")")
end
end
end
function getResult(L,P,f)
file=f*"/out"*string(L)*"_"*string(P)*".h5"
X=h5open(file,"r") do file
read(file,"X")
end
xcc=h5open(file,"r") do file
read(file,"XCS")
end
Tactual=h5open(file,"r") do file
read(file,"Tactual")
end
SDX=h5open(file,"r") do file
read(file,"SDX")
end
IE=h5open(file,"r") do file
read(file,"IE")
end
ISD=h5open(file,"r") do file
read(file,"IE")
end
return X,xcc,Tactual,SDX,IE,ISD
end