Skip to content
Snippets Groups Projects
Commit 2a422efb authored by Tammo Jan Dijkema's avatar Tammo Jan Dijkema
Browse files

Merge branch 'oskar-nearest-neighbor' into 'master'

Select nearest neighbor frequency in oskar dataset

See merge request RD/EveryBeam!136
parents 62ed313d d83033a6
No related branches found
No related tags found
No related merge requests found
......@@ -5,26 +5,52 @@
#include <cassert>
#include <stdexcept>
#include <cstring>
#include <limits>
#include "oskardataset.h"
Dataset::Dataset(H5::H5File& h5_file, const unsigned int freq) {
// Try to read coefficients for this frequency
std::string dataset_name = std::to_string((int)(freq / 1e6));
H5::DataSet dataset;
const int freq_mhz = (int)(freq / 1e6);
try {
// Try to find exact match
const std::string dataset_name = std::to_string(freq_mhz);
dataset = h5_file.openDataSet(dataset_name);
} catch (H5::FileIException& e) {
// Find nearest neighbor
int dfreq = std::numeric_limits<int>::max();
int freq_nearest = 0;
for (size_t i = 0; i < h5_file.getNumObjs(); ++i) {
const int h5_freq = stoi(h5_file.getObjnameByIdx(i));
if (std::abs(h5_freq - freq_mhz) < dfreq) {
dfreq = std::abs(h5_freq - freq_mhz);
freq_nearest = h5_freq;
}
}
std::cout << "Could not load dataset for frequency "
<< std::to_string(freq_mhz)
<< " MHz, using the nearest neighbor with frequency "
<< freq_nearest << " MHz instead" << std::endl;
const std::string dataset_name = std::to_string(freq_nearest);
try {
// Open dataset
H5::DataSet dataset = h5_file.openDataSet(dataset_name);
dataset = h5_file.openDataSet(dataset_name);
} catch (H5::FileIException& e) {
throw std::runtime_error("Could not load dataset for frequency " +
dataset_name + "MHz.");
}
}
// Read dataset dimensions
H5::DataSpace dataspace = dataset.getSpace();
unsigned int rank = dataspace.getSimpleExtentNdims();
const size_t rank = dataspace.getSimpleExtentNdims();
assert(rank == dataset_rank_);
// Get dimensions
std::vector<hsize_t> dims(rank);
dataspace.getSimpleExtentDims(dims.data(), NULL);
auto nr_elements = dims[0];
auto nr_coeffs = dims[1];
const size_t nr_elements = dims[0];
const size_t nr_coeffs = dims[1];
assert(dims[2] == 4); // tetm*pol
// Coefficient data stored as:
......@@ -38,8 +64,8 @@ Dataset::Dataset(H5::H5File& h5_file, const unsigned int freq) {
#endif
// Check total number of coefficients to find l_max
auto l_max_d = sqrt(nr_coeffs + 1) - 1;
auto l_max = (int)round(l_max_d);
const double l_max_d = sqrt(nr_coeffs + 1) - 1;
const size_t l_max = (size_t)round(l_max_d);
#ifndef NDEBUG
std::cout << "l_max: " << l_max << std::endl;
#endif
......@@ -58,12 +84,6 @@ Dataset::Dataset(H5::H5File& h5_file, const unsigned int freq) {
H5::DataType data_type = dataset.getDataType();
assert(data_type.getSize() == sizeof(std::complex<double>));
dataset.read(data_.data(), data_type, dataspace);
} catch (H5::FileIException& e) {
std::stringstream message;
message << "Could not load dataset for frequency " << dataset_name
<< " Mhz";
throw std::runtime_error(message.str());
}
}
size_t Dataset::GetIndex(const unsigned int element) const {
......
......@@ -36,9 +36,9 @@ class Dataset {
// Members
std::vector<std::complex<double>> data_;
unsigned int nr_elements_;
unsigned int nr_coeffs_;
unsigned int l_max_;
size_t nr_elements_;
size_t nr_coeffs_;
size_t l_max_;
};
#endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment