# Modeling the Joint Distribution of Wind Speed and Direction using Gaussain Mixture Models¶

OEN Method: Harris, Cook The parent wind speed distribution: Why Weibull? http://www.sciencedirect.com/science/article/pii/S0167610514001056

Gaussian Mixture Models, http://scikit-learn.org/stable/modules/mixture.html

# 1. Set up¶

## 1.1 Environment¶

In [1]:
%matplotlib inline

from import_file import *
from helpers.parallel_helper import *

plt.rcParams['axes.autolimit_mode'] = 'round_numbers'
plt.rcParams['axes.xmargin'] = 0.
plt.rcParams['axes.ymargin'] = 0.
mpl.rcParams['patch.force_edgecolor'] = True


In [2]:
# file_path= './data/NCDC/jerez/dat.txt' # time shift
# file_path= './data/NCDC/almeria/dat.txt'

# Greece
# file_path= './data/NCDC/eleftherios_intl/dat.txt'
# file_path= './data/NCDC/elefsis/dat.txt' # bad dataset
# file_path= './data/NCDC/malaga/dat.txt'
# file_path= './data/NCDC/gibraltar/dat.txt' # bad fit

# Turkey
# file_path= './data/NCDC/turkey/konya/dat.txt'
# file_path= './data/NCDC/turkey/sivas/dat.txt' # bad dataset
# file_path= './data/NCDC/turkey/balikesir/dat.txt' # bad dataset
# file_path= './data/NCDC/turkey/bartin/dat.txt' # bad dataset

# Iran
# file_path= './data/NCDC/iran/chahbahar/dat.txt'
# file_path= './data/NCDC/iran/zabol/dat.txt' # Problematic data
# file_path= './data/NCDC/iran/torbat_heydarieh/dat.txt' # Unusable

# UAE
# file_path= './data/NCDC/al_maktoum/dat.txt'
# file_path= './data/NCDC/abu_dhabi_intl/dat.txt' # Time shift
# file_path= './data/NCDC/bateen/dat.txt' # Time shift
# file_path= './data/NCDC/buraimi/dat.txt' # not good dataset

# file_path= './data/NCDC/uk/marham/dat.txt'
# file_path= './data/NCDC/uk/tiree/dat.txt'  # try 4
# file_path= './data/NCDC/uk/boscombe_down/dat.txt' # 4?, numpy bug
# file_path= './data/NCDC/uk/middle_wallop/dat.txt'
# file_path= './data/NCDC/uk/southhamption/dat.txt' # high 0, trend
# file_path= './data/NCDC/uk/bournemouth/dat.txt' # 4?
# file_path= "./data/NCDC/uk/weybourne/dat.txt"
# file_path= "./data/NCDC/uk/skye_lusa/dat.txt" #
# file_path= "./data/NCDC/uk/wattisham/dat.txt"
# file_path= "./data/NCDC/uk/south_uist_range/dat.txt" # inpropoer direction R square measure
# file_path= "./data/NCDC/uk/holbeach/dat.txt" # inpropoer direction R square measure
# file_path= "./data/NCDC/uk/cambridge/dat.txt" # inpropoer direction R square measure
# file_path= "./data/NCDC/us/baltimore/dat.txt" # time too short
# file_path= "./data/NCDC/uk/bealach_na_ba/dat.txt" # time too short
# file_path= "./data/NCDC/uk/benbecula/dat.txt" # truncate (untruncate in m/s), 4?

