% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/merge_TATB.r
\name{merge_TATB}
\alias{merge_TATB}
\title{Merge TA and TB tables (haul-level catches)}
\usage{
merge_TATB(
  ta,
  tb,
  species,
  country = "all",
  strata = BioIndex::strata_scheme,
  wd = NA,
  save = FALSE,
  verbose = TRUE
)
}
\arguments{
\item{ta}{A `data.frame`/`data.table` containing a full TA dataset
(columns listed in `BioIndex::TA_cols`).}

\item{tb}{A `data.frame`/`data.table` containing a full TB dataset
(columns listed in `BioIndex::TB_cols`).}

\item{species}{Character, MEDITS 7-letter code (e.g. `"ARISFOL"`).
The first 4 characters are interpreted as *GENUS* and
the last 3 as *SPECIES*.}

\item{country}{Character vector of ISO-3 country codes to keep; use
`"all"` (default) to keep every country present in `ta`.}

\item{strata}{Depth-stratum reference table; default is
`BioIndex::strata_scheme`.}

\item{wd}{Working directory.  If `NA` (the default when
`save     = FALSE`) no files are written.}

\item{save}{Logical.  If `TRUE`, writes
`"mergeTATB_<GENUS><SPECIES>.csv"` to
`file.path(wd, "output")`.}

\item{verbose}{Logical; if `TRUE` (default) prints progress messages.}
}
\value{
A `data.frame` with one row per haul and the following groups
        of variables:
        * **TA** metadata (haul position, times, depths, etc.)
        * **TB** catch totals (numbers and weight)
        * Calculated fields: mean lat/lon, swept area, depth stratum,
          density (`N_h`, `N_km2`) and biomass (`kg_h`, `kg_km2`)
}
\description{
Combines haul-level operational data (TA) with species catch data (TB),
providing the essential foundation for calculating stratified abundance (N)
and biomass (kg) indices.
The function keeps only valid hauls, performs quality checks (times,
positions, wing opening, haul/date consistency) using internal validation
functions (based on \code{RoME} version 0.2.3), computes spatial means,
swept area, depth stratum, biomass/density indicators, andâ€”optionallyâ€”writes
the merged data to disk.
}
\details{
The implementation mirrors the original BioIndex routine.

\strong{Data Validation:}
The function performs syntactic data validation using internal
implementations of the validation routines (based on \code{RoME} v0.2.3).

\strong{Optimisations:}
Includes two speed-ups: (1) vectorised replacement of missing `"NA NA"` records,
(2) a single loop over depth strata instead of a nested haul Ã— stratum
loop.  Results are identical to the reference routine.
}
\note{
This version of \code{BioIndex} uses internal validation functions based on
\strong{RoME version 0.2.3}. The package is fully self-sufficient and does
not require external validation packages to be installed.
}
\examples{
# Use internal data
data("TA", package = "BioIndex")
data("TB", package = "BioIndex")
m_tb <- merge_TATB(
  ta       = TA,
  tb       = TB,
  species  = "ARISFOL",
  country  = "ITA",
  wd       = tempdir(),
  save     = FALSE
)
head(m_tb)
}
\seealso{
[BioIndex] package documentation.
}
