From ed44723b42ddf58d806c81fa5b66a7daf7f04bea Mon Sep 17 00:00:00 2001 From: Carsten Hinz <c.hinz@fz-juelich.de> Date: Mon, 27 May 2024 14:53:07 +0200 Subject: [PATCH] Changed functions and how to use added an utility function to update the list of available variables. Updated get function to reduce the number of repetitions updated README --- README.md | 3 ++- toargridding/setupFunctions.py | 13 +++++++++++++ toargridding/static_metadata.py | 16 +++++++++------- 3 files changed, 24 insertions(+), 8 deletions(-) create mode 100644 toargridding/setupFunctions.py diff --git a/README.md b/README.md index dbd1511..97bc4cf 100644 --- a/README.md +++ b/README.md @@ -153,7 +153,8 @@ The first supported grid is a regular grid with longitude and latitude. # Supported Variables -At the moment only a limited number of variables from the TOAR database is supported. +This module supports all variables of the TOAR database (Extraction: 2024-05-27). They can be identified by their "cf_standardname" or their name as stored in the TOAR database. +The full list can be accesses by querying the TOAR database, e.g. with https://toar-data.fz-juelich.de/api/v2/variables/?limit=None # Supported Time intervals diff --git a/toargridding/setupFunctions.py b/toargridding/setupFunctions.py new file mode 100644 index 0000000..08ff791 --- /dev/null +++ b/toargridding/setupFunctions.py @@ -0,0 +1,13 @@ +from toargridding.static_metadata import TOAR_VARIABLES_METADATA_PATH +import requests +import json + + +def updateTOARVariables(): + """Download the most recent list of variables from the TOAR database + This overwrites the current static file + """ + response = requests.get("https://toar-data.fz-juelich.de/api/v2/variables/?limit=None") + varList = response.json() + with open(TOAR_VARIABLES_METADATA_PATH, "w") as f: + json.dump(varList, f, indent=2) \ No newline at end of file diff --git a/toargridding/static_metadata.py b/toargridding/static_metadata.py index 71789d5..dfca7ef 100644 --- a/toargridding/static_metadata.py +++ b/toargridding/static_metadata.py @@ -59,13 +59,15 @@ class TOARVariable: return: provides direct access to the meta data of the selected variable """ - if name == "" or name == None: - raise ValueError("No name provided for variable.") - for var in cls.vars: - if var.standard_name == name or var.name== name: - return var - else: - raise ValueError(f"TOAR Database contains no variable {name}") + #TODO: update README + try: + return cls.get_from_cf_standardname(standard_name=name) + except ValueError as e: + pass + try: + return cls.get_from_name(name=name) + except ValueError as e: + raise ValueError(f"TOAR Database contains no variable with cf_standardname or name '{name}'") @classmethod def get_from_cf_standardname(cls, standard_name: str) -> "TOARVariable": """searches the known variables for the given cf_standardname -- GitLab