From 521b359642470e6403bda548b6096d2020982979 Mon Sep 17 00:00:00 2001 From: Jessica Scheick Date: Sat, 14 Dec 2024 15:34:21 -0500 Subject: [PATCH 1/2] add __repr__ for DataGranules --- earthaccess/search.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/earthaccess/search.py b/earthaccess/search.py index 2d394010..0680a3ea 100644 --- a/earthaccess/search.py +++ b/earthaccess/search.py @@ -980,6 +980,9 @@ def __eq__(self, other: 'DataGranules') -> bool: return self.granules == other.granules # TODO: display methods - def __repr__(self) -> str: - reprs = ", ".join([granule.__repr__() for granule in self.granules]) - return f'DataGranules([{reprs}])' + def __repr__(self): + + if (count := len(self)) > 0: + return f'DataGranules().parameters({self.params}).load({count})' + + return f'DataGranules().parameters({self.params})' From 84337933446b7bb572e21e119e413958e66e0cb0 Mon Sep 17 00:00:00 2001 From: Jessica Scheick Date: Sat, 14 Dec 2024 16:43:32 -0500 Subject: [PATCH 2/2] add granule ID property to results.py and __str__ to search.py --- earthaccess/results.py | 10 ++++++++++ earthaccess/search.py | 23 ++++++++++++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/earthaccess/results.py b/earthaccess/results.py index df21e336..0de616a0 100644 --- a/earthaccess/results.py +++ b/earthaccess/results.py @@ -211,6 +211,7 @@ class DataGranule(CustomDict): "TemporalExtent", "RelatedUrls", "DataGranule", + "GranuleUR", ] def __init__( @@ -356,3 +357,12 @@ def dataviz_links(self) -> List[str]: """ links = self._filter_related_links("GET RELATED VISUALIZATION") return links + + def granule_ID(self) -> str: + """Get the granule ID + + Returns: + str: Granule ID + """ + + return self['umm']['GranuleUR'] diff --git a/earthaccess/search.py b/earthaccess/search.py index 0680a3ea..8ac3a5b5 100644 --- a/earthaccess/search.py +++ b/earthaccess/search.py @@ -955,6 +955,20 @@ def granules(self, value: list): def granules(self): del self._granules + @property + def granule_ids(self) -> list[str]: + """List of granule IDs for each granule in results + + Returns: + list: Granule ID for each granule in results + """ + + gran_ids=[] + for gran in self.granules: + gran_ids.append(gran.granule_ID()) + + return gran_ids + def __iter__(self): return iter(self.granules) @@ -980,9 +994,16 @@ def __eq__(self, other: 'DataGranules') -> bool: return self.granules == other.granules # TODO: display methods - def __repr__(self): + def __repr__(self) -> str: if (count := len(self)) > 0: return f'DataGranules().parameters({self.params}).load({count})' return f'DataGranules().parameters({self.params})' + + def __str__(self) -> str: + + return "Granule Count: {0} \nGranule IDs: {1}".format( + len(self), + self.granule_ids, + ) \ No newline at end of file