Add code snippets

This commit is contained in:
2024-08-28 17:22:21 +02:00
parent de2368c07e
commit 064a0f75cb
32 changed files with 2354 additions and 0 deletions

View File

@@ -0,0 +1,40 @@
% function [Catalog] = CatalogFilterNaN(fullcatalog,...)
% Select lines/values from catalog without NaN and empty string
% Input parameters are pairs:
% names of fields
% eg. CatalogSelectRange(fullcatalog,'Mw','EID')
%
% (c) Dorota Olszewska IG PAS
function [Catalog] = CatalogFilterNaN(fullcatalog,varargin)
Catalog = fullcatalog ;
range = true(size(fullcatalog(1).val)) ;
na_varargin = numel(varargin) ;
for i= 1:na_varargin
if ~ischar(varargin{i})
warning(['Wrong type of parameter ' num2str(i)])
continue
end
index = find(strcmp(varargin{i},{fullcatalog.field})) ;
if length(index) ~= 1
warning(['Can not select field ' varargin{i}])
continue
end
if fullcatalog(index).type == 3
for ii=1:length(Catalog(index).val)
lrange(ii,1) = ~isempty(Catalog(index).val{ii});
end
else
lrange = ~isnan(fullcatalog(index).val) ;
%nnel = numel(lrange) ;
end
range = range & lrange;
end
for i = 1:numel(fullcatalog)
if fullcatalog(i).type == 3
Catalog(i).val = {fullcatalog(i).val{range}}' ;
else
Catalog(i).val = fullcatalog(i).val(range) ;
end
end
end

View File

@@ -0,0 +1,55 @@
% function [Catalog] = CatalogSelectFields(fullcatalog,...)
% Select fields from IS-POIG catalog v2.0
% Produce reduced IS-POIG catalog v2.0
% Input parameters are names of fields
% eg. CatalogSelectFields(fullcatalog,'Date','Lat','Long','Mw')
% or cells with names of fields
% eg. CatalogSelectFields(fullcatalog,{'Date','Lat','Long','Mw'})
%
% (c) Jan Wiszniowski IG PAS
% v_2.0 19-09-2016 Dorota Olszewska
% 2021-10-22: test of inputs and error messages.
%
function [Catalog] = CatalogSelectFields(fullcatalog,varargin)
na_varargin = length(varargin) ;
no_cols = 0 ;
for i= 1:na_varargin
if iscell(varargin{i})
cellin = varargin{i} ;
for j= 1:numel(cellin)
if ischar(cellin{j})
if ~any(strcmp(cellin{j},{fullcatalog.field}))
error('Field %s does not exist in the catalog', cellin{j})
end
no_cols = no_cols+1 ;
else
error('Wrong %d{%d} input argument of the CatalogSelectFields function', j, i)
end
end
elseif ischar(varargin{i})
if ~any(strcmp(varargin{i},{fullcatalog.field}))
error('Field %s does not exist in the catalog', varargin{i})
end
no_cols = no_cols+1 ;
else
error('Wrong %d input of the CatalogSelectFields function', i)
end
end
Catalog = struct('field',cell(1,no_cols),'type',cell(1,no_cols),'val',cell(1,no_cols),'unit',cell(1,no_cols),'description',cell(1,no_cols),'fieldType',cell(1,no_cols)) ;
no_cols = 0 ;
for i= 1:na_varargin
if iscell(varargin{i})
cellin = varargin{i} ;
for j= 1:numel(cellin)
if ischar(cellin{j})
no_cols = no_cols+1 ;
Catalog(no_cols) = fullcatalog(strcmp(cellin{j},{fullcatalog.field})) ;
end
end
elseif ischar(varargin{i})
no_cols = no_cols+1 ;
Catalog(no_cols) = fullcatalog(strcmp(varargin{i},{fullcatalog.field})) ;
end
end
end

View File

