Downscale SILO Hourly Data and Visualise Quality of Predictions

This document illustrates the useage of the JohnConner R package to downscale Queenslan SILO weather data from daily to hourly and send an e-mail alert to subscribers, warning them of heat stress to their crop.

  library(JohnConner)
  BoM_SILO_data <- downscale()
  plot_density(BoM_SILO_data)

Check Error

rmse(BoM_SILO_data[, 3], BoM_SILO_data[, 5])

mae(BoM_SILO_data[, 3], BoM_SILO_data[, 5])

Generate Map of Heat Stress

library(raster)
library(fields)
library(reshape)
library(readr)

hourlyT <- BoM_SILO_data[, c(1:2, 4:5)]
head(hourlyT)
hourlyT$hour <- substr(hourlyT[, 3], 6, 7)
hourlyT <- subset(hourlyT, hourlyT$JDay == 1)[, -3]

hourlyT <- melt(hourlyT, id.vars = c("station_number", "hour", "JDay"), measure.vars = "SILO_TMP")
hourlyT <- cast(hourlyT, hour~station_number, fun = "mean")

head(QLD_SILO_and_hourly_stations)

header_info <- QLD_SILO_and_hourly_stations[, 2]

temp_threshold <- 30

hours_of_high_temps <- c()

for (x in names(hourlyT[, 2:71])) {
  hours_of_high_temps <- c(hours_of_high_temps, length(hourlyT[, x][hourlyT[, x] > temp_threshold]))
}

high_temps <- data.frame(names(hourlyT[, 2:71]), as.numeric(hours_of_high_temps))
names(high_temps) <- c("station_number", "hours_of_high_temps")

df <- plyr::join(high_temps, QLD_SILO_and_hourly_stations, by = "station_number", type = "left")
head(df)

# create the surface to plot

spline <- Tps(data.frame(df$LONGITUDE, df$LATITUDE), df$hours_of_high_temps)
grid <- predictSurface(spline, nx = 2000, ny = 2000)
predict(spline, cbind(151.9507, -27.5598))

Oz <- getData("GADM", country = "AUS", level = 1)
QLD <- Oz[Oz@data$NAME_1 == "Queensland", ]

image.plot(grid, horizontal = TRUE)
plot(QLD, add = TRUE)

Automated e-mails to subscribers

# send email abut specific locations
our_email <- "toowoombatrio@gmail.com"
attachmentName <- "QLDmap.png"
#subscribers details

library(mailR)

for (value in subscribers$run) {
  email <- as.character(subscribers[value,1])
  location <- as.character(subscribers[value,2])
  lat <- as.numeric(subscribers[value,3])
  lon <- as.numeric(subscribers[value,4])
  hours_of_heat_stress <- as.character(round(predict(spline, cbind(lat,lon)),0))
  body_text <- paste("Hello from the Toowoomba Trio Your crops at",
                     location, "recieved",
                     hours_of_heat_stress,
                     "hours of heat stress yesterday")

  sender <- our_email
  recipients <- email
  send.mail(from = sender,
            to = recipients,
            subject = "Hours of heat stress yesterday",
            body = body_text,
            attach.files = attachmentName,
            smtp = list(host.name = "smtp.gmail.com", port = 465, 
                        user.name = "toowoombatrio@gmail.com",
                        passwd = "qwertyytrewq", ssl = TRUE),
            authenticate = TRUE,
            send = TRUE)

}


ToowoombaTrio/John_Conner documentation built on May 9, 2019, 5:11 p.m.