# file_path, NUMBER_OF_GAUSSIAN = "./data/NCDC/europe/landsberg_lech/dat.txt", 4 # very good, can try 4
file_path= './data/NCDC/europe/pau_pyrenees/dat.txt' # unit shift, 2; force using knot
# file_path= "./data/NCDC/europe/vatry/dat.txt"  # double peak, initial speed (should be good with m/s), mixed report type
# file_path= "./data/NCDC/europe/neuburg/dat.txt"
# file_path= "./data/NCDC/europe/laupheim/dat.txt" # double peak, 4; very good, trend
# file_path= "./data/NCDC/europe/avord/dat.txt" # try 4, initial speed (should be good with m/s)
# file_path= './data/NCDC/europe/ciampino/dat.txt' # try 4, bandwidth?
# file_path= "./data/NCDC/europe/holzdorf/dat.txt" # 2008 year
# file_path= "./data/NCDC/europe/huspel_aws/dat.txt"  # integer, 4?
# file_path= "./data/NCDC/europe/barayas/dat.txt" # numpy problem
# file_path= './data/NCDC/europe/tenerife_sur/dat.txt'  # some directions are blocked
# file_path= './data/NCDC/europe/nantes/dat.txt' # some dir R square / K-S differs big, unit detect fails

# file_path= "./data/NCDC/cn/shanghai/hongqiao_intl/dat.txt" # care for the sampling time
# file_path= "./data/NCDC/cn/shanghai/pudong/dat.txt"
# file_path= "./data/NCDC/cn/hefei_luogang/dat.txt" # few 0, trend, try 2
# file_path= "./data/NCDC/cn/nanjing_lukou/dat.txt"
# file_path= "./data/NCDC/cn/zhengzhou_xinzheng/dat.txt"
# file_path= "./data/NCDC/cn/tianjin/binhai/dat.txt" # few 0, trend, stationary speed, unstationary direction
# file_path= "./data/NCDC/cn/tianjin/tianjing/dat.txt" # 16 sectors
# file_path= "./data/NCDC/cn/shijiazhuang_zhengding/dat.txt"
# file_path= "./data/NCDC/cn/henan_gushi/dat.txt" # 16 sectors, fit not very good
# file_path= "./data/NCDC/cn/nanning_wuxu/dat.txt" # numpy priblem, unstationary speed
# file_path= './data/NCDC/cn/macau/dat.txt'
# file_path= "./data/NCDC/cn/hk_intl/dat.txt" # few 0

# file_path= './data/NCDC/southeast_asia/malaysia/mersing/dat.txt' # 2 mode, paper comparison
# file_path= './data/NCDC/southeast_asia/malaysia/penang/dat.txt'
# file_path= './data/NCDC/southeast_asia/malaysia/butterworth/dat.txt' # 2 mode
# file_path= "./data/NCDC/southeast_asia/malaysia/bsultan_mahmud/dat.txt" # stable
# file_path= "./data/NCDC/southeast_asia/malaysia/bsultan_ismail/dat.txt" #
# file_path= "./data/NCDC/southeast_asia/singapore/changi/dat.txt" # trend, no 0, questionary data
# file_path= "./data/NCDC/southeast_asia/singapore/paya_lebar/dat.txt" # questionary data
# file_path= "./data/NCDC/southeast_asia/singapore/seletar/dat.txt"
# file_path= "./data/NCDC/east_asia/cheongju_intl/dat.txt" # 2005-2009  may have problem, fit is good; numpy problem
# file_path= "./data/NCDC/east_asia/daegu_ab/dat.txt" # recent 5 year may have problem, but fit is generally good; numpy problem

# file_path= "./data/NCDC/oceania/auckland_intl/dat.txt"  # Good data, Weird KDE shape, might be blocked?
# file_path= "./data/NCDC/oceania/brisbane_archerfield/dat.txt" # high 0, few data
# file_path= "./data/NCDC/oceania/narrandera/dat.txt" # high 0, few data
# file_path= "./data/NCDC/oceania/canberra/dat.txt" # high 0, numpy problem

# file_path= './data/NCDC/us/boston_16nm/dat.txt' # Offshore

# file_path = './data/asos/denver/hr_avg.csv'
# file_path = './data/asos/bismarck_ND/hr_avg.csv' # try 4
# file_path = './data/asos/aberdeen_SD/hr_avg.csv' # only to 2012, good fit, try 2
# file_path = './data/asos/minneapolis/hr_avg.csv'
# file_path = './data/asos/lincoln_NE/hr_avg.csv'
# file_path = './data/asos/des_moines_IA/hr_avg.csv'
# file_path = './data/asos/springfield_IL/hr_avg.csv' # good fit
# file_path = './data/asos/topeka/hr_avg.csv' # High 0