@@ -0,0 +1,83 @@
% function [Catalog] = CatalogSelectRange(fullcatalog,...)
% Select lines/values from catalog
% Input parameters are pairs:
% names of fields
% and
% range of values
% for numeric there are: [minval maxval] or [val] or {val1, val2, val3, ...}
% for strings there are: 'valtext' or {'valtext1', 'valtext2', 'valtext3', ...}
% eg. CatalogSelectRange(fullcatalog,'Mw',[0 4.5])
%
% (c) Jan Wiszniowski IG PAS
% Modyfied 2016.02.04 - numerical comparison with tolerance 1e-14
function [Catalog] = CatalogSelectRange(fullcatalog,varargin)
Catalog = fullcatalog ;
range = true(size(fullcatalog(1).val)) ;
na_varargin = numel(varargin) ;
for i= 1:2:na_varargin-1
if ~ischar(varargin{i})
warning(['Wrong type of parameter ' num2str(i)])
continue
end
index = find(strcmp(varargin{i},{fullcatalog.field})) ;
if length(index) ~= 1
warning(['Can not select field ' varargin{i}])
continue
end
if fullcatalog(index).type == 3
lrange = varargin{i+1} ;
if ischar(lrange)
range = range & strcmp(lrange,fullcatalog(index).val) ;
elseif iscell(lrange)
l_range = false(size(range)) ;
for j = 1:numel(lrange)
llrange = lrange{j} ;
if ischar(llrange)
l_range = l_range | strcmp(llrange,fullcatalog(index).val) ;
end
end
range = range & l_range ;
else
warning(['Wrong type of walues for parameter ' varargin{i}])
end
else
lrange = varargin{i+1} ;
nnel = numel(lrange) ;
if isnumeric(lrange)
tol = abs(lrange) ./ 1e15 ;
if nnel == 2
range = range & fullcatalog(index).val >= lrange(1)-tol(1) & fullcatalog(index).val <= lrange(2)+tol(2) ;
elseif nnel == 1
range = range & abs(fullcatalog(index).val - lrange(1)) <= tol(1) ;
end
elseif iscell(lrange)
l_range = false(size(range)) ;
for j = 1:numel(lrange)
llrange = lrange{j} ;
tol = abs(llrange) ./ 1e15 ;
if isnumeric(llrange)
if numel(llrange) == 2
l_range = l_range | fullcatalog(index).val >= llrange(1)-tol(1) & fullcatalog(index).val <= llrange(2)+tol(2) ;
elseif numel(llrange) == 1
l_range = l_range | abs(range & fullcatalog(index).val - llrange(1)) <= tol(1);
end
else
warning(['Wrong type of walues for parameter ' varargin{i}])
end
end
range = range & l_range ;
else
warning(['Wrong type of walues for parameter ' varargin{i}])
end
end
end
for i = 1:numel(fullcatalog)
if fullcatalog(i).type == 3
Catalog(i).val = {fullcatalog(i).val{range}}' ;
else
Catalog(i).val = fullcatalog(i).val(range) ;
end
end
end

View File

@@ -0,0 +1,24 @@
%
% -----------------
% Copyright © 2019 ACK Cyfronet AGH, Poland.
%
% This work was partially funded by EPOS Project funded in frame of PL-POIR4.2
% --------------
%
function varargout = eliminateEmptyValues(varargin)
if isempty(varargin)
return;
end
indexes = 1:length(varargin{1});
for i = 1:length(varargin)
column = varargin{i};
if isnumeric(column)
indexes = intersect(indexes, find(~isnan(column)));
elseif iscell(column)
indexes = intersect(indexes, find(~cellfun(@isempty, column)));
end
end
for i = 1:length(varargin)
varargout{i} = varargin{i}(indexes);
end
end

View File

@@ -0,0 +1,25 @@
%
% -----------------
% Copyright © 2019 ACK Cyfronet AGH, Poland.
%
% This work was partially funded by EPOS Project funded in frame of PL-POIR4.2
% --------------
%
function [varargout] = extractColumns(catalog, varargin)
for i=1:length(varargin)
colName = varargin{i};
varargout{i} = findColumn(catalog, colName);
end
end
function column = findColumn(catalog, colName)
for c=1:length(catalog)
if strcmp(catalog(c).field, colName)
column = catalog(c).val;
break;
end
end
if exist('column') ~= 1
error('no column named %s', colName);
end
end

View File

