clear all
close all

filename = {'46042w1996.txt'; '46042w1997.txt'; '46042w1998.txt'; '46042w1999.txt'; ...
            '46042w2000.txt'; '46042w2001.txt'; '46042w2002.txt'; '46042w2003.txt'; ...
            '46042w2004.txt'; '46042w2005.txt'; '46042w2006.txt'; '46042w2007.txt'; ...
            '46042w2008.txt'; '46042w2009.txt'}; 
        
offset1 = 0;
offset2 = 0;
for i = 1:14    
    fprintf(1, 'Processing file %s\n', char(filename(i)));
    fid = fopen(char(filename(i)), 'r');
    
    headerLine = fgets(fid);
    if i < 4
        freqVector = sscanf(headerLine(12:length(headerLine)), '%f');
    elseif ((i>=4) && (i<10))
        freqVector = sscanf(headerLine(14:length(headerLine)), '%f');
    elseif i >= 10
        freqVector = sscanf(headerLine(17:length(headerLine)), '%f');
    end
    fclose(fid);
    
    data = dlmread(char(filename(i)), '', 1, 0);
    
    if i < 4
        timestamp = datenum( (1900 + data(:,1)), data(:,2), (data(:,3)+ data(:,4)/24.0));
    else
        timestamp = datenum( data(:,1), data(:,2), (data(:,3)+ data(:,4)/24.0));
    end
    
    if i < 10
        spec = data(:,5:length(data(1,:)));
    else
        spec = data(:,6:length(data(1,:)));
    end        
    
    [r,c] = find(data(:,2) == 9);
    if length(r) > 0
        if i < 10
            septSpec1((r-r(1)+1+offset1),:) = spec(r,:);
            septTS1((r-r(1)+1+offset1),:) = timestamp(r);
            offset1 = offset1 + length(r);
            
            [r,c] = find(septSpec1 == 999.00);
            septSpec1(r,c) = NaN;
            freqVector1 = freqVector;
        else
            septSpec2((r-r(1)+1+offset2),:) = spec(r,:);
            septTS2((r-r(1)+1+offset2),:) = timestamp(r);
            offset2 = offset2 + length(r);
            
            [r,c] = find(septSpec2 == 999.00);
            septSpec2(r,c) = NaN;
            freqVector2 = freqVector;
        end
    end
end
waterfall(freqVector1, septTS1, septSpec1)
datetick('y',2)
view(5,5)
title( '1996 thru 2004 (Sept. only)' )

figure
waterfall(freqVector2, septTS2, septSpec2)
datetick('y',2)
view(5,5)
title( '2005 thru 2009 (Sept. only)' )


