Skip to content

Commit

Permalink
return DataGranules from search_data; add some type hints
Browse files Browse the repository at this point in the history
  • Loading branch information
jhkennedy committed Dec 14, 2024
1 parent dae6435 commit 6c90e10
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
15 changes: 7 additions & 8 deletions earthaccess/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def search_datasets(count: int = -1, **kwargs: Any) -> List[DataCollection]:
return query.get_all()


def search_data(count: int = -1, **kwargs: Any) -> List[DataGranule]:
def search_data(count: int = -1, **kwargs: Any) -> DataGranules:
"""Search dataset granules using NASA's CMR.
[https://cmr.earthdata.nasa.gov/search/site/docs/search/api.html](https://cmr.earthdata.nasa.gov/search/site/docs/search/api.html)
Expand Down Expand Up @@ -122,14 +122,13 @@ def search_data(count: int = -1, **kwargs: Any) -> List[DataGranule]:
```
"""
if earthaccess.__auth__.authenticated:
query = DataGranules(earthaccess.__auth__).parameters(**kwargs)
results = DataGranules(earthaccess.__auth__).parameters(**kwargs)
else:
query = DataGranules().parameters(**kwargs)
granules_found = query.hits()
logger.info(f"Granules found: {granules_found}")
if count > 0:
return query.get(count)
return query.get_all()
results = DataGranules().parameters(**kwargs)

results.load(count)
logger.info(f"Granules found: {len(results)}")
return results


def search_services(count: int = -1, **kwargs: Any) -> List[Any]:
Expand Down
10 changes: 5 additions & 5 deletions earthaccess/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -960,25 +960,25 @@ def __iter__(self):
def __len__(self):
return len(self.granules)

def __getitem__(self, index: int):
def __getitem__(self, index: int) -> DataGranule:
# FIXME: allow slicing
# if isinstance(index, slice):
# return DataGranules(self.jobs[index])
return self.granules[index]

def __setitem__(self, index: int, granule: DataGranule):
def __setitem__(self, index: int, granule: DataGranule) -> 'DataGranules':
self.granules[index] = granule
return self

# FIXME: Is a granule in this results object? what do we use to tell?
# def __contains__(self, job: Job):
# return job in self.jobs

def __eq__(self, other: 'DataGranules'):
def __eq__(self, other: 'DataGranules') -> bool:
# FIXME: compare query parameters too? what does it mean to be equal?
return self.graunles == other.granules
return self.granules == other.granules

# TODO: display methods
def __repr__(self):
def __repr__(self) -> str:
reprs = ", ".join([granule.__repr__() for granule in self.granules])
return f'DataGranules([{reprs}])'

0 comments on commit 6c90e10

Please sign in to comment.