clear all;

%% Add a unique gridID to your FEWS grid and save as csv file to be processed in QGIS

%Read lat and lon variables from your netcdf
lat = ncread('/Users/wiebkejager/Dropbox/BN DSS/PraiadeFaro/trainingData/run1/hazard.nc','lat');
lon = ncread('/Users/wiebkejager/Dropbox/BN DSS/PraiadeFaro/trainingData/run1/hazard.nc','lon');

dimlat = size(lat);
dimlat = dimlat(1) *dimlat(2);
dimlon = size(lon);
dimlon = dimlon(1) *dimlon(2);
Lat = reshape(lat,dimlat,1);
Lon = reshape(lon,dimlon,1);
gridID = (1:length(Lat))';
coordinates = table(Lon,Lat,gridID);
writetable(coordinates,'FEWSGrid')



%% Test <receptor>.csv file created in QGIS against your grid.

%Open your csv file
IDs = csvread('ResBuildings.csv',1,0);

% Check if receptorIDs are sorted
if(IDs(:,2) ~= sort(IDs(:,2)))
    error('ERROR: receptorIDs are not sorted in ascending order')
end

% Check if the gridIDs are correct
areas = unique(IDs(:,1));
cmap = hsv(length(areas));  
figure
for i = 1:length(areas)   
    grid_ind = IDs(logical(IDs(:,1) == i),3);
    plot(Lat(grid_ind),Lon(grid_ind),'o','Color',cmap(i,:))
    hold on
end

%% Save as <receptor.csv> file to be saved in workDir for the BN Adaptor

dlmwrite('ResBuildings.txt',IDs,'delimiter',',','precision',10)