<commit_before>################
#
# Code to generate annual report tables for the Coho Technical Committee.
#
# <NAME>
# <EMAIL>
# January 14, 2015
# Using: http://google-styleguide.googlecode.com/svn/trunk/google-r-style.html
#
#
################

kAnnualReportLibVersion <- "v1.2"

kCanadaGroupCode <- "BC"
kUSGroupCode <- "Southern U.S."
kTotalFisheryName <- "TOTAL"

kCapMethodImu <- "imu" #US Inside Management Unit
kCapMethodOmu <- "omu" #US Outside Management Unit
kCapMethodFixed <- "fixed" #Fixed Canadian ER Management Unit

kUSCapMethods <- c(kCapMethodImu, kCapMethodOmu)
kCdnCapMethods <- c(kCapMethodFixed)

kPstLowStatusER <- 0.2
kPstModerateStatusER <- 0.4
kPstAbundantStatusER <- 0.6

kStatusAbundantCode <- "A"
kStatusModerateCode <- "M"
kStatusLowCode <- "L"

source(file.path(source.lib.dir, "Util.r"))
source(file.path(source.lib.dir, "FramDb.r"))

required.packages <- c("RODBC")
InstallRequiredPackages(required.packages)

GetColumnSums <- function(data, label.column, label.value) {
  column.sums <- t(colSums(data))
  label <- data.frame(label.value)
  names(label) <- label.column
  column.sums <- cbind(label, column.sums)
}

GetPscMortality <- function(fishery.mortality, psc.fishery, psc.fishery.map, psc.stock, psc.stock.map) {
  psc.stock <- psc.stock[,names(psc.stock) %in% c("psc.stock.id", "psc.stock.name", "psc.stock.order")]
  
  psc.full.fishery <- merge(psc.fishery, psc.fishery.map, by=c("psc.fishery.id"))
  
  psc.full.stock <- merge(psc.stock, psc.stock.map, by=c("psc.stock.id"))
  psc.full.fishery <- merge(psc.full.fishery, psc.full.stock, all=TRUE, by=c())
  
  
  psc.full.fishery <- merge(fishery.mortality, psc.full.fishery, by.x=c("fishery.id", "stock.id"), by.y=c("fram.fishery.id", "fram.stock.id"), all.y=TRUE)
  
  psc.full.fishery$total.mortality[is.na(psc.full.fishery$total.mortality)] <- 0
  
  aggr.factors <- with(psc.full.fishery,  
                       list(run.id=run.id, 
                            run.year=run.year, 
                            group.code=group.code,
                            psc.fishery.name=psc.fishery.name, 
                            psc.fishery.order=psc.fishery.order,
                            psc.stock.name=psc.stock.name,
                            psc.stock.order=psc.stock.order))
  psc.fishery.mortality <- aggregate(data.frame(fishery.mortality=psc.full.fishery$total.mortality), aggr.factors, sum)
  
  psc.fishery.mortality <- psc.fishery.mortality[order(psc.fishery.mortality$psc.stock.order, psc.fishery.mortality$psc.fishery.order),]
  return(psc.fishery.mortality)
}

GetPscEscapement <- function(escapement, psc.stock, psc.stock.map) {
  psc.stock.tbl <- merge(psc.stock, psc.stock.map, by=c("psc.stock.id"))
  
  psc.escapement <- merge(escapement, psc.stock.tbl, by.x=c("stock.id"), by.y=c("fram.stock.id"), all.y=TRUE)
  
  
  unmatched <- merge(escapement, psc.stock.tbl, by.x=c("stock.id"), by.y=c("fram.stock.id"), all.x=TRUE)
  psc.escapement$escapement[is.na(psc.escapement$escapement)] <- 0
  
  aggr.factors <- with(psc.escapement,  
                       list(run.id=run.id, 
                            run.year=run.year,
                            psc.stock.id=psc.stock.id,
                            psc.stock.name=psc.stock.name,
                            psc.stock.order=psc.stock.order))
  psc.escapement <- aggregate(data.frame(escapement=psc.escapement$escapement), aggr.factors, sum)
  
  psc.escapement <- psc.escapement[order(psc.escapement$psc.stock.order),]
  return(psc.escapement)
}

BuildFisheryTable <- function(fishery.mortality, column.name) {
  stock.names <- unique(fishery.mortality[, c("psc.stock.order", "psc.stock.name")])
  stock.names <- stock.names[order(stock.names$psc.stock.order), "psc.stock.name"]
  
  fisheries <- unique(fishery.mortality[,c("psc.fishery.order", "group.code", "psc.fishery.name")])
  
  for(col.idx in 1:length(stock.names)) {
    stock.frame <- data.frame(
      psc.fishery.name=fishery.mortality$psc.fishery.name[fishery.mortality$psc.stock.name == stock.names[col.idx]],<commit_msg>Add a function comment for getting PSC Escapement<commit_after>################
#
# Code to generate annual report tables for the Coho Technical Committee.
#
# <NAME>
# <EMAIL>
# January 14, 2015
# Using: http://google-styleguide.googlecode.com/svn/trunk/google-r-style.html
#
#
################

kAnnualReportLibVersion <- "v1.2"

kCanadaGroupCode <- "BC"
kUSGroupCode <- "Southern U.S."
kTotalFisheryName <- "TOTAL"

kCapMethodImu <- "imu" #US Inside Management Unit
kCapMethodOmu <- "omu" #US Outside Management Unit
kCapMethodFixed <- "fixed" #Fixed Canadian ER Management Unit

kUSCapMethods <- c(kCapMethodImu, kCapMethodOmu)
kCdnCapMethods <- c(kCapMethodFixed)

kPstLowStatusER <- 0.2
kPstModerateStatusER <- 0.4
kPstAbundantStatusER <- 0.6

kStatusAbundantCode <- "A"
kStatusModerateCode <- "M"
kStatusLowCode <- "L"

source(file.path(source.lib.dir, "Util.r"))
source(file.path(source.lib.dir, "FramDb.r"))

required.packages <- c("RODBC")
InstallRequiredPackages(required.packages)

GetColumnSums <- function(data, label.column, label.value) {
  column.sums <- t(colSums(data))
  label <- data.frame(label.value)
  names(label) <- label.column
  column.sums <- cbind(label, column.sums)
}

GetPscMortality <- function(fishery.mortality, psc.fishery, psc.fishery.map, psc.stock, psc.stock.map) {
  psc.stock <- psc.stock[,names(psc.stock) %in% c("psc.stock.id", "psc.stock.name", "psc.stock.order")]
  
  psc.full.fishery <- merge(psc.fishery, psc.fishery.map, by=c("psc.fishery.id"))
  
  psc.full.stock <- merge(psc.stock, psc.stock.map, by=c("psc.stock.id"))
  psc.full.fishery <- merge(psc.full.fishery, psc.full.stock, all=TRUE, by=c())
  
  
  psc.full.fishery <- merge(fishery.mortality, psc.full.fishery, by.x=c("fishery.id", "stock.id"), by.y=c("fram.fishery.id", "fram.stock.id"), all.y=TRUE)
  
  psc.full.fishery$total.mortality[is.na(psc.full.fishery$total.mortality)] <- 0
  
  aggr.factors <- with(psc.full.fishery,  
                       list(run.id=run.id, 
                            run.year=run.year, 
                            group.code=group.code,
                            psc.fishery.name=psc.fishery.name, 
                            psc.fishery.order=psc.fishery.order,
                            psc.stock.name=psc.stock.name,
                            psc.stock.order=psc.stock.order))
  psc.fishery.mortality <- aggregate(data.frame(fishery.mortality=psc.full.fishery$total.mortality), aggr.factors, sum)
  
  psc.fishery.mortality <- psc.fishery.mortality[order(psc.fishery.mortality$psc.stock.order, psc.fishery.mortality$psc.fishery.order),]
  return(psc.fishery.mortality)
}

GetPscEscapement <- function(escapement, psc.stock, psc.stock.map) {
  # Sum up the escapement from a fram run to PSC Stocks
  #
  # Args:
  #   escapement: Escapement values from a FRAM run
  #   psc.stock: The list of PSC stocks
  #   psc.stock.map: Relates FRAM stocks to PSC stocks
  #
  # Returns:
  #   A data frame with PSC escapement
  #
  # Exceptions:
  #   None
  #
  
  psc.stock.tbl <- merge(psc.stock, psc.stock.map, by=c("psc.stock.id"))
  
  psc.escapement <- merge(escapement, psc.stock.tbl, by.x=c("stock.id"), by.y=c("fram.stock.id"), all.y=TRUE)
  
  
  unmatched <- merge(escapement, psc.stock.tbl, by.x=c("stock.id"), by.y=c("fram.stock.id"), all.x=TRUE)
  psc.escapement$escapement[is.na(psc.escapement$escapement)] <- 0
  
  aggr.factors <- with(psc.escapement,  
                       list(run.id=run.id, 
                            run.year=run.year,
                            psc.stock.id=psc.stock.id,
                            psc.stock.name=psc.stock.name,
                            psc.stock.order=psc.stock.order))
  psc.escapement <- aggregate(data.frame(escapement=psc.escapement$escapement), aggr.factors, sum)
  
  psc.escapement <- psc.escapement[order(psc.escapement$psc.stock.order),]
  return(psc.escapement)
}

BuildFisheryTable <- function(fishery.mortality, column.name) {
  stock.names <- unique(fishery.mortality[, c("psc.stock.order", "psc.stock.name")])
  stock.names <- stock.names[order(stock.names$psc.stock.order), "psc.stock.name"]
  
  fisheries <- unique(fishery.mortality[,c("psc.fishery.order", "group.code", "psc.fishery.name")])
  
  for(col.idx in 1:length(stock.names)) {
    stock.frame <- data.frame(
      psc.fishery.name=fishery.mortality$psc.fishery.name[fishery.mortality$psc.stock.name == stock.names[col.idx]],
{'fraction_non_alphanumeric': 0.16633290140181411, 'fraction_numerical': 0.004005183178230651, 'mean_word_length': 3.1054158607350097, 'pattern_counts': {'":': 0, '<': 95, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4187867', 'n_tokens_mistral': 3483, 'n_tokens_neox': 3256, 'n_words': 433}