diff --git a/DESCRIPTION b/DESCRIPTION index 1319bd3..f688446 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: senamhiR Type: Package Title: A Collection of Functions to Obtain Peruvian Climate Data -Version: 0.5.0 +Version: 0.5.1 Date: 2017-10-08 Authors@R: c(person(given = c("Conor", "I."), family = "Anderson", role = c("aut","cre"), email = "conor.anderson@utoronto.ca"), diff --git a/NAMESPACE b/NAMESPACE index af2d6ba..722164f 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -12,10 +12,14 @@ importFrom(DBI,dbGetQuery) importFrom(DBI,dbListTables) importFrom(DBI,dbReadTable) importFrom(RMySQL,MySQL) +importFrom(dplyr,arrange) importFrom(dplyr,filter) +importFrom(dplyr,mutate) +importFrom(dplyr,rowwise) importFrom(dplyr,select) importFrom(geosphere,distGeo) importFrom(leaflet,addAwesomeMarkers) +importFrom(leaflet,addCircleMarkers) importFrom(leaflet,addTiles) importFrom(leaflet,awesomeIcons) importFrom(leaflet,leaflet) diff --git a/R/map_stations.R b/R/map_stations.R index df0ca20..d00255b 100644 --- a/R/map_stations.R +++ b/R/map_stations.R @@ -5,7 +5,8 @@ ##' @param station character; one or more station id numbers to show on the map. ##' @param zoom numeric; the level to zoom the map to. ##' -##' @importFrom leaflet addAwesomeMarkers addTiles awesomeIcons leaflet setView +##' @importFrom dplyr filter +##' @importFrom leaflet addAwesomeMarkers addCircleMarkers addTiles awesomeIcons leaflet setView ##' @importFrom magrittr %>% ##' ##' @export @@ -22,31 +23,24 @@ map_stations <- function(station, zoom) { - if (inherits(station, "data.frame")) { - station <- station$StationID + if (!inherits(station, "data.frame")) { + if (any(nchar(station) < 6)) { + station[nchar(station) < 6] <- suppressWarnings( + try(sprintf("%06d", as.numeric(station[nchar(station) < 6])), + silent = TRUE)) + } + + if (inherits(station, "try-error") || !station %in% catalogue$StationID) { + stop("One or more requested stations invalid.") + } + + station <- filter(catalogue, StationID %in% station) } - if (any(nchar(station) < 6)) { - station[nchar(station) < 6] <- suppressWarnings( - try(sprintf("%06d", as.numeric(station[nchar(station) < 6])), - silent = TRUE)) - } - - if (inherits(station, "try-error") || !station %in% catalogue$StationID) { - stop("One or more requested stations invalid.") - } - - poi <- NULL - - for (i in station) { - poi <- c(poi, which(catalogue$StationID == i)) - } - poi <- catalogue[poi,] - - hilat <- ceiling(max(poi$Latitude)) - lolat <- floor(min(poi$Latitude)) - hilon <- ceiling(max(poi$Longitude)) - lolon <- floor(min(poi$Longitude)) + hilat <- ceiling(max(station$Latitude)) + lolat <- floor(min(station$Latitude)) + hilon <- ceiling(max(station$Longitude)) + lolon <- floor(min(station$Longitude)) lats <- (hilat + lolat)/2 lons <- (hilon + lolon)/2 if (missing(zoom)) { @@ -83,14 +77,22 @@ map_stations <- function(station, zoom) { } icons <- awesomeIcons( - icon = defIcons(poi), + icon = defIcons(station), iconColor = 'black', library = 'ion', - markerColor = defColours(poi) + markerColor = defColours(station) ) - leaflet(poi) %>% addTiles() %>% + map <- leaflet(station) %>% addTiles() %>% setView(lng = lons, lat = lats, zoom = zoom) %>% addAwesomeMarkers(~Longitude, ~Latitude, icon = icons, - label = paste0(poi$StationID, " - ", poi$Station, " (", poi$Configuration, ")")) + label = paste0(station$StationID, " - ", station$Station, " (", station$Configuration, ")")) + + # Add a target if it exists + target <- c(attr(station, "target_lon"), attr(station, "target_lat")) + if (!is.null(target)) { + map <- map %>% addCircleMarkers(lng = target[1], lat = target[2], color = "red", label = "target") + } + + map } diff --git a/R/station_search.R b/R/station_search.R index 7a7e436..ee17896 100644 --- a/R/station_search.R +++ b/R/station_search.R @@ -6,7 +6,7 @@ ##' @param ignore.case logical; by default the search for station names is not case-sensitive. ##' @param glob logical; whether to allow regular expressions in the \code{name}. See \code{\link{glob2rx}}. ##' @param region character; optional character string to filter results by region. -##' @param baseline vector; optional vector with a start and end year for a desired baseline. +##' @param period numeric; optional, either a range of years or the total number of years of data that must be available. ##' @param config character; the configuration of the station ((m)eteorological or (h)ydrological) ##' @param target numeric; optional station ID of a target station, or a vector of length 2 containing latitude and longitude (in that order). ##' @param dist numeric; vector with a range of distance from the target in km. Only used if a target is specified. (default is 0:100) @@ -15,6 +15,7 @@ ##' ##' @return A data frame containing the details of matching stations. ##' +##' @importFrom dplyr arrange filter mutate rowwise ##' @importFrom geosphere distGeo ##' @importFrom utils glob2rx ##' @@ -30,14 +31,14 @@ ##' station_search(name = "San*", glob = TRUE) ##' ##' # Find stations with data available from 1971 to 2000. -##' station_search(baseline = 1971:2000) +##' station_search(period = 1971:2000) ##' ##' # Find all stations between 0 and 100 km from Station '000401' ##' station_search(target = '000401', dist = 0:100) ##' station_search <- function(name = NULL, ignore.case = TRUE, glob = FALSE, region = NULL, - baseline = NULL, config = NULL, target = NULL, dist = 0:100, sort = TRUE, ...) { + period = NULL, config = NULL, target = NULL, dist = 0:100, sort = TRUE, ...) { if (!is.null(target) && length(target) == 1L && nchar(target) < 6) { target <- suppressWarnings(try(sprintf("%06d", as.numeric(target)), silent = TRUE)) @@ -46,65 +47,56 @@ station_search <- function(name = NULL, ignore.case = TRUE, glob = FALSE, region } } + filt <- catalogue + # If `name` is not NULL, filter by name if (!is.null(name)) { if (glob) name <- glob2rx(name) - index <- grep(name, catalogue$Station, ignore.case = ignore.case, ...) - } else { - index <- 1:nrow(catalogue) - } + filt <- filter(filt, grepl(name, Station, ignore.case = ignore.case, ...)) + } # If `region` is not NULL, filter by name if (!is.null(region)) { - index <- index[which(catalogue$Region == toupper(region))] - if (length(index) == 0) { + filt <- filter(filt, Region == toupper(region)) + if (nrow(filt) == 0) { stop("No data found for that region. Did you spell it correctly?") } } - # If `config` is not NULL, filter by name + # If `config` is not NULL, filter by config if (!is.null(config)) { - index <- index[grep(config, catalogue$Configuration[index], ignore.case = ignore.case, - ...)] - if (length(index) == 0) { + filt <- filter(filt, grepl(config, Configuration, ignore.case = ignore.case, ...)) + if (nrow(filt) == 0) { stop("No data found for that config. Did you pass \"m\" or \"h\"?") } } - # Make a table with the info we want - df <- catalogue[index, ] - - # If `baseline` is not NULL, filter by available data - if (!is.null(baseline)) { - index = NULL - # Identify all stations outside of our baseline - for (i in 1:nrow(df)) { - if (is.na(df$`Data Start`[i]) | df$`Data Start`[i] > min(baseline)) - index <- c(index, i) else if (is.na(df$`Data End`[i]) | df$`Data End`[i] < max(baseline)) - index <- c(index, i) + # If `period` is not NULL, filter by available data + if (!is.null(period)) { + if (length(period) == 1) { + filt <- filter(filt, `Period (Yr)` >= period) + } else { + filt <- filter(filt, `Data Start` <= min(period) & `Data End` >= max(period)) + } + if (nrow(filt) == 0) { + stop("No station was found for the specified period.") } - # Delete those stations - if (!is.null(index)) - df <- df[-index, ] } - + # If `target` is not NULL, filter by distance to target if (!is.null(target)) { if (length(target) == 1L) { - p1 <- c(df$Longitude[grep(paste0("\\b", as.character(target), "\\b"), - df$StationID)], df$Latitude[grep(paste0("\\b", as.character(target), - "\\b"), df$StationID)]) + p1 <- filt %>% filter(StationID == target) %>% select(Longitude, Latitude) %>% unlist } else if (length(target) == 2L) { p1 <- c(target[2], target[1]) } else stop("error: check target format") - df$Dist <- rep(NA, nrow(df)) - for (j in 1:nrow(df)) { - df$Dist[j] <- (distGeo(p1, c(df$Longitude[j], df$Latitude[j]))/1000) - } - df <- df[(!is.na(df$Dist) & (df$Dist >= min(dist)) & (df$Dist <= max(dist))), - ] - if (sort == TRUE) - df <- df[order(df$Dist), ] + filt <- rowwise(filt) %>% + mutate(Dist = distGeo(p1, c(Longitude, Latitude))/1000) %>% + filter(Dist >= min(dist) & Dist <= max(dist)) + if (sort == TRUE) filt <- arrange(filt, Dist) + attr(filt, "target_lon") <- p1[1] + attr(filt, "target_lat") <- p1[2] } - df + + filt } diff --git a/README.Rmd b/README.Rmd index 2e84c43..ab45b64 100644 --- a/README.Rmd +++ b/README.Rmd @@ -50,15 +50,15 @@ Note that you can also use wildcards as supported by the `glob2rx()` from the `u station_search("San*", glob = TRUE) ``` -You can filter your search results by region, by station type, by a given baseline period, and by proximity to another station or a vector of coordinates. You can use any combination of these four filters in your search. The function is fully documented, so take a look at `?station_search`. Let's see some examples. +You can filter your search results by region, by station type, by a given period, and by proximity to another station or a vector of coordinates. You can use any combination of these four filters in your search. The function is fully documented, so take a look at `?station_search`. Let's see some examples. #### Find all stations in the San Martín Region ```{r} station_search(region = "SAN MARTIN") ``` -#### Find stations named "Santa", with data available between 1971 to 2000 +#### Find stations named "Santa", with data available between 1971 to 2000 ```{r} -station_search("Santa", baseline = 1971:2000) +station_search("Santa", period = 1971:2000) ``` #### Find all stations between 0 and 100 km from Station No. 000401 ```{r} diff --git a/README.md b/README.md index fac2761..99291f7 100644 --- a/README.md +++ b/README.md @@ -35,22 +35,22 @@ station_search("Santa") ``` ``` -## # A tibble: 42 x 14 +## # A tibble: 42 x 13 ## Station StationID Type Configuration `Data Start` `Data End` -## -## 1 SANTA MARIA DE N… 000256 CON M 1951 2017 -## 2 SANTA 000433 CON M 1964 1986 -## 3 SANTA RITA 000829 CON M 1977 1992 -## 4 SANTA ELENA 000834 CON M 1963 1973 -## 5 SANTA ISABEL DE … 158201 CON M 1964 1982 -## 6 SANTA CRUZ DE HO… 113248 SUT M 2015 2016 -## 7 SANTA CRUZ 000351 CON M 1963 2018 -## 8 SANTA CATALINA D… 153200 CON M 1963 1983 -## 9 HACIENDA SANTA I… 000766 CON M 1954 1955 -## 10 SANTAROSA LLIHUA 151505 CON M 1980 1998 -## # ... with 32 more rows, and 8 more variables: `Period (Yr)` , +## +## 1 SANTA MARIA DE N… 000256 CON M 1951 2017 +## 2 SANTA 000433 CON M 1964 1986 +## 3 SANTA RITA 000829 CON M 1977 1992 +## 4 SANTA ELENA 000834 CON M 1963 1973 +## 5 SANTA ISABEL DE … 158201 CON M 1964 1982 +## 6 SANTA CRUZ DE HO… 113248 SUT M 2015 2016 +## 7 SANTA CRUZ 000351 CON M 1963 2018 +## 8 SANTA CATALINA D… 153200 CON M 1963 1983 +## 9 HACIENDA SANTA I… 000766 CON M 1954 1955 +## 10 SANTAROSA LLIHUA 151505 CON M 1980 1998 +## # ... with 32 more rows, and 7 more variables: `Period (Yr)` , ## # `Station Status` , Latitude , Longitude , Region , -## # Province , District , Downloaded +## # Province , District ``` Note that the `tibble` object (a special sort of `data.frame`) won't print more than the first 10 rows by default. To see all of the results, you can wrap the command in `View()` so that it becomes `View(find_station("Santa"))`. @@ -63,25 +63,25 @@ station_search("San*", glob = TRUE) ``` ``` -## # A tibble: 135 x 14 +## # A tibble: 135 x 13 ## Station StationID Type Configuration `Data Start` `Data End` -## -## 1 SANTA MARIA DE N… 000256 CON M 1951 2017 -## 2 SAN RAFAEL 152222 CON M 1965 1966 -## 3 SAN LORENZO # 5 000430 CON M 1966 1972 -## 4 SAN JACINTO DE N… 000424 CON M 1956 1968 -## 5 SAN JACINTO 201901 CON H 1947 1990 -## 6 SAN DIEGO 000420 CON M 1960 1960 -## 7 SANTIAGO ANTUNEZ… 000426 CON M 1998 2018 -## 8 SANTA 000433 CON M 1964 1986 -## 9 SAN PEDRO 211404 CON H 2009 2018 -## 10 SANTA RITA 000829 CON M 1977 1992 -## # ... with 125 more rows, and 8 more variables: `Period (Yr)` , +## +## 1 SANTA MARIA DE N… 000256 CON M 1951 2017 +## 2 SAN RAFAEL 152222 CON M 1965 1966 +## 3 SAN LORENZO # 5 000430 CON M 1966 1972 +## 4 SAN JACINTO DE N… 000424 CON M 1956 1968 +## 5 SAN JACINTO 201901 CON H 1947 1990 +## 6 SAN DIEGO 000420 CON M 1960 1960 +## 7 SANTIAGO ANTUNEZ… 000426 CON M 1998 2018 +## 8 SANTA 000433 CON M 1964 1986 +## 9 SAN PEDRO 211404 CON H 2009 2018 +## 10 SANTA RITA 000829 CON M 1977 1992 +## # ... with 125 more rows, and 7 more variables: `Period (Yr)` , ## # `Station Status` , Latitude , Longitude , Region , -## # Province , District , Downloaded +## # Province , District ``` -You can filter your search results by region, by station type, by a given baseline period, and by proximity to another station or a vector of coordinates. You can use any combination of these four filters in your search. The function is fully documented, so take a look at `?station_search`. Let's see some examples. +You can filter your search results by region, by station type, by a given period, and by proximity to another station or a vector of coordinates. You can use any combination of these four filters in your search. The function is fully documented, so take a look at `?station_search`. Let's see some examples. #### Find all stations in the San Martín Region @@ -90,46 +90,46 @@ station_search(region = "SAN MARTIN") ``` ``` -## # A tibble: 72 x 14 +## # A tibble: 72 x 13 ## Station StationID Type Configuration `Data Start` `Data End` -## -## 1 MOYOBAMBA 000378 CON M 1946 2017 -## 2 NARANJILLO 000219 CON M 1975 2017 -## 3 NAVARRO 000386 CON M 1964 2017 -## 4 NARANJILLO 4724851A SUT M1 2000 2018 -## 5 EL PORVENIR 4723013A SUT M1 2001 2018 -## 6 NUEVO LIMA 153312 CON M 1963 2017 -## 7 SHEPTE 153301 CON M 1963 1985 -## 8 TINGO DE PONAZA 153318 CON M 1963 2005 -## 9 TINGO DE PONAZA 000297 CON M 1998 2017 -## 10 PUEBLO LIBRE 152228 CON M 1996 1998 -## # ... with 62 more rows, and 8 more variables: `Period (Yr)` , +## +## 1 MOYOBAMBA 000378 CON M 1946 2017 +## 2 NARANJILLO 000219 CON M 1975 2017 +## 3 NAVARRO 000386 CON M 1964 2017 +## 4 NARANJILLO 4724851A SUT M1 2000 2018 +## 5 EL PORVENIR 4723013A SUT M1 2001 2018 +## 6 NUEVO LIMA 153312 CON M 1963 2017 +## 7 SHEPTE 153301 CON M 1963 1985 +## 8 TINGO DE PONAZA 153318 CON M 1963 2005 +## 9 TINGO DE PONAZA 000297 CON M 1998 2017 +## 10 PUEBLO LIBRE 152228 CON M 1996 1998 +## # ... with 62 more rows, and 7 more variables: `Period (Yr)` , ## # `Station Status` , Latitude , Longitude , Region , -## # Province , District , Downloaded +## # Province , District ``` -#### Find stations named "Santa", with data available between 1971 to 2000 +#### Find stations named "Santa", with data available between 1971 to 2000 ```r -station_search("Santa", baseline = 1971:2000) +station_search("Santa", period = 1971:2000) ``` ``` -## # A tibble: 10 x 14 +## # A tibble: 10 x 13 ## Station StationID Type Configuration `Data Start` `Data End` -## -## 1 SANTA MARIA DE N… 000256 CON M 1951 2017 -## 2 SANTA CRUZ 000351 CON M 1963 2018 -## 3 SANTA EULALIA 155213 CON M 1963 2018 -## 4 SANTA CRUZ 155202 CON M 1963 2018 -## 5 SANTA ROSA 000536 CON M 1967 2006 -## 6 SANTA MARIA DE N… 152409 CON M 1963 2018 -## 7 SANTA RITA DE CA… 152401 CON M 1963 2018 -## 8 SANTA CLOTILDE 000177 CON M 1963 2014 -## 9 SANTA CRUZ 152303 CON M 1963 2008 -## 10 SANTA ROSA 000823 CON M 1966 2017 -## # ... with 8 more variables: `Period (Yr)` , `Station Status` , +## +## 1 SANTA MARIA DE N… 000256 CON M 1951 2017 +## 2 SANTA CRUZ 000351 CON M 1963 2018 +## 3 SANTA EULALIA 155213 CON M 1963 2018 +## 4 SANTA CRUZ 155202 CON M 1963 2018 +## 5 SANTA ROSA 000536 CON M 1967 2006 +## 6 SANTA MARIA DE N… 152409 CON M 1963 2018 +## 7 SANTA RITA DE CA… 152401 CON M 1963 2018 +## 8 SANTA CLOTILDE 000177 CON M 1963 2014 +## 9 SANTA CRUZ 152303 CON M 1963 2008 +## 10 SANTA ROSA 000823 CON M 1966 2017 +## # ... with 7 more variables: `Period (Yr)` , `Station Status` , ## # Latitude , Longitude , Region , Province , -## # District , Downloaded +## # District ``` #### Find all stations between 0 and 100 km from Station No. 000401 @@ -138,22 +138,22 @@ station_search(target = "000401", dist = 0:100) ``` ``` -## # A tibble: 57 x 15 +## # A tibble: 57 x 14 ## Station StationID Type Configuration `Data Start` `Data End` -## -## 1 TARAPOTO 000401 CON M 1998 2017 -## 2 CUNUMBUQUE 153311 CON M 1963 2017 -## 3 CUMBAZA 221801 CON H 1968 2017 -## 4 LAMAS 000383 CON M 1963 2017 -## 5 SAN ANTONIO 153314 CON M 1963 2017 -## 6 SHANAO 221802 CON H 1965 2017 -## 7 SHANAO 153328 CON M 2002 2017 -## 8 SHANAO 210006 SUT H 2016 2018 -## 9 TABALOSOS 000322 CON M 1963 2017 -## 10 EL PORVENIR 000310 CON M 1964 2017 -## # ... with 47 more rows, and 9 more variables: `Period (Yr)` , +## +## 1 TARAPOTO 000401 CON M 1998 2017 +## 2 CUNUMBUQUE 153311 CON M 1963 2017 +## 3 CUMBAZA 221801 CON H 1968 2017 +## 4 LAMAS 000383 CON M 1963 2017 +## 5 SAN ANTONIO 153314 CON M 1963 2017 +## 6 SHANAO 221802 CON H 1965 2017 +## 7 SHANAO 153328 CON M 2002 2017 +## 8 SHANAO 210006 SUT H 2016 2018 +## 9 TABALOSOS 000322 CON M 1963 2017 +## 10 EL PORVENIR 000310 CON M 1964 2017 +## # ... with 47 more rows, and 8 more variables: `Period (Yr)` , ## # `Station Status` , Latitude , Longitude , Region , -## # Province , District , Downloaded , Dist +## # Province , District , Dist ``` #### Find all stations that are within 50 km of Machu Picchu @@ -162,31 +162,31 @@ station_search(target = c(-13.163333, -72.545556), dist = 0:50) ``` ``` -## # A tibble: 19 x 15 +## # A tibble: 19 x 14 ## Station StationID Type Configuration `Data Start` `Data End` -## -## 1 MACHU PICCHU 000679 CON M 1964 2017 -## 2 HUYRO 000678 CON M 1964 1981 -## 3 CHILCA 472A9204 SUT H 2015 2018 -## 4 ECHARATE 000716 CON M 1981 1982 -## 5 MARANURA 000676 CON M 1970 1978 -## 6 OLLANTAYTAMBO 47295014 SUT M 2011 2013 -## 7 QUILLABAMBA 4729B3E6 SUT M1 2001 2018 -## 8 QUILLABAMBA 000606 CON M 1964 2018 -## 9 OCOBAMBA 000681 CON M 1964 1983 -## 10 MOLLEPATA 000680 CON M 1963 1978 -## 11 CUNYAC 156224 CON M 2002 2018 -## 12 ECHARATE 156300 CON M 1963 1981 -## 13 PUENTE CUNYAC 230503 CON H 1995 2018 -## 14 ZURITE 000682 CON M 1964 1983 -## 15 CURAHUASI 000677 CON M 1963 2017 -## 16 URUBAMBA 113131 DAV M 2006 2008 -## 17 URUBAMBA 000683 CON M 1963 2017 -## 18 ANTA ANCACHURO 000684 CON M 1964 2017 -## 19 HUACHIBAMBA 156303 CON M 1963 1978 -## # ... with 9 more variables: `Period (Yr)` , `Station Status` , +## +## 1 MACHU PICCHU 000679 CON M 1964 2017 +## 2 HUYRO 000678 CON M 1964 1981 +## 3 CHILCA 472A9204 SUT H 2015 2018 +## 4 ECHARATE 000716 CON M 1981 1982 +## 5 MARANURA 000676 CON M 1970 1978 +## 6 OLLANTAYTAMBO 47295014 SUT M 2011 2013 +## 7 QUILLABAMBA 4729B3E6 SUT M1 2001 2018 +## 8 QUILLABAMBA 000606 CON M 1964 2018 +## 9 OCOBAMBA 000681 CON M 1964 1983 +## 10 MOLLEPATA 000680 CON M 1963 1978 +## 11 CUNYAC 156224 CON M 2002 2018 +## 12 ECHARATE 156300 CON M 1963 1981 +## 13 PUENTE CUNYAC 230503 CON H 1995 2018 +## 14 ZURITE 000682 CON M 1964 1983 +## 15 CURAHUASI 000677 CON M 1963 2017 +## 16 URUBAMBA 113131 DAV M 2006 2008 +## 17 URUBAMBA 000683 CON M 1963 2017 +## 18 ANTA ANCACHURO 000684 CON M 1964 2017 +## 19 HUACHIBAMBA 156303 CON M 1963 1978 +## # ... with 8 more variables: `Period (Yr)` , `Station Status` , ## # Latitude , Longitude , Region , Province , -## # District , Downloaded , Dist +## # District , Dist ``` ### Acquire data: `senamhiR()` diff --git a/data/catalogue.rda b/data/catalogue.rda index 8b64f16..e2b1a13 100644 Binary files a/data/catalogue.rda and b/data/catalogue.rda differ diff --git a/man/station_search.Rd b/man/station_search.Rd index d16cfbd..9d298b5 100644 --- a/man/station_search.Rd +++ b/man/station_search.Rd @@ -5,7 +5,7 @@ \title{Find Senamhi stations matching various criteria} \usage{ station_search(name = NULL, ignore.case = TRUE, glob = FALSE, - region = NULL, baseline = NULL, config = NULL, target = NULL, + region = NULL, period = NULL, config = NULL, target = NULL, dist = 0:100, sort = TRUE, ...) } \arguments{ @@ -17,7 +17,7 @@ station_search(name = NULL, ignore.case = TRUE, glob = FALSE, \item{region}{character; optional character string to filter results by region.} -\item{baseline}{vector; optional vector with a start and end year for a desired baseline.} +\item{period}{numeric; optional, either a range of years or the total number of years of data that must be available.} \item{config}{character; the configuration of the station ((m)eteorological or (h)ydrological)} @@ -43,7 +43,7 @@ station_search('Tarapoto') station_search(name = "San*", glob = TRUE) # Find stations with data available from 1971 to 2000. -station_search(baseline = 1971:2000) +station_search(period = 1971:2000) # Find all stations between 0 and 100 km from Station '000401' station_search(target = '000401', dist = 0:100) diff --git a/tests/testthat/test-map_stations.R b/tests/testthat/test-map_stations.R index a0a8a05..74bb10a 100644 --- a/tests/testthat/test-map_stations.R +++ b/tests/testthat/test-map_stations.R @@ -9,7 +9,7 @@ test_that("map_stations() can map a single station", { map <- map_stations("000401") expect_that(attr(map$x, "leafletData"), is_a("tbl_df")) expect_output(str(attr(map$x, "leafletData")), "1 obs") - expect_output(str(attr(map$x, "leafletData")), "14 variables") + expect_output(str(attr(map$x, "leafletData")), "13 variables") }) ## test a map of one station padded with zeros @@ -17,7 +17,7 @@ test_that("map_stations() can pad a StationID", { map <- map_stations(401) expect_that(attr(map$x, "leafletData"), is_a("tbl_df")) expect_output(str(attr(map$x, "leafletData")), "1 obs") - expect_output(str(attr(map$x, "leafletData")), "14 variables") + expect_output(str(attr(map$x, "leafletData")), "13 variables") }) ## test a map of one searched stations diff --git a/tests/testthat/test-station_search.R b/tests/testthat/test-station_search.R index b1fbaf6..49b2feb 100644 --- a/tests/testthat/test-station_search.R +++ b/tests/testthat/test-station_search.R @@ -8,15 +8,23 @@ test_that("station_search() can locate a station by name regex", { df <- station_search("Tara*", glob = TRUE) expect_that(df, is_a("tbl_df")) expect_output(str(df), "4 obs") - expect_output(str(df), "14 variables") + expect_output(str(df), "13 variables") }) -## test finding a station by baseline -test_that("station_search() can locate a station by baseline", { - df <- station_search(baseline = 1965:2015) +## test finding a station by period +test_that("station_search() can locate a station by period", { + df <- station_search(period = 1965:2015) expect_that(df, is_a("tbl_df")) expect_output(str(df), "363 obs") - expect_output(str(df), "14 variables") + expect_output(str(df), "13 variables") +}) + +## test finding a station by period (years) +test_that("station_search() can locate a station by period (years)", { + df <- station_search(period = 55) + expect_that(df, is_a("tbl_df")) + expect_output(str(df), "94 obs") + expect_output(str(df), "13 variables") }) ## test finding a station by region @@ -24,7 +32,7 @@ test_that("station_search() can locate a station by region", { df <- station_search(region = "TACNA") expect_that(df, is_a("tbl_df")) expect_output(str(df), "56 obs") - expect_output(str(df), "14 variables") + expect_output(str(df), "13 variables") }) ## test finding a station by distance from target @@ -32,7 +40,7 @@ test_that("station_search() can locate a station by distance from target", { df <- station_search(target = 410, dist = 0:10) expect_that(df, is_a("tbl_df")) expect_output(str(df), "2 obs") - expect_output(str(df), "15 variables") + expect_output(str(df), "14 variables") }) ## test finding a station by distance from coordinates @@ -40,7 +48,7 @@ test_that("station_search() can locate a station by distance for coordinates", { df <- station_search(target = c(-6.50, -76.47), dist = 0:10) expect_that(df, is_a("tbl_df")) expect_output(str(df), "2 obs") - expect_output(str(df), "15 variables") + expect_output(str(df), "14 variables") }) ## station_search should fail if we spell the region incorrectly