|
| 1 | +"""ASAR Xarray Dataset Reader.""" |
| 2 | + |
1 | 3 | import os |
2 | 4 | from typing import Dict, Any |
3 | 5 |
|
@@ -26,30 +28,54 @@ def get_attributes(gdal_dataset: gdal.Dataset) -> Dict[str, Any]: |
26 | 28 | process_records_metadata(gdal_dataset, attributes) |
27 | 29 | process_derived_subdatasets_metadata(gdal_dataset, attributes) |
28 | 30 |
|
29 | | - |
30 | 31 | attributes['chirp_parameters'] = get_chirp_parameters(gdal_dataset) |
31 | 32 |
|
32 | 33 | return attributes |
33 | 34 |
|
34 | 35 |
|
35 | 36 | def open_asar_dataset(filepath: str | os.PathLike[Any] | ReadBuffer[Any] | AbstractDataStore) -> xr.Dataset: |
| 37 | + """ |
| 38 | + Open an ASAR dataset and converts it into an xarray Dataset. |
| 39 | +
|
| 40 | + This function reads the metadata and pixel data from the given ASAR dataset file, |
| 41 | + processes the metadata into attributes, and constructs an xarray Dataset. |
| 42 | +
|
| 43 | + :param filepath: The path to the ASAR dataset file. It can be a string, |
| 44 | + a PathLike object, a ReadBuffer, or an AbstractDataStore. |
| 45 | + :return: An xarray Dataset containing the pixel data and metadata attributes. |
| 46 | + :raises NotImplementedError: If the filepath is not a string. |
| 47 | + """ |
36 | 48 | if not isinstance(filepath, str): |
37 | 49 | raise NotImplementedError(f'Filepath type {type(filepath)} is not supported') |
| 50 | + |
38 | 51 | logger.debug('Opening ASAR dataset {}', filepath) |
| 52 | + |
| 53 | + # Open the dataset using a custom reader |
39 | 54 | gdal_dataset: gdal.Dataset = reader.get_gdal_dataset(filepath) |
| 55 | + |
| 56 | + # Extract metadata attributes |
40 | 57 | attributes = get_attributes(gdal_dataset) |
| 58 | + |
| 59 | + # Read pixel data from the dataset |
41 | 60 | data = gdal_dataset.ReadAsArray() |
42 | | - dataset: xr.Dataset = xr.Dataset(data_vars={'pixel_values': (('y', 'x'), data)}, |
43 | | - coords={ |
44 | | - 'x': np.arange(data.shape[1]), |
45 | | - 'y': np.arange(data.shape[0]) |
46 | | - }, attrs=attributes) |
| 61 | + |
| 62 | + # Create an xarray Dataset with pixel data and metadata attributes |
| 63 | + dataset: xr.Dataset = xr.Dataset( |
| 64 | + data_vars={'pixel_values': (('y', 'x'), data)}, |
| 65 | + coords={ |
| 66 | + 'x': np.arange(data.shape[1]), |
| 67 | + 'y': np.arange(data.shape[0]) |
| 68 | + }, |
| 69 | + attrs=attributes |
| 70 | + ) |
| 71 | + |
47 | 72 | return dataset |
48 | 73 |
|
49 | 74 |
|
50 | 75 | def get_chirp_parameters(dataset: gdal.Dataset) -> dict[str, Any]: |
51 | 76 | """ |
52 | | - Parses dataset metadata for chirp parameters. |
| 77 | + Parse dataset metadata for chirp parameters. |
| 78 | +
|
53 | 79 | :param dataset: ASAR dataset. |
54 | 80 | :return: dictionary with chirp parameters. |
55 | 81 | """ |
|
0 commit comments