Load relevant packages and set working directory
library(ncdf4)
setwd("G:/Thesis data")
getwd()
## [1] "G:/Thesis data"
path <- file.path(getwd());
Open the u and v wind files in working directory
nc_u <-nc_open(file.path(path,'uwnd.sig995.mon.mean.nc'))
nc_v <-nc_open(file.path(path,'vwnd.sig995.mon.mean.nc'))
get the data, uwnd = monthly mean eastward wind
uwind <- ncvar_get(nc_u, 'uwnd')
get the data, vwnd = monthly mean northward wind
vwind <- ncvar_get(nc_v, 'vwnd')
Replace missing values with NA
uwind[uwind==-9.99999] <- NA
vwind[vwind==-9.99999] <- NA
^^Missing values in the v and u wind datasets are -9.99999, and so replaced with NA
dim(uwind) ####= [1] 180 91 1968
## [1] 180 91 1968
dim(vwind) ####= [1] 180 91 1968
## [1] 180 91 1968
lat<-ncvar_get(nc_u, 'lat') #### latitude, -90 90
lon<-ncvar_get(nc_v, 'lon') #### logitude, 0 358
load time
time<-ncvar_get(nc_u, 'time')
get units
tunits<-ncatt_get(nc_u,"time",attname="units")
tustr<-strsplit(tunits$value, " ")
date<-as.character(as.Date(time/24,origin=unlist(tustr)[3]))
‘date’ now contains 1968 points (one for each month in the dataset)
Getting mean windspeed from u and v wind
windspeed <- sqrt(uwind^2 + vwind^2)
windspeed is on a 0 to 360 grid. This can be seen by viewing lon, values go from 0 to 358 degrees.
lon_split = which(lon==180) #### find the date line
lon = c(lon[(lon_split+1):length(lon)]-360,lon[1:lon_split]) #### rearrange the longitudes
windspeed = windspeed[c((lon_split+1):length(lon),1:lon_split),,] #### and the data
Data Manipulation
Subsetting the data
Irish series
Windspeed_IRE <- windspeed[83:87,18:21, ] #164 year monthly mean windspeed over Ireland
Now need to get on same time frame as Met Éireann series (1940-2014)
1069 represents January 1940 1968 represents December 2014
Windspeed_IRE <- Windspeed_IRE[1069:1968] # 75 year monthly mean windspeed over Ireland
Getting annual means
Array of 1968 months
year <- format(as.Date(date, format="%Y-%m-%d"),"%Y")
For Irish series…
year_IRE <- year[1069:1968]
Join ‘windspeed_IRE’ to ‘year_IRE’ to create a data frame
Windspeed_IRE_monmeans <- data.frame(year_IRE, Windspeed_IRE)
Export to wd as a csv file
write.csv(Windspeed_IRE_monmeans, file = "20C windspeed mon means 1940-2014.csv")
Import dataset again using read.csv
Windspeed_monmeans <- read.csv("20C windspeed mon means 1940-2014.csv")
Convert from monthly means to annual means Need to write a function to to this:
n.colmeans = function(df, n = 12){
aggregate(x = df,
by = list(gl(ceiling(nrow(df)/n), n)[1:nrow(df)]) ,FUN = mean)}
n.colmeans(Windspeed_monmeans, 12) -> Windspeed_anmeans ####annual mean windspeed, 1940-2014
Tidy up the data
years <- Windspeed_anmeans$year_IRE
anmeans <- Windspeed_anmeans$Windspeed
Annual_mean_windspeed <- data.frame(years,anmeans)
Plotting
Get the series mean to include on final plot
series_mean <- mean(anmeans) # series mean = 4.35781815244222
Time series of annual mean windspeed over Ireland, 1940-2014
plot(Annual_mean_windspeed, type='l', xlab='Year', ylab='Windspeed(m/s)',
main='20C annual mean windspeed Ireland', sub = 'Mean = 4.36', col='black')