Skip to content
This repository has been archived by the owner on May 14, 2024. It is now read-only.

Commit

Permalink
added get_coastal_forecast function
Browse files Browse the repository at this point in the history
- finalised function
- tested
- added unit tests
- updated documentation
- rebuilt and tested package
  • Loading branch information
deanmarchiori committed Jul 28, 2018
1 parent 6a8cc93 commit d54f9f6
Show file tree
Hide file tree
Showing 26 changed files with 318 additions and 105 deletions.
5 changes: 3 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,11 @@ Suggests:
maps,
mapproj,
rappdirs
RoxygenNote: 6.0.1
RoxygenNote: 5.0.1
NeedsCompilation: no
ByteCompile: TRUE
VignetteBuilder: knitr
X-schema.org-applicationCategory: Tools
X-schema.org-keywords: bom, meteorological-data, weather-forecast, australia, weather, weather-data, meteorology, australia-bureau-of-meteorology
X-schema.org-keywords: bom, meteorological-data, weather-forecast, australia,
weather, weather-data, meteorology, australia-bureau-of-meteorology
X-schema.org-isPartOf: https://ropensci.org
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

export(get_ag_bulletin)
export(get_available_imagery)
export(get_coastal_forecast)
export(get_current_weather)
export(get_historical)
export(get_precis_forecast)
Expand Down
62 changes: 25 additions & 37 deletions R/get_coastal_forecast.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#' Get BOM Coastal Waters Forecast
#'
#' Fetch the BOM daily Coastal Waters Forecast and retun a tidy data frame of
#' Fetch the BOM daily Coastal Waters Forecast and return a tidy data frame of
#' the forecast regions for a specified state or region.
#'
#' @param state Australian state or territory as full name or postal code.
#' Fuzzy string matching via \code{\link[base]{agrep}} is done. Defaults to
#' "AUS" returning all state bulletins, see details for further information.
#' "AUS" returning all state forecasts, see details for further information.
#'
#' @details Allowed state and territory postal codes, only one state per request
#' or all using \code{AUS}.
Expand Down Expand Up @@ -34,14 +34,13 @@
#' Services \cr
#' \url{http://www.bom.gov.au/catalogue/data-feeds.shtml}
#'
#' Location data and other metadata for towns come from
#' Location data and other metadata come from
#' the BOM anonymous FTP server with spatial data \cr
#' \url{ftp://ftp.bom.gov.au/anon/home/adfd/spatial/}, specifically the DBF
#' file portion of a shapefile, \cr
#' \url{ftp://ftp.bom.gov.au/anon/home/adfd/spatial/IDM00013.dbf}
#' \url{ftp://ftp.bom.gov.au/anon/home/adfd/spatial/IDM00003.dbf}
#'
#' @author Adam H Sparks, \email{adamhsparks@@gmail.com} and Keith Pembleton,
#' \email{keith.pembleton@@usq.edu.au}
#' @author Dean Marchiori, \email{deanmarchiori@@gmail.com}
#' @importFrom magrittr %>%
#' @export
get_coastal_forecast <- function(state = "AUS") {
Expand Down Expand Up @@ -81,24 +80,21 @@ get_coastal_forecast <- function(state = "AUS") {
the_state == "WA" |
the_state == "WESTERN AUSTRALIA" ~ paste0(ftp_base, AUS_XML[7])
)
out <- .parse_forecast(xmlforecast_url)
out <- .parse_coastal_forecast(xmlforecast_url)
} else {
file_list <- paste0(ftp_base, AUS_XML)
out <- lapply(X = file_list, FUN = .parse_forecast)
out <- lapply(X = file_list, FUN = .parse_coastal_forecast)
out <- as.data.frame(data.table::rbindlist(out, fill = TRUE))
}

return(out)

}


