Skip to content

Commit

Permalink
Simplify creation of DPIRD APSIM objects
Browse files Browse the repository at this point in the history
  • Loading branch information
adamhsparks committed May 15, 2024
1 parent a553a48 commit b5d618a
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 113 deletions.
142 changes: 35 additions & 107 deletions R/get_dpird_apsim.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@



#' Get DPIRD Summary Weather Data in the APSIM Format From the Weather 2.0 API
#'
#' Automates the retrieval and conversion of summary data from the
Expand All @@ -18,6 +19,8 @@
#' @param api_key A `character` string containing your \acronym{API} key from
#' \acronym{DPIRD}, <https://www.agric.wa.gov.au/web-apis>, for the
#' \acronym{DPIRD} Weather 2.0 \acronym{API}.
#' @param filename A `character` string with your desired filename in the .met
#' object. Default \sQuote{noname.met} as with [apsimx::as_apsim_met()].
#'
#' @section Saving objects:
#' To save \dQuote{met} objects, please use [apsimx::write_apsim_met()].
Expand All @@ -29,17 +32,16 @@
#'
#' wd <- get_dpird_apsim(
#' station_code = "BI",
#' start_date = "20220401",
#' end_date = "20221101",
#' start_date = "20220101",
#' end_date = "20221231",
#' api_key = "your_api_key"
#' )
#' }
#'
#'
#' @author Adam H. Sparks, \email{adamhsparks@@gmail.com}
#'
#' @return An \CRANpkg{apsimx} object of class \sQuote{met} with attributes,
#' alternatively, a local APSIM .met file if `file` is specified.
#' @return An \CRANpkg{apsimx} object of class \sQuote{met} with attributes.
#'
#' @family DPIRD
#' @family data fetching
Expand All @@ -51,7 +53,8 @@
get_dpird_apsim <- function(station_code,
start_date,
end_date = Sys.Date(),
api_key) {
api_key,
filename = NULL) {
apsim <- get_dpird_summaries(
station_code = station_code,
start_date = start_date,
Expand All @@ -69,22 +72,11 @@ get_dpird_apsim <- function(station_code,
api_key = api_key
)

station_name <- apsim$station_name[1]
longitude <- apsim$longitude[1]
latitude <- apsim$latitude[1]
site <- apsim$station_name[1]
tav <- round(mean(colMeans(apsim[, c("air_tmax", "air_tmin")], na.rm = TRUE), na.rm = TRUE), 2)
tav <-
sprintf(
"tav = %g (oC) ! Average ambient temperature. Based on %s to %s.",
tav,
lubridate::ymd(start_date),
lubridate::ymd(end_date)
)

latitude = apsim$latitude[1]
longitude = apsim$longitude[1]
apsim[, day := NULL]
apsim[, day := lubridate::yday(apsim$date)]

apsim <-
apsim[, c(
"year",
Expand Down Expand Up @@ -112,95 +104,31 @@ get_dpird_apsim <- function(station_code,
new = c("radn", "maxt", "mint", "rain", "evap", "rh", "windspeed")
)

data.table::setDF(apsim)

units <-
c("()",
"()",
"(MJ/m2/day)",
"(oC)",
"(oC)",
"(mm)",
"(mm)",
"(%)",
"(m/s)")
comments <-
sprintf("!data from DPIRD Weather 2.0 API. retrieved: %s", Sys.time())

attr(apsim, "site") <- sprintf("%s.met", site)
attr(apsim, "latitude") <-
sprintf("latitude = %f (DECIMAL DEGREES)", latitude)
attr(apsim, "longitude") <-
sprintf("longitude = %f (DECIMAL DEGREES)", longitude)
attr(apsim, "tav") <- tav
attr(apsim, "colnames") <- names(apsim)
attr(apsim, "units") <- units
attr(apsim, "comments") <- comments

class(apsim) <- c("met", "data.frame")

apsim <-
.amp_apsim_met(met = apsim,
start_date = start_date,
end_date = end_date)

return(apsim)
}

#' Calculates Attribute Amp for an Object of Class \sQuote{met}
#'
#' This function recalculates mean monthly amplitude for an object of class
#' \sQuote{met} adapted from \CRANpkg{apsimx}.
#'
#' @param met object of class \sQuote{met}
#' @return an object of class \sQuote{met} with a recalculation of annual
#' amplitude in mean monthly temperature.
#' @author Fernando Miguez, \email{femiguez@@iastate.edu} with modifications
#' by Adam Sparks \email{adamhsparks@@gmail.com}
#' @autoglobal
#' @noRd

.amp_apsim_met <- function(met, start_date, end_date) {
if (!inherits(met, "met"))
stop("Object should be of class 'met", call. = FALSE)

## Step 1: create date
date <-
as.Date(paste(met$year, met$day, sep = "-"), format = "%Y-%j")
## Step 2: create month column
mnth <- as.numeric(format(date, "%m"))

met <-
apsimx::add_column_apsim_met(
met = met,
value = mnth,
name = "month",
units = "()"
)

mtemp <- (met$maxt + met$mint) / 2
met <-
apsimx::add_column_apsim_met(
met = met,
value = mtemp,
name = "mean.temp",
units = "(oC)"
)

met.agg <- stats::aggregate(mean.temp ~ mnth, data = met, FUN = mean)

ans <- round(max(met.agg$mean.temp) - min(met.agg$mean.temp), 2)

## Clean up
met <- apsimx::remove_column_apsim_met(met, "mean.temp")
met <- apsimx::remove_column_apsim_met(met, "month")

attr(met, "amp") <- sprintf(
"amp = %s (oC) ! Amplitude in mean monthly temperature. Based on %s to %s.",
ans,
lubridate::ymd(start_date),
lubridate::ymd(end_date)
# if no `filename` is provided,
#. remove & use the default that `as_apsim_met()` provides, 'noname.met'
if (is.null(filename)) {
filename <- "noname.met"
}

apsim <- apsimx::as_apsim_met(
filename = filename,
x = apsim,
site = site,
latitude = latitude,
longitude = longitude,
colnames = names(apsim),
units = c("()",
"()",
"(MJ/m2/day)",
"(oC)",
"(oC)",
"(mm)",
"(mm)",
"(%)",
"(m/s)"),
comments = sprintf("!data from DPIRD Weather 2.0 API. retrieved: %s",
Sys.time())
)

return(met)
return(apsim)
}
18 changes: 13 additions & 5 deletions man/get_dpird_apsim.Rd

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

2 changes: 1 addition & 1 deletion vignettes/weatherOz_for_DPIRD.Rmd.orig
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ library(weatherOz)
## Getting APSIM Ready Data

For work with APSIM, you can use `get_dpird_apsim()` to get an object in your R session that's ready for saving using [apsimx::write_apsim_met()] of DPIRD weather data.
This function only needs the `station_code`, `start_date`, `end_date` and your `api_key` to return the necessary values.
This function only needs the `station_code`, `start_date`, `end_date` and your `api_key` values to return the necessary values.

### What You Get Back

Expand Down

0 comments on commit b5d618a

Please sign in to comment.