forked from isithot/isithotrightnow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlgorithm.R
More file actions
79 lines (68 loc) · 2.83 KB
/
Copy pathAlgorithm.R
File metadata and controls
79 lines (68 loc) · 2.83 KB
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
# IsItHotRightNow.com
# Algorithm
# Libraries
library(jsonlite)
library(lubridate)
library(plotly)
# Set working directory
setwd("~/ownCloud/IsItHotRightNow/isithotrightnow/")
# Get Data
# Get Climatology data
# Use Mat's pre-calculated Tmax 90th pc data
SydObs.tmax90p.clim.raw <- read.csv("data/tmax90p_066062.csv", header = F,
stringsAsFactors = F)
names(SydObs.tmax90p.clim.raw) <- c("Month", "Day", "Tmax90p")
SydObs.tmin90p.clim.raw <- read.csv("data/tmin90p_066062.csv", header = F,
stringsAsFactors = F)
names(SydObs.tmin90p.clim.raw) <- c("Month", "Day", "Tmin90p")
# Get current half hourly data for the past 3 days
# from http://www.bom.gov.au/fwo/IDN60901/IDN60901.94768.json
url = "http://www.bom.gov.au/fwo/IDN60901/IDN60901.94768.json"
SydObs.json <- readLines(url)
SydObs.data <- fromJSON(SydObs.json)
# Create a dataframe with Date_time in first column and air_temp in second column
date_time <- ymd_hms(SydObs.data$observations$data$local_date_time_full,
tz = "Australia/Sydney")
air_temp <- SydObs.data$observations$data$air_temp
# Now create the data frame
SydObs.df <- data.frame(date_time, air_temp)
# sample plot
plot_ly(x = ~date_time, y = ~air_temp, type = 'scatter', mode = 'lines') %>%
layout(xaxis = list(title = "Time"),
yaxis = list(title = "Temperature (Degrees C)"))
# Now the algorithm
# --
# Take the maximum and minimum temperatures of the last 24h
# and average them to the the avg(Tmax,Tmin)
# then compare this Tavg with the climatology
# We use pre-calculated statistics from BOM
# Climatology value is create by averaging
# 90th pc Tmax and 90th pc Tmin from BOM statistics
# --
# Let's first get the current month, day and current date_time
current.date_time <- SydObs.df$date_time[1]
current.month <- month(current.date_time)
current.day <- day(current.date_time)
# Now let's get the air_temp max and min over the past
# 24h and average them
Tmax.now <- max(SydObs.df$air_temp[1:48])
Tmin.now <- min(SydObs.df$air_temp[1:48])
# Note this is not a true average, just an average of the
# max and min values
Tavg.now <- mean(c(Tmax.now, Tmin.now))
#Now we get the 90th pc Tmax and Tmin from the climatology
# row 7 is Decile 9 maximum temperature (Degrees C) for years 1859 to 2017
# row 17 is Decile 9 minimum temperature (Degrees C) for years 1859 to 2017
clim.row <- which(SydObs.tmax90p.clim.raw$Month == current.month &
SydObs.tmax90p.clim.raw$Day == current.day)
# Tmax90p is in the third column
Tmax90p.clim <- as.numeric(SydObs.tmax90p.clim.raw[clim.row, 3])
Tmin90p.clim <- as.numeric(SydObs.tmin90p.clim.raw[clim.row, 3])
Tavg90p.clim <- mean(c(Tmax90p.clim, Tmin90p.clim))
# Now we return 1 if Tavg.now >= Tavg90p.clim and
# return 0 otherwise
if (Tavg.now >= Tavg90p.clim) {
answer = 1
} else {
answer = 0
}