# file_path = './data/NDAWN/baker/hr_avg.csv' # 4 might be better
# file_path = './data/NDAWN/dickinson/hr_avg.csv'
# file_path = './data/NDAWN/rugby/hr_avg.csv'
# file_path = './data/NDAWN/bowman/hr_avg.csv'
# file_path = './data/NDAWN/grand_forks/hr_avg.csv'
# file_path = './data/NDAWN/williston/hr_avg.csv'
# file_path = './data/NDAWN/jamestown/hr_avg.csv'

In [3]:
if "cn_database" in file_path:
elif 'NCDC' in file_path:
df.rename(columns={'Date':'date','Dir':'dir','Spd':'speed','Type':'type','I.1':'wind_type'}, inplace=True)
df = df[['date','HrMn','type','dir','speed','wind_type' ]]
df.dropna(subset=['dir','speed'], inplace=True)
integer_data = True
elif 'NDAWN' in file_path:
df['type']='default'
df['wind_type']='default'
df = df.dropna()
integer_data = False
knot_unit = False
else:
# ASOS
df['type']='default'
df['wind_type']='default'
df = df.dropna()
integer_data = False
knot_unit = True

In [4]:
df

Out[4]:
date HrMn type dir speed wind_type
0 19790101 0000 FM-12 260 6.2 N
1 19790101 0100 FM-15 260 5.1 N
2 19790101 0200 FM-15 260 7.2 N
3 19790101 0300 FM-12 260 10.3 N
4 19790101 0600 FM-12 280 10.3 N
5 19790101 0700 FM-15 280 7.2 N
6 19790101 0800 FM-15 280 6.1 N
7 19790101 0900 FM-12 360 5.1 N
8 19790101 1000 FM-15 360 3.6 N
9 19790101 1100 FM-15 60 1.0 N
10 19790101 1200 FM-12 999 0.0 C
11 19790101 1300 FM-15 20 1.0 N
12 19790101 1400 FM-15 999 0.0 C
13 19790101 1500 FM-12 180 1.0 N
14 19790101 1600 FM-15 999 0.0 C
15 19790101 1700 FM-15 140 2.0 N
16 19790101 1800 FM-12 140 2.6 N
17 19790101 1900 FM-15 120 2.0 N
18 19790101 2000 FM-15 140 1.5 N
19 19790101 2100 FM-12 80 1.0 N
20 19790102 0100 FM-15 60 3.6 N
21 19790102 0200 FM-15 40 4.1 N
22 19790102 0300 FM-12 40 4.1 N
23 19790102 0500 FM-15 60 2.5 N
24 19790102 0600 FM-12 100 2.6 N
25 19790102 0800 FM-15 80 2.5 N
26 19790102 0900 FM-12 60 3.1 N
27 19790102 1000 FM-15 80 5.1 N
28 19790102 1200 FM-12 80 3.1 N
29 19790102 1300 FM-15 80 4.1 N
... ... ... ... ... ... ...
366609 20170401 0900 FM-15 310 4.1 N
366610 20170401 0930 FM-15 999 1.5 V
366611 20170401 1000 FM-15 230 3.6 V
366612 20170401 1030 FM-15 260 6.7 V
366613 20170401 1100 FM-12 270 6.7 N
366614 20170401 1130 FM-15 260 6.2 N
366615 20170401 1200 FM-15 260 4.1 N
366616 20170401 1230 FM-15 250 5.7 V
366617 20170401 1300 FM-15 270 8.2 V
366618 20170401 1330 FM-15 280 9.3 N
366619 20170401 1400 FM-15 270 8.2 N
366620 20170401 1430 FM-15 250 7.7 N
366621 20170401 1500 FM-15 250 5.1 V
366622 20170401 1530 FM-15 230 4.1 V
366623 20170401 1600 FM-15 220 3.6 V
366624 20170401 1630 FM-15 230 4.1 V
366625 20170401 1700 FM-15 270 6.2 N
366626 20170401 1730 FM-15 250 6.2 N
366627 20170401 1800 FM-15 250 5.1 N
366628 20170401 1830 FM-15 270 5.1 N
366629 20170401 1900 FM-12 260 5.1 N
366630 20170401 1930 FM-15 250 5.1 N
366631 20170401 2000 FM-15 260 5.1 N
366632 20170401 2030 FM-15 260 4.6 N
366633 20170401 2100 FM-15 260 3.6 V
366634 20170401 2130 FM-15 260 4.6 N
366635 20170401 2200 FM-15 250 4.1 N
366636 20170401 2230 FM-15 240 3.1 N
366637 20170401 2300 FM-15 230 3.1 V
366638 20170401 2330 FM-15 250 3.6 V

