changeset 1613:c2c7af395bc6

*** empty log message ***
author baghdadi <baghdadi>
date Wed, 07 Jan 2004 17:16:34 +0000
parents 1a2decaf408a
children 44ddaa74b103
files libsrc2/dimension.c libsrc2/minc2_private.h libsrc2/volume.c
diffstat 3 files changed, 119 insertions(+), 97 deletions(-) [+]
line wrap: on
line diff
--- a/libsrc2/dimension.c
+++ b/libsrc2/dimension.c
@@ -290,14 +290,17 @@
   hid_t hdf_file, hdf_type;	
   hid_t hdf_dims_grp;
   hid_t dataset, attribute;
+  hid_t dataset_width;
   hsize_t number_of_dims; 
   herr_t status;
   char *name;
+  char *name_width;
   ssize_t size_of_obj;
   midimclass_t dim_class;
   midimattr_t  dim_attr;
   double *direction_cosines;
-  
+  ssize_t name_len;   /* Length of an object's name */
+
   int i=0, j=0, max_dims;
   
   if (volume == NULL) {
@@ -338,16 +341,17 @@
      which one has a matching class and attirbute.
    */
   for (i=0; i< max_dims; i++) {
-    /* Get the name of the dimension by providing its index
+    /* Get the length of the name of the dimension by providing its index
        to the following function.
      */
-    //PROBLEM FOUND WITH HDF H5Gget_objname_by_idx
-    // DOES NOT RETURN THE CORRECT SIZE OF THE OBJECT when name
-    // is set to NULL according to the documentation
-    // DOES NOT ALLOCATE SPACE FOR NAME
-    // LEFT malloc with MAX CHAR LENGTH until the problem is fixed
-    // EMAILED  Quincey Koziol DEC 22 /2003
-    name = malloc(MI2_CHAR_LENGTH);
+    name_len=H5Gget_objname_by_idx(hdf_dims_grp, i, NULL, 0);
+    
+    if (name_len < MI2_CHAR_LENGTH) {
+      name = malloc(name_len + 1);
+    }
+    else {
+      name = malloc(MI2_CHAR_LENGTH);
+    }
     size_of_obj = H5Gget_objname_by_idx(hdf_dims_grp, i, name, MI2_CHAR_LENGTH);
     
     if (size_of_obj < 0 || name == NULL) {
@@ -440,36 +444,37 @@
 	}
 	
 	if ( dim_attr == MI_DIMATTR_NOT_REGULARLY_SAMPLED) {
-	  /* Attach to offsets attribute using its name
-	     ONLY IF an IRREGULAR SAMPLE is specified.
-           */
-	  attribute = H5Aopen_name(dataset, "offsets");
-	  if (attribute < 0) {
-	    return (MI_ERROR);
-	  }
-	
+
 	  handle->offsets = (double *) malloc(handle->length *sizeof(double));
-	  status = H5Aread(attribute, H5T_NATIVE_DOUBLE, handle->offsets);
+	  status = H5Dread(dataset, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, 
+			 H5P_DEFAULT, handle->offsets);
 	  if (status < 0) {
 	    return (MI_ERROR);
 	  }
 	  printf( " OFFSETS %f %f %f \n", handle->offsets[0], handle->offsets[1], handle->offsets[2]);
-	  /* Attach to widths attribute using its name
-	     ONLY IF an IRREGULAR SAMPLE is specified.
-           */
-	  attribute = H5Aopen_name(dataset, "widths");
-	  if (attribute < 0) {
-	    return (MI_ERROR);
+	  
+	  name_len = strlen(name) + 6;
+	  if (name_len < MI2_CHAR_LENGTH) {
+	    name_width = malloc(name_len);
+	  }
+	  else {
+	    name_width = malloc(MI2_CHAR_LENGTH);
 	  }
-	
+	  strcpy(name_width, name);
+	  strcat(name_width, "-width");
+	  dataset_width = H5Dopen(hdf_dims_grp, name_width);
+	  if (dataset_width < 0) {
+	     return (MI_ERROR);
+	  }
 	  handle->widths = (double *) malloc(handle->length *sizeof(double));
-	  status = H5Aread(attribute, H5T_NATIVE_DOUBLE, handle->widths);
+	  status = H5Dread(dataset_width, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, 
+			 H5P_DEFAULT, handle->widths);
 	  if (status < 0) {
 	    return (MI_ERROR);
 	  }
 	  printf( " WIDTHS %f %f %f \n", handle->widths[0], handle->widths[1], handle->widths[2]);
+	  
 	}
-
 	/* Attach to sampling_flag attribute using its name
          */
 	attribute = H5Aopen_name(dataset, "sampling_flag");
@@ -538,7 +543,7 @@
 	dimensions[j] = handle;
 	j++;
       }
-    } 
+    }
   }
   
   return (MI_NOERROR);
