function lidartransect2netcdfblock(filename, transectStruct)
%TRANSECT2NETCDF


%% Lookup variables
% This assumes a grid already has been saved to the file
yearArray = nc_varget(filename, 'time');
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);
    %block to store to netcdf. Storing more data at once is faster. But
    %it will require more memory.
    transectsForYearStruct = transectStruct([transectStruct.time] == time);
    datablock(1,:,:) = repmat(missing, length(transectIdArray), length(crossShoreCoordinateArray));

    [lon, lat, x, y, cross_shore_distance, regression] = deal(datablock);

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

    end
    nc_varput(filename, 'time', time, [i-1], [1]);

    nc_varput(filename, 'altitude', datablock, [i-1, 0, 0], [size(datablock)]);
    
    nc_varput(filename, 'cross_shore_distance', cross_shore_distance, [i-1, 0, 0], [size(datablock)]);

    nc_varput(filename, 'lon', lon, [i-1, 0, 0], [size(datablock)]);

    nc_varput(filename, 'lat', lat, [i-1, 0, 0], [size(datablock)]);

    nc_varput(filename, 'x', x, [i-1, 0, 0], [size(datablock)]);

    nc_varput(filename, 'y', y, [i-1, 0, 0], [size(datablock)]);
    
    nc_varput(filename, 'regression', regression, [i-1, 0, 0], [size(datablock)]);

end

end