-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New functions findKey() and getSAG()
- Loading branch information
Arni Magnusson
committed
Oct 29, 2016
1 parent
fc81a92
commit b566f2d
Showing
12 changed files
with
238 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#' Find a Key | ||
#' | ||
#' Find a lookup key corresponding to a stock in a given assessment year. | ||
#' | ||
#' @param stock a stock name, e.g. cod-347d, or NULL to process all stocks. | ||
#' @param year the assessment year, e.g. 2015, or 0 to process all years. | ||
#' @param published whether to include only years where status is "Published". | ||
#' @param full whether to return a data frame with all stock list columns. | ||
#' | ||
#' @return A vector of keys (default) or a data frame if full is TRUE. | ||
#' | ||
#' @seealso | ||
#' \code{\link{getListStocks}} gets a list of stocks. | ||
#' | ||
#' \code{\link{icesSAG-package}} gives an overview of the package. | ||
#' | ||
#' @author Arni Magnusson. | ||
#' | ||
#' @examples | ||
#' summary <- getSAG("cod-347d", 2015) | ||
#' refpts <- getSAG("cod-347d", 2015, "refpts") | ||
#' | ||
#' @export | ||
|
||
findKey <- function(stock, year, published = TRUE, full = FALSE) | ||
{ | ||
# check web services are running | ||
if (!checkSAGWebserviceOK()) return (FALSE) | ||
|
||
# get stock list | ||
url <- | ||
sprintf( | ||
"https://standardgraphs.ices.dk/StandardGraphsWebServices.asmx/getListStocks?year=%i", | ||
year) | ||
out <- lapply(url, | ||
function(u) { | ||
out <- curlSAG(u) | ||
parseSAG(out) | ||
}) | ||
out <- do.call(rbind, out) | ||
|
||
# filter and format | ||
if (!is.null(stock)) out <- out[out$FishStockName %in% stock,] | ||
if (published) out <- out[out$Status == "Published",] | ||
if (!full) out <- out$key | ||
|
||
out | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#' Get Any SAG Data | ||
#' | ||
#' This function combines the functionality of getListStocks, | ||
#' getFishStockReferencePoints, and getSummaryTable. | ||
#' It supports querying many years and quarters in one function call. | ||
#' | ||
#' @param stock a stock name, e.g. cod-347d, or NULL to process all stocks. | ||
#' @param year the assessment year, e.g. 2015, or 0 to process all years. | ||
#' @param data the data of interest, either "summary" or "refpts" | ||
#' @param combine whether to combine the list output to a data frame. | ||
#' | ||
#' @note Only years with "Published" status are returned. | ||
#' | ||
#' @return A data frame (default) or a list if \code{combine} is \code{TRUE}. | ||
#' | ||
#' @seealso | ||
#' \code{\link{getListStocks}}, \code{\link{getSummaryTable}}, and | ||
#' \code{\link{getFishStockReferencePoints}} get a list of stocks, summary | ||
#' results, and reference points. | ||
#' | ||
#' \code{\link{findKey}} finds lookup keys. | ||
#' | ||
#' \code{\link{icesSAG-package}} gives an overview of the package. | ||
#' | ||
#' @author Arni Magnusson. | ||
#' | ||
#' @examples | ||
#' summary <- getSAG("cod-347d", 2015) | ||
#' refpts <- getSAG("cod-347d", 2015, "refpts") | ||
#' | ||
#' @export | ||
|
||
getSAG <- function(stock, year, data = "summary", combine = TRUE) { | ||
# select web service operation and parser | ||
data <- match.arg(data, c("summary", "refpts")) | ||
operation <- switch(data, | ||
summary = "getSummaryTable", | ||
refpts = "getFishStockReferencePoints") | ||
parseFunction <- switch(data, | ||
summary = parseSummary, | ||
refpts = parseSAG) | ||
|
||
# check web services are running | ||
if (!checkSAGWebserviceOK()) return (FALSE) | ||
|
||
# find lookup key | ||
key <- findKey(stock, year) | ||
|
||
# get data requested by user | ||
url <- | ||
sprintf( | ||
"https://standardgraphs.ices.dk/StandardGraphsWebServices.asmx/%s?key=%i", | ||
operation, key) | ||
out <- lapply(url, | ||
function(u) { | ||
out <- curlSAG(u) | ||
parseFunction(out) | ||
}) | ||
if (combine) out <- do.call(rbind, out) | ||
out | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.