.parse_forecast <- function(xmlforecast_url) {
.parse_coastal_forecast <- function(xmlforecast_url) {
# CRAN note avoidance
AAC_codes <- attrs <- end_time_local <- precipitation_range <- # nocov start
AAC_codes <- marine_AAC_codes <- attrs <- end_time_local <- precipitation_range <- # nocov start
start_time_local <- values <- NULL # nocov end

# download the XML forecast --------------------------------------------------
# download the XML forecast
tryCatch({
xmlforecast <- xml2::read_xml(xmlforecast_url)
},
Expand All @@ -109,39 +105,28 @@ get_coastal_forecast <- function(state = "AUS") {
))

areas <- xml2::xml_find_all(xmlforecast, ".//*[@type='coast']")

out <- lapply(X = areas, FUN = .parse_areas)
out <- as.data.frame(do.call("rbind", out))

# This is the actual returned value for the main function. The functions
# below chunk the xml into locations and then days, this assembles into
# the final data frame

out <- tidyr::spread(out, key = attrs, value = values)

# tidy up names
names(out) <- gsub("c\\(", "", names(out))
names(out) <- gsub("\\)", "", names(out))

out <- out %>%
janitor::clean_names() %>%
janitor::clean_names(., case = "snake") %>%
janitor::remove_empty("cols")

out <-
out %>%
tidyr::separate(end_time_local,
into = c("end_time_local", "UTC_offset"),
sep = "\\+") %>%
out <- out %>%
tidyr::separate(
end_time_local,
into = c("end_time_local", "UTC_offset"),
sep = "\\+") %>%
tidyr::separate(
start_time_local,
into = c("start_time_local", "UTC_offset_drop"),
sep = "\\+"
)

sep = "\\+")

# drop the "UTC_offset_drop" column
out <- out[!names(out) %in% "UTC_offset_drop"]


# remove the "T" from the date/time columns
out[, c("start_time_local",
"end_time_local",
Expand All @@ -160,7 +145,6 @@ get_coastal_forecast <- function(state = "AUS") {
"end_time_utc")], 2, function(x)
chartr("Z", " ", x))


# convert factors to character for left merge, otherwise funny stuff happens
out[, seq_len(ncol(out))] <-
lapply(out[, seq_len(ncol(out))], as.character)
Expand All @@ -178,6 +162,7 @@ get_coastal_forecast <- function(state = "AUS") {
load(system.file("extdata", "marine_AAC_codes.rda", package = "bomrang")) # nocov

# return final forecast object -----------------------------------------------

# merge with aac codes for location information
tidy_df <-
dplyr::left_join(out,
Expand Down Expand Up @@ -205,12 +190,15 @@ get_coastal_forecast <- function(state = "AUS") {
"start_time_utc",
"end_time_utc",
"forecast_seas",
"forecast_swell1",
"forecast_weather",
"forecast_winds"
"forecast_winds",
"forecast_swell1"
)

# create factors
tidy_df$index <- as.factor(tidy_df$index)

tidy_df <- tidy_df[c(refcols, setdiff(names(tidy_df), refcols))]

return(tidy_df)
}

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ devtools::install_github("ropensci/bomrang", build_vignettes = TRUE)
Using *bomrang*
---------------

Several functions are provided by *bomrang* to retrieve Australian Bureau of Meteorology (BOM) data. A family of functions retrieve weather data and return tidy data frames; `get_precis_forecast()`, which retrieves the précis (short) forecast; `get_current_weather()`, which fetches the current weather from a given station; `get_ag_bulletin()`, which retrieves the agriculture bulletin; `get_weather_bulletin()`, which retrieves the BOM 0900 or 1500 bulletins; and `get_historical()`, which retrieves historical daily observations for a given station. A second group of functions retrieve information pertaining to satellite imagery, `get_available_imagery()` and the imagery itself, `get_satellite_imagery()`. Vignettes are provided illustrating examples of all functions and a use case.
Several functions are provided by *bomrang* to retrieve Australian Bureau of Meteorology (BOM) data. A family of functions retrieve weather data and return tidy data frames; `get_precis_forecast()`, which retrieves the précis (short) forecast; `get_current_weather()`, which fetches the current weather from a given station; `get_ag_bulletin()`, which retrieves the agriculture bulletin; `get_weather_bulletin()`, which retrieves the BOM 0900 or 1500 bulletins; `get_coastal_forecast()`, which returns coastal waters forecasts and `get_historical()`, which retrieves historical daily observations for a given station. A second group of functions retrieve information pertaining to satellite imagery, `get_available_imagery()` and the imagery itself, `get_satellite_imagery()`. Vignettes are provided illustrating examples of all functions and a use case.

Meta
----
Expand Down
1 change: 1 addition & 0 deletions bomrang.Rproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ LaTeX: pdfLaTeX
BuildType: Package
PackageUseDevtools: Yes
PackageInstallArgs: --no-multiarch --with-keep.source
PackageRoxygenize: rd,collate,namespace
3 changes: 2 additions & 1 deletion data-raw/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ Australian Bureau of Meteorology (BOM). See BOM's
details.

Data sources are acknowledged in the
[create_BOM_forecast_locations.md](create_BOM_forecast_locations.md) and
[create_BOM_forecast_locations.md](create_BOM_forecast_locations.md) and
[create_BOM_marine_locations.md](create_BOM_marine_locations.md) and
[create_BOM_station_list.md](create_BOM_station_list.md) files respectively.

Also data sources are acknowledged in the references section for each applicable
Expand Down
2 changes: 1 addition & 1 deletion data-raw/create_BOM_marine_locations.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ knitr::opts_chunk$set(echo = TRUE)

BOM maintains a shapefile of forecast marine zone names and their geographic locations.
For ease, we'll just use the .dbf file part of the shapefile to extract AAC
codes that can be used to add lat/lon values to the forecast `data.frame` that
codes that can be used to add locations to the forecast `data.frame` that
`get_coastal_forecast()` returns. The file is available from BOM's anonymous
FTP server with spatial data <ftp://ftp.bom.gov.au/anon/home/adfd/spatial/>,
specifically the DBF file portion of a shapefile,
Expand Down
2 changes: 1 addition & 1 deletion data-raw/create_BOM_marine_locations.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Get BOM Marine Zones
Get BOM Forecast Marine Zones
-----------------------------

BOM maintains a shapefile of forecast marine zone names and their geographic locations. For ease, we'll just use the .dbf file part of the shapefile to extract AAC codes that can be used to add lat/lon values to the forecast `data.frame` that `get_coastal_forecast()` returns. The file is available from BOM's anonymous FTP server with spatial data <ftp://ftp.bom.gov.au/anon/home/adfd/spatial/>, specifically the DBF file portion of a shapefile, <ftp://ftp.bom.gov.au/anon/home/adfd/spatial/IDM00003.dbf>
BOM maintains a shapefile of forecast marine zone names and their geographic locations. For ease, we'll just use the .dbf file part of the shapefile to extract AAC codes that can be used to add locations to the forecast `data.frame` that `get_coastal_forecast()` returns. The file is available from BOM's anonymous FTP server with spatial data <ftp://ftp.bom.gov.au/anon/home/adfd/spatial/>, specifically the DBF file portion of a shapefile, <ftp://ftp.bom.gov.au/anon/home/adfd/spatial/IDM00003.dbf>

``` r
utils::download.file(
Expand Down
Binary file modified inst/extdata/AAC_codes.rda
Binary file not shown.
Binary file modified inst/extdata/JSONurl_site_list.rda
Binary file not shown.
Binary file modified inst/extdata/stations_site_list.rda
Binary file not shown.
9 changes: 5 additions & 4 deletions man/bomrang.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions man/get_ag_bulletin.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions man/get_available_imagery.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 56 additions & 0 deletions man/get_coastal_forecast.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions man/get_current_weather.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit d54f9f6

Please sign in to comment.