Ejemplo n.º 1
0
/*------------------------------------------------------------------------------
 * Purpose: Read data from file 
 * Parameters: H5Dataset* d -- The dataset whose value to be retrieved from file
 *     to server: { opID, fid, fullpath, type, space }                      *
 *     to client: { value, error }                                          *
 * Return:  Returns a non-negative value if successful; otherwise returns a negative value.
 *------------------------------------------------------------------------------
 */
int H5Dataset_read(H5Dataset* ind, H5Dataset* outd)
{
    int ret_value=0, i=0;
    hid_t did=-1, sid=-1, tid=-1, msid=-1, ftid=-1;
    unsigned int npoints=1;
    hsize_t start[H5S_MAX_RANK], stride[H5S_MAX_RANK], count[H5S_MAX_RANK], mdims[1];
    time_t t0=0, t1=0;
    H5Eclear();

    if (ind->space.rank <=0 )
        H5Dataset_init(ind);

    t0 = time(NULL);

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

    /* TODO at H5Dataset -- we need to convert the datatype to the datatype which is passed 
     *  from client machine so that the data reading from file will be right for 
     *  the client. The byte order of the client machine, server machine, and data
     *  in the file can be all different.
     * 
     *  For the first version, we assume the server machine and client machine
     *  have the same byte order.
     */ 
    ftid = H5Dget_type(did);
    tid = H5Tget_native_type(ftid, H5T_DIR_ASCEND);
    H5Tclose(ftid);

    /* TODO at H5Dataset -- to support different byte order between the server and client */
    if (ind->type.order != get_machine_endian())
        THROW_ERROR(ind->error,
            "H5Dataset_read", "The byte order is different between the server and client",
            ret_value, done);


    sid = msid = H5S_ALL;
    npoints = 1;
    for (i=0; i<ind->space.rank; i++)
        npoints *= ind->space.count[i];
    ind->space.npoints = npoints;
    mdims[0] = npoints;
    
    /* cannot select hyperslab for scalar data point or 
       1D array with a single data point 
     */
    if ( (ind->space.rank>0) && (ind->space.rank * ind->space.dims[0]>1) )
    {
        sid = H5Dget_space(did);
        for (i=0; i<ind->space.rank; i++)
        {
            start[i] = ind->space.start[i];
            stride[i] = ind->space.stride[i];
            count[i] = ind->space.count[i];
        }
        if (H5Sselect_hyperslab(sid, H5S_SELECT_SET, start, stride, count, NULL)<0)
            THROW_H5LIBRARY_ERROR(ind->error,ret_value, done);

        msid = H5Screate_simple(1, mdims, NULL);
    }

    if (ind->value)
        H5Dataset_freeBuffer(ind->value, ind->space, ind->type, ind->nvalue);

    if (NULL == (ind->value = malloc(npoints*ind->type.size)) )
        THROW_ERROR(ind->error, "H5Dataset_read",
            "unable to allocate memory to read data from file",
            ret_value, done);
   
    ind->nvalue = npoints*ind->type.size; 
    ind->tclass = ind->type.tclass; 
    if ( H5Dread(did, tid, msid, sid, H5P_DEFAULT, ind->value) < 0)
    {
        free (ind->value);
        ind->value = NULL;
        THROW_H5LIBRARY_ERROR(ind->error,ret_value, done);
    } 
  
    /* convert compound and variable length data into strings
     * so that the client application is able to display it
     */ 
    if ( (H5DATATYPE_VLEN == ind->type.tclass )
         || (H5DATATYPE_COMPOUND == ind->type.tclass)
         || (H5DATATYPE_STRING == ind->type.tclass )
       )
    {
        H5Dataset_value_to_string(ind, tid, msid);
    }

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

    t1 = time(NULL);

#ifndef HDF5_LOCAL
    memset (outd, 0, sizeof (H5Dataset));

    /* pass on the value */
    outd->nvalue = ind->nvalue;
    outd->value = ind->value;
    outd->tclass = ind->tclass;
    outd->error = ind->error;
    outd->time = (long)(t1-t0);

    ind->value = NULL;
    ind->nvalue = 0;
#endif

    return ret_value;
}
Ejemplo n.º 2
0
/*------------------------------------------------------------------------------
 * Purpose: Read data value of an attribute from file
 * Parameters: H5Attribute* a -- The attribute whose value is read from file
 * Return:  Returns a non-negative value if successful; otherwise returns a negative value.
 *------------------------------------------------------------------------------
 */
int H5Attribute_read(H5Attribute *a)
{
    int ret_value=0, i=0;
    unsigned int npoints;
    hid_t aid=-1, loc_id=-1, tid=-1, ftid=-1, sid=-1;

    assert(a);
    H5Eclear();

    if (a->obj_type == H5OBJECT_DATASET)
        loc_id = H5Dopen(a->fid, a->obj_path);
    else if (a->obj_type == H5OBJECT_GROUP)
        loc_id = H5Gopen(a->fid, a->obj_path);
    else
        THROW_ERROR(a->error, "H5Attribute_read", 
        "Object must be either H5Dataset or H5Group",
        ret_value, done);

	if (loc_id < 0)
        THROW_H5LIBRARY_ERROR(a->error, ret_value, done);

    if ( (aid = H5Aopen_name(loc_id, a->name)) < 0)
        THROW_H5LIBRARY_ERROR(a->error, ret_value, done);

    if ( (ftid = H5Aget_type(aid)) < 0)
        THROW_H5LIBRARY_ERROR(a->error, ret_value, done);

    tid = H5Tget_native_type(ftid, H5T_DIR_ASCEND);
    H5Tclose(ftid);

    /* TODO at H5Attribute -- to support different byte order between the server and client */
    if (a->type.order != get_machine_endian())
        THROW_ERROR(a->error, "H5Attribute_read", 
        "Different byte-order between server and client",
        ret_value, done);

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

    if (a->value)
        H5Dataset_freeBuffer(a->value, a->space, a->type, a->nvalue);

    if (NULL == (a->value = malloc(npoints*a->type.size)) )
        THROW_ERROR(a->error, "H5Attribute_read", 
        "unable to allocate memory to read data from file",
        ret_value, done);

    a->nvalue = npoints*a->type.size;
    if ( H5Aread(aid, tid, a->value) < 0)
    {
        free (a->value);
        a->value = NULL;
        THROW_H5LIBRARY_ERROR(a->error, ret_value, done);
    }

    /* convert compound and variable length data into strings
     * so that the client application is able to display it
     */
    if ( (H5DATATYPE_VLEN == a->type.tclass )
         || (H5DATATYPE_COMPOUND == a->type.tclass)
         || (H5DATATYPE_STRING == a->type.tclass )
       )
    {
        sid = H5Aget_space(aid);
        H5Attribute_value_to_string(a, tid, sid);
        H5Sclose(sid);
    }


done:
    if (tid > 0 ) H5Tclose(tid); 
    if (aid > 0) H5Aclose(aid);

    if (loc_id > 0)
    {
        if (a->obj_type == H5OBJECT_DATASET)
            H5Dclose(loc_id);
        else if (a->obj_type == H5OBJECT_GROUP)
            H5Gclose(loc_id);
    }

    return ret_value;
}
Ejemplo n.º 3
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;
}
Ejemplo n.º 4
0
/*--------------------------------------------------------------------------*/
void HDF5ErrorCleanup()
{
    H5Eclear(H5Eget_current_stack());
}