--- a/libsrc2/minc2_private.h
+++ b/libsrc2/minc2_private.h
@@ -1,3 +1,4 @@
+
 /** The root of all MINC 2.0 objects in the HDF5 hierarchy.
  */
 #define MI_ROOT_PATH "/minc-2.0"
--- a/libsrc2/volume.c
+++ b/libsrc2/volume.c
@@ -21,14 +21,17 @@
   hid_t file_id;
   hid_t hdf_attr, hdf_type;
   hid_t hdf_plist;
+  hid_t fspc_id;
   hsize_t dim[1];
   hid_t grp_root_id, grp_image_id;
   hid_t grp_fullimage_id, grp_dimensions_id; 
-
+  herr_t status;
   hid_t dimage_id = -1;
   hid_t dataset_id = -1;
+  hid_t dataset_width = -1;
   hid_t dataspace_id = -1;
-  
+  char *name;
+  int size;
   hsize_t hdf_chunk_size[MI2_MAX_BLOCK_EDGES];
   volumehandle *handle;
   volprops *props_handle;
@@ -42,7 +45,7 @@
   hdf_type = mitype_to_hdftype(volume_type);
 
   /* Create file in HDF5 with the given filename and
-     H5F_AA_TRUNC: Truncate file, if it already exists, 
+     H5F_ACC_TRUNC: Truncate file, if it already exists, 
                    erasing all data previously stored in the file.
      and create ID and ID access as default.
   */
