Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch stockeff #53

Merged
merged 25 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
1efa8a8
Switched to pulling from stockeff table
Nov 9, 2023
e241174
Merge branch 'switch_stockeff' of github.com:NOAA-EDAB/comlandr into …
Nov 9, 2023
b1b966f
switch channel to Sole
Nov 9, 2023
89f681e
Needed to specify the object sql
Nov 9, 2023
526ffd2
Missed an old comland.yr reference
Nov 9, 2023
c99f10f
Switched to one call for channel instead of two
Nov 21, 2023
763cf97
Switched to toncl2...may want to revisit default
Nov 21, 2023
61c756c
Needed to remove NESPP4 code from raw data
Nov 21, 2023
900a38f
Changed from MKTCAT to MARKET_CODE...need to revisit default
Nov 21, 2023
6da97a4
Needed to remove SPPLNDLB column
Nov 21, 2023
be7294b
Replaced toncl1 with toncl2
Nov 21, 2023
5cc9dc3
changed MKTCAT to MARKET_CODE
Nov 21, 2023
e913998
Needed to comment out toncl1 now using toncl2
Nov 21, 2023
0ebf47a
Added gtools
Nov 21, 2023
4b49d29
missed a call to data.table
Nov 21, 2023
9fdd81e
added reference to data.table
Nov 21, 2023
ba74623
updated documentation relating to change in number of function arguments
andybeet Nov 27, 2023
d4feaf0
added MARKET_CODE to foreign data
andybeet Nov 27, 2023
3ea428c
used MARKET_CODE = NA for foreign catch
andybeet Nov 27, 2023
965e3ea
aded place holders for argument documention in get_comland_data
andybeet Nov 27, 2023
5c0bb16
Merge pull request #52 from NOAA-EDAB/fix_foreign_stockeff
Nov 27, 2023
127a074
Need a fix for inflation PPI - using flat file for now
Nov 27, 2023
27bbbb1
Removed PPI code into a data script to make it available to the package
Nov 28, 2023
c474f48
Added useLanded argument for foreign data
Nov 30, 2023
d06f0ad
Missed a typo
Nov 30, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@ Suggests:
ggforce,
NEFSCspatial
VignetteBuilder: knitr
RoxygenNote: 7.2.2
RoxygenNote: 7.2.3
Imports:
data.table,
sf,
DBI,
odbc,
magrittr,
ggplot2,
gtools,
dbutils,
survdat
Remotes:
Expand Down
24 changes: 13 additions & 11 deletions R/adjust_inflation.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,20 @@ adjust_inflation <- function(comland, refYear, refMonth){
sql <- comland$sql
comland <- comland$comland

temp <- tempfile()
download.file("http://download.bls.gov/pub/time.series/wp/wp.data.3.ProcessedFoods", temp)
inflate <- data.table::as.data.table(read.delim(temp))
unlink(temp)

inflate[, series_id := gsub(" ", "", inflate[, series_id])]
deflate <- inflate[series_id == "WPU0223", ]
deflate[, MONTH := as.numeric(substr(period, 2, 3))]
data.table::setnames(deflate, c('year', 'value'), c('YEAR', 'PPI'))
deflate <- deflate[, list(YEAR, MONTH, PPI)]

#This isn't working right now - using downloaded file
# temp <- tempfile()
# download.file("http://download.bls.gov/pub/time.series/wp/wp.data.3.ProcessedFoods", temp)
#inflate <- data.table::as.data.table(read.delim(comlandr::wp.data.3.ProcessedFoods))
#unlink(temp)

# inflate[, series_id := gsub(" ", "", inflate[, series_id])]
# deflate <- inflate[series_id == "WPU0223", ]
# deflate[, MONTH := as.numeric(substr(period, 2, 3))]
# data.table::setnames(deflate, c('year', 'value'), c('YEAR', 'PPI'))
# deflate <- deflate[, list(YEAR, MONTH, PPI)]

#Set yearly deflator to 0 instead of 13 to match unknown month designation
deflate <- comlandr::deflate
deflate[MONTH == 13, MONTH := 0]
deflate.base <- deflate[YEAR == refYear & MONTH == refMonth, PPI]

Expand Down
12 changes: 6 additions & 6 deletions R/assign_unknown.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ assign_unknown <- function (comData, unkVar,
comdata[QY %in% 3:4, HY := 2]

#Assign size classes
comdata[TONCL1 %in% 1:3, SIZE := 1] #Small
comdata[TONCL1 > 3, SIZE := 2] #Large
#comdata[TONCL1 %in% 1:3, SIZE := 1] #Small
#comdata[TONCL1 > 3, SIZE := 2] #Large

for(ivar in 1:length(unkVar)){
if(unkVar[ivar] %in% knStrata){
Expand Down Expand Up @@ -88,7 +88,7 @@ assign_unknown <- function (comData, unkVar,
#combinations of strata codes
num.combos <- data.table::setDT(list(gtools::combinations(length(strata.code),
inum, strata.code)))
combos <- rbindlist(list(combos, num.combos), fill = T)
combos <- data.table::rbindlist(list(combos, num.combos), fill = T)
}


Expand All @@ -108,7 +108,7 @@ assign_unknown <- function (comData, unkVar,
'SPPLIVMT'))]

#Remove extra columns from known
known.simple <- copy(known)
known.simple <- data.table::copy(known)
known.simple <- known.simple[, c(names(known)[which(names(known) %in%
ext.col)]) := NULL]

Expand Down Expand Up @@ -158,7 +158,7 @@ assign_unknown <- function (comData, unkVar,
##Still no match - use 3 or 5 year window then drop year
if(nrow(unknown) > 0){
#Remove extra columns from known
known.all <- copy(known)
known.all <- data.table::copy(known)
known.all <- known.all[, list(YEAR, NESPP3, VAR, SPPLIVMT)]

#3 year window
Expand Down Expand Up @@ -260,7 +260,7 @@ assign_unknown <- function (comData, unkVar,
##Still no match - match to species
if(nrow(unknown) > 0){
#Remove extra columns from known
known.all <- copy(known)
known.all <- data.table::copy(known)
known.all <- known.all[, list(NESPP3, VAR, SPPLIVMT)]

#Sum landings per stratification
Expand Down
12 changes: 6 additions & 6 deletions R/disaggregate_skates_hakes.R
Original file line number Diff line number Diff line change
Expand Up @@ -122,21 +122,21 @@ disaggregate_skates_hakes <- function(comland, channel, filterByYear, filterByAr
#put skates in comland format to merge back
#Little
little <- comland.skates[, list(YEAR, AREA, MONTH, NEGEAR,
TONCL1, NESPP3, UTILCD, MESHCAT, MKTCAT,
TONCL2, NESPP3, UTILCD, MESHCAT, MARKET_CODE,
little, little.value, US)]
little[, NESPP3 := 366]
data.table::setnames(little, c('little', 'little.value'), c('SPPLIVMT', 'SPPVALUE'))
little <- little[SPPLIVMT > 0, ]
#Winter
winter <- comland.skates[, list(YEAR, AREA, MONTH, NEGEAR,
TONCL1, NESPP3, UTILCD, MESHCAT, MKTCAT,
TONCL2, NESPP3, UTILCD, MESHCAT, MARKET_CODE,
winter, winter.value, US)]
winter[, NESPP3 := 367]
data.table::setnames(winter, c('winter', 'winter.value'), c('SPPLIVMT', 'SPPVALUE'))
winter <- winter[SPPLIVMT > 0, ]
#Other skates
other <- comland.skates[, list(YEAR, AREA, MONTH, NEGEAR,
TONCL1, NESPP3, UTILCD, MESHCAT, MKTCAT,
TONCL2, NESPP3, UTILCD, MESHCAT, MARKET_CODE,
other.skate, other.skate.value, US)]
other[, NESPP3 := 365]
data.table::setnames(other, c('other.skate', 'other.skate.value'), c('SPPLIVMT', 'SPPVALUE'))
Expand Down Expand Up @@ -239,21 +239,21 @@ disaggregate_skates_hakes <- function(comland, channel, filterByYear, filterByAr
#Silver (509), offshore (508), whiting (507)
#put hakes in comland format to merge back
silver <- comland.hakes[, list(YEAR, AREA, MONTH, NEGEAR,
TONCL1, NESPP3, UTILCD, MESHCAT, MKTCAT,
TONCL2, NESPP3, UTILCD, MESHCAT, MARKET_CODE,
silver, silver.value, US)]
silver[, NESPP3 := 509]
data.table::setnames(silver, c('silver', 'silver.value'), c('SPPLIVMT', 'SPPVALUE'))
silver <- silver[SPPLIVMT > 0, ]

offshore <- comland.hakes[, list(YEAR, AREA, MONTH, NEGEAR,
TONCL1, NESPP3, UTILCD, MESHCAT, MKTCAT,
TONCL2, NESPP3, UTILCD, MESHCAT, MARKET_CODE,
offshore, offshore.value, US)]
offshore[, NESPP3 := 508]
data.table::setnames(offshore, c('offshore', 'offshore.value'), c('SPPLIVMT', 'SPPVALUE'))
offshore <- offshore[SPPLIVMT > 0, ]

other <- comland.hakes[, list(YEAR, AREA, MONTH, NEGEAR,
TONCL1, NESPP3, UTILCD, MESHCAT, MKTCAT,
TONCL2, NESPP3, UTILCD, MESHCAT, MARKET_CODE,
other.hakes, other.hakes.value, US)]
other[, NESPP3 := 507]
data.table::setnames(other, c('other.hakes', 'other.hakes.value'), c('SPPLIVMT', 'SPPVALUE'))
Expand Down
88 changes: 53 additions & 35 deletions R/get_comland_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,40 @@
#'
#'@param channel an Object inherited from \link[DBI]{DBIConnection-class}. This object is used to connect
#' to communicate with the database engine. (see \code{\link[dbutils]{connect_to_database}})
#'@param endyear Numeric Scalar. Final year of query.
#'@param landed Character String. Use landed weight ("y" - meatwt) for scallops and clams instead of live weight ("n" - livewt).
#'@param out.dir path to directory where final output will be saved
#'@param filterByYear numeric vector
#'@param filterByArea numeric vector
#'@param useLanded boolean
#'@param removeParts boolean
#'@param useHerringMaine boolean
#'@param useForeign boolean
#'@param refYear numeric
#'@param refMonth numeric
#'@param disaggSkatesHakes boolean
#'@param aggArea boolean
#'@param userAreas data frame
#'@param areaDescription character string
#'@param propDescription character string
#'@param applyPropLand boolean
#'@param applyPropValue boolean
#'@param aggGear boolean
#'@param userGears data frame
#'@param fleetDescription character string
#'@param unkVar character vector
#'@param knStrata character vector
#'
#'
#'@return Data frame (data.table) (n x 10)
#'@return Data frame (data.table) (n x 12)
#'Each row of the data.table represents a species record for a given tow/trip
#'
#'\item{YEAR}{Year of trip/tow}
#'\item{MONTH}{Month of trip/tow}
#'\item{NEGEAR}{Fishing gear used on trip/tow}
#'\item{TONCL1}{Tonnage class of the fishing vessel}
#'\item{TONCL2}{Tonnage class of the fishing vessel (2 digit value)}
#'\item{NESPP3}{Species code (3 charachters)}
#'\item{NESPP4}{Species code and market code (4 characters)}
#'\item{AREA}{Statistical area in which species was reportly caught}
#'\item{MARKET_CODE}{market code (2 characters)}
#'\item{MESHCAT}{Code to describe the mesh size for a trawl vessel}
#'\item{AREA/EPU}{Statistical area/ Ecological Production Unit in which species was reportly caught}
#'\item{UTILCD}{Utilization code}
#'\item{US}{Landing from the USA vessels or foreign vessels}
#'\item{SPPLIVLB}{live weight (landed = "n") or landed weight (landed="y") in lbs}
#'\item{SPPVALUE}{The value of landed catch to the nearest dollar (U.S.), paid to fisherman by dealer, for a given species.}
#'
Expand All @@ -34,75 +52,75 @@
#'@export


get_comland_data <- function(channelSole, channelNova, filterByYear = NA,
filterByArea = NA, useLanded = T, removeParts = T,
useHerringMaine = T, useForeign = T, refYear = NA,
refMonth = NA, disagSkatesHakes = T, aggArea = F,
userAreas = comlandr::mskeyAreas,
get_comland_data <- function(channel, filterByYear = NA,
filterByArea = NA, useLanded = T, removeParts = T,
useHerringMaine = T, useForeign = T, refYear = NA,
refMonth = NA, disagSkatesHakes = T, aggArea = F,
userAreas = comlandr::mskeyAreas,
areaDescription = 'EPU', propDescription = 'MeanProp',
applyPropLand = T, applyPropValue = T,
aggGear = F, userGears = comlandr::mskeyGears,
fleetDescription = 'Fleet', unkVar = 'AREA',
knStrata = c('NESPP3', 'YEAR', 'HY', 'QY', 'MONTH',
'NEGEAR', 'TONCL1', 'AREA')) {
fleetDescription = 'Fleet',
unkVar = c('MONTH','NEGEAR','AREA'),
knStrata = c('HY', 'QY','MONTH','NEGEAR', 'TONCL2', 'AREA')) {




call <- dbutils::capture_function_call()

#Pull raw data
comland <- comlandr::get_comland_raw_data(channelSole, channelNova,
filterByYear, filterByArea,
comland <- comlandr::get_comland_raw_data(channel,
filterByYear, filterByArea,
useLanded, removeParts)

#Pull herring data from the state of Maine
if(useHerringMaine){
comland <- comlandr::get_herring_data(channelSole, comland,
filterByYear, filterByArea,
comland <- comlandr::get_herring_data(channel, comland,
filterByYear, filterByArea,
useForeign)
}

#Pull foreign landings
if(useForeign){
#Look up NAFO divisions that contain Stat areas
if (all(!is.na(filterByArea))) {
NAFOAreas <- comlandr::get_areas(channelSole)$data %>%
NAFOAreas <- comlandr::get_areas(channel)$data %>%
dplyr::select(AREA,NAFDVCD) %>%
dplyr::filter(AREA %in% filterByArea) %>%
dplyr::pull(NAFDVCD) %>%
unique() %>%
as.integer()
filterByArea <- c(filterByArea, NAFOAreas)
}

#Pull data and process to look like comland data
comland.foreign <- comlandr::get_foreign_data(filterByYear, filterByArea)
comland.foreign <- comlandr::process_foreign_data(channelSole, comland.foreign,
useHerringMaine)
comland.foreign <- comlandr::process_foreign_data(channel, comland.foreign,
useLanded, useHerringMaine)

#Combine foreign landings
comland$comland <- data.table::rbindlist(list(comland$comland, comland.foreign),
comland$comland <- data.table::rbindlist(list(comland$comland, comland.foreign),
use.names = T)
}
}


#Apply correction for inflation
if(!is.na(refYear)) comland <- comlandr::adjust_inflation(comland, refYear, refMonth)

#Disaggregate skates and hakes
if(disagSkatesHakes) comland <- comlandr::disaggregate_skates_hakes(comland,
channelSole,
if(disagSkatesHakes) comland <- comlandr::disaggregate_skates_hakes(comland,
channel,
filterByYear, filterByArea)

#Aggregate areas
if(aggArea) comland <- comlandr::aggregate_area(comland, userAreas,
if(aggArea) comland <- comlandr::aggregate_area(comland, userAreas,
areaDescription, propDescription,
useForeign, channelSole,
useForeign, channel,
applyPropLand, applyPropValue)

#Aggregate gears
if(aggGear) comland <- comlandr::aggregate_gear(comland, userGears, fleetDescription)

#Impute unknown catch variables
if(!is.null(unkVar)) comland <- comlandr::assign_unknown(comland, unkVar, knStrata)

Expand Down
Loading
Loading