clear all
close all
clc
warning off all

%MCTools behoevend
golf_dir = 'D:\werf_je\Werk\1.201.819-Proefsuppletie_Galgeplaat_2e_evaluatie\Data\Golven\';
lof = 18000;%grootte van het bestand (ongeveer)
i = 0;

for file = dir(fullfile(golf_dir,'*.data'))',
    clear tijdstip t hwb Hs Ts Tp 
    hwb = waitbar(0,[strrep(file.name,'.data',''),'...']);
    jj = 0;
    i = i + 1;
    
    fid = fopen(fullfile(golf_dir,file.name));
    
    fgetl(fid);
    fgetl(fid);
    fgetl(fid);
    for j = 1:lof
        waitbar(j/lof);
        clear dum
        try dum = fscanf(fid,'%s',1);
            dum = strrep(dum,'mrt','mar');
            dum = strrep(dum,'mei','may');
            dum = strrep(dum,'okt','oct');
            try datestr(dum);                    
                clear dum2 dum3
                jj = jj+1;
                tijdstip{jj} = fscanf(fid,'%s',1);
                t(jj) = datenum([dum,' ',tijdstip{jj}],'dd-mmm-yyyy HH:MM');        	
                dum2 = fscanf(fid,'%f',1);
                if ~isempty(dum2)
                    Hs(jj) = 0.01*dum2;
                else
                    Hs(jj) = NaN;                    
                end
                dum3 = fscanf(fid,'%f',20);
                if ~isempty(dum3)
                    Ts(jj) = 0.1*dum3(11);
                    Tp(jj) = (0.001*dum3(end))^-1;
                else
                    Ts(jj) = NaN;
                    Tp(jj) = NaN;
                end
                fgetl(fid);
            catch
                fgetl(fid);
                fgetl(fid);
                fgetl(fid);
            end
        catch
            break
        end
    end
    fclose(fid);
    close(hwb);
    golf_data.naam(i) = {strrep(file.name,'.data','')};
    golf_data.t(i) = {t};
    golf_data.Hs(i) = {Hs};    
    golf_data.Tp(i) = {Tp};
    golf_data.Ts(i) = {Ts};

    h = figure;
    subplot(2,1,1)
    plot(golf_data.t{i},golf_data.Hs{i},'b-')
    hold on
    ylabel('H_{1/3} (m)')
    title(golf_data.naam(i))
    xData = linspace(min(golf_data.t{i}),max(golf_data.t{i}),12);
    set(gca,'XTick',xData);
    datetick('x','mm','keepticks')    
    grid on
    
    subplot(2,1,2)
    plot(golf_data.t{i},golf_data.Tp{i},'b-')
    hold on
    xlabel('datum')
    plot(golf_data.t{i},golf_data.Ts{i},'r-')
    ylabel('T_p, T_{1/3} (s)')
    title(golf_data.naam(i))
    xData = linspace(min(golf_data.t{i}),max(golf_data.t{i}),12);
    set(gca,'XTick',xData);
    datetick('x','mm','keepticks')    
    grid on
    pause
    close(h);
end

save golf_data golf_data