function [ travel_times ] = travel_time(source, receivers)
% TRAVEL_TIME is a function to get the travel times from
% a source point to an array of receiver points. The times
% are looked up in a Map container loaded from the file
% traveltimes.txt.
%
% Inputs: source = vector of length 2 holding [lat lon]
%                  coordinates of the source point
%         receivers = matrix of size nx2 holding n pairs
%                     of lat/lon values for all receiver points
%         table = Map container holding travel time data,
%                 must be loaded using the load_traveltimes function
% Output: travel_times = vector of length n holding travel
%                        times between source point and all
%                        receiver points

    [nreceivers y] = size(receivers);

    assert(length(source) == 2);
    assert(y == 2);

    travel_times = zeros(nreceivers, 1);
    
    try
        M = load_traveltimes('traveltimes.txt');
    catch
        error('You must save your travel times in the file traveltimes.txt');
    end

    for i = 1:nreceivers
        s = num2str([source receivers(i,:)]);
        try
            travel_times(i) = M(s);
        catch
            error('Travel time not found in file.');
        end
    end

end


function [ M ] = load_traveltimes( filename )
% LOAD_TRAVELTIMES is a function to load a text file
% with source/receiver locations and travel times between
% them into a hash table for fast lookup
%
% Input: filename (text file to be loaded, must be formatted:
%         sourcelat sourcelon reclat reclon traveltime
% Output: Map container mapping strings to travel times
%         strings are generated using num2str applied to
%         the length four vector of source and receiver
%         locations

    traveltimes = load(filename);

    [x y] = size(traveltimes);

    assert(y == 5);

    M = containers.Map('KeyType','char','ValueType','double');

    for i = 1:x
        M(num2str(traveltimes(i,1:4))) = traveltimes(i,5);
    end

end