Skip to content

Commit

Permalink
fix #219 change seaice fxn to sea_ice, painful to change fxn name
Browse files Browse the repository at this point in the history
but wanted to change user interface, so useful i think to change fxn name to make it easier
updated tests
  • Loading branch information
sckott committed Sep 9, 2019
1 parent de73acc commit a9a7289
Show file tree
Hide file tree
Showing 12 changed files with 256 additions and 105 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Suggests:
raster,
leaflet,
rgdal,
sf,
rmarkdown,
purrr,
ggmap,
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ export(noaa_stations)
export(readshpfile)
export(se_data)
export(se_files)
export(sea_ice)
export(seaice)
export(seaice_tabular)
export(seaiceeurls)
Expand Down
9 changes: 9 additions & 0 deletions R/defunct.R
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,14 @@ ncdc_leg_data <- function() {
.Defunct(msg = "This function is defunct, see ncdc_*() functions")
}

#' This function is defunct.
#' @export
#' @rdname seaice-defunct
#' @keywords internal
seaice <- function() {
.Defunct(msg = "This function is defunct, see sea_ice()")
}

#' Defunct functions in rnoaa
#'
#' \itemize{
Expand Down Expand Up @@ -196,6 +204,7 @@ ncdc_leg_data <- function() {
#' \item \code{\link{ncdc_leg_sites}}: Removed. See \code{NCDC Legacy} below
#' \item \code{\link{ncdc_leg_site_info}}: Removed. See \code{NCDC Legacy} below
#' \item \code{\link{ncdc_leg_data}}: Removed. See \code{NCDC Legacy} below
#' \item \code{\link{seaice}}: Replaced with \code{\link{sea_ice}}
#' }
#'
#' @section NCDC Legacy:
Expand Down
159 changes: 108 additions & 51 deletions R/seaice.r
Original file line number Diff line number Diff line change
@@ -1,32 +1,56 @@
#' Get sea ice data.
#'
#' @export
#' @param url A url for a NOAA sea ice ftp file
#' @param ... Further arguments passed on to readshpfile function, see
#' `readshpfile`
#' @return A data.frame
#' @details If you want to reproject the shape files, use
#' @param year (numeric) a year
#' @param month (character) a month, as character abbrevation of a month
#' @param pole (character) one of S (south) or N (north)
#' @param format (character) one of shp (default), geotiff-extent (for geotiff
#' extent data), or geotiff-conc (for geotiff concentration data)
#' @param ... Further arguments passed on to `rgdal::readshpfile()` if
#' `format="shp"` or `raster::raster()` if not
#' @return data.frame if `format="shp"`; `raster::raster()` if not
#' @details For shp files, if you want to reproject the shape files, use
#' [readshpfile()] to read in shape file, then reproject, and so on.
#' @seealso [seaice_tabular()]
#' @references See the "User Guide" pdf at https://nsidc.org/data/g02135
#' @examples \dontrun{
#' # Look at data.frame's for a series of years for Feb, South pole
#' urls <- sapply(seq(1979,1990,1), function(x) seaiceeurls(yr=x,
#' mo='Feb', pole='S'))
#' out <- lapply(urls, seaice)
#' lapply(out, head)
#' # the new way
#' library(raster)
#' library(sf)
#'
#' ## one file
#' sea_ice(year = 1990, month = "Apr", pole = "N")
#' sea_ice(year = 1990, month = "Apr", pole = "N", format = "geotiff-extent")
#' sea_ice(year = 1990, month = "Apr", pole = "N", format = "geotiff-conc")
#' ## many files
#' sea_ice(year = 1990, month = "Apr")
#' x <- sea_ice(year = 1990, month = "Apr", format = "geotiff-extent")
#' y <- sea_ice(year = 1990, month = "Apr", format = "geotiff-conc")
#' plot(x[[1]])
#' plot(y[[1]])
#'
#' # Map a single year/month/pole combo
#' urls <- seaiceeurls(mo='Apr', pole='N', yr=1990)
#' out <- seaice(urls)
#' library('ggplot2')
#' ggplot(out, aes(long, lat, group=group)) +
#' geom_polygon(fill="steelblue") +
#' theme_ice()
#' out <- sea_ice(year = 1990, month = 'Apr', pole = 'N')
#' library('sf')
#' plot(out[[1]])
#' }
seaice <- function(url, ...) {
check4pkg("rgdal")
tt <- readshpfile(url, ...)
suppressMessages(fortify(tt))
sea_ice <- function(year = NULL, month = NULL, pole = NULL, format = "shp",
...) {

assert(year, c('integer', 'numeric'))
assert(month, 'character')
assert(pole, 'character')
assert(format, 'character')
if (!format %in% c("shp", "geotiff-extent", "geotiff-conc"))
stop("'format' must be one of: 'shp', 'geotiff-extent', 'geotiff-conc'")
urls <- seaiceeurls(yr=year, mo=month, pole, format)
if (format == "shp") {
check4pkg("sf")
lapply(urls, readshpfile, ...)
} else {
check4pkg("raster")
lapply(urls, raster::raster, ...)
}
}

#' Make all urls for sea ice data
Expand All @@ -49,14 +73,47 @@ seaice <- function(url, ...) {
#'
#' # Get urls for Feb of 1980, just S pole
#' seaiceeurls(yr=1980, mo='Feb', pole='S')
#'
#' # GeoTIFF
#' seaiceeurls(yr=1980, mo='Feb', pole='S', format = "geotiff")
#' }
seaiceeurls <- function(yr = NULL, mo = NULL, pole = NULL) {
# previous years
seaiceeurls <- function(yr = NULL, mo = NULL, pole = NULL, format = "shp") {
type <- if (!grepl("geotiff", format)) NULL else strsplit(format, "-")[[1]][2]
urls <- generate_urls(format, type)
if (!is.null(pole)) {
pole <- switch(format, shp=sprintf("_%s_", pole), sprintf("%s_", pole))
}
if (!is.null(yr)) yr <- sprintf("_%s", yr)

ss <- urls
if (!is.null(yr) & is.null(mo) & is.null(pole))
ss <- grep(yr, urls, value = TRUE)
if (is.null(yr) & !is.null(mo) & is.null(pole))
ss <- grep(mo, urls, value = TRUE)
if (is.null(yr) & is.null(mo) & !is.null(pole))
ss <- grep(pole, urls, value = TRUE)
if (!is.null(yr) & !is.null(mo) & is.null(pole))
ss <- grep(yr, grep(mo, urls, value = TRUE), value = TRUE)
if (!is.null(yr) & is.null(mo) & !is.null(pole))
ss <- grep(yr, grep(pole, urls, value = TRUE), value = TRUE)
if (is.null(yr) & !is.null(mo) & !is.null(pole))
ss <- grep(pole, grep(mo, urls, value = TRUE), value = TRUE)
if (!is.null(yr) & !is.null(mo) & !is.null(pole))
ss <- grep(yr, grep(pole, grep(mo, urls, value = TRUE),
value = TRUE), value = TRUE)

return( ss )
}

generate_urls <- function(format, type) {
fun <- if (format == "shp") make_urls_shp else make_urls_geotiff
if (!is.null(type)) type <- switch(type, extent = "extent", "concentration")

yrs_prev <- seq(1979, year(today()) - 1, 1)
months_prevyr <- c(paste0(0, seq(1, 9)), c(10, 11, 12))
yrs_months <- do.call(c, lapply(yrs_prev, function(x)
paste(x, months_prevyr, sep = '')))
urls <- make_seaice_urls(yrs_months, month.abb)
urls <- fun(yrs_months, month.abb, type = type)

# this year
months_thisyr <- seq(1, as.numeric(format(Sys.Date(), "%m")))
Expand All @@ -68,38 +125,16 @@ seaiceeurls <- function(yr = NULL, mo = NULL, pole = NULL) {
yrs_months_thisyr <- paste0(format(Sys.Date(), "%Y"), months_thisyr)
eachmonth_thiyr <- month.abb[1:grep(format(Sys.Date() - months(1), "%b"),
month.abb)]
urls_thisyr <- make_seaice_urls(yrs_months_thisyr, eachmonth_thiyr)
urls_thisyr <- fun(yrs_months_thisyr, eachmonth_thiyr, type = type)
} else {
urls_thisyr <- c()
}

# all urls
allurls <- c(urls, urls_thisyr)

if (!is.null(pole)) pole <- sprintf("_%s_", pole)
if (!is.null(yr)) yr <- sprintf("_%s", yr)

ss <- allurls
if (!is.null(yr) & is.null(mo) & is.null(pole))
ss <- grep(yr, allurls, value = TRUE)
if (is.null(yr) & !is.null(mo) & is.null(pole))
ss <- grep(mo, allurls, value = TRUE)
if (is.null(yr) & is.null(mo) & !is.null(pole))
ss <- grep(pole, allurls, value = TRUE)
if (!is.null(yr) & !is.null(mo) & is.null(pole))
ss <- grep(yr, grep(mo, allurls, value = TRUE), value = TRUE)
if (!is.null(yr) & is.null(mo) & !is.null(pole))
ss <- grep(yr, grep(pole, allurls, value = TRUE), value = TRUE)
if (is.null(yr) & !is.null(mo) & !is.null(pole))
ss <- grep(pole, grep(mo, allurls, value = TRUE), value = TRUE)
if (!is.null(yr) & !is.null(mo) & !is.null(pole))
ss <- grep(yr, grep(pole, grep(mo, allurls, value = TRUE),
value = TRUE), value = TRUE)

return( ss )
c(urls, urls_thisyr)
}

make_seaice_urls <- function(yrs_months, mos) {
make_urls_shp <- function(yrs_months, mos, type = NULL) {
do.call(
"c",
lapply(c('south', 'north'), function(x) {
Expand All @@ -110,17 +145,38 @@ make_seaice_urls <- function(yrs_months, mos) {
mos,
sep = "_"
)
tmp <- sprintf(seaiceftp, x, mm)
tmp <- sprintf(ftp_url_shp, x, mm)
route <- paste('extent_', switch(x, south = "S", north = "N"),
'_', yrs_months, '_polygon_v3.0.zip',
sep = '')
file.path(tmp, route)
})
)
}
make_urls_geotiff <- function(yrs_months, mos, type = "extent") {
do.call(
"c",
lapply(c('south', 'north'), function(x) {
mm <- paste(
vapply(seq_along(mos), function(z) {
if (nchar(z) == 1) paste0(0, z) else as.character(z)
}, ""),
mos,
sep = "_"
)
tmp <- sprintf(ftp_url_geotiff, x, mm)
route <- paste(switch(x, south = "S", north = "N"),
'_', yrs_months, sprintf('_%s_v3.0.tif', type),
sep = '')
file.path(tmp, route)
})
)
}

seaiceftp <-
ftp_url_shp <-
'ftp://sidads.colorado.edu/DATASETS/NOAA/G02135/%s/monthly/shapefiles/shp_extent/%s'
ftp_url_geotiff <-
'ftp://sidads.colorado.edu/DATASETS/NOAA/G02135/%s/monthly/geotiff/%s'

#' Function to read shapefiles
#'
Expand All @@ -144,7 +200,8 @@ readshpfile <- function(x, storepath = NULL) {
dir.create(path_write, showWarnings = FALSE)
unzip(path, exdir = path_write)
my_layer <- rgdal::ogrListLayers(path.expand(path_write))
rgdal::readOGR(path.expand(path_write), layer = my_layer, verbose = FALSE)
sf::st_as_sf(rgdal::readOGR(path.expand(path_write), layer = my_layer,
verbose = FALSE, stringsAsFactors = FALSE))
}

#' ggplot2 map theme
Expand Down
10 changes: 8 additions & 2 deletions R/seaice_tabular.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,25 @@ si_tab_pat <- "ftp://sidads.colorado.edu/DATASETS/NOAA/G02135/%s/monthly/data/"

#' Sea ice tabular data
#'
#' Collects `.csv` files from NOAA, and binds them together into
#' a single data.frame. Data across years, with extent and area of ice.
#'
#' An example file, for January, North pole:
#' `ftp://sidads.colorado.edu/DATASETS/NOAA/G02135/north/monthly/data/N_01_extent_v3.0.csv`
#'
#' @export
#' @param ... Curl options passed on to [crul::verb-GET] - beware
#' that curl options are passed to each http request, for each of
#' 24 requests.
#' @return A data.frame with columns:
#'
#'
#' - year (integer)
#' - mo (integer)
#' - data.type (character)
#' - region (character)
#' - extent (numeric)
#' - area (numeric)
#'
#'
#' @details a value in any cell of -9999 indicates missing data
#' @seealso [seaice()]
#' @examples \dontrun{
Expand Down
1 change: 1 addition & 0 deletions man/rnoaa-defunct.Rd

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

61 changes: 61 additions & 0 deletions man/sea_ice.Rd

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

12 changes: 12 additions & 0 deletions man/seaice-defunct.Rd

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

Loading

0 comments on commit a9a7289

Please sign in to comment.