forked from JohanvandeKoppel/clAridlands
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlotArid.r
executable file
·213 lines (158 loc) · 7.59 KB
/
PlotArid.r
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
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Plotting script for the CUDA implementation of the #
# Arid bushlands patterns model of Rietkerk et al 2002 #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
remove(list=ls()) # Remove all variables from memory
on=1;off=0;
setwd('/Simulations/OpenCL/clAridLands/clAridLands')
require(fields)
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Program settings and parameters
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
Movie=off
Wait=off
AllWindows=off
SmallMovie=off
DisplayEvolution=off
DPI=144
Width = 250*0.5
if (AllWindows==on){
WinWidth = 1440
WinHeight = 600
if (SmallMovie==on){
Resolution='960x400'
} else{
Resolution="1440x600"
}
}else {
WinWidth = 960
WinHeight = 720
Resolution= '960x720'
}
# Graphical parameters & palette definitions
ColorPalette = function(x)rev(terrain.colors(x))
water.palette = colorRampPalette(c("white", "blue"))
ColorPalette = colorRampPalette(c("#cd9557", "#f8e29f", "#82A045", "#628239", "#506736","#385233"))
# The maximal value of P, O and W to be plotted. If one goes over that, the value is capped
PGraphMax = 20
OGraphMax = 20
WGraphMax = 20
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Reading the parameters from the data file and declaring variables
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
FID = file("AridLands.dat", "rb")
NX= readBin(FID, integer(), n = 1, endian = "little");
NY = readBin(FID, integer(), n = 1, endian = "little");
NumFrames = readBin(FID, integer(), n = 1, endian = "little");
EndTime = readBin(FID, integer(), n = 1, endian = "little");
P_in_Time = O_in_Time = W_in_Time = 1:NumFrames
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Opening a window and starting the display loop
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
if (Movie==off)
quartz(width=WinWidth/DPI, height=WinHeight/DPI, dpi=DPI)
for (jj in 0:(NumFrames-1)){ # Here the time loop starts
# If a movie is to be made, frames are written as jpegs
if (Movie==on)
jpeg(filename = sprintf("Images/Rplot%03d.jpeg",jj),
width = WinWidth, height = WinHeight,
units = "px", pointsize = 24,
quality = 100,
bg = "white", res = NA,
type = "quartz")
# Reading the data from the files
Data_P = matrix(nrow=NY, ncol=NX, readBin(FID, numeric(), size=4, n = NX*NY, endian = "little"));
Data_O = matrix(nrow=NY, ncol=NX, readBin(FID, numeric(), size=4, n = NX*NY, endian = "little"));
Data_W = matrix(nrow=NY, ncol=NX, readBin(FID, numeric(), size=4, n = NX*NY, endian = "little"));
# recording means values per recorded frame
P_in_Time[jj+1] = mean(Data_P)
O_in_Time[jj+1] = mean(Data_O)
W_in_Time[jj+1] = mean(Data_W)
if (AllWindows==on){
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Displaying the 3 combined plots for P, O, and W
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# First figure, setting margin, plotting, and adding a title
par(mar=c(1, 1, 3, 1), mfrow=c(1,3))
image.plot(pmin(Data_P,PGraphMax), zlim=c(0,PGraphMax), xaxt="n", yaxt="n", horizontal = T,
col = ColorPalette(255),asp=1, bty="n", useRaster=TRUE,
legend.shrink = 0.93, legend.width = 0.5)
title(expression(bold(Vegetation~(~g~m^-2))), line=1.5)
# Second figure, seting margin, plotting, adding title and counter text
par(mar=c(mar=c(1, 1, 3, 1)))
image.plot(pmin(Data_O,OGraphMax), zlim=c(0,OGraphMax), xaxt='n', yaxt="n", horizontal = T,
col = water.palette(255),asp=1, bty="n", useRaster=TRUE,
legend.shrink = 0.93, legend.width = 0.5)
title(expression(bold(Surface~water~(mm))), line=1.5)
mtext(text=paste("Time : ",sprintf("%1.0f",(jj+1)/NumFrames*EndTime),
"of" ,sprintf("%1.0f",EndTime), "days"),
side=1, adj=0.5, line=5, cex=0.7)
# Last figure, setting margin, plotting, and adding a title
par(mar=c(mar=c(1, 1, 3, 1)))
image.plot(pmin(Data_W,WGraphMax), zlim=c(0,WGraphMax), yaxt="n", xaxt="n", horizontal = T,
col = water.palette(255), asp=1, bty="n", useRaster=TRUE,
legend.shrink = 0.93, legend.width = 0.5)
title(expression(bold(Soil~water~(mm))), line=1.5)
# Adding the scale bar and text
axis(side=1, at=c(0.4,0.6), line=4.9, labels = c(0,Width/5),
cex.axis=1, tck = -0.02, mgp=c(3, .5, 0))
mtext(text="Scale (m): ", side=1, adj=0.23, line=5, cex=0.7)
}else{
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Displaying the vegetation plot only
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Seting margin, plotting, adding title and counter text
par(mar=c(1.5, 4, 2, 6) + 0.1)
image.plot(pmin(Data_P,PGraphMax), zlim=c(0,PGraphMax), xaxt="n", yaxt="n",
col = ColorPalette(255),asp=1, bty="n", useRaster=TRUE,
legend.shrink = 0.95, legend.width = 1, axis.args=list(cex=0.2),
legend.args=list(text=expression(Biomass~(~g~m^-2)),
cex=1, line=0.5))
title('Arid vegetation', line=0.5, cex=0.7)
mtext(text=paste("Time : ",sprintf("%1.0f",(jj+1)/NumFrames*EndTime),
"of" ,sprintf("%1.0f",EndTime), "days"),
side=1, line=0, cex=1)
# Adding the scale bar and text
axis(side=1, at=c(0.8,1), line=0, labels = c(0,Width/5),
cex.axis=0.7, tck = -0.015, mgp=c(3, .25, 0))
mtext(text="Scale (m)", side=1, adj=1.28, line=0, cex=1)
}
# Finishing JPEG, or updating graph
if (Movie==on) dev.off() else {
dev.flush()
dev.hold()
}
# For debugging, this lets you go frame by frame
if (Wait==on){
cat ("Press [enter] to continue, [q] to quit")
line <- readline()
if (line=='q'){ stop() }
}
}
# Closing Aridlands.dat
close(FID)
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Building the movie via ffmpeg
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
if (Movie==on) {
# Building command line to run ffmpeg
InFiles=paste(getwd(),"/Images/Rplot%03d.jpeg", sep="")
OutFile="AridLands.mp4"
CmdLine=sprintf("ffmpeg -y -r 25 -i %s -s %s -c:v libx264 -pix_fmt yuv420p -b:v 5000k %s",
InFiles, Resolution, OutFile)
# Executing the command
cmd = system(CmdLine)
# Unhash to immediately display the movie
# if (cmd==0) try(system(paste("open ", paste(getwd(),"Mussels_PDE.mp4"))))
}
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Display the evolution in time if needed
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
if (DisplayEvolution==on){
quartz(width=1920/DPI, height=800/DPI, dpi=DPI)
plot(x=RecordTimes[-(NumFrames+1)],y=P_in_Time,type='l', bty='n', col='green', ylim=c(0,20),
xlab='Time (days)', ylab='Biomass/mm water')
lines(RecordTimes[-(NumFrames+1)],O_in_Time, col='cyan')
lines(RecordTimes[-(NumFrames+1)],W_in_Time, col='darkblue')
}
system('say All ready')