Пример #1
0
/* This is actually an extension of H5Tget_order to handle complex types */
herr_t get_order(hid_t type_id, char *byteorder) {
  H5T_order_t h5byteorder;
  /*
  hid_t class_id;

  class_id = H5Tget_class(type_id);
  */

  if (is_complex(type_id)) {
    h5byteorder = get_complex_order(type_id);
  }
  else {
    h5byteorder = H5Tget_order(type_id);
  }
  if (h5byteorder == H5T_ORDER_LE) {
    strcpy(byteorder, "little");
    return h5byteorder;
  }
  else if (h5byteorder == H5T_ORDER_BE ) {
    strcpy(byteorder, "big");
    return h5byteorder;
  }
  else if (h5byteorder == H5T_ORDER_NONE ) {
    strcpy(byteorder, "irrelevant");
    return h5byteorder;
  }
  else {
    /* This should never happen! */
    fprintf(stderr, "Error: unsupported byteorder <%d>\n", h5byteorder);
    strcpy(byteorder, "unsupported");
    return -1;
  }
}
Пример #2
0
/* Counterpart for complex256 */
hid_t create_ieee_complex256(const char *byteorder) {
  herr_t err = 0;
  hid_t float_id, complex_id;
  H5T_order_t h5order = H5Tget_order(H5T_NATIVE_LDOUBLE);

  complex_id = H5Tcreate(H5T_COMPOUND, sizeof(npy_complex256));
  float_id = H5Tcopy(H5T_NATIVE_LDOUBLE);
  if (float_id < 0)
  {
    H5Tclose(complex_id);
    return float_id;
  }

  if ((strcmp(byteorder, "little") == 0) && (h5order != H5T_ORDER_LE))
    err = H5Tset_order(float_id, H5T_ORDER_LE);
  else if ((strcmp(byteorder, "big") == 0) && (h5order != H5T_ORDER_BE))
    err = H5Tset_order(float_id, H5T_ORDER_BE);

  if (err < 0)
  {
    H5Tclose(complex_id);
    return err;
  }

  H5Tinsert(complex_id, "r", HOFFSET(npy_complex256, real), float_id);
  H5Tinsert(complex_id, "i", HOFFSET(npy_complex256, imag), float_id);
  H5Tclose(float_id);
  return complex_id;
}
Пример #3
0
/*-------------------------------------------------------------------------
 * Function:	test_named
 *
 * Purpose:	Create an enumeration data type and store it in the file.
 *
 * Return:	Success:	0
 *
 *		Failure:	number of errors
 *
 * Programmer:	Robb Matzke
 *              Wednesday, December 23, 1998
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static int
test_named(hid_t file)
{
    hid_t	type=-1, cwg=-1;
    c_e1	val;
    signed char	val8;

    TESTING("named enumeration types");
    if ((cwg=H5Gcreate(file, "test_named", 0))<0) goto error;

    /* A native integer */
    if ((type = H5Tcreate(H5T_ENUM, sizeof(c_e1)))<0) goto error;
    if (H5Tenum_insert(type, "RED",   CPTR(val, E1_RED  ))<0) goto error;
    if (H5Tenum_insert(type, "GREEN", CPTR(val, E1_GREEN))<0) goto error;
    if (H5Tenum_insert(type, "BLUE",  CPTR(val, E1_BLUE ))<0) goto error;
    if (H5Tenum_insert(type, "WHITE", CPTR(val, E1_WHITE))<0) goto error;
    if (H5Tenum_insert(type, "BLACK", CPTR(val, E1_BLACK))<0) goto error;
    if (H5Tcommit(cwg, "e1_a", type)<0) goto error;
    if (H5Tclose(type)<0) goto error;

    /* A smaller type */
    if ((type = H5Tcreate(H5T_ENUM, 1))<0) goto error;
    if (H5Tenum_insert(type, "RED",   CPTR(val8, E1_RED  ))<0) goto error;
    if (H5Tenum_insert(type, "GREEN", CPTR(val8, E1_GREEN))<0) goto error;
    if (H5Tenum_insert(type, "BLUE",  CPTR(val8, E1_BLUE ))<0) goto error;
    if (H5Tenum_insert(type, "WHITE", CPTR(val8, E1_WHITE))<0) goto error;
    if (H5Tenum_insert(type, "BLACK", CPTR(val8, E1_BLACK))<0) goto error;
    if (H5Tcommit(cwg, "e1_b", type)<0) goto error;
    if (H5Tclose(type)<0) goto error;

    /* A non-native type */
    if (H5T_ORDER_BE==H5Tget_order(H5T_NATIVE_INT)) {
	if ((type = H5Tenum_create(H5T_STD_U8LE))<0) goto error;
    } else {
	if ((type = H5Tenum_create(H5T_STD_U8BE))<0) goto error;
    }
    if (H5Tenum_insert(type, "RED",   CPTR(val8, E1_RED  ))<0) goto error;
    if (H5Tenum_insert(type, "GREEN", CPTR(val8, E1_GREEN))<0) goto error;
    if (H5Tenum_insert(type, "BLUE",  CPTR(val8, E1_BLUE ))<0) goto error;
    if (H5Tenum_insert(type, "WHITE", CPTR(val8, E1_WHITE))<0) goto error;
    if (H5Tenum_insert(type, "BLACK", CPTR(val8, E1_BLACK))<0) goto error;
    if (H5Tcommit(cwg, "e1_c", type)<0) goto error;
    if (H5Tclose(type)<0) goto error;

    if (H5Gclose(cwg)<0) goto error;
    PASSED();
    return 0;

 error:
    H5E_BEGIN_TRY {
	H5Tclose(type);
	H5Gclose(cwg);
    } H5E_END_TRY;
    return 1;
}
Пример #4
0
int
main (void)
{
  hid_t fid1=-1;
  hid_t fid2=-1;
  hid_t gid=-1;
  char filename1[NAME_BUF_SIZE];
  char filename2[NAME_BUF_SIZE];

  /* Name the files differently depending on the endianness of this platform */

  switch(H5Tget_order(H5T_NATIVE_INT))
  {
    case H5T_ORDER_LE:
      strcpy(filename1, NAME_LE_1);
      strcpy(filename2, NAME_LE_2);
      break;
    case H5T_ORDER_BE:
      strcpy(filename1, NAME_BE_1);
      strcpy(filename2, NAME_BE_2);
      break;
    default:
      goto error;
  }

  /* Create the two files */
  if((fid1 = H5Fcreate(filename1, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT)) < 0) goto error;
  if((fid2 = H5Fcreate(filename2, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT)) < 0) goto error;

  /* Create two groups in the second file */
  if((gid = H5Gcreate2(fid2, "group", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0) goto error;
  if((H5Gclose(gid)) < 0) goto error;
  if((gid = H5Gcreate2(fid2, "group/subgroup", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0) goto error;
  if((H5Gclose(gid)) < 0) goto error;

  /* Create an external link in the first file pointing to the group in the second file */
  if(H5Lcreate_external(filename2, "group", fid1, "ext_link", H5P_DEFAULT, H5P_DEFAULT) < 0) goto error;

  if((H5Fclose(fid1)) < 0) goto error;
  if((H5Fclose(fid2)) < 0) goto error;

  return 0;

error:
  H5E_BEGIN_TRY {
    H5Fclose(fid1);
    H5Fclose(fid2);
    H5Gclose(gid);
  } H5E_END_TRY
  return 1;
}
Пример #5
0
/* Return the byteorder of a complex datatype.
   It is obtained from the real part, which is the first member. */
static H5T_order_t get_complex_order(hid_t type_id) {
  hid_t class_id, base_type_id;
  hid_t real_type = 0;
  H5T_order_t result = 0;

  class_id = H5Tget_class(type_id);
  if (class_id == H5T_COMPOUND) {
    real_type = H5Tget_member_type(type_id, 0);
  }
  else if (class_id == H5T_ARRAY) {
    /* Get the array base component */
    base_type_id = H5Tget_super(type_id);
    /* Get the type of real component. */
    real_type = H5Tget_member_type(base_type_id, 0);
    H5Tclose(base_type_id);
  }
  if ((class_id == H5T_COMPOUND) || (class_id == H5T_ARRAY)) {
    result = H5Tget_order(real_type);
    H5Tclose(real_type);
  }
  return result;
}
Пример #6
0
/* Purpose: retrieve datatype and dataspace information from file 
 * Parameters: H5Dataset *d -- The dataset to be initialized 
 * Return:  Returns a non-negative value if successful; otherwise returns a negative value.
 */
int H5Dataset_init(H5Dataset *d)
{
    int ret_value=0, i=0;
    hsize_t dims[H5S_MAX_RANK];
    hid_t did=-1, sid=-1, tid=-1, ftid=-1;
    unsigned int npoints;

    assert (d);
    H5Eclear();

    if (d->space.rank > 0)
        goto done; /* already called */

    if ( (did = H5Dopen(d->fid, d->fullpath)) < 0)
        THROW_H5LIBRARY_ERROR(d->error,ret_value, done);

    ftid = H5Dget_type(did);
    tid = H5Tget_native_type(ftid, H5T_DIR_ASCEND);
    if ( ftid > 0) H5Tclose(ftid);

    d->tclass = d->type.tclass = (H5Datatype_class_t)H5Tget_class(tid);
    d->type.size = H5Tget_size(tid);

    if  ( d->type.tclass == H5DATATYPE_INTEGER 
         || d->type.tclass == H5DATATYPE_FLOAT 
         || d->type.tclass == H5DATATYPE_BITFIELD 
         || d->type.tclass == H5DATATYPE_REFERENCE 
         || d->type.tclass == H5DATATYPE_ENUM ) 
    {
        d->type.order = (H5Datatype_order_t)H5Tget_order(tid);

        if (d->type.tclass == H5DATATYPE_INTEGER)
        {
            d->type.sign = (H5Datatype_sign_t)H5Tget_sign(tid);

            /* check palette for image */
            H5Dataset_readPalette(d, did);

            H5Dataset_check_image(d, did);
        }
    }
    else if (d->type.tclass == H5DATATYPE_COMPOUND) {
        d->type.nmembers = H5Tget_nmembers(tid );
        d->type.mnames = (char **)malloc(d->type.nmembers*sizeof(char*));
        d->type.mtypes = (int *)malloc(d->type.nmembers*sizeof(int));
        for (i=0; i<d->type.nmembers; i++) {
            hid_t mtid = -1;
            int mtype = 0, mclass, msign, msize;
            d->type.mnames[i] = H5Tget_member_name(tid, i);
            mtid = H5Tget_member_type(tid, i); 
            mclass = H5Tget_class(mtid);
            msign = H5Tget_sign(mtid);
            msize = H5Tget_size(mtid);
            mtype = mclass<<28 | msign<<24 | msize;
            d->type.mtypes[i] = mtype;
            H5Tclose(mtid);
        }
    }

    sid = H5Dget_space(did);
    d->space.rank = H5Sget_simple_extent_ndims(sid);
    if ( H5Sget_simple_extent_dims(sid, dims, NULL) < 0 )
        THROW_H5LIBRARY_ERROR(d->error,ret_value, done);

    if (d->space.rank<=0)
    {
        d->space.rank = 1;
        dims[0] = 1;
    }

    npoints = 1; 
    for (i=0; i<d->space.rank; i++)
    {
        d->space.dims[i] = dims[i];
        d->space.start[i] = 0;
        d->space.stride[i] = 1;
        d->space.count[i] = dims[i];
        npoints *= dims[i];
    }
    d->space.npoints = npoints;

done:
    if (sid > 0 ) H5Sclose(sid);
    if (tid > 0 ) H5Tclose(tid);
    if (did > 0 ) H5Dclose(did);
    return ret_value;
}
Пример #7
0
PyObject *H5UIget_info( hid_t loc_id,
                        const char *dset_name,
                        char *byteorder)
{
  hid_t       dataset_id;
  int         rank;
  hsize_t     *dims;
  hid_t       space_id;
  H5T_class_t class_id;
  H5T_order_t order;
  hid_t       type_id;
  PyObject    *t;
  int         i;

  /* Open the dataset. */
  if ( (dataset_id = H5Dopen( loc_id, dset_name, H5P_DEFAULT )) < 0 ) {
    Py_INCREF(Py_None);
    return Py_None;     /* Not chunked, so return None */
  }

  /* Get an identifier for the datatype. */
  type_id = H5Dget_type( dataset_id );

  /* Get the class. */
  class_id = H5Tget_class( type_id );

  /* Get the dataspace handle */
  if ( (space_id = H5Dget_space( dataset_id )) < 0 )
    goto out;

  /* Get rank */
  if ( (rank = H5Sget_simple_extent_ndims( space_id )) < 0 )
    goto out;

  /* Book resources for dims */
  dims = (hsize_t *)malloc(rank * sizeof(hsize_t));

  /* Get dimensions */
  if ( H5Sget_simple_extent_dims( space_id, dims, NULL) < 0 )
    goto out;

  /* Assign the dimensions to a tuple */
  t = PyTuple_New(rank);
  for(i=0;i<rank;i++) {
    /* I don't know if I should increase the reference count for dims[i]! */
    PyTuple_SetItem(t, i, PyInt_FromLong((long)dims[i]));
  }

  /* Release resources */
  free(dims);

  /* Terminate access to the dataspace */
  if ( H5Sclose( space_id ) < 0 )
    goto out;

  /* Get the byteorder */
  /* Only integer, float, time and enum classes can be byteordered */
  if ((class_id == H5T_INTEGER) || (class_id == H5T_FLOAT)
      || (class_id == H5T_BITFIELD) || (class_id == H5T_TIME)
      ||  (class_id == H5T_ENUM)) {
    order = H5Tget_order( type_id );
    if (order == H5T_ORDER_LE)
      strcpy(byteorder, "little");
    else if (order == H5T_ORDER_BE)
      strcpy(byteorder, "big");
    else {
      fprintf(stderr, "Error: unsupported byteorder: %d\n", order);
      goto out;
    }
  }
  else {
    strcpy(byteorder, "irrelevant");
  }

  /* End access to the dataset */
  H5Dclose( dataset_id );

  /* Return the dimensions tuple */
  return t;

out:
 H5Tclose( type_id );
 H5Dclose( dataset_id );
 Py_INCREF(Py_None);
 return Py_None;    /* Not chunked, so return None */

}
Пример #8
0
int main(void)
{
    /* handles */
    hid_t   file, dataset;
    hid_t   datatype, dataspace;
    hid_t   memspace;

    herr_t status;

    H5T_class_t t_class;        /* data type class */
    
    H5T_order_t order;          /* data order */
    
    size_t size;                /* size of the data element
                                 * stored in the file
                                 */

    hsize_t dimsm[3];           /* memory space dimensions */
    hsize_t dims_out[2];        /* dataset dimensions */

    int data_out[NX][NY][NZ];   /* output buffer */

    hsize_t count[2];           /* size of the hyperslab in the file */
    hsize_t offset[2];          /* hyperslab offset in the file */
    hsize_t count_out[3];       /* size of the hyperslab in memory */
    hsize_t offset_out[3];      /* hyperslab offset in memory */

    int i,j,k;
    int status_n;
    int rank;

    for (j = 0; j < NX; j++)
    {
        for (i = 0; i < NY; i++)
        {
            for (k = 0; k < NZ; k++)
            {
                data_out[j][i][k] = 0;
            }
        }
    }


    /*
     * Open the file and the dataset.
     */
    file = H5Fopen(H5FILE_NAME, H5F_ACC_RDONLY, H5P_DEFAULT);
    dataset = H5Dopen(file, DATASET_NAME);


    /*
     * Get datatype and dataspace handles and then query
     * dataset class, order, size, rank and dimensions.
     */

    datatype = H5Dget_type(dataset); /* datatype handle */
    
    t_class = H5Tget_class(datatype);
    if (t_class == H5T_INTEGER)
        printf("Dataset has INTEGER type\n");

    order = H5Tget_order(datatype);
    if (order == H5T_ORDER_LE)
        printf("Little endian order\n");

    size = H5Tget_size(datatype);
    printf("Data size is %d\n", (int)size);

    dataspace = H5Dget_space(dataset); /* dataspace handle */
    rank      = H5Sget_simple_extent_ndims(dataspace);
    status_n  = H5Sget_simple_extent_dims(dataspace, dims_out, NULL);
    printf("rank %d, dimensions %lu x %lu\n", rank,
        (unsigned long)(dims_out[0]),
        (unsigned long)(dims_out[1]));


    /*
     * Define hyperslab in the dataset.
     */

    offset[0] = 1;
    offset[1] = 2;

    count[0] = NX_SUB;
    count[1] = NY_SUB;

    status = H5Sselect_hyperslab(dataspace, H5S_SELECT_SET, offset, NULL, count, NULL);


    /*
     * Define the memory dataspace.
     */

    dimsm[0] = NX;
    dimsm[1] = NY;
    dimsm[2] = NZ;

    memspace = H5Screate_simple(RANK_OUT, dimsm, NULL);


    /*
     * Define memory hyperslab
     */

    offset_out[0] = 3;
    offset_out[1] = 0;
    offset_out[2] = 0;

    count_out[0] = NX_SUB;
    count_out[1] = NY_SUB;
    count_out[2] = 1;

    status = H5Sselect_hyperslab(memspace, H5S_SELECT_SET, offset_out, NULL, count_out, NULL);


    /*
     * Read data from hyperslab in the file into the hyperslab
     * in memory and display it.
     */

    status = H5Dread(dataset, H5T_NATIVE_INT, memspace, dataspace, H5P_DEFAULT, data_out);

    for (j = 0; j < NX; j++)
    {
        for (i = 0; i < NY; i++)
        {
            printf("%d ", data_out[j][i][0]);
        }
        printf("\n");
    }
    /* Result:
     * 0 0 0 0 0 0 0
     * 0 0 0 0 0 0 0
     * 0 0 0 0 0 0 0
     * 3 4 5 6 0 0 0
     * 4 5 6 7 0 0 0
     * 5 6 7 8 0 0 0
     * 0 0 0 0 0 0 0
     */


    /*
     * Close/release resources.
     */
    H5Tclose(datatype);
    H5Dclose(dataset);
    H5Sclose(dataspace);
    H5Sclose(memspace);
    H5Fclose(file);

    return 0;
}
Пример #9
0
/*-------------------------------------------------------------------------
 * test_rw_non-native_dt
 *
 * test reading and writing packet table using datatypes that are not
 * native.
 *
 *-------------------------------------------------------------------------
 */
static int test_rw_nonnative_dt(hid_t fid)
{
    hid_t          ptable;     /* Packet table identifier */

    herr_t         err;        /* Function return status */
    hsize_t        count;      /* Number of records in the table */

    int            x;          /* Loop variable */

    /* Buffers to hold data */
    int writeBuffer[5];
    int readBuffer[5];

    TESTING("reading/writing non-native packet table");

    /* Initialize buffers */
    for(x=0; x<5; x++) {
        writeBuffer[x]=x;
        readBuffer[x] = -1;
    }

    /* Create a fixed-length packet table within the file */
    /* This table's "packets" will be simple integers and it will use no compression */
    if(H5Tget_order(H5T_NATIVE_INT) == H5T_ORDER_LE) {
        ptable = H5PTcreate_fl(fid, "Packet Test Dataset, Non-native", H5T_STD_I32BE, (hsize_t)100, -1);
    } else {
        ptable = H5PTcreate_fl(fid, "Packet Test Dataset, Non-native", H5T_STD_I32LE, (hsize_t)100, -1);
    }
    if(ptable == H5I_INVALID_HID)
        goto out;

    /* Write one packet to the packet table */
    if( (err = H5PTappend(ptable, (hsize_t)1, &(writeBuffer[0]))) < 0 )
        goto out;

    /* Write several packets to the packet table */
    if( (err = H5PTappend(ptable, (hsize_t)4, &(writeBuffer[1]))) < 0)
        goto out;

    if( (err = H5PTclose(ptable)) < 0)
        goto out;

    /* Open the Packet table */
    if( (ptable = H5PTopen(fid, "Packet Test Dataset, Non-native")) < 0)
        goto out;

    /* Get the number of packets in the packet table.  This should be five. */
    if( (err = H5PTget_num_packets(ptable, &count)) < 0)
        goto out;

    if( (int)count != 5 )
        goto out;

    /* Initialize packet table's "current record" */
    if( (err = H5PTcreate_index(ptable)) < 0)
        goto out;

    /* Iterate through packets, read each one back */
    for(x=0; x<5; x++) {
        if( (err = H5PTget_next(ptable, (hsize_t)1, &(readBuffer[x]))) < 0)
            goto out;
        if( x != readBuffer[x])
            goto out;
    }

    /* Close the packet table */
    if( (err = H5PTclose(ptable)) < 0)
        goto out;

    PASSED();
    return 0;

out:
    H5_FAILED();
    if( H5PTis_valid(ptable) < 0)
        H5PTclose(ptable);
    return -1;
}
Пример #10
0
/*-------------------------------------------------------------------------
* subroutine for test_text_dtype(): test_enums().
*-------------------------------------------------------------------------
*/
static int test_enums(void)
{
    hid_t   dtype;
    size_t  size = 16;
    char    name1[16];
    int     value1 = 7;
    const char    *name2 = "WHITE";
    int     value2;
    H5T_class_t type_class;
    char*   dt_str;
    size_t  str_len;
    H5T_order_t native_order = H5Tget_order(H5T_NATIVE_INT);

    TESTING3("        text for enum types");

    if((dtype = H5LTtext_to_dtype("H5T_ENUM { H5T_STD_I32LE; \"RED\" 5; \"GREEN\" 6; \"BLUE\" 7; \"WHITE\" 8; }", H5LT_DDL))<0)
        goto out;

    if((type_class = H5Tget_class(dtype))<0)
        goto out;
    if(type_class != H5T_ENUM)
        goto out;

    /* Convert the variable before using it */
    if(!H5Tequal(H5T_STD_I32LE, H5T_NATIVE_INT)) {
        if(H5Tconvert(H5T_NATIVE_INT, H5T_STD_I32LE, 1, &value1, NULL, H5P_DEFAULT) < 0)
            goto out;
    }

    if(H5Tenum_nameof(dtype, &value1, name1, size)<0)
        goto out;
    if(strcmp(name1, "BLUE"))
        goto out;

    if(H5Tenum_valueof(dtype, name2, &value2)<0)
        goto out;

    /* Convert the variable before comparing it */
    if(!H5Tequal(H5T_STD_I32LE, H5T_NATIVE_INT)) {
        if(H5Tconvert(H5T_NATIVE_INT, H5T_STD_I32LE, 1, &value2, NULL, H5P_DEFAULT) < 0)
            goto out;
    }

    if(value2 != 8)
        goto out;

    if(H5LTdtype_to_text(dtype, NULL, H5LT_DDL, &str_len)<0)
        goto out;
    dt_str = (char*)calloc(str_len, sizeof(char));
    if(H5LTdtype_to_text(dtype, dt_str, H5LT_DDL, &str_len)<0)
        goto out;
    if(strcmp(dt_str, "H5T_ENUM {\n      H5T_STD_I32LE;\n      \"RED\"              5;\n      \"GREEN\"            6;\n      \"BLUE\"             7;\n      \"WHITE\"            8;\n   }")) {

        printf("dt=\n%s\n", dt_str);
        goto out;
    }

    free(dt_str);

    if(H5Tclose(dtype)<0)
        goto out;

    PASSED();
    return 0;

out:
    H5_FAILED();
    return -1;
}
Пример #11
0
int H5Attribute_init(H5Attribute *a, hid_t aid) {
    int ret_value=0, i=0;
    hsize_t dims[H5S_MAX_RANK];
    hid_t sid=-1, tid=-1, ftid=-1;
    unsigned int npoints;

    if (NULL == a || aid < 0) {
        ret_value = -1;
        goto done;
    }

    if (a->space.rank > 0)
        goto done; /* already called */

    ftid = H5Aget_type(aid);
    tid = H5Tget_native_type(ftid, H5T_DIR_ASCEND);
    if (ftid > 0) H5Tclose(ftid);

    a->name = (char *)malloc(MAX_NAME_LEN);
    H5Aget_name(aid, MAX_NAME_LEN, a->name);

    a->tclass = a->type.tclass = (H5Datatype_class_t)H5Tget_class(tid);
    a->type.size = H5Tget_size(tid);

    if  ( a->type.tclass == H5DATATYPE_INTEGER
         || a->type.tclass == H5DATATYPE_FLOAT
         || a->type.tclass == H5DATATYPE_BITFIELD
         || a->type.tclass == H5DATATYPE_REFERENCE
         || a->type.tclass == H5DATATYPE_ENUM ) {
        a->type.order = (H5Datatype_order_t)H5Tget_order(tid);
        if (a->type.tclass == H5DATATYPE_INTEGER) {
            a->type.sign = (H5Datatype_sign_t)H5Tget_sign(tid);
        }
    }
    else if (a->type.tclass == H5DATATYPE_COMPOUND) {
        a->type.nmembers = H5Tget_nmembers(tid );
        a->type.mnames = (char **)malloc(a->type.nmembers*sizeof(char*));
        a->type.mtypes = (int *)malloc(a->type.nmembers*sizeof(int));
        for (i=0; i<a->type.nmembers; i++) {
            a->type.mnames[i] = H5Tget_member_name(tid, i);
            a->type.mtypes[i] = H5Tget_class(H5Tget_member_type(tid, i));
        }
    }

    sid = H5Aget_space(aid);
    a->space.rank = H5Sget_simple_extent_ndims(sid);
    if ( H5Sget_simple_extent_dims(sid, dims, NULL) < 0 )
        THROW_H5LIBRARY_ERROR(a->error,ret_value, done);

    if (a->space.rank<=0)
    {
        a->space.rank = 1;
        dims[0] = 1;
    }

    npoints = 1;
    for (i=0; i<a->space.rank; i++)
    {
        a->space.dims[i] = dims[i];
        a->space.start[i] = 0;
        a->space.stride[i] = 1;
        a->space.count[i] = dims[i];
        npoints *= dims[i];
    }
    a->space.npoints = npoints;

done:
    if (sid > 0 ) H5Sclose(sid);
    if (tid > 0 ) H5Tclose(tid);

    return ret_value;
}
Пример #12
0
int ImageBase::readHDF5(size_t select_img)
{
    bool isStack = false;

    H5infoProvider provider = getProvider(fhdf5); // Provider name

    int errCode = 0;

    hid_t dataset;    /* Dataset and datatype identifiers */
    hid_t filespace;
    hsize_t dims[4]; // We are not going to support more than 4 dimensions, at this moment.
    hsize_t nobjEman;
    hid_t cparms;
    int rank;

    String dsname = filename.getBlockName();
    
    // Setting default dataset name
    if (dsname.empty())
    {
        dsname = provider.second;

        switch (provider.first)
        {
        case EMAN: // Images in stack are stored in separated groups
            hid_t grpid;
            grpid = H5Gopen(fhdf5,"/MDF/images/", H5P_DEFAULT);
            /*herr_t err = */
            H5Gget_num_objs(grpid, &nobjEman);
            dsname = formatString(dsname.c_str(), IMG_INDEX(select_img));
            H5Gclose(grpid);
            break;
        default:
        	break;
        }
    }
    else
    {
        switch (provider.first)
        {
        case EMAN: // Images in stack are stored in separated groups
            nobjEman=1;
            break;
        default:
            break;
        }
    }

    dataset = H5Dopen2(fhdf5, dsname.c_str(), H5P_DEFAULT);

    if( dataset < 0)
        REPORT_ERROR(ERR_IO_NOTEXIST, formatString("readHDF5: Dataset '%s' not found",dsname.c_str()));

    cparms = H5Dget_create_plist(dataset); /* Get properties handle first. */

    // Get dataset rank and dimension.
    filespace = H5Dget_space(dataset);    /* Get filespace handle first. */
    //    rank      = H5Sget_simple_extent_ndims(filespace);
    rank  = H5Sget_simple_extent_dims(filespace, dims, NULL);

    // Offset only set when it is possible to access to data directly
    offset = (H5D_CONTIGUOUS == H5Pget_layout(cparms))? H5Dget_offset(dataset) : 0;


    //    status = H5Dread(dataset, tid, H5S_ALL, H5S_ALL, H5P_DEFAULT, bm_out);

    hid_t h5datatype = H5Dget_type(dataset);

    // Reading byte order
    switch(H5Tget_order(h5datatype))
    {
    case H5T_ORDER_ERROR:
        REPORT_ERROR(ERR_IO, "readHDF5: error reading endianness.");
        break;
    case H5T_ORDER_LE:
        swap = IsBigEndian();
        break;
    case H5T_ORDER_BE:
        swap = IsLittleEndian();
        break;
    default:
        REPORT_ERROR(ERR_IO, "readHDF5: unknown endianness type, maybe mixed types.");
        break;
    }

    DataType datatype = datatypeH5(h5datatype);
    MDMainHeader.setValue(MDL_DATATYPE,(int) datatype);

    // Setting isStack depending on provider
    switch (provider.first)
    {
    case MISTRAL: // rank 3 arrays are stacks
        isStack = true;
        break;
        //    case EMAN: // Images in stack are stored in separated groups
    default:
    	break;
    }


    ArrayDim aDim;
    size_t nDimFile;
    aDim.xdim = dims[rank-1];
    aDim.ydim = (rank>1)?dims[rank-2]:1;
    aDim.zdim = (rank>3 || (rank==3 && !isStack))?dims[rank-3]:1;
    if ( provider.first == EMAN )
        nDimFile = nobjEman;
    else
        nDimFile = ( rank<3 || !isStack )?1:dims[0] ;

    if (select_img > nDimFile)
        REPORT_ERROR(ERR_INDEX_OUTOFBOUNDS, formatString("readHDF5 (%s): Image number %lu exceeds stack size %lu", filename.c_str(), select_img, nDimFile));

    aDim.ndim = replaceNsize = (select_img == ALL_IMAGES)? nDimFile :1 ;
    setDimensions(aDim);

    //Read header only
    if(dataMode == HEADER || (dataMode == _HEADER_ALL && aDim.ndim > 1))
        return errCode;


    // EMAN stores each image in a separate dataset
    if ( provider.first == EMAN )
        select_img = 1;

    size_t   imgStart = IMG_INDEX(select_img);
    size_t   imgEnd = (select_img != ALL_IMAGES) ? imgStart + 1 : aDim.ndim;



    MD.clear();
    MD.resize(imgEnd - imgStart,MDL::emptyHeader);

    if (dataMode < DATA)   // Don't read  data if not necessary but read the header
        return errCode;

    if ( H5Pget_layout(cparms) == H5D_CONTIGUOUS ) //We can read it directly
        readData(fimg, select_img, datatype, 0);
    else // We read it by hyperslabs
    {
        // Allocate memory for image data (Assume xdim, ydim, zdim and ndim are already set
        //if memory already allocated use it (no resize allowed)
        mdaBase->coreAllocateReuse();

        hid_t       memspace;

        hsize_t offset[4]; // Hyperslab offset in the file
        hsize_t  count[4]; // Size of the hyperslab in the file

        // Define the offset and count of the hyperslab to be read.

        switch (rank)
        {
        case 4:
            count[0] = 1;
        case 3:
            //            if (stack)
            count[rank-3] = aDim.zdim;
            offset[rank-2]  = 0;
        case 2:
            count[rank-2]  = aDim.ydim;
            offset[rank-2]  = 0;
            break;
        }
        count[rank-1]  = aDim.xdim;
        offset[rank-1]  = 0;

        aDim.xdim = dims[rank-1];
        aDim.ydim = (rank>1)?dims[rank-2]:1;
        aDim.zdim = (rank == 4)?dims[1]:1;
        // size_t nDimFile = (rank>2)?dims[0]:1 ;

        // Define the memory space to read a hyperslab.
        memspace = H5Screate_simple(rank,count,NULL);

        size_t data = (size_t) this->mdaBase->getArrayPointer();
        size_t pad = aDim.zyxdim*gettypesize(myT());


        for (size_t idx = imgStart, imN = 0; idx < imgEnd; ++idx, ++imN)
        {

            // Set the offset of the hyperslab to be read
            offset[0] = idx;

            if ( H5Sselect_hyperslab(filespace, H5S_SELECT_SET, offset, NULL,
                                     count, NULL) < 0 )
                REPORT_ERROR(ERR_IO_NOREAD, formatString("readHDF5: Error selecting hyperslab %d from filename %s",
                             imgStart, filename.c_str()));

            //            movePointerTo(ALL_SLICES,imN);
            // Read
            if ( H5Dread(dataset, H5Datatype(myT()), memspace, filespace,
                         H5P_DEFAULT, (void*)(data + pad*imN)) < 0 )
                REPORT_ERROR(ERR_IO_NOREAD,formatString("readHDF5: Error reading hyperslab %d from filename %s",
                                                        imgStart, filename.c_str()));
        }
        H5Sclose(memspace);
    }

    H5Pclose(cparms);
    H5Sclose(filespace);
    H5Dclose(dataset);

    return errCode;
}
bool ossimHdf5SubDataset::open()
{
   static const char MODULE[] = "ossimHdf5SubDataset::open";

   bool result = false;

   m_fid = H5Fopen(m_fileName.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT);
   if (m_fid < 0)
   {
      H5Fclose(m_fid);
      return false;
   }

   // Third arg added for compile fix but not checked. (drb)
   m_dataset_id = H5Dopen(m_fid, m_dataset_name, 0); 

   if (m_dataset_id < 0)
   {
      close();
      return false;
   }

   m_dataType = H5Dget_type(m_dataset_id);
   m_native = H5Tget_native_type(m_dataType, H5T_DIR_ASCEND );
   setOutputScalarType();

   H5Tget_class(m_dataType);   
   H5Tget_order(m_dataType);

   m_size = H5Tget_size(m_dataType);

   m_dataspace = H5Dget_space(m_dataset_id);    /* dataspace handle */
   m_rank = H5Sget_simple_extent_ndims(m_dataspace);

   if (m_rank == 4)
   {
      hsize_t  dims_out[4];
      herr_t status_n  = H5Sget_simple_extent_dims(m_dataspace, dims_out, NULL);
      if (status_n >= 0)
      {
         m_numberOfBands = dims_out[2];
         m_numberOfLines = dims_out[0];
         m_numberOfSamples = dims_out[1];
      }
   }
   else if (m_rank == 3)
   {
      hsize_t  dims_out[3];
      herr_t status_n  = H5Sget_simple_extent_dims(m_dataspace, dims_out, NULL);
      if (status_n >= 0)
      {
         if (dims_out[2] < dims_out[0] && dims_out[2] < dims_out[1])
         {
            m_numberOfBands = dims_out[2];
            m_numberOfLines = dims_out[0];
            m_numberOfSamples = dims_out[1];
         }
         else
         {
            m_numberOfBands = dims_out[0];
            m_numberOfLines = dims_out[1];
            m_numberOfSamples = dims_out[2];
         }
      }
   }
   else if (m_rank == 2)
   {
      hsize_t  dims_out[2];
      herr_t status_n  = H5Sget_simple_extent_dims(m_dataspace, dims_out, NULL);
      if (status_n >= 0)
      {
         m_numberOfBands = 1;
         m_numberOfLines = dims_out[0];
         m_numberOfSamples = dims_out[1];
      }
   }
   else if (m_rank == 1)
   {
      hsize_t  dims_out[1];
      herr_t status_n  = H5Sget_simple_extent_dims(m_dataspace, dims_out, NULL);
      if (status_n >= 0)
      {
         m_numberOfBands = 0;
         m_numberOfLines = dims_out[0];
         m_numberOfSamples = 0;
      }
   }
   else
   {
      m_numberOfBands = 0;
      m_numberOfLines = 0;
      m_numberOfSamples = 0;
   }

   result = initMeta();

   if (traceDebug())
   {
      ossimNotify(ossimNotifyLevel_DEBUG)
         << MODULE << " exit status = " << (result?"true":"false\n")
         << std::endl;
   }

   return result;
}
Пример #14
0
/* ****************************************************************************************************************************** */
int main(int argc, char *argv[]) {
  hid_t   fileID, dataSetID;
  herr_t  hErrVal;
  int     i;
  hsize_t dims[1024], maxDims[1024];
  H5T_class_t class;
  char classStr[32];
  hid_t dataTypeID;
  size_t dataSize;
  H5T_order_t order;
  int rank; /* Note this is an int, not an hssize_t */
  int intVal;
  hid_t dataSpaceID;
  hid_t rootGroupID;
  hsize_t numInRootGrp, firstDataSetIdx, foundFirstDataSet;
  char attrName[1024], firstDataSetName[1024];
  ssize_t objectNameSize, attrNameSize;
  H5G_stat_t objectStatInfo;
  int numAttrs;
  int curAttrIdx;
  hid_t attrID;
  hsize_t numDataPoints;
  unsigned majnum, minnum, relnum;

  /* Load the library -- not required most platforms. */
  hErrVal = H5open();
  mjrHDF5_chkError(hErrVal);

  /* Get the library version */
  hErrVal = H5get_libversion(&majnum, &minnum, &relnum);
  mjrHDF5_chkError(hErrVal);
  printf("Lib Version: v%lu.%lur%lu\n", (unsigned long)majnum, (unsigned long)minnum, (unsigned long)relnum);

  /*  Open an existing file. */
  fileID = H5Fopen(TST_FILE_NAME, H5F_ACC_RDWR, H5P_DEFAULT);
  mjrHDF5_chkError(fileID);

  /*  Get the ID for the "root" group -- every HDF5 has one */
  rootGroupID = H5Gopen(fileID, "/", H5P_DEFAULT);
  mjrHDF5_chkError(rootGroupID);

  /* Get the number of objects in the root group. */
  hErrVal = H5Gget_num_objs(rootGroupID, &numInRootGrp);
  mjrHDF5_chkError(hErrVal);

  printf("The root group contains %lu object%c\n", (unsigned long)numInRootGrp, (numInRootGrp==1?' ':'s'));

  if(numInRootGrp < 1) {
    printf("As the file contains NO objects, I have nothing left to do...\n");
    exit(1);
  } /* end if */

  /*  Find the first dataset in the root group. */
  for(foundFirstDataSet=0,firstDataSetIdx=0; (!foundFirstDataSet)&&(firstDataSetIdx<numInRootGrp); firstDataSetIdx++) {
    /* Get object name from the index. */
    objectNameSize = H5Gget_objname_by_idx(rootGroupID, firstDataSetIdx, firstDataSetName, 1024);
    mjrHDF5_chkError(objectNameSize);
    if(objectNameSize == 0) { /* Need to check for zero return too */
      printf("ERROR: Object with index %lu doesn't exist in root group!\n", (unsigned long)firstDataSetIdx);
      exit(1);
    } /* end if */
    /*  Now use the object name to get info about the object... */
    hErrVal = H5Gget_objinfo(rootGroupID, firstDataSetName, 0, &objectStatInfo);
    mjrHDF5_chkError(hErrVal);
    /* If the object is a dataset, then print out some info. */
    if(objectStatInfo.type == H5G_DATASET) {
      printf("Object %luth (%s) is a dataset!\n", (unsigned long)firstDataSetIdx, firstDataSetName);
      printf("The name of the %luth object of the root group is: %s\n", (unsigned long)firstDataSetIdx, firstDataSetName);
      foundFirstDataSet = 1;
      printf("Info for the %s dataset:\n", firstDataSetName);
      printf("  Modify time: %lu\n", (unsigned long)objectStatInfo.mtime);
      printf("  Type: %lu\n", (unsigned long)objectStatInfo.type);
      printf("  Link count: %lu\n", (unsigned long)objectStatInfo.nlink);
    } /* end if */
  } /* end for */
  /* Note: At this point index of the dataset will be: firstDataSetIdx-- */
  if(!foundFirstDataSet) {
    printf("ERROR: Could not find a dataset in the root group\n");
    exit(1);
  } /* end if */

  /* Open the dataset we found -- we know it exists. */
  dataSetID = H5Dopen(rootGroupID, firstDataSetName, H5P_DEFAULT);
  mjrHDF5_chkError(dataSetID);

  /* ****************************************************************************************************************************** */
  /* Get some info regarding the TYPE of the dataset. */
  dataTypeID  = H5Dget_type(dataSetID);
  mjrHDF5_chkError(dataTypeID);
  /*  Get the class of the data */
  class = H5Tget_class(dataTypeID);
  mjrHDF5_Tclass2str(class, classStr);
  printf("  Object class: %s\n", classStr);
  /*  Get the size of the type */
  dataSize = H5Tget_size(dataTypeID);
  if(dataSize == 0) {
    printf("ERROR: Failure in H5Tget_size().\n");
    exit(1);
  } /* end if */
  printf("  Size of data type: %lu\n", (unsigned long)dataSize);
  /*  Get the byte order */
  order = H5Tget_order(dataTypeID);
  printf("  Byte Order: ");
  switch(order) {
    case H5T_ORDER_ERROR  : printf("ERROR\n");            break;
    case H5T_ORDER_LE     : printf("Little Endian\n");    break;
    case H5T_ORDER_BE     : printf("Big Endian\n");       break;
    case H5T_ORDER_VAX    : printf("VAX mixed endian\n"); break;
    case H5T_ORDER_MIXED  : printf("Mixed endian\n"); break;
    case H5T_ORDER_NONE   : printf("particular order\n"); break;
  } /* end switch */
  /*  We are done with the datatype. */
  hErrVal = H5Tclose(dataTypeID);
  mjrHDF5_chkError(hErrVal);

  /* ****************************************************************************************************************************** */
  /* Figure out the size of the dataset. */
  dataSpaceID = H5Dget_space(dataSetID);
  mjrHDF5_chkError(dataSpaceID);
  /*  Get the number of dims. */
  rank = H5Sget_simple_extent_ndims(dataSpaceID);
  mjrHDF5_chkError(rank);
  if(rank > 1024) {
    /*  This can't really happen (limit is 32) */
    printf("ERROR: rank too large.\n");
    exit(1);
  } /* end if */
  /* Get the size of each dim. */
  intVal = H5Sget_simple_extent_dims(dataSpaceID, dims, maxDims);
  mjrHDF5_chkError(intVal);
  printf("  Dataspace Rank %lu\n", (unsigned long)rank);
  printf("  Dim Lengths: ");
  for(i=0; i<rank; i++)
    if(dims[i] == H5S_UNLIMITED) {
      printf("%s ", "UNLIMITED");
    } else {
      printf("%ld ", (long)(dims[i]));
    } /* end if/else */
  printf("\n");
  printf("  Max Dim Lengths: ");
  for(i=0; i<rank; i++)
    if(maxDims[i] == H5S_UNLIMITED) {
      printf("%s ", "UNLIMITED");
    } else {
      printf("%ld ", (long)(maxDims[i]));
    } /* end if/else */
  printf("\n");
  numDataPoints = H5Sget_simple_extent_npoints(dataSpaceID);
  if(numDataPoints == 0) {
    printf("ERROR: Call to H5Sget_simple_extent_npoints failed.\n");
    exit(1);
  } /* end if */
  printf("Number of data points: %lu\n", (unsigned long)numDataPoints);

  /* We are done with the dataSpaceID */
  hErrVal = H5Sclose(dataSpaceID);
  mjrHDF5_chkError(hErrVal);

  /* Get the number of attributes for the dataSet. */
  numAttrs = H5Aget_num_attrs(dataSetID);
  mjrHDF5_chkError(numAttrs);

  printf("  Number of attrs: %lu\n", (unsigned long)numAttrs);

  /* If we have any attributes, we get info for them */
  if(numAttrs > 0) {
    printf("  Attribute info:\n");

    for(curAttrIdx=0; curAttrIdx<numAttrs; curAttrIdx++) {
      attrID = H5Aopen_idx(dataSetID, curAttrIdx);
      mjrHDF5_chkError(attrID);

      attrNameSize = H5Aget_name(attrID, 1024, attrName);
      mjrHDF5_chkError(attrNameSize);

      printf("    Number %3lu:  ", (unsigned long)curAttrIdx);

	  dataTypeID  = H5Aget_type(attrID);
	  mjrHDF5_chkError(dataTypeID);
	  /* Get the class for the type. */
	  class = H5Tget_class(dataTypeID);
	  mjrHDF5_Tclass2str(class, classStr);
	  printf(" Class: %-16s", classStr);
	  /*  Get the size of the type */
	  dataSize = H5Tget_size(dataTypeID);
	  if(dataSize == 0) {
		printf("ERROR: Failure in H5Tget_size().\n");
		exit(1);
	  } /* end if */
	  printf(" Size: %3lu ", (unsigned long)dataSize);
	  hErrVal = H5Tclose(dataTypeID);
	  mjrHDF5_chkError(hErrVal);

      printf(" Name: %s \n", attrName);

      hErrVal = H5Aclose(attrID);
	  mjrHDF5_chkError(hErrVal);
    } /* end for */
  } /* end if */