%% File to convert .xyz file (with variable named "data") to .mat file
% Created during a January 2011 introductory workshop for Matlab held at
% the Horn Point Laboratory.
%
% First download a bathymetry/topography file from the GEODAS Grid 
% Translator - Design-a-Grid website
%       http://www.ngdc.noaa.gov/mgg/gdas/gd_designagrid.html
% here. Then import the .xyz file into matlab and rename the variable "data".
% Once those steps are completed you can run this file to grid the
% latitude, longitude, and elevation triplets for contouring and plotting.
%
% Jamie Pierson, January 2011

%% Find the unique Longitude and Latitude values from the .xyz file

x=unique(data(:,1));
y=unique(data(:,2));

%% Grid the Longitude, Latitude, and Depth data
% Note that the x and y vectors that specify the locations for the newly
% gridded data  must be in opposite orientations, thus I use the ' command
% on the x data to transpose it and make it a horizontal vector.

[Lon,Lat,Depth]=griddata(data(:,1),data(:,2),data(:,3),x',y);

%% Old, gridding

% The lines below show an iterative process for gridding the data. In
% benchmark tests on my desktop (Win XP SP 3, Matlab R2010a), the old
% method below took 14.65 seconds, while using "griddata" took only 1.55
% seconds.


% % Here we are gridding the unique Lon/Lat pairs
% [Lon,Lat]=meshgrid(x,y);
% Depth=nan(size(Lon));
% tic
% for i=1:length(x)
%     for j=1:length(y)
%         
%         Depth(j,i)=data(data(:,1)==x(i) & data(:,2)==y(j),3);
%         
%     end
% end
% toc

% clearing variables we will not be using
clear i j
