Key error cdsapi/api.py

,

Hello,


I tried to download glacier data. I did the following:

import cdsapi

c = cdsapi.Client()

c.retrieve("insitu-glaciers-elevation-mass",{"variable":"all","product_type":"elevation_change","file_version":"20170405","format":"tgz"},"download.tar.gz")

I got the following error: 

  File "net_calculate_emission_from_point.py", line 40, in <module>

    "download.tar.gz")

  File ".../lib/python2.7/site-packages/cdsapi/api.py", line 317, in retrieve

    result = self._api('%s/resources/%s' % (self.url, name), request, 'POST')

  File ".../lib/python2.7/site-packages/cdsapi/api.py", line 420, in _api

    if reply['state'] != self.last_state:

KeyError: u'state'

My version of cdsapi = 0.2.7 . 

Does anyone understands this specific error? 


thank you in advance! 



hi Juliette,

I just tried your script and it ran without an error and downloaded the file.

Has the CDS API worked for you before? Can you try reinstalling the CDS API on your system?

Thanks,

Kevin

I've just encountered this error when another user tried to use one of my download scripts.

Turns out the issue was that the URL in their .cdsapirc file used `http` instead of `https` - which causes this interaction:

```

2021-02-23 14:07:48,975 DEBUG Starting new HTTP connection (1): cds.climate.copernicus.eu:80
2021-02-23 14:07:49,019 DEBUG http://cds.climate.copernicus.eu:80 "POST /api/v2/resources/reanalysis-era5-pressure-levels HTTP/1.1" 301 169
2021-02-23 14:07:49,023 DEBUG Starting new HTTPS connection (1): cds.climate.copernicus.eu:443
2021-02-23 14:07:49,151 DEBUG https://cds.climate.copernicus.eu:443 "GET /api/v2/resources/reanalysis-era5-pressure-levels HTTP/1.1" 200 None
2021-02-23 14:07:49,152 DEBUG REPLY {'slug': 'era5-hourly-data-on-pressure-levels-from-1979-to-present', 'name': 'reanalysis-era5-pressure-levels', 'id': 'eu.copernicus.climate.reanalysis-era5-pressure-levels', 'type': 'dataset', 'rich_abstract': '<div class=\'abstract-text\'>\n <p><strong>ERA5</strong> is the fifth generation ECMWF reanalysis for the global climate and weather for the past 4 to 7 decades.\nCurrently data is ava...

```

The error message the python script throws up isn't very helpful at the moment. Perhaps it could check for the existence of the `state` field before using it, and if it doesn't exist then return a message which suggests that the user might want to check their URL settings?