Skip to content
This repository has been archived by the owner on Apr 24, 2024. It is now read-only.

Commit

Permalink
add method outside to open_xarray_conn c>
Browse files Browse the repository at this point in the history
- Fix extent in `open_xarray_conn`.
- Fix load env values.
- Fix assert in filter test.
- Add parameter `method=outside` to `open_xarray_conn`.
  • Loading branch information
felipem775 committed Jan 7, 2022
1 parent c1362b7 commit 03fb070
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 6 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
# Changelog
## 0.9.3
- Fix extent in `open_xarray_conn`.
- Fix load env values.
- Fix assert in filter test.
- Add parameter `method=outside` to `open_xarray_conn`.

## 0.9.2
- Fix SECRET_AUTH env key is not required.
Expand Down
2 changes: 1 addition & 1 deletion datahub/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
""" Datahub client"""
__version__ = "0.9.2"
__version__ = "0.9.3"
52 changes: 49 additions & 3 deletions datahub/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from io import StringIO
import json
import math
import numpy as np
import os
import requests
import urllib.request
Expand Down Expand Up @@ -286,27 +287,72 @@ def _coordinates_to_string(self, coordinates):
logger.debug(f"coordinates={coordinates}")
return text

def open_xarray_conn(self, dates=None, extent=None):
def open_xarray_conn(self, dates=None, extent=None, method=None):

if method and method == "outside" and not extent:
raise Exception(f"Outside method requires an extent")

list_conn = [dataset.opendap_url for dataset in self.datasets]
logger.debug(f"opening: {','.join(list_conn)}")

ds = self._join_datasets(list_conn)
# ds = self._join_datasets(list_conn)
if dates:
start = dates["start"] if "start" in dates else None
end = dates["end"] if "end" in dates else None
if "time" in ds.dims:
if method == "outside" and start:
start = (
start
- np.diff(ds["time"].values)
.max()
.astype("timedelta64[s]")
.item()
)
end = (
end
+ np.diff(ds["time"].values)
.max()
.astype("timedelta64[s]")
.item()
)
ds = ds.sel(time=slice(start, end))
elif "t" in ds.dims:
if method == "outside" and start:
start = (
start
- np.diff(ds["t"].values).max().astype("timedelta64[s]").item()
)
end = (
end
+ np.diff(ds["t"].values).max().astype("timedelta64[s]").item()
)
ds = ds.sel(t=slice(start, end))
if extent:
if "longitud" in ds.dims:

if "longitude" in ds.dims:
if method == "outside":
extent["west"] = (
extent["west"] - np.diff(ds["longitude"].values).max()
)
extent["east"] = (
extent["east"] - np.diff(ds["longitude"].values).max()
)
extent["south"] = (
extent["south"] - np.diff(ds["latitude"].values).max()
)
extent["north"] = (
extent["north"] - np.diff(ds["latitude"].values).max()
)
ds = ds.sel(
longitude=slice(extent["west"], extent["east"]),
latitude=slice(extent["south"], extent["north"]),
)
elif "lon" in ds.dims:
if method == "outside":
extent["west"] = extent["west"] - np.diff(ds["lon"].values).max()
extent["east"] = extent["east"] - np.diff(ds["lon"].values).max()
extent["south"] = extent["south"] - np.diff(ds["lat"].values).max()
extent["north"] = extent["north"] - np.diff(ds["lat"].values).max()
ds = ds.sel(
lon=slice(extent["west"], extent["east"]),
lat=slice(extent["south"], extent["north"]),
Expand Down
50 changes: 48 additions & 2 deletions datahub/dataset.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from bs4 import BeautifulSoup
import logging
import numpy as np
import requests
import xarray

Expand Down Expand Up @@ -190,23 +191,68 @@ def _coordinates_to_string(self, coordinates):
logger.debug(f"coordinates={text}")
return text

def open_xarray_conn(self, dates=None, extent=None):
def open_xarray_conn(self, dates=None, extent=None, method=None):
if method and method == "outside" and not extent:
raise Exception(f"Outside method requires an extent")
logger.debug(f"opening {self.opendap_url}")
ds = xarray.open_dataset(self.opendap_url)
if dates:
start = dates["start"] if "start" in dates else None
end = dates["end"] if "end" in dates else None
if "time" in ds.dims:
if method == "outside" and start:
start = (
start
- np.diff(ds["time"].values)
.max()
.astype("timedelta64[s]")
.item()
)
end = (
end
+ np.diff(ds["time"].values)
.max()
.astype("timedelta64[s]")
.item()
)
ds = ds.sel(time=slice(start, end))
elif "t" in ds.dims:
if method == "outside" and start:
start = (
start
- np.diff(ds["t"].values).max().astype("timedelta64[s]").item()
)
end = (
end
+ np.diff(ds["t"].values).max().astype("timedelta64[s]").item()
)
ds = ds.sel(t=slice(start, end))
if extent:
if "longitud" in ds.dims:
if "longitude" in ds.dims:
if method == "outside":
extent["west"] = (
extent["west"] - np.diff(ds["longitude"].values).max()
)
extent["east"] = (
extent["east"] - np.diff(ds["longitude"].values).max()
)
extent["south"] = (
extent["south"] - np.diff(ds["latitude"].values).max()
)
extent["north"] = (
extent["north"] - np.diff(ds["latitude"].values).max()
)
ds = ds.sel(
longitude=slice(extent["west"], extent["east"]),
latitude=slice(extent["south"], extent["north"]),
)
elif "lon" in ds.dims:
if method == "outside":
extent["west"] = extent["west"] - np.diff(ds["lon"].values).max()
extent["east"] = extent["east"] - np.diff(ds["lon"].values).max()
extent["south"] = extent["south"] - np.diff(ds["lat"].values).max()
extent["north"] = extent["north"] - np.diff(ds["lat"].values).max()

ds = ds.sel(
lon=slice(extent["west"], extent["east"]),
lat=slice(extent["south"], extent["north"]),
Expand Down

0 comments on commit 03fb070

Please sign in to comment.