Skip to content

Commit

Permalink
Standardise style of kwargs
Browse files Browse the repository at this point in the history
  - Use Optional[Dict] for all third-party libraries kwargs
  - Code review for ioos#280

  Co-authored-by: Filipe <ocefpaf@gmail.com>
  Co-authored-by: Alex Kerney <abk@mac.com>
  • Loading branch information
vinisalazar authored and ocefpaf committed Dec 8, 2022
1 parent 0692a7b commit 9892267
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions erddapy/core/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@
import xarray as xr
from netCDF4 import Dataset

<<<<<<< HEAD
def to_pandas(url: str, requests_kwargs: Optional[Dict] = None, **kw) -> "pd.DataFrame":
=======
def to_pandas(
url: str, requests_kwargs: Optional[Dict] = None, **pandas_kwargs
) -> pd.DataFrame:
>>>>>>> bf7cbb2 (Renaming **kw according to each library)
url: str,
requests_kwargs: Optional[Dict] = None,
pandas_kwargs: Optional[Dict] = None,
) -> "pd.DataFrame":
"""
Convert a URL to Pandas DataFrame.
Expand All @@ -33,30 +31,27 @@ def to_pandas(
"""
if requests_kwargs is None:
requests_kwargs = {}
if pandas_kwargs is None:
pandas_kwargs = {}
data = urlopen(url, **requests_kwargs)
try:
return pd.read_csv(data, **pandas_kwargs)
except Exception as e:
raise ValueError(f"Could not read url {url} with Pandas.read_csv.") from e


<<<<<<< HEAD
def to_ncCF(url: str, protocol: str = None, **kw) -> "Dataset":
"""Convert a URL to a netCDF4 Dataset."""
=======
def to_ncCF(
url: str,
protocol: str = None,
requests_kwargs: Optional[Dict] = None,
) -> Dataset:
) -> "Dataset":
"""
Convert a URL to a netCDF4 Dataset.
url: URL to request data from.
protocol: 'griddap' or 'tabledap'.
requests_kwargs: arguments to be passed to urlopen method (including auth).
"""
>>>>>>> bf7cbb2 (Renaming **kw according to each library)
if protocol == "griddap":
raise ValueError(
f"Cannot use .ncCF with griddap protocol. The URL you tried to access is: '{url}'.",
Expand All @@ -70,28 +65,34 @@ def to_xarray(
url: str,
response="opendap",
requests_kwargs: Optional[Dict] = None,
**xarray_kwargs,
xarray_kwargs: Optional[Dict] = None,
) -> "xr.Dataset":
"""
Convert a URL to an xarray dataset.
url: URL to request data from.
response: type of response to be requested from the server.
requests_kwargs: arguments to be passed to urlopen method
requests_kwargs: arguments to be passed to urlopen method.
xarray_kwargs: kwargs to be passed to third-party library (xarray).
"""
import xarray as xr

if requests_kwargs is None:
requests_kwargs = {}
if xarray_kwargs is None:
xarray_kwargs = {}
if response == "opendap":
return xr.open_dataset(url, **xarray_kwargs)
else:
nc = _nc_dataset(url, **requests_kwargs)
return xr.open_dataset(xr.backends.NetCDF4DataStore(nc), **xarray_kwargs)


def to_iris(url: str, requests_kwargs: Optional[Dict] = None, **iris_kwargs):
def to_iris(
url: str,
requests_kwargs: Optional[Dict] = None,
iris_kwargs: Optional[Dict] = None,
):
"""
Convert a URL to an iris CubeList.
Expand All @@ -103,6 +104,8 @@ def to_iris(url: str, requests_kwargs: Optional[Dict] = None, **iris_kwargs):

if requests_kwargs is None:
requests_kwargs = {}
if iris_kwargs is None:
iris_kwargs = {}
data = urlopen(url, **requests_kwargs)
with _tempnc(data) as tmp:
cubes = iris.load_raw(tmp, **iris_kwargs)
Expand Down

0 comments on commit 9892267

Please sign in to comment.