function [grid] = updateLidarGrid(grid, metaDataDir)
%UPDATEGRID
%
% Ben often calls this the meta data.  There is one data point per profile.
% These are the variables that are added:
% grid.id                       profile number
% grid.shorePos                 distance from shoreline point to profile origin
% grid.shoreNorth               northing of shoreline position
% grid.shoreEast                easting of shoreline position
% grid.actionCode               this code tells us more about data 
% grid.bias                     
% grid.significant_wave_height 
% grid.deep_water_wave_length 
% grid.beach_slope 

% TODO add more variables...

%See also:

    
    % First part: update grid with information from matfiles which is time
    % independant
    
    % create a new transect structure
    transect = createlidartransectstruct();
    
    metadata = dir([metaDataDir '*.mat']);
    for i = 1:length(metadata)
        load([metaDataDir metadata(i).name]);
        disp(['Extracting info from ' metadata(i).name])
    end
    
    % identify transect ID
    transect.id = shorePrNum';    
    
    % load all other metadata from matfile
    transect.meanHighWater = Z_mhw;
    transect.shorepos = shorePos;
    transect.shoreNorth = shoreNorth;
    transect.shoreEast = shoreEast;
    transect.significant_wave_height = Hs;
    transect.deep_water_wave_length = L0;
    % this following line is wrong, afarris@usgs.gov 2009oct9; the netcdf files need to be re-made
    % transect.beach_slope = tanb;
    % this is the correct slope:
    transect.beach_slope = slope;
    transect.actionCode = actionCode; 
    transect.bias = bias; 
    if exist('shoreNorth1880')
    transect.shoreNorth1880 = shoreNorth1880;
    transect.shoreEast1880 = shoreEast1880;
    transect.shoreNorth1930 = shoreNorth1930;
    transect.shoreEast1930 = shoreEast1930;
    end
    
    % find points in the transect which are also in the grid
    [c, ia, ib] = intersect(transect.id, grid.id);
    if (length(c) ~= length(grid.id))
        warning('found grids which are not present in meta information or vice versa'); 
    end
    nnodata = length(setdiff(transect.id, grid.id));
    if (nnodata)
        msg = sprintf('found %d transects in metadata without data', nnodata);
        warning(msg);
    end
    nnodata = length(setdiff(grid.id, transect.id));
    if (nnodata)
        msg = sprintf('found %d transects in data without metadata', nnodata);
        warning(msg);
    end              

    % update grid with additional metadata
    grid.meanHighWater = transect.meanHighWater(ia)';
    grid.shorepos = transect.shorepos(ia)';
    grid.shoreNorth = transect.shoreNorth(ia)';
    grid.shoreEast = transect.shoreEast(ia)';
    grid.significant_wave_height = transect.significant_wave_height(ia)';
    grid.deep_water_wave_length = transect.deep_water_wave_length(ia)';
    grid.beach_slope = transect.beach_slope(ia)';
    grid.bias = transect.bias(ia)';
    if exist('shoreNorth1880')
    grid.shoreNorth1880 = transect.shoreNorth1880(ia)';
    grid.shoreEast1880 = transect.shoreEast1880(ia)';
    grid.shoreNorth1930 = transect.shoreNorth1930(ia)';
    grid.shoreEast1930 = transect.shoreEast1930(ia)';
    end
    
end