Skip to content

Commit f40c348

Browse files
author
Stephanie Owen
committed
get_commercial_data indicator script
1 parent 695a53d commit f40c348

1 file changed

Lines changed: 60 additions & 67 deletions

File tree

data-raw/scripts/Economic_ESP_Indicators_Commercial.R renamed to R/get_commercial_data.R

Lines changed: 60 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,62 @@
1-
## Commercial ESP indicators Script
2-
## translated from Stata code 2/24/2026
3-
## Owner: Samantha Werner
4-
5-
#overview-- This code creates six economic commercial fishing indicators used in ESP work.
6-
# The code below pulls data, deflates any monetary values and formats data to what is needed for the time series plots.
7-
8-
#Indicators created: Commercial Landings (LBS): The total weight of the species landed (e.g., Commercial_LONGFINSQUID_Landings_LBS).
9-
10-
#Number of Commercial Vessels: The count of unique permits landing that species (e.g., N_Commercial_Vessels_Landing_LONGFINSQUID).
11-
#Average Price per Pound: The average annual price, winsorized to handle outliers and adjusted for inflation (e.g., AVGPRICE_LONGFINSQUID_2024_DOLlb).
12-
#Total Annual Revenue: The total value of all landings for that species, adjusted for inflation (e.g., TOTALANNUALREV_LONGFINSQUID_2024Dols).
13-
#Average Revenue per Vessel: The average revenue earned per permit per year, adjusted for inflation (e.g., AVGVESREVperYr_LONGFINSQUID_2024_DOLlb).
14-
#Average Annual Diesel Price: The price of Ultra-Low-Sulfur No. 2 Diesel (from FRED), adjusted for inflation (e.g., AVGANNUAL_DIESEL_PRICE2024dols).
15-
16-
## This uses CFDERS data but may need to be updated to CAMs
17-
18-
19-
20-
#######################Running the code steps##########################
21-
#1. Change top of the code to update:
22-
# - Oracle log in information
23-
# - The time series you want
24-
# - The species (nespp3) and how you want your species to be named within the end file (sppname)
25-
# - the year you want to deflate to
26-
27-
## before running!!!#
28-
# - make sure you are connected to VPN
29-
#-- ensure all packages are installed
30-
# -- you followed the steps above
31-
#-- you have a folder in your directory called "data"
32-
33-
34-
#install.packages("ROracle")
35-
#install.packages("DescTools")
1+
#' Pull Commercial Fisheries Data from Oracle
2+
#'
3+
#' translated from Stata code 2/24/2026
4+
#' Owner: Samantha Werner
5+
#'
6+
#' overview-- This code creates six economic commercial fishing indicators used in ESP work.
7+
#' The code below pulls data, deflates any monetary values and formats data to what is needed for the time series plots.
8+
#' Must have access to the 'NEFSC_GARFO' schema in Oracle to run this code.
9+
#' Indicators created: Commercial Landings (LBS): The total weight of the species landed (e.g., Commercial_LONGFINSQUID_Landings_LBS).
10+
#'
11+
#' Number of Commercial Vessels: The count of unique permits landing that species (e.g., N_Commercial_Vessels_Landing_LONGFINSQUID).
12+
#' Average Price per Pound: The average annual price, winsorized to handle outliers and adjusted for inflation (e.g., AVGPRICE_LONGFINSQUID_2024_DOLlb).
13+
#' Total Annual Revenue: The total value of all landings for that species, adjusted for inflation (e.g., TOTALANNUALREV_LONGFINSQUID_2024Dols).
14+
#' Average Revenue per Vessel: The average revenue earned per permit per year, adjusted for inflation (e.g., AVGVESREVperYr_LONGFINSQUID_2024_DOLlb).
15+
#' Average Annual Diesel Price: The price of Ultra-Low-Sulfur No. 2 Diesel (from FRED), adjusted for inflation (e.g., AVGANNUAL_DIESEL_PRICE2024dols).
16+
#'
17+
#' This uses CFDERS data but may need to be updated to CAMs
18+
#'
19+
#' before running!!!
20+
#' make sure you are connected to VPN
21+
#' ensure all packages are installed
22+
#' you have a folder in your directory called "data"
23+
#'
24+
#' @param ora_id username for Oracle connection (in quotation marks)
25+
#' @param oraprod_pw password for Oracle connection (in quotation marks)
26+
#' @param spp_name the name of the species you want to pull (e.g., "LONGFINSQUID")
27+
#' @param nespp3_codes the NESPP3 codes for the species you want to (e.g., "('801')") - note the single quotes inside the string for SQL
28+
#' @param start_year the first year you want to pull (e.g., 1996)
29+
#' @param end_year the last year you want to pull (e.g., 2025)
30+
#' @param deflate_yr the year you want to deflate to (e.g, 2025)
31+
#' @export
32+
#'
3633

