diff --git a/scripts/reorganize_data.m b/scripts/reorganize_data.m index 0861a97..2684c1e 100644 --- a/scripts/reorganize_data.m +++ b/scripts/reorganize_data.m @@ -2,30 +2,76 @@ function reorganize_data() basePath = '../'; rawDataPath = fullfile(basePath, 'raw_data'); - % Get all .mat files in the rawDataPath + % Initialize maps to track run files and transient run numbers + runFilesMap = containers.Map('KeyType', 'char', 'ValueType', 'any'); + transientRunMap = containers.Map('KeyType', 'char', 'ValueType', 'double'); + files = dir(fullfile(rawDataPath, '**/*.mat')); + % Process files to categorize runs and identify transient runs for i = 1:length(files) - filePath = fullfile(files(i).folder, files(i).name); - data = load(filePath); - + data = load(fullfile(files(i).folder, files(i).name)); if isfield(data, 'tireid') && isfield(data, 'testid') tireid = data.tireid; testType = formatTestType(data.testid); - newDir = fullfile('../sorted_data', tireid); - if ~exist(newDir, 'dir') - mkdir(newDir); + runNumber = extractRunNumber(files(i).name); + + key = sprintf('%s_%s', tireid, testType); + if ~runFilesMap.isKey(key) + runFilesMap(key) = {}; end + runFiles = runFilesMap(key); + runFiles{end+1} = struct('path', fullfile(files(i).folder, files(i).name), 'runNumber', runNumber); + runFilesMap(key) = runFiles; - newFilePath = fullfile(newDir, sprintf('%s.mat', testType)); + % Identify the transient run for cornering + if strcmpi(testType, 'cornering') + if transientRunMap.isKey(tireid) + transientRunMap(tireid) = min(transientRunMap(tireid), runNumber); + else + transientRunMap(tireid) = runNumber; + end + end + end + end - if exist(newFilePath, 'file') - mergeData(newFilePath, filePath); +% Create directories for each tireid if they don't exist + mapKeys = runFilesMap.keys(); + tireids = unique(cellfun(@(x) strtok(x, '_'), mapKeys, 'UniformOutput', false)); + for i = 1:length(tireids) + newDir = fullfile('../sorted_data', tireids{i}); + if ~exist(newDir, 'dir') + mkdir(newDir); + end + end + + % Merge files in ascending order of run number and handle transient runs + for i = 1:length(mapKeys) + key = mapKeys{i}; + runFiles = runFilesMap(key); + [tireid, testType] = strtok(key, '_'); + testType = testType(2:end); % Remove leading underscore + + % Extract run numbers and sort runs by run number + runNumbers = arrayfun(@(x) x.runNumber, [runFiles{:}]); + [~, idx] = sort(runNumbers); + sortedRuns = runFiles(idx); + + newDir = fullfile('../sorted_data', tireid); + for j = 1:length(sortedRuns) + if strcmpi(testType, 'cornering') && sortedRuns{j}.runNumber == transientRunMap(tireid) + % Handle transient cornering run separately + newFilePath = fullfile(newDir, 'cornering_transient.mat'); + copyfile(sortedRuns{j}.path, newFilePath); else - copyfile(filePath, newFilePath); + % Regular file processing + newFilePath = fullfile(newDir, sprintf('%s.mat', testType)); + if j == 1 || ~exist(newFilePath, 'file') + copyfile(sortedRuns{j}.path, newFilePath); + else + mergeData(newFilePath, sortedRuns{j}.path); + end end - else - warning('Required fields not found in %s', filePath); end end end @@ -71,3 +117,13 @@ function mergeData(mergedFilePath, newFilePath) % Save the merged data save(mergedFilePath, '-struct', 'existingData'); end + +function runNumber = extractRunNumber(fileName) + num = sscanf(fileName, 'B2356run%d'); + if isempty(num) + runNumber = NaN; % Indicate missing or invalid run number + else + runNumber = num; + end +end +