shared-snippets/matlab/catalog/sortByTime.m

36 lines
1.2 KiB
Mathematica
Raw Permalink Normal View History

2024-08-29 13:01:31 +02:00
%
% -----------------
% Copyright © 2022 ACK Cyfronet AGH, Poland.
% -----------------
%
2024-08-28 17:22:21 +02:00
function [sortedCatalog] = sortByTime(catalog)
% SORTBYTIME Sorts a seismic catalog structure by the 'Time' column
2024-08-29 13:11:12 +02:00
%
% sortedCatalog = SORTBYTIME(catalog) sorts the given catalog structure
% based on the values in the 'Time' column. The catalog is expected to be
% an array of structures where each structure has fields 'field' and
% 'val'. The function rearranges the catalog entries in ascending order
% of time.
%
% This function is specific to seismic catalogs. For more general
% information on catalogs, refer to:
% https://docs.cyfronet.pl/display/ISDOC/Catalog+-+description
%
2024-08-29 13:11:12 +02:00
% Example:
% catalog(1).field = 'ID'; catalog(1).val = [3, 2, 1];
% catalog(2).field = 'Time'; catalog(2).val = [300, 200, 100];
% sortedCatalog = sortByTime(catalog);
% % Result: sortedCatalog(2).val = [100, 200, 300]
%
% See also: FINDCATALOGCOLUMN, SORTROWS.
2024-08-28 17:22:21 +02:00
timeColIndex = find(strcmp('Time', {catalog.field}));
if ~isempty(timeColIndex)
[~, sortedIndexes] = sortrows(catalog(timeColIndex).val);
2024-08-29 13:11:12 +02:00
for i = 1:length(catalog)
2024-08-28 17:22:21 +02:00
catalog(i).val = catalog(i).val(sortedIndexes);
end
end
sortedCatalog = catalog;
2024-08-29 13:11:12 +02:00
end