Skip to content

Commit

Permalink
add tests/ folder with testdata
Browse files Browse the repository at this point in the history
  • Loading branch information
Eva Marques committed Jun 5, 2024
1 parent 4b07723 commit d9a22f2
Showing 1 changed file with 72 additions and 49 deletions.
121 changes: 72 additions & 49 deletions tests/testdata/testdata_creation.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ output: html_document
Configuration

```{r}
triangle <- rbind(c(-79.19,35.6),
c(-79.19, 36.11),
c(-78.39, 36.11),
c(-78.39, 35.6)) |>
triangle <- rbind(
c(-79.19, 35.6),
c(-79.19, 36.11),
c(-78.39, 36.11),
c(-78.39, 35.6)
) |>
terra::vect("polygons", crs = "epsg:4326")
```

Expand All @@ -22,11 +24,13 @@ inv <- "../../input/weatherunderground/Raleigh_Stations_inventory.csv" |>
read.csv() |>
sf::st_as_sf(coords = c("lon", "lat"), crs = 4326, remove = FALSE)
inv$ts_utc <- as.POSIXct(inv$ts_utc,
format = "%Y-%m-%d %H:%M:%S",
tz = "UTC")
format = "%Y-%m-%d %H:%M:%S",
tz = "UTC"
)
inv$te_utc <- as.POSIXct(inv$te_utc,
format = "%Y-%m-%d %H:%M:%S",
tz = "UTC")
format = "%Y-%m-%d %H:%M:%S",
tz = "UTC"
)
samp <- sample(seq(1, nrow(inv), 1), size = 150, replace = FALSE)
inv <- inv[samp, ]
```
Expand All @@ -46,47 +50,59 @@ Weather Underground testdata
```{r}
setwd("../..")
wu <- load_wu(config$ts, config$te, config$area, config$wu_inv)
keep <- c("stationID",
"timezone",
"obsTimeUtc",
"lat",
"lon",
"humidityAvg",
"tempAvg")
keep <- c(
"stationID",
"timezone",
"obsTimeUtc",
"lat",
"lon",
"humidityAvg",
"tempAvg"
)
wu <- wu[, ..keep]
```

Create simulated dataset from raw wu

```{r}
# replace stationID
id <- data.table(stationID = unique(wu$stationID),
lon = unique(wu$lon),
lat = unique(wu$lat),
new = as.character(1:length(unique(wu$stationID))))
id <- data.table(
stationID = unique(wu$stationID),
lon = unique(wu$lon),
lat = unique(wu$lat),
new = as.character(seq_along(unique(wu$stationID)))
)
# add noise to lat and lon
id$lat <- id$lat + rnorm(nrow(id), 0, 0.005)
id$lon <- id$lon + rnorm(nrow(id), 0, 0.005)
wu_sim <- merge(wu[, c("stationID",
"timezone",
"obsTimeUtc",
"humidityAvg",
"tempAvg")],
id,
by = "stationID")
wu_sim <- merge(
wu[, c(
"stationID",
"timezone",
"obsTimeUtc",
"humidityAvg",
"tempAvg"
)],
id,
by = "stationID"
)
wu_sim$new <- NULL
lubridate::second(wu_sim$obsTimeUtc) <- sample(0:59,
nrow(wu_sim),
replace = TRUE)
nrow(wu_sim),
replace = TRUE
)
lubridate::minute(wu_sim$obsTimeUtc) <- sample(0:59,
nrow(wu_sim),
replace = TRUE)
nrow(wu_sim),
replace = TRUE
)
wu_sim$humidityAvg <- wu_sim$humidityAvg + sample(-5:5,
nrow(wu_sim),
replace = TRUE)
nrow(wu_sim),
replace = TRUE
)
wu_sim$tempAvg <- wu_sim$tempAvg + sample(-5:5,
nrow(wu_sim),
replace = TRUE)
nrow(wu_sim),
replace = TRUE
)
saveRDS(wu_sim, "wu_raw_simulated_testdata.rds")
```

Expand All @@ -98,26 +114,34 @@ PurpleAir testdata
pa_file <- "../../input/rtp/pa_20210720_20210727.csv"
pa <- load_pa(config$ts, config$te, config$area, storage_file = pa_file)
head(pa)
id <- data.table(sensor_index = unique(pa$sensor_index),
longitude = unique(pa$longitude),
latitude = unique(pa$latitude),
new = as.character(1:length(unique(pa$sensor_index))))
id <- data.table(
sensor_index = unique(pa$sensor_index),
longitude = unique(pa$longitude),
latitude = unique(pa$latitude),
new = as.character(seq_along(unique(pa$sensor_index)))
)
# add noise to latitude and longitude
id$latitude <- id$latitude + rnorm(nrow(id), 0, 0.005)
id$longitude <- id$longitude + rnorm(nrow(id), 0, 0.005)
pa_sim <- merge(pa[, c("sensor_index",
"time_stamp",
"humidity",
"temperature")],
id,
by = "sensor_index")
pa_sim <- merge(
pa[, c(
"sensor_index",
"time_stamp",
"humidity",
"temperature"
)],
id,
by = "sensor_index"
)
pa_sim$new <- NULL
pa_sim$humidity <- pa_sim$humidity + sample(-5:5,
nrow(pa_sim),
replace = TRUE)
nrow(pa_sim),
replace = TRUE
)
pa_sim$temperature <- pa_sim$temperature + sample(-5:5,
nrow(pa_sim),
replace = TRUE)
nrow(pa_sim),
replace = TRUE
)
saveRDS(pa_sim, "pa_raw_simulated_testdata.rds")
```

Expand All @@ -128,4 +152,3 @@ GHCNh testdata
ghcnh <- download_ghcnh(config$ts, config$te, config$area)
saveRDS(ghcnh, "ghcnh_testdata.rds")
```

0 comments on commit d9a22f2

Please sign in to comment.