From 8f7d027a9341402289e86315bffa372405400953 Mon Sep 17 00:00:00 2001 From: GabrieleMeoni Date: Mon, 2 Oct 2023 19:03:50 +0200 Subject: [PATCH] Fixing a bug on database utils returning the wrong number of useful granules. Fixing a bug on event not checking that event is not void when a new event is created. --- pyraws/raw/raw_event.py | 167 +++++++++++++++++---------------- pyraws/utils/database_utils.py | 11 +-- 2 files changed, 92 insertions(+), 86 deletions(-) diff --git a/pyraws/raw/raw_event.py b/pyraws/raw/raw_event.py index d600adb..ac6db7a 100644 --- a/pyraws/raw/raw_event.py +++ b/pyraws/raw/raw_event.py @@ -84,48 +84,52 @@ def from_path(self, raw_dir_path, bands_list, verbose=True): bands list. If None, all bands are used and sorted according to the datasheet order. Defaults to None. verbose (bool, optional): if True, if True, verbose mode is used. Defaults to True. """ - if bands_list is None: - bands_list = list(BANDS_RAW_SHAPE_DICT.keys()) - try: - ( - granules_collection, - granules_paths, - polygon_coordinates_list, - cloud_percentages_list, - ) = read_Raw_event_from_path( - raw_dir_path, bands_list, verbose, self.__device - ) - except: # noqa: E722 - raise ValueError( - "Impossible to open the raw file at: " - + colored(raw_dir_path, "red") - + "." - ) - granule_names = find_granules_names(granules_paths) - self.__bands_names = bands_list - # These pieces of information are not available since the event is not read from database. - self.__raw_useful_granules_idx = [] - self.__raw_complementary_granules_idx = [] - self.__event_class = None - self.__useful_granule_bounding_box_dict = {} - - for granule, n in zip(granules_collection, range(len(granules_collection))): - new_granule = Raw_granule(device=self.__device) - along_track_size = geopy.distance.geodesic( - polygon_coordinates_list[n][0], polygon_coordinates_list[n][1] - ).km - new_granule.create_granule( - bands_list, - granule, - granule_names[n], - polygon_coordinates_list[n], - along_track_size, - cloud_percentages_list[n], - ) - self.__granules_collection.append(new_granule) + if not(self.is_void()): + print("Impossible to create a new event from path: " +colored(raw_dir_path, "blue")+". " + colored("Event already instantiated.", "red")) + else: + if bands_list is None: + bands_list = list(BANDS_RAW_SHAPE_DICT.keys()) + try: + ( + granules_collection, + granules_paths, + polygon_coordinates_list, + cloud_percentages_list, + ) = read_Raw_event_from_path( + raw_dir_path, bands_list, verbose, self.__device + ) + except: # noqa: E722 + raise ValueError( + "Impossible to open the raw file at: " + + colored(raw_dir_path, "red") + + "." + ) - self.n_granules = len(self.__granules_collection) + granule_names = find_granules_names(granules_paths) + self.__bands_names = bands_list + # These pieces of information are not available since the event is not read from database. + self.__raw_useful_granules_idx = [] + self.__raw_complementary_granules_idx = [] + self.__event_class = None + self.__useful_granule_bounding_box_dict = {} + + for granule, n in zip(granules_collection, range(len(granules_collection))): + new_granule = Raw_granule(device=self.__device) + along_track_size = geopy.distance.geodesic( + polygon_coordinates_list[n][0], polygon_coordinates_list[n][1] + ).km + new_granule.create_granule( + bands_list, + granule, + granule_names[n], + polygon_coordinates_list[n], + along_track_size, + cloud_percentages_list[n], + ) + self.__granules_collection.append(new_granule) + + self.n_granules = len(self.__granules_collection) def from_database( self, @@ -148,49 +152,52 @@ def from_database( verbose (bool, optional): if True, if True, verbose mode is used. Defaults to True. database (string, optional): database name. Defaults to "THRAWS". """ - if bands_list is None: - bands_list = list(BANDS_RAW_SHAPE_DICT.keys()) - - ( - granules_collection, - event_class, - granule_names, - raw_useful_granules, - raw_complementary_granules, - polygon_coordinates_list, - cloud_percentages_list, - useful_granule_bounding_box_dict, - ) = read_Raw_event_from_database( - id_event, - bands_list, - cfg_file_dict, - id_raw_l1_dict, - database, - verbose, - device=self.__device, - ) - self.__bands_names = bands_list - self.__raw_useful_granules_idx = raw_useful_granules - self.__raw_complementary_granules_idx = raw_complementary_granules - self.__event_class = event_class - self.__useful_granule_bounding_box_dict = useful_granule_bounding_box_dict - - for granule, n in zip(granules_collection, range(len(granules_collection))): - new_granule = Raw_granule(device=self.__device) - along_track_size = geopy.distance.geodesic( - polygon_coordinates_list[n][0], polygon_coordinates_list[n][1] - ).km - new_granule.create_granule( + if not(self.is_void()): + print("Impossible to create a new event from: " +colored(id_event, "blue")+". " + colored("Event already instantiated.", "red")) + else: + if bands_list is None: + bands_list = list(BANDS_RAW_SHAPE_DICT.keys()) + + ( + granules_collection, + event_class, + granule_names, + raw_useful_granules, + raw_complementary_granules, + polygon_coordinates_list, + cloud_percentages_list, + useful_granule_bounding_box_dict, + ) = read_Raw_event_from_database( + id_event, bands_list, - granule, - granule_names[n], - polygon_coordinates_list[n], - along_track_size, - cloud_percentages_list[n], + cfg_file_dict, + id_raw_l1_dict, + database, + verbose, + device=self.__device, ) - self.__granules_collection.append(new_granule) + self.__bands_names = bands_list + self.__raw_useful_granules_idx = raw_useful_granules + self.__raw_complementary_granules_idx = raw_complementary_granules + self.__event_class = event_class + self.__useful_granule_bounding_box_dict = useful_granule_bounding_box_dict - self.n_granules = len(self.__granules_collection) + for granule, n in zip(granules_collection, range(len(granules_collection))): + new_granule = Raw_granule(device=self.__device) + along_track_size = geopy.distance.geodesic( + polygon_coordinates_list[n][0], polygon_coordinates_list[n][1] + ).km + new_granule.create_granule( + bands_list, + granule, + granule_names[n], + polygon_coordinates_list[n], + along_track_size, + cloud_percentages_list[n], + ) + self.__granules_collection.append(new_granule) + + self.n_granules = len(self.__granules_collection) def get_bands_list(self): """Returns the list of bands of every Raw_granule object in the collection. diff --git a/pyraws/utils/database_utils.py b/pyraws/utils/database_utils.py index 6874ea3..60a8b01 100644 --- a/pyraws/utils/database_utils.py +++ b/pyraws/utils/database_utils.py @@ -475,19 +475,18 @@ def parse_string(str, skip_marks=True, return_int=False): ) + "." ) - if (raw_useful_granules_str is not None) and (len(raw_useful_granules_str) != 0): raw_useful_granules_str = ( raw_useful_granules_str[1:-1].replace(" ", "").split("],") ) - if not ("[" in raw_useful_granules_str) and not ( - "]" in raw_useful_granules_str - ): # Single granules list + if len(raw_useful_granules_str) == 1: + # Single granules list raw_useful_granules = [ - int(x[0]) if ((x[0] is not None) and (x[0] != "None")) else [None] - for x in [x.split(",") for x in raw_useful_granules_str] + int(x) if ((x is not None) and (x != "None")) else None + for x in raw_useful_granules_str[0].split(",") ] else: + print(raw_useful_granules_str) raw_useful_granules = [ [int(x), int(y)] if (x != "None") and (y != "None") else [None, None] for [x, y] in [