Skip to content

Commit

Permalink
CRAN Release
Browse files Browse the repository at this point in the history
  • Loading branch information
rmendels committed Jun 4, 2019
1 parent a009ab3 commit 8b61bae
Show file tree
Hide file tree
Showing 10 changed files with 203 additions and 234 deletions.
2 changes: 1 addition & 1 deletion CRAN-RELEASE
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
This package was submitted to CRAN on 2019-05-29.
This package was submitted to CRAN on 2019-06-03.
Once it is accepted, delete this file and tag the release (commit 4c2ea05e5e).
40 changes: 24 additions & 16 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,32 +1,40 @@
Package: rerddapXtracto
Type: Package
Title: Extracts Environmental Data from ERD's ERDDAP Web Service
Version: 0.3.5.900
Title: Extracts Environmental Data from 'ERDDAP' Web Services
Version: 0.3.5
Authors@R: person("Roy", "Mendelssohn", email = "[email protected]", role = c("aut","cre"))
Description: The xtractomatic package contains three functions that access
environmental data from ERD's ERDDAP service. The rxtracto function extracts
Description: Contains three functions that access
environmental data from any 'ERDDAP' data web service. The rxtracto() function extracts
data along a trajectory for a given "radius" around the point. The
rxtracto_3D function extracts data in a box. The rxtractogon function
extracts data in a polygon. There are also two helper functions to obtain
information about available data.
These functions differ from those in the xtractomatic package in that they
use the `rerddap` package to access gridded data on any ERDDAP server, but
they require the user to provide initial information about the data
to be extracted.
rxtracto_3D() function extracts data in a box. The rxtractogon() function
extracts data in a polygon. All of those three function use the 'rerddap' package
to extract the data, and should work with any 'ERDDAP' server.
There are also two functions, plotBBox() and plotTrack() that use the 'plotdap'
package to simplify the creation of maps of the data.
URL: https://github.com/rmendels/rerddapXtracto
BugReports: https://github.com/rmendels/rerddapXtracto/issues
Depends:
R(>= 3.5.0)
License: CC0
LazyData: TRUE
Imports:
abind,
dplyr,
ggplot2,
httr,
methods,
ncdf4,
parsedate,
rerddap,
plotdap,
readr,
rerddap (>= 0.6.0),
sp,
stats
stats,
Suggests:
ggplot2,
gganimate,
knitr,
mapdata,
RoxygenNote: 6.0.1
rmarkdown
RoxygenNote: 6.1.1
Encoding: UTF-8
LazyData: TRUE
VignetteBuilder: knitr
67 changes: 26 additions & 41 deletions R/rxtracto.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#' Extract environmental data along a trajectory from an ERDDAP server using rerddap.
#' Extract environmental data along a trajectory from an 'ERDDAP' server using 'rerddap'.
#'
#' \code{rxtracto} uses the R program rerddap to extract environmental
#' data from an ERDDAP server along a (x,y,z, time) trajectory.
#' \code{rxtracto} uses the R program 'rerddap' to extract environmental
#' data from an 'ERDDAP' server along a (x,y,z, time) trajectory.
#' @export
#' @param dataInfo - the return from an rerddap "info" call to an ERDDAP server
#' @param dataInfo - the return from an 'rerddap::info' call to an 'ERDDAP' server
#' @param parameter - character string containing the name of the parameter to extract
#' @param xcoord - a real array with the x-coordinates of the trajectory (if longitude in #' decimal degrees East, either 0-360 or -180 to 180)
#' @param ycoord - a real array with the y-coordinate of the trajectory (if latitude in
Expand All @@ -14,11 +14,10 @@
#' @param xlen - real array defining the longitude box around the given point (xlen/2 around the point)
#' @param ylen - real array defining the latitude box around the given point (ylen/2 around the point)
#' @param zlen - real array defining the depth or altitude box around the given point (zlen/2 around the point)
#' @param xName - character string with name of the xcoord in the ERDDAP dataset (default "longitude")
#' @param yName - character string with name of the ycoord in the ERDDAP dataset (default "latitude")
#' @param zName - character string with name of the zcoord in the ERDDAP dataset (default "altitude")
#' @param tName - character string with name of the tcoord in the ERDDAP dataset (default "time")
#' @param urlbase - base URL of the ERDDAP server being accessed - default "http://upwell.pfeg.noaa.gov/erddap"
#' @param xName - character string with name of the xcoord in the 'ERDDAP' dataset (default "longitude")
#' @param yName - character string with name of the ycoord in the 'ERDDAP' dataset (default "latitude")
#' @param zName - character string with name of the zcoord in the 'ERDDAP' dataset (default "altitude")
#' @param tName - character string with name of the tcoord in the 'ERDDAP' dataset (default "time")
#' @param verbose - logical variable (default FALSE)
#' if the the URL request should be verbose
#' @return A dataframe containing:
Expand All @@ -36,42 +35,28 @@
#' \item column 11 = median absolute deviation of data within search radius
#' }
#' @examples
#' urlbase <- 'https://upwell.pfeg.noaa.gov/erddap'
#' dataInfo <- rerddap::info('erdMBsstd8day')
#' # toy example to show use
#' # but keep execution time down
#' \donttest{
#' dataInfo <- rerddap::info('erdHadISST')
#' }
#' parameter <- 'sst'
#' xcoord <- c(230, 231)
#' ycoord <- c(40, 41)
#' tcoord <- c('2006-01-15', '2006-01-20')
#' zcoord <- c(0., 0.)
#' xlen <- 0.5
#' ylen <- 0.5
#' xcoord <- c(-130.5)
#' ycoord <- c(40.5)
#' tcoord <- c('2006-01-16')
#' xlen <- 0.01
#' ylen <- 0.01
#' extract <- rxtracto(dataInfo, parameter = parameter, xcoord = xcoord,
#' ycoord = ycoord, tcoord= tcoord, zcoord = zcoord,
#' ycoord = ycoord, tcoord= tcoord,
#' xlen = xlen, ylen = ylen)
#' \donttest{
#' # 2-D example getting bathymetry
#' dataInfo <- rerddap::info('etopo360')
#' parameter <- 'altitude'
#' extract <- rxtracto(dataInfo, parameter, xcoord = xcoord, ycoord = ycoord,
#' xlen = xlen, ylen = ylen)
#' }
#'
#' # Example where grid is not latitude-longitude
#' dataInfo <- rerddap::info('glos_tds_5912_ca66_3f41')
#' parameter <- 'temp'
#' xName <- 'nx'
#' yName <- 'ny'
#' zName <- 'nsigma'
#' xcoord <- c(10, 11)
#' ycoord <- c(10, 11)
#' zcoord <- c(1, 1)
#' # time span changes in this dataset - get last three times
#' myURL <- "https://upwell.pfeg.noaa.gov/erddap/griddap/glos_tds_5912_ca66_3f41.csv0?time[last - 2:1:last]"
#' myTimes <- utils::read.csv(utils::URLencode(myURL), header = FALSE, stringsAsFactors = FALSE)[[1]]
#' tcoord <- c(myTimes[1], myTimes[3])
#' xlen <- 0
#' ylen <- 0
#' extract <- rxtracto(dataInfo, parameter, xcoord = xcoord, ycoord = ycoord,
#' zcoord = zcoord, tcoord = tcoord, xlen = xlen,
#' ylen = ylen, xName = xName, yName = yName, zName = zName)