@@ -0,0 +1,19 @@
%
% -----------------
% Copyright © 2022 ACK Cyfronet AGH, Poland.
% --------------
%
% Returns index of the column with the specified colName within the given catalog
%
% TODO use this function in extractColumns.m
function columnIdx = findCatalogColumn(catalog, colName)
for c=1:length(catalog)
if strcmp(catalog(c).field, colName)
columnIdx = c;
break;
end
end
if exist('columnIdx') ~= 1
error('no column named %s', colName);
end
end

View File

@@ -0,0 +1,100 @@
% function [Catalog] = merge2Catalog(catalog1,catalog2)
% Add values from 1st Catalog to 2nd
%
% v1.1 - 2021119 - sort according by time was added
% (c) Dorota Olszewska IG PAS
function [Catalog] = merge2Catalog(Catalog,catalog2,ID)
if sum(strcmp(ID,{Catalog.field}))+sum(strcmp(ID,{catalog2.field})) == 0
error([ID,' is not field of both catalogs ', ID])
end
ID1=Catalog(strcmp(ID,{Catalog.field})).val;
ID2=catalog2(strcmp(ID,{catalog2.field})).val;
if length(unique(ID1))~=length(ID1) || length(unique(ID2))~=length(ID2)
error([ID,' has not unique variable at both catalogs '])
end
no_cols = size(Catalog,2);
for j = 1:numel(ID2)
llrange = ID2{j} ;
l_range(j) = sum(strcmp(llrange,ID1)) ;
end
catalog2_row=CatalogSelectRange(catalog2,ID,catalog2(strcmp(ID,{catalog2.field})).val(l_range==0));
% add rows by RID
no_rows_row = size(catalog2_row(1).val,1);
if no_rows_row == 0
else
for i=1:no_cols
if Catalog(i).type == 3
if sum(strcmp(Catalog(i).field,{catalog2_row.field})) == 0
Catalog(i).val=[Catalog(1).val;repmat({'NaN'},no_rows_row,1)];
else
Catalog(i).val=[Catalog(i).val;catalog2_row(strcmp(Catalog(i).field,{catalog2_row.field})).val];
end
else
if sum(strcmp(Catalog(i).field,{catalog2_row.field})) == 0
Catalog(i).val=[Catalog(i).val;NaN(no_rows_row,1)];
else
Catalog(i).val=[Catalog(i).val;catalog2_row(strcmp(Catalog(i).field,{catalog2_row.field})).val];
end
end
end
end
ID1=Catalog(strcmp(ID,{Catalog.field})).val;
ID2=catalog2(strcmp(ID,{catalog2.field})).val;
no_cols = size(Catalog,2);
no_rows = size(Catalog(1).val,1);
for j = 1:numel(ID2)
llrange = ID2{j} ;
l_range(j) = sum(strcmp(llrange,ID1)) ;
end
catalog2_col=CatalogSelectRange(catalog2,ID,catalog2(strcmp(ID,{catalog2.field})).val(l_range==1));
% add columns by RID
no_rows_col = size(catalog2_col(1).val,1);
if no_rows_col == 0
else
column1={Catalog.field};
column2={catalog2_col.field};
for j = 1:numel(column2)
llrange2 = column2{j} ;
l_range2(j) = sum(strcmp(llrange2,column1)) ;
end
new_column=column2(l_range2==0);
for k=1:numel(new_column)
Catalog(no_cols+k).field = catalog2_col(strcmp(new_column{k},{catalog2_col.field})).field;
Catalog(no_cols+k).type = catalog2_col(strcmp(new_column{k},{catalog2_col.field})).type;
Catalog(no_cols+k).unit = catalog2_col(strcmp(new_column{k},{catalog2_col.field})).unit;
Catalog(no_cols+k).description = catalog2_col(strcmp(new_column{k},{catalog2_col.field})).description;
Catalog(no_cols+k).fieldType = catalog2_col(strcmp(new_column{k},{catalog2_col.field})).fieldType;
for kk=1:no_rows
rr=strcmp(Catalog(strcmp(ID,{Catalog.field})).val(kk),catalog2_col(strcmp(ID,{catalog2_col.field})).val);
if sum(rr) ==0
Catalog(no_cols+k).val(kk,1)=NaN;
else
Catalog(no_cols+k).val(kk,1) = catalog2_col(strcmp(new_column{k},{catalog2_col.field})).val(rr);
end
end
end
Catalog = sortByTime(Catalog);
end

