Skip to content

Commit 80ee318

Browse files
authored
Merge pull request #14103 from mcgratta/master
Python: Add layer_height.py
2 parents 51857bc + 62d8af6 commit 80ee318

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
import os
2+
import numpy as np
3+
4+
validation_dir = '../../Validation/'
5+
output_base_dir = '../../../out/'
6+
7+
list_dir = os.listdir(validation_dir)
8+
output_dir = []
9+
input_file = []
10+
k = 0
11+
12+
for name in list_dir:
13+
output_directory = os.path.join(output_base_dir, name)
14+
if os.path.exists(output_directory) and os.path.isdir(output_directory):
15+
list_files = [f for f in os.listdir(output_directory) if f.endswith('HGL.input')]
16+
if list_files:
17+
for file_name in list_files:
18+
if not file_name.startswith('.'): # ignore hidden files
19+
k += 1
20+
output_dir.append(os.path.join(output_base_dir, name) + '/')
21+
input_file.append(file_name)
22+
23+
# Uncomment the following line to just list the files for testing purposes
24+
# exit()
25+
26+
ntd = 20000
27+
ncd = 500
28+
29+
for idx in range(len(input_file)): # input_file loop
30+
tmp = np.zeros((ntd, ncd))
31+
32+
input_path = os.path.join(output_dir[idx], input_file[idx])
33+
with open(input_path, 'r') as fid:
34+
# Read number of trees
35+
ntrees = int(fid.readline())
36+
37+
# Read number of TCs in the tree
38+
ntc = int(fid.readline())
39+
ztc = np.zeros(ntc)
40+
icol = np.zeros((ntc, ntrees), dtype=int)
41+
for n in range(ntc):
42+
line = fid.readline()
43+
S = line.strip().split()
44+
ztc[n] = float(S[0])
45+
for nn in range(ntrees):
46+
icol[n, nn] = int(S[nn + 1]) - 1
47+
48+
# Read weight of each tree
49+
line = fid.readline()
50+
S = line.strip().split()
51+
wgt = np.array([float(s) for s in S[:ntrees]])
52+
53+
# Read data file name
54+
infile = fid.readline().strip()
55+
56+
# Read number of columns in data file
57+
nc = int(fid.readline())
58+
59+
# Read row number where data starts
60+
nr = int(fid.readline())
61+
62+
# Read ceiling height
63+
z_h = float(fid.readline())
64+
65+
# Read starting time
66+
t_start = float(fid.readline())
67+
68+
# Read name of output file
69+
outfile = fid.readline().strip()
70+
71+
# Read data from file
72+
data_path = os.path.join(output_dir[idx], infile)
73+
M = np.loadtxt(data_path, delimiter=',', skiprows=nr-1)
74+
t = M[:, 0]
75+
d = M[:, 1:nc]
76+
77+
z = np.zeros(ntc)
78+
for n in range(ntc - 1):
79+
z[n] = (ztc[n] + ztc[n + 1]) / 2
80+
z[-1] = z_h
81+
82+
fout_path = os.path.join(output_dir[idx], outfile)
83+
with open(fout_path, 'w') as fout:
84+
fout.write('Time, Height, T_lower, T_upper\n')
85+
86+
for i in range(len(t)): # time loop
87+
if t[i] < t_start:
88+
continue
89+
tmp[i, :] = 0
90+
for nn in range(ntrees):
91+
for n in range(ntc):
92+
tmp[i, n] += (273 + d[i, icol[n, nn] - 1]) * wgt[nn]
93+
94+
i1 = 0
95+
i2 = 0
96+
for n in range(ntc):
97+
if n == 0:
98+
i1 += tmp[i, n] * (z[n] - 0)
99+
i2 += (1.0 / tmp[i, n]) * (z[n] - 0)
100+
else:
101+
i1 += tmp[i, n] * (z[n] - z[n - 1])
102+
i2 += (1.0 / tmp[i, n]) * (z[n] - z[n - 1])
103+
zint = tmp[i, 0] * (i1 * i2 - z_h**2) / (0.00001 + i1 + i2 * tmp[i, 0]**2 - 2 * tmp[i, 0] * z_h)
104+
tmpl = tmp[i, 0]
105+
i1 = 0
106+
for n in range(ntc):
107+
if n == 0:
108+
if z[n] > zint:
109+
if 0 >= zint:
110+
i1 += tmp[i, n] * (z[n] - 0)
111+
if 0 < zint:
112+
i1 += tmp[i, n] * (z[n] - zint)
113+
else:
114+
if z[n] > zint:
115+
if z[n - 1] >= zint:
116+
i1 += tmp[i, n] * (z[n] - z[n - 1])
117+
if z[n - 1] < zint:
118+
i1 += tmp[i, n] * (z[n] - zint)
119+
tmph = max(tmpl, (1.0 / (z_h - zint)) * i1)
120+
121+
fout.write(f'{t[i]}, {zint}, {tmpl - 273}, {tmph - 273}\n')
122+

0 commit comments

Comments
 (0)