Matlab script to concatenate 20-Hz data

Discussion of meteorological data.

Moderators: Bonnie.Jonkman, Andy.Clifton

Andy.Clifton
Posts: 83
Joined: Wed Feb 29, 2012 3:13 pm
Organization: NREL
Location: Boulder, CO
Contact:

Matlab script to concatenate 20-Hz data

Postby Andy.Clifton » Tue Aug 12, 2014 3:13 pm

I've had a few people ask recently if I have a matlab utility to concatenate several 20-Hz data files. Below is a script that should do that, and keep the 20-Hz data format that you are used to working with.

You will need to adjust the first few lines (file location, files names) to your situation. Make sure that the first file in the list has the first data, as all of the sonic timestamps will be based off the first timestamp in that file.

Code: Select all

directory = '~/Downloads';
files = {'03254_14_20_00_009.mat'; ...
    '03254_14_30_00_009.mat';...
    '03254_14_40_00_059.mat'};
nfiles = length(files);

% initalize the array that we will dump data into
data = struct;
datanew = struct;
for f = 1:nfiles
    datain = load(fullfile(directory, files{f}));
    if f == 1
        datanames = fieldnames(datain);
        newnames = datanames;
    end
    % work through each of the elements so that we can catch special cases
    for n = 1:length(datanames)
        if strcmp(datanames{n},'tower')
            % ignore this field
            newnames{n} = '';
        else
            if strfind(datanames{n},'dt')
                % need to change dt so it's a UTC time instead
                newnames{n} = strrep(datanames{n},'dt','timestamp');
                datanew.(newnames{n}) = struct('val', datain.time_UTC.val(1)+datain.(datanames{n}).val,...
                    'label',datain.(datanames{n}).label,...
                    'units',datain.(datanames{n}).units,...
                    'height',datain.(datanames{n}).height);
            else
                datanew.(newnames{n}) = struct('val',datain.(datanames{n}).val,...
                    'label',datain.(datanames{n}).label,...
                    'units',datain.(datanames{n}).units,...
                    'height',datain.(datanames{n}).height);
            end
        end
    end
    % append the new data to the old data
    if f == 1
        data = datanew;
    else
        for n = 1:length(newnames)
            if ~isempty(newnames{n})
                data.(newnames{n}).val = vertcat(data.(newnames{n}).val,...
                    datanew.(newnames{n}).val);
            end
        end
    end
end


plotting then works the same as always.

Code: Select all

%% plot some data
figure
plot(data.Sonic_timestamp_clean_100m.val,...
    data.Sonic_x_clean_100m.val,'k.')
Andy Clifton, Ph.D.
Senior Engineer

Return to “NWTC Wind Data”

Who is online

Users browsing this forum: No registered users and 1 guest