clear all
close all
clc

sb50m_dir = 'D:\werf_je\Werk\1.201.819-Proefsuppletie_Galgeplaat_2e_evaluatie\Data\SB50m';
i = 0;

%Data controleren!!!
datum = {'07-05-2008','14-12-2008',...
         '14-03-2009','21-09-2009'};

for file = dir(fullfile(sb50m_dir,'*.txt'))',
    %clear data
    clear noc nor xorg yorg grid_res x y z
    i = i + 1;
    
    fid = fopen(fullfile(sb50m_dir,file.name));

    fscanf(fid,'%s',1);
    noc = fscanf(fid,'%f',1);
    
    fscanf(fid,'%s',1);
    nor = fscanf(fid,'%f',1);
    
    fscanf(fid,'%s',1);
    xorg = fscanf(fid,'%f',1);
    
    fscanf(fid,'%s',1);
    yorg = fscanf(fid,'%f',1);
    
    fscanf(fid,'%s',1);
    grid_res = str2num(fscanf(fid,'%s',1));

    x = xorg:grid_res:xorg+grid_res*(noc-1);
    y = yorg+grid_res*(nor-1):-grid_res:yorg;
    
    fgetl(fid);
    fgetl(fid);

    z = fscanf(fid,'%f',[noc nor]);
    fclose(fid);
    z = z';
    z(find(z == -9999)) = NaN;
    z = 0.01*z;
    sb50m_data.datum(i) = {datenum(datum(i),'dd-mm-yyyy')};
    sb50m_data.naam(i) = {strrep(file.name,'.txt','')};
    sb50m_data.x(i) = {x};
    sb50m_data.y(i) = {y};
    sb50m_data.z(i) = {z};   
    
    h = figure;
    contourf(sb50m_data.x{i},sb50m_data.y{i},sb50m_data.z{i},22)
    hold on
    xlabel('xRD (m)')
    ylabel('yRD (m)')
    axis equal   
    axis([0.530 0.570 3.96 4.00]*1e5)
    title(datestr(sb50m_data.datum{i}))
    caxis([-12 1])
    h2 = colorbar;
    set(h2,'YTick',[-12:1:1])
    pause
    close(h);
end

save MAT\sb50m_data sb50m_data