From dd25609048f059f82c3302acae721d0e8a124a4f Mon Sep 17 00:00:00 2001 From: vincentsarago Date: Wed, 7 Feb 2024 11:33:02 +0100 Subject: [PATCH 1/2] allow datetime object to be provided for temporal queries --- earthaccess/search.py | 47 +++++++++++++++------------ tests/unit/test_collection_queries.py | 31 ++++++++++++++++++ tests/unit/test_granule_queries.py | 2 ++ 3 files changed, 60 insertions(+), 20 deletions(-) diff --git a/earthaccess/search.py b/earthaccess/search.py index 8392ca51..a0231b8d 100644 --- a/earthaccess/search.py +++ b/earthaccess/search.py @@ -1,6 +1,6 @@ import datetime as dt from inspect import getmembers, ismethod -from typing import Any, Dict, List, Optional, Tuple, Type +from typing import Any, Dict, List, Optional, Tuple, Type, Union import dateutil.parser as parser # type: ignore from cmr import CollectionQuery, GranuleQuery # type: ignore @@ -262,31 +262,36 @@ def get(self, limit: int = 2000) -> list: return results def temporal( - self, date_from: str, date_to: str, exclude_boundary: bool = False + self, + date_from: Optional[Union[str, dt.datetime]] = None, + date_to: Optional[Union[str, dt.datetime]] = None, + exclude_boundary: bool = False, ) -> Type[CollectionQuery]: """Filter by an open or closed date range. Dates can be provided as datetime objects or ISO 8601 formatted strings. Multiple ranges can be provided by successive calls. to this method before calling execute(). Parameters: - date_from (String): earliest date of temporal range - date_to (string): latest date of temporal range + date_from (String or Datetime object): earliest date of temporal range + date_to (String or Datetime object): latest date of temporal range exclude_boundary (Boolean): whether or not to exclude the date_from/to in the matched range """ DEFAULT = dt.datetime(1979, 1, 1) - if date_from is not None: + if date_from is not None and not isinstance(date_from, dt.datetime): try: - parsed_from = parser.parse(date_from, default=DEFAULT).isoformat() + "Z" + date_from = parser.parse(date_from, default=DEFAULT).isoformat() + "Z" except Exception: print("The provided start date was not recognized") - parsed_from = "" - if date_to is not None: + date_from = "" + + if date_to is not None and not isinstance(date_to, dt.datetime): try: - parsed_to = parser.parse(date_to, default=DEFAULT).isoformat() + "Z" + date_to = parser.parse(date_to, default=DEFAULT).isoformat() + "Z" except Exception: print("The provided end date was not recognized") - parsed_to = "" - super().temporal(parsed_from, parsed_to, exclude_boundary) + date_to = "" + + super().temporal(date_from, date_to, exclude_boundary) return self @@ -614,8 +619,8 @@ def debug(self, debug: bool = True) -> Type[GranuleQuery]: def temporal( self, - date_from: Optional[str] = None, - date_to: Optional[str] = None, + date_from: Optional[Union[str, dt.datetime]] = None, + date_to: Optional[Union[str, dt.datetime]] = None, exclude_boundary: bool = False, ) -> Type[GranuleQuery]: """Filter by an open or closed date range. @@ -628,19 +633,21 @@ def temporal( exclude_boundary (Boolean): whether or not to exclude the date_from/to in the matched range """ DEFAULT = dt.datetime(1979, 1, 1) - if date_from is not None: + if date_from is not None and not isinstance(date_from, dt.datetime): try: - parsed_from = parser.parse(date_from, default=DEFAULT).isoformat() + "Z" + date_from = parser.parse(date_from, default=DEFAULT).isoformat() + "Z" except Exception: print("The provided start date was not recognized") - parsed_from = "" - if date_to is not None: + date_from = "" + + if date_to is not None and not isinstance(date_to, dt.datetime): try: - parsed_to = parser.parse(date_to, default=DEFAULT).isoformat() + "Z" + date_to = parser.parse(date_to, default=DEFAULT).isoformat() + "Z" except Exception: print("The provided end date was not recognized") - parsed_to = "" - super().temporal(parsed_from, parsed_to, exclude_boundary) + date_to = "" + + super().temporal(date_from, date_to, exclude_boundary) return self def version(self, version: str = "") -> Type[GranuleQuery]: diff --git a/tests/unit/test_collection_queries.py b/tests/unit/test_collection_queries.py index b050d42e..bc152b63 100644 --- a/tests/unit/test_collection_queries.py +++ b/tests/unit/test_collection_queries.py @@ -1,7 +1,22 @@ # package imports +import datetime as dt +import pytest from earthaccess.search import DataCollections +valid_single_dates = [ + ("2001-12-12", "2001-12-21", "2001-12-12T00:00:00Z,2001-12-21T00:00:00Z"), + ("2021-02-01", "", "2021-02-01T00:00:00Z,"), + ("1999-02-01 06:00", "2009-01-01", "1999-02-01T06:00:00Z,2009-01-01T00:00:00Z"), + (dt.datetime(2021, 2, 1), dt.datetime(2021, 2, 2), "2021-02-01T00:00:00Z,2021-02-02T00:00:00Z") +] + +invalid_single_dates = [ + ("2001-12-45", "2001-12-21", None), + ("2021w1", "", None), + ("2999-02-01", "2009-01-01", None), +] + def test_query_can_find_cloud_provider(): query = DataCollections().daac("PODAAC").cloud_hosted(True) assert query.params["provider"] == "POCLOUD" @@ -18,3 +33,19 @@ def test_querybuilder_can_handle_doi(): assert query.params["doi"] == doi query = DataCollections().cloud_hosted(True).daac("PODAAC").doi(doi) assert query.params["doi"] == doi + + +@pytest.mark.parametrize("start,end,expected", valid_single_dates) +def test_query_can_parse_single_dates(start, end, expected): + query = DataCollections().temporal(start, end) + assert query.params["temporal"][0] == expected + + +@pytest.mark.parametrize("start,end,expected", invalid_single_dates) +def test_query_can_handle_invalid_dates(start, end, expected): + query = DataCollections() + try: + query = query.temporal(start, end) + except Exception as e: + assert isinstance(e, ValueError) + assert "temporal" not in query.params diff --git a/tests/unit/test_granule_queries.py b/tests/unit/test_granule_queries.py index f16f2ee8..fa05fcd8 100644 --- a/tests/unit/test_granule_queries.py +++ b/tests/unit/test_granule_queries.py @@ -1,4 +1,5 @@ # package imports +import datetime as dt import pytest from earthaccess.search import DataGranules @@ -6,6 +7,7 @@ ("2001-12-12", "2001-12-21", "2001-12-12T00:00:00Z,2001-12-21T00:00:00Z"), ("2021-02-01", "", "2021-02-01T00:00:00Z,"), ("1999-02-01 06:00", "2009-01-01", "1999-02-01T06:00:00Z,2009-01-01T00:00:00Z"), + (dt.datetime(2021, 2, 1), dt.datetime(2021, 2, 2), "2021-02-01T00:00:00Z,2021-02-02T00:00:00Z") ] invalid_single_dates = [ From 9fd6fb4881e0f8b9f3df4f301aa00c8a3edfd0ba Mon Sep 17 00:00:00 2001 From: vincentsarago Date: Wed, 7 Feb 2024 11:51:21 +0100 Subject: [PATCH 2/2] lint --- tests/unit/test_collection_queries.py | 9 +++++++-- tests/unit/test_granule_queries.py | 7 ++++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/tests/unit/test_collection_queries.py b/tests/unit/test_collection_queries.py index bc152b63..f2c42a38 100644 --- a/tests/unit/test_collection_queries.py +++ b/tests/unit/test_collection_queries.py @@ -1,14 +1,18 @@ # package imports import datetime as dt + import pytest from earthaccess.search import DataCollections - valid_single_dates = [ ("2001-12-12", "2001-12-21", "2001-12-12T00:00:00Z,2001-12-21T00:00:00Z"), ("2021-02-01", "", "2021-02-01T00:00:00Z,"), ("1999-02-01 06:00", "2009-01-01", "1999-02-01T06:00:00Z,2009-01-01T00:00:00Z"), - (dt.datetime(2021, 2, 1), dt.datetime(2021, 2, 2), "2021-02-01T00:00:00Z,2021-02-02T00:00:00Z") + ( + dt.datetime(2021, 2, 1), + dt.datetime(2021, 2, 2), + "2021-02-01T00:00:00Z,2021-02-02T00:00:00Z", + ), ] invalid_single_dates = [ @@ -17,6 +21,7 @@ ("2999-02-01", "2009-01-01", None), ] + def test_query_can_find_cloud_provider(): query = DataCollections().daac("PODAAC").cloud_hosted(True) assert query.params["provider"] == "POCLOUD" diff --git a/tests/unit/test_granule_queries.py b/tests/unit/test_granule_queries.py index fa05fcd8..33280810 100644 --- a/tests/unit/test_granule_queries.py +++ b/tests/unit/test_granule_queries.py @@ -1,5 +1,6 @@ # package imports import datetime as dt + import pytest from earthaccess.search import DataGranules @@ -7,7 +8,11 @@ ("2001-12-12", "2001-12-21", "2001-12-12T00:00:00Z,2001-12-21T00:00:00Z"), ("2021-02-01", "", "2021-02-01T00:00:00Z,"), ("1999-02-01 06:00", "2009-01-01", "1999-02-01T06:00:00Z,2009-01-01T00:00:00Z"), - (dt.datetime(2021, 2, 1), dt.datetime(2021, 2, 2), "2021-02-01T00:00:00Z,2021-02-02T00:00:00Z") + ( + dt.datetime(2021, 2, 1), + dt.datetime(2021, 2, 2), + "2021-02-01T00:00:00Z,2021-02-02T00:00:00Z", + ), ] invalid_single_dates = [