Skip to content

Commit

Permalink
Merge branch 'master' of github.com:noaa-nwfsc/FishSET into policy_ad…
Browse files Browse the repository at this point in the history
…ditions
  • Loading branch information
anna-abelman committed May 8, 2024
2 parents d1cc943 + 7910750 commit 77cf615
Show file tree
Hide file tree
Showing 22 changed files with 87 additions and 267 deletions.
11 changes: 5 additions & 6 deletions R/create_distance_matrix.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ create_dist_matrix <-
warning("CRS is not specfied, distance matrix will be created using WGS 84 (4326).",
call. = FALSE)
}

# occasion ----

## Centroid ----
Expand All @@ -62,20 +63,17 @@ create_dist_matrix <-

fromXY <- dataset[zone_ind, o_var]

if (occasion == "zonal centroid") cent_tab <- zone_cent
else if (occasion == "fishing centroid") cent_tab <- fish_cent
if (occasion == "zonal centroid") cent_tab <- zone_cent
else if (occasion == "fishing centroid") cent_tab <- fish_cent

if (o_var == zoneID | !any(fromXY[[o_var]] %in% port$Port_Name)) {
# o_var == zoneID = single haul
# otherwise, multi-haul w/ prev area variable
if (o_var == zoneID){ # o_var == zoneID = single haul
# join centroid lon-lat by zoneID
join_by <- stats::setNames("ZoneID", o_var)
fromXY <- dplyr::left_join(fromXY, cent_tab, by = join_by)

} else { # Previous Area (multi-haul)
# include port table previous area if o_var contains port names (this happens
# when creating a previous area/starting loc variable and the port is not within a zone)

area_tab <-
do.call(rbind,
lapply(list(port, cent_tab), function(x) {
Expand Down Expand Up @@ -125,6 +123,7 @@ create_dist_matrix <-
fromXY <- dataset[zone_ind, occasion_var]
}


# alt_var ----

## centroid ----
Expand Down
56 changes: 30 additions & 26 deletions R/create_startingloc.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#' Create starting location variable
#'
#' Creates a variable containing the zone/area location of a vessel when choice of
#' where to fish next was made. This variable is required for the full information
#' model with Dahl's correction (\code{\link{logit_correction}}).
#' where to fish next was made. This variable is required for data with multiple sets
#' or hauls in a single trip and for the full information model with
#' Dahl's correction (\code{\link{logit_correction}}).
#'
#' @param dat Primary data containing information on hauls or trips.
#' Table in FishSET database contains the string 'MainDataTable'.
Expand All @@ -12,6 +13,9 @@
#' @param port Port data. Contains columns: Port_Name, Port_Long, Port_Lat.
#' Table is generated using the \code{\link{load_port}} and saved in the FishSET
#' database as the project and port table, for example 'pollockPortTable'.
#' @param port_name Character string indicating the column in port table that contains the port name
#' @param port_lon Character string indication the column in port table that contains port longitude
#' @param port_lat Character string indication the column in port table that contains port latitude
#' @param trip_id Variable in \code{dat} that identifies unique trips.
#' @param haul_order Variable in \code{dat} containing information on the order
#' that hauls occur within a trip. Can be time, coded variable, etc.
Expand Down Expand Up @@ -45,23 +49,24 @@
#' )
#' }
#
create_startingloc <-
function(dat,
project = NULL,
spat,
port,
trip_id,
haul_order,
starting_port,
zoneID,
spatID,
name = "startingloc") {
create_startingloc <- function(dat,
project = NULL,
spat,
port,
port_name,
port_lon,
port_lat,
trip_id,
haul_order,
starting_port,
zoneID,
spatID,
name = "startingloc") {

# TODO: consider removing assignment_column() functionality
# TODO: Change this to required? project is required for nearly every other function
# why not here?
if (is.null(project)) {

project <- sub("\\MainDataTable", "", dat)
}

Expand All @@ -78,29 +83,29 @@ create_startingloc <-
# Call in spatial data
spat_out <- data_pull(spat, project)
spatdat <- spat_out$dataset
spat <- parse_data_name(dat, "spat", project)
spat <- parse_data_name(spat, "spat", project)

# Make sure columns are present in primary data table
column_check(dataset, cols = c(trip_id, haul_order, starting_port, zoneID))

column_check(spatdat, cols = c(zoneID))
# Make sure columns are present in spat table
column_check(spatdat, cols = c(spatID))

# name <- ifelse(is_empty(name), "startingloc", name)
# Make sure new column name is unique and doesn't already exist
name <- name_check(dataset, name, repair = TRUE)

port.table <- assignment_column(
dat = port.table, project = project, spat = spatdat, hull.polygon = FALSE,
lon.dat = "Port_Long", lat.dat = "Port_Lat", cat = spatID, closest.pt = TRUE,
lon.dat = port_lon, lat.dat = port_lat, cat = spatID, closest.pt = TRUE,
log.fun = FALSE, bufferval = 100 # need to consider how this affects things
)

# Create starting loc variable
# TODO: trip_id is currently required; can't be NULL. Resolve.
if (is.null(trip_id)) {

dataset <- dataset[order(dataset[[haul_order]]), ]

} else {

dataset <- dataset[order(dataset[[trip_id]], dataset[[haul_order]]), ]
}

Expand All @@ -109,24 +114,23 @@ create_startingloc <-

# Make starting of trips set to zone of starting port
if (!is.null(trip_id)) {

rownumbers <- match(trimws(dataset[tapply(seq_along(dataset[[trip_id]]), dataset[[trip_id]], min), starting_port]), port.table$Port_Name)
rownumbers <- match(
trimws(dataset[tapply(seq_along(dataset[[trip_id]]), dataset[[trip_id]], min), starting_port][[1]]),
port.table[port_name])

if (any(is.na(rownumbers))) {

warning('NAs produced. At least one disembarked port was not found in the port table.')
}

newvar[tapply(seq_along(dataset[[trip_id]]), dataset[[trip_id]], min)] <- port.table[rownumbers, 'ZoneID']

} else {

rownumbers <- match(trimws(dataset[1, starting_port]), port.table$Port_Name)
newvar[1] <- port.table[rownumbers, zoneID]
}

g <- cbind(dataset, newvar)
colnames(g)[dim(g)[2]] = name
g <- cbind(dataset, as.character(newvar))
colnames(g)[dim(g)[2]] <- name

create_startingloc_function <- list()
create_startingloc_function$functionID <- "create_startingloc"
Expand Down
17 changes: 0 additions & 17 deletions R/data-SST.R

This file was deleted.

58 changes: 1 addition & 57 deletions R/data.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,12 @@
#' \describe{
#' \item{TRIPID}{Randomly assigned trip ID number.}
#' \item{DATE_TRIP}{Date of landing.}
#' \item{scallop_fishing_year}{Scallop fishing year.}
#' \item{OPERNUM}{Randomly assigned captains Identification number.}
#' \item{PERMIT.y}{Randomly assigned six-digit vessel fishing permit number.}
#' \item{TRIP_LENGTH}{Days calculated from the elapsed time between the
#' date-time sailed and date-time landed; this is a measure of days absent.}
#' \item{DEALER_RPT_ID}{}
#' \item{GEARCODE}{Fishing gear used on the trip.}
#' \item{dbsource}{Primary data source for observation.}
#' \item{geoid}{10 digit county subdivision from US Census.}
#' \item{namelsad}{Port of landing.}
#' \item{port_lat}{Latitude of the geoid.}
#' \item{port_lon}{longitude of the geoid.}
#' \item{previous_namelsad}{Previous port of landing.}
#' \item{previous_state_fips}{Previous 2 digit state fips code.}
#' \item{previous_geoid}{Previous geoid.}
#' \item{previous_port_lat}{Previous latitude of geoid.}
#' \item{previous_port_lon}{Previous longitude of geoid.}
#' \item{Plan Code}{Portion of the VMS declaration code that identifies the
Expand All @@ -38,8 +29,6 @@
#' 99th percentile value, respectively.}
#' \item{DDLAT}{The latitude reported on a VTR (Vessel Trip Reports).}
#' \item{DDLON}{The longitude reported on a VTR (Vessel Trip Reports).}
#' \item{MN30SQID}{Unique identifier assigned to a thirty minute square.}
#' \item{MN10SQID}{Unique identifier assigned to a ten minute square.}
#' \item{NAME}{Name of wind lease which is found within a given ten minute square.}
#' \item{ZoneID}{FishSET's version of a ten minute square.}
#' \item{POUNDS}{Live pounds.}
Expand Down Expand Up @@ -100,49 +89,4 @@
#' \item{NAME}{Name of wind lease.}
#' }
#'
"windLease"


#' Alaskan Ports
#'
#' A dataframe of Alaskan ports.
#'
#' @format `alaskaPorts`
#' A dataframe with three columns and 88 rows:
#' \describe{
#' \item{PORT}{Name of port.}
#' \item{LONGITUDE}{LONGITUDE of port.}
#' \item{LATITUDE}{LATITUDE of port.}
#' }
#'
"alaskaPorts"


#' Alaskan NMFS Areas
#'
#' A spatial dataset of Alaskan NMFS areas.
#'
#' @format `alaskaNMFSAreas`
#' A simple features multipolygon dataframe with 10 features and two fields:
#' \describe{
#' \item{NMFS_AREA}{NMFS area number.}
#' \item{geometry}{Coordinates for NMFS area.}
#' }
#'
"alaskaNMFSAreas"


#' Pollock Data
#'
#' @description
#' Bering Sea pollock data from 2011
#'
#' @format `PollockData`
#' A dataframe with 36 columns and 839 rows:
#' \describe{
#' }
#'
#' @usage data(PollockData)
#'
"PollockData"

"windLease"
5 changes: 2 additions & 3 deletions R/map_viewer.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,12 @@ map_viewer <- function(dat, project, spat, avd, avm, num_vars, temp_vars, id_var
#' avd = 'ZoneID', avm = 'TEN_ID', num_vars = 'LANDED_thousands',
#' temp_vars = 'DATE_TRIP', lon_start = 'previous_port_lon',
#' lat_start = 'previous_port_lat', lon_end = 'DDLON',
#' lat_end = 'DDLAT', id_vars = 'previous_namelsad')
#' lat_end = 'DDLAT')
#'
#' # Plot observed fishing locations
#' map_viewer(scallopMainDataTable, 'scallop', "scallopTMSSpatTable",
#' avd = 'ZoneID', avm = 'TEN_ID', num_vars = 'LANDED_thousands',
#' temp_vars = 'DATE_TRIP', lon_start = 'DDLON', lat_start = 'DDLAT',
#' id_vars = 'previous_namelsad')
#' temp_vars = 'DATE_TRIP', lon_start = 'DDLON', lat_start = 'DDLAT')
#'
#' #Plot haul path
#' map_viewer(pollockMainDataTable, 'pollock', spat=spatdat, avd='NMFS_AREA',
Expand Down
4 changes: 0 additions & 4 deletions R/view_grid_dat.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ view_grid_dat <- function(grid, project, lon, lat, value, split_by = NULL,
#' @export
#' @import ggplot2
#' @importFrom rlang sym
#' @examples
#' \dontrun{
#' view_grid_dat('SST', "pollock", "lon", "lat", value = "analysed_sst")
#' }
#'

out <- data_pull(grid, project)
Expand Down
Binary file removed data/ExpData.RData
Binary file not shown.
Binary file removed data/PollockData.rda
Binary file not shown.
Binary file removed data/SST.RData
Binary file not shown.
Binary file removed data/alaskaNMFSAreas.rda
Binary file not shown.
Binary file removed data/alaskaPorts.rda
Binary file not shown.
Binary file modified data/scallop.rda
Binary file not shown.
Loading

0 comments on commit 77cf615

Please sign in to comment.