Skip to content

Commit

Permalink
Merge pull request #22 from NIEHS/eva_dev
Browse files Browse the repository at this point in the history
debug again
  • Loading branch information
eva0marques authored Jun 12, 2024
2 parents 7f745df + 77f6f3d commit 3dac007
Show file tree
Hide file tree
Showing 15 changed files with 89 additions and 143 deletions.
111 changes: 0 additions & 111 deletions .github/workflows/test-coverage.yaml

This file was deleted.

File renamed without changes.
30 changes: 24 additions & 6 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,36 @@ export(plot_ts_ref)
export(request_sensor_history)
export(summarize_hourly_temp)
export(tile_ts)
import(RCurl)
import(data.table)
import(dplyr)
import(ggplot2)
import(ggspatial)
import(httr)
import(sftime)
import(stats)
import(tidyr)
import(tidyterra)
import(utils)
importFrom(RCurl,url.exists)
importFrom(data.table,as.data.table)
importFrom(data.table,data.table)
importFrom(data.table,fread)
importFrom(dplyr,between)
importFrom(dplyr,filter)
importFrom(dplyr,group_by)
importFrom(dplyr,n)
importFrom(dplyr,rename)
importFrom(dplyr,summarise)
importFrom(dplyr,summarize)
importFrom(dplyr,ungroup)
importFrom(jsonlite,fromJSON)
importFrom(lubridate,floor_date)
importFrom(lubridate,with_tz)
importFrom(methods,new)
importFrom(sf,st_bbox)
importFrom(stats,median)
importFrom(stats,quantile)
importFrom(terra,as.polygons)
importFrom(terra,mask)
importFrom(terra,project)
importFrom(terra,vect)
importFrom(tidyr,drop_na)
importFrom(tidyterra,geom_spatraster)
importFrom(tidyterra,geom_spatraster_contour)
importFrom(tidyterra,geom_spatvector)
importFrom(tidyterra,scale_color_whitebox_c)
3 changes: 3 additions & 0 deletions R/calib_cws.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
#' @return a list with the calibrated observations, the average bias,
#' the number of reference stations used to compute the bias, and the
#' start and end time of the observations.
#' @importFrom dplyr filter group_by summarise ungroup
#' @importFrom stats median
calib_cws <- function(x, ref, max_dist = 10000) {
temp_err <- site_id <- geometry <- network <- temp <- dist_to_ref <- NULL
hour <- NULL
stopifnot(
"max_dist must be between 0 and 20000" =
max_dist > 0 & max_dist <= 20000
Expand Down
2 changes: 2 additions & 0 deletions R/class_hourly_temp.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ setClass("hourly_temp",
contains = c("data.frame")
)


setValidity("hourly_temp", function(object) {
stopifnot(
"object is not a data.frame" =
Expand Down Expand Up @@ -41,6 +42,7 @@ setValidity("hourly_temp", function(object) {
#' @param network the name of the network
#' @return a hourly_temp object
#' @importFrom methods new
#' @importFrom dplyr rename
#' @author Eva Marques
hourly_temp <- function(x,
temp = "temp",
Expand Down
3 changes: 2 additions & 1 deletion R/clean_cws.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#' @param data formatted sftime (with columns: site_id, temp, time)
#' @param na_thresh threshold of NA to remove a station (0 <= na_tresh <= 1)
#' @return cleaned data.frame
#' @import dplyr
#' @importFrom dplyr group_by n summarize
manage_na <- function(data, na_thresh = 0.1) {
stopifnot(
"threshold must be between 0 and 1" =
Expand Down Expand Up @@ -93,6 +93,7 @@ clean_cws <- function(x) {
#' @param epsg_m crs in meters (default: epsg:32119)
#' @param res resolution of the squares in meters (default: 100km)
#' @return sf of polygons
#' @importFrom terra vect project mask as.polygons
cut_area <- function(area, epsg_m = "epsg:32119", res = 100000) {
# project area with a crs in meters
area_m <- area |>
Expand Down
8 changes: 4 additions & 4 deletions R/download_ghcnh.R
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,8 @@ find_nearest_ghcnh <- function(lat, lon) {
#' @return a data.frame with the GHCN-H station raw data
#' @author Eva Marques
#' @export
#' @import RCurl
#' @import tidyr
#' @import dplyr
#' @importFrom RCurl url.exists
#' @importFrom tidyr drop_na
#' @import utils
download_ghcnh_station <- function(site_id, year) {
temperature <- NULL
Expand Down Expand Up @@ -113,6 +112,7 @@ download_ghcnh_station <- function(site_id, year) {
#' @param te end date
#' @param area a sf, sfc, SpatRaster or SpatVector object
#' @return a data.frame with the GHCN-H stations observations in the area
#' @importFrom dplyr between
download_ghcnh <- function(ts, te, area) {
stopifnot(
"ts and te should be POSIXct objects" =
Expand Down Expand Up @@ -143,7 +143,7 @@ download_ghcnh <- function(ts, te, area) {
return(NULL)
} else {
ghcnh <- format_ghcnh(ghcnh)
ghcnh <- ghcnh[which(between(ghcnh$time, ts, te)), ]
ghcnh <- ghcnh[which(dplyr::between(ghcnh$time, ts, te)), ]
return(ghcnh)
}
}
17 changes: 15 additions & 2 deletions R/download_purpleair.R
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,18 @@ request_sensor_history <- function(start_ts,
}
}


#' Load PurpleAir sensors history data
#' @param nwlat North-west latitude
#' @param selat South-east latitude
#' @param nwlng North-west longitude
#' @param selng South-east longitude
#' @param location_type Location type
#' @param start_ts Start timestamp
#' @param end_ts End timestamp
#' @param api_key API key for PurpleAir
#' @param average Average time in minutes
#' @param fields Fields to be included in the data
#' @return A data frame with sensors history data
request_sensors_history <- function(nwlat,
selat,
nwlng,
Expand Down Expand Up @@ -147,12 +158,12 @@ request_sensors_history <- function(nwlat,
}
}

# to be tested
#' Download all PurpleAir stations data in area between two dates
#' @param ts start date
#' @param te end date
#' @param area a sf, sfc, SpatRaster or SpatVector object
#' @param api_key API key for PurpleAir
#' @importFrom sf st_bbox
download_pa <- function(ts, te, area, api_key) {
bounds <- area |>
format_area() |>
Expand All @@ -177,6 +188,8 @@ download_pa <- function(ts, te, area, api_key) {
#' @param storage_file file path where PurpleAir data is stored
#' @param api_key API key for PurpleAir
#' @import utils
#' @importFrom data.table data.table
#' @importFrom dplyr between
load_pa <- function(ts, te, area, storage_file = NULL, api_key = NULL) {
if (is.null(storage_file)) {
pa <- download_pa(ts, te, area, api_key)
Expand Down
1 change: 1 addition & 0 deletions R/estimate_measurement_error.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ find_closest_ref <- function(cws, ref) {
#' with columns site_id and geometry
#' @return sf (or inherited) object with additional columns temp_ref, temp_err,
#' ref_id, dist_to_ref
#' @importFrom dplyr rename
est_temp_error <- function(cws, ref) {
# check column names
cols <- c("site_id", "temp", "geometry", "time")
Expand Down
5 changes: 5 additions & 0 deletions R/format_observations.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#' @author Eva Marques
#' @importFrom lubridate floor_date
#' @importFrom stats median
#' @importFrom dplyr group_by summarise ungroup
#' @importFrom data.table as.data.table
#' @export
summarize_hourly_temp <- function(x, time, temp, lat, lon) {
stopifnot(
Expand Down Expand Up @@ -47,6 +49,7 @@ summarize_hourly_temp <- function(x, time, temp, lat, lon) {
#' @param raw_temp_unit the initial temperature unit
#' @param raw_crs the initial coordinate reference system
#' @return sftime from hourly_temp class
#' @importFrom lubridate with_tz floor_date
#' @author Eva Marques
#' @export
format_pa <- function(raw,
Expand Down Expand Up @@ -92,6 +95,7 @@ format_pa <- function(raw,
#' @param raw_temp_unit the initial temperature unit
#' @param raw_crs the initial coordinate reference system
#' @return sftime from hourly_temp class
#' @importFrom lubridate floor_date
#' @author Eva Marques
#' @export
format_wu <- function(raw,
Expand Down Expand Up @@ -141,6 +145,7 @@ format_wu <- function(raw,
#' and columns "Year", "Month", "Day", "Hour", "temperature", "Latitude",
#' "Longitude", "temperature_Source_Code"
#' @return sftime from hourly_temp class
#' @importFrom dplyr rename
#' @author Eva Marques
#' @export
format_ghcnh <- function(raw) {
Expand Down
12 changes: 6 additions & 6 deletions R/generate_unique_site_id.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# ` @title Generate unique sensor ID
# ` @description This function generates a unique sensor "id" column based on
# ` the latitude and longitude of the sensor.
# ` @param x A data.frame, data.table, sf or sftime with columns "lat" and "lon"
# ` @return A data frame with an additional column "id"
# ` @author Eva Marques
#' @title Generate unique sensor ID
#' @description This function generates a unique sensor "id" column based on
#' the latitude and longitude of the sensor.
#' @param x A data.frame, data.table, sf or sftime with columns "lat" and "lon"
#' @return A data frame with an additional column "id"
#' @author Eva Marques
generate_site_id <- function(x) {
stopifnot(
"lat and lon columns are missing" =
Expand Down
2 changes: 1 addition & 1 deletion R/load_weatherunderground.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#' @param area Area of interest
#' @param inventory an sf object with inventory of
#' @return a data.table with the raw WU data
#' @import data.table
#' @importFrom data.table fread
#' @author Eva Marques
#' Weather Underground stations (see create_wu_inventory function)
load_wu <- function(ts, te, area, inventory) {
Expand Down
16 changes: 8 additions & 8 deletions R/plot_cws.R
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,10 @@ tile_ts <- function(data) {
#' @param data data frame or sftime with all the network, contains "network"
#' column
#' @param var continuous variable to boxplot (in general: temp)
#' @return A ggplo2 object with the boxplots
#' @return A ggplot2 object with the boxplots
#' @export
#' @import ggplot2
#' @import stats
#' @importFrom stats quantile
#' @author Eva Marques
hourly_boxplot_networks <- function(data, var) {
network <- NULL
Expand Down Expand Up @@ -223,7 +223,7 @@ hourly_boxplot_networks <- function(data, var) {

#' @import ggplot2
#' @import ggspatial
#' @import tidyterra
#' @importFrom tidyterra geom_spatvector
map_observations <- function(data,
var,
background,
Expand Down Expand Up @@ -276,7 +276,7 @@ map_observations <- function(data,

#' @import ggplot2
#' @import ggspatial
#' @import tidyterra
#' @importFrom tidyterra geom_spatraster scale_color_whitebox_c
map_observations_imp <- function(data,
var,
imp,
Expand All @@ -301,11 +301,11 @@ map_observations_imp <- function(data,
scale_fill_gradientn(colours = c("white", "grey"), na.value = NA) +
tidyterra::scale_color_whitebox_c(
palette = "bl_yl_rd",
labels = scales::label_number(suffix = paste0("ºC")),
labels = scales::label_number(suffix = paste0("C")),
n.breaks = 12,
guide = guide_legend(reverse = TRUE)
) +
guides(fill = guide_legend(title = "Imperviousness (%)")) +
guides(fill = guide_legend(title = "Imperviousness")) +
labs(
title = title,
subtitle = date
Expand Down Expand Up @@ -335,8 +335,8 @@ map_observations_imp <- function(data,

#' @import ggplot2
#' @import ggspatial
#' @import tidyterra
#' @import stats
#' @importFrom tidyterra geom_spatraster geom_spatraster_contour
#' @importFrom stats quantile
map_observations_hw <- function(data,
var,
imp,
Expand Down
Loading

0 comments on commit 3dac007

Please sign in to comment.