|
14 | 14 | import particle_tracking_manager |
15 | 15 |
|
16 | 16 | from particle_tracking_manager.config_ocean_model import ocean_model_simulation_mapper |
| 17 | +from particle_tracking_manager.models.opendrift.utils import ( |
| 18 | + find_json_files_in_date_range, |
| 19 | +) |
17 | 20 | from particle_tracking_manager.ocean_model_registry import ocean_model_registry |
18 | 21 |
|
19 | 22 |
|
@@ -188,6 +191,92 @@ def test_start_end_times(): |
188 | 191 | ) |
189 | 192 |
|
190 | 193 |
|
| 194 | +class MockFileSystem: |
| 195 | + """Mock file system to simulate globbing for kerchunk JSON files.""" |
| 196 | + |
| 197 | + def __init__(self, files): |
| 198 | + self.files = files |
| 199 | + |
| 200 | + def glob(self, year): |
| 201 | + # Mock glob method takes a year string instead of a pattern |
| 202 | + return [file for file in self.files if year in file] |
| 203 | + |
| 204 | + |
| 205 | +MOCK_FILES_YEARLY_DATES = [ |
| 206 | + (1, 1), |
| 207 | + (1, 2), |
| 208 | + (1, 31), |
| 209 | + (3, 15), |
| 210 | + (5, 31), |
| 211 | + (6, 1), |
| 212 | + (6, 15), |
| 213 | + (6, 30), |
| 214 | + (7, 1), |
| 215 | + (9, 15), |
| 216 | + (12, 1), |
| 217 | + (12, 30), |
| 218 | + (12, 31), |
| 219 | +] |
| 220 | +MOCK_FILES_DATES = [ |
| 221 | + datetime(year, month, day) |
| 222 | + for month, day in MOCK_FILES_YEARLY_DATES |
| 223 | + for year in [2019, 2020, 2021] |
| 224 | +] |
| 225 | +TEST_DATE_RANGES = [ |
| 226 | + ((2020, 6, 5), (2020, 6, 25), 1), |
| 227 | + ((2020, 1, 1), (2020, 1, 31), 3), |
| 228 | + ((2020, 1, 31), (2020, 1, 1), 3), |
| 229 | + ((2019, 12, 1), (2020, 1, 31), 6), |
| 230 | + ((2019, 12, 31), (2020, 1, 1), 2), |
| 231 | + ((2020, 1, 1), (2020, 12, 31), 13), |
| 232 | + ((2020, 1, 3), (2020, 6, 29), 5), |
| 233 | + ((2020, 6, 29), (2020, 1, 3), 5), |
| 234 | + ((2019, 12, 31), (2021, 1, 1), None), |
| 235 | + ((2019, 12, 31), (2021, 12, 31), None), |
| 236 | + ((2020, 1, 1), (2021, 12, 31), 26), |
| 237 | +] |
| 238 | + |
| 239 | + |
| 240 | +@pytest.mark.parametrize( |
| 241 | + "start_tuple, end_tuple, expected", |
| 242 | + TEST_DATE_RANGES, |
| 243 | + ids=[f"{start}->{end}" for start, end, _ in TEST_DATE_RANGES], |
| 244 | +) |
| 245 | +@pytest.mark.parametrize( |
| 246 | + "filename_format", |
| 247 | + ["%Y_0%j", "ciofs_%Y-%m-%d", "nwgoa_%Y-%m-%d"], |
| 248 | + ids=["CIOFS", "CIOFSOP", "NWGOA"], |
| 249 | +) |
| 250 | +def test_krchunk_json_filtering(filename_format, start_tuple, end_tuple, expected): |
| 251 | + """Check that kerchunk JSON files are correctly filtered based on date range""" |
| 252 | + start = datetime(*start_tuple) |
| 253 | + end = datetime(*end_tuple) |
| 254 | + |
| 255 | + all_files = [f"{d.strftime(filename_format)}.json" for d in MOCK_FILES_DATES] |
| 256 | + mock_fs = MockFileSystem(all_files) |
| 257 | + |
| 258 | + def make_glob_with_check(year: str) -> str: |
| 259 | + """Check that the input string is a year and return it instead of any pattern. |
| 260 | +
|
| 261 | + The return value will be used by `MockFileSystem.glob` which accounts for this. |
| 262 | + """ |
| 263 | + assert year == f"{start.year:04}" or year == f"{end.year:04}" |
| 264 | + return year |
| 265 | + |
| 266 | + try: |
| 267 | + jsons = find_json_files_in_date_range( |
| 268 | + mock_fs, |
| 269 | + make_glob_with_check, |
| 270 | + start, |
| 271 | + end, |
| 272 | + filename_format, |
| 273 | + ) |
| 274 | + |
| 275 | + assert len(jsons) == expected |
| 276 | + except ValueError: |
| 277 | + assert expected is None, f"Expected {expected} JSON files but got ValueError" |
| 278 | + |
| 279 | + |
191 | 280 | def test_user_registry(): |
192 | 281 |
|
193 | 282 | # Create a temporary directory |
|
0 commit comments