Last active
September 25, 2019 15:30
-
-
Save ihnorton/39723683ed5dd6694f20ced512ec907f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <tiledb/tiledb> | |
#include <iostream> | |
#include <limits> | |
#include <iomanip> | |
std::string array_path("/tmp/test_cc"); | |
std::string array_path2("/tmp/test_cc_2"); | |
std::string attr_name("a"); | |
using namespace tiledb; | |
void print_results(std::string attr_name, size_t result_num, | |
std::vector<double> coords, std::vector<double> data) { | |
std::cout << "attr_name: " << attr_name << " result_num: " << result_num << std::endl; | |
for (int r = 0; r < result_num; r++) { | |
double i = coords[2 * r], j = coords[2 * r + 1], k = coords[3 * r + 1]; | |
double a = data[r]; | |
std::cout << std::setprecision(17) << "Cell (" << i << ", " << j << ", " << k << ") has data " << a << "\n"; | |
} | |
} | |
int main(int argc, char** argv) { | |
Context ctx; | |
Domain domain(ctx); | |
domain.add_dimension(Dimension::create<double>(ctx, "x", {{-89.75,89.75}}, 1)) | |
.add_dimension(Dimension::create<double>(ctx, "y", {{-179.75,179.75}}, 1)) | |
.add_dimension(Dimension::create<double>(ctx, "z", {{157498.0, 157857.0}}, 1)); | |
ArraySchema schema(ctx, TILEDB_SPARSE); | |
schema.set_domain(domain).set_order({{TILEDB_ROW_MAJOR, TILEDB_ROW_MAJOR}}); | |
schema.add_attribute(Attribute::create<double>(ctx, attr_name)); | |
VFS vfs(ctx); | |
if (vfs.is_dir(array_path)) { | |
vfs.remove_dir(array_path); | |
} | |
Array::create(array_path, schema); | |
{ | |
std::cout << "first write to: " << array_path << std::endl; | |
std::vector<double> coords = {-89.75, -179.75, 157498.0}; | |
std::vector<double> data = {.432321}; | |
Array array(ctx, array_path, TILEDB_WRITE); | |
Query query(ctx, array, TILEDB_WRITE); | |
query.set_layout(TILEDB_UNORDERED) | |
.set_buffer(attr_name, data) | |
.set_coordinates(coords); | |
query.submit(); | |
array.close(); | |
} | |
{ | |
std::cout << "second write to: " << array_path << std::endl; | |
std::vector<double> coords = { | |
-89.24860335195531 , -178.74581005586592 , 157499.0027932961 | |
}; | |
std::vector<double> data = { | |
.7654345 | |
}; | |
Array array(ctx, array_path, TILEDB_WRITE); | |
Query query(ctx, array, TILEDB_WRITE); | |
query.set_layout(TILEDB_UNORDERED) | |
.set_buffer(attr_name, data) | |
.set_coordinates(coords); | |
query.submit(); | |
array.close(); | |
} | |
// read back once | |
{ | |
std::cout << "read from: " << array_path << std::endl; | |
Array array(ctx, array_path, TILEDB_READ); | |
const std::vector<double> subarray = { | |
-89.75, -89.75, | |
-179.75, -179.75, | |
157498.0, 157498.0 | |
}; | |
auto max_el = array.max_buffer_elements(subarray); | |
std::vector<double> data(max_el[attr_name].second); | |
std::vector<double> coords(max_el[TILEDB_COORDS].second); | |
std::cout << " -- got max_el: " << max_el[attr_name].second << std::endl; | |
Query query(ctx, array, TILEDB_READ); | |
query.set_subarray(subarray) | |
.set_layout(TILEDB_ROW_MAJOR) | |
.set_buffer(attr_name, data) | |
.set_coordinates(coords); | |
query.submit(); | |
array.close(); | |
auto result_num = (int)query.result_buffer_elements()[attr_name].second; | |
print_results(attr_name, result_num, coords, data); | |
} | |
std::cout << "----------------------------------------------------------------------------" << std::endl; | |
{ | |
std::cout << "second write to: " << array_path2 << std::endl; | |
VFS vfs(ctx); | |
if (vfs.is_dir(array_path2)) { | |
vfs.remove_dir(array_path2); | |
} | |
Array::create(array_path2, schema); | |
std::vector<double> coords = { | |
-89.75, -179.75, 157498.0, | |
-89.24860335195531 , -178.74581005586592 , 157499.0027932961 | |
}; | |
std::vector<double> data = { | |
.432321, | |
.7654345 | |
}; | |
Array array(ctx, array_path2, TILEDB_WRITE); | |
Query query(ctx, array, TILEDB_WRITE); | |
query.set_layout(TILEDB_UNORDERED) | |
.set_buffer(attr_name, data) | |
.set_coordinates(coords); | |
query.submit(); | |
array.close(); | |
} | |
// read from array_path2 | |
{ | |
std::cout << "read from: " << array_path2 << std::endl; | |
Array array(ctx, array_path2, TILEDB_READ); | |
const std::vector<double> subarray = { | |
-89.75, -89.75, | |
-179.75, -179.75, | |
157498.0, 157498.0 | |
}; | |
// try hard-coding -- then the result_num is 0 instead | |
//size_t max_el = 2; | |
//std::vector<double> data(2); | |
//std::vector<double> coords(2 * 3); | |
try { | |
auto max_el = array.max_buffer_elements(subarray); | |
std::vector<double> data(max_el[attr_name].second); | |
std::vector<double> coords(max_el[TILEDB_COORDS].second); | |
std::cout << " -- got max_el: " << max_el[attr_name].second << std::endl; | |
Query query(ctx, array, TILEDB_READ); | |
// exception here setting the buffer, because max_el is 0 | |
query.set_subarray(subarray) | |
.set_layout(TILEDB_ROW_MAJOR) | |
.set_buffer(attr_name, data) | |
.set_coordinates(coords); | |
query.submit(); | |
array.close(); | |
auto result_num = (int)query.result_buffer_elements()[attr_name].second; | |
print_results(attr_name, result_num, coords, data); | |
} catch (const std::exception& e) { | |
std::cout << "----------------" << std::endl; | |
std::cout << "exception near file: " << __FILE__ << " : " << __LINE__ << std::endl; | |
std::cout << e.what() << std::endl; | |
std::cout << "----------------" << std::endl; | |
} | |
} | |
// read from array_path2 whole domain | |
{ | |
std::cout << "read whole domain from: " << array_path2 << std::endl; | |
Array array(ctx, array_path2, TILEDB_READ); | |
const std::vector<double> subarray = { | |
-89.75, 89.75, | |
-179.75, 179.75, | |
157498.0, 157857.0 | |
}; | |
auto max_el = array.max_buffer_elements(subarray); | |
std::vector<double> data(max_el[attr_name].second); | |
std::vector<double> coords(max_el[TILEDB_COORDS].second); | |
std::cout << " -- got max_el: " << max_el[attr_name].second << std::endl; | |
Query query(ctx, array, TILEDB_READ); | |
query.set_subarray(subarray) | |
.set_layout(TILEDB_ROW_MAJOR) | |
.set_buffer(attr_name, data) | |
.set_coordinates(coords); | |
query.submit(); | |
array.close(); | |
// print out the results. | |
auto result_num = (int)query.result_buffer_elements()[attr_name].second; | |
print_results(attr_name, result_num, coords, data); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment