-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindspeedcomparison.py
89 lines (73 loc) · 2.45 KB
/
windspeedcomparison.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
"""
This script performs a comparison between maximum wind speeds from different storms as
reported in IBTrACS and ERA5 datasets.
"""
import cdsapi
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import re
from urllib.request import urlopen
import xarray as xr
from tqdm import tqdm
from Spherical.functions import addtolon
mps2kts = 1.94384 # unit conversion ratio
# where to save the data from each storm
savepath = "WindComparisonBasins.csv"
np.random.seed(0)
# IBTrACS data where max wind and basin are recorded
ibt = pd.read_csv('ibtracs.since1980.list.v04r00.csv')
ibt = ibt[1:]
ibt = ibt[ibt['WMO_WIND'] != ' ']
ibt = ibt[(ibt['BASIN'] == 'NA') | (ibt['BASIN'] == 'EP') | (ibt['BASIN'] == 'WP') | (ibt['BASIN'] == 'NI') |
(ibt['BASIN'] == 'SI') | (ibt['BASIN'] == 'SP') | (ibt['BASIN'] == 'SA')]
nrows, _ = ibt.shape
# Copernicus client
client = cdsapi.Client(quiet=True)
# get data from N random storms
N = 600
idx = ibt.index[np.random.randint(0, nrows, N)]
basin = []
wmowind = []
era5wind = []
for i in tqdm(idx):
basin.append(ibt['BASIN'][i])
wmowind.append(float(ibt['WMO_WIND'][i]))
lat = float(ibt['LAT'][i])
lon = float(ibt['LON'][i])
time = ibt['ISO_TIME'][i]
m = re.search("(\d\d\d\d)-(\d\d)-(\d\d) (\d\d:\d\d):\d\d", time)
params = {
'product_type': 'reanalysis',
'format': 'netcdf',
'year': m.group(1),
'month': m.group(2),
'day': m.group(3),
'time': m.group(4),
'variable': 'instantaneous_10m_wind_gust',
'area': [lat + 1, addtolon(lon, -1), lat - 1, addtolon(lon, 1)]
}
response = client.retrieve('reanalysis-era5-single-levels', params)
with urlopen(response.location) as f:
ds = xr.open_dataset(f.read())
df = ds.to_dataframe().reset_index()
'''
# this will plot all readings from near the storm
plt.scatter(df['longitude'], df['latitude'], c=df['i10fg'])
plt.colorbar()
plt.show()'''
era5wind.append(df['i10fg'].max() * mps2kts)
basin = np.array(basin)
wmowind = np.array(wmowind)
era5wind = np.array(era5wind)
for b in np.unique(basin):
plt.scatter(wmowind[basin == b], era5wind[basin == b], label=b)
plt.xlabel('IBTrACS wind speed (kts)')
plt.ylabel('ERA5 wind speed (kts)')
plt.legend()
plt.plot([0, 100], [0, 100])
plt.axis('equal')
plt.show()
savedata = pd.DataFrame({'ibtracs': wmowind, 'era5': era5wind, 'basin': basin})
print(f'Saving to {savepath}')
savedata.to_csv(savepath)