-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRainfall_Data_Analysis_-_Source_Code.R
226 lines (203 loc) · 11 KB
/
Rainfall_Data_Analysis_-_Source_Code.R
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#---------------------------------------------------------------------------------------------------------------------------------------------------------
# Reading in Data as data frame, converting into Time series.
#---------------------------------------------------------------------------------------------------------------------------------------------------------
# Loading library "readxl" for reading in .xlsx file via R.
# Loading library "openxlsx" for writing in .xlsx file via R.
library(readxl)
library(openxlsx)
# Data from file is stored in the data frame named as 'Intermediate'.
# "read_excel" function of "readxl" library is used.
Intermediate<-read_excel("Rainfall_Data_(111_years).xlsx")
# Removing NA values from 'Intermediate'.
Intermediate<-na.omit(Intermediate)
# Finding names of each district seperately using "unique" function.
Unique<-unique(Intermediate$DIST)
# "which" function is used to find the row number for which certain value is present in the data frame.
# This function can be used to create subset of data for each district.
# In order to view an example for showing the working of "which" function, remove hash from the following snippet.
# Intermediate[which(Intermediate$DIST==Unique[1])[1]:which(Intermediate$DIST==Unique[1])[length(which(Intermediate$DIST==Unique[1]))],]
# Creating a folder for storing all .xlsx files having data for each district seperately.
dir.create("Intermediate")
# Creating loop for writing an excel file with a particular district data.
for(i in 1:length(Unique))
{
write.xlsx(Intermediate[which(Intermediate$DIST==Unique[i])[1]:which(Intermediate$DIST==Unique[i])[length(which(Intermediate$DIST==Unique[i]))],-1:-3],
file=paste0("Intermediate/",Unique[i],".xlsx"),
sheetName = Unique[i], col.names = TRUE, row.names = TRUE, append = TRUE)
}
# Reading each file and changing its rownames with years.
# Removing all "WC" data.
# Storing each district's data in a separate data frame having the name of that particular district.
for(j in 1:length(Unique))
{
Data<-read_excel(paste0("Intermediate/",Unique[j],".xlsx"))
Data<-as.data.frame(Data)
row.names(Data)<-Intermediate$Year[1:length(row.names(Data))]
Data<-Data[,-1]
assign(Unique[j],Data)
}
# Deleting "Intermediate" directory.
unlink("Intermediate",recursive=T)
#---------------------------------------------------------------------------------------------------------------------------------------------------------
# Plotting Monthwise and Annual data for each district.
#---------------------------------------------------------------------------------------------------------------------------------------------------------
# Creating a folder for storing all analysis charts for each district seperately.
dir.create("Analysis")
for(k in 1:length(Unique))
{
# Creating a folder for each district.
dir.create(paste0("Analysis/",Unique[k]))
}
# Converting each data frame containing district rainfall data into time series and plotting its graph.
# Saving each plot in its respective folder.
for(l in 1:length(Unique))
{
# Creating dataframes each having a seperate district data with the same name as the respective district.
Data<-eval(parse(text=paste0("`",Unique[1],"`")))
# For plotting monthwise.
Month<-Data
Month<-(as.vector(t(as.matrix(Month[,-13:-18]))))
Month<-ts(Month,frequency=12,start=Intermediate$Year[1],
end=Intermediate$Year[length(Intermediate$Year)])
# Saving the plot on monthwise rainfall in working directory.
png(file=paste0("Analysis/",Unique[l],"/Monthwise.png"),width=800,height=600)
plot(Month,main=paste0("Monthwise Data Visualization - ",Unique[l]),
ylab="Rainfall in mm",xlab="Years")
dev.off(which=dev.cur())
}
for(m in 1:length(Unique))
{
# Creating dataframes each having a seperate district data with the same name as the respective district.
Data<-eval(parse(text=paste0("`",Unique[m],"`")))
# For plotting Annual Rainfall.
Annual<-Data
Annual<-(as.vector(t(as.matrix(Annual[,13]))))
Annual<-ts(Annual,frequency=1,start=Intermediate$Year[1],
end=Intermediate$Year[length(Intermediate$Year)])
# Saving the plot on annual rainfall in working directory.
png(file=paste0("Analysis/",Unique[m],"/Annual.png"),width=800,height=600)
plot(Annual,main=paste0("Annual Data Visualization - ",Unique[m]),
ylab="Rainfall in mm",xlab="Years")
dev.off(which=dev.cur())
}
#---------------------------------------------------------------------------------------------------------------------------------------------------------
# 30 year moving average estimation and deviation of actual rainfall from 30 year moving average for "Adilabad" district using Annual rainfall data.
#---------------------------------------------------------------------------------------------------------------------------------------------------------
library(pracma)
for(n in 1:length(Unique))
{
# Creating dataframes each having a seperate district data with the same name as the respective district.
Data<-eval(parse(text=paste0("`",Unique[n],"`")))
# Analysing Annual data.
Annual<-Data
Annual<-(as.vector(t(as.matrix(Annual[,13]))))
Annual<-ts(Annual,frequency=1,start=Intermediate$Year[1],
end=Intermediate$Year[length(Intermediate$Year)])
# Saving the plot on moving average for 30 years and deviation between original data and moving average.
png(file=paste0("Analysis/",Unique[n],"/Moving Average for 30 years.png"),width=800,height=600)
plot(movavg(Annual,n=30),type="l",ylab="Rainfall in mm",
main=paste0("Moving Average for 30 years - ",Unique[n]),xlab="Period (Number of years past 1901)")
dev.off(which=dev.cur())
#x-----------------------------------------------------------------------------------------------------------------------------------------------------x
png(file=paste0("Analysis/",Unique[n],"/Deviation of Actual Rainfall from 30 years Moving Average.png"),width=800,height=600)
plot(Annual-movavg(Annual,n=30),type="l",ylab="Deviation in mm",
main=paste0("Deviation of Actual Rainfall from 30 years Moving Average - ",Unique[n]),xlab="Years")
dev.off(which=dev.cur())
}
#---------------------------------------------------------------------------------------------------------------------------------------------------------
# Defining above and below Normal rainfall.
#---------------------------------------------------------------------------------------------------------------------------------------------------------
for(o in 1:length(Unique))
{
# Creating dataframes each having a seperate district data with the same name as the respective district.
Data<-eval(parse(text=paste0("`",Unique[o],"`")))[13]
# Finding normal rainfall value.
Normal<-sum(Data)/length(Data$WC13)
Data$Frequency<-c(0)
for(p in 1:length(rownames(Data)))
{
if(Data$WC13[p]>=(Normal*1.1))
{
Data$Frequency[p]=1
}
else if(Data$WC13[p]<=(Normal*0.9))
{
Data$Frequency[p]=-1
}
}
# Saving the plot on above & below normal rainfall and its frequency.
png(file=paste0("Analysis/",Unique[o],"/Above & Below Normal Rainfall and its Frequency.png"),width=800,height=600)
plot(Data$WC13,type="l",main=paste0("Above & Below Normal Rainfall and its Frequency - ",
Unique[o]),ylab="Rainfall in mm",xlab="Period (Number of years past 1901)")
points(Data$WC13,col=ifelse(Data$Frequency==1,"Red",ifelse(Data$Frequency==0,"Black","Blue")),pch=20)
abline(h=Normal*1.1,col="Red")
abline(h=Normal*0.9,col="Blue")
legend('bottomleft',horiz=TRUE,c(paste0("Above Normal Frequency - ",sum(Data$Frequency==1,na.rm=TRUE)," years"),
paste0("Below Normal Frequency - ",sum(Data$Frequency==-1,na.rm=TRUE)," years")),cex=.7,
fill=c("red","blue"),bty='n')
dev.off(which=dev.cur())
}
#---------------------------------------------------------------------------------------------------------------------------------------------------------
# Estimating percentage of districts having above and below normal rainfall for each year.
#---------------------------------------------------------------------------------------------------------------------------------------------------------
# Creating vector for storing all normal rainfall values for each district.
Normal_Rainfall<-vector(mode='numeric',length=length(Unique))
for(q in 1:length(Unique))
{
Temp<-eval(parse(text=paste0("`",Unique[q],"`")))[13]
Normal_Rainfall[q]=sum(Temp$WC13)/length(Temp$WC13)
}
# Converting into a data frame.
Normal_Rainfall<-as.data.frame(Normal_Rainfall)
rownames(Normal_Rainfall)<-Unique
# Adding frequency of districts having above and below normal rainfall for all years.
for(r in 1:length(rownames(Data)))
{
Frequency<-vector(mode='numeric',length=length(Unique))
for(s in 1:length(Unique))
{
if(((eval(parse(text=paste0("`",Unique[s],"`")))$WC13[r]))>=(Normal_Rainfall$Normal_Rainfall[s]*1.1))
{
Frequency[s]=1
}
else if(((eval(parse(text=paste0("`",Unique[s],"`")))$WC13[r]))<=(Normal_Rainfall$Normal_Rainfall[s]*0.9))
{
Frequency[s]=-1
}
}
Normal_Rainfall[paste0(rownames(Data)[r])]<-Frequency
}
Normal_Rainfall<-Normal_Rainfall[,-1]
Above_Normal<-vector(mode='numeric',length=length(rownames(Data)))
Below_Normal<-vector(mode='numeric',length=length(rownames(Data)))
Above_Normal_percent<-vector(mode='numeric',length=length(rownames(Data)))
Below_Normal_percent<-vector(mode='numeric',length=length(rownames(Data)))
for(t in 1:length(rownames(Data)))
{
Above_Normal[t]=(sum(Normal_Rainfall[,t]==1,na.rm=T))
Below_Normal[t]=(sum(Normal_Rainfall[,t]==-1,na.rm=T))
}
for(t in 1:length(rownames(Data)))
{
Above_Normal_percent[t]=(sum(Normal_Rainfall[,t]==1,na.rm=T)/length(Unique))*100
Below_Normal_percent[t]=(sum(Normal_Rainfall[,t]==-1,na.rm=T)/length(Unique))*100
}
Above_Normal<-as.data.frame(Above_Normal)
rownames(Above_Normal)<-rownames(Data)
Below_Normal<-as.data.frame(Below_Normal)
rownames(Below_Normal)<-rownames(Data)
Above_Normal_percent<-as.data.frame(Above_Normal_percent)
rownames(Above_Normal_percent)<-rownames(Data)
Below_Normal_percent<-as.data.frame(Below_Normal_percent)
rownames(Below_Normal_percent)<-rownames(Data)
# Saving plot on percentage of districts having above and below normal rainfall for each year.
png(file=paste0("Analysis/","Percentage of Districts having Above Normal rainfall.png"),width=800,height=600)
plot(ts(Above_Normal,start=as.numeric(rownames(Above_Normal)[1]),
end=as.numeric(rownames(Above_Normal)[length(rownames(Above_Normal))])),
main="Percentage of districts having above normal rainfall",ylab="Percentage of Districts",xlab="Years",col="Red")
dev.off(which=dev.cur())
png(file=paste0("Analysis/","Percentage of Districts having Below Normal rainfall.png"),width=800,height=600)
plot(ts(Below_Normal,start=as.numeric(rownames(Below_Normal)[1]),
end=as.numeric(rownames(Below_Normal)[length(rownames(Below_Normal))])),
main="Percentage of districts having below normal rainfall",ylab="Percentage of Districts",xlab="Years",col="Blue")
dev.off(which=dev.cur())