Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add supports for writing file and attributes #1

Merged
merged 3 commits into from
Jun 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ target_link_libraries(test_cpp LINK_PUBLIC ${TEST_LIBS} ${COMPRESSION_LIBRARIES}
add_executable(test_c main.c z5wrapper.cc)
target_link_libraries(test_c LINK_PUBLIC ${TEST_LIBS} ${COMPRESSION_LIBRARIES})

add_executable(test_attributes test_attributes.c z5wrapper.cc)
target_link_libraries(test_attributes LINK_PUBLIC ${TEST_LIBS} ${COMPRESSION_LIBRARIES})


install(FILES "${CMAKE_CURRENT_SOURCE_DIR}/z5wrapper.h"
DESTINATION "${CMAKE_INSTALL_PREFIX}/include")
109 changes: 109 additions & 0 deletions test_attributes.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
#include <stdlib.h>
#include <assert.h>
#include "z5wrapper.h"

void my_create_dataset(char* arrayName)
{
size_t shape[] = {20, 20, 20};
size_t chunks[] = {10, 10, 10};
size_t offset[] = {0, 0, 0};
size_t offset2[] = {10, 10, 10};
float data1[10][10][10];
float data2[10][10][10];
float rdata[10][10][10];
long long int idata1[10][10][10];
long long int idata2[10][10][10];
long long int irdata[10][10][10];
unsigned int ndim = 3;
for (int i = 0; i < chunks[0]; i++){
for (int j = 0; j < chunks[1]; j++){
for (int k = 0; k<chunks[2]; k++){
data1[i][j][k]=rand()%1000+1;
idata1[i][j][k]=rand()%1000+1;
}
}
}
for (int i = 0; i < chunks[0]; i++){
for (int j = 0; j < chunks[1]; j++){
for (int k = 0; k<chunks[2]; k++){
data2[i][j][k]=rand()%1000+1;
idata2[i][j][k]=rand()%1000+1;
}
}
}
//char* arrayName = "test_c.z5";
int cusezlib = 1;
int level = 1;
z5CreateFloatDataset(arrayName, ndim, shape, chunks, cusezlib, level);
z5WriteFloatSubarray(arrayName, data1, ndim, chunks, offset);
z5ReadFloatSubarray(arrayName, rdata, ndim, chunks, offset);
for (int i = 0; i < chunks[0]; i++){
for (int j = 0; j < chunks[1]; j++)
for (int k = 0; k<chunks[2]; k++)
assert(data1[i][j][k] == rdata[i][j][k]);
printf("data1 = %f\n",data1[i][0][0]);
}
z5WriteFloatSubarray(arrayName, data2, ndim, chunks, offset2);
z5ReadFloatSubarray(arrayName, rdata, ndim, chunks, offset2);
printf("after read float\n");
for (int i = 0; i < chunks[0]; i++){
for (int j = 0; j < chunks[1]; j++)
for (int k = 0; k<chunks[2]; k++)
assert(data2[i][j][k] == rdata[i][j][k]);
printf("data2 = %f\n",data2[i][0][0]);
}
z5writeAttributesString(arrayName, "time", "noon");

float floatval = 42.0;
z5writeAttributesfloat(arrayName, "float", &floatval);

double doubleval = 42.0;
z5writeAttributesdouble(arrayName, "double", &doubleval);

int intval = 42;
z5writeAttributesint(arrayName, "int", &intval);

long int longval = 42;
z5writeAttributeslong(arrayName, "long", &longval);

long long longlongval = 42;
z5writeAttributeslonglong(arrayName, "longlong", &longlongval);

unsigned long long ulonglongval = 42;
z5writeAttributesulonglong(arrayName, "ulonglong", &ulonglongval);

unsigned short ushortval = 42;
z5writeAttributesushort(arrayName, "unshort", &ushortval);

unsigned int uint = 42;
z5writeAttributesuint(arrayName, "uint", &uint);

unsigned short usint = 42;
z5writeAttributesusint(arrayName, "usint", &usint);

short shortint = 42;
z5writeAttributesshort(arrayName, "short", &shortint);

printf("after assert\n");
}

void test_create_file()
{
// create a group
char* fileName = "new_file";
z5CreateFile(fileName);
char* groupName1 = "new_file/group1";
z5CreateGroup(groupName1);
char* groupName2 = "new_file/group2";
z5CreateGroup(groupName2);
char* arrayName = "new_file/group1/array1";
my_create_dataset(arrayName);

}


int main()
{
test_create_file();
return 0;
}
132 changes: 132 additions & 0 deletions z5wrapper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Created by Kevin Paul on 2019-04-24.
//
#include <string>
#include <stdint.h>
#include <vector>
#include <iostream>
#include "z5wrapper.h"
Expand All @@ -10,6 +11,14 @@ namespace fs = boost::filesystem;
namespace z5 {
extern "C" {

void z5CreateFile(char* path)
{
std::string path_s(path);
bool asZarr = true;
handle::File cFile(path_s);
createFile(cFile, asZarr);
}

void z5CreateGroup(char* path) {
std::string path_s(path);
bool asZarr = true;
Expand Down Expand Up @@ -124,6 +133,129 @@ namespace z5 {
file.close();
return fileSize;
}

void z5writeAttributesString(char *path, const char *name, const char *value)
{
std::string path_s(path);
bool asZarr = true;
handle::Handle cHandle(path_s);
nlohmann::json j;
std::string name_s(name);
std::string value_s(value);
j[name_s] = value_s;
writeAttributes(cHandle, j);
}

void z5writeAttributesshort(char *path, const char *name, const short *value)
{
std::string path_s(path);
bool asZarr = true;
handle::Handle cHandle(path_s);
nlohmann::json j;
std::string name_s(name);
j[name_s] = (int64_t) *value;
writeAttributes(cHandle, j);
}

void z5writeAttributesint(char *path, const char *name, const int *value)
{
std::string path_s(path);
bool asZarr = true;
handle::Handle cHandle(path_s);
nlohmann::json j;
std::string name_s(name);
j[name_s] = (int64_t) *value;
writeAttributes(cHandle, j);
}

void z5writeAttributeslong(char *path, const char *name, const long *value)
{
std::string path_s(path);
bool asZarr = true;
handle::Handle cHandle(path_s);
nlohmann::json j;
std::string name_s(name);
j[name_s] = (int64_t) *value;
writeAttributes(cHandle, j);
}

void z5writeAttributesfloat(char *path, const char *name, const float *value)
{
std::string path_s(path);
bool asZarr = true;
handle::Handle cHandle(path_s);
nlohmann::json j;
std::string name_s(name);
j[name_s] = (double) *value;
writeAttributes(cHandle, j);
}

void z5writeAttributesdouble(char *path, const char *name, const double *value)
{
std::string path_s(path);
bool asZarr = true;
handle::Handle cHandle(path_s);
nlohmann::json j;
std::string name_s(name);
j[name_s] = *value;
writeAttributes(cHandle, j);
}

void z5writeAttributesushort(char *path, const char *name, const unsigned short *value)
{
std::string path_s(path);
bool asZarr = true;
handle::Handle cHandle(path_s);
nlohmann::json j;
std::string name_s(name);
j[name_s] = (int64_t) *value;
writeAttributes(cHandle, j);
}

void z5writeAttributesusint(char *path, const char *name, const unsigned short *value)
{
std::string path_s(path);
bool asZarr = true;
handle::Handle cHandle(path_s);
nlohmann::json j;
std::string name_s(name);
j[name_s] = (int64_t) *value;
writeAttributes(cHandle, j);
}

void z5writeAttributeslonglong(char *path, const char *name, const long long *value)
{
std::string path_s(path);
bool asZarr = true;
handle::Handle cHandle(path_s);
nlohmann::json j;
std::string name_s(name);
j[name_s] = (int64_t) *value;
writeAttributes(cHandle, j);
}

void z5writeAttributesulonglong(char *path, const char *name, const unsigned long long *value)
{
std::string path_s(path);
bool asZarr = true;
handle::Handle cHandle(path_s);
nlohmann::json j;
std::string name_s(name);
j[name_s] = (int64_t) *value;
writeAttributes(cHandle, j);
}

void z5writeAttributesuint(char *path, const char *name, const unsigned int *value)
{
std::string path_s(path);
bool asZarr = true;
handle::Handle cHandle(path_s);
nlohmann::json j;
std::string name_s(name);
j[name_s] = (int64_t) *value;
writeAttributes(cHandle, j);
}

void z5Delete(char *path ){
std::string path_s(path);
fs::path filename(path_s);
Expand Down
29 changes: 27 additions & 2 deletions z5wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@

#ifdef __cplusplus
#include "z5/dataset_factory.hxx"
#include "z5/file.hxx"
#include "z5/groups.hxx"
#include "z5/attributes.hxx"
#include "z5/compression/zlib_compressor.hxx"
#include "z5/types/types.hxx"
#endif
Expand All @@ -20,7 +22,8 @@
namespace z5 {
extern "C" {
#endif

void z5CreateFile(char* path);

void z5CreateGroup(char* path);

void z5CreateFloatDataset(char *path, unsigned int ndim, size_t *shape, size_t *chunks, int cuseZlib, int level);
Expand All @@ -37,7 +40,29 @@ namespace z5 {

size_t z5GetFileSize(char *path);

void z5Delete(char *path );
void z5writeAttributesString(char *path, const char *name, const char *value);

void z5writeAttributesshort(char *path, const char *name, const short *value);

void z5writeAttributesint(char *path, const char *name, const int *value);

void z5writeAttributeslong(char *path, const char *name, const long *value);

void z5writeAttributesfloat(char *path, const char *name, const float *value);

void z5writeAttributesdouble(char *path, const char *name, const double *value);

void z5writeAttributesushort(char *path, const char *name, const unsigned short *value);

void z5writeAttributesusint(char *path, const char *name, const unsigned short int *value);

void z5writeAttributeslonglong(char *path, const char *name, const long long *value);

void z5writeAttributesulonglong(char *path, const char *name, const unsigned long long *value);

void z5writeAttributesuint(char *path, const char *name, const unsigned int *value);

void z5Delete(char *path );
#ifdef __cplusplus
}
}
Expand Down