-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_sim_tds.py
More file actions
284 lines (206 loc) · 8.65 KB
/
create_sim_tds.py
File metadata and controls
284 lines (206 loc) · 8.65 KB
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
import numpy as np
import matplotlib.pyplot as plt
import random
import csv
import h5py
import os
from tqdm import tqdm
def main():
root_dir = os.getcwd()
albedo_table = load_albedo(root_dir, 'albedo.csv')
# Uncomment the function that you want to run
eight_categories(root_dir, albedo_table)
# three_categories_random(root_dir, albedo_table)
# three_categories_determine(root_dir, albedo_table)
def eight_categories(write_dir, albedo_table):
num_obs = 64000
surf_frac = sample_surf_dist(num_obs, 3)
# Remove all generated fractions that have an absolute or relative melt pond fraction
# higher than 60%
s = 0
while s < len(surf_frac):
if surf_frac[s][1] > .65:
surf_frac = np.delete(surf_frac, s, 0)
elif surf_frac[s][1] / (surf_frac[s][1] + surf_frac[s][0]) > 0.65:
surf_frac = np.delete(surf_frac, s, 0)
else:
s += 1
num_obs = len(surf_frac)
obs_refl = np.zeros((num_obs, 6)) # 6 MODIS BANDS USED
for i in tqdm(range(num_obs)):
fi, fp, fo = surf_frac[i]
# Find the subcategory distributions
sub_frac_ice, sub_frac_pond = calc_subfrac_8cat()
for b in range(6): # 7 MODIS 8 WV
# Find the three category reflectances using the subcategory distributions
ice_refl, pond_refl, ocean_refl = parse_8cat(b, albedo_table, sub_frac_ice, sub_frac_pond)
obs_refl[i, b] = ((ice_refl * fi) + (pond_refl * fp) + (ocean_refl * fo))
tds_name = os.path.join(write_dir, 'modis_artificial_tds_8cat.hdf')
write_hdf(tds_name, obs_refl, surf_frac)
def parse_8cat(b, albedo_table, sub_frac_ice, sub_frac_pond):
# Sample possible subsurfaces to find ice reflectance
ice_refl = 0
ice_refl += (albedo_table[b, 0] * sub_frac_ice[0]) # Cold Snow
ice_refl += (albedo_table[b, 1] * sub_frac_ice[1]) # Melting Snow
ice_refl += (albedo_table[b, 2] * sub_frac_ice[2]) # Deter. Melting Ice
ice_refl += (albedo_table[b, 3] * sub_frac_ice[3]) # Dirty Ice
# Sample possible subsurfaces to find pond reflectance
pond_refl = 0
pond_refl += (albedo_table[b, 4] * sub_frac_pond[0]) # Bright ponds
pond_refl += (albedo_table[b, 5] * sub_frac_pond[1]) # Early Ponds
pond_refl += (albedo_table[b, 6] * sub_frac_pond[2]) # Late Ponds
# Ocean has no subsurfaces
ocean_refl = albedo_table[b, 7]
return ice_refl, pond_refl, ocean_refl
def calc_subfrac_8cat():
# Subsurface distribution
sub_frac_ice = sample_surf_dist(1, 4)[0]
# Limit the amount of dirty or deteriorating ice.
while sub_frac_ice[3] > 0.1 or sub_frac_ice[2] > 0.2:
sub_frac_ice = sample_surf_dist(1, 4)[0]
sub_frac_pond = sample_surf_dist(1, 3)[0]
return sub_frac_ice, sub_frac_pond
def three_categories_random(write_dir, albedo_table):
num_obs = 64000
surf_frac = sample_surf_dist(num_obs, 3)
# Remove all generated fractions that have an absolute or relative melt pond fraction
# higher than 60%
s = 0
while s < len(surf_frac):
if surf_frac[s][1] > .65:
surf_frac = np.delete(surf_frac, s, 0)
elif surf_frac[s][1] / (surf_frac[s][1] + surf_frac[s][0]) > 0.65:
surf_frac = np.delete(surf_frac, s, 0)
else:
s += 1
num_obs = len(surf_frac)
obs_refl = np.zeros((num_obs, 8)) ##6 for MODIS
for i in tqdm(range(num_obs)):
fi, fp, fo = surf_frac[i]
for b in range(6):
# Find the three category reflectances using the subcategory distributions
ice_refl, pond_refl, ocean_refl = parse_3cat(b, albedo_table)
obs_refl[i, b] = ((ice_refl * fi) + (pond_refl * fp) + (ocean_refl * fo))
tds_name = os.path.join(write_dir, 'modis_artificial_tds_3cat_random.hdf')
write_hdf(tds_name, obs_refl, surf_frac)
def three_categories_determine(write_dir, albedo_table):
'''
Creates the training dataset with three surface categories
(using all possible fraction combinations)
:param write_dir:
:return:
'''
surf_frac = exhaustive_3cat_dist()
num_obs = len(surf_frac)
obs_refl = np.zeros((num_obs, 6))
for i in tqdm(range(num_obs)):
fi, flp, fo = surf_frac[i]
for b in range(6):
ir, lpr, ocr = parse_3cat(b, albedo_table)
obs_refl[i, b] = ((ir * fi) + (lpr * flp) + (ocr * fo))
tds_name = os.path.join(write_dir, 'modis_artificial_tds_3cat_determine.hdf')
write_hdf(tds_name, obs_refl, surf_frac)
# All possibilities with 3 surfaces
def exhaustive_3cat_dist():
surf_frac = []
for i in range(101):
for m in range(101-i):
if (m) / float(m+i+0.1) > 0.65:
continue
o = 100 - (i + m)
surf_frac.append([i, m, o])
surf_frac = np.divide(surf_frac, 100)
print(np.shape(surf_frac))
return surf_frac
def parse_3cat(b, albedo_table):
ice_refl = albedo_table[b, 0] # Cold Snow
pond_refl = albedo_table[b, 5] # Early Ponds
ocean_refl = albedo_table[b, 7]
return ice_refl, pond_refl, ocean_refl
def calc_subfrac_v1():
# Subsurface distribution
sub_frac_ice = sample_surf_dist(1, 5)[0]
while sub_frac_ice[4] > 0.3 or sub_frac_ice[3] > 0.2:
sub_frac_ice = sample_surf_dist(1, 5)[0]
sub_frac_pond = sample_surf_dist(1, 4)[0]
return sub_frac_ice, sub_frac_pond
def parse_v1(b, albedo_table, sub_frac_ice, sub_frac_pond):
# Sample possible subsurfaces to find ice reflectance
ice_refl = 0
ice_refl += (albedo_table[b, 0] * sub_frac_ice[0]) # Cold Snow
ice_refl += (albedo_table[b, 1] * sub_frac_ice[1]) # Melting Snow
ice_refl += (albedo_table[b, 2] * sub_frac_ice[2]) # Deter. Melting Ice
ice_refl += (albedo_table[b, 3] * sub_frac_ice[3]) # Undetr Melting Ice
ice_refl += (albedo_table[b, 7] * sub_frac_ice[4]) # Dirty Ice
# Sample possible subsurfaces to find pond reflectance
pond_refl = 0
pond_refl += (albedo_table[b, 4] * sub_frac_pond[0]) # B-G ice
pond_refl += (albedo_table[b, 5] * sub_frac_pond[1]) # EMP
pond_refl += (albedo_table[b, 6] * sub_frac_pond[2]) # LMP
# Add some more darkness to the pond options
pond_refl += (albedo_table[b, 8] * sub_frac_pond[3]) * 2. # Ocean
# Ocean has no subsurfaces
ocean_refl = albedo_table[b, 8]
return ice_refl, pond_refl, ocean_refl
# All possibilities with 4 surfaces
def sample_surf_dist_v4():
surf_frac = []
for i in range(101):
for lm in range(101-i):
for dm in range(101-lm-i):
if (lm+dm) / float(lm+dm+i+0.1) > 0.65:
continue
o = 100 - (i + lm + dm)
surf_frac.append([i, lm, dm, o])
surf_frac = np.divide(surf_frac, 100)
print(np.shape(surf_frac))
for i in range(2400,2410):
print(surf_frac[i], sum(surf_frac[i]))
return surf_frac
def parse_v4(b, albedo_table):
# Sample possible subsurfaces to find ice reflectance
ice_refl = (albedo_table[b, 1]) # Melting Snow
# Sample possible subsurfaces to find pond reflectance
lpond_refl = (albedo_table[b, 4]) # Light Ponds
dpond_refl = (albedo_table[b, 7])
# Ocean has no subsurfaces
ocean_refl = albedo_table[b, 8]
return ice_refl, lpond_refl, dpond_refl, ocean_refl
def sample_surf_dist(num_obs, num_surf):
'''
Establishes the sample surface distribution
:param num_obs: Number of sample observations
:return: List of randomly generated surface distributions
'''
surf_frac = np.zeros((num_obs, num_surf))
for i in range(num_obs):
rlist = [0 for s in range(num_surf + 1)]
rlist[0] = 0
rlist[-1] = 1000
# Establish the sampled surface distribution
for s in range(num_surf - 1):
rlist[s+1] = random.randint(0, 1000)
rlist.sort()
for s in range(num_surf - 1):
surf_frac[i, s] = (rlist[s + 2] - rlist[s + 1]) / 1000
surf_frac[i, num_surf-1] = rlist[1] / 1000.
return surf_frac
def load_albedo(root_dir, filename):
# Read albedo data 7 MODIS 8 WV
albedo_table = np.zeros((8, 9))
csvfname = os.path.join(root_dir, filename)
with open(csvfname) as csvfile:
csv_reader = csv.reader(csvfile, delimiter=',')
for row in csv_reader:
b = int(row[0])-1
for c in range(1, len(row)):
albedo_table[b, c - 1] = float(row[c])
print(albedo_table)
return albedo_table
def write_hdf(fname, features, labels):
tds_file = h5py.File(fname, 'w')
tds_file.create_dataset("features", data=features)
tds_file.create_dataset("labels", data=labels)
tds_file.close()
if __name__ == '__main__':
main()