% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/merge_TATBTC.r
\name{merge_TATBTC}
\alias{merge_TATBTC}
\title{Merge TA-TB and TA-TC tables (MEDITS protocol)}
\usage{
merge_TATBTC(
  ta,
  tb,
  tc,
  species,
  country = "all",
  strata = BioIndex::strata_scheme,
  wd = NA,
  save = TRUE,
  verbose = TRUE,
  rome_checks = FALSE
)
}
\arguments{
\item{ta}{A MEDITS or MEDITS-like \strong{TA} table
(columns listed in \code{BioIndex::TA_cols}).}

\item{tb}{A MEDITS or MEDITS-like \strong{TB} table
(columns listed in \code{BioIndex::TB_cols}).}

\item{tc}{A MEDITS or MEDITS-like \strong{TC} table
(columns listed in \code{BioIndex::TC_cols}).}

\item{species}{Character. MEDITS 7-letter RUBIN code (e.g. \code{"MERLMER"})
or \code{"all"}. For a RUBIN code, the first 4 letters are
used as \emph{GENUS}, the last 3 as \emph{SPECIES}. If
\code{species = "all"}, no species filter is applied to TB
or TC and all species records are retained.}

\item{country}{Character vector of MEDITS country codes to keep.
Use \code{"all"} (default) to include every country present
in \code{ta}.}

\item{strata}{A data-frame with the depth-stratification scheme adopted
by the MEDITS survey. Defaults to
\code{BioIndex::strata_scheme}.}

\item{wd}{Working directory. When \code{save = TRUE}, the merged
tables are written to \code{file.path(wd, "output")}.}

\item{save}{Logical. If \code{TRUE} (default) the function writes
\code{"mergeTATB_<GENUS><SPECIES>.csv"} and
\code{"mergeTATC_<GENUS><SPECIES>.csv"}; with
\code{species = "all"}, filenames use \code{"all_species"}.}

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

\item{rome_checks}{Logical (default \code{FALSE}); if \code{TRUE}, performs
the internal syntactic validation checks based on
\code{RoME} version 0.2.3 logic.}
}
\value{
A list of two \code{data.frame}s:
  \describe{
    \item{\code{merge_TA_TB}}{One row per haul/species with TA metadata,
          TB catch totals, depth stratum, densities and biomasses.}
    \item{\code{merge_TA_TC}}{One row per haul/length-class/sex/maturity
          with TA metadata, TC counts, depth stratum, raising factor,
          densities and biomasses.}
  }
}
\description{
This function is the primary entry point for integrating MEDITS survey data
(e.g. TA, TB, and TC tables), producing unified datasets essential for
population analysis and the estimation of demographic indicators.
The routine:
\itemize{
  \item filters hauls by validity and by country;
  \item optionally performs a full set of quality checks (times, positions,
        wing opening, haul/date consistency) using internal validation
        functions (based on \code{RoME} version 0.2.3);
  \item merges TA with TB and TA with TC, respectively;
  \item computes swept area, mean positions, depth stratum, raising
        factors, densities and biomasses;
  \item (optionally) writes the two merged tables to
        \code{file.path(wd, "output")}.
}
}
\details{
The implementation reproduces the official BioIndex workflow. The only
species-selection extension is \code{species = "all"}, which retains all
TB and TC species records instead of filtering by a single RUBIN code.

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

\strong{Optimisations:}
Two micro-optimisations are included:
\enumerate{
  \item vectorised replacement of missing \code{"NA NA"} entries
        in both merges;
  \item a single loop over depth strata (instead of haul x stratum) to
        assign \code{STRATUM_CODE}.
}
Numerical outputs 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{
data("TA", package = "BioIndex")
data("TB", package = "BioIndex")
data("TC", package = "BioIndex")
m <- merge_TATBTC(
  ta      = TA,
  tb      = TB,
  tc      = TC,
  species = "MERLMER",
  country = "all",
  wd      = tempdir(),
  verbose = TRUE
)
m_all <- merge_TATBTC(
  ta      = TA,
  tb      = TB,
  tc      = TC,
  species = "all",
  country = "all",
  wd      = tempdir(),
  verbose = TRUE
)

}
\author{
Walter Zupa \email{zupa@fondazionecoispa.org}
}
