library(ggplot2)
library(openair)
library(reshape2)

ecan <- read.csv('RangioraWinter2016.csv',stringsAsFactors=FALSE)
names(ecan) <- c('date','time','wind.speed','wind.dir','wind.dir.std','wind.speed.std',
                 'wind.max','co','temp.ground','temp.2m','temp.6m','pm10',
                 'pm2.5','pm.course')
ecan$date <- as.POSIXct(ecan$date,format = '%m/%d/%Y %H:%M', tz='Etc/GMT-12')

ecan <- ecan[,c('date','wind.speed','wind.dir')]
# convert wind from polar to cartesian coords

# convert from degrees to radians
ecan$wind.dir <- ecan$wind.dir * pi / 180

# assume wind.dir = 0 means pointing north
# want wind.dir = 0 to be pointing east
ecan$wind.dir <- ecan$wind.dir - pi/2

# also assume that wind.dir goes anti-clockwise
ecan$wind.x <- ecan$wind.speed * cos(ecan$wind.dir)
ecan$wind.y <- ecan$wind.speed * sin(ecan$wind.dir)

Day Wind Plots

day <- 6*24 # measurements taken at 10 minute intervals, thus 6*24 measurements = 1 day

# grab three 24 hour periods starting at 00:00
ecan$datel <- as.POSIXlt(ecan$date)
start <- 1
while (ecan[start,'datel']$min != 0 & ecan[start,'datel'] != 0)
  start <- start + 1
mid <- as.integer(nrow(ecan)/2)
while (ecan[mid,'datel']$min != 0 & ecan[mid,'datel'] != 0)
  mid <- mid + 1
end <- nrow(ecan) - day
while (ecan[mid,'datel']$min != 0 & ecan[mid,'datel'] != 0)
  end <- end + 1

ecan$hour <- ecan$datel$hour + ecan$datel$min / 60

first.day <- ecan[start:(start+day),]
mid.day <- ecan[mid:(mid+day),]
last.day <- ecan[end:(end+day),]

for (n.day in list(first.day,mid.day,last.day)) {
  n.day <- melt(n.day,measure.vars=c("wind.x","wind.y"))
  date <- trunc(n.day$date[1],'day')
  plt <- ggplot(n.day) +
            geom_line(aes(x=hour,y=value,colour=variable)) + 
            scale_colour_manual(values=c("red","blue")) + 
            ylab("Wind Speed (m/s)") +
            xlab("Time of Day (h)") +
            ggtitle(paste("Wind on",date))
  print(plt)
}

Month Wind Plots

I don’t know why the month on the x-axis is different to that in the title.

ecan$day <- ecan$datelt$day

april <- ecan[ecan$datel$mon==4,]
june <- ecan[ecan$datel$mon==6,]
october <- ecan[ecan$datel$mon==10,]
month.names <- c('April','June','October')
months <- list(april,june,october)

for (i in 1:3) {
  month <- months[[i]]
  month$datel <- NULL # timeAverage doesn't like this
  month <- timeAverage(month,avg.time="day")
  month <- melt(month,measure.vars=c("wind.x","wind.y"))
  date <- month$date[1]#trunc(month$date[1],'day')
  plt <- ggplot(month) +
            geom_line(aes(x=date,y=value,colour=variable)) + 
            scale_colour_manual(values=c("red","blue")) + 
            ylab("Wind Speed (m/s)") +
            xlab("Date") +
            ggtitle(paste("Daily Average Wind in",month.names[i]))
  print(plt)
}

Whole Interval Wind Plot

whole <- ecan
whole$datel <- NULL # timeAverage doesn't like POSIXlt
whole <- timeAverage(whole,avg.time="7 day")
whole <- melt(whole,measure.vars=c("wind.x","wind.y"))
plt <- ggplot(whole) +
          geom_line(aes(x=date,y=value,colour=variable)) + 
          scale_colour_manual(values=c("red","blue")) + 
          ylab("Wind Speed (m/s)") +
          xlab("Date") +
          ggtitle("Weekly Averages Over Whole Time Period")
print(plt)