366639 rows Ã— 6 columns

In [5]:
df['time']=pd.to_datetime(df["date"].astype(str).map(str) + df["HrMn"], format='%Y%m%d%H%M')
df.set_index(['time'], inplace=True)
df['HrMn']=df['HrMn'].astype(int)
df = df.query("(dir <= 999) & (speed < 100) & \
(date >= 19700000) & (date < 20170000) ")

In [6]:
plot_speed_and_angle_distribution(df.speed, df.dir)

D:\ProgramData\Anaconda3\lib\site-packages\matplotlib\__init__.py:938: UserWarning: axes.color_cycle is deprecated and replaced with axes.prop_cycle; please use the latter.
warnings.warn(self.msg_depr % (key, alt_key))

In [7]:
# Dir [10,360]=> [0,350]
df['dir'] = df['dir'].apply(lambda x: x%360 if x < 999 else x)
df['month'] = df['date']%10000//100
# Convert Windrose coordianates to Polar Cooridinates
df['dir_windrose'] = df['dir']
df['dir'] = df['dir'].apply(lambda x: (90 - x)%360 if x < 999 else x)
df.describe()

D:\ProgramData\Anaconda3\lib\site-packages\ipykernel\__main__.py:2: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
from ipykernel import kernelapp as app
D:\ProgramData\Anaconda3\lib\site-packages\ipykernel\__main__.py:3: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
app.launch_new_instance()
D:\ProgramData\Anaconda3\lib\site-packages\ipykernel\__main__.py:5: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
D:\ProgramData\Anaconda3\lib\site-packages\ipykernel\__main__.py:6: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy

Out[7]:
date HrMn dir speed month dir_windrose
count 3.622460e+05 362246.00000 362246.000000 362246.000000 362246.000000 362246.000000
mean 2.001603e+07 1142.66531 287.071871 2.378766 6.545927 283.826955
std 1.113944e+05 678.73662 277.331580 1.792495 3.452641 276.351481
min 1.979010e+07 0.00000 0.000000 0.000000 1.000000 0.000000
25% 1.993013e+07 600.00000 140.000000 1.000000 4.000000 120.000000
50% 2.004122e+07 1130.00000 200.000000 2.100000 7.000000 230.000000
75% 2.011102e+07 1700.00000 320.000000 3.100000 10.000000 300.000000
max 2.016123e+07 2330.00000 999.000000 26.200000 12.000000 999.000000
In [8]:
df.plot(y='speed',legend=True,figsize=(20,5))

Out[8]:
<matplotlib.axes._subplots.AxesSubplot at 0xde94b38>

### 1.2.1 Unit Detection¶

In [9]:
df['decimal'] = df.speed % 1
df.decimal.hist(alpha=0.5, label='m/s', figsize=(4, 3))
if 'knot_unit' not in globals():
#     knot_unit = True if len(df.query('decimal >= 0.2')) / len(df) > 0.3 else False
knot_unit=True
if knot_unit:
df['speed'] = df['speed'] * 1.943845
df['decimal'] = df.speed % 1
df.decimal.hist(alpha=0.5, label='knot')
# need more elaboration, some is not near an integer
df['speed'] = df['speed'].apply(lambda x: int(round(x)))
plt_configure(xlabel='Decimal', ylabel='Frequency', legend={'loc': 'best'}, title='Decimal Distribution')

df.drop(['decimal'], 1,inplace=True)
print(knot_unit)

D:\ProgramData\Anaconda3\lib\site-packages\ipykernel\__main__.py:1: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
if __name__ == '__main__':
D:\ProgramData\Anaconda3\lib\site-packages\ipykernel\__main__.py:7: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
D:\ProgramData\Anaconda3\lib\site-packages\ipykernel\__main__.py:8: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
D:\ProgramData\Anaconda3\lib\site-packages\ipykernel\__main__.py:11: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
D:\ProgramData\Anaconda3\lib\site-packages\ipykernel\__main__.py:14: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy

True

In [10]:
dir_unit_text = ' (degree)'
if knot_unit == True:
speed_unit_text = ' (knot)'
else:
speed_unit_text = ' (m/s)'


### 1.2.2 Sampling Type Selection¶

In [11]:
sample_type = df.query('date > 20000000')['type']
sample_type.value_counts().plot(
kind = 'bar', title = 'Report Types Comprisement', figsize=(4,3))

report_type_most_used = sample_type.value_counts().argmax()
df = df.query("type==@report_type_most_used")


### 1.2.3 Sampling Time Selection¶

In [12]:
MID_YEAR = (min(df.date)//10000+max(df.date)//10000)//2

df['HrMn'].value_counts().sort_index().plot(kind='bar', alpha=0.5,label='Overall')
df.query('date > @MID_YEAR * 10000')['HrMn'].value_counts().sort_index().plot(
kind='bar', alpha=0.5, label='> %s' %  MID_YEAR )

plt_configure(xlabel='Sampling Time', ylabel='Frequency', legend={'loc':'best'}, figsize=(8, 4),
title = 'Sampling Time Distribution, Overall and > %s ' %  MID_YEAR)

In [13]:
df['sample_time'] = df.HrMn % 100
sample_time = df.query('date > 20000000')['sample_time']
sample_times = sample_time.value_counts()[sample_time.value_counts() > 2000]
sample_times = sample_times.index.tolist()
# df = df.query("sample_time in @sample_times")
df = df.query("sample_time == @sample_times[0]")
df.drop(['sample_time'], 1,inplace=True)
print(sample_times)

df['HrMn'].value_counts().sort_index().plot(kind='bar', alpha=0.5, figsize=(10, 4))

[0]

Out[13]:
<matplotlib.axes._subplots.AxesSubplot at 0xe91c5f8>

## 1.3 Data Wrangling¶

### 1.3.1 Artefacts¶

#### 1.3.1.1 wrong direction record¶

In [14]:
if integer_data:
display(df.query("(dir % 10 >= 0.1) & (dir != 999)"))
df = df.query('(dir % 10 <= 0.1) | (dir == 999)')

date HrMn type dir speed wind_type month dir_windrose
time

#### 1.3.1.2 sudden increase in speed¶

In [15]:
# sudden increse
df['incre'] = df.speed.diff(1)
df['incre'].fillna(0, inplace=True)
df['incre_reverse'] = df.speed.diff(-1)
df['incre_reverse'].fillna(0, inplace=True)

df['incre'].plot(kind='hist', bins=arange(-15, 15), legend=True, figsize=(8, 3))

date HrMn type dir speed wind_type month dir_windrose incre incre_reverse
time
1999-12-27 18:00:00 19991227 1800 SY-MT 160 51 N 12 290 24.0 14.0
1984-11-28 00:00:00 19841128 0 SY-MT 350 40 N 11 100 36.0 38.0
1984-06-17 12:00:00 19840617 1200 SY-MT 70 40 N 6 20 36.0 36.0
2002-05-13 16:00:00 20020513 1600 SY-MT 170 37 N 5 280 27.0 20.0
1999-12-27 19:00:00 19991227 1900 SY-MT 180 37 N 12 270 -14.0 4.0
2007-02-14 11:00:00 20070214 1100 SY-MT 180 35 N 2 270 14.0 10.0
2009-01-24 08:00:00 20090124 800 SY-MT 180 35 N 1 270 2.0 2.0
2009-01-24 04:00:00 20090124 400 SY-MT 160 35 N 1 290 15.0 18.0
2009-01-24 10:00:00 20090124 1000 SY-MT 170 35 N 1 280 2.0 6.0
2012-12-21 14:00:00 20121221 1400 SY-MT 180 35 N 12 270 14.0 12.0
Out[15]:
<matplotlib.axes._subplots.AxesSubplot at 0xe8fcfd0>
In [16]:
incre_threshold = 20 if knot_unit else 10
print('sudden increase number', len(df.query('(incre > @incre_threshold )&(incre_reverse > @incre_threshold )')))
df = df.query('(incre < @incre_threshold )|(incre_reverse < @incre_threshold )')

# Check the max speed
df.drop(['incre', 'incre_reverse'], 1, inplace=True)

sudden increase number 2

date HrMn type dir speed wind_type month dir_windrose incre incre_reverse
time
1999-12-27 18:00:00 19991227 1800 SY-MT 160 51 N 12 290 24.0 14.0
1999-12-27 19:00:00 19991227 1900 SY-MT 180 37 N 12 270 -14.0 4.0
2012-12-21 14:00:00 20121221 1400 SY-MT 180 35 N 12 270 14.0 12.0
2009-01-24 04:00:00 20090124 400 SY-MT 160 35 N 1 290 15.0 18.0
2009-01-24 08:00:00 20090124 800 SY-MT 180 35 N 1 270 2.0 2.0
2009-01-24 10:00:00 20090124 1000 SY-MT 170 35 N 1 280 2.0 6.0
2007-02-14 11:00:00 20070214 1100 SY-MT 180 35 N 2 270 14.0 10.0
2006-02-18 00:00:00 20060218 0 SY-MT 140 35 N 2 310 14.0 25.0
2009-01-24 07:00:00 20090124 700 SY-MT 190 33 N 1 260 2.0 -2.0
1996-02-07 18:00:00 19960207 1800 SY-MT 160 33 N 2 290 4.0 23.0

### 1.3.2 0 Speed¶

In [17]:
with_too_many_zero, null_wind_frequency = is_with_too_many_zero(df.query("(date >= 20050000)"))
delete_zero = with_too_many_zero
if delete_zero:
df = df.query('(speed > 0)')
print(delete_zero, null_wind_frequency)

False 0.0572973129237


### 1.3.3 Direction re-aligment and 999¶

For some dataset, the 16 sectors are not record properly,

e.g. the sectors are [0,20,30,50], need to redistribute the angle into 22.5

In [18]:
display(df['dir'].value_counts().sort_index())
effective_column = df.query('dir < 999')['dir'].value_counts()[df['dir'].value_counts() > 30].sort_index()
if integer_data:
SECTOR_LENGTH = 360/len(effective_column)
else:
SECTOR_LENGTH = 10
print(len(effective_column), SECTOR_LENGTH)

0      3789
10     3928
20     3016
30     3102
40     2448
50     2630
60     1922
70     2066
80     1730
90     2197
100    2083
110    2891
120    2725
130    3563
140    3421
150    4754
160    4626
170    6436
180    5347
190    6230
200    4789
210    5008
220    3309
230    2888
240    1958
250    1685
260    1284
270    1495
280    1389
290    2040
300    2566
310    4939
320    5425
330    7138
340    4891
350    4832
999    7612
Name: dir, dtype: int64
36 10.0

In [19]:
df.query('dir == 999')['speed'].value_counts()

Out[19]:
0    7611
2       1
Name: speed, dtype: int64
In [20]:
df=realign_direction(df, effective_column)
df=fill_direction_999(df, SECTOR_LENGTH)


## 1.4 Time Shift Comparison¶

In [21]:
DIR_REDISTRIBUTE = 'even'
if DIR_REDISTRIBUTE == 'even':
DIR_BIN = arange(-5, 360, 10)
elif DIR_REDISTRIBUTE == 'round_up':
DIR_BIN = arange(0, 360+10, 10)

# Comparison between mid_year, looking for:
# 1. Odd Even Bias
# 2. Time Shift of Wind Speed Distribution
bins = arange(0, df.speed.max() + 1)
df.query('date < @MID_YEAR * 10000')['speed'].plot(
kind='hist', alpha=0.5,bins=bins, label='< %s' % MID_YEAR)

df.query('date > @MID_YEAR * 10000')['speed'].plot(
kind='hist', alpha=0.5,bins=bins, label='> %s' % MID_YEAR)

plt.suptitle('Speed Comparison between year < %s, > %s ' % (MID_YEAR, MID_YEAR), fontsize = 14)
plt_configure(xlabel='Speed', ylabel='Frequency', legend=True, figsize=(8, 3))

In [22]:
df.query('date < @MID_YEAR * 10000')['dir'].plot(
kind='hist', alpha=0.5,bins=DIR_BIN, label='< %s' % MID_YEAR)

df.query('date > @MID_YEAR * 10000')['dir'].plot(
kind='hist', alpha=0.5,bins=DIR_BIN, label='> %s' % MID_YEAR)

plt.suptitle('Dir Comparison between year < %s, and > %s ' % (MID_YEAR, MID_YEAR), fontsize = 14)
plt_configure(xlabel='Dir', ylabel='Frequency', legend={'loc':'best'}, figsize=(8, 3), tight='x')

In [23]:
# Inspect the time shift of speed and degree distribution, and odd-even bias
check_time_shift(df, speed_unit_text=speed_unit_text, dir_unit_text=dir_unit_text)

1979 - 1979

D:\ProgramData\Anaconda3\lib\site-packages\matplotlib\__init__.py:938: UserWarning: axes.color_cycle is deprecated and replaced with axes.prop_cycle; please use the latter.
warnings.warn(self.msg_depr % (key, alt_key))

1980 - 1984

1985 - 1989

1990 - 1994

1995 - 1999

2000 - 2004

2005 - 2009

2010 - 2013

In [24]:
df.resample('A').mean().plot(y='speed')
plt.gca().set_ylim(bottom=0)
df.resample('M').mean().plot(y='speed', figsize=(20,4))
plt.gca().set_ylim(bottom=0)

Out[24]:
(0, 10.0)
In [25]:
display(df[df['dir'].isnull()])
df.dropna(subset=['dir'], inplace=True)

date HrMn type dir speed wind_type month dir_windrose
time
In [26]:
for column in ['speed', 'dir']:
if column == 'speed':
bins = arange(0, df[column].max()+1, 1)
else:
bins = arange(0, 361, 10)
den, _ = np.histogram(df[column], bins=bins, density=True)
y_top=max(den)*1.2
for year in arange(1980, 2016):
end_year = year
sub_df = df[str(year):str(end_year)]
if len(sub_df) > 5000:
plt.figure()
df[column].hist(bins=bins, alpha=0.3, normed=True)
sub_df[column].hist(bins=bins, alpha=0.5, figsize=(3,1.5), normed=True)
plt.gca().set_ylim(top=y_top)
plt_configure(title=str(year))
align_figures()

In [27]:
for column in ['speed', 'dir']:
if column == 'speed':
bins = arange(0, df[column].max()+1, 1)
else:
bins = arange(0, 361, 10)
density_all, _ = np.histogram(df[column], bins=bins, density=True)
df[column].hist(bins=bins, figsize=(5,3))

R_squares = []
years = []
for year in arange(1980, 2016):
start_year, end_year = year-1, year+1
sub_df = df[str(start_year):str(end_year)]
if len(sub_df) > 5000:
density, _ = np.histogram(sub_df[column], bins=bins, density=True)
y_mean = np.mean(density_all)
SS_tot = np.sum(np.power(density_all - y_mean, 2))
SS_res = np.sum(np.power(density_all - density, 2))

R_square = 1 - SS_res / SS_tot
R_squares.append(R_square)
years.append(year)

plt.figure()
plot(years, R_squares)
ylim = max(min(plt.gca().get_ylim()[0],0.85),0)
plt.gca().set_ylim(bottom=ylim, top=1)
plt_configure(figsize=(5,3))
align_figures()


## 1.5 Re-distribute Direction and Speed (Optional)¶

e.g. Dir 50 -> -45 ~ 55, to make KDE result better

In [28]:
if integer_data:
df = randomize_angle(df, DIR_REDISTRIBUTE, SECTOR_LENGTH)

In [29]:
if integer_data:
if delete_zero:
redistribute_method = 'down'
else:
redistribute_method = 'up'

df, speed_redistribution_info = randomize_speed(df, redistribute_method)

Redistribute upward, e.g. 0 -> [0,1]


## 1.6 Generate (x,y) from (speed,dir)¶

In [30]:
# Cook orientation
# df['dir']= (df['dir'] + 180)%360

In [31]:
# There might be a small dot in the centre, which is due to too many zero (more than 1 speed) in center
# Scatter plot in matplot has performance issue, the speed is very slow
df['x'] = df['speed'] * cos(df['dir'] * pi / 180.0)
df['y'] = df['speed'] * sin(df['dir'] * pi / 180.0)


# 2. Re-select Data and Overview¶

## 2.1 Data Overview¶

In [32]:
## Summery of the data selection
print('Knot unit?', knot_unit)
print('Report type used:', report_type_most_used)
print('Sampling time used:', sample_times)
if 'speed_redistribution_info' in globals():
print('Speed redistribution info:', speed_redistribution_info )

df_all_years = df # for later across-year comparison
df = df_all_years.query('(date >= 20100000) & (date < 20150000)')
# df = df.query('(HrMn == 0) and (speed >= 0.5) and (date%10000 > 900) and (date%10000 < 1000)' )
df.describe()

Knot unit? True
Report type used: SY-MT
Sampling time used: [0]
Speed redistribution info: Redistribute upward, e.g. 0 -> [0,1]

Out[32]:
date HrMn dir speed month dir_windrose x y
count 2.806600e+04 28066.000000 28066.000000 28066.000000 28066.000000 28066.000000 28066.000000 28066.000000
mean 2.011260e+07 1148.574788 190.745288 4.970712 6.138780 234.555013 -1.233491 -0.066619
std 9.704539e+03 696.268540 103.438455 3.369369 3.519564 214.959077 4.985859 3.110743
min 2.010010e+07 0.000000 -4.986196 0.000259 1.000000 0.000000 -35.059729 -21.181042
25% 2.010110e+07 500.000000 120.250914 2.801845 3.000000 110.000000 -4.190693 -2.186894
50% 2.011090e+07 1100.000000 189.209888 4.143457 6.000000 210.000000 -0.185727 -0.353921
75% 2.012070e+07 1800.000000 291.390825 6.319304 9.000000 280.000000 2.465441 1.904472
max 2.013043e+07 2300.000000 354.989938 35.164171 12.000000 999.000000 13.824255 19.257946
In [33]:
df.plot(y='speed',legend=True,figsize=(20,5))

Out[33]:
<matplotlib.axes._subplots.AxesSubplot at 0x15154860>
In [34]:
# Accumulation by month
df.resample('M').count().plot(y='date', kind='bar',figsize=(20,4))

Out[34]:
<matplotlib.axes._subplots.AxesSubplot at 0x14d92cc0>
In [35]:
# 90 degree is east
ax = WindroseAxes.from_ax()
viridis = plt.get_cmap('viridis')
ax.bar(df.dir_windrose, df.speed, normed=True, opening=0.8, edgecolor='white', nsector=36, cmap=viridis)
ax.set_legend()

D:\ProgramData\Anaconda3\lib\site-packages\matplotlib\cbook.py:136: MatplotlibDeprecationWarning: The axisbg attribute was deprecated in version 2.0. Use facecolor instead.
warnings.warn(message, mplDeprecation, stacklevel=1)