Deltares / dfm_tools

A Python package for pre- and postprocessing D-Flow FM model input and output files
https://deltares.github.io/dfm_tools/
GNU General Public License v3.0
70 stars 13 forks source link

fix cds_credentials #1034

Closed veenstrajelmer closed 4 weeks ago

veenstrajelmer commented 4 weeks ago

Failing when there is no ~/.cdsapirc file present yet:

Downloading CDS/ERA5 data requires a ECMWF API-key, copy your API-key from https://cds.climate.copernicus.eu/profile (first register, login and accept the terms). More info in https://forum.ecmwf.int/t/3743.

Warning: QtConsole does not support password mode, the text you type will be visible.
Enter your ECMWF API-key (string with dashes): aaa
Traceback (most recent call last):

  File C:\DATA\dfm_tools\dfm_tools\download.py:108 in cds_credentials
    c = cdsapi.Client()

  File ~\AppData\Local\miniforge3\envs\dfm_tools_env\Lib\site-packages\cdsapi\api.py:281 in __new__
    _, token, _ = get_url_key_verify(url, key, None)

  File ~\AppData\Local\miniforge3\envs\dfm_tools_env\Lib\site-packages\cdsapi\api.py:69 in get_url_key_verify
    raise Exception("Missing/incomplete configuration file: %s" % (dotrc))

Exception: Missing/incomplete configuration file: C:\Users\veenstra/.cdsapirc

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

  File ~\AppData\Local\miniforge3\envs\dfm_tools_env\Lib\site-packages\spyder_kernels\py3compat.py:356 in compat_exec
    exec(code, globals, locals)

  File c:\data\dfm_tools\dfm_tools\untitled0.py:10
    dfmt.download.cds_credentials()

  File C:\DATA\dfm_tools\dfm_tools\download.py:119 in cds_credentials
    cds_set_credentials(cds_url, cds_apikey)

UnboundLocalError: cannot access local variable 'cds_url' where it is not associated with a value`

To reproduce

import dfm_tools as dfmt
dfmt.download.cds_credentials()

Workaround

import os
os.environ["CDSAPI_KEY"] = "put-your-apikey-here"