% % ----------------- % 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