Expand All @@ -80,15 +65,14 @@ rxtracto <- function(dataInfo, parameter = NULL, xcoord=NULL, ycoord = NULL,
zcoord = NULL, tcoord = NULL, xlen = 0., ylen = 0., zlen = 0.,
xName = 'longitude', yName = 'latitude', zName = 'altitude',
tName = 'time',
urlbase = 'https://upwell.pfeg.noaa.gov/erddap',
verbose = FALSE) {

# Check Passed Info -------------------------------------------------------
rerddap::cache_setup(temp_dir = TRUE)
callDims <- list(xcoord, ycoord, zcoord, tcoord)
names(callDims) <- c(xName, yName, zName, tName)
dataInfo1 <- dataInfo

urlbase <- dataInfo1$base_url
# Check that the non-null input vectors are the same length
dimLengths <- lapply(callDims, length)
dimLengths[dimLengths == 0] <- NULL
Expand Down Expand Up @@ -243,7 +227,6 @@ oldDataFrame <- out_dataframe[1, ]
# will try 5 times, if failure will stop and save data
tryn <- 0
goodtry <- -1
options(warn = 2)
while ((tryn <= numtries) & (goodtry == -1)) {
tryn <- tryn + 1
extract <- try(do.call(rerddap::griddap, griddapCmd ))
Expand All @@ -254,7 +237,9 @@ oldDataFrame <- out_dataframe[1, ]
# extract <- do.call(rerddap::griddap, griddapCmd )
if (goodtry == -1) {
print(griddapCmd)
print("There was an error in the url call, perhaps a time out. See message on screen and URL called")
text1 <- "There was an error in the url call, perhaps a time out."
text2 <- "See message on screen and URL called"
print(paste(text1, text2))
print("Returning incomplete download")
out_dataframe <- out_dataframe[1:(i - 1), ]
remove('paramdata')
Expand Down Expand Up @@ -297,7 +282,7 @@ oldDataFrame <- out_dataframe[1, ]
oldDataFrame <- out_dataframe[i,]

}
out_dataframe <- structure(out_dataframe, class = 'rxtractoTrack')
out_dataframe <- structure(out_dataframe, class = c('list', 'rxtractoTrack'))
return(out_dataframe)
}

Expand Down
78 changes: 30 additions & 48 deletions R/rxtracto_3D.R
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
#' Extract environmental data in a 3-dimnesional box from an ERDDAP server using rerddap.
#' Extract environmental data in a 3-dimensional box from an 'ERDDAP' server using 'rerddap'.
#'
#' \code{rxtracto_3D} uses the R program rerddap to extract environmental data
#' from an ERDDAP server in an (x,y,z, time) bounding box.
#' \code{rxtracto_3D} uses the R program 'rerddap' to extract environmental data
#' from an 'ERDDAP' server in an (x,y,z, time) bounding box.
#' The same call could be made directly in rerddap,
#' but function is maintained as it is used in the polygon routine.
#' @export
#' @param dataInfo - the return from an rerddap "info" call to an ERDDAP server
#' @param dataInfo - the return from an 'rerddap:info' call to an 'ERDDAP' server
#' @param parameter - character string containing the name of the parameter to extract
#' @param xcoord - a real array with the x-coordinates of the trajectory (if longitude in #' decimal degrees East, either 0-360 or -180 to 180)
#' @param ycoord - a real array with the y-coordinate of the trajectory (if latitude in
#' decimal degrees N; -90 to 90)
#' @param zcoord - a real array with the z-coordinate (usually altitude or depth)
#' @param tcoord - a character array with the times of the trajectory in
#' "YYYY-MM-DD" - for now restricted to be time.
#' @param xName - character string with name of the xcoord in the ERDDAP dataset (default "longitude")
#' @param yName - character string with name of the ycoord in the ERDDAP dataset (default "latitude")
#' @param zName - character string with name of the zcoord in the ERDDAP dataset (default "altitude")
#' @param tName - character string with name of the tcoord in the ERDDAP dataset (default "time")
#' @param urlbase - base URL of the ERDDAP server being accessed - default "http://upwell.pfeg.noaa.gov/erddap"
#' @param xName - character string with name of the xcoord in the 'ERDDAP' dataset (default "longitude")
#' @param yName - character string with name of the ycoord in the 'ERDDAP' dataset (default "latitude")
#' @param zName - character string with name of the zcoord in the 'ERDDAP' dataset (default "altitude")
#' @param tName - character string with name of the tcoord in the 'ERDDAP' dataset (default "time")
#' @param verbose - logical variable (default FALSE) if the the URL request should be verbose
#' @param cache_remove - logical variable (default TRUE) whether to delete 'rerddap' cache
#' @return structure with data and dimensions:
#' \itemize{
#' \item extract$data - the data array dimensioned (lon,lat,time)
Expand All @@ -29,38 +29,23 @@
#' \item extract$time - the times of the extracts
#' }
#' @examples
#' urlbase <- 'https://upwell.pfeg.noaa.gov/erddap'
#' dataInfo <- rerddap::info('erdMBsstd8day')
#' # toy example to show use
#' # and keep execution time low
#' \donttest{
#' dataInfo <- rerddap::info('erdHadISST')
#' }
#' parameter <- 'sst'
#' xcoord <- c(230, 235)
#' ycoord <- c(40, 45)
#' tcoord <- c('2006-01-15', '2006-01-20')
#' zcoord <- c(0., 0.)
#' xcoord <- c(-130.5, -130.5)
#' ycoord <- c(40.5, 40.5)
#' tcoord <- c('2006-01-16', '2006-01-16')
#' extract <- rxtracto_3D(dataInfo, parameter, xcoord = xcoord, ycoord = ycoord,
#' tcoord = tcoord, zcoord = zcoord)
#' tcoord = tcoord)
#'
#' \donttest{
#' # 2-D example getting bathymetry
#' dataInfo <- rerddap::info('etopo360')
#' parameter <- 'altitude'
#' extract <- rxtracto_3D(dataInfo, parameter, xcoord = xcoord, ycoord = ycoord)
#'
#' # Example where grid is not latitude-longitude
#' dataInfo <- rerddap::info('glos_tds_5912_ca66_3f41')
#' parameter = 'temp'
#' xName <- 'nx'
#' yName <- 'ny'
#' zName <- 'nsigma'
#' xcoord <- c(10, 11)
#' ycoord <- c(10, 11)
#' zcoord <- c(1, 1)
#' # time span changes in this dataset - get last three times
#' myURL <- "https://upwell.pfeg.noaa.gov/erddap/griddap/glos_tds_5912_ca66_3f41.csv0?time[last - 2:1:last]"
#' myTimes <- utils::read.csv(utils::URLencode(myURL), header = FALSE, stringsAsFactors = FALSE)[[1]]
#' tcoord <- c(myTimes[1], myTimes[3])
#' extract <- rxtracto_3D(dataInfo, parameter, xcoord = xcoord, ycoord = ycoord,
#' zcoord = zcoord, tcoord = tcoord, xName = xName,
#' yName = yName, zName = zName)
#'
#' # Dataset that has depth also
#' # 3 months of subsurface temperature at 70m depth from SODA 2.2.4
#' dataInfo <- rerddap::info('erdSoda331oceanmday')
Expand All @@ -75,16 +60,22 @@
#' extract <- rxtracto_3D(dataInfo, parameter, xcoord = xcoord, ycoord = ycoord,
#' zcoord = zcoord, tcoord = tcoord, xName = xName,
#' yName = yName, zName = zName)
#' }
#'

rxtracto_3D <- function(dataInfo, parameter = NULL, xcoord = NULL, ycoord = NULL, zcoord = NULL, tcoord = NULL, xName = 'longitude', yName = 'latitude', zName = 'altitude', tName = 'time', urlbase = 'https://upwell.pfeg.noaa.gov/erddap/', verbose=FALSE) {
rxtracto_3D <- function(dataInfo, parameter = NULL, xcoord = NULL,
ycoord = NULL, zcoord = NULL, tcoord = NULL,
xName = 'longitude', yName = 'latitude',
zName = 'altitude', tName = 'time',
verbose=FALSE, cache_remove = TRUE) {


# Check Passed Info -------------------------------------------------------
rerddap::cache_setup(temp_dir = TRUE)
callDims <- list(xcoord, ycoord, zcoord, tcoord)
names(callDims) <- c(xName, yName, zName, tName)
dataInfo1 <- dataInfo
urlbase <- dataInfo1$base_url
urlbase <- checkInput(dataInfo1, parameter, urlbase, callDims)


Expand Down Expand Up @@ -217,20 +208,11 @@ if (grepl('etopo',extract[[2]])) {

}

# copy netcdf file from cache to the present directory and rename
copyFile <- paste0(getwd(), '/', parameter, '.nc')
iFile <- 1
while (file.exists(copyFile)) {
copyFile <- paste0(getwd(), '/', parameter, '_', iFile, '.nc')
iFile <- iFile + 1
}
fcopy <- file.copy(griddapExtract$summary$filename, copyFile)
if (!fcopy) {
print('copying and renaming downloaded file from default ~/.rerddap failed')
}
# remove netcdf file from cache
rerddap::cache_delete(griddapExtract)
extract <- structure(extract, class = 'rxtracto3D')
if (cache_remove) {
rerddap::cache_delete(griddapExtract)
}
extract <- structure(extract, class = c('list', 'rxtracto3D'))
return(extract)
}

Loading

0 comments on commit 8b61bae

Please sign in to comment.