-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Simon Adamov
committed
May 21, 2024
1 parent
244284c
commit 0ba441b
Showing
11 changed files
with
96 additions
and
89 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
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
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
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
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,59 @@ | ||
import functools | ||
from pathlib import Path | ||
|
||
import cartopy.crs as ccrs | ||
import yaml | ||
|
||
|
||
class Config: | ||
""" | ||
Class for loading configuration files. | ||
This class loads a configuration file and provides a way to access its | ||
values as attributes. | ||
""" | ||
|
||
def __init__(self, values): | ||
self.values = values | ||
|
||
@classmethod | ||
def from_file(cls, filepath): | ||
if filepath.endswith(".yaml"): | ||
with open(filepath, encoding="utf-8", mode="r") as file: | ||
return cls(values=yaml.safe_load(file)) | ||
else: | ||
raise NotImplementedError(Path(filepath).suffix) | ||
|
||
def __getattr__(self, name): | ||
keys = name.split(".") | ||
value = self.values | ||
for key in keys: | ||
if key in value: | ||
value = value[key] | ||
else: | ||
return None | ||
if isinstance(value, dict): | ||
return Config(values=value) | ||
return value | ||
|
||
def __getitem__(self, key): | ||
value = self.values[key] | ||
if isinstance(value, dict): | ||
return Config(values=value) | ||
return value | ||
|
||
def __contains__(self, key): | ||
return key in self.values | ||
|
||
def num_data_vars(self): | ||
"""Return the number of data variables for a given key.""" | ||
return len(self.dataset.var_names) | ||
|
||
@functools.cached_property | ||
def coords_projection(self): | ||
"""Return the projection.""" | ||
proj_config = self.values["projection"] | ||
proj_class_name = proj_config["class"] | ||
proj_class = getattr(ccrs, proj_class_name) | ||
proj_params = proj_config.get("kwargs", {}) | ||
return proj_class(**proj_params) |
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
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
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
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
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.