Skip to content

Commit

Permalink
Renaming **kw according to each library
Browse files Browse the repository at this point in the history
  - Code review for ioos#280
  - Rename **kw variables to the name of each library
  • Loading branch information
vinisalazar committed Feb 22, 2023
1 parent 3064ceb commit de343c3
Showing 1 changed file with 40 additions and 14 deletions.
54 changes: 40 additions & 14 deletions erddapy/core/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,70 +17,96 @@
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)
"""
Convert a URL to Pandas DataFrame.
url: URL to request data from.
requests_kwargs: arguments to be passed to urlopen method.
**kw: kwargs to be passed to third-party library (pandas).
**pandas_kwargs: kwargs to be passed to third-party library (pandas).
"""
if requests_kwargs is None:
requests_kwargs = {}
data = urlopen(url, **requests_kwargs)
try:
return pd.read_csv(data, **kw)
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:
"""
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}'.",
)
auth = kw.pop("auth", None)
return _nc_dataset(url, auth=auth, **kw)
if requests_kwargs is None:
requests_kwargs = {}
auth = requests_kwargs.pop("auth", None)
return _nc_dataset(url, auth=auth, **requests_kwargs)


def to_xarray(
url: str, response="opendap", requests_kwargs: Optional[Dict] = None, **kw
url: str,
response="opendap",
requests_kwargs: Optional[Dict] = None,
**xarray_kwargs,
) -> "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.
**kw: kwargs to be passed to third-party library (xarray).
xarray_kwargs: kwargs to be passed to third-party library (xarray).
"""
import xarray as xr

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


def to_iris(url: str, requests_kwargs: Optional[Dict] = None, **kw):
def to_iris(url: str, requests_kwargs: Optional[Dict] = None, **iris_kwargs):
"""
Convert a URL to an iris CubeList.
url: URL to request data from.
requests_kwargs: arguments to be passed to urlopen method.
**kw: kwargs to be passed to third-party library (iris).
iris_kwargs: kwargs to be passed to third-party library (iris).
"""
import iris

if requests_kwargs is None:
requests_kwargs = {}
data = urlopen(url, **requests_kwargs)
with _tempnc(data) as tmp:
cubes = iris.load_raw(tmp, **kw)
cubes = iris.load_raw(tmp, **iris_kwargs)
_ = [cube.data for cube in cubes]
return cubes

0 comments on commit de343c3

Please sign in to comment.