|
23 | 23 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
24 | 24 | # See the License for the specific language governing permissions and limitations under the License. |
25 | 25 |
|
| 26 | +import os |
26 | 27 | from pathlib import Path |
27 | 28 |
|
| 29 | +import earthaccess |
28 | 30 | import netCDF4 as nC |
29 | 31 | import numpy as np |
30 | 32 | import pytest |
|
33 | 35 | from ncompare.printing import Outputter |
34 | 36 |
|
35 | 37 |
|
| 38 | +@pytest.fixture(scope="session") |
| 39 | +def icesat2_cache_dir(): |
| 40 | + """Persistent cache directory for ICESat-2 test data.""" |
| 41 | + cache_dir = Path.home() / ".cache" / "icesat2_test_data" |
| 42 | + cache_dir.mkdir(parents=True, exist_ok=True) |
| 43 | + return cache_dir |
| 44 | + |
| 45 | + |
| 46 | +@pytest.fixture(scope="session") |
| 47 | +def earthdata_auth(): |
| 48 | + """ |
| 49 | + Authenticate with NASA Earthdata. |
| 50 | + Uses credentials from environment variables or .netrc file. |
| 51 | + """ |
| 52 | + # Check for environment variables (used in CI) |
| 53 | + username = os.getenv("EARTHDATA_USERNAME") |
| 54 | + password = os.getenv("EARTHDATA_PASSWORD") |
| 55 | + |
| 56 | + if username and password: |
| 57 | + earthaccess.login(strategy="environment") |
| 58 | + else: |
| 59 | + # Use .netrc or prompt for local testing |
| 60 | + earthaccess.login() |
| 61 | + |
| 62 | + return True |
| 63 | + |
| 64 | + |
| 65 | +@pytest.fixture(scope="session") |
| 66 | +def icesat2_atl06_granule_1(icesat2_cache_dir, earthdata_auth): |
| 67 | + """ |
| 68 | + Download or use cached ICESat-2 ATL06 granule #1 for comparison tests. |
| 69 | + Temporal range: 2023-08-16 16:16:15 to 2023-08-16 16:25:00 |
| 70 | + """ |
| 71 | + # Check if already cached |
| 72 | + cached_files = list(icesat2_cache_dir.glob("ATL06_20230816161508_*.h5")) |
| 73 | + if cached_files: |
| 74 | + return str(cached_files[0]) |
| 75 | + |
| 76 | + # Download if not cached |
| 77 | + results = earthaccess.search_data( |
| 78 | + short_name="ATL06", temporal=("2023-08-16 16:16:15", "2023-08-16 16:25:00"), count=1 |
| 79 | + ) |
| 80 | + |
| 81 | + if not results: |
| 82 | + pytest.skip("ICESat-2 granule #1 not found for test") |
| 83 | + |
| 84 | + # Download the data |
| 85 | + files = earthaccess.download(results, str(icesat2_cache_dir)) |
| 86 | + |
| 87 | + if not files: |
| 88 | + pytest.skip("Failed to download ICESat-2 granule #1") |
| 89 | + |
| 90 | + return files[0] |
| 91 | + |
| 92 | + |
| 93 | +@pytest.fixture(scope="session") |
| 94 | +def icesat2_atl06_granule_2(icesat2_cache_dir, earthdata_auth): |
| 95 | + """ |
| 96 | + Download or use cached ICESat-2 ATL06 granule #2 for comparison tests. |
| 97 | + Temporal range: 2023-08-16 23:46:00 to 2023-08-16 23:48:00 |
| 98 | + """ |
| 99 | + # Check if already cached |
| 100 | + cached_files = list(icesat2_cache_dir.glob("ATL06_20230816234629_*.h5")) |
| 101 | + if cached_files: |
| 102 | + return str(cached_files[0]) |
| 103 | + |
| 104 | + # Download if not cached |
| 105 | + results = earthaccess.search_data( |
| 106 | + short_name="ATL06", temporal=("2023-08-16 23:46:00", "2023-08-16 23:48:00"), count=1 |
| 107 | + ) |
| 108 | + |
| 109 | + if not results: |
| 110 | + pytest.skip("ICESat-2 granule #2 not found for test") |
| 111 | + |
| 112 | + # Download the data |
| 113 | + files = earthaccess.download(results, str(icesat2_cache_dir)) |
| 114 | + |
| 115 | + if not files: |
| 116 | + pytest.skip("Failed to download ICESat-2 granule #2") |
| 117 | + |
| 118 | + return files[0] |
| 119 | + |
| 120 | + |
36 | 121 | @pytest.fixture(scope="session") |
37 | 122 | def temp_data_dir(tmpdir_factory) -> Path: |
38 | 123 | return Path(tmpdir_factory.mktemp("data")) |
|
0 commit comments