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

\item{tc}{A `data.frame`/`data.table` containing a full TC dataset
(columns listed in `BioIndex::TC_cols`).}

\item{species}{Character. MEDITS 7-letter code (e.g. `"ARISFOL"`) or
`"all"`. For a MEDITS code, the first 4 characters are
interpreted as *GENUS* and the last 3 as *SPECIES*. If
`species = "all"`, no species filter is applied to TC and
all length-frequency records are retained.}

\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
`"mergeTATC_<GENUS><SPECIES>.csv"` to
`file.path(wd, "output")`.}

\item{verbose}{Logical; if `TRUE` (default) prints progress messages.}

\item{rome_checks}{Logical; if `TRUE`, runs the internal syntactic
validation checks based on \code{RoME} version 0.2.3.
Default is `FALSE`.}
}
\value{
A `data.frame` in which each row represents one length-class
        (and sex/maturity) sampled in a haul, including:
        * **TA** metadata
        * **TC** length-frequency counts and weights
        * Calculated fields: mean lat/lon, swept area, depth stratum,
          raising factor, density (`N_h`, `N_km2`) and biomass
          (`kg_h`, `kg_km2`)
}
\description{
Links haul metadata (TA) with length-frequency samples (TC),
enabling the analysis of population demographic structure and the
reconstruction of length-frequency distributions (LFD).
The function keeps only valid hauls, optionally runs the relevant quality
checks (times, positions, wing opening, haul/date consistency) using
internal validation functions (based on \code{RoME} version 0.2.3),
computes swept area, depth stratum, raising factors,
abundance/biomass indicators, and optionally writes the merged data.
}
\details{
The code is identical to the official BioIndex routine, except for the
targeted handling of `species = "all"` and the optional execution of the
RoME-like syntactic checks.

\strong{Data Validation:}
RoME-like syntactic checks are skipped by default. Set
`rome_checks = TRUE` to run the internal validation routines based on
\code{RoME} v0.2.3.

\strong{Species selection:}
If `species = "all"`, the TC table is not filtered by `GENUS` and
`SPECIES`; all species records are retained.

\strong{Optimisations:}
Includes two micro-optimisations: vectorised handling of missing `"NA NA"` entries
and a single depth-stratum loop. Numerical results remain unchanged.
}
\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("TC", package = "BioIndex")
m_tc <- merge_TATC(
  ta       = TA,
  tc       = TC,
  species  = "ARISFOL",
  country  = "ESP",
  wd       = tempdir(),
  save=FALSE
)
m_tc_all <- merge_TATC(
  ta       = TA,
  tc       = TC,
  species  = "all",
  country  = "all",
  wd       = tempdir(),
  save=FALSE
)
head(m_tc)
}
\seealso{
[BioIndex] package documentation.
}
