|
| 1 | +# The MIT License (MIT) |
| 2 | +# Copyright (c) 2021 by Brockmann Consult GmbH and contributors |
| 3 | +# |
| 4 | +# Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 5 | +# this software and associated documentation files (the "Software"), to deal in |
| 6 | +# the Software without restriction, including without limitation the rights to |
| 7 | +# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
| 8 | +# of the Software, and to permit persons to whom the Software is furnished to do |
| 9 | +# so, subject to the following conditions: |
| 10 | +# |
| 11 | +# The above copyright notice and this permission notice shall be included in all |
| 12 | +# copies or substantial portions of the Software. |
| 13 | +# |
| 14 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 | +# SOFTWARE. |
| 21 | + |
| 22 | +from typing import Dict, Any |
| 23 | + |
| 24 | +import fsspec |
| 25 | +import fsspec.implementations.local |
| 26 | +import xarray as xr |
| 27 | + |
| 28 | +from .log import LOGGER |
| 29 | +from .log import log_duration |
| 30 | + |
| 31 | + |
| 32 | +class DatasetVerifier: |
| 33 | + def __init__(self, |
| 34 | + output_path: str, |
| 35 | + output_s3_kwargs: Dict[str, Any] = None, |
| 36 | + verify_enabled: Dict[str, Any] = None, |
| 37 | + verify_open_params: Dict[str, Any] = None, |
| 38 | + dry_run: bool = False): |
| 39 | + if not output_path: |
| 40 | + raise ValueError('output_path must be given') |
| 41 | + self._output_path = output_path |
| 42 | + self._output_s3_kwargs = output_s3_kwargs |
| 43 | + self._verify_enabled = verify_enabled |
| 44 | + self._verify_open_params = verify_open_params or {} |
| 45 | + self._dry_run = dry_run |
| 46 | + |
| 47 | + def verify_dataset(self): |
| 48 | + |
| 49 | + if not self._verify_enabled: |
| 50 | + LOGGER.info('Dataset verification disabled.') |
| 51 | + return |
| 52 | + |
| 53 | + with log_duration(f'Verifying dataset'): |
| 54 | + if not self._dry_run: |
| 55 | + if self._output_s3_kwargs or self._output_path.startswith('s3://'): |
| 56 | + fs = fsspec.filesystem('s3', **(self._output_s3_kwargs or {})) |
| 57 | + else: |
| 58 | + fs = fsspec.filesystem('file') |
| 59 | + store = fs.get_mapper(self._output_path, check=False, create=False) |
| 60 | + # noinspection PyBroadException |
| 61 | + try: |
| 62 | + dataset = xr.open_zarr(store, **self._verify_open_params) |
| 63 | + LOGGER.info(dataset) |
| 64 | + LOGGER.info('Dataset verification passed.') |
| 65 | + except BaseException as e: |
| 66 | + LOGGER.error('Dataset verification failed!') |
| 67 | + else: |
| 68 | + LOGGER.info('Dataset verification skipped, it is a dry run.') |
0 commit comments