[cig-commits] r6820 - in cs/cigma/trunk: include/cigma src

luis at geodynamics.org luis at geodynamics.org
Wed May 9 17:06:09 PDT 2007


Author: luis
Date: 2007-05-09 17:06:09 -0700 (Wed, 09 May 2007)
New Revision: 6820

Added:
   cs/cigma/trunk/include/cigma/dataset.h
   cs/cigma/trunk/src/dataset.c
Log:
These are convenient wrappers around the usual HDF5 dataset API calls.



Added: cs/cigma/trunk/include/cigma/dataset.h
===================================================================
--- cs/cigma/trunk/include/cigma/dataset.h	2007-05-10 00:02:46 UTC (rev 6819)
+++ cs/cigma/trunk/include/cigma/dataset.h	2007-05-10 00:06:09 UTC (rev 6820)
@@ -0,0 +1,53 @@
+#ifndef __CIGMA_DATASET_H__
+#define __CIGMA_DATASET_H__
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <hdf5.h>
+
+
+hid_t dataset_open(hid_t loc_id, const char *name,
+                   int *rank, int *shape, int *npoints);
+
+
+int dataset_read(hid_t loc_id, const char *name, hid_t type_id,
+               int rank, int *shape, void **data);
+
+
+int dataset_write(hid_t loc_id, const char *name, hid_t type_id,
+                int rank, hsize_t *dims, void *data);
+
+
+int dataset_read1(hid_t loc_id, const char *name, hid_t type_id,
+                void **data, int *n);
+
+int dataset_read2(hid_t loc_id, const char *name, hid_t type_id,
+                void **data, int *m, int *n);
+
+int dataset_read3(hid_t loc_id, const char *name, hid_t type_id,
+                void **data, int *l, int *m, int *n);
+
+
+int dataset_write1(hid_t loc_id, const char *name, hid_t type_id,
+                 void *data, int n);
+
+int dataset_write2(hid_t loc_id, const char *name, hid_t type_id,
+                 void *data, int m, int n);
+
+int dataset_write3(hid_t loc_id, const char *name, hid_t type_id,
+                 void *data, int l, int m, int n);
+
+
+int dataset_read3_slice(hid_t loc_id, const char *name, hid_t type_id,
+                      int rank, hsize_t *dims, void *data,
+                      int start, int end);
+
+int dataset_write3_slice(hid_t loc_id, const char *name, hid_t type_id,
+                       int start, int end);
+
+
+#ifdef __cplusplus
+}
+#endif
+#endif

Added: cs/cigma/trunk/src/dataset.c
===================================================================
--- cs/cigma/trunk/src/dataset.c	2007-05-10 00:02:46 UTC (rev 6819)
+++ cs/cigma/trunk/src/dataset.c	2007-05-10 00:06:09 UTC (rev 6820)
@@ -0,0 +1,192 @@
+#include <stdlib.h>
+#include <hdf5.h>
+#include <cigma/dataset.h>
+
+hid_t dataset_open(hid_t loc_id, const char *name,
+                 int *rank, int *shape, int *npoints)
+{
+    hid_t dataset_id;
+    hid_t dataspace_id;
+    hsize_t *dims;
+    herr_t status;
+    int i;
+
+    dataset_id = H5Dopen(loc_id, name);
+    if (dataset_id < 0)
+    {
+        return -1;
+    }
+    
+    if ((npoints != NULL) || (rank != NULL) || (shape != NULL))
+    {
+        dataspace_id = H5Dget_space(dataset_id);
+
+        if (rank != NULL)
+        {
+            *rank = H5Sget_simple_extent_ndims(dataspace_id);
+        }
+
+        if (npoints != NULL)
+        {
+            *npoints = H5Sget_simple_extent_npoints(dataspace_id);
+        }
+
+        if (shape != NULL)
+        {
+            dims = (hsize_t *)malloc((*rank) * sizeof(hsize_t));
+            status = H5Sget_simple_extent_dims(dataspace_id, dims, NULL);
+            for (i = 0; i < *rank; i++)
+            {
+                shape[i] = (int)dims[i];
+            }
+            free(dims);
+        }
+
+        status = H5Sclose(dataspace_id);
+    }
+
+    return dataset_id;
+}
+
+int dataset_read(hid_t loc_id, const char *name, hid_t type_id,
+               int rank, int *shape, void **data)
+{
+    int dset_rank;
+    int dset_npts;
+    hid_t dataset_id;
+    herr_t status;
+
+    dataset_id = dataset_open(loc_id, name, &dset_rank, shape, &dset_npts);
+    if (dataset_id < 0)
+    {
+        return -1;
+    }
+    if (rank != dset_rank)
+    {
+        H5Dclose(dataset_id);
+        return -2;
+    }
+
+    *data = malloc(dset_npts * H5Tget_size(type_id));
+    if (*data == NULL)
+    {
+        H5Dclose(dataset_id);
+        return -3;
+    }
+
+    status = H5Dread(dataset_id, type_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, *data);
+    if (status < 0)
+    {
+        H5Dclose(dataset_id);
+        return -4;
+    }
+
+    status = H5Dclose(dataset_id);
+
+    return 0;
+}
+
+int dataset_write(hid_t loc_id, const char *name, hid_t type_id,
+                int rank, hsize_t *dims, void *data)
+{
+    hid_t dataspace_id;
+    hid_t dataset_id;
+    herr_t status;
+
+    dataspace_id = H5Screate_simple(2, dims, NULL);
+    if (dataspace_id < 0)
+    {
+        return -1;
+    }
+
+    dataset_id = H5Dcreate(loc_id, name, type_id, dataspace_id,
+                           H5P_DEFAULT);
+    if (dataset_id < 0)
+    {
+        H5Sclose(dataspace_id);
+        return -2;
+    }
+
+    status = H5Dwrite(dataset_id, type_id, H5S_ALL, H5S_ALL,
+                      H5P_DEFAULT, data);
+    if (status < 0)
+    {
+        H5Sclose(dataspace_id);
+        H5Dclose(dataset_id);
+        return -3;
+    }
+    
+    H5Sclose(dataspace_id);
+    H5Dclose(dataset_id);
+    return 0;
+}
+
+
+int dataset_read1(hid_t loc_id, const char *name, hid_t type_id,
+                void **data, int *n)
+{
+    return dataset_read(loc_id, name, type_id, 1, n, data);
+}
+
+int dataset_read2(hid_t loc_id, const char *name, hid_t type_id,
+                void **data, int *m, int *n)
+{
+    int shape[2];
+    int ret;
+    
+    ret = dataset_read(loc_id, name, type_id, 2, shape, data);
+
+    *m = shape[0];
+    *n = shape[1];
+
+    return ret;
+}
+
+int dataset_read3(hid_t loc_id, const char *name, hid_t type_id,
+                void **data, int *l, int *m, int *n)
+{
+    int shape[3];
+    int ret;
+
+    ret = dataset_read(loc_id, name, type_id, 3, shape, data);
+
+    *l = shape[0];
+    *m = shape[1];
+    *n = shape[2];
+
+    return ret;
+}
+
+
+int dataset_write1(hid_t loc_id, const char *name, hid_t type_id,
+                 void *data, int n)
+{
+    hsize_t dims[1];
+
+    dims[0] = n;
+
+    return dataset_write(loc_id, name, type_id, 1, dims, data);
+}
+
+int dataset_write2(hid_t loc_id, const char *name, hid_t type_id,
+                 void *data, int m, int n)
+{
+    hsize_t dims[2];
+
+    dims[0] = m;
+    dims[1] = n;
+
+    return dataset_write(loc_id, name, type_id, 2, dims, data);
+}
+
+int dataset_write3(hid_t loc_id, const char *name, hid_t type_id,
+                 void *data, int l, int m, int n)
+{
+    hsize_t dims[3];
+
+    dims[0] = l;
+    dims[1] = m;
+    dims[2] = n;
+
+    return dataset_write(loc_id, name, type_id, 3, dims, data);
+}



More information about the cig-commits mailing list