Skip to content

Commit 972b4ee

Browse files
committed
Add basic support for directories and S3 prefixes
A rather hacky implementation. This will be tidied up later.
1 parent 95dde18 commit 972b4ee

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

xrenmap/xrenmap.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,15 @@ def open_dataset(
4545
drop_variables: str | Iterable[str] | None = None,
4646
) -> xr.Dataset:
4747
self.temp_dir = tempfile.mkdtemp(prefix="xrenmap-")
48-
ds = read_dataset_from_archive(filename_or_obj, self.temp_dir)
48+
path = pathlib.Path(filename_or_obj)
49+
if path.is_file():
50+
ds = read_dataset_from_archive(filename_or_obj, self.temp_dir)
51+
elif path.is_dir():
52+
ds = read_dataset_from_directory(path)
53+
elif filename_or_obj.startswith("s3://"):
54+
ds = read_dataset_from_directory(filename_or_obj)
55+
else:
56+
raise ValueError(f"{filename_or_obj} is neither a path nor a directory.")
4957
ds.set_close(self.close)
5058
return ds
5159

@@ -66,7 +74,7 @@ def read_dataset_from_archive(
6674
def read_dataset_from_directory(data_dir):
6775
LOGGER.info(f"Processing {data_dir}")
6876
arrays = {
69-
name: rioxarray.open_rasterio(data_dir / (filename + ".TIF")).squeeze()
77+
name: rioxarray.open_rasterio(str(data_dir) + "/" + (filename + ".TIF")).squeeze()
7078
for name, filename in VAR_MAP.items()
7179
}
7280
ds = xr.Dataset(arrays)
@@ -75,8 +83,13 @@ def read_dataset_from_directory(data_dir):
7583

7684

7785
def add_metadata(ds: xr.Dataset, data_dir: pathlib.Path):
78-
root = xml.etree.ElementTree.parse(data_dir / "METADATA.XML").getroot()
79-
86+
if str(data_dir).startswith("s3://"):
87+
import fsspec
88+
fs = fsspec.filesystem("s3")
89+
with fs.open(str(data_dir) + "/" + "METADATA.XML") as fh:
90+
root = xml.etree.ElementTree.parse(fh).getroot()
91+
else:
92+
root = xml.etree.ElementTree.parse(str(data_dir) + "/" + "METADATA.XML").getroot()
8093
points = root.findall("base/spatialCoverage/boundingPolygon/point")
8194
bounds = shapely.Polygon(
8295
[float(p.find("longitude").text), p.find("latitude").text]

0 commit comments

Comments
 (0)