[ismrmrd] 14/177: Can read and write XML header.
Ghislain Vaillant
ghisvail-guest at moszumanska.debian.org
Wed Jan 14 20:01:56 UTC 2015
This is an automated email from the git hooks/post-receive script.
ghisvail-guest pushed a commit to annotated tag v1.1.0.beta.1
in repository ismrmrd.
commit dc09e2d42bac142e8874e0b3a1348297bf89d38f
Author: Souheil Inati <souheil.inati at nih.gov>
Date: Wed Jul 30 14:36:01 2014 -0400
Can read and write XML header.
---
ismrmrd_dataset.c | 127 +++++++++++++++++++++++++++++++++++++++++++++------
ismrmrd_dataset.h | 6 +--
tests/c/basic_test.c | 26 +++++++----
3 files changed, 134 insertions(+), 25 deletions(-)
diff --git a/ismrmrd_dataset.c b/ismrmrd_dataset.c
index fe430e7..7731435 100644
--- a/ismrmrd_dataset.c
+++ b/ismrmrd_dataset.c
@@ -37,8 +37,7 @@ static bool link_exists(const ISMRMRD_Dataset *dset, const char *link_path) {
}
}
-static int create_link(ISMRMRD_Dataset *dset, const char *link_path) {
-
+static int create_link(const ISMRMRD_Dataset *dset, const char *link_path) {
if (link_exists(dset, link_path)) {
return ISMRMRD_NOERROR;
}
@@ -46,19 +45,40 @@ static int create_link(ISMRMRD_Dataset *dset, const char *link_path) {
hid_t lcpl_id;
lcpl_id = H5Pcreate(H5P_LINK_CREATE);
H5Pset_create_intermediate_group(lcpl_id, 1);
- H5Gcreate2(dset->fileid, link_path, lcpl_id, H5P_DEFAULT, H5P_DEFAULT);
+ hid_t gid = H5Gcreate2(dset->fileid, link_path, lcpl_id, H5P_DEFAULT, H5P_DEFAULT);
+ H5Gclose(gid);
// TODO does this thing return error
return ISMRMRD_NOERROR;
}
+}
+static int delete_var(const ISMRMRD_Dataset *dset, const char *var) {
+ herr_t status;
+ char *path;
+ path = (char *) malloc(strlen(dset->groupname)+strlen(var)+2);
+ memset(path, '\0', strlen(dset->groupname)+strlen(var)+2);
+ strcat(path, dset->groupname);
+ strcat(path, "/");
+ strcat(path, var);
+ if (link_exists(dset, path)) {
+ status = H5Ldelete(dset->fileid, path, H5P_DEFAULT);
+ // TODO handle errors
+ }
+ free(path);
+ return ISMRMRD_NOERROR;
}
/********************/
/* Public functions */
/********************/
-void ismrmrd_init_dataset(ISMRMRD_Dataset *dset) {
- dset->filename = NULL;
- dset->groupname = NULL;
+void ismrmrd_init_dataset(ISMRMRD_Dataset *dset, const char *filename, const char *groupname) {
+
+ dset->filename = (char *) realloc(dset->filename, (strlen(filename)+1)*sizeof(char));
+ strcpy(dset->filename, filename);
+
+ dset->groupname = (char *) realloc(dset->groupname, (strlen(groupname)+1)*sizeof(char));
+ strcpy(dset->groupname, groupname);
+
dset->fileid = 0;
}
@@ -115,7 +135,7 @@ int ismrmrd_open_dataset(ISMRMRD_Dataset *dset, const bool create_if_needed) {
}
}
- /* Open the dataset exists, create if needed */
+ /* Open the existing dataset */
/* insure that /groupname exists */
int val = create_link(dset, dset->groupname);
@@ -124,7 +144,7 @@ int ismrmrd_open_dataset(ISMRMRD_Dataset *dset, const bool create_if_needed) {
int ismrmrd_close_dataset(ISMRMRD_Dataset *dset) {
- herr_t status;
+ herr_t status;
/* Check for a valid fileid before trying to close the file */
if (dset->fileid > 0) {
@@ -135,17 +155,96 @@ int ismrmrd_close_dataset(ISMRMRD_Dataset *dset) {
};
-/*****************************/
-/* TODO Implement these ones */
-/*****************************/
-int ismrmrd_write_xml_header(const ISMRMRD_Dataset *dset, const char *xml) {
+int ismrmrd_write_header(const ISMRMRD_Dataset *dset, const char *xmlstring) {
+
+ hid_t dataset, dataspace, datatype, props;
+ hsize_t dims[] = {1};
+ herr_t status;
+
+ /* The path to the xml header */
+ const char *var = "xml";
+ char *path;
+ path = (char *) malloc(strlen(dset->groupname)+strlen(var)+2);
+ memset(path, '\0', strlen(dset->groupname)+strlen(var)+2);
+ strcat(path, dset->groupname);
+ strcat(path, "/");
+ strcat(path, var);
+
+ /* Delete the old header if it exists */
+ status = delete_var(dset, var);
+
+ /* Create a new dataset for the xmlstring */
+ /* i.e. create the memory type, data space, and data set */
+ dataspace = H5Screate_simple(1, dims, NULL);
+ datatype = H5Tcopy(H5T_C_S1);
+ status = H5Tset_size(datatype, H5T_VARIABLE);
+ props = H5Pcreate (H5P_DATASET_CREATE);
+ dataset = H5Dcreate(dset->fileid, path, datatype, dataspace, H5P_DEFAULT, props, H5P_DEFAULT);
+
+ /* Write it out */
+ /* We have to wrap the xmlstring in an array */
+ void *buff[1];
+ buff[0] = (void *) xmlstring; /* safe to get rid of const the type */
+ status = H5Dwrite(dataset, datatype, H5S_ALL, H5S_ALL, H5P_DEFAULT, buff);
+
+ /* Clean up */
+ status = H5Pclose(props);
+ status = H5Tclose(datatype);
+ status = H5Sclose(dataspace);
+ status = H5Dclose(dataset);
+
return ISMRMRD_NOERROR;
};
-int ismrmrd_read_xml_header(const ISMRMRD_Dataset *dset, char *xml) {
- return ISMRMRD_NOERROR;
+char * ismrmrd_read_header(const ISMRMRD_Dataset *dset) {
+
+ hid_t dataset, dataspace, datatype, props;
+ hsize_t dims[] = {1};
+ herr_t status;
+ char * xmlstring;
+
+ /* The path to the xml header */
+ const char *var = "xml";
+ char *path;
+ path = (char *) malloc(strlen(dset->groupname)+strlen(var)+2);
+ memset(path, '\0', strlen(dset->groupname)+strlen(var)+2);
+ strcat(path, dset->groupname);
+ strcat(path, "/");
+ strcat(path, var);
+
+ if (link_exists(dset, path)) {
+ dataset = H5Dopen(dset->fileid, path, H5P_DEFAULT);
+ /* Get the datatype */
+ datatype = H5Dget_type(dataset);
+ /* Read it into a 1D buffer*/
+ void *buff[1];
+ status = H5Dread(dataset, datatype, H5S_ALL, H5S_ALL, H5P_DEFAULT, buff);
+
+ /* Unpack */
+ xmlstring = (char *) malloc(strlen(buff[0])+1);
+ memcpy(xmlstring, buff[0], strlen(buff[0])+1);
+
+ /* Clean up */
+ status = H5Pclose(props);
+ status = H5Tclose(datatype);
+ status = H5Sclose(dataspace);
+ status = H5Dclose(dataset);
+ free(path);
+
+ return xmlstring;
+ }
+ else {
+ // No XML String found
+ // TODO handle errors
+ free(path);
+ return NULL;
+ }
+
};
+/*****************************/
+/* TODO Implement these ones */
+/*****************************/
int ismrmrd_append_acquisition(const ISMRMRD_Dataset *dset, const ISMRMRD_Acquisition *a) {
return ISMRMRD_NOERROR;
};
diff --git a/ismrmrd_dataset.h b/ismrmrd_dataset.h
index 8787485..ed810a7 100644
--- a/ismrmrd_dataset.h
+++ b/ismrmrd_dataset.h
@@ -38,7 +38,7 @@ typedef struct ISMRMRD_Dataset {
* Initializes an ISMRMRD dataset structure
*
*/
-void ismrmrd_init_dataset(ISMRMRD_Dataset *dset);
+void ismrmrd_init_dataset(ISMRMRD_Dataset *dset, const char *filename, const char *groupname);
/**
* Opens an ISMRMRD dataset.
@@ -58,7 +58,7 @@ int ismrmrd_close_dataset(ISMRMRD_Dataset *dset);
* @warning There is no check of whether the string is a valid XML document at this point.
*
*/
-int ismrmrd_write_xml_header(const ISMRMRD_Dataset *dset, const char *xml);
+int ismrmrd_write_header(const ISMRMRD_Dataset *dset, const char *xmlstring);
/**
* Reads the XML configuration header from the dataset.
@@ -66,7 +66,7 @@ int ismrmrd_write_xml_header(const ISMRMRD_Dataset *dset, const char *xml);
* @warning There is no check of whether the string is a valid XML document at this point.
*
*/
-int ismrmrd_read_xml_header(const ISMRMRD_Dataset *dset, char *xml);
+char * ismrmrd_read_header(const ISMRMRD_Dataset *dset);
/**
* Appends and NMR/MRI acquisition to the dataset.
diff --git a/tests/c/basic_test.c b/tests/c/basic_test.c
index 1ceab30..5db4ac2 100644
--- a/tests/c/basic_test.c
+++ b/tests/c/basic_test.c
@@ -1,6 +1,9 @@
#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
#include "ismrmrd.h"
+#include "ismrmrd_dataset.h"
int main(void)
{
@@ -28,23 +31,30 @@ int main(void)
//printf("Array ndim: %d\n", arr.ndim);
//printf("Array dim[0]: %d\n", arr.dims[0]);
+ const char *filename = "myfile.h5";
+ const char *groupname = "/G1/V";
+
ISMRMRD_Dataset dataset;
- ismrmrd_init_dataset(&dataset);
- dataset.filename = "myfile.h5";
- dataset.groupname = "/G1/V";
+ ismrmrd_init_dataset(&dataset, filename, groupname);
+ printf("Filename: %s\n", dataset.filename);
+ printf("Groupname: %s\n", dataset.groupname);
int status;
status = ismrmrd_open_dataset(&dataset, true);
printf("Status from open: %d\n", status);
printf("File_id: %d\n", dataset.fileid);
+ const char *xmlhdr="Hello world.";
+ status = ismrmrd_write_header(&dataset, xmlhdr);
+ printf("XMLString: %s\nLength: %lu\n", xmlhdr, strlen(xmlhdr));
+
+ char *xmlstring = ismrmrd_read_header(&dataset);
+ printf("XMLString OUT: %s\n", xmlstring);
+ free(xmlstring);
+
status = ismrmrd_close_dataset(&dataset);
printf("File_id: %d\n", dataset.fileid);
printf("File close status: %d\n", status);
-
- //const char *filename="test.h5";
- //const char *datasetname="Today";
- //const char *xmlstring = "This is the end my friend.";
-
+
return 0;
}
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/ismrmrd.git
More information about the debian-science-commits
mailing list