Skip to content
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

12-dependencies-unit-tests-etc #13

Draft
wants to merge 17 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
^renv$
^renv\.lock$
^.*\.Rproj$
^\.Rproj\.user$
^LICENSE\.md$
6 changes: 2 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
Package: GSODTools
Title: R Functions to Select, Download, and Process GSOD Data
Version: 1.0.5
Version: 1.0.5.9003
Author: Florian Detsch
Maintainer: Florian Detsch <fdetsch@web.de>
Description: R functions to select, download, and process GSOD data.
License: MIT + file LICENSE
Depends:
R (>= 4.1)
Imports:
rworldmap,
gmt,
dplyr,
zoo,
Rssa,
reshape2,
@@ -26,6 +24,6 @@ Suggests:
checkmate,
tinytest
LazyData: true
RoxygenNote: 7.2.3
RoxygenNote: 7.3.2
Roxygen: list(markdown = TRUE)
Encoding: UTF-8
21 changes: 0 additions & 21 deletions LICENSE.md

This file was deleted.

2 changes: 0 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -28,12 +28,10 @@ import(utils)
importFrom(Rssa,nsigma)
importFrom(Rssa,ssa)
importFrom(TSA,harmonic)
importFrom(dplyr,arrange)
importFrom(forecast,forecast)
importFrom(gmt,geodist)
importFrom(graphics,points)
importFrom(reshape2,melt)
importFrom(rworldmap,mapGriddedData)
importFrom(sf,st_as_sf)
importFrom(zoo,as.yearmon)
importFrom(zoo,read.zoo)
13 changes: 13 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
# GSODTools 1.0.5.9003 (2024-07-19)

#### ✨ features and improvements

#### 🐛 bug fixes

#### 💬 documentation etc

#### 🍬 miscellaneous

* Eliminates {dplyr} and {rworldmap} dependencies (#12)


# GSODTools 1.0.5 (2022-06-08)

#### 🍬 miscellaneous
4 changes: 1 addition & 3 deletions R/GSODTools-package.R
Original file line number Diff line number Diff line change
@@ -3,7 +3,6 @@
#' R functions to select, download, and process GSOD data.
#'
#' @name GSODTools-package
#' @docType package
#' @title R Functions to Select, Download, and Process GSOD Data
#' @author Florian Detsch \cr
#' \cr
@@ -12,5 +11,4 @@
#' @import methods stats utils
#'
#' @keywords package
#'
NULL
"_PACKAGE"
2 changes: 1 addition & 1 deletion R/as.ki.data.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#' Convert data set to ki.data object
#'
#' @param input_filepath Needs documentation.
#' @param start.column [\code{integer}] Defaults to \code{9L}.
#' @param start.column \[`integer`\] Defaults to `9L`.
#' @param ... Currently not used.
#'
#' @importFrom reshape2 melt
20 changes: 9 additions & 11 deletions R/data.R
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@
#' A data set containing the full Global Summary of the Day (GSOD) station list.
#'
#' @seealso Integrated Surface Database Station History, May 2022. Available
#' online: <https://www.ncei.noaa.gov/pub/data/noaa/isd-history.txt> (last
#' online: <https://www.ncei.noaa.gov/pub/data/noaa/isd-history.csv> (last
#' accessed 2023-12-12).
#'
#' @format A `data.table` with 29640 rows and 11 columns:
@@ -12,7 +12,7 @@
#' * `"WBAN"`: \[`character`\] NCDC WBAN number.
#' * `"STATION NAME"`: \[`character`\] Station name.
#' * `"CTRY"`: \[`character`\] FIPS country ID, see also
#' <https://www.ncei.noaa.gov/data/global-summary-of-the-day/doc/country-list.txt>.
#' <https://www.ncei.noaa.gov/pub/data/noaa/country-list.txt>.
#' * `"STATE"`: \[`character`\] State for US stations.
#' * `"ICAO"`: \[`character`\] ICAO ID.
#' * `"LAT, LON"`: \[`numeric`\] Latitude, longitude in thousandths of decimal
@@ -32,14 +32,12 @@
#' imputed based on singular spectrum analysis (SSA).
#'
#' @format A \code{data.table} with 30684 rows and 6 columns:
#' \itemize{
#' \item{"PlotId": }{[\code{factor}] Station name (\code{"kil"}: Kilimanjaro,
#' \code{"jom"}: Nairobi)}
#' \item{"Status": }{[\code{factor}] Preprocess level (\code{"cleansed"}:
#' outliers removed, \code{"filled"}: gaps filled via SSA)}
#' \item{"Datetime": }{[\code{POSIXct}] Time in Eastern Africa Time}
#' \item{"TEMP,MIN,MAX": }{[\code{numeric}] Mean, minimum, maximum air
#' temperature in degC}
#' }
#' * `"PlotId"`: \[`factor`\] Station name (\code{"kil"}: Kilimanjaro,
#' \code{"jom"}: Nairobi);
#' * `"Status"`: \[`factor`\] Preprocess level (\code{"cleansed"}: outliers
#' removed, \code{"filled"}: gaps filled via SSA);
#' * `"Datetime"`: \[`POSIXct`\] Time in Eastern Africa Time;
#' * `"TEMP, MIN, MAX"`: \[`numeric\] Mean, minimum, maximum air temperature in
#' degC.
#'
"eastafrica"
21 changes: 12 additions & 9 deletions R/stationFromCoords.R
Original file line number Diff line number Diff line change
@@ -29,10 +29,6 @@
#' y = -3.065053,
#' width = 100)
#'
#' rworldmap::mapGriddedData(mapRegion = "africa", plotData = FALSE, borderCol = "black",
#' addLegend = FALSE)
#' points(sf::st_coordinates(gsod_shp), col = "red", pch = 20, cex = 2)
#'
#' # Alternatively
#' gsod_shp <- stationFromCoords(x = c(37.359031, -3.065053),
#' width = 100)
@@ -50,8 +46,6 @@
#' gsod_shp <- stationFromCoords(x = kibo,
#' width = 100)
#'
#' @importFrom dplyr arrange
#'
#' @export
stationFromCoords <- function(x,
y = NULL,
@@ -69,6 +63,7 @@ stationFromCoords <- function(x,
}

# Calculate distance from point of interest to supplied stations
# TODO: what happens if there are 2+ points in input
stations <- gsodstations
x.to.stations <- gmt::geodist(
Nfrom = y
@@ -78,8 +73,16 @@ stationFromCoords <- function(x,
# Add calculated distances to stations
stations$DIST <- round(x.to.stations, ...)

# Identify and return GSOD stations that lie within the given buffer width
stations <- stations |> subset(DIST <= width) |> dplyr::arrange(DIST) |> gsodDf2Sp()
# Find stations that lie within the given buffer width and convert to spatial
stations = stations |>
subset(
DIST <= width
) |>
gsodDf2Sp()

return(stations)
# Sort by distance and return
stations[
order(stations$DIST)
,
]
}
6 changes: 0 additions & 6 deletions R/stationFromExtent.R
Original file line number Diff line number Diff line change
@@ -23,12 +23,6 @@
#' kili <- sf::st_bbox(c(xmin = 37, xmax = 37.72, ymin = -3.4, ymax = -2.84))
#' gsod_shp_kili <- stationFromExtent(bb = kili)
#'
#' rworldmap::mapGriddedData(mapRegion = "africa", plotData = FALSE, borderCol = "black",
#' addLegend = FALSE)
#' points(sf::st_coordinates(gsod_shp_kili), col = "red")
#'
#' @importFrom rworldmap mapGriddedData
#'
#' @export
stationFromExtent <- function(bb,
...) {
18 changes: 6 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -73,20 +73,18 @@ numerics. For instance, let’s search for GSOD stations in a circle of
500 km around Kibo summit, Mt. Kilimanjaro, Tanzania. The referring
coordinates are `c(37.359031, -3.065053)`.

library(mapview)

shp_kibo <- stationFromCoords(x = 37.359031, y = -3.065053, width = 500)
# or: stationFromCoords(x = c(37.359031, -3.065053), width = 500)
# or: stationFromCoords(x = SpatialPoints(data.frame(x = 37.359031,
# y = -3.065053),
# proj4string = CRS("+init=epsg:4326")),
# width = 500)

rworldmap::mapGriddedData(
mapRegion = "africa"
, plotData = FALSE
, borderCol = "black"
, addLegend = FALSE
mapview(
shp_kibo
)
points(sf::st_coordinates(shp_kibo), col = "red", pch = 20, cex = 2)

![](figure/stationFromCoords-1.png)

@@ -115,13 +113,9 @@ automatically disabled.
)
shp_kili_south <- stationFromExtent(bb = bbox_kibo_south)

rworldmap::mapGriddedData(
mapRegion = "africa"
, plotData = FALSE
, borderCol = "black"
, addLegend = FALSE
mapview(
shp_kili_south
)
points(sf::st_coordinates(shp_kili_south), col = "red", pch = 20, cex = 2)

![](figure/stationFromExtent-1.png)

20 changes: 8 additions & 12 deletions README.rmd
Original file line number Diff line number Diff line change
@@ -74,20 +74,19 @@ in a circle of 500 km around Kibo summit, Mt. Kilimanjaro, Tanzania. The referri
coordinates are `c(37.359031, -3.065053)`.

```{r stationFromCoords}
library(mapview)

shp_kibo <- stationFromCoords(x = 37.359031, y = -3.065053, width = 500)
# or: stationFromCoords(x = c(37.359031, -3.065053), width = 500)
# or: stationFromCoords(x = SpatialPoints(data.frame(x = 37.359031,
# y = -3.065053),
# proj4string = CRS("+init=epsg:4326")),
# width = 500)

rworldmap::mapGriddedData(
mapRegion = "africa"
, plotData = FALSE
, borderCol = "black"
, addLegend = FALSE
mapview(
shp_kibo
)
points(sf::st_coordinates(shp_kibo), col = "red", pch = 20, cex = 2)

```

`stationFromExtent`, just like `stationFromCoords`, allows station selection
@@ -111,13 +110,10 @@ bbox_kibo_south <- sf::st_bbox(
)
shp_kili_south <- stationFromExtent(bb = bbox_kibo_south)

rworldmap::mapGriddedData(
mapRegion = "africa"
, plotData = FALSE
, borderCol = "black"
, addLegend = FALSE
mapview(
shp_kili_south
)
points(sf::st_coordinates(shp_kili_south), col = "red", pch = 20, cex = 2)

```

The third and, at the moment, final possibility to select a GSOD station is to
2 changes: 1 addition & 1 deletion TODO
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@ General
- further details: https://roxygen2.r-lib.org/articles/markdown.html
* add ci/cd
* gsod --> isd
* {dplyr}, {data.table} (built-in data object) or base
* {data.table} (built-in data object) or base
* re-enable (some) `\dontrun` examples
* handle records in `gsodstations` with lon = lat = 0.0
* find solution for `gsodstations` duplication (internal vs. external)
Binary file modified figure/stationFromCoords-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified figure/stationFromExtent-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 10 additions & 8 deletions inst/tinytest/test_gsodPlot.R
Original file line number Diff line number Diff line change
@@ -22,15 +22,17 @@ filled_data = lst[
)
]

expect_inherits(
gsodPlot(
cleansed_data
, filled_data
, stations = c("NAIROBI JKIA", "KILIMANJARO INTL")
, type = "trends"
for (type in c("original", "trends", "both")) {
expect_inherits(
gsodPlot(
cleansed_data
, filled_data
, stations = c("NAIROBI JKIA", "KILIMANJARO INTL")
, type = type
)
, class = "ggplot"
)
, class = "ggplot"
)
}

## early exit: non-matching type
expect_error(
5 changes: 5 additions & 0 deletions inst/tinytest/test_stationFrom.R
Original file line number Diff line number Diff line change
@@ -50,6 +50,11 @@ expect_true(
, info = "in case of coords, 'DIST' column is appended to output"
)

expect_false(
is.unsorted(gsod_shp$DIST)
, info = "appended 'DIST' column is sorted"
)


## `stationFromExtent()` ====

11 changes: 11 additions & 0 deletions inst/tinytest/test_utils.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
out = toCelsius(c(32, 212)) # freezing point, boiling point

expect_identical(
out
, target = c(
0.
, 100.
)
, info = "output is of the same length as input and content is correct"
)

36 changes: 36 additions & 0 deletions inst/tinytest/test_vectorHarmonics.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
dat = subset(
eastafrica
, PlotId == "kil" &
Datetime < "1984-01-01"
)

agg = aggregate(
dat["TEMP"]
, by = list(
YEARMO = format(
dat$Datetime
, "%Y-%m"
)
)
, FUN = mean
, na.rm = TRUE
)

out = vectorHarmonics(
agg$TEMP
, st = c(1980, 1)
, nd = c(1983, 12)
)

expect_inherits(
out
, class = "numeric"
)

expect_identical(
length(out)
, target = formals(
vectorHarmonics
)$frq
, info = "output is of the same length as input frequency"
)
1 change: 1 addition & 0 deletions man/GSODTools-package.Rd

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

Loading