36 #include <Structure.h>
52 #include "BESInternalError.h"
53 #include "BESSyntaxUserError.h"
55 #include "StareFunctions.h"
69 string stare_storage_path =
"";
70 string stare_sidecar_suffix =
"_sidecar";
85 assert(m.stare_indices.size() == m.x_indices.size()
86 && m.x_indices.size() == m.y_indices.size());
88 auto ti = m.target_indices.begin();
89 auto si = m.stare_indices.begin();
90 auto xi = m.x_indices.begin();
91 auto yi = m.y_indices.begin();
93 while (si != m.stare_indices.end()) {
94 out <<
"Target: " << *ti++ <<
", Dataset Index: " << *si++ <<
", coord: x: " << *xi++ <<
", y: " << *yi++ << endl;
111 static vector<dods_uint64> *extract_uint64_array(Array *var) {
114 int length = var->length();
116 auto *newVar =
new vector<dods_uint64>;
117 newVar->resize(length);
118 var->value(&(*newVar)[0]);
125 extract_uint64_array(Array *var, vector<dods_uint64> &values) {
127 values.resize(var->length());
128 var->value(&values[0]);
153 target_in_dataset(
const vector<dods_uint64> &targetIndices,
const vector<dods_uint64> &dataStareIndices) {
156 for (
const dods_uint64 &i : targetIndices) {
157 for (
const dods_uint64 &j :dataStareIndices ) {
161 int result = cmpSpatial(i, j);
185 count(
const vector<dods_uint64> &target_indices,
const vector<dods_uint64> &dataset_indices,
bool all_target_matches ) {
186 unsigned int counter = 0;
187 for (
const dods_uint64 &i : dataset_indices) {
188 for (
const dods_uint64 &j : target_indices)
191 if (cmpSpatial(i, j) != 0) {
193 BESDEBUG(STARE,
"Matching (dataset, target) indices: " << i <<
", " << j << endl);
194 if (!all_target_matches)
212 unique_ptr<stare_matches>
213 stare_subset_helper(
const vector<dods_uint64> &target_indices,
const vector<dods_uint64> &dataset_indices,
214 const vector<int> &dataset_x_coords,
const vector<int> &dataset_y_coords)
216 assert(dataset_indices.size() == dataset_x_coords.size());
217 assert(dataset_indices.size() == dataset_y_coords.size());
220 unique_ptr<stare_matches> subset(
new stare_matches());
222 auto x = dataset_x_coords.begin();
223 auto y = dataset_y_coords.begin();
224 for (
const dods_uint64 &i : dataset_indices) {
225 for (
const dods_uint64 &j : target_indices) {
226 if (cmpSpatial(i, j) != 0) {
227 subset->add(*x, *y, i, j);
253 void stare_subset_array_helper(vector<T> &result_data,
const vector<T> &src_data,
254 const vector<dods_uint64> &target_indices,
const vector<dods_uint64> &dataset_indices)
256 assert(dataset_indices.size() == src_data.size());
257 assert(dataset_indices.size() == result_data.size());
259 auto r = result_data.begin();
260 auto s = src_data.begin();
261 for (
const dods_uint64 &i : dataset_indices) {
262 for (
const dods_uint64 &j : target_indices) {
263 if (cmpSpatial(i, j) != 0) {
290 void StareSubsetArrayFunction::build_masked_data(Array *dependent_var,
const vector<dods_uint64> &dep_var_stare_indices,
291 const vector<dods_uint64> &target_s_indices, unique_ptr<Array> &result) {
292 vector<T> src_data(dependent_var->length());
293 dependent_var->read();
294 dependent_var->value(&src_data[0]);
297 vector<T> result_data(dependent_var->length(), mask_value);
299 stare_subset_array_helper(result_data, src_data, target_s_indices, dep_var_stare_indices);
301 result->set_value(result_data, result_data.size());
315 get_sidecar_file_pathname(
const string &pathName,
const string &token)
317 string granuleName = pathName;
318 if (!stare_storage_path.empty()) {
319 granuleName = pathName.substr(pathName.find_last_of(
'/') + 1);
322 size_t findDot = granuleName.find_last_of(
'.');
326 string extension = granuleName.substr(findDot);
327 granuleName = granuleName.substr(0, findDot).append(token).append(extension);
329 if (!stare_storage_path.empty()) {
346 get_sidecar_int32_values(
const string &filename,
const string &variable, vector<dods_int32> &values)
349 hid_t file = H5Fopen(filename.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT);
351 throw BESInternalError(
"Could not open file " + filename, __FILE__, __LINE__);
353 hid_t dataset = H5Dopen(file, variable.c_str(), H5P_DEFAULT);
355 throw BESInternalError(
string(
"Could not open dataset: ").append(variable), __FILE__, __LINE__);
357 hid_t dspace = H5Dget_space(dataset);
358 const int ndims = H5Sget_simple_extent_ndims(dspace);
359 vector<hsize_t> dims(ndims);
364 H5Sget_simple_extent_dims(dspace, &dims[0], NULL);
367 hid_t filespace = H5Dget_space(dataset);
369 hid_t memspace = H5Screate_simple(ndims, &dims[0], NULL);
372 values.resize(H5Sget_select_npoints(filespace));
375 H5Dread(dataset, H5T_NATIVE_INT, memspace, filespace, H5P_DEFAULT, &values[0]);
385 get_sidecar_uint64_values(
const string &filename, BaseType *, vector<dods_uint64> &values)
388 hid_t file = H5Fopen(filename.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT);
390 throw BESInternalError(
"Could not open file " + filename, __FILE__, __LINE__);
395 hid_t dataset = H5Dopen(file, s_index_name.c_str(), H5P_DEFAULT);
397 throw BESInternalError(
string(
"Could not open dataset: ").append(s_index_name), __FILE__, __LINE__);
399 hid_t dspace = H5Dget_space(dataset);
400 const int ndims = H5Sget_simple_extent_ndims(dspace);
401 vector<hsize_t> dims(ndims);
406 H5Sget_simple_extent_dims(dspace, &dims[0], NULL);
409 hid_t filespace = H5Dget_space(dataset);
411 hid_t memspace = H5Screate_simple(ndims, &dims[0], NULL);
414 values.resize(H5Sget_select_npoints(filespace));
417 H5Dread(dataset, H5T_NATIVE_ULLONG, memspace, filespace, H5P_DEFAULT, &values[0]);
421 read_stare_indices_from_function_argument(BaseType *raw_stare_indices, vector<dods_uint64>&s_indices) {
423 Array *stare_indices =
dynamic_cast<Array *
>(raw_stare_indices);
424 if (stare_indices ==
nullptr)
426 "Expected an Array but found a " + raw_stare_indices->type_name(), __FILE__, __LINE__);
428 if (stare_indices->var()->type() != dods_uint64_c)
430 "Expected an Array of UInt64 values but found an Array of " + stare_indices->var()->type_name(),
433 stare_indices->read();
435 extract_uint64_array(stare_indices, s_indices);
449 StareIntersectionFunction::stare_intersection_dap4_function(D4RValueList *args, DMR &dmr)
451 if (args->size() != 2) {
453 oss <<
"stare_intersection(): Expected two arguments, but got " << args->size();
458 string fullPath = get_sidecar_file_pathname(dmr.filename(), stare_sidecar_suffix);
460 BaseType *dependent_var = args->get_rvalue(0)->value(dmr);
461 BaseType *raw_stare_indices = args->get_rvalue(1)->value(dmr);
464 vector<dods_uint64> dep_var_stare_indices;
465 get_sidecar_uint64_values(fullPath, dependent_var, dep_var_stare_indices);
468 vector<dods_uint64> target_s_indices;
469 read_stare_indices_from_function_argument(raw_stare_indices, target_s_indices);
471 bool status = target_in_dataset(target_s_indices, dep_var_stare_indices);
474 Int32 *result =
new Int32(
"result");
476 unique_ptr<Int32> result(
new Int32(
"result"));
478 result->set_value(1);
481 result->set_value(0);
484 return result.release();
506 StareCountFunction::stare_count_dap4_function(D4RValueList *args, DMR &dmr)
508 if (args->size() != 2) {
510 oss <<
"stare_intersection(): Expected two arguments, but got " << args->size();
515 string fullPath = get_sidecar_file_pathname(dmr.filename(), stare_sidecar_suffix);
517 BaseType *dependent_var = args->get_rvalue(0)->value(dmr);
518 BaseType *raw_stare_indices = args->get_rvalue(1)->value(dmr);
521 vector<dods_uint64> dep_var_stare_indices;
522 get_sidecar_uint64_values(fullPath, dependent_var, dep_var_stare_indices);
525 vector<dods_uint64> target_s_indices;
526 read_stare_indices_from_function_argument(raw_stare_indices, target_s_indices);
528 int num = count(target_s_indices, dep_var_stare_indices);
531 Int32 *result =
new Int32(
"result");
533 unique_ptr<Int32> result(
new Int32(
"result"));
534 result->set_value(num);
535 return result.release();
553 StareSubsetFunction::stare_subset_dap4_function(D4RValueList *args, DMR &dmr)
555 if (args->size() != 2) {
557 oss <<
"stare_subset(): Expected two arguments, but got " << args->size();
562 string fullPath = get_sidecar_file_pathname(dmr.filename(), stare_sidecar_suffix);
564 BaseType *dependent_var = args->get_rvalue(0)->value(dmr);
565 BaseType *raw_stare_indices = args->get_rvalue(1)->value(dmr);
568 vector<dods_uint64> dep_var_stare_indices;
569 get_sidecar_uint64_values(fullPath, dependent_var, dep_var_stare_indices);
572 vector<dods_uint64> target_s_indices;
573 read_stare_indices_from_function_argument(raw_stare_indices, target_s_indices);
575 vector<dods_int32> dataset_x_coords;
576 get_sidecar_int32_values(fullPath,
"X", dataset_x_coords);
577 vector<dods_int32> dataset_y_coords;
578 get_sidecar_int32_values(fullPath,
"Y", dataset_y_coords);
580 unique_ptr <stare_matches> subset = stare_subset_helper(target_s_indices, dep_var_stare_indices, dataset_x_coords, dataset_y_coords);
583 if (subset->stare_indices.size() == 0) {
584 subset->stare_indices.push_back(0);
585 subset->target_indices.push_back(0);
586 subset->x_indices.push_back(-1);
587 subset->y_indices.push_back(-1);
590 unique_ptr<Structure> result(
new Structure(
"result"));
592 unique_ptr<Array> stare(
new Array(
"stare",
new UInt64(
"stare")));
593 stare->set_value(&(subset->stare_indices[0]), subset->stare_indices.size());
594 stare->append_dim(subset->stare_indices.size());
595 result->add_var_nocopy(stare.release());
597 unique_ptr<Array> target(
new Array(
"target",
new UInt64(
"target")));
598 target->set_value(&(subset->target_indices[0]), subset->target_indices.size());
599 target->append_dim(subset->target_indices.size());
600 result->add_var_nocopy(target.release());
602 unique_ptr<Array> x(
new Array(
"x",
new Int32(
"x")));
603 x->set_value(subset->x_indices, subset->x_indices.size());
604 x->append_dim(subset->x_indices.size());
605 result->add_var_nocopy(x.release());
607 unique_ptr<Array> y(
new Array(
"y",
new Int32(
"y")));
608 y->set_value(subset->y_indices, subset->y_indices.size());
609 y->append_dim(subset->y_indices.size());
610 result->add_var_nocopy(y.release());
612 return result.release();
616 StareSubsetArrayFunction::stare_subset_array_dap4_function(D4RValueList *args, DMR &dmr)
618 if (args->size() != 3) {
620 oss <<
"stare_subset_array(): Expected three arguments, but got " << args->size();
625 string fullPath = get_sidecar_file_pathname(dmr.filename(), stare_sidecar_suffix);
627 Array *dependent_var =
dynamic_cast<Array*
>(args->get_rvalue(0)->value(dmr));
629 throw BESSyntaxUserError(
"stare_subset_array() expected an Array as the first argument.", __FILE__, __LINE__);
632 BaseType *mask_val_var = args->get_rvalue(1)->value(dmr);
634 Array *raw_stare_indices =
dynamic_cast<Array*
>(args->get_rvalue(2)->value(dmr));
635 if (!raw_stare_indices)
636 throw BESSyntaxUserError(
"stare_subset_array() expected an Array as the third argument.", __FILE__, __LINE__);
639 vector<dods_uint64> dep_var_stare_indices;
640 get_sidecar_uint64_values(fullPath, dependent_var, dep_var_stare_indices);
642 vector<dods_uint64> target_s_indices;
643 read_stare_indices_from_function_argument(raw_stare_indices, target_s_indices);
646 unique_ptr<Array> result(
static_cast<Array*
>(dependent_var->ptr_duplicate()));
649 switch(dependent_var->var()->type()) {
651 build_masked_data<dods_int16>(dependent_var, dep_var_stare_indices, target_s_indices, result);
654 case dods_float32_c: {
655 build_masked_data<dods_float32>(dependent_var, dep_var_stare_indices, target_s_indices, result);
660 throw BESInternalError(
string(
"stare_subset_array() failed: Unsupported array element type (")
661 + dependent_var->var()->type_name() +
").", __FILE__, __LINE__);
664 return result.release();
exception thrown if internal error encountered
error thrown if there is a user syntax error in the request or any other user error
static std::string pathConcat(const std::string &firstPart, const std::string &secondPart, char separator='/')
Concatenate path fragments making sure that they are separated by a single '/' character.
Hold the result from the subset helper function as a collection of vectors.