Skip to content

Commit

Permalink
Merge pull request #64 from NOAA-EDAB/andy_tablenames
Browse files Browse the repository at this point in the history
sql statement fixes due to sole migration
  • Loading branch information
andybeet authored Feb 6, 2024
2 parents ba44082 + c56b89e commit f9e29d3
Show file tree
Hide file tree
Showing 13 changed files with 83 additions and 66 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Imports:
dplyr,
magrittr,
ggplot2
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
Suggests:
knitr,
rmarkdown,
Expand Down
54 changes: 27 additions & 27 deletions R/apply_conversion_factors.r
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ apply_conversion_factors <- function(channel,survdat.raw,use.SAD = F) {

#Grab all conversion factors off the network
convert.qry <- "select *
from survan_conversion_factors"
from svdbs.survan_conversion_factors"

convert <- data.table::as.data.table(DBI::dbGetQuery(channel, convert.qry))

Expand Down Expand Up @@ -74,19 +74,19 @@ apply_conversion_factors <- function(channel,survdat.raw,use.SAD = F) {
#Bigelow >2008 Vessel Conversion
#Use Bigelow conversions for Pisces as well (PC)
#Tables 53-55 from Miller et al. 2010 - number estimators
big.abund <- data.table::data.table(svspp = c('012', '022', '024', '027', '028',
'031', '033', '034', '073', '076',
'106', '107', '109', '121', '135',
'136', '141', '143', '145', '149',
'155', '164', '171', '181', '193',
'197', '502', '512', '015', '023',
'026', '032', '072', '074', '077',
'078', '102', '103', '104', '105',
'108', '131', '163', '301', '313',
'401', '503', '015', '023', '026',
'032', '072', '074', '077', '078',
'102', '103', '104', '105', '108',
'131', '163', '301', '313', '401',
big.abund <- data.table::data.table(svspp = c('012', '022', '024', '027', '028',
'031', '033', '034', '073', '076',
'106', '107', '109', '121', '135',
'136', '141', '143', '145', '149',
'155', '164', '171', '181', '193',
'197', '502', '512', '015', '023',
'026', '032', '072', '074', '077',
'078', '102', '103', '104', '105',
'108', '131', '163', '301', '313',
'401', '503', '015', '023', '026',
'032', '072', '074', '077', '078',
'102', '103', '104', '105', '108',
'131', '163', '301', '313', '401',
'503'),
season = c(rep('both', 28), rep('spring', 19), rep('fall', 19)),
rho = c(1.161, 4.44, 6.689, 4.384, 3.792, 1.227, 1.323, 1.29,
Expand All @@ -100,19 +100,19 @@ apply_conversion_factors <- function(channel,survdat.raw,use.SAD = F) {
3.166, 0.84))

#Tables 56-58 from Miller et al. 2010 Biomass estimators
big.bio <- data.table::data.table(svspp = c('012', '022', '024', '027', '028',
'031', '033', '034', '073', '076',
'106', '107', '109', '121', '135',
'136', '141', '143', '145', '149',
'155', '164', '171', '181', '193',
'197', '502', '512', '015', '023',
'026', '032', '072', '074', '077',
'078', '102', '103', '104', '105',
'108', '131', '163', '301', '313',
'401', '503', '015', '023', '026',
'032', '072', '074', '077', '078',
'102', '103', '104', '105', '108',
'131', '163', '301', '313', '401',
big.bio <- data.table::data.table(svspp = c('012', '022', '024', '027', '028',
'031', '033', '034', '073', '076',
'106', '107', '109', '121', '135',
'136', '141', '143', '145', '149',
'155', '164', '171', '181', '193',
'197', '502', '512', '015', '023',
'026', '032', '072', '074', '077',
'078', '102', '103', '104', '105',
'108', '131', '163', '301', '313',
'401', '503', '015', '023', '026',
'032', '072', '074', '077', '078',
'102', '103', '104', '105', '108',
'131', '163', '301', '313', '401',
'503'),
season = c(rep('both', 28), rep('spring', 19), rep('fall', 19)),
rhoW = c(1.082, 3.661, 6.189, 4.45, 3.626, 1.403, 1.1, 2.12,
Expand Down
14 changes: 7 additions & 7 deletions R/get_survdat_clam_data.r
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ get_survdat_clam_data <- function(channel,
#Generate cruise list
#V1.2 - remove surveys prior to 1982 due to difference in seasons/gear
cruise.qry <- "select unique year, cruise6, svvessel
from mstr_cruise
from svdbs.mstr_cruise
where purpose_code = 50
and year >= 1982
order by year, cruise6"
Expand All @@ -96,14 +96,14 @@ get_survdat_clam_data <- function(channel,
if(shg.check == T){
station.qry <- paste("select unique cruise6, svvessel, station, stratum, decdeg_beglat as lat, decdeg_beglon as lon,
avgdepth as depth, surftemp, surfsalin, bottemp, botsalin
from Union_fscs_svsta
from svdbs.Union_fscs_svsta
where cruise6 in (", cruise6, ")
and SHG <= 136
order by cruise6, station", sep='')
} else {
station.qry <- paste("select unique cruise6, svvessel, station, stratum, decdeg_beglat as lat, decdeg_beglon as lon,
avgdepth as depth, surftemp, surfsalin, bottemp, botsalin
from UNION_FSCS_SVSTA
from svdbs.UNION_FSCS_SVSTA
where cruise6 in (", cruise6, ")
order by cruise6, station", sep='')
}
Expand All @@ -117,13 +117,13 @@ get_survdat_clam_data <- function(channel,
#Catch data
if(clam.only == T){
catch.qry <- paste("select cruise6, station, stratum, svspp, catchsex, expcatchnum as abundance, expcatchwt as biomass
from UNION_FSCS_SVCAT
from svdbs.UNION_FSCS_SVCAT
where cruise6 in (", cruise6, ")
and svspp in ('403', '409')
order by cruise6, station, svspp", sep='')
} else {
catch.qry <- paste("select cruise6, station, stratum, svspp, catchsex, expcatchnum as abundance, expcatchwt as biomass
from UNION_FSCS_SVCAT
from svdbs.UNION_FSCS_SVCAT
where cruise6 in (", cruise6, ")
order by cruise6, station, svspp", sep='')
}
Expand All @@ -138,13 +138,13 @@ get_survdat_clam_data <- function(channel,
#Length data
if(clam.only == T){
length.qry <- paste("select cruise6, station, stratum, svspp, catchsex, length, expnumlen as numlen
from UNION_FSCS_SVLEN
from svdbs.UNION_FSCS_SVLEN
where cruise6 in (", cruise6, ")
and svspp in ('403', '409')
order by cruise6, station, svspp, length", sep='')
} else {
length.qry <- paste("select cruise6, station, stratum, svspp, catchsex, length, expnumlen as numlen
from UNION_FSCS_SVLEN
from svdbs.UNION_FSCS_SVLEN
where cruise6 in (", cruise6, ")
order by cruise6, station, svspp, length", sep='')
}
Expand Down
14 changes: 7 additions & 7 deletions R/get_survdat_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,15 @@ get_survdat_data <- function(channel, filterByYear = NA, all.season = F,

if(all.season == F){ # Spring and Fall
cruise.qry <- paste0("select unique year, cruise6, svvessel, season
from mstr_cruise
from svdbs.mstr_cruise
where purpose_code = 10
and year ", years,
"and (season = 'FALL'
or season = 'SPRING')
order by year, cruise6")
} else if(all.season == T){ # Everything
cruise.qry <- paste0("select unique year, cruise6, svvessel, season
from mstr_cruise
from svdbs.mstr_cruise
where purpose_code = 10
and year ", years,
"order by year, cruise6")
Expand All @@ -140,7 +140,7 @@ get_survdat_data <- function(channel, filterByYear = NA, all.season = F,
tow, decdeg_beglat as lat, decdeg_beglon as lon,
begin_est_towdate as est_towdate, avgdepth as depth,
surftemp, surfsalin, bottemp, botsalin
from UNION_FSCS_SVSTA
from svdbs.UNION_FSCS_SVSTA
where cruise6 in (", cruise6, ")
and (SHG <= 136 and cruise6 <= 200900)
or (TOGA <= 1324 and cruise6 > 200900)
Expand All @@ -151,7 +151,7 @@ get_survdat_data <- function(channel, filterByYear = NA, all.season = F,
decdeg_beglat as lat, decdeg_beglon as lon,
begin_est_towdate as est_towdate, avgdepth as depth,
surftemp, surfsalin, bottemp, botsalin
from UNION_FSCS_SVSTA
from svdbs.UNION_FSCS_SVSTA
where cruise6 in (", cruise6, ")
order by cruise6, station")
}
Expand All @@ -167,7 +167,7 @@ get_survdat_data <- function(channel, filterByYear = NA, all.season = F,
message("Getting Species data ...")
catch.qry <- paste0("select cruise6, station, stratum, tow, svspp, catchsex,
expcatchnum as abundance, expcatchwt as biomass
from UNION_FSCS_SVCAT
from svdbs.UNION_FSCS_SVCAT
where cruise6 in (", cruise6, ")
and stratum not like 'YT%'
order by cruise6, station, svspp")
Expand All @@ -189,7 +189,7 @@ get_survdat_data <- function(channel, filterByYear = NA, all.season = F,
#Length data
length.qry <- paste0("select cruise6, station, stratum, tow, svspp, catchsex,
length, expnumlen as numlen
from UNION_FSCS_SVLEN
from svdbs.UNION_FSCS_SVLEN
where cruise6 in (", cruise6, ")
and stratum not like 'YT%'
order by cruise6, station, svspp, length")
Expand Down Expand Up @@ -250,7 +250,7 @@ get_survdat_data <- function(channel, filterByYear = NA, all.season = F,
message("Getting Individual Fish (Bio) Data ...")
bio.qry <- paste0("select cruise6, station, stratum, svspp, catchsex, length, indid,
indwt, sex, maturity, age, stom_volume, stom_wgt
from UNION_FSCS_SVBIO
from svdbs.UNION_FSCS_SVBIO
where cruise6 in (", cruise6, ")")
bio <- data.table::as.data.table(DBI::dbGetQuery(channel, bio.qry))

Expand Down
28 changes: 14 additions & 14 deletions R/get_survdat_scallop_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#' @param getWeightLength Boolean. Include the weight at length by applying length
#' weight coefficients from SVDBS. (Default = F)
#' @param scallopOnly Boolean. Include only sea scallop catch. (Default = T)
#'
#'
#' @return A list containing a Data frame (data.table) (n x 21), a list of SQL queries used to pull the data,
#' the date of the pull, and the call expression
#' Each row of the data.table represents the number at length of a species on a specific tow along with physical attributes of the tow.
Expand Down Expand Up @@ -105,11 +105,11 @@ get_survdat_scallop_data <- function(channel, filterByYear = NA, shg.check = T,
}

cruise.qry <- paste0("select unique year, cruise6, svvessel, season
from mstr_cruise
from svdbs.mstr_cruise
where purpose_code = 60
and year ", years,
"order by year, cruise6")


cruise <- data.table::as.data.table(DBI::dbGetQuery(channel, cruise.qry))
cruise <- na.omit(cruise)
Expand All @@ -127,17 +127,17 @@ get_survdat_scallop_data <- function(channel, filterByYear = NA, shg.check = T,
tow, decdeg_beglat as lat, decdeg_beglon as lon,
begin_est_towdate as est_towdate, avgdepth as depth,
surftemp, surfsalin, bottemp, botsalin
from UNION_FSCS_SVSTA
from svdbs.UNION_FSCS_SVSTA
where cruise6 in (", cruise6, ")
and SHG <= 136
and SHG <= 136
order by cruise6, station")
}
if(shg.check == F){
station.qry <- paste0("select unique cruise6, svvessel, station, stratum, tow,
decdeg_beglat as lat, decdeg_beglon as lon,
begin_est_towdate as est_towdate, avgdepth as depth,
surftemp, surfsalin, bottemp, botsalin
from UNION_FSCS_SVSTA
from svdbs.UNION_FSCS_SVSTA
where cruise6 in (", cruise6, ")
order by cruise6, station")
}
Expand All @@ -153,7 +153,7 @@ get_survdat_scallop_data <- function(channel, filterByYear = NA, shg.check = T,
message("Getting Species data ...")
catch.qry <- paste0("select cruise6, station, stratum, tow, svspp, catchsex,
expcatchnum as abundance, expcatchwt as biomass
from UNION_FSCS_SVCAT
from svdbs.UNION_FSCS_SVCAT
where cruise6 in (", cruise6, ")
and stratum not like 'YT%'
order by cruise6, station, svspp")
Expand All @@ -173,7 +173,7 @@ get_survdat_scallop_data <- function(channel, filterByYear = NA, shg.check = T,
#Length data
length.qry <- paste0("select cruise6, station, stratum, tow, svspp, catchsex,
length, expnumlen as numlen
from UNION_FSCS_SVLEN
from svdbs.UNION_FSCS_SVLEN
where cruise6 in (", cruise6, ")
and stratum not like 'YT%'
order by cruise6, station, svspp, length")
Expand Down Expand Up @@ -237,30 +237,30 @@ get_survdat_scallop_data <- function(channel, filterByYear = NA, shg.check = T,
# from UNION_FSCS_SVBIO
# where cruise6 in (", cruise6, ")")
# bio <- data.table::as.data.table(DBI::dbGetQuery(channel, bio.qry))
#
#
# #Remove YT Stratum
# bio <- bio[!STRATUM %like% 'YT', ]
#
#
# # fix bugs in SVDBS for character sex values
# bio[SEX %in% c("M","m"), SEX := 1]
# bio[SEX %in% c("F","f"), SEX := 2]
#
#
# #Fix catch sex prior to 2001
# bio[is.na(CATCHSEX), CATCHSEX := 0]
# bio[SVSPP %in% c('015', '301') & SEX == 1 & CRUISE6 < 200100, CATCHSEX := 1]
# bio[SVSPP %in% c('015', '301') & SEX == 2 & CRUISE6 < 200100, CATCHSEX := 2]
#
#
# data.table::setkey(bio, CRUISE6, STATION, STRATUM, SVSPP, CATCHSEX, LENGTH)
# data.table::setkey(survdat, CRUISE6, STATION, STRATUM, SVSPP, CATCHSEX, LENGTH)
#
#
# # data.table::setkey(bio, CRUISE6, STATION, STRATUM, TOW, SVSPP)
# # data.table::setkey(survdat, CRUISE6, STATION, STRATUM, TOW, SVSPP)
# survdat <- merge(survdat, bio, by = key(survdat))
# sql <- c(sql,bio=bio.qry)
# }

if(scallopOnly) survdat <- survdat[SVSPP == 401, ]

#Convert number fields from chr to num
numberCols <- c('CRUISE6', 'STATION', 'STRATUM', 'TOW', 'SVSPP', 'CATCHSEX', 'YEAR')
survdat[, (numberCols):= lapply(.SD, as.numeric), .SDcols = numberCols][]
Expand Down
4 changes: 1 addition & 3 deletions R/survdat.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,4 @@
#'To learn more about using \code{survdat}, start with the vignette: \code{browseVignettes(package="survdat")} or click the index below
#'
#'
#' @docType package
#' @name survdat
NULL
"_PACKAGE"
2 changes: 1 addition & 1 deletion man/get_conversion_factors.Rd

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

2 changes: 1 addition & 1 deletion man/get_cruise_purpose.Rd

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

2 changes: 1 addition & 1 deletion man/get_maturity.Rd

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

2 changes: 1 addition & 1 deletion man/get_species.Rd

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

2 changes: 1 addition & 1 deletion man/get_strata.Rd

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

2 changes: 1 addition & 1 deletion man/get_vessel.Rd

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

Loading

0 comments on commit f9e29d3

Please sign in to comment.