-
Notifications
You must be signed in to change notification settings - Fork 1
/
BlueFish.py
executable file
·321 lines (259 loc) · 10.5 KB
/
BlueFish.py
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import datetime
import geopandas as gpd
import pandas as pd
import yaml
import requests
import glob
import os
import timeit
import numpy as np
from pandas import Timedelta
import subprocess
import re
my_globals = {}
exec("from osgeo import gdal ;gdal.UseExceptions()", my_globals)
def is_directory_empty(directory: str):
return len(os.listdir(directory)) == 0
def pingttl_server(server: str) -> str:
server = server.replace("https://", "")
p = subprocess.Popen(["ping", "-v", "-c 5", f"{server}"], stdout=subprocess.PIPE)
res = p.communicate()[0]
if p.returncode > 0:
print("server error")
else:
return res.decode("utf-8")
def config_reader() -> dict:
# Read the settings file and return the dictionary
with open("/root/testbed/settings.yml") as yaml_file:
settings = yaml.safe_load(yaml_file)
return settings
def gcp_reader(settings: dict) -> gpd.GeoDataFrame:
# Read the control points file and return the GeoDataFrame
gcp_total = gpd.read_file(
settings["ControlPoints"]["path"],
where=rf"q_score='{settings['ControlPoints']['q_score']}'",
)
return gcp_total
def gcp_selector(gcp_total: gpd.GeoDataFrame, settings: dict) -> gpd.GeoDataFrame:
# Select random points from the control points GeoDataFrame
random_points_gdf = gcp_total.sample(settings["Analysis"]["n_samples"])
random_points_gdf.reset_index(drop=True, inplace=True)
date_range = pd.date_range(
settings["Collection"]["start_date"],
settings["Collection"]["end_date"],
freq="D",
)
date_index = pd.DataFrame(date_range, columns=["date"])
date_subset = date_index.sample(n=settings["Analysis"]["n_samples"])
date_subset.reset_index(drop=True, inplace=True)
rnd = random_points_gdf.join(date_subset)
return rnd
def cdse_path_get(df: gpd.GeoDataFrame, settings: dict) -> str:
# Get the path of the CDSE product
pre_path = os.path.join(
settings["Local"]["cdse"],
df.iloc[0]["S3Path"][8:],
"GRANULE/*/IMG_DATA/R20m/*B07_20m.jp2",
)
try:
path = glob.glob(pre_path)
if len(path) == 0:
print(f'No path found for {df.iloc[0]["Name"]}')
raise IOError
except (IOError, Exception) as e:
print(f"Error {e}, {path}")
return None
return path[0]
def aws_path_get(df: gpd.GeoDataFrame, settings: dict) -> str:
# Get the path of the AWS product
product_name = df.iloc[0]["Name"]
nm_component = product_name.split("_")
yr = nm_component[2][:4]
mm = nm_component[2][4:6]
dd = nm_component[2][6:8]
zone = nm_component[5][1:3]
row = nm_component[5][3:4]
square = nm_component[5][4:6]
return os.path.join(
settings["Local"]["aws"],
f"tiles/{zone}/{row}/{square}/{yr}/{int(mm)}/{int(dd)}/1/R20m/B07.jp2",
)
def benchmarker_info(path: str, settings: dict):
# Get the timing information of the path
try:
timing = timeit.repeat(
f"gdal.Info('{path}')",
# setup='from osgeo import gdal',
repeat=settings["Analysis"]["repeat_n"],
number=settings["Analysis"]["number_n"],
globals=my_globals,
)
timing = np.array(timing) / settings["Analysis"]["number_n"]
mean = np.mean(timing).round(3)
std = np.std(timing).round(3)
min_val = np.min(timing).round(3)
max_val = np.max(timing).round(3)
size = os.path.getsize(path)
MBps = np.round(size / (np.mean(timing) * 1e6),3)
except Exception as e:
mean, std, min_val, max_val, size, MBps = [0] * 6
return mean, std, min_val, max_val, size, MBps
def scene_selector(settings: dict) -> pd.DataFrame:
# Select the scenes based on the settings
gcp_total = gcp_reader(settings)
gcp_selection = gcp_selector(gcp_total, settings)
# base URL of the product catalogue
catalogue_odata_url = settings["Catalog"]["url"]
collection_name = settings["Collection"]["collection_name"]
product_type = settings["Collection"]["product_type"]
max_cloud_cover = settings["Collection"]["max_cloud_cover"]
paths = pd.DataFrame(columns=["product_name", "CDSE_path", "AWS_path"])
print("Waiting for the fishes...")
for row in gcp_selection.iterrows():
poi = row[1].get(key="geometry")
date = row[1].get(key="date")
dd_number = settings["Analysis"]["dd_number"]
search_period_start = (date - Timedelta(dd_number, unit="day")).strftime(
"%Y-%m-%dT00:00:00.000Z"
)
search_period_end = (date + Timedelta(dd_number, unit="day")).strftime(
"%Y-%m-%dT00:00:00.000Z"
)
search_query = (
f"{catalogue_odata_url}/Products?$"
f"filter=Collection/Name eq '{collection_name}' "
f"and Attributes/OData.CSC.StringAttribute/any(att:att/Name eq 'productType' and att/Value eq '{product_type}') "
# f"and Attributes/OData.CSC.DoubleAttribute/any(att:att/Name eq 'cloudCover' and att/OData.CSC.DoubleAttribute/Value lt {max_cloud_cover})"
f"and OData.CSC.Intersects(area=geography'SRID=4326;{poi}') "
f"and ContentDate/Start gt {search_period_start} "
f"and ContentDate/Start lt {search_period_end}"
# f"&$expand=Attributes"
)
response = requests.get(search_query)
if response.status_code == 200:
response = response.json()
result = pd.DataFrame.from_dict(response["value"])
if result.empty:
print(f"No results for {poi}")
continue
else:
selection = result[
(result["ContentLength"] != 0)
& (result["Online"] == True)
& (
result["Name"].str.contains("_N05")
| result["Name"].str.contains("_N04")
)
]
if selection.empty:
print(f"No results for {poi}")
continue
cdse_path = cdse_path_get(selection, settings)
aws_path = aws_path_get(selection, settings)
if cdse_path is not None and aws_path is not None:
paths = pd.concat(
(
paths,
pd.DataFrame(
[[selection.iloc[0]["Name"], cdse_path, aws_path]],
columns=["product_name", "CDSE_path", "AWS_path"],
),
)
)
else:
continue
else:
print(f"{response.status_code}")
continue
return paths
def result_df(): # -> pd.DataFrame:
# Create an empty DataFrame for the results
return pd.DataFrame(
{
"product_name": pd.Series(dtype="str"),
"mean": pd.Series(dtype="float32"),
"min": pd.Series(dtype="float32"),
"max": pd.Series(dtype="float32"),
"std": pd.Series(dtype="float32"),
"size": pd.Series(dtype="int32"),
"MBps": pd.Series(dtype="float32"),
}
)
def main(settings: dict):
# Main function
print("Preparing the fishing rod...")
settings = config_reader()
pth_list = scene_selector(settings)
results_CDSE = result_df()
results_AWS = result_df()
print("Fishing ...")
for _, row in pth_list.iterrows():
product_name = row.iloc[0]
CDSE_path = row.iloc[1]
AWS_path = row.iloc[2]
if "cdse" in settings["Analysis"]["endpoints"]:
mean_CDSE, std_CDSE, min_CDSE, max_CDSE, size_CDSE, MBps_CDSE = benchmarker_info(
CDSE_path, settings
)
results_CDSE = pd.concat(
(
results_CDSE,
pd.DataFrame(
[[product_name, mean_CDSE, min_CDSE, max_CDSE, std_CDSE, size_CDSE, MBps_CDSE]],
columns=["product_name", "mean", "min", "max", "std", "size", "MBps"],
),
)
)
if "aws" in settings["Analysis"]["endpoints"]:
mean_AWS, std_AWS, min_AWS, max_AWS, size_AWS, MBps_AWS = benchmarker_info(AWS_path, settings)
results_AWS = pd.concat(
(
results_AWS,
pd.DataFrame(
[[product_name, mean_AWS, min_AWS, max_AWS, std_AWS, size_AWS, MBps_AWS]],
columns=["product_name", "mean", "min", "max", "std", "size", "MBps"],
),
)
)
print("Fishes are in the basket! let's go home!")
stamp = datetime.datetime.now().strftime("_%Y%m%d_%H%M")
if settings["Analysis"]["ping"]:
ping_ttl = pingttl_server(settings["Catalog"]["url"])
if ping_ttl:
with open(os.path.join(settings["Local"]["output"], f"CDSE_ping_{stamp}.txt"), "w") as f:
f.write(ping_ttl)
print(f"CDSE ping is saved! file name: CDSE_ping_{stamp}.txt")
else:
print("Ping error")
if "cdse" in settings["Analysis"]["endpoints"]:
file_name = f"CDSE{stamp}.csv"
results_CDSE.to_csv(
os.path.join(settings["Local"]["output"], file_name), index=False
)
print(f"CDSE results are saved! file name: {file_name}")
if "aws" in settings["Analysis"]["endpoints"]:
file_name = f"AWS{stamp}.csv"
results_AWS.to_csv(
os.path.join(settings["Local"]["output"], file_name), index=False
)
print(f"AWS results are saved! file name: {file_name}")
print("Look behind you, a Three-Headed Monkey! ...")
if __name__ == "__main__":
print("Welcome to the BlueFish lake!")
try:
try:
if os.path.exists("/root/testbed/settings.yml"):
settings = config_reader()
else:
print("Settings file not found!")
raise FileNotFoundError
if os.path.exists("/root/cdse/"):
if len(os.listdir("/root/cdse/")) == 0:
print("The /root/cdse directory is empty! Please check the credentials settings.")
raise FileNotFoundError
except FileNotFoundError:
print("Something went wrong! Please check the settings file. Exiting...")
exit()
main(settings)
except KeyboardInterrupt:
print("Caught KeyboardInterrupt, stop the fishing!")