3734
library(ROracle)
3835
library(DBI)
3936
library(fredr)
4037
library(tidyverse)
4138
library(DescTools) # For Winsorize
4239

43-
######################################################################
44-
############################update code below to match the years, species and user information#############
45-
#####################################################################
46-
47-
# 1. Setup Credentials
48-
ora_id <- "INSERT USERNAME HERE"
49-
oraprod_pw <- "INSERT PASSWORD HERE"
40+
get_commercial_data <- function(
41+
ora_id,
42+
oraprod_pw,
43+
spp_name,
44+
nespp3_codes,
45+
start_year,
46+
end_year,
47+
deflate_yr
48+
) {
5049

51-
spp_name <- "LONGFINSQUID" # Replace with your actual species name
52-
nespp3_codes <- "('801')" # Note the single quotes inside the string for SQL
53-
54-
START.YEAR <- 1996
55-
END.YEAR <- 2025
56-
57-
## set year you would like to deflate to
58-
deflate_yr <- 2025
5950

6051
# This looks for a folder named 'data' then 'intermediate' inside your current directory
6152
data_intermediate <- file.path("data/intermediate")
6253

63-
# Set your API key for FRED data (deflation) you may need to log into FRED API Keys and request a new one. https://fred.stlouisfed.org/docs/api/api_key.html
64-
fredr_set_key("a09e5d083681605146191f4996992c6e")
6554

6655
#################################################################
6756
##no editiing should be needed past this point
6857
#####################################################
69-
# 2. Build the connection string
70-
shost <- "nefsc-prod-01-db.nmfs.noaa.gov"
71-
port <- 1521
72-
ssid <- "NEFSC_DB_PROD.nefscproddbsn.nefscprodvcn.oraclevcn.com"
58+
59+
source("\\nefscdata\\SOE_ESP_Data\\ESPs\\connect_socioeco_oracle.r")
7360

7461
# Using the name consistent with your loop
7562
nefscusers.connect.string <- paste0(
@@ -162,17 +149,17 @@ print(head(Nvessels_final))
162149
##################average prices##################
163150

164151
#1. Pull Price Data from Oracle ---
165-
# Uses the 'conn' object you already established
166-
query_price <- paste0(
167-
"SELECT SPPVALUE, SPPLNDLB, YEAR FROM NEFSC_GARFO.CFDERS_ALL_YEARS ",
168-
"WHERE NESPP3 IN ", nespp3_codes,
169-
" AND YEAR BETWEEN ", START.YEAR, " AND ", END.YEAR
170-
)
152+
# Uses the 'conn' object you already established
153+
query_price <- paste0(
154+
"SELECT SPPVALUE, SPPLNDLB, YEAR FROM NEFSC_GARFO.CFDERS_ALL_YEARS ",
155+
"WHERE NESPP3 IN ", nespp3_codes,
156+
" AND YEAR BETWEEN ", START.YEAR, " AND ", END.YEAR
157+
)
171158

172159
price_raw <- dbGetQuery(conn, query_price)
173160

174161
#--- 2. Calculate Average Annual Prices (Manual Winsorize) ---
175-
price_annual <- price_raw %>%
162+
price_annual <- price_raw %>%
176163
mutate(price_lb = SPPVALUE / SPPLNDLB) %>%
177164
# Remove Infinity or NA if pounds were 0
178165
filter(is.finite(price_lb)) %>%
@@ -329,8 +316,14 @@ write.csv(
329316
# 5. View a summary of what you appended
330317
print(table(final_master_file$INDICATOR_NAME))
331318

319+
}
332320

333-
334-
335-
336-
321+
get_commercial_data(
322+
ora_id = "user",
323+
oraprod_pw = "password",
324+
spp_name = "LONGFINSQUID",
325+
nespp3_codes = "('801')",
326+
start_year = 1996,
327+
end_year = 2025,
328+
deflate_yr = 2025
329+
)

0 commit comments

Comments
 (0)