Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor functions out of erddapy.erddapy module
- Move URL handling functions to erddapy.core.url - Move GRIDDAP handling functions to erddapy.core.griddap - Refactor imports
- Loading branch information
1 parent
a47642a
commit 15d66d5
Showing
4 changed files
with
241 additions
and
219 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
"""Griddap handling.""" | ||
|
||
import functools | ||
from typing import Dict, List, Optional, Tuple, Union | ||
|
||
import pandas as pd | ||
|
||
from erddapy.core.url import urlopen | ||
|
||
ListLike = Union[List[str], Tuple[str]] | ||
OptionalStr = Optional[str] | ||
|
||
|
||
@functools.lru_cache(maxsize=256) | ||
def _griddap_get_constraints( | ||
dataset_url: str, | ||
step: int, | ||
) -> Tuple[Dict, List, List]: | ||
""" | ||
Fetch metadata of griddap dataset and set initial constraints. | ||
Step size is applied to all dimensions. | ||
""" | ||
dds_url = f"{dataset_url}.dds" | ||
url = urlopen(dds_url) | ||
data = url.read().decode() | ||
dims, *variables = data.split("GRID") | ||
dim_list = dims.split("[")[:-1] | ||
dim_names, variable_names = [], [] | ||
for dim in dim_list: | ||
dim_name = dim.split(" ")[-1] | ||
dim_names.append(dim_name) | ||
for var in variables: | ||
phrase, *__ = var.split("[") | ||
var_name = phrase.split(" ")[-1] | ||
variable_names.append(var_name) | ||
table = pd.DataFrame({"dimension name": [], "min": [], "max": [], "length": []}) | ||
for dim in dim_names: | ||
url = f"{dataset_url}.csvp?{dim}" | ||
data = pd.read_csv(url).values | ||
if dim == "time": | ||
data_start = data[-1][0] | ||
else: | ||
data_start = data[0][0] | ||
|
||
meta = pd.DataFrame( | ||
[ | ||
{ | ||
"dimension name": dim, | ||
"min": data_start, | ||
"max": data[-1][0], | ||
"length": len(data), | ||
}, | ||
], | ||
) | ||
table = pd.concat([table, meta]) | ||
table = table.set_index("dimension name", drop=True) | ||
constraints_dict = {} | ||
for dim, data in table.iterrows(): | ||
constraints_dict[f"{dim}>="] = data["min"] | ||
constraints_dict[f"{dim}<="] = data["max"] | ||
constraints_dict[f"{dim}_step"] = step | ||
|
||
return constraints_dict, dim_names, variable_names | ||
|
||
|
||
def _griddap_check_constraints(user_constraints: Dict, original_constraints: Dict): | ||
"""Check that constraints changed by user match those expected by dataset.""" | ||
if user_constraints.keys() != original_constraints.keys(): | ||
raise ValueError( | ||
"keys in e.constraints have changed. Re-run e.griddap_initialize", | ||
) | ||
|
||
|
||
def _griddap_check_variables(user_variables: ListLike, original_variables: ListLike): | ||
"""Check user has not requested variables that do not exist in dataset.""" | ||
invalid_variables = [] | ||
for variable in user_variables: | ||
if variable not in original_variables: | ||
invalid_variables.append(variable) | ||
if invalid_variables: | ||
raise ValueError( | ||
f"variables {invalid_variables} are not present in dataset. Re-run e.griddap_initialize", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.