| 482 | } |
| 483 | |
| 484 | void Storage::writeData(const string& id, const string& name, const AnyValue& data) |
| 485 | { |
| 486 | try { |
| 487 | checkGroupWrite(id, false); |
| 488 | } catch (const CanteraError& err) { |
| 489 | // rethrow with public method attribution |
| 490 | throw CanteraError("Storage::writeData", "{}", err.getMessage()); |
| 491 | } catch (const std::exception& err) { |
| 492 | // convert HighFive exception |
| 493 | throw CanteraError("Storage::writeData", |
| 494 | "Encountered exception for group '{}':\n{}", id, err.what()); |
| 495 | } |
| 496 | h5::Group sub = m_file->getGroup(id); |
| 497 | if (sub.exist(name)) { |
| 498 | throw NotImplementedError("Storage::writeData", |
| 499 | "Unable to overwrite existing DataSet '{}' in group '{}'.", name, id); |
| 500 | } |
| 501 | size_t size = data.vectorSize(); |
| 502 | auto [rows, cols] = data.matrixShape(); |
| 503 | if (size == npos && rows == npos) { |
| 504 | throw CanteraError("Storage::writeData", |
| 505 | "Cannot write DataSet '{}' in group '{}' as input data with type\n" |
| 506 | "'{}'\nis neither a vector nor a matrix.", name, id, data.type_str()); |
| 507 | } |
| 508 | vector<size_t> dims{data.vectorSize()}; |
| 509 | if (data.isVector<long int>()) { |
| 510 | h5::DataSet dataset = sub.createDataSet<long int>(name, h5::DataSpace(dims)); |
| 511 | dataset.write(data.asVector<long int>()); |
| 512 | return; |
| 513 | } |
| 514 | if (data.isVector<double>()) { |
| 515 | h5::DataSet dataset = sub.createDataSet<double>(name, h5::DataSpace(dims)); |
| 516 | dataset.write(data.asVector<double>()); |
| 517 | return; |
| 518 | } |
| 519 | if (data.isVector<string>()) { |
| 520 | h5::DataSet dataset = sub.createDataSet<string>(name, h5::DataSpace(dims)); |
| 521 | dataset.write(data.asVector<string>()); |
| 522 | return; |
| 523 | } |
| 524 | if (cols != npos) { |
| 525 | dims.clear(); |
| 526 | dims.push_back(rows); |
| 527 | dims.push_back(cols); |
| 528 | } else { |
| 529 | throw NotImplementedError("Storage::writeData", |
| 530 | "Cannot write DataSet '{}' in group '{}' as input data with type\n" |
| 531 | "'{}'\nis not supported.", name, id, data.type_str()); |
| 532 | } |
| 533 | if (m_compressionLevel) { |
| 534 | // Set chunk size to single chunk and apply compression level; for caveats, see |
| 535 | // https://stackoverflow.com/questions/32994766/compressed-files-bigger-in-h5py |
| 536 | h5::DataSpace space(dims, dims); //{h5::DataSpace::UNLIMITED, dims[1]}); |
| 537 | h5::DataSetCreateProps props; |
| 538 | props.add(h5::Chunking(vector<hsize_t>{dims[0], dims[1]})); |
| 539 | props.add(h5::Deflate(m_compressionLevel)); |
| 540 | if (data.isVector<vector<long int>>()) { |
| 541 | h5::DataSet dataset = sub.createDataSet<long int>(name, space, props); |