Skip to content

Commit 5cf0be2

Browse files
committed
internal functions for processing pulled data from API
1 parent ee240aa commit 5cf0be2

2 files changed

Lines changed: 134 additions & 0 deletions

File tree

R/process_stock_summary_data.R

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#' Process the summary data table for an individual itis
2+
#'
3+
#' @description Relabel and combine fields
4+
#'
5+
#' @param url String. The url of the stocksmart API
6+
#' @param stockids Vector. The Stock ID numbers by which data are referenced
7+
#'
8+
#' @return A data frame
9+
#'
10+
#' @family processing
11+
#'@noRd
12+
13+
process_stock_summary_data <- function(url, stockids) {
14+
## Now get summary data
15+
16+
## Get summary data using assessment id's (asmtids)
17+
# create object to convert to JSON for url parameter
18+
summary <- list()
19+
ssummary <- list()
20+
summary$dataType <- ""
21+
summary$scName <- "- Science Center -"
22+
summary$ecoName <- "- Reg. Ecosystem -"
23+
summary$jurName <- "- Jurisdiction -"
24+
summary$rgnName <- ""
25+
summary$fmpName <- "- Fish Mgmt Plan -"
26+
summary$monName <- ""
27+
summary$entityIdList <- paste(stockids, collapse = ",")
28+
summary$outputFieldList <- "as_year,as_month,as_last_data_year,as_update_type,as_review_type,as_model,as_model_version,as_lead_lab,as_citation,as_files,as_point_of_contact,as_life_history,as_abundance,as_catch,as_level,as_frequency,as_type,as_model_cat,as_catch_data,as_abundance_data,as_biological_data,as_ecosystem_data,as_comp_data,as_f_year,as_f_best,as_f_unit,as_f_basis,as_flimit,as_flimit_basis,as_fmsy,as_fmsy_basis,as_f_flimit_ratio,as_f_fmsy_ratio,as_ftarget,as_ftarget_basis,as_f_ftarget_ratio,as_b_year,as_b_best,as_b_unit,as_b_basis,as_blimit,as_blimit_basis,as_bmsy,as_bmsy_basis,as_b_blimit_ratio,as_b_bmsy_ratio,as_msy,as_msy_unit"
29+
summary$outputLabelList <- "Assessment Year,Assessment Month,Last Data Year,Update Type,Review Result,Assessment Model,Model Version,Lead Lab,Citation,Final Assessment Report,Point of Contact,Life History Data,Abundance Data,Catch Data,Assessment Level,Assessment Frequency,Assessment Type,Model Category,Catch Input Data,Abundance Input Data,Biological Input Data,Ecosystem Linkage,Composition Input Data,F Year,Estimated F,F Unit,F Basis,Flimit,Flimit Basis,Fmsy,Fmsy Basis,F/Flimit,F/Fmsy,Ftarget,Ftarget Basis,F/Ftarget,B Year,Estimated B,B Unit,B Basis,Blimit,Blimit Basis,Bmsy,Bmsy Basis,B/Blimit,B/Bmsy,MSY,MSY Unit"
30+
summary$entityAttrList <- "ent_name,jur_name,fmp_name,sc_name,eco_name,ent_fssi_flag,tsn,sn,cn,sa"
31+
summary$entityAttrLabelList <- "Stock Name,Jurisdiction,FMP,Science Center,Regional Ecosystem,FSSI Stock?,ITIS Taxon Serial Number,Scientific Name,Common Name,Stock Area"
32+
summary$fileTypeList <- ""
33+
summary$DownloadTool_includeNoAsmt <- "N"
34+
summary$segIndex <- "0"
35+
summary$DownloadTool_carryForward <- "N"
36+
summary$DownloadTool_ent_name <- ""
37+
summary$DownloadTool_jur_select <- "%"
38+
summary$DownloadTool_fmp_select <- "%"
39+
summary$DownloadTool_sc_select <- "%"
40+
summary$DownloadTool_eco_select <- "%"
41+
summary$DownloadTool_fssi_select <- ""
42+
summary$startYear <- "1800"
43+
summary$endYear <- format(Sys.Date(), "%Y") #current year
44+
summary$asmtYears <- ""
45+
ssummary$crit <- summary
46+
ssummary$dataType <- "Assessment Summary Data"
47+
ssummary$dataFormat <- "excel"
48+
49+
# convert to JSON
50+
jsonquery <- jsonlite::toJSON(ssummary, pretty = TRUE, auto_unbox = TRUE)
51+
52+
# make url request
53+
file <- httr::GET(
54+
paste0(url, "data-export-servlet"),
55+
query = list(fileTypeList = "", jsonParam = jsonquery)
56+
)
57+
58+
# The following is hacky but needed since the only output format is xlsx
59+
# would be better if API returned data in JSON format
60+
# pull data and download in temporary file
61+
summary_api_data <- httr::GET(file$url)
62+
# create a temporary file to store download
63+
tmp <- tempfile(fileext = ".xlsx")
64+
# Write the binary content to the temp file
65+
writeBin(httr::content(summary_api_data, as = "raw"), tmp)
66+
# read the temporary file
67+
summary_data_itis <- suppressMessages(readxl::read_xlsx(
68+
tmp,
69+
col_names = TRUE
70+
))
71+
72+
summary_data <- summary_data_itis |>
73+
dplyr::mutate(
74+
Type = dplyr::case_when(
75+
is.na(`Update Type`) ~ `Assessment Type`,
76+
TRUE ~ `Update Type`
77+
)
78+
) |>
79+
dplyr::select(-`Assessment Type`, -`Update Type`) |>
80+
dplyr::rename(`Assessment Type` = Type)
81+
82+
return(summary_data)
83+
}

R/process_stock_tsdata.R

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#' Process the Timeseries data from stocksmart website
2+
#'
3+
#' @param dataf data.frame. Exported xlsx data object from stocksmart API
4+
#'
5+
#' @returns A data frame
6+
#'
7+
#' @family processing
8+
#'@noRd
9+
10+
process_stock_tsdata <- function(dataf) {
11+
stock_tsdata <- NULL
12+
year <- as.numeric(unlist(dataf[9:nrow(dataf), 2]))
13+
for (icol in 3:ncol(dataf)) {
14+
# get metadata for this species. 1st 5 rows of data
15+
meta <- as.vector(unlist(dataf[1:8, icol]))
16+
# parse metadata to extract species name, region, metric,
17+
#Description, Units
18+
spnm_region <- meta[1]
19+
stockid <- as.numeric(meta[2])
20+
assessmentid <- as.numeric(meta[3])
21+
AssessmentYear <- as.numeric(meta[4])
22+
AssessmentMonth <- as.numeric(meta[5])
23+
metric <- meta[6]
24+
description <- meta[7]
25+
units <- meta[8]
26+
# time series data
27+
ts <- as.numeric(unlist(dataf[9:nrow(dataf), icol]))
28+
# select years where data is available
29+
ind <- !is.na(ts)
30+
Year <- year[ind]
31+
ts <- ts[ind]
32+
# length of data available
33+
nYrs <- length(Year)
34+
# build a tidy tibble
35+
species_data <- tibble::tibble(
36+
StockName = rep(spnm_region, nYrs),
37+
Stockid = rep(stockid, nYrs),
38+
Assessmentid = rep(assessmentid, nYrs),
39+
Year = Year,
40+
Value = ts,
41+
Metric = rep(metric, nYrs),
42+
Description = rep(description, nYrs),
43+
Units = rep(units, nYrs),
44+
AssessmentYear = rep(AssessmentYear, nYrs)
45+
)
46+
# combine data for this column to master
47+
stock_tsdata <- rbind(stock_tsdata, species_data)
48+
}
49+
50+
return(stock_tsdata)
51+
}

0 commit comments

Comments
 (0)