[ismrmrd] 44/177: Added function to append a vector in general.

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Wed Jan 14 20:02:00 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 e343902da7387d28071ab184e819988157634d6a
Author: Souheil Inati <souheil.inati at nih.gov>
Date:   Fri Sep 5 22:23:55 2014 -0400

    Added function to append a vector in general.
---
 examples/c/main.c |  24 ++++++--
 ismrmrd.c         |   4 +-
 ismrmrd_dataset.c | 162 ++++++++++++++++++++++++++++++++++++++++--------------
 3 files changed, 141 insertions(+), 49 deletions(-)

diff --git a/examples/c/main.c b/examples/c/main.c
index c3e7699..068af67 100644
--- a/examples/c/main.c
+++ b/examples/c/main.c
@@ -99,7 +99,25 @@ int main(void)
     
     printf("Pointers 3: %p\t 2: %p\n", (void *) acq3.data, (void *) acq2.data);
     printf("Data 3: %f\t 2: %f\n", creal(acq3.data[4]), creal(acq2.data[4]));
-        
+
+    /* Create and store an image */
+    ISMRMRD_Image im;
+    ismrmrd_init_image(&im);
+    im.head.data_type = ISMRMRD_FLOAT;
+    im.head.matrix_size[0] = 256;
+    im.head.matrix_size[1] = 256;
+    im.head.matrix_size[2] = 4;
+    im.head.channels = 8;
+    /* Add an attribute string */
+    const char *attr_string = "Yo! This is some text for the attribute string.";
+    im.head.attribute_string_len = strlen(attr_string);
+    ismrmrd_make_consistent_image(&im);
+    memcpy(im.attribute_string, attr_string, im.head.attribute_string_len);
+    
+    printf("Image Version: %d\n", im.head.version);
+    printf("Image String: %s\n", im.attribute_string);
+    ismrmrd_append_image(&dataset2, "testimages", 1, &im);
+                         
     /* Clean up */
     /* This frees the internal memory of the acquisitions */
     ismrmrd_cleanup_acquisition(&acq);
@@ -110,10 +128,6 @@ int main(void)
     /* Close the dataset */
     status = ismrmrd_close_dataset(&dataset2);
 
-    //   Image im;
-    //initImage(&im);
-    //printf("Image Version: %d\n", im.head.version);
-
     //NDArray arr;
     //initNDArray(&arr);
     //printf("Array ndim: %d\n", arr.ndim);
diff --git a/ismrmrd.c b/ismrmrd.c
index 583e7c7..61e11bf 100644
--- a/ismrmrd.c
+++ b/ismrmrd.c
@@ -132,8 +132,8 @@ void ismrmrd_free_image(ISMRMRD_Image *im) {
     
 void ismrmrd_init_image(ISMRMRD_Image *im) {
     ismrmrd_init_image_header(&im->head);
-    im->attribute_string = NULL;
-    im->data = NULL;
+    im->attribute_string = (char *) malloc(0); /* can be passed to free */
+    im->data = (void *) malloc(0);
 }
 
 void ismrmrd_cleanup_image(ISMRMRD_Image *im) {
diff --git a/ismrmrd_dataset.c b/ismrmrd_dataset.c
index 369fe86..92a5628 100644
--- a/ismrmrd_dataset.c
+++ b/ismrmrd_dataset.c
@@ -54,13 +54,11 @@ static int create_link(const ISMRMRD_Dataset *dset, const char *link_path) {
 
 static char * make_path(const ISMRMRD_Dataset *dset, const char * var) {
     size_t len = strlen(dset->groupname) + strlen(var) + 2;
-
     char *path = (char *) malloc(len);
     if (path == NULL) {
         ISMRMRD_THROW(ISMRMRD_MEMORYERROR, "Failed to malloc path");
         return NULL;
     }
-
     memset(path, 0, len);
     strcat(path, dset->groupname);
     strcat(path, "/");
@@ -68,6 +66,20 @@ static char * make_path(const ISMRMRD_Dataset *dset, const char * var) {
     return path;
 }
 
+static char * append_to_path(const ISMRMRD_Dataset *dset, const char * path, const char * var) {
+    size_t len = strlen(path) + strlen(var) + 2;
+    char *newpath = (char *) malloc(len);
+    if (newpath == NULL) {
+        ISMRMRD_THROW(ISMRMRD_MEMORYERROR, "Failed to realloc newpath");
+        return NULL;
+    }
+    memset(newpath, 0, len);
+    strcat(newpath, path);
+    strcat(newpath, "/");
+    strcat(newpath, var);
+    return newpath;
+}
+
 static int delete_var(const ISMRMRD_Dataset *dset, const char *var) {
     int status = ISMRMRD_NOERROR;
     herr_t h5status;
@@ -93,13 +105,6 @@ typedef struct HDF5_Acquisition
     hvl_t data;
 } HDF5_Acquisition;
 
-typedef struct HDF5_Image
-{
-    ISMRMRD_ImageHeader head;
-    hvl_t attribute_string;
-    hvl_t data;
-} HDF5_Image;
-
 typedef struct HDF5_NDArray
 {
     uint16_t version;
@@ -114,22 +119,22 @@ static hid_t get_hdf5type_char(void) {
     return datatype;
 }
 
-static hid_t get_hdf5type_ushort(void) {
+static hid_t get_hdf5type_uint16(void) {
     hid_t datatype = H5Tcopy(H5T_NATIVE_UINT16);
     return datatype;
 }
 
-static hid_t get_hdf5type_short(void) {
+static hid_t get_hdf5type_int16(void) {
     hid_t datatype = H5Tcopy(H5T_NATIVE_INT16);
     return datatype;
 }
 
-static hid_t get_hdf5type_uint(void) {
+static hid_t get_hdf5type_uint32(void) {
     hid_t datatype = H5Tcopy(H5T_NATIVE_UINT32);
     return datatype;
 }
     
-static hid_t get_hdf5type_int(void) {
+static hid_t get_hdf5type_int32(void) {
     hid_t datatype = H5Tcopy(H5T_NATIVE_INT32);
     return datatype;
 }
@@ -347,30 +352,6 @@ static hid_t get_hdf5type_image_attribute_string(void) {
     }
     return datatype;
 }
-
-static hid_t get_hdf5type_image(void) {
-    hid_t datatype, vartype, vlvartype;
-    herr_t h5status;
-
-    datatype = H5Tcreate(H5T_COMPOUND, sizeof(HDF5_Image));
-    vartype = get_hdf5type_imageheader();
-    h5status = H5Tinsert(datatype, "head", HOFFSET(HDF5_Image, head), vartype);
-    vartype = get_hdf5type_image_attribute_string();
-    vlvartype = H5Tvlen_create(vartype);
-    h5status = H5Tinsert(datatype, "attribute_string", HOFFSET(HDF5_Image, attribute_string), vlvartype);
-    vartype = get_hdf5type_char();
-    vlvartype = H5Tvlen_create(vartype);
-    h5status = H5Tinsert(datatype, "data", HOFFSET(HDF5_Image, data), vlvartype);
-    
-    H5Tclose(vartype);
-    H5Tclose(vlvartype);
-
-    if (h5status < 0) {
-        ISMRMRD_THROW(ISMRMRD_FILEERROR, "Failed get acquisition data type");
-    }
-
-    return datatype;
-}
     
 static hid_t get_hdf5type_ndarray(void) {
     hid_t datatype, vartype, vlvartype;
@@ -397,6 +378,67 @@ static hid_t get_hdf5type_ndarray(void) {
     return datatype;
 }
 
+int append_to_vector(const ISMRMRD_Dataset * dset, const char * path, const hid_t datatype, void * elem)
+{
+    hid_t dataset, dataspace, props, filespace, memspace;
+    herr_t h5status;
+    hsize_t ext_dims[1] = {1};
+    hsize_t chunk_dims[1] = {1};
+    hsize_t offset[1] = {0};
+    hsize_t dims[1] = {1};
+    hsize_t maxdims[1] = {H5S_UNLIMITED};
+    int ndims;
+
+    /* Check the path, extend or create if needed, and select the last block */
+    if (link_exists(dset, path)) {
+        /* open */
+        dataset = H5Dopen2(dset->fileid, path, H5P_DEFAULT);
+        /* TODO check that the header dataset's datatype is correct */
+        dataspace = H5Dget_space(dataset);
+        ndims = H5Sget_simple_extent_ndims(dataspace);
+        if (ndims != 1) {
+            ISMRMRD_THROW(ISMRMRD_FILEERROR, "Dimensions are incorrect.");
+            return ISMRMRD_FILEERROR;
+        }
+        h5status = H5Sget_simple_extent_dims(dataspace, dims, maxdims);
+        /* extend it by one */
+        dims[0] += 1;
+        h5status = H5Dset_extent(dataset, dims);
+    }
+    else {
+        /* create a new dataset */
+        dataspace = H5Screate_simple(1, dims, maxdims);
+        props = H5Pcreate(H5P_DATASET_CREATE);
+        /* enable chunking so that the dataset is extensible */
+        h5status = H5Pset_chunk (props, 1, chunk_dims);
+        /* create */
+        dataset = H5Dcreate2(dset->fileid, path, datatype, dataspace, H5P_DEFAULT, props,  H5P_DEFAULT);
+        h5status = H5Pclose(props);
+    }
+    /* Select the last block */
+    offset[0] = dims[0]-1;
+    filespace = H5Dget_space(dataset);
+    h5status  = H5Sselect_hyperslab (filespace, H5S_SELECT_SET, offset, NULL, ext_dims, NULL);
+    memspace = H5Screate_simple(1, ext_dims, NULL);
+
+    /* Write it */
+    /* since this is a 1 element array we can just pass the pointer to the header */
+    h5status = H5Dwrite(dataset, datatype, H5S_ALL, H5S_ALL, H5P_DEFAULT, elem);
+
+    /* Clean up */
+    h5status = H5Sclose(dataspace);
+    h5status = H5Sclose(filespace);
+    h5status = H5Sclose(memspace);
+    h5status = H5Dclose(dataset);
+
+    if (h5status < 0) {
+        ISMRMRD_THROW(ISMRMRD_FILEERROR, "Failed to get number of acquisitions.");
+        return ISMRMRD_FILEERROR;
+    }
+
+    return ISMRMRD_NOERROR;
+}
+
 /********************/
 /* Public functions */
 /********************/
@@ -683,7 +725,7 @@ int ismrmrd_append_acquisition(const ISMRMRD_Dataset *dset, const ISMRMRD_Acquis
     free(path);
     
     if (h5status < 0) {
-        ISMRMRD_THROW(ISMRMRD_FILEERROR, "Failed to append acquisitions.");
+        ISMRMRD_THROW(ISMRMRD_FILEERROR, "Failed to append acquisition.");
         return ISMRMRD_FILEERROR;
     }
 
@@ -750,16 +792,52 @@ int ismrmrd_read_acquisition(const ISMRMRD_Dataset *dset, unsigned long index, I
     return ISMRMRD_NOERROR;
 };
 
-/*****************************/
-/* TODO Implement these ones */  
-/*****************************/
-
 int ismrmrd_append_image(const ISMRMRD_Dataset *dset, const char *varname,
                          const int block_mode, const ISMRMRD_Image *im) {
+
+    int status;
+    hid_t datatype;
+
+    /* The group for this set of images */
+    /* /groupname/varname */
+    char *path = make_path(dset, varname);
+    /* Make sure the path exists */
+    status = create_link(dset, path);        
+
+    /* Handle the header */
+    char *headerpath = append_to_path(dset, path, "header");
+    datatype = get_hdf5type_imageheader();
+    status = append_to_vector(dset, headerpath, datatype, (void *) &im->head);
+    if (status != ISMRMRD_NOERROR) {
+        ISMRMRD_THROW(ISMRMRD_FILEERROR, "Failed to append image header.");
+        return ISMRMRD_FILEERROR;
+    }
+    free(headerpath);
+    
+    /* Handle the attribute string */
+    char *attrpath = append_to_path(dset, path, "attributes");
+    datatype = get_hdf5type_image_attribute_string();
+    status = append_to_vector(dset, attrpath, datatype, (void *) &im->attribute_string);
+    if (status != ISMRMRD_NOERROR) {
+        ISMRMRD_THROW(ISMRMRD_FILEERROR, "Failed to append image attribute string.");
+        return ISMRMRD_FILEERROR;
+    }
+    free(attrpath);
+    
+    /* Handle the data */
+    char *datapath = append_to_path(dset, path, "data");
+    free(datapath);
+
+    /* Final cleanup */
+    H5Tclose(datatype);
+    free(path);
     
     return ISMRMRD_NOERROR;
 };
 
+/*****************************/
+/* TODO Implement these ones */  
+/*****************************/
 int ismrmrd_read_image(const ISMRMRD_Dataset *dset, const char *varname,
                        const unsigned long index, ISMRMRD_Image *im) {
     return ISMRMRD_NOERROR;

-- 
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