-
Notifications
You must be signed in to change notification settings - Fork 1
Make weather class and fix test XML #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6fb7a69
initial class for weather
nllong 18b720e
formatting
nllong 3160976
use new class in bsync_utils
nllong 5f4b767
precommit
nllong c2a220f
fix test xml
nllong 00e95dc
remove intervalfrequency
nllong 54a1b1b
Update R/weather_data_fetcher.R
nllong File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| # Ensure all lines have proper tags and remove unmatched parentheses | ||
| linters: with_defaults( | ||
| line_length_linter = line_length_linter(120) | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| # BuildingSync®, Copyright (c) Alliance for Sustainable Energy, LLC, and other contributors. | ||
| # See also https://github.com/BuildingSync/bsyncr/blob/main/LICENSE.txt | ||
|
|
||
| library(R6) | ||
| library(rnoaa) | ||
|
|
||
| weather_data_fetcher <- R6Class( | ||
| "weather_data_fetcher", | ||
| public = list( | ||
| station_id = NULL, | ||
| datatype_id = "TAVG", # Temperature Average | ||
| ts_start = NULL, | ||
| ts_end = NULL, | ||
| station_data = NULL, | ||
| weather_data = NULL, | ||
| initialize = function(station_id, ts_start, ts_end) { | ||
| self$station_id <- station_id | ||
| self$ts_start <- ts_start | ||
| self$ts_end <- ts_end | ||
|
|
||
| # Takes a while to run - but should be cached when initializing | ||
| # the this class for the first time. | ||
| self$station_data <- rnoaa::ghcnd_stations() | ||
| }, | ||
| get_nearest_station = function(lat, long) { | ||
| lat_lon_df <- data.frame( | ||
| id = c("my_building"), | ||
| latitude = c(lat), | ||
| longitude = c(long) | ||
| ) | ||
| nearby_stations <- rnoaa::meteo_nearby_stations( | ||
| lat_lon_df = lat_lon_df, | ||
| station_data = self$station_data, | ||
| limit = 1, | ||
| var = c("TAVG") | ||
| ) | ||
| self$station_id <- nearby_stations$my_building$id | ||
| }, | ||
| get_weather_data = function() { | ||
| # check if station_id is NULL | ||
| if (is.null(self$station_id)) { | ||
| stop("Station ID is NULL. Please set the station ID first or call get_nearest_station()") | ||
| } | ||
|
|
||
| # get MONTHLY temp data from station | ||
| weather_result <- rnoaa::ncdc( | ||
| datasetid = "GSOM", | ||
| stationid = sprintf("GHCND:%s", self$station_id), | ||
| datatypeid = "TAVG", | ||
| # messy solution, but ensures that we get data before our start time | ||
| startdate = strftime(self$ts_start - (60 * 60 * 24 * 31), "%Y-%m-%dT%H:%M:%S"), | ||
| enddate = self$ts_end, | ||
| add_units = TRUE | ||
| ) | ||
|
|
||
| self$weather_data <- weather_result$data | ||
| return(self$weather_data) | ||
| }, | ||
| to_df = function(n_samples = 12) { | ||
| # convert the weather data to a data frame | ||
|
|
||
| # check if weather_data is NULL | ||
| if (is.null(self$weather_data)) { | ||
| stop("Weather data is NULL. Please call get_weather_data() first.") | ||
| } | ||
| # check if weather_data is empty | ||
| if (length(self$weather_data) == 0) { | ||
| stop("Weather data is empty. Please check the station ID and date range.") | ||
| } | ||
| # check if weather_data has the expected structure | ||
| if (!all(c("date", "value", "units") %in% names(self$weather_data))) { | ||
| stop("Weather data does not have the expected structure.") | ||
| } | ||
|
|
||
| cat(paste("Weather data:", self$weather_data, "\n")) | ||
| temp_df <- data.frame( | ||
| date = self$weather_data$date, | ||
| temp = self$weather_data$value, | ||
| units = self$weather_data$units | ||
| ) | ||
|
|
||
| # fix data types? | ||
| # temp_df[, "temp"] <- as.double(temp_df[, "temp"]) | ||
| # temp_df[, "time"] <- as.POSIXct.numeric(temp_df[, "time"], origin = lubridate::origin) | ||
|
|
||
| return(temp_df) | ||
| } | ||
| ) | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.