88from openeo .rest .datacube import DataCube
99
1010BAND_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" ,
1218}
1319BAND_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" ,
1529}
1630BAND_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" ,
1838}
1939BAND_MAPPING_PROBAV = {
20- "BLUE" : "B" , "RED" : "R" , "NIR" : "N" , "SWIR" : "S1"
40+ "BLUE" : "B" ,
41+ "RED" : "R" ,
42+ "NIR" : "N" ,
43+ "SWIR" : "S1" ,
2144}
2245BAND_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" ,
2558}
2659
2760
@@ -41,9 +74,7 @@ def _get_expression_map(cube: DataCube, x: ProcessBuilder) -> Dict[str, ProcessB
4174 elif "TERRASCOPE_S2" in collection_id or "SENTINEL2" in collection_id :
4275 band_mapping = BAND_MAPPING_SENTINEL2
4376 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" )
4778
4879 cube_bands = [band .replace ("0" , "" ).upper () for band in cube .metadata .band_names ]
4980 # TODO: use `label` parameter from `array_element` to avoid index based band references.
@@ -56,7 +87,7 @@ def load_indices() -> Dict[str, dict]:
5687
5788 for path in [
5889 "resources/awesome-spectral-indices/spectral-indices-dict.json" ,
59- "resources/extra-indices-dict.json"
90+ "resources/extra-indices-dict.json" ,
6091 ]:
6192 with pkg_resources .resource_stream ("openeo.extra.spectral_indices" , path ) as stream :
6293 specs .update (json .load (stream )["SpectralIndices" ])
@@ -70,20 +101,24 @@ def list_indices() -> List[str]:
70101 return list (specs .keys ())
71102
72103
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" ]
75106 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+ )
77110 for rng in range_vals :
78- if params [rng ] == None :
111+ if params [rng ] is None :
79112 continue
80113 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+ )
82117 # TODO: allow float too?
83118 if not all (isinstance (val , int ) for val in params [rng ]):
84119 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" )
87122
88123
89124def _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):
95130 # a more generic machine learning data preparation feature
96131 input_vals = ["collection" , "indices" ]
97132 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 ():
101138 if index not in index_specs .keys ():
102139 raise NotImplementedError ("Index " + index + " is not supported." )
103140 _check_params (index , params )
@@ -115,7 +152,9 @@ def _callback(x: ProcessBuilder, index_dict: dict, datacube: DataCube, index_spe
115152 index_result = index_result .linear_scale_range (* params ["input_range" ], * params ["output_range" ])
116153 index_values .append (index_result )
117154 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+ )
119158 if append :
120159 return array_modify (data = x_res , values = index_values , index = len (datacube .metadata .band_names ))
121160 else :
@@ -155,11 +194,13 @@ def compute_and_rescale_indices(datacube: DataCube, index_dict: dict, append=Fal
155194 index_specs = load_indices ()
156195
157196 _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+ )
159200 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 ()))
161202 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 ()))
163204
164205
165206def 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
205246 index_dict = {
206247 "collection" : {
207248 "input_range" : None ,
208- "output_range" : None
249+ "output_range" : None ,
209250 },
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 },
213252 }
214253 return compute_and_rescale_indices (datacube = datacube , index_dict = index_dict , append = append )
215254
0 commit comments