function [lat, lon, date, diff, trackingMethod] = findPositionByDatevec(pTrajectory, pDatevec)
%FINDPOSITIONBYDATEVEC Searches the next position close to a datevec in the format Y m d H M s.
%   Returns:
%       diff (double): The difference between pDatevec and the trajectory
%                      position.

%% Convert datevec to rafos date
rafosDate = artoa.convert.datevec2rd(pDatevec);

%% Calculate the difference
timeDifferences = pTrajectory.date - rafosDate;
[~, minimum] = min(abs(timeDifferences));

%% Find the tracking method
selection = (pTrajectory.trackParameter.soundsourceCombinations.combinationBegin <= pTrajectory.date(minimum)) ...
    & (pTrajectory.trackParameter.soundsourceCombinations.combinationEnd >= pTrajectory.date(minimum));
selection = find(selection);
trackingMethod = pTrajectory.trackParameter.soundsourceCombinations.trackingMethod{selection(1)};

%% Store required data
lat = pTrajectory.latitude(minimum);
lon = pTrajectory.longitude(minimum);
date = pTrajectory.date(minimum);
diff = timeDifferences(minimum);

end

