src
This commit is contained in:
parent
a66b5a7af0
commit
884d75fd2f
|
@ -0,0 +1,168 @@
|
|||
import os
|
||||
import cdsapi
|
||||
|
||||
DEFAULT_URL = 'https://cds-beta.climate.copernicus.eu/api'
|
||||
ENV_CDSAPI_URL = 'CDSAPI_URL'
|
||||
ENV_CDSAPI_KEY = 'CDSAPI_KEY'
|
||||
|
||||
CONFIG_URL = 'url'
|
||||
CONFIG_KEY = 'key'
|
||||
|
||||
class AuthCDS:
|
||||
def __init__(self, verbose=False):
|
||||
"""Initialize AuthCDS with optional verbose logging."""
|
||||
self.verbose = verbose
|
||||
self.CONFIG_FILE = ".cdsapirc"
|
||||
self.CURRENT_CDSAPIRC = "./" + self.CONFIG_FILE
|
||||
# Expands `~` to the home directory path
|
||||
self.HOME_CDSAPIRC = os.path.expanduser("~/" + self.CONFIG_FILE)
|
||||
self.DEFAULT_URL = DEFAULT_URL
|
||||
# self.ENV_CDSAPI_URL = ENV_CDSAPI_URL
|
||||
# self.ENV_CDSAPI_KEY = ENV_CDSAPI_KEY
|
||||
# self.CONFIG_URL = CONFIG_URL
|
||||
# self.CONFIG_KEY = CONFIG_KEY
|
||||
|
||||
def log(self, message, force=False):
|
||||
"""Log messages if verbose mode is enabled."""
|
||||
if self.verbose or force:
|
||||
print(message)
|
||||
|
||||
def create_config(self, url, key):
|
||||
"""Create a configuration dictionary."""
|
||||
return {self.CONFIG_URL: url, self.CONFIG_KEY: key}
|
||||
|
||||
def config_from_env(self):
|
||||
"""Retrieve configuration from environment variables."""
|
||||
url = os.environ.get(ENV_CDSAPI_URL)
|
||||
key = os.environ.get(ENV_CDSAPI_KEY)
|
||||
self.log("ENV - %s %s" % ( url, key))
|
||||
if url is None and key is None:
|
||||
self.log("env is not configured")
|
||||
return None
|
||||
|
||||
config = {}
|
||||
config[CONFIG_URL] = url
|
||||
config[CONFIG_KEY] = key
|
||||
|
||||
self.log(f"Configuration from environment {config}")
|
||||
return config
|
||||
|
||||
def config_to_env(self, config):
|
||||
"""Set configuration in environment variables."""
|
||||
os.environ[ENV_CDSAPI_URL] = config[CONFIG_URL]
|
||||
os.environ[ENV_CDSAPI_KEY] = config[CONFIG_KEY]
|
||||
self.log(f"Set environment variables {
|
||||
ENV_CDSAPI_URL}, {ENV_CDSAPI_KEY}")
|
||||
|
||||
def config_to_file(self, config, config_path=None):
|
||||
"""Save configuration to a file."""
|
||||
if not config_path:
|
||||
config_path = self.HOME_CDSAPIRC
|
||||
else:
|
||||
# Expands `~` to the full home directory path
|
||||
config_path = os.path.expanduser(config_path)
|
||||
|
||||
URL = config[CONFIG_URL]
|
||||
KEY = config[CONFIG_KEY]
|
||||
with open(config_path, 'w') as file:
|
||||
file.write(f"url: {URL}\nkey: {KEY}\n")
|
||||
self.log(f"Saved Configuration file {config_path}")
|
||||
|
||||
def config_to_home(self, config):
|
||||
"""Save configuration to user home."""
|
||||
self.config_to_file(config, self.HOME_CDSAPIRC)
|
||||
|
||||
def remove_config_from_home(self, config):
|
||||
"""remove configuration from user home."""
|
||||
self.remove_conf_credentials(config, from_home=True)
|
||||
|
||||
def config_from_file(self, scan_all=False, custom_path=None):
|
||||
"""Retrieve configuration from a file."""
|
||||
config = None
|
||||
check_paths = [self.CURRENT_CDSAPIRC, self.HOME_CDSAPIRC]
|
||||
if custom_path:
|
||||
check_paths = [custom_path]
|
||||
|
||||
for path in check_paths:
|
||||
if os.path.exists(path):
|
||||
found_config = cdsapi.api.read_config(path)
|
||||
self.log(f"Configuration from file {path}: {found_config}")
|
||||
config = config or found_config
|
||||
if not scan_all:
|
||||
break
|
||||
return config
|
||||
|
||||
def authenticate(self, set_environment=True):
|
||||
"""Authenticate and set environment variables."""
|
||||
config = self.config_from_env()
|
||||
if not config:
|
||||
config = self.config_from_file()
|
||||
if not config:
|
||||
config = self.query_credentials()
|
||||
|
||||
if set_environment and config:
|
||||
self.log("saving config to env")
|
||||
self.config_to_env(config)
|
||||
|
||||
return config
|
||||
|
||||
def query_credentials(self):
|
||||
"""Prompt user for URL and key if no configuration is found."""
|
||||
url = input(
|
||||
f"Insert URL (default: {self.DEFAULT_URL}): ") or self.DEFAULT_URL
|
||||
key = input("Insert your key: ")
|
||||
config = self.create_config(url, key)
|
||||
save_option = input("Save config? (y/n): ").lower() == "y"
|
||||
if save_option:
|
||||
self.config_to_file(config)
|
||||
return config
|
||||
|
||||
def get_authenticated_client(self):
|
||||
"""Return an authenticated CDS client."""
|
||||
config = self.authenticate()
|
||||
if not config:
|
||||
self.log("Cannot obtain authentication")
|
||||
return None
|
||||
return cdsapi.Client(url=config[CONFIG_URL], key=config[CONFIG_KEY])
|
||||
|
||||
def show_credentials(self):
|
||||
"""Display current configuration from env and files."""
|
||||
config_env = self.config_from_env()
|
||||
config_file = self.config_from_file(scan_all=True)
|
||||
self.log(f"Configuration from environment: {config_env}")
|
||||
self.log(f"Configuration from file: {config_file}")
|
||||
return config_env, config_file
|
||||
|
||||
def remove_conf_credentials(self, from_home=True, from_current_path=True, custom_path=None):
|
||||
"""Remove saved configurations from files."""
|
||||
paths_removed = []
|
||||
if from_home and os.path.exists(self.HOME_CDSAPIRC):
|
||||
os.remove(self.HOME_CDSAPIRC)
|
||||
paths_removed.append(self.HOME_CDSAPIRC)
|
||||
self.log(f"Removed configuration {self.HOME_CDSAPIRC}")
|
||||
|
||||
if from_current_path and os.path.exists(self.CURRENT_CDSAPIRC):
|
||||
os.remove(self.CURRENT_CDSAPIRC)
|
||||
paths_removed.append(self.CURRENT_CDSAPIRC)
|
||||
self.log(f"Removed configuration {self.CURRENT_CDSAPIRC}")
|
||||
|
||||
if custom_path and os.path.exists(custom_path):
|
||||
os.remove(custom_path)
|
||||
paths_removed.append(custom_path)
|
||||
self.log(f"Removed configuration {custom_path}")
|
||||
|
||||
return paths_removed
|
||||
|
||||
|
||||
def remove_env_credentials(self):
|
||||
"""Remove authentication credentials from environment variables."""
|
||||
removed_envs = []
|
||||
if self.ENV_CDSAPI_URL in os.environ:
|
||||
del os.environ[self.ENV_CDSAPI_URL]
|
||||
self.log(f"Removed environment variable {self.ENV_CDSAPI_URL}")
|
||||
|
||||
if self.ENV_CDSAPI_KEY in os.environ:
|
||||
del os.environ[self.ENV_CDSAPI_KEY]
|
||||
self.log(f"Removed environment variable {self.ENV_CDSAPI_KEY}")
|
||||
|
||||
return removed_envs
|
|
@ -0,0 +1,175 @@
|
|||
import argparse
|
||||
from AuthCDS import AuthCDS
|
||||
|
||||
# Version of the library
|
||||
__version__ = "1.0.0"
|
||||
|
||||
# Standalone function for authentication from Jupyter
|
||||
|
||||
def cds_help():
|
||||
"""Provide instructions on how to use methods programmatically from Jupyter."""
|
||||
help_message = '''
|
||||
CDS Authentication Helper - Jupyter Usage Guide
|
||||
Version: %s
|
||||
|
||||
Available Functions:
|
||||
---------------------
|
||||
- cds_authenticate(verbose=True): Perform authentication and return CDS client.
|
||||
- cds_remove_conf(verbose=True): Remove saved configurations from default paths.
|
||||
- cds_save_conf(config=None, verbose=True): Save configuration to the default path.
|
||||
- cds_remove_env(verbose=True): Remove environment-based credentials for CDS.
|
||||
- cds_show_conf(verbose=True): Display current configuration from environment and files.
|
||||
|
||||
Usage Examples:
|
||||
---------------
|
||||
1. Authenticate and get CDS client:
|
||||
client = cds_authenticate()
|
||||
|
||||
2. Remove configurations from default paths:
|
||||
cds_remove_conf()
|
||||
|
||||
3. Save the current or new configuration:
|
||||
cds_save_conf()
|
||||
|
||||
4. Remove environment variables for CDS API:
|
||||
cds_remove_env()
|
||||
|
||||
5. Show current configuration from environment and files:
|
||||
cds_show_conf()
|
||||
|
||||
''' % __version__
|
||||
print(help_message)
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(
|
||||
description="""Authenticate with CDS and configure .cdsapirc
|
||||
Version: {__version__}""",
|
||||
add_help=False
|
||||
)
|
||||
parser.add_argument(
|
||||
"-v", "--verbose",
|
||||
action="store_true",
|
||||
help="Enable verbose mode for detailed logging"
|
||||
)
|
||||
parser.add_argument(
|
||||
"-a", "--authenticate",
|
||||
action="store_true",
|
||||
help="Run authentication and set environment variables"
|
||||
)
|
||||
parser.add_argument(
|
||||
"-s", "--save-config",
|
||||
metavar="PATH",
|
||||
type=str,
|
||||
help="Save configuration to a specific file path (default: ~/.cdsapirc)"
|
||||
)
|
||||
parser.add_argument(
|
||||
"-r", "--remove-config",
|
||||
action="store_true",
|
||||
help="Remove saved configurations from default paths"
|
||||
)
|
||||
parser.add_argument(
|
||||
"-q", "--query-credentials",
|
||||
action="store_true",
|
||||
help="Prompt user to re-enter credentials and optionally save them"
|
||||
)
|
||||
parser.add_argument(
|
||||
"-p", "--print-config",
|
||||
action="store_true",
|
||||
help="Print current configuration from environment and files"
|
||||
)
|
||||
parser.add_argument(
|
||||
"-h", "--help",
|
||||
action="store_true",
|
||||
help="Display help information with usage examples"
|
||||
)
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
if not any(vars(args).values()):
|
||||
print("CDS Authentication CLI")
|
||||
print("Version: %s" % __version__)
|
||||
print("----------------------")
|
||||
print("Usage:")
|
||||
print(" python auth_cds.py [OPTIONS]")
|
||||
print("Options:")
|
||||
print(" -v, --verbose Enable verbose mode for detailed logging")
|
||||
print(" -a, --authenticate Run authentication and set environment variables")
|
||||
print(" -s, --save-config PATH Save configuration to a specific file path")
|
||||
print(" -r, --remove-config Remove saved configurations from default paths")
|
||||
print(" -q, --query-credentials Prompt user to re-enter credentials")
|
||||
print(" -p, --print-config Print current configuration from environment and files")
|
||||
print(" -h, --help Display this help information with usage examples")
|
||||
print("Run with -h or --help to see examples and additional information.")
|
||||
return
|
||||
|
||||
if args.help:
|
||||
cds_help()
|
||||
return
|
||||
|
||||
auth = AuthCDS(verbose=args.verbose)
|
||||
|
||||
if args.authenticate:
|
||||
client = auth.authenticate()
|
||||
if client:
|
||||
auth.log("Authenticated successfully")
|
||||
else:
|
||||
auth.log("Authentication failed")
|
||||
|
||||
if args.save_config:
|
||||
config = auth.config_from_env() or auth.config_from_file()
|
||||
if config:
|
||||
auth.config_to_file(config, config_path=args.save_config)
|
||||
auth.log(f"Configuration saved to {args.save_config}")
|
||||
else:
|
||||
auth.log("No configuration found to save")
|
||||
|
||||
if args.remove_config:
|
||||
removed_paths = auth.remove_conf_credentials()
|
||||
auth.log(f"Removed configurations from paths: {removed_paths}")
|
||||
|
||||
if args.query_credentials:
|
||||
auth.query_credentials()
|
||||
auth.log("Credentials queried and optionally saved")
|
||||
|
||||
if args.print_config:
|
||||
env_config, file_config = auth.show_credentials()
|
||||
auth.log(f"Environment Config: {env_config}", force=True)
|
||||
auth.log(f"File Config: {file_config}", force=True)
|
||||
|
||||
def cds_authenticate(verbose=True):
|
||||
"""Perform authentication and return the CDS client if successful."""
|
||||
auth = AuthCDS(verbose=verbose)
|
||||
conf = auth.authenticate()
|
||||
return conf
|
||||
|
||||
def cds_get_client(verbose=True):
|
||||
auth = AuthCDS(verbose=verbose)
|
||||
return auth.get_authenticated_client()
|
||||
|
||||
def cds_remove_conf(verbose=True):
|
||||
auth = AuthCDS(verbose=verbose)
|
||||
auth.remove_conf_credentials()
|
||||
|
||||
|
||||
def cds_save_conf(config=None, verbose=True):
|
||||
auth = AuthCDS(verbose=verbose)
|
||||
if config is None:
|
||||
config = auth.authenticate()
|
||||
if config is not None:
|
||||
auth.config_to_file(config)
|
||||
|
||||
|
||||
def cds_remove_env(verbose=True):
|
||||
auth = AuthCDS(verbose=verbose)
|
||||
auth.remove_env_credentials()
|
||||
|
||||
|
||||
def cds_show_conf(verbose=True):
|
||||
auth = AuthCDS(verbose=verbose)
|
||||
auth.show_credentials()
|
||||
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
|
@ -0,0 +1 @@
|
|||
"~/.cdsapirc"
|
|
@ -0,0 +1,304 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"#### Enter your CDS API key\n",
|
||||
"\n",
|
||||
"We will request data from the Climate Data Store (CDS) programmatically with the help of the CDS API. Let us make use of the option to manually set the CDS API credentials. First, you have to define two variables: `URL` and `KEY` which build together your CDS API key. The string of characters that make up your KEY include your personal User ID and CDS API key. To obtain these, first register or login to the CDS (https://cds-beta.climate.copernicus.eu), then visit https://cds-beta.climate.copernicus.eu/how-to-api and copy the string of characters listed after \"key:\". Replace the `#########` below with this string."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"zsh:1: /Users/Alfredo/works/copernicus/d4science_copernicus_cds/venv/bin/pip: bad interpreter: /Users/Alfredo/works/copernicus/d4science_auth_cds/venv/bin/python3.13: no such file or directory\n",
|
||||
"\n",
|
||||
"\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m24.2\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m24.3.1\u001b[0m\n",
|
||||
"\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpython3.12 -m pip install --upgrade pip\u001b[0m\n",
|
||||
"\u001b[1;31merror\u001b[0m: \u001b[1mexternally-managed-environment\u001b[0m\n",
|
||||
"\n",
|
||||
"\u001b[31m×\u001b[0m This environment is externally managed\n",
|
||||
"\u001b[31m╰─>\u001b[0m To install Python packages system-wide, try brew install\n",
|
||||
"\u001b[31m \u001b[0m xyz, where xyz is the package you are trying to\n",
|
||||
"\u001b[31m \u001b[0m install.\n",
|
||||
"\u001b[31m \u001b[0m \n",
|
||||
"\u001b[31m \u001b[0m If you wish to install a Python library that isn't in Homebrew,\n",
|
||||
"\u001b[31m \u001b[0m use a virtual environment:\n",
|
||||
"\u001b[31m \u001b[0m \n",
|
||||
"\u001b[31m \u001b[0m python3 -m venv path/to/venv\n",
|
||||
"\u001b[31m \u001b[0m source path/to/venv/bin/activate\n",
|
||||
"\u001b[31m \u001b[0m python3 -m pip install xyz\n",
|
||||
"\u001b[31m \u001b[0m \n",
|
||||
"\u001b[31m \u001b[0m If you wish to install a Python application that isn't in Homebrew,\n",
|
||||
"\u001b[31m \u001b[0m it may be easiest to use 'pipx install xyz', which will manage a\n",
|
||||
"\u001b[31m \u001b[0m virtual environment for you. You can install pipx with\n",
|
||||
"\u001b[31m \u001b[0m \n",
|
||||
"\u001b[31m \u001b[0m brew install pipx\n",
|
||||
"\u001b[31m \u001b[0m \n",
|
||||
"\u001b[31m \u001b[0m You may restore the old behavior of pip by passing\n",
|
||||
"\u001b[31m \u001b[0m the '--break-system-packages' flag to pip, or by adding\n",
|
||||
"\u001b[31m \u001b[0m 'break-system-packages = true' to your pip.conf file. The latter\n",
|
||||
"\u001b[31m \u001b[0m will permanently disable this error.\n",
|
||||
"\u001b[31m \u001b[0m \n",
|
||||
"\u001b[31m \u001b[0m If you disable this error, we STRONGLY recommend that you additionally\n",
|
||||
"\u001b[31m \u001b[0m pass the '--user' flag to pip, or set 'user = true' in your pip.conf\n",
|
||||
"\u001b[31m \u001b[0m file. Failure to do this can result in a broken Homebrew installation.\n",
|
||||
"\u001b[31m \u001b[0m \n",
|
||||
"\u001b[31m \u001b[0m Read more about this behavior here: <https://peps.python.org/pep-0668/>\n",
|
||||
"\n",
|
||||
"\u001b[1;35mnote\u001b[0m: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages.\n",
|
||||
"\u001b[1;36mhint\u001b[0m: See PEP 668 for the detailed specification.\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"!pip install 'cdsapi>=0.7.2'"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"zsh:1: /Users/Alfredo/works/copernicus/d4science_copernicus_cds/venv/bin/pip: bad interpreter: /Users/Alfredo/works/copernicus/d4science_auth_cds/venv/bin/python3.13: no such file or directory\n",
|
||||
"\n",
|
||||
"\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m24.2\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m24.3.1\u001b[0m\n",
|
||||
"\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpython3.12 -m pip install --upgrade pip\u001b[0m\n",
|
||||
"\u001b[1;31merror\u001b[0m: \u001b[1mexternally-managed-environment\u001b[0m\n",
|
||||
"\n",
|
||||
"\u001b[31m×\u001b[0m This environment is externally managed\n",
|
||||
"\u001b[31m╰─>\u001b[0m To install Python packages system-wide, try brew install\n",
|
||||
"\u001b[31m \u001b[0m xyz, where xyz is the package you are trying to\n",
|
||||
"\u001b[31m \u001b[0m install.\n",
|
||||
"\u001b[31m \u001b[0m \n",
|
||||
"\u001b[31m \u001b[0m If you wish to install a Python library that isn't in Homebrew,\n",
|
||||
"\u001b[31m \u001b[0m use a virtual environment:\n",
|
||||
"\u001b[31m \u001b[0m \n",
|
||||
"\u001b[31m \u001b[0m python3 -m venv path/to/venv\n",
|
||||
"\u001b[31m \u001b[0m source path/to/venv/bin/activate\n",
|
||||
"\u001b[31m \u001b[0m python3 -m pip install xyz\n",
|
||||
"\u001b[31m \u001b[0m \n",
|
||||
"\u001b[31m \u001b[0m If you wish to install a Python application that isn't in Homebrew,\n",
|
||||
"\u001b[31m \u001b[0m it may be easiest to use 'pipx install xyz', which will manage a\n",
|
||||
"\u001b[31m \u001b[0m virtual environment for you. You can install pipx with\n",
|
||||
"\u001b[31m \u001b[0m \n",
|
||||
"\u001b[31m \u001b[0m brew install pipx\n",
|
||||
"\u001b[31m \u001b[0m \n",
|
||||
"\u001b[31m \u001b[0m You may restore the old behavior of pip by passing\n",
|
||||
"\u001b[31m \u001b[0m the '--break-system-packages' flag to pip, or by adding\n",
|
||||
"\u001b[31m \u001b[0m 'break-system-packages = true' to your pip.conf file. The latter\n",
|
||||
"\u001b[31m \u001b[0m will permanently disable this error.\n",
|
||||
"\u001b[31m \u001b[0m \n",
|
||||
"\u001b[31m \u001b[0m If you disable this error, we STRONGLY recommend that you additionally\n",
|
||||
"\u001b[31m \u001b[0m pass the '--user' flag to pip, or set 'user = true' in your pip.conf\n",
|
||||
"\u001b[31m \u001b[0m file. Failure to do this can result in a broken Homebrew installation.\n",
|
||||
"\u001b[31m \u001b[0m \n",
|
||||
"\u001b[31m \u001b[0m Read more about this behavior here: <https://peps.python.org/pep-0668/>\n",
|
||||
"\n",
|
||||
"\u001b[1;35mnote\u001b[0m: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages.\n",
|
||||
"\u001b[1;36mhint\u001b[0m: See PEP 668 for the detailed specification.\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"!pip install -U cdsapi\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"zsh:1: /Users/Alfredo/works/copernicus/d4science_copernicus_cds/venv/bin/pip: bad interpreter: /Users/Alfredo/works/copernicus/d4science_auth_cds/venv/bin/python3.13: no such file or directory\n",
|
||||
"\n",
|
||||
"\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m24.2\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m24.3.1\u001b[0m\n",
|
||||
"\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpython3.12 -m pip install --upgrade pip\u001b[0m\n",
|
||||
"\u001b[1;31merror\u001b[0m: \u001b[1mexternally-managed-environment\u001b[0m\n",
|
||||
"\n",
|
||||
"\u001b[31m×\u001b[0m This environment is externally managed\n",
|
||||
"\u001b[31m╰─>\u001b[0m To install Python packages system-wide, try brew install\n",
|
||||
"\u001b[31m \u001b[0m xyz, where xyz is the package you are trying to\n",
|
||||
"\u001b[31m \u001b[0m install.\n",
|
||||
"\u001b[31m \u001b[0m \n",
|
||||
"\u001b[31m \u001b[0m If you wish to install a Python library that isn't in Homebrew,\n",
|
||||
"\u001b[31m \u001b[0m use a virtual environment:\n",
|
||||
"\u001b[31m \u001b[0m \n",
|
||||
"\u001b[31m \u001b[0m python3 -m venv path/to/venv\n",
|
||||
"\u001b[31m \u001b[0m source path/to/venv/bin/activate\n",
|
||||
"\u001b[31m \u001b[0m python3 -m pip install xyz\n",
|
||||
"\u001b[31m \u001b[0m \n",
|
||||
"\u001b[31m \u001b[0m If you wish to install a Python application that isn't in Homebrew,\n",
|
||||
"\u001b[31m \u001b[0m it may be easiest to use 'pipx install xyz', which will manage a\n",
|
||||
"\u001b[31m \u001b[0m virtual environment for you. You can install pipx with\n",
|
||||
"\u001b[31m \u001b[0m \n",
|
||||
"\u001b[31m \u001b[0m brew install pipx\n",
|
||||
"\u001b[31m \u001b[0m \n",
|
||||
"\u001b[31m \u001b[0m You may restore the old behavior of pip by passing\n",
|
||||
"\u001b[31m \u001b[0m the '--break-system-packages' flag to pip, or by adding\n",
|
||||
"\u001b[31m \u001b[0m 'break-system-packages = true' to your pip.conf file. The latter\n",
|
||||
"\u001b[31m \u001b[0m will permanently disable this error.\n",
|
||||
"\u001b[31m \u001b[0m \n",
|
||||
"\u001b[31m \u001b[0m If you disable this error, we STRONGLY recommend that you additionally\n",
|
||||
"\u001b[31m \u001b[0m pass the '--user' flag to pip, or set 'user = true' in your pip.conf\n",
|
||||
"\u001b[31m \u001b[0m file. Failure to do this can result in a broken Homebrew installation.\n",
|
||||
"\u001b[31m \u001b[0m \n",
|
||||
"\u001b[31m \u001b[0m Read more about this behavior here: <https://peps.python.org/pep-0668/>\n",
|
||||
"\n",
|
||||
"\u001b[1;35mnote\u001b[0m: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages.\n",
|
||||
"\u001b[1;36mhint\u001b[0m: See PEP 668 for the detailed specification.\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"!pip install -U attrs"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"!pip install -U typing_extensions"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import auth_cds"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"ENV - None None\n",
|
||||
"env is not configured\n",
|
||||
"Configuration from file /Users/Alfredo/.cdsapirc: {'url': 'https://cds-beta.climate.copernicus.eu/api', 'key': 'db1f2085-6b8b-42e6-b832-625dfaf831a4'}\n",
|
||||
"Configuration from environment: None\n",
|
||||
"Configuration from file: {'url': 'https://cds-beta.climate.copernicus.eu/api', 'key': 'db1f2085-6b8b-42e6-b832-625dfaf831a4'}\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"auth_cds.cds_show_conf()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"ENV - None None\n",
|
||||
"env is not configured\n",
|
||||
"Configuration from file /Users/Alfredo/.cdsapirc: {'url': 'https://cds-beta.climate.copernicus.eu/api', 'key': 'db1f2085-6b8b-42e6-b832-625dfaf831a4'}\n",
|
||||
"saving config to env\n",
|
||||
"Set environment variables CDSAPI_URL, CDSAPI_KEY\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"{'url': 'https://cds-beta.climate.copernicus.eu/api',\n",
|
||||
" 'key': 'db1f2085-6b8b-42e6-b832-625dfaf831a4'}"
|
||||
]
|
||||
},
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"auth_cds.cds_authenticate()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 11,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"2024-11-03 10:02:43,960 INFO [2024-09-28T00:00:00] **Welcome to the New Climate Data Store (CDS)!** This new system is in its early days of full operations and still undergoing enhancements and fine tuning. Some disruptions are to be expected. Your \n",
|
||||
"[feedback](https://jira.ecmwf.int/plugins/servlet/desk/portal/1/create/202) is key to improve the user experience on the new CDS for the benefit of everyone. Thank you.\n",
|
||||
"2024-11-03 10:02:43,961 WARNING [2024-09-26T00:00:00] Should you have not yet migrated from the old CDS system to the new CDS, please check our [informative page](https://confluence.ecmwf.int/x/uINmFw) for guidance.\n",
|
||||
"2024-11-03 10:02:43,962 INFO [2024-09-26T00:00:00] Watch our [Forum](https://forum.ecmwf.int/) for Announcements, news and other discussed topics.\n",
|
||||
"2024-11-03 10:02:43,962 INFO [2024-09-16T00:00:00] Remember that you need to have an ECMWF account to use the new CDS. **Your old CDS credentials will not work in new CDS!**\n",
|
||||
"2024-11-03 10:02:43,962 WARNING [2024-06-16T00:00:00] CDS API syntax is changed and some keys or parameter names may have also changed. To avoid requests failing, please use the \"Show API request code\" tool on the dataset Download Form to check you are using the correct syntax for your API request.\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import cdsapi\n",
|
||||
"client = cdsapi.Client()\n",
|
||||
" "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# auth_cds.removeCredentials()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "venv",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.13.0"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 4
|
||||
}
|
Loading…
Reference in New Issue