Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically call griddap_initialize if a dataset_id exists #353

Merged
merged 5 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ repos:
- id: add-trailing-comma

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.7.2
rev: v0.8.0
hooks:
- id: ruff
args: ["--fix", "--show-fixes"]
- id: ruff-format

- repo: https://github.com/nbQA-dev/nbQA
rev: 1.8.7
rev: 1.9.1
hooks:
- id: nbqa-check-ast
- id: nbqa-black
Expand Down
3 changes: 0 additions & 3 deletions erddapy/core/griddap.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@

from __future__ import annotations

import functools

import pandas as pd

from erddapy.core.url import urlopen

OptionalList = list[str] | tuple[str] | None


@functools.lru_cache(maxsize=128)
def _griddap_get_constraints(
dataset_url: str,
step: int,
Expand Down
25 changes: 16 additions & 9 deletions erddapy/erddapy.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@

# Objects used by downstream packages
__all__ = [
"ERDDAP",
"_check_substrings",
"_distinct",
"_format_constraints_url",
"_quote_string_constraints",
"parse_dates",
"urlopen",
"ERDDAP",
]

if TYPE_CHECKING:
Expand Down Expand Up @@ -141,11 +141,11 @@ def __init__(
self.response = response

# Initialized only via properties.
self.constraints: dict | None = None
self.server_functions: dict | None = None
self.dataset_id: OptionalStr = None
self.requests_kwargs: dict = {}
self.auth: tuple | None = None

self.constraints: dict | None = None
self.variables: OptionalList | None = None
self.dim_names: OptionalList | None = None

Expand All @@ -157,6 +157,16 @@ def __init__(
self._dataset_id: OptionalStr = None
self._variables: dict = {}

@property
def dataset_id(self) -> str:
"""dataset_id property."""
return self._dataset_id

@dataset_id.setter
def dataset_id(self, value: str) -> None:
self._dataset_id = value
self.griddap_initialize(dataset_id=value)

def griddap_initialize(
self: ERDDAP,
dataset_id: OptionalStr = None,
Expand All @@ -171,15 +181,12 @@ def griddap_initialize(

"""
dataset_id = dataset_id if dataset_id else self.dataset_id
msg = f"Method only valid using griddap protocol, got {self.protocol}"
if self.protocol != "griddap":
raise ValueError(msg)
# Short-circuit for opendap and/or non-griddap datasets.
if self.protocol != "griddap" or self.response == "opendap":
return
msg = f"Must set a valid dataset_id, got {self.dataset_id}"
if dataset_id is None:
raise ValueError(msg)
# Return the opendap URL without any slicing.
if self.response == "opendap":
return

metadata_url = f"{self.server}/griddap/{self.dataset_id}"
(
Expand Down
49 changes: 36 additions & 13 deletions notebooks/01a-griddap.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@
"CAVEAT: Note that ERDDAP can serve gridded data with longitudes in the 0–360 format or -180–180.\n",
"The user must inspect the dataset and modify your query accordingly.\n",
"\n",
"Information on the griddap dataset is fetched with `griddap_initialize`.\n",
"This fills in the `variables` and `constraints` properties for that dataset."
"Information on the griddap dataset is fetched automatically and the `variables` and `constraints` properties for that dataset are filled using ERDDAP's defaults."
]
},
{
Expand All @@ -83,24 +82,55 @@
"source": [
"import json\n",
"\n",
"e.griddap_initialize()\n",
"constraints = json.dumps(e.constraints, indent=1)\n",
"\n",
"print(f\"variables in this dataset:\\n\\n{e.variables}\")\n",
"constraints = json.dumps(e.constraints, indent=1)\n",
"print(f\"\\nconstraints of this dataset:\\n\\n{constraints}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The default behaviour is to use erddap standard subsetting to return all variables at the most recent timestep and every point of the remaining dimensions.\n",
"ERDDAP's standard subsetting return all variables at the most recent timestep and every point of the remaining dimensions.\n",
"\n",
"This can result in large data requests!\n",
"However, the values of the constraints can be changed and variables dropped before data set is downloaded.\n",
"Here we will download only one variable from that list."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"e.constraints[\"latitude_step\"] = 10\n",
"e.constraints[\"longitude_step\"] = 10\n",
"\n",
"constraints = json.dumps(e.constraints, indent=1)\n",
"print(f\"\\nconstraints of this dataset:\\n\\n{constraints}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The user can reset to the defaults with `griddap_initialize()`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"e.griddap_initialize()\n",
"\n",
"constraints = json.dumps(e.constraints, indent=1)\n",
"print(f\"\\nconstraints of this dataset:\\n\\n{constraints}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down Expand Up @@ -258,13 +288,6 @@
"e.dataset_id = \"etopo5_lon180\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The data can be downloaded immediately, no need to run `griddap_initialize`\n"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down Expand Up @@ -332,7 +355,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.5"
"version": "3.13.0"
}
},
"nbformat": 4,
Expand Down
Loading