-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSixthStep 2018-05-10 17_44_03.m
202 lines (193 loc) · 7.38 KB
/
SixthStep 2018-05-10 17_44_03.m
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% In this step I plot the PPPs for the data above 60 KMPH
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%==================================
% Basic Initialization instructions
%==================================
clear; % Clear Memory
clc; %Clear Command Window
%===================================
% Basic Data Extraction instructions
%===================================
%Define the folder location of CSV files
folder='C:\Users\Maulin Amin\OneDrive - University of Waterloo\Waterloo\Winter 2018\Environment Canada\Wind&SnowData\CSV\Trenton';
i=1; %Set i=1 so that it does not give error for data{0} which is impossible
%use the Datastore function to read all the CSV files in the folder
ds = tabularTextDatastore(folder,'FileExtensions','.csv','SelectedVariableNames',{'Date_Time','Year','Month','Day','SpdOfMaxGust_km_h_'});
%a while loop divides the Datastore variable data into years
wind_data = readall(ds);
%===================================
% Getting the extracted data ready for analysis
%===================================
%Make a second set of data. Here we just replace <31 with 30.5 in the data
t = cell2table(wind_data.SpdOfMaxGust_km_h_); %extract just the data column for modifications
y = table2array(t); %prepare to make modifications
y = strrep(y,'<31','30.5'); %make modifications
wind_data.SpdOfMaxGust_km_h_ = y;
%remove the rows with missing entries
wind_data = rmmissing(wind_data);
%Covert to DatTime
temp_Date_Time = datetime(wind_data.Date_Time,'InputFormat','yyyy-MM-dd'); %Array that converts 'string date' to datetime format
wind_data.Date_Time = temp_Date_Time;
%Convert Wind data from String to Number
Wind_Gust = cell2table(wind_data.SpdOfMaxGust_km_h_);
Wind_Gust = table2array(Wind_Gust);
Wind_Gust = str2double(Wind_Gust);
wind_data.SpdOfMaxGust_km_h_ = (Wind_Gust);
%Convert Year data from String to Number
Wind_Year = cell2table(wind_data.Year);
Wind_Year = table2array(Wind_Year);
Wind_Year = str2double(Wind_Year);
wind_data.Year = (Wind_Year);
%Convert Month data from String to Number
Wind_Month = cell2table(wind_data.Month);
Wind_Month = table2array(Wind_Month);
Wind_Month = str2double(Wind_Month);
wind_data.Month = (Wind_Month);
%Convert Day data from String to Number
Wind_Day = cell2table(wind_data.Day);
Wind_Day = table2array(Wind_Day);
Wind_Day = str2double(Wind_Day);
wind_data.Day = (Wind_Day);
%===================================
% Modifying the data further for analysis
%===================================
%Delete all the data below 60kmph
A = wind_data;
A = table2timetable(A);
toDelete = A.SpdOfMaxGust_km_h_ < 61;
A(toDelete,:) = [];
%Shift the kmph data
for k = 1:numel(A.SpdOfMaxGust_km_h_)
A.SpdOfMaxGust_km_h_(k) = A.SpdOfMaxGust_km_h_(k) - 60;
end
WindData = A;
PPTH60 = WindData(:,4);
PPTH60 = sortrows(PPTH60,1,'ascend');
PPTH60.Properties.VariableNames{'SpdOfMaxGust_km_h_'} = 'Kmph';
%calculations for PPP
z = numel(PPTH60);
for k = 1:z
PPTH60.Ln_Kmph(k) = log(PPTH60.Kmph(k));
end
toDelete = PPTH60.Ln_Kmph == 0;
PPTH60(toDelete,:) = [];
z = numel(PPTH60.Ln_Kmph);
for k = 1:z
PPTH60.Rank(k) = k;
end
for k = 1:z
PPTH60.Pi(k) = k/(z+1);
end
for k = 1:z
PPTH60.InvPi(k) = norminv(PPTH60.Pi(k));
end
for k = 1:z
PPTH60.ExpPi(k) = -log(1-PPTH60.Pi(k));
end
for k = 1:z
PPTH60.WeibPi(k) = log(-log(1-PPTH60.Pi(k)));
end
for k = 1:z
PPTH60.GumbPi(k) = -log(-log(PPTH60.Pi(k)));
end
%PLOT THE Exponential PPP
p = polyfitB(PPTH60.ExpPi,PPTH60.Kmph,1,0);
f = polyval(p,PPTH60.ExpPi);
figure;
% subplot(2,2,1)
plot(PPTH60.ExpPi,PPTH60.Kmph,'.',PPTH60.ExpPi,f,'-')
title('PPP Gumbel for >60 kmph');
grid on;
legend('data','linear fit')
dim = [0.2 0.5 0.3 0.3];
mdl = fitlm(PPTH60.ExpPi,PPTH60.Kmph);
ylabel('Data(Xi)'); xlabel('-Ln(1-Pi)');
X = sprintf('Exponential PPP: %f X + %f and R-squared = %f',p(1),p(2),mdl.Rsquared.Ordinary);
legend('data','linear fit','Location','southeast')
title(X);
%set(gca,'Ylim',[30 160]) % Adjust Y limits of "current axes"
set(gca,'FontName','Times');
set(gcf,'Units','inches') % Set figure size units of "current figure"
set(gcf,'Color','white');
set(gcf,'Position',[0,0,6,4]) % Set figure width (6 in.) and height (4 in.)
print -deps2c 3.eps % Save as PDF
%PLOT THE LOGNORMAL PPP
p = polyfit(PPTH60.InvPi,PPTH60.Ln_Kmph,1);
f = polyval(p,PPTH60.InvPi);
figure;
% subplot(2,2,2)
plot(PPTH60.InvPi,PPTH60.Ln_Kmph,'.',PPTH60.InvPi,f,'-')
title('PPP LogNormal for >60 kmph');
grid on;
legend('data','linear fit')
dim = [0.2 0.5 0.3 0.3];
mdl = fitlm(PPTH60.InvPi,PPTH60.Ln_Kmph);
ylabel('Ln(Xi)'); xlabel('Standard Normal Percentile');
X = sprintf('LogNormal PPP: %f X + %f and R-squared = %f',p(1),p(2),mdl.Rsquared.Ordinary);
legend('data','linear fit','Location','southeast')
title(X);
%(gca,'Ylim',[30 160]) % Adjust Y limits of "current axes"
set(gca,'FontName','Times');
set(gcf,'Units','inches') % Set figure size units of "current figure"
set(gcf,'Color','white');
set(gcf,'Position',[0,0,6,4]) % Set figure width (6 in.) and height (4 in.)
print -deps2c 4.eps % Save as PDF
%====================
%PLOT THE WEIBULL PPP
p = polyfit(PPTH60.WeibPi,PPTH60.Ln_Kmph,1);
f = polyval(p,PPTH60.WeibPi);
figure;
% subplot(2,2,3)
plot(PPTH60.WeibPi,PPTH60.Ln_Kmph,'.',PPTH60.WeibPi,f,'-')
title('PPP Weibull for >60 kmph');
grid on;
legend('data','linear fit')
dim = [0.2 0.5 0.3 0.3];
mdl = fitlm(PPTH60.WeibPi,PPTH60.Ln_Kmph);
ylabel('Ln(Xi)'); xlabel('Ln(-Ln(1-Pi))');
X = sprintf('Weibull PPP: %f X + %f and R-squared = %f',p(1),p(2),mdl.Rsquared.Ordinary);
legend('data','linear fit','Location','southeast')
title(X);
%set(gca,'Ylim',[30 160]) % Adjust Y limits of "current axes"
set(gca,'FontName','Times');
set(gcf,'Units','inches') % Set figure size units of "current figure"
set(gcf,'Color','white');
set(gcf,'Position',[0,0,6,4]) % Set figure width (6 in.) and height (4 in.)
print -deps2c 5.eps % Save as PDF
%PLOT THE Gumbel PPP
p = polyfit(PPTH60.GumbPi,PPTH60.Kmph,1);
f = polyval(p,PPTH60.GumbPi);
figure;
% subplot(2,2,4)
plot(PPTH60.GumbPi,PPTH60.Kmph,'.',PPTH60.GumbPi,f,'-')
title('PPP Gumbel for >60 kmph');
grid on;
legend('data','linear fit')
dim = [0.2 0.5 0.3 0.3];
mdl = fitlm(PPTH60.GumbPi,PPTH60.Kmph);
ylabel('Data(Xi)'); xlabel('(-Ln(-Ln(Pi)))');
X = sprintf('Gumbel PPP: %f X + %f and R-squared = %f',p(1),p(2),mdl.Rsquared.Ordinary);
legend('data','linear fit','Location','southeast')
title(X);
%set(gca,'Ylim',[30 160]) % Adjust Y limits of "current axes"
set(gca,'FontName','Times');
set(gcf,'Units','inches') % Set figure size units of "current figure"
set(gcf,'Color','white');
set(gcf,'Position',[0,0,6,4]) % Set figure width (6 in.) and height (4 in.)
print -deps2c 6.eps % Save as PDF
% File = 'C:\Users\Maulin Amin\OneDrive - University of Waterloo\Waterloo\Winter 2018\Environment Canada\Wind&Snow\Step6.xlsx';
% ===================================
% CLEAR THE EXCEL FILE BEFORE RUNNING THE PROGRAM
% ===================================
% for SheetNum=1:4
% [N, T, Raw]=xlsread(File, SheetNum);
% [Raw{:, :}]=deal(NaN);
% xlswrite(File, Raw, SheetNum);
% end
% ===================================
% WRITE THE DATA ON EXCEL FILE
% ===================================
% PPTH60 = timetable2table(PPTH60);
% writetable(PPTH60,File,'Sheet','PPPs 60KMPH');