function lidartransect2netcdf(filename, transectStruct)
%TRANSECT2NETCDF


%% Lookup variables
% This assumes a grid already has been saved to the file
yearArray = nc_varget(filename, 'time'); disp(num2str(yearArray));
transectIdArray = nc_varget(filename, 'id');
crossShoreCoordinateArray = nc_varget(filename, 'cross_shore');
try
    missing = nc_attget(filename, 'altitude', '_FillValue');
catch
    missing = -9999;
end
%% Write data to file
% Loop over time first, this is most efficient if it's the slowest
% moving dimension.
for i = 1 : length(yearArray)
    time = yearArray(i);
    transectsForYearStruct = transectStruct([transectStruct.time] == time);
    datablock(1,1,:) = repmat(missing, 1, length(crossShoreCoordinateArray));

    for j = 1 : length(transectIdArray)
        id = transectIdArray(j);
        transect = transectsForYearStruct([transectsForYearStruct.id] == id);
        if ~isempty(transect)
            
            [transectdata, lon, lat, x, y, cross_shore_distance, regression] = deal(datablock);
            [c, ia, ib] = intersect(crossShoreCoordinateArray, [1 : length(transect.crossShoreCoordinate)]');
            
            transectdata(1,1,ia) = transect.altitude(ib);
            lon(1,1,ia) = transect.lon(ib);
            lat(1,1,ia) = transect.lat(ib);
            x(1,1,ia) = transect.x(ib);
            y(1,1,ia) = transect.y(ib);
            cross_shore_distance(1,1,ia) = transect.crossShoreCoordinate(ib);
            regression(1,1,ia) = transect.regression(ib);

%             nc_varput(filename, 'time', time, [i-1], [1]);
            nc_varput(filename, 'altitude', transectdata, [i-1, j-1, 0], [1 1 length(datablock)]);
            nc_varput(filename, 'cross_shore_distance', cross_shore_distance, [i-1, j-1, 0], [1 1 length(datablock)]);
            nc_varput(filename, 'lon', lon, [i-1, j-1, 0], [1 1 length(datablock)]);
            nc_varput(filename, 'lat', lat, [i-1, j-1, 0], [1 1 length(datablock)]);
            nc_varput(filename, 'x', x, [i-1, j-1, 0], [1 1 length(datablock)]);
            nc_varput(filename, 'y', y, [i-1, j-1, 0], [1 1 length(datablock)]);
            nc_varput(filename, 'regression', regression, [i-1, j-1, 0], [1 1 length(datablock)]);
        end
    end
end

end