View File

@@ -0,0 +1,90 @@
%
% -----------------
% Copyright © 2022 ACK Cyfronet AGH, Poland.
%
% Licensed under the Apache License, Version 2.0 (the "License");
% you may not use this file except in compliance with the License.
% You may obtain a copy of the License at
%
% http://www.apache.org/licenses/LICENSE-2.0
%
% Unless required by applicable law or agreed to in writing, software
% distributed under the License is distributed on an "AS IS" BASIS,
% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
% See the License for the specific language governing permissions and
% limitations under the License.
%
% This work was partially funded by EPOS-SP Project.
% -----------------
%
% Modifies event with the given 'eventId' in the catalog. If such event was not yet present in the catalog,
% it is added at the correct position with respect to time (the event is added at the end and the resulting
% catalog is sorted by time)
%
function outCatalog = modifyCatalog(catalog, eventId, eventValues)
newColIdxs = findAbsentFields({eventValues.field}, {catalog.field});
if ~isempty(newColIdxs)
% add columns that were not yet present in the catalog, but are specified in eventValues
outCatalog = insertColumns(catalog, eventValues, newColIdxs);
else
outCatalog = catalog;
end
idColumn = outCatalog(1).val; % assuming ID is always the first column
eventIdx = find(strcmp(idColumn, eventId));
isNewEvent = 0;
if isempty(eventIdx)
isNewEvent = 1;
eventIdx = length(idColumn) + 1;
outCatalog(1).val(eventIdx) = eventId;
end
for e=1:length(eventValues)
colIdx = findCatalogColumn(outCatalog, eventValues(e).field);
outCatalog(colIdx).val(eventIdx) = eventValues(e).value;
end
if isNewEvent
% fill fields that were not specified in eventValues with empty values
absentColIdxs = findAbsentFields({outCatalog.field}, {eventValues.field});
if ~isempty(absentColIdxs)
for i=2:length(absentColIdxs) % skip 1 (ID column index)
absColIdx = absentColIdxs(i);
outCatalog(absColIdx).val(eventIdx) = getEmptyFieldValue(outCatalog(absColIdx).type, 1);
end
end
end
outCatalog = sortByTime(outCatalog);
end
% find Catalog columns that were not used in eventValues
function colIdxs = findAbsentFields(catalogFields, eventValuesFields)
colIdxs = [];
for c=1:length(catalogFields)
if ~ismember(catalogFields(c), eventValuesFields)
colIdxs(end + 1) = c;
end
end
end
% get value that should be used as empty field for the given column type
function empty = getEmptyFieldValue(type, fieldsNo)
if (type == 3)
empty = cell(fieldsNo, 1);
else
empty = nan(fieldsNo, 1);
end
end
function outCatalog = insertColumns(catalog, eventValues, newColIdxs)
outCatalog = catalog;
rowsNo = length(catalog(1).val);
colsNo = length(catalog);
for i=1:length(newColIdxs)
newColIdx = colsNo+i;
eventFields = eventValues(newColIdxs(i));
outCatalog(newColIdx).field = eventFields.field;
outCatalog(newColIdx).type = eventFields.type;
outCatalog(newColIdx).unit = eventFields.unit;
outCatalog(newColIdx).description = eventFields.description;
outCatalog(newColIdx).fieldType = eventFields.fieldType;
outCatalog(newColIdx).val = getEmptyFieldValue(eventFields.type, rowsNo);
end
end

View File

@@ -0,0 +1,10 @@
function [sortedCatalog] = sortByTime(catalog)
timeColIndex = find(strcmp('Time', {catalog.field}));
if ~isempty(timeColIndex)
[~, sortedIndexes] = sortrows(catalog(timeColIndex).val);
for i=1:length(catalog)
catalog(i).val = catalog(i).val(sortedIndexes);
end
end
sortedCatalog = catalog;
end

1
matlab/catalog/test Normal file
View File

@@ -0,0 +1 @@
filterByMminAndTime