I upgraded the .cdsapirc file with the new url and key, and also upgraded my cdsapi package to 0.7.3, but when I run my script, I keep getting this error:
Traceback (most recent call last):
File “c:\Users\USER\OneDrive\Documents\Programs\Code for Storms\Code-for-storms\TestingStreamlines.py”, line 81, in
main()
File “c:\Users\USER\OneDrive\Documents\Programs\Code for Storms\Code-for-storms\TestingStreamlines.py”, line 76, in main
download_data()
File “c:\Users\USER\OneDrive\Documents\Programs\Code for Storms\Code-for-storms\TestingStreamlines.py”, line 26, in download_data
client = cdsapi.Client()
File “C:\ProgramData\Miniconda3\lib\site-packages\cdsapi\api.py”, line 337, in init
assert len(self.session.auth) == 2, (
AssertionError: The cdsapi key provided is not the correct format, please ensure it conforms to:
:
It seems that the api still thinks that the .cdsapirc file should be similar to the old version, so I cannot use it right now. Please suggest help!
Here’s the script I am using:
import cfgrib
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
import cartopy.crs as ccrs
import cartopy.feature as cfeature
def download_data():
import cdsapi
dataset = "reanalysis-era5-pressure-levels"
request = {
'product_type': ['reanalysis'],
'variable': ['u_component_of_wind', 'v_component_of_wind'],
'year': ['2000'],
'month': ['02'],
'day': ['06'],
'time': ['00:00'],
'pressure_level': ['200'],
'data_format': 'grib',
'download_format': 'unarchived',
'area': [0, 90, -20, 120]
}
client = cdsapi.Client()
client.retrieve(dataset, request).download()
def read_grib_data(grib_file):
ds = cfgrib.open_dataset(grib_file)
u850_data = ds[‘u’].values
v850_data = ds[‘v’].values
lats = ds['latitude'].values
lons = ds['longitude'].values
return lons, lats, u850_data, v850_data
def plot_streamlines(lons, lats, u850_data, v850_data):
wind_speed_m_s = np.sqrt(u850_data**2 + v850_data**2)
wind_speed_knots = wind_speed_m_s * 1.94384
fig = plt.figure(figsize=(12, 8))
ax = plt.axes(projection=ccrs.PlateCarree())
ax.set_extent([123, 133, 18, 28], crs=ccrs.PlateCarree())
ax.add_feature(cfeature.COASTLINE)
ax.add_feature(cfeature.BORDERS)
ax.add_feature(cfeature.LAND, edgecolor='black')
ax.add_feature(cfeature.OCEAN, edgecolor='black')
ax.gridlines(draw_labels=True, linewidth=0.5, color='gray', alpha=0.5, linestyle='--')
strm = ax.streamplot(lons, lats, u850_data, v850_data, transform=ccrs.PlateCarree(), linewidth=1, density=2,
color=wind_speed_knots, cmap='gist_ncar', arrowstyle='->', arrowsize=1.5)
cbar = plt.colorbar(strm.lines, ax=ax, orientation='horizontal', pad=0.05, aspect=50, shrink=0.75)
cbar.set_label('Wind Speed (knots)')
plt.title('Streamlines of Winds at 200 hPa\n 12 PM UTC on October 4th, 2013')
plt.xlabel('Longitude')
plt.ylabel('Latitude')
plt.show()
def main():
download_data()
lons, lats, u850_data, v850_data = read_grib_data(‘download.grib’)
plot_streamlines(lons, lats, u850_data, v850_data)
if name == “main”:
main()