function OUT = vdm_bounds_test(varsfile, ncfile)

%% Read the vars data file.
% This assumes that only the default fields are outputed.

[associations, conceptNames, depths, imageUrls, latitudes, longitudes, ...
    observationIds, recordedDates, timecodes, videoArchiveNames] = textread( ...
    varsfile, '%s%s%s%s%s%s%s%s%s%s', ...
    'headerlines', 10, 'delimiter', '\t');

depths = str2double(depths);
latitudes = str2double(latitudes);
longitudes = str2double(longitudes);
%observationIds = str2double(observationIds);
%recordedDates = datestr(recordedDates, 31);

%% Read the netcdf file
lat = nc_varget(ncfile, 'latitude');
minLat = min(lat);
maxLat = max(lat);
lon = nc_varget(ncfile, 'longitude');
minLon = min(lon);
maxLon = max(lon);

%% Check that the data point is within the bounds of the ncfile
altitudes = ones(size(depths)) * NaN;
bottomDepths = ones(size(depths)) * NaN;
ok = zeros(size(depths));
for i = 1:length(depths)
    ok(i) = (latitudes(i) >= minLat) & (latitudes(i) <= maxLat) & ...
        (longitudes(i) >= minLon) & (longitudes(i) <= maxLon) & ~isnan(depths(i));
    if ok(i)
        iy = near(lat, latitudes(i));
        ix = near(lon, longitudes(i));

        nix = ix - 1; % NEtcdf are 0 based indices
        niy = iy - 1; % NEtcdf are 0 based indices
        % dem is -  down, depth is + down
        bottomDepths(i) = -(nc_varget(ncfile, 'elevation', [niy nix] , [1 1]));
        altitudes(i) = bottomDepths(i) - depths(i);
    end
end

%% Return values
fprintf(1, 'Found %f annotations within the maps bounds\n', max(size(find(ok ~= 0))));
good = find(altitudes(find(ok)) <= 40);
fprintf(1, 'Found %f annotations within the maps bounds and less than 40 meters altitude\n', max(size(good)));
OUT.associations = associations;
OUT.conceptNames = conceptNames;
OUT.depths = depths;
OUT.imageUrls = imageUrls;
OUT.latitudes = latitudes;
OUT.longitudes = longitudes;
OUT.observationIds = observationIds;
OUT.recordedDates = recordedDates;
OUT.timecodes = timecodes;
OUT.videoArchiveNames = videoArchiveNames;
OUT.inBounds = ok;
OUT.altitudes = altitudes;
OUT.bottomDepths = bottomDepths;
