/***************************************************************************************** * * * OpenSpace * * * * Copyright (c) 2014-2023 * * * * Permission is hereby granted, free of charge, to any person obtaining a copy of this * * software and associated documentation files (the "Software"), to deal in the Software * * without restriction, including without limitation the rights to use, copy, modify, * * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to * * permit persons to whom the Software is furnished to do so, subject to the following * * conditions: * * * * The above copyright notice and this permission notice shall be included in all copies * * or substantial portions of the Software. * * * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, * * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF * * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE * * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * ****************************************************************************************/ #include #include #include #include using json = nlohmann::json; namespace openspace { DataProcessorJson::DataProcessorJson() : DataProcessor() {} DataProcessorJson::~DataProcessorJson() {} std::vector DataProcessorJson::readMetadata(const std::string& data, glm::size3_t& dimensions) { std::vector options = std::vector(); if (!data.empty()) { const json& j = json::parse(data); json variables = j["variables"]; for (json::iterator it = variables.begin(); it != variables.end(); ++it) { std::string option = it.key(); if (option == "ep") { const json& row = it.value(); const json& col = row.at(0); dimensions = glm::size3_t(col.size(), row.size(), 1); } if (_coordinateVariables.find(option) == _coordinateVariables.end()) { options.push_back(std::move(option)); } } } return options; } void DataProcessorJson::addDataValues(const std::string& data, properties::SelectionProperty& dataOptions) { int numOptions = static_cast(dataOptions.options().size()); initializeVectors(numOptions); if (!data.empty()) { const json& j = json::parse(data); json variables = j["variables"]; std::vector sum(numOptions, 0.f); std::vector> optionValues(numOptions, std::vector()); const std::vector& options = dataOptions.options(); for (int i = 0; i < numOptions; ++i) { const json& row = variables[options[i]]; // int rowsize = row.size(); for (size_t y = 0; y < row.size(); ++y) { const json& col = row.at(y); const int colsize = static_cast(col.size()); for (int x = 0; x < colsize; ++x) { const float value = col.at(x); optionValues[i].push_back(value); _min[i] = std::min(_min[i], value); _max[i] = std::max(_max[i], value); sum[i] += value; } } } add(optionValues, sum); } } std::vector DataProcessorJson::processData(const std::string& data, properties::SelectionProperty& optionProp, glm::size3_t& dimensions) { if (data.empty()) { return std::vector(); } const json& j = json::parse(data); json variables = j["variables"]; const std::set& selectedOptions = optionProp; const std::vector& options = optionProp.options(); std::vector selectedOptionsIndices; for (const std::string& option : selectedOptions) { auto it = std::find(options.begin(), options.end(), option); ghoul_assert(it != options.end(), "Selected option must be in all options"); int idx = static_cast(std::distance(options.begin(), it)); selectedOptionsIndices.push_back(idx); } std::vector dataOptions(options.size(), nullptr); for (int option : selectedOptionsIndices) { // @CLEANUP: This memory is very easy to lose and should be replaced by some // other mechanism (std::vector most likely) dataOptions[option] = new float[dimensions.x * dimensions.y] { 0.f }; json row = variables[options[option]]; const int rowsize = static_cast(row.size()); for (int y = 0; y < rowsize; ++y) { json col = row.at(y); const int colsize = static_cast(col.size()); for (int x = 0; x < colsize; ++x) { const float value = col.at(x); const int i = x + y * colsize; dataOptions[option][i] = processDataPoint(value, option); } } } calculateFilterValues(selectedOptionsIndices); return dataOptions; } } //namespace openspace