Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
b8raoult committed Jan 16, 2021
1 parent 5e4c0e1 commit 04154a3
Show file tree
Hide file tree
Showing 15 changed files with 143 additions and 91 deletions.
6 changes: 4 additions & 2 deletions climetlab/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
from .core.caching import CACHE as cache
from .core.metadata import init_metadata
from .core.settings import SETTINGS as settings
from .datasets import load as load_dataset
from .datasets import dataset, load_dataset

from .plotting import new_plot
from .plotting import options as plotting_options
from .plotting import plot_map
Expand All @@ -24,7 +25,7 @@

# import logging

__version__ = "0.1.3"
__version__ = "0.1.4"


# if ipython_active:
Expand All @@ -34,6 +35,7 @@
__all__ = [
"load_source",
"load_dataset",
"dataset",
"plot_map",
"new_plot",
"settings",
Expand Down
17 changes: 0 additions & 17 deletions climetlab/dataset.py

This file was deleted.

66 changes: 46 additions & 20 deletions climetlab/datasets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ def source(self, source):
self._source = source
source.dataset = self

def __call__(self, *args, **kwargs):
self._load(*args, **kwargs)
return self

def __len__(self):
return len(self.source)

Expand Down Expand Up @@ -74,25 +78,30 @@ def _module_callback(plugin):
return import_module(plugin, package=__name__).dataset


class DatasetLoader:
class YamlDefinedDataset(Dataset):
def __init__(self, path, dataset):
self._path = path
for k, v in dataset.get("metadata", {}).items():
setattr(self, k, v)
self._src = dataset["source"]
self._args = dataset.get("args", {})

kind = "dataset"
def _load(self, *args, **kwargs):
self.source = climetlab.load_source(self._src, **self._args)

def load_yaml(self, path):
with open(path) as f:
dataset = yaml.load(f.read(), Loader=yaml.SafeLoader)["dataset"]
def __repr__(self):
return f"YAML[{self._path}]"

class Wrapped(Dataset):
def __init__(self, *args, **kwargs):

for k, v in dataset.get("metadata", {}).items():
setattr(self, k, v)
class DatasetLoader:

self.source = climetlab.load_source(
dataset["source"], **dataset.get("args", {})
)
kind = "dataset"

return Wrapped
def load_yaml(self, path):
with open(path) as f:
return YamlDefinedDataset(
path, yaml.load(f.read(), Loader=yaml.SafeLoader)["dataset"]
)

def load_module(self, module):
return import_module(module, package=__name__).dataset
Expand All @@ -101,10 +110,27 @@ def load_entry(self, entry):
return entry.load().dataset


def load(name, *args, **kwargs):
loader = DatasetLoader()
dataset = find_plugin(os.path.dirname(__file__), name, loader)
dataset = dataset(*args, **kwargs)
if getattr(dataset, "name", None) is None:
dataset.name = name
return dataset
class DatasetMaker:
def __call__(self, name, *args, **kwargs):
loader = DatasetLoader()
klass = find_plugin(os.path.dirname(__file__), name, loader)
dataset = klass(*args, **kwargs)
if getattr(dataset, "name", None) is None:
dataset.name = name
return dataset

def __getattr__(self, name):
return self(name.replace("_", "-"))


dataset = DatasetMaker()


def load_dataset(name, *args, **kwargs):
try:
ds = dataset(name)
ds._load(*args, **kwargs)
return ds
except Exception:
# Backwards compatibility
return dataset(name, *args, **kwargs)
7 changes: 5 additions & 2 deletions climetlab/datasets/era5_single_levels.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@


class Era5SingleLevels(Dataset):
def __init__(self, variable, period, domain=None, time=None, grid=None):
def __init__(self, variable):
self.variable = variable

def _load(self, period, domain=None, time=None, grid=None):

request = dict(
variable=variable,
variable=self.variable,
product_type="reanalysis",
)

Expand Down
5 changes: 4 additions & 1 deletion climetlab/datasets/high_low.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ def normalise_01(a):


class HighLow(Dataset):
def __init__(self, **req):
def __init__(self):
pass

def _load(self, **req):
self._fields = []
for date, area, label in SAMPLES:

Expand Down
5 changes: 4 additions & 1 deletion climetlab/datasets/hurricane_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ class HurricaneDatabase(Dataset):

home_page = "https://www.aoml.noaa.gov/hrd/hurdat/Data_Storm.html"

def __init__(self, bassin="atlantic", url=None):
def __init__(self):
pass

def _load(self, bassin="atlantic", url=None):

if url is None:
url = URLS[bassin.lower()]
Expand Down
5 changes: 4 additions & 1 deletion climetlab/datasets/meteonet/ground_stations.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ class MeteonetGroundStations(Meteonet):
See https://github.com/meteofrance/meteonet
"""

def __init__(self, domain="NW", date="20160101"):
def __init__(self):
pass

def _load(self, domain="NW", date="20160101"):

url = "{url}/ground_stations/{domain}_{date}.csv".format(
url=self.URL, domain=domain, date=date
Expand Down
5 changes: 4 additions & 1 deletion climetlab/datasets/meteonet/masks.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ class MeteonetMasks(Meteonet):
See https://github.com/meteofrance/meteonet
"""

def __init__(
def __init__(self):
pass

def _load(
self,
domain="NW",
):
Expand Down
5 changes: 4 additions & 1 deletion climetlab/datasets/meteonet/radar.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ class MeteonetRadar(Meteonet):
See https://github.com/meteofrance/meteonet
"""

def __init__(self, domain="NW", variable="rainfall", year=2016, month=8, part=3):
def __init__(self):
pass

def _load(self, domain="NW", variable="rainfall", year=2016, month=8, part=3):

url = "{url}/radar/radar_coords_{domain}.npz".format(
url=self.URL, domain=domain
Expand Down
5 changes: 4 additions & 1 deletion climetlab/datasets/meteonet/weather_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ class MeteonetWeatherModels(Meteonet):
See https://github.com/meteofrance/meteonet
"""

def __init__(
def __init__(self):
pass

def _load(
self, model="arome", variable="2m", domain="NW", date="20180501", time="0000"
):

Expand Down
10 changes: 7 additions & 3 deletions climetlab/datasets/weather_bench.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,20 @@ class WeatherBench(Dataset):
}
"""

def __init__(self, parameter="geopotential_500", resolution=5.625):
def __init__(self):
pass

def _load(self, parameter="geopotential_500", resolution=5.625):

self.check_parameter(
"parameter",
parameter,
"10m_u_component_of_wind",
"10m_v_component_of_wind",
"2m_temperature",
"constants",
"geopotential" "geopotential_500",
# "constants",
"geopotential",
"geopotential_500",
"potential_vorticity",
"relative_humidity",
"specific_humidity",
Expand Down
1 change: 1 addition & 0 deletions climetlab/plotting/drivers/magics/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

try:
import Magics

Magics.strict_mode()
except Exception as e:
print(e, file=sys.stderr)
Expand Down
14 changes: 8 additions & 6 deletions docs/examples/02-source-url.ipynb

Large diffs are not rendered by default.

19 changes: 16 additions & 3 deletions docs/examples/09-weatherbench.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,20 @@
"metadata": {
"tags": []
},
"outputs": [],
"outputs": [
{
"output_type": "error",
"ename": "AttributeError",
"evalue": "'WeatherBench' object has no attribute '_load'",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-4-c4fee7942768>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mds\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcml\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mload_dataset\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"weather-bench\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;32m~/git/climetlab/climetlab/datasets/__init__.py\u001b[0m in \u001b[0;36mload\u001b[0;34m(name, *args, **kwargs)\u001b[0m\n\u001b[1;32m 116\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mload\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 117\u001b[0m \u001b[0mds\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdataset\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 118\u001b[0;31m \u001b[0mds\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_load\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 119\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mds\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mAttributeError\u001b[0m: 'WeatherBench' object has no attribute '_load'"
]
}
],
"source": [
"ds = cml.load_dataset(\"weather-bench\")"
]
Expand Down Expand Up @@ -1268,9 +1281,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.5"
"version": "3.8.2-final"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
}
64 changes: 32 additions & 32 deletions docs/examples/10-meteonet.ipynb

Large diffs are not rendered by default.

0 comments on commit 04154a3

Please sign in to comment.