Skip to content

Commit a359461

Browse files
committed
first release for importing vegetation binary files on leibniz
1 parent 0ce071b commit a359461

2 files changed

Lines changed: 191 additions & 0 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
__pycache__/vfc.cpython-37.pyc

vfc.py

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
import numpy as np
2+
from scipy.signal import fftconvolve
3+
4+
5+
################################################################################
6+
7+
def convolution_simple(veg, mask, dx):
8+
9+
"""
10+
veg: numpy array of shape (nx, ny)
11+
mask: numpy array of shape (mx, my)
12+
dx: grid size [m]
13+
14+
return:
15+
vh: numpy array of shape (nx, ny)
16+
"""
17+
18+
# scaling factor
19+
fs = fftconvolve(veg, np.ones(mask.shape, dtype = float), mode = 'same') * \
20+
(dx ** 2)
21+
fs[fs < 1] = 1
22+
23+
# convolution product
24+
vh = np.exp(fftconvolve(veg / (fs ** .5), np.log(mask), mode = 'same'))
25+
26+
return vh
27+
28+
29+
################################################################################
30+
31+
def mask_diffusion_1d(mask, s, axis = 1):
32+
33+
"""
34+
mask: numpy array of shape (nx, ny)
35+
s: diffusion number (s = nu * dt / dx ** 2)
36+
nu: diffusion coefficient [m^2/s]
37+
dt: time step [s]
38+
dx: grid size [m]
39+
axis: axis along which the 1d diffusion is applied
40+
41+
return:
42+
mask: numpy array of shape (nx, ny)
43+
"""
44+
45+
# rotate mask
46+
if axis == 0:
47+
mask = mask.T
48+
49+
# number of iterations
50+
smax = .5
51+
if s < smax:
52+
n = 1
53+
else:
54+
n = np.int(np.floor(s / smax)) + 1
55+
56+
# iteration diffusion number
57+
ss = s / n
58+
59+
# for each iteration
60+
for i in range(n):
61+
62+
# no-flux boundary condition
63+
mask[:, 0] = 4 / 3 * mask[:, 1] - 1 / 3 * mask[:, 2]
64+
mask[:, -1] = 4 / 3 * mask[:, -2] - 1 / 3 * mask[:, -3]
65+
66+
# inside domain
67+
mask[:, 1:-1] += ss * (mask[:, :-2] - 2 * mask[:, 1:-1] + mask[:, 2:])
68+
69+
# rotate mask
70+
if axis == 0:
71+
mask = mask.T
72+
73+
return mask
74+
75+
76+
################################################################################
77+
78+
def mask_export(filename, mask, dx):
79+
80+
"""
81+
filename: file name in which mask will be exported
82+
mask: numpy array of shape (nx, ny)
83+
dx: mask grid size
84+
"""
85+
86+
# open file
87+
file = open(filename, 'w')
88+
89+
# write header
90+
np.array(mask.shape[0], dtype = int).tofile(file)
91+
np.array(mask.shape[1], dtype = int).tofile(file)
92+
np.array(dx, dtype = float).tofile(file)
93+
94+
# write data
95+
np.array(mask, dtype = float).tofile(file)
96+
97+
# close file
98+
file.close()
99+
100+
101+
################################################################################
102+
103+
def mask_import(filename):
104+
105+
"""
106+
filename: file name from which mask will be imported
107+
108+
return:
109+
mask: numpy array of shape (nx, ny)
110+
dx: mask grid size
111+
"""
112+
113+
# open file
114+
file = open(filename, 'r')
115+
116+
# read header
117+
nx = np.fromfile(file, dtype = int, count = 1)[0]
118+
ny = np.fromfile(file, dtype = int, count = 1)[0]
119+
dx = np.fromfile(file, dtype = float, count = 1)[0]
120+
121+
# read data
122+
mask = np.reshape(np.fromfile(file, dtype = float, count = nx * ny), (nx, ny))
123+
124+
# close file
125+
file.close()
126+
127+
# return
128+
return [mask, dx]
129+
130+
131+
################################################################################
132+
133+
def veg_export(filename, veg, x0, y0, dx):
134+
135+
"""
136+
filename: file name in which mask will be exported
137+
veg: numpy array of shape (nx, ny)
138+
x0, y0: center coordinates of the lower left cell
139+
dx: mask grid size
140+
"""
141+
142+
# open file
143+
file = open(filename, 'w')
144+
145+
# write header
146+
np.array(x0, dtype = float).tofile(file)
147+
np.array(y0, dtype = float).tofile(file)
148+
np.array(veg.shape[0], dtype = int).tofile(file)
149+
np.array(veg.shape[1], dtype = int).tofile(file)
150+
np.array(dx, dtype = float).tofile(file)
151+
152+
# write data
153+
np.array(veg, dtype = int).tofile(file)
154+
155+
# close file
156+
file.close()
157+
158+
159+
################################################################################
160+
161+
def veg_import(filename):
162+
163+
"""
164+
filename: file name from which mask will be imported
165+
166+
return:
167+
veg: numpy array of shape (nx, ny)
168+
x0, y0: center coordinates of the lower left cell
169+
dx: mask grid size
170+
"""
171+
172+
# open file
173+
file = open(filename, 'r')
174+
175+
# read header
176+
x0 = np.fromfile(file, dtype = float, count = 1)[0]
177+
y0 = np.fromfile(file, dtype = float, count = 1)[0]
178+
nx = np.fromfile(file, dtype = int, count = 1)[0]
179+
ny = np.fromfile(file, dtype = int, count = 1)[0]
180+
dx = np.fromfile(file, dtype = float, count = 1)[0]
181+
182+
# read data
183+
veg = np.reshape(np.fromfile(file, dtype = int, count = nx * ny), (nx, ny))
184+
185+
# close file
186+
file.close()
187+
188+
# return
189+
return [veg, x0, y0, dx]

0 commit comments

Comments
 (0)