% 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
)
}
\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"}):
the first 4 letters are used as \emph{GENUS}, the last 3 as
\emph{SPECIES}.}

\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"}.}

\item{verbose}{Logical (default \code{TRUE}); prints progress messages.}
}
\value{
A list of two \code{data.frame}s:
  \describe{
    \item{\code{merge_TA_TB}}{One row per haul 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.}
  }

A \code{list} containing two data frames: the first with merged TA-TB-TC data at the haul level, and the second with the length-frequency distribution.
}
\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 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.

\strong{Data Validation:}
The function automatically performs syntactic data validation using internal
routines based on \code{RoME} version 0.2.3 logic to ensure data integrity
and standardisation.

\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 Ã— 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{
# Use internal data

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
)
mTATB <- m[[1]]   # TAâ€“TB merged table
mTATC <- m[[2]]   # TAâ€“TC merged table

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