Skip to content

Commit 9e42e47

Browse files
authored
Remove algorithm argument (#130)
* remove algorithm argument * remove useless prints
1 parent bfe609f commit 9e42e47

File tree

1 file changed

+15
-119
lines changed

1 file changed

+15
-119
lines changed

earthspy/earthspy.py

Lines changed: 15 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727

2828

2929
class EarthSpy:
30-
"""Monitor and study any place on Earth and in Near Real-Time
30+
"""
31+
Monitor and study any place on Earth and in Near Real-Time
3132
(NRT) using the SentinelHub services.
32-
3333
"""
3434

3535
def __init__(self, CLIENT_credentials_file: str) -> None:
@@ -51,10 +51,13 @@ def __init__(self, CLIENT_credentials_file: str) -> None:
5151
self.configure_connection()
5252

5353
def configure_connection(self) -> shb.SHConfig:
54-
"""Build a shb configuration class for the connection to Sentinel Hub services.
54+
"""
55+
Build a shb configuration class for the connection to
56+
Sentinel Hub services.
5557
5658
:return: sentinelhub-py package configuration class.
5759
:rtype: shb.SHConfig
60+
5861
"""
5962

6063
# setup Sentinel Hub connection
@@ -76,7 +79,6 @@ def set_query_parameters(
7679
time_interval: Union[int, tuple],
7780
data_collection: str,
7881
evaluation_script: Union[None, str] = None,
79-
algorithm: Union[None, str] = None,
8082
resolution: Union[None, int] = None,
8183
store_folder: Union[None, str] = None,
8284
multithreading: bool = True,
@@ -102,11 +104,6 @@ def set_query_parameters(
102104
not specified, a default script is used.
103105
:type evaluation_script: str
104106
105-
:param algorithm: Name of the algorithm to apply (some algorithms
106-
would require a large number of variables to be set by the user, we
107-
therefore decided to encapsule them).
108-
:type algorithm: Union[None, str], optional
109-
110107
:param data_collection: Data collection name. Check
111108
shb.DataCollection.get_available_collections() for a list of all
112109
collections currently available.
@@ -157,7 +154,6 @@ def set_query_parameters(
157154
self.multithreading = multithreading
158155
self.verbose = verbose
159156
self.remove_splitboxes = remove_splitboxes
160-
self.algorithm = algorithm
161157

162158
# set query attributes
163159
self.data_collection_str = data_collection
@@ -228,10 +224,7 @@ def get_data_collection(self) -> shb.DataCollection:
228224
"""
229225

230226
# set Sentinel Hub data collection object
231-
if self.algorithm == "SICE":
232-
self.data_collection = shb.DataCollection.SENTINEL3_OLCI
233-
else:
234-
self.data_collection = shb.DataCollection[self.data_collection_str]
227+
self.data_collection = shb.DataCollection[self.data_collection_str]
235228

236229
return self.data_collection
237230

@@ -499,9 +492,6 @@ def get_store_folder(self, store_folder: Union[str, None]) -> str:
499492
if self.bounding_box_name:
500493
store_folder += f"{os.sep}{self.bounding_box_name}"
501494

502-
if self.algorithm:
503-
store_folder += f"{os.sep}{self.algorithm}"
504-
505495
# create subfolder if doesn't exist
506496
if not os.path.exists(store_folder):
507497
os.makedirs(store_folder)
@@ -817,7 +807,9 @@ def sentinelhub_request(
817807
shb.SentinelHubRequest.input_data(
818808
data_collection=self.data_collection,
819809
time_interval=(date_string, date_string),
820-
other_args={"processing": {"orthorectify": True}},
810+
other_args={
811+
"processing": {"orthorectify": True},
812+
},
821813
)
822814
],
823815
responses=[
@@ -828,46 +820,6 @@ def sentinelhub_request(
828820
config=self.config,
829821
)
830822

831-
if self.algorithm == "SICE":
832-
self.response_files = [
833-
"r_TOA_01",
834-
"r_TOA_06",
835-
"r_TOA_17",
836-
"r_TOA_21",
837-
"snow_grain_diameter",
838-
"snow_specific_surface_area",
839-
"diagnostic_retrieval",
840-
"albedo_bb_planar_sw",
841-
"albedo_bb_spherical_sw",
842-
]
843-
844-
shb_request = shb.SentinelHubRequest(
845-
data_folder=self.store_folder,
846-
evalscript=self.evaluation_script,
847-
input_data=[
848-
shb.SentinelHubRequest.input_data(
849-
data_collection=shb.DataCollection.DEM_COPERNICUS_30,
850-
identifier="COP_30",
851-
upsampling="NEAREST",
852-
downsampling="NEAREST",
853-
),
854-
shb.SentinelHubRequest.input_data(
855-
data_collection=shb.DataCollection.SENTINEL3_OLCI,
856-
identifier="OLCI",
857-
time_interval=(date_string, date_string),
858-
upsampling="NEAREST",
859-
downsampling="NEAREST",
860-
),
861-
],
862-
responses=[
863-
shb.SentinelHubRequest.output_response(rf, shb.MimeType.TIFF)
864-
for rf in self.response_files
865-
],
866-
bbox=loc_bbox,
867-
size=loc_size,
868-
config=self.config,
869-
)
870-
871823
return shb_request
872824

873825
def send_sentinelhub_requests(self) -> list:
@@ -941,10 +893,6 @@ def rename_output_files(self) -> None:
941893
# get raw folders created by Sentinel Hub API
942894
folders = [f"{self.store_folder}/{fn}" for fn in self.raw_folder_names]
943895

944-
# extract outputs stored in archives
945-
if self.algorithm == "SICE":
946-
self.extract_sentinelhub_responses(folders)
947-
948896
# store new file names
949897
self.output_filenames = []
950898

@@ -959,36 +907,13 @@ def rename_output_files(self) -> None:
959907
# extract date of acquisition
960908
date = list(request_tree.execute("$..timeRange"))[0]["from"].split("T")[0]
961909

962-
# if SICE, store files in date subfolders if multiple outputs
963-
if self.algorithm == "SICE":
964-
# build folder name
965-
date_folder = f"{self.store_folder}/{date}"
966-
967-
# create folder if doesn't exist
968-
if not os.path.exists(date_folder):
969-
os.makedirs(date_folder)
970-
971-
# list all output files available for date
972-
date_files = sorted(glob.glob(f"{folder}/*.tif"))
973-
974910
# if D download mode, set file name using date and data collection
975911
if self.download_mode == "D":
976912
# build new file name
977913
new_filename = (
978914
f"{self.store_folder}/" + "{date}_{self.data_collection_str}.tif"
979915
)
980916

981-
# If SICE, don't rename file but move to date folder
982-
if self.algorithm == "SICE":
983-
for f in date_files:
984-
# include date in path
985-
os.rename(
986-
f, f"{self.store_folder}/{date}/{f.split(os.sep)[-1]}"
987-
)
988-
989-
# store output file name
990-
self.output_filenames.append(f)
991-
992917
# if SM download mode, set file name using date, data collection and box id
993918
elif self.download_mode == "SM":
994919
# recreate split box
@@ -1008,26 +933,6 @@ def rename_output_files(self) -> None:
1008933
+ f"{date}_{self.data_collection_str}_{split_box_id}.tif"
1009934
)
1010935

1011-
# if SICE, add split box id in all names and move to date folder
1012-
if self.algorithm == "SICE":
1013-
for f in date_files:
1014-
# extract absolute path
1015-
absolute_file_name = f.split(os.sep)[-1]
1016-
new_absolute_file_name = absolute_file_name.replace(
1017-
".tif", f"_{split_box_id}.tif"
1018-
)
1019-
1020-
# include date in path
1021-
new_full_file_name = (
1022-
f"{self.store_folder}/{date}/{new_absolute_file_name}"
1023-
)
1024-
1025-
# rename file
1026-
os.rename(f, new_full_file_name)
1027-
1028-
# store output file name
1029-
self.output_filenames.append(new_full_file_name)
1030-
1031936
# rename file using new file name
1032937
if os.path.exists(f"{folder}/response.tiff"):
1033938
os.rename(f"{folder}/response.tiff", new_filename)
@@ -1048,31 +953,22 @@ def merge_rasters(self) -> None:
1048953
the different rasters have been merged.
1049954
"""
1050955

1051-
# extract dates from file or folder names (depending on algorithm)
1052-
if self.algorithm == "SICE":
1053-
dates = [f.split(os.sep)[-2] for f in self.output_filenames]
1054-
else:
1055-
dates = [f.split(os.sep)[-1].split("_")[0] for f in self.output_filenames]
956+
# extract dates from file
957+
dates = [f.split(os.sep)[-1].split("_")[0] for f in self.output_filenames]
1056958

1057959
# get distinct dates because several split boxes a day
1058960
distinct_dates = list(Counter(dates).keys())
1059961

1060962
# store new file names
1061963
self.output_filenames_renamed = []
1062964

1063-
# merge rasters for each distinct date (work with distinct dates because
1064-
# of different trees depending on the algorithm used)
965+
# merge rasters for each distinct date
1065966
for date in distinct_dates:
1066967
# select files matching acquisition date only
1067968
date_output_files = [f for f in self.output_filenames if date in f]
1068969

1069-
# loop over response files if multiple ones (they all need to be
1070-
# merged, but only to their respective boxes)
1071-
if self.algorithm == "SICE":
1072-
file_iterator = self.response_files
1073-
else:
1074-
# set to tif to select all files (but keep a general file_iterator)
1075-
file_iterator = ["tif"]
970+
# set to tif to select all files (but keep a general file_iterator)
971+
file_iterator = ["tif"]
1076972

1077973
for pattern in file_iterator:
1078974
date_response_files = [f for f in date_output_files if pattern in f]

0 commit comments

Comments
 (0)