8
8
from openeo .rest .datacube import DataCube
9
9
10
10
BAND_MAPPING_LANDSAT457 = {
11
- "B1" : "B" , "B2" : "G" , "B3" : "R" , "B4" : "N" , "B5" : "S1" , "B6" : "T1" , "B7" : "S2"
11
+ "B1" : "B" ,
12
+ "B2" : "G" ,
13
+ "B3" : "R" ,
14
+ "B4" : "N" ,
15
+ "B5" : "S1" ,
16
+ "B6" : "T1" ,
17
+ "B7" : "S2" ,
12
18
}
13
19
BAND_MAPPING_LANDSAT8 = {
14
- "B1" : "A" , "B2" : "B" , "B3" : "G" , "B4" : "R" , "B5" : "N" , "B6" : "S1" , "B7" : "S2" , "B10" : "T1" , "B11" : "T2"
20
+ "B1" : "A" ,
21
+ "B2" : "B" ,
22
+ "B3" : "G" ,
23
+ "B4" : "R" ,
24
+ "B5" : "N" ,
25
+ "B6" : "S1" ,
26
+ "B7" : "S2" ,
27
+ "B10" : "T1" ,
28
+ "B11" : "T2" ,
15
29
}
16
30
BAND_MAPPING_MODIS = {
17
- "B3" : "B" , "B4" : "G" , "B1" : "R" , "B2" : "N" , "B5" : np .nan , "B6" : "S1" , "B7" : "S2"
31
+ "B3" : "B" ,
32
+ "B4" : "G" ,
33
+ "B1" : "R" ,
34
+ "B2" : "N" ,
35
+ "B5" : np .nan ,
36
+ "B6" : "S1" ,
37
+ "B7" : "S2" ,
18
38
}
19
39
BAND_MAPPING_PROBAV = {
20
- "BLUE" : "B" , "RED" : "R" , "NIR" : "N" , "SWIR" : "S1"
40
+ "BLUE" : "B" ,
41
+ "RED" : "R" ,
42
+ "NIR" : "N" ,
43
+ "SWIR" : "S1" ,
21
44
}
22
45
BAND_MAPPING_SENTINEL2 = {
23
- "B1" : "A" , "B2" : "B" , "B3" : "G" , "B4" : "R" , "B5" : "RE1" , "B6" : "RE2" , "B7" : "RE3" , "B8" : "N" ,
24
- "B8A" : "RE4" , "B9" : "WV" , "B11" : "S1" , "B12" : "S2"
46
+ "B1" : "A" ,
47
+ "B2" : "B" ,
48
+ "B3" : "G" ,
49
+ "B4" : "R" ,
50
+ "B5" : "RE1" ,
51
+ "B6" : "RE2" ,
52
+ "B7" : "RE3" ,
53
+ "B8" : "N" ,
54
+ "B8A" : "RE4" ,
55
+ "B9" : "WV" ,
56
+ "B11" : "S1" ,
57
+ "B12" : "S2" ,
25
58
}
26
59
27
60
@@ -41,9 +74,7 @@ def _get_expression_map(cube: DataCube, x: ProcessBuilder) -> Dict[str, ProcessB
41
74
elif "TERRASCOPE_S2" in collection_id or "SENTINEL2" in collection_id :
42
75
band_mapping = BAND_MAPPING_SENTINEL2
43
76
else :
44
- raise ValueError ("Could not detect supported satellite platform from {cid!r} for index computation!" .format (
45
- cid = collection_id
46
- ))
77
+ raise ValueError (f"Could not detect supported satellite platform from { collection_id !r} for index computation" )
47
78
48
79
cube_bands = [band .replace ("0" , "" ).upper () for band in cube .metadata .band_names ]
49
80
# TODO: use `label` parameter from `array_element` to avoid index based band references.
@@ -56,7 +87,7 @@ def load_indices() -> Dict[str, dict]:
56
87
57
88
for path in [
58
89
"resources/awesome-spectral-indices/spectral-indices-dict.json" ,
59
- "resources/extra-indices-dict.json"
90
+ "resources/extra-indices-dict.json" ,
60
91
]:
61
92
with pkg_resources .resource_stream ("openeo.extra.spectral_indices" , path ) as stream :
62
93
specs .update (json .load (stream )["SpectralIndices" ])
@@ -70,20 +101,24 @@ def list_indices() -> List[str]:
70
101
return list (specs .keys ())
71
102
72
103
73
- def _check_params (item ,params ):
74
- range_vals = ["input_range" ,"output_range" ]
104
+ def _check_params (item , params ):
105
+ range_vals = ["input_range" , "output_range" ]
75
106
if set (params ) != set (range_vals ):
76
- raise ValueError ("You have set the following parameters {} on {}, while the following are required {}" .format (params ,item ,range_vals ))
107
+ raise ValueError (
108
+ f"You have set the parameters { params } on { item } , while the following are required { range_vals } "
109
+ )
77
110
for rng in range_vals :
78
- if params [rng ] == None :
111
+ if params [rng ] is None :
79
112
continue
80
113
if len (params [rng ]) != 2 :
81
- raise ValueError ("The list of values you have supplied {} for parameter {} for {} is not of length 2" .format (params [rng ], rng , item ))
114
+ raise ValueError (
115
+ f"The list of provided values { params [rng ]} for parameter { rng } for { item } is not of length 2"
116
+ )
82
117
# TODO: allow float too?
83
118
if not all (isinstance (val , int ) for val in params [rng ]):
84
119
raise ValueError ("The ranges you supplied are not all of type int" )
85
- if (params ["input_range" ] == None ) != (params ["output_range" ] == None ):
86
- raise ValueError ("The index_range and output_range of {} should either be both supplied, or both None" . format ( item ) )
120
+ if (params ["input_range" ] is None ) != (params ["output_range" ] is None ):
121
+ raise ValueError (f "The index_range and output_range of { item } should either be both supplied, or both None" )
87
122
88
123
89
124
def _check_validity_index_dict (index_dict : dict , index_specs : dict ):
@@ -95,9 +130,11 @@ def _check_validity_index_dict(index_dict: dict, index_specs: dict):
95
130
# a more generic machine learning data preparation feature
96
131
input_vals = ["collection" , "indices" ]
97
132
if set (index_dict .keys ()) != set (input_vals ):
98
- raise ValueError ("The first level of the dictionary should contain the keys 'collection' and 'indices', but they contain {}" .format (index_dict .keys ()))
99
- _check_params ("collection" ,index_dict ["collection" ])
100
- for index ,params in index_dict ["indices" ].items ():
133
+ raise ValueError (
134
+ f"The first level of the dictionary should contain the keys 'collection' and 'indices', but they contain { index_dict .keys ()} "
135
+ )
136
+ _check_params ("collection" , index_dict ["collection" ])
137
+ for index , params in index_dict ["indices" ].items ():
101
138
if index not in index_specs .keys ():
102
139
raise NotImplementedError ("Index " + index + " is not supported." )
103
140
_check_params (index , params )
@@ -115,7 +152,9 @@ def _callback(x: ProcessBuilder, index_dict: dict, datacube: DataCube, index_spe
115
152
index_result = index_result .linear_scale_range (* params ["input_range" ], * params ["output_range" ])
116
153
index_values .append (index_result )
117
154
if index_dict ["collection" ]["input_range" ] is not None :
118
- x_res = x_res .linear_scale_range (* index_dict ["collection" ]["input_range" ], * index_dict ["collection" ]["output_range" ])
155
+ x_res = x_res .linear_scale_range (
156
+ * index_dict ["collection" ]["input_range" ], * index_dict ["collection" ]["output_range" ]
157
+ )
119
158
if append :
120
159
return array_modify (data = x_res , values = index_values , index = len (datacube .metadata .band_names ))
121
160
else :
@@ -155,11 +194,13 @@ def compute_and_rescale_indices(datacube: DataCube, index_dict: dict, append=Fal
155
194
index_specs = load_indices ()
156
195
157
196
_check_validity_index_dict (index_dict , index_specs )
158
- res = datacube .apply_dimension (dimension = "bands" ,process = lambda x : _callback (x , index_dict , datacube , index_specs , append ))
197
+ res = datacube .apply_dimension (
198
+ dimension = "bands" , process = lambda x : _callback (x , index_dict , datacube , index_specs , append )
199
+ )
159
200
if append :
160
- return res .rename_labels (' bands' , target = datacube .metadata .band_names + list (index_dict ["indices" ].keys ()))
201
+ return res .rename_labels (" bands" , target = datacube .metadata .band_names + list (index_dict ["indices" ].keys ()))
161
202
else :
162
- return res .rename_labels (' bands' , target = list (index_dict ["indices" ].keys ()))
203
+ return res .rename_labels (" bands" , target = list (index_dict ["indices" ].keys ()))
163
204
164
205
165
206
def append_and_rescale_indices (datacube : DataCube , index_dict : dict ) -> DataCube :
@@ -205,11 +246,9 @@ def compute_indices(datacube: DataCube, indices: List[str], append: bool = False
205
246
index_dict = {
206
247
"collection" : {
207
248
"input_range" : None ,
208
- "output_range" : None
249
+ "output_range" : None ,
209
250
},
210
- "indices" : {
211
- index : {"input_range" : None , "output_range" : None } for index in indices
212
- }
251
+ "indices" : {index : {"input_range" : None , "output_range" : None } for index in indices },
213
252
}
214
253
return compute_and_rescale_indices (datacube = datacube , index_dict = index_dict , append = append )
215
254
0 commit comments