diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 776ba95..4fc85d2 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -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 diff --git a/erddapy/core/griddap.py b/erddapy/core/griddap.py index 69f894b..8a60529 100644 --- a/erddapy/core/griddap.py +++ b/erddapy/core/griddap.py @@ -2,8 +2,6 @@ from __future__ import annotations -import functools - import pandas as pd from erddapy.core.url import urlopen @@ -11,7 +9,6 @@ OptionalList = list[str] | tuple[str] | None -@functools.lru_cache(maxsize=128) def _griddap_get_constraints( dataset_url: str, step: int, diff --git a/erddapy/erddapy.py b/erddapy/erddapy.py index a7fdf3b..ac8c4f6 100644 --- a/erddapy/erddapy.py +++ b/erddapy/erddapy.py @@ -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: @@ -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 @@ -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, @@ -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}" ( diff --git a/notebooks/01a-griddap.ipynb b/notebooks/01a-griddap.ipynb index b4c1f83..94ee236 100644 --- a/notebooks/01a-griddap.ipynb +++ b/notebooks/01a-griddap.ipynb @@ -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." ] }, { @@ -83,10 +82,9 @@ "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}\")" ] }, @@ -94,13 +92,45 @@ "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, @@ -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, @@ -332,7 +355,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.12.5" + "version": "3.13.0" } }, "nbformat": 4,