@@ -145,55 +148,94 @@
   }
    
   for (i=0; i < number_of_dimensions ; i++) {
-    
+    /* First create the dataspace required to create a 
+       dimension variable (dataset)
+     */
     if (dimensions[i]->attr == MI_DIMATTR_REGULARLY_SAMPLED) {
-      /* Dimension variable for a regular dimension contains
-	 no meaningful data.
-      */
       dataspace_id = H5Screate(H5S_SCALAR);
     }
-    else {
-      /* Dimension variable for an irregular dimension contains
-	 a vector with the lengths equal to the sampled points
-	 along the dimension.
-      */
+    else if (dimensions[i]->attr == MI_DIMATTR_NOT_REGULARLY_SAMPLED){
+      
       dim[0] = dimensions[i]->length;
       dataspace_id = H5Screate_simple(1, dim, NULL);
     }
+    else {
+      return (MI_ERROR);
+    }
+
     if (dataspace_id < 0) {
       return (MI_ERROR);
     }
+    
+    /* Create a dataset(dimension variable name) in DIMENSIONS GROUP */
+    dataset_id = H5Dcreate(grp_dimensions_id, dimensions[i]->name, H5T_NATIVE_DOUBLE, dataspace_id, H5P_DEFAULT);
+    
+    /* Dimension variable for a regular dimension contains
+       no meaningful data. Whereas, Dimension variable for 
+       an irregular dimension contains a vector with the lengths 
+       equal to the sampled points along the dimension.
+       Also, create a variable named "<dimension>-width" and 
+       write the dimension->widths.
+     */
+    
+    if (dimensions[i]->attr == MI_DIMATTR_NOT_REGULARLY_SAMPLED) {
+      fspc_id = H5Dget_space(dataset_id);
+      if (fspc_id < 0) {
+	return (MI_ERROR);
+      }
+      status = H5Dwrite(dataset_id, H5T_NATIVE_DOUBLE, dataspace_id, fspc_id, H5P_DEFAULT, dimensions[i]->offsets);
+      if (status < 0) {
+	return (MI_ERROR);
+      }
+      size = strlen(dimensions[i]->name) + 6;
+      if (size <= MI2_CHAR_LENGTH) {
+	name = malloc(size);
+      }
+      else {
+	name = malloc(MI2_CHAR_LENGTH);
+      }
+      strcpy(name, dimensions[i]->name);
+      strcat(name, "-width");
+      dataset_width = H5Dcreate(grp_dimensions_id, name, H5T_NATIVE_DOUBLE, dataspace_id, H5P_DEFAULT);
+      fspc_id = H5Dget_space(dataset_width);
+      if (fspc_id < 0) {
+	return (MI_ERROR);
+      }
+      status = H5Dwrite(dataset_width, H5T_NATIVE_DOUBLE, dataspace_id, fspc_id, H5P_DEFAULT, dimensions[i]->widths);
+      if (status < 0) {
+	return (MI_ERROR);
+      }
+      H5Dclose(dataset_width);
+    }
+    
+    /* Create Dimension attribute  "attr" */
+    dataspace_id = H5Screate(H5S_SCALAR);
+    /* Create attribute. */
+    hdf_attr = H5Acreate(dataset_id, "attr", H5T_NATIVE_INT, dataspace_id, H5P_DEFAULT);
+    if (hdf_attr < 0) {
+      return (MI_ERROR);
+    }
   
-    /* Create a dataset(dimension's name) in DIMENSIONS GROUP */
-    dataset_id = H5Dcreate(grp_dimensions_id, dimensions[i]->name, hdf_type, dataspace_id, H5P_DEFAULT);
-    /* Create Dimension attribute  "attr" */
-   dataspace_id = H5Screate(H5S_SCALAR);
-   /* Create attribute. */
-   hdf_attr = H5Acreate(dataset_id, "attr", H5T_NATIVE_INT, dataspace_id, H5P_DEFAULT);
-   if (hdf_attr < 0) {
-     return (MI_ERROR);
-   }
-  
-   /* Write data to the attribute. */
-   H5Awrite(hdf_attr, H5T_NATIVE_INT, &dimensions[i]->attr);
-   /* Close attribute dataspace. */
-   H5Sclose(dataspace_id); 
-   /* Close attribute. */
-   H5Aclose(hdf_attr);
+    /* Write data to the attribute. */
+    H5Awrite(hdf_attr, H5T_NATIVE_INT, &dimensions[i]->attr);
+    /* Close attribute dataspace. */
+    H5Sclose(dataspace_id); 
+    /* Close attribute. */
+    H5Aclose(hdf_attr);
 
-   /* Create Dimension attribute  "class" */
-   dataspace_id = H5Screate(H5S_SCALAR);
-   /* Create attribute. */
-   hdf_attr = H5Acreate(dataset_id, "class", H5T_NATIVE_INT, dataspace_id, H5P_DEFAULT);
-   if (hdf_attr < 0) {
-     return (MI_ERROR);
-   }
-   /* Write data to the attribute. */
-   H5Awrite(hdf_attr, H5T_NATIVE_INT, &dimensions[i]->class);
-   /* Close attribute dataspace. */
-   H5Sclose(dataspace_id); 
-   /* Close attribute. */
-   H5Aclose(hdf_attr);
+    /* Create Dimension attribute  "class" */
+    dataspace_id = H5Screate(H5S_SCALAR);
+    /* Create attribute. */
+    hdf_attr = H5Acreate(dataset_id, "class", H5T_NATIVE_INT, dataspace_id, H5P_DEFAULT);
+    if (hdf_attr < 0) {
+      return (MI_ERROR);
+    }
+    /* Write data to the attribute. */
+    H5Awrite(hdf_attr, H5T_NATIVE_INT, &dimensions[i]->class);
+    /* Close attribute dataspace. */
+    H5Sclose(dataspace_id); 
+    /* Close attribute. */
+    H5Aclose(hdf_attr);
    
    /* Create Dimension attribute "direction_cosines"  */
    dim[0] = 3;
@@ -220,20 +262,6 @@
    /* Close attribute. */
    H5Aclose(hdf_attr);
 
-   if (dimensions[i]->offsets != NULL) {
-     /* Create Dimension attribute "offsets"  */
-     dim[0] = dimensions[i]->length;
-     dataspace_id = H5Screate_simple(1, dim, NULL);
-     hdf_attr = H5Acreate(dataset_id, "offsets", H5T_NATIVE_DOUBLE, dataspace_id, H5P_DEFAULT);
-     if (hdf_attr < 0) {
-       return (MI_ERROR);
-     }
-     H5Awrite(hdf_attr, H5T_NATIVE_DOUBLE, dimensions[i]->offsets);
-     /* Close attribute dataspace. */
-     H5Sclose(dataspace_id); 
-     /* Close attribute. */
-     H5Aclose(hdf_attr);
-   }
    
    /* Create Dimension attribute "sampling_flag" */
    dataspace_id = H5Screate(H5S_SCALAR);
@@ -309,20 +337,7 @@
    H5Sclose(dataspace_id); 
    /* Close attribute. */
    H5Aclose(hdf_attr);
-   if (dimensions[i]->widths != NULL) {
-     /* Create Dimension attribute "widths" */
-     dim[0] = dimensions[i]->length;
-     dataspace_id = H5Screate_simple(1, dim, NULL);
-     hdf_attr = H5Acreate(dataset_id, "widths", H5T_NATIVE_DOUBLE, dataspace_id, H5P_DEFAULT);
-     if (hdf_attr < 0) {
-       return (MI_ERROR);
-     }
-     H5Awrite(hdf_attr, H5T_NATIVE_DOUBLE, dimensions[i]->widths);
-     /* Close attribute dataspace. */
-     H5Sclose(dataspace_id); 
-     /* Close attribute. */
-     H5Aclose(hdf_attr);
-   }
+   
   }
   /* "mitype_to_hdftype" returns a copy of the datatype, so the returned value 
      must be explicitly freed with a call to H5Tclose().
@@ -330,6 +345,7 @@
    */
   H5Tclose(hdf_type);
   H5Sclose(dataspace_id);
+  H5Dclose(dataset_id);
   H5Dclose(dimage_id);
   H5Gclose(grp_dimensions_id);
   H5Pclose(hdf_plist);