Esempio n. 1
0
static VALUE
rb_H5Fget_obj_ids (VALUE mod, VALUE v_file_id, VALUE v_type, VALUE v_ids)
{
  int count;
  hid_t *ids;

  count = H5Fget_obj_count(NUM2INT(v_file_id), NUM2ULONG(v_type));

  if ( count < 0 )
    rb_hdf5_raise("can't count object");
  
  ids = xmalloc(count*sizeof(hid_t));
  
  count = H5Fget_obj_ids(NUM2INT(v_file_id), NUM2ULONG(v_type), count, ids);

  if ( count >= 0 )
    HID2ARY(count, ids, v_ids);  

  free(ids);

  if ( count < 0 )
    rb_hdf5_raise("can't get object ids");

  return INT2NUM(count);
}
Esempio n. 2
0
/*
Write an existing noise correlation object to disk.
If filename is already a file, fail.
*/
void WriteNoiseCorrelations(std::string filename, const NoiseCorrelations& noise)
{
  hid_t fileID = H5Fcreate(filename.c_str(), H5F_ACC_EXCL, H5P_DEFAULT, H5P_DEFAULT);

  // Write a version number of 1.
  {
    unsigned char version = 1;
    hid_t scalarID = H5Screate(H5S_SCALAR);
    hid_t attID = H5Acreate2(fileID, "version", H5T_STD_U8LE, scalarID, H5P_DEFAULT, H5P_DEFAULT);
    H5Awrite(attID, H5T_NATIVE_UCHAR, reinterpret_cast<void*>(&version));
    H5Aclose(attID);
    H5Sclose(scalarID);
  }

  // Write the channel index as an ordered list of included channels.
  HDF5Helper::WriteMapAsAttribute(noise.GetNoiseBlockIndex().MinorIndex(),
                                  fileID,
                                  "channel_list");

  // Write the actual noise information.
  // We choose to write one dataset per frequency, since those are stored in memory as separate arrays.
  // However, we need to manually pick out only the entries which contain non-redundant information.
  std::vector<double> PackedArray; // Reuse rather than re-allocating each time.
  for(size_t f = 0; f < 1024; f++) {
    const NoiseMatrix& mat = noise.GetMatrixForIndex(f);
    const NoiseCorrelations::NoiseBlockIndexT& NoiseBlockIndex = noise.GetNoiseBlockIndex();
    assert(NoiseBlockIndex.MaxIndex() % 2 == 0 and NoiseBlockIndex.MaxIndex() > 0);

    // Create the name for this dataset.
    std::ostringstream strstream;
    strstream << "/noise_corr_" << std::setfill('0') << std::setw(4) << f;
    std::string dataset_name = strstream.str();

    // Allocate space in the temporary vector.
    hsize_t ExpectedSize = ExpectedPackedSize(f, NoiseBlockIndex.MaxIndex());
    PackedArray.resize(0);
    PackedArray.reserve(NoiseBlockIndex.MaxIndex()*NoiseBlockIndex.MaxIndex()/4);

    // Fill PackedArray. Take into account all appropriate symmetries.
    for(size_t i = 0; i < NoiseBlockIndex.MaxIndex(); i++) {
      for(size_t j = i; j < NoiseBlockIndex.MaxIndex(); j++) {
        if(not IncludeEntryInPackedArray(f, i, j, NoiseBlockIndex)) continue;
        PackedArray.push_back(mat.GetCorrByIndex(i, j));
      }
    }
    assert(PackedArray.size() == ExpectedSize);

    // Write the array to file.
    hid_t vectorID = H5Screate_simple(1, &ExpectedSize, NULL);
    hid_t datasetID = H5Dcreate2(fileID, dataset_name.c_str(), H5T_IEEE_F64LE, vectorID, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
    H5Dwrite(datasetID, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT,
             reinterpret_cast<void*>(&PackedArray[0]));
    H5Dclose(datasetID);
    H5Sclose(vectorID);
  }

  assert(H5Fget_obj_count(fileID, H5F_OBJ_ALL) == 1); // The file should be the only object left.
  H5Fclose(fileID);
}
Esempio n. 3
0
//-*****************************************************************************
AwImpl::~AwImpl()
{
    delete m_top;
    m_top = NULL;

    // empty out the map so any dataset IDs will be freed up
    m_writtenArraySampleMap.m_map.clear();

    if ( m_file >= 0 )
    {
        int dsetCount = H5Fget_obj_count( m_file,
            H5F_OBJ_LOCAL | H5F_OBJ_DATASET);
        int grpCount = H5Fget_obj_count( m_file,
            H5F_OBJ_LOCAL | H5F_OBJ_GROUP );
        int dtypCount = H5Fget_obj_count( m_file,
            H5F_OBJ_LOCAL | H5F_OBJ_DATATYPE );
        int attrCount = H5Fget_obj_count( m_file,
            H5F_OBJ_LOCAL | H5F_OBJ_ATTR );

        int objCount = dsetCount + grpCount + dtypCount + attrCount;

        if ( objCount != 0 )
        {
            std::string excStr =
                ( boost::format(
                      "Open HDF5 handles detected during writing:\n"
                      "DataSets: %d, Groups: %d, "
                      "DataTypes: %d, Attributes: %d" )
                  % dsetCount
                  % grpCount
                  % dtypCount
                  % attrCount ).str();

            m_file = -1;
            ABCA_THROW( excStr );
        }

        H5Fclose( m_file );
        m_file = -1;
    }
}
Esempio n. 4
0
static VALUE
rb_H5Fget_obj_count (VALUE mod, VALUE v_file_id, VALUE v_type)
{
  int count;

  count = H5Fget_obj_count(NUM2INT(v_file_id), NUM2ULONG(v_type));

  if ( count < 0 )
    rb_hdf5_raise("can't count object");
  
  return INT2NUM(count);
}
Esempio n. 5
0
int
mhdf_checkOpenHandles( mhdf_FileHandle handle,
                       mhdf_Status* status )
{
  FileHandle* file_ptr;
  int result;
  API_BEGIN;

  file_ptr = (FileHandle*)(handle);
  if (!mhdf_check_valid_file( file_ptr, status ))
    return -1;
  
  /* Check for open handles.  HDF5 will not actually close the
     file until all handles are closed. */
  result = H5Fget_obj_count( file_ptr->hdf_handle, H5F_OBJ_ALL );
  if (result != 1)
  {
    mhdf_setFail( status, "Cannot close file with open handles: "
                 "%d file, %d data, %d group, %d type, %d attr\n",
                  H5Fget_obj_count( file_ptr->hdf_handle, H5F_OBJ_FILE ) - 1,
                  H5Fget_obj_count( file_ptr->hdf_handle, H5F_OBJ_DATASET ),
                  H5Fget_obj_count( file_ptr->hdf_handle, H5F_OBJ_GROUP ),
                  H5Fget_obj_count( file_ptr->hdf_handle, H5F_OBJ_DATATYPE ),
                  H5Fget_obj_count( file_ptr->hdf_handle, H5F_OBJ_ATTR ) );
    return result - 1;
  }

  API_END_H( 0 );
  return 0;
}
Esempio n. 6
0
H5File::~H5File()
{
    H5Fflush(file, H5F_SCOPE_GLOBAL);
    cleanup();
    if (file >= 0)
    {

#if defined(__HDF5OBJECTS_DEBUG__)

        std::cout << "File " << filename << " is closing." << std::endl
                  << "Open groups: " << H5Fget_obj_count(file, H5F_OBJ_GROUP) << std::endl
                  << "Open datasets: " << H5Fget_obj_count(file, H5F_OBJ_DATASET) << std::endl
                  << "Open datatypes: " << H5Fget_obj_count(file, H5F_OBJ_DATATYPE) << std::endl
                  << "Open attributes: " << H5Fget_obj_count(file, H5F_OBJ_ATTR) << std::endl
                  << "Open all (except the file itself): " << H5Fget_obj_count(file, H5F_OBJ_ALL) - 1 << std::endl;

#endif

        H5Fclose(file);
        H5garbage_collect();
    }
}
Esempio n. 7
0
//-*****************************************************************************
ArImpl::~ArImpl()
{
    delete m_top;
    m_top = NULL;

    if ( m_file >= 0 )
    {
        int dsetCount = H5Fget_obj_count( m_file,
            H5F_OBJ_LOCAL | H5F_OBJ_DATASET);
        int grpCount = H5Fget_obj_count( m_file,
            H5F_OBJ_LOCAL | H5F_OBJ_GROUP );
        int dtypCount = H5Fget_obj_count( m_file,
            H5F_OBJ_LOCAL | H5F_OBJ_DATATYPE );
        int attrCount = H5Fget_obj_count( m_file,
            H5F_OBJ_LOCAL | H5F_OBJ_ATTR );

        int objCount = dsetCount + grpCount + dtypCount + attrCount;

        if ( objCount != 0 )
        {
            std::string excStr =
                ( boost::format(
                      "Open HDF5 handles detected during reading:\n"
                      "DataSets: %d, Groups: %d, "
                      "DataTypes: %d, Attributes: %d" )
                  % dsetCount
                  % grpCount
                  % dtypCount
                  % attrCount ).str();

            m_file = -1;
            ABCA_THROW( excStr );
        }

        H5Fclose( m_file );
        m_file = -1;
    }
}
Esempio n. 8
0
int_f
nh5fget_obj_count_c ( hid_t_f *file_id , int_f *obj_type, size_t_f * obj_count)
{
  int ret_value = 0;
  hid_t c_file_id;
  unsigned c_obj_type;
  ssize_t c_obj_count;


  c_file_id = (hid_t)*file_id;
  c_obj_type = (unsigned) *obj_type;
  if ( (c_obj_count=H5Fget_obj_count(c_file_id, c_obj_type)) < 0  ) ret_value = -1;
  *obj_count = (size_t_f)c_obj_count;
  return ret_value;
}
Esempio n. 9
0
int AH5_close(hid_t file_id)
{
  const ssize_t count = H5Fget_obj_count(file_id, H5F_OBJ_ALL) - 1;
  const herr_t err = H5Fclose(file_id);

  if (count) {
    AH5_log_error(
        "Number of open object identifiers for file %d not 0 but %d. *****\n\n",
        (int) file_id, (int) count);
    return count;
  }

  if (err < 0)
    return err;

  return 0;
}
Esempio n. 10
0
void FileHDF5::close() {

    if (!isOpen())
        return;

    data.close();
    metadata.close();
    root.close();

    unsigned types = H5F_OBJ_GROUP|H5F_OBJ_DATASET|H5F_OBJ_DATATYPE;

    ssize_t obj_count = H5Fget_obj_count(hid, types);

    if (obj_count < 0) {
        throw H5Exception("FileHDF5::close(): Could not get object count");
    }

    std::vector<hid_t> objs(static_cast<size_t>(obj_count));

    if (obj_count > 0) {
        obj_count = H5Fget_obj_ids(hid, types, objs.size(), objs.data());

        if (obj_count < 0) {
            throw H5Exception("FileHDF5::close(): Could not get objs");
        }
    }

    for (auto obj : objs) {
        int ref_count = H5Iget_ref(obj);

        for (int j = 0; j < ref_count; j++) {
            H5Oclose(obj);
        }
    }

    H5Object::close();
}
Esempio n. 11
0
int
main(int argc, char **argv) {
  // Try block to detect exceptions raised by any of the calls inside it
  try {
    // Turn off the auto-printing when failure occurs so that we can
    // handle the errors appropriately
    H5std_string FILE_NAME(argv[1]);
    Exception::dontPrint();

    // Open the file and the dataset in the file.
    H5File file(FILE_NAME, H5F_ACC_RDONLY);

    DataSet dataset;
    H5std_string dataset_name;
    auto objCount(H5Fget_obj_count(file.getId(), H5F_OBJ_ALL));
    for (size_t i = 0; i != objCount; ++i)
      if (H5G_DATASET == file.getObjTypeByIdx(i)) {
	dataset_name = file.getObjnameByIdx(i);
	dataset = file.openDataSet(dataset_name);
      }

    auto datatype(dataset.getDataType());
    auto dataspace(dataset.getSpace());

    hsize_t dims_in[2];
    auto ndims(dataspace.getSimpleExtentDims(dims_in, NULL));

    hsize_t dims_out[2] = { DIM0, DIM1 };	// dataset dimensions

    double *buf = new double[dims_in[0] * dims_in[1]];

    // Read data.
    dataset.read(buf, PredType::NATIVE_DOUBLE);//, memspace, dataspace);

    H5std_string outFileName("out.h5");

    // Create a new file using the default property lists. 
    H5File outfile(outFileName, H5F_ACC_TRUNC);

    // Create the data space for the dataset.
    DataSpace *output_dataspace = new DataSpace(ndims, dims_out);

    hsize_t chunk_dims[2] = { 20, 20 };	// chunk dimensions
    // Modify dataset creation property to enable chunking
    DSetCreatPropList *plist = new DSetCreatPropList;
    plist->setChunk(2, chunk_dims);

    // Set ZLIB (DEFLATE) Compression using level 9.
    plist->setDeflate(9);

    // Create the attributes.
    const size_t numAttrs = file.getNumAttrs();
    for (size_t i = 0; i != numAttrs; ++i) {
      auto attr(file.openAttribute(i));
      auto output_attr(outfile.createAttribute(attr.getName(), 
					       attr.getDataType(), 
					       attr.getSpace()));

      switch (attr.getTypeClass()) {
      case H5T_FLOAT: {
	double buf;
    	attr.read(attr.getDataType(), &buf);
    	output_attr.write(attr.getDataType(), &buf);
      }
	break;
      case H5T_STRING: {
	char *buf = new char[(unsigned long)attr.getStorageSize()];
    	attr.read(attr.getDataType(), buf);
    	output_attr.write(attr.getDataType(), buf);
	delete buf;
      }
	break;
      default:
	break;
      }
    }

    // Create the dataset.      
    DataSet *output_dataset = new DataSet(outfile.createDataSet(dataset_name, datatype, *output_dataspace, *plist));

    // Write data to dataset.
    output_dataset->write(buf, datatype);

    // Close objects and file.  Either approach will close the HDF5 item.
    delete output_dataspace;
    delete output_dataset;
    delete plist;
    file.close();
  }  // end of try block

  // catch failure caused by the H5File operations
  catch(FileIException &error) {
    error.printError();
    return -1;
  }

  // catch failure caused by the DataSet operations
  catch(DataSetIException &error) {
    error.printError();
    return -1;
  }

  // catch failure caused by the DataSpace operations
  catch(DataSpaceIException &error) {
    error.printError();
    return -1;
  }

  // catch failure caused by the Attribute operations
  catch (AttributeIException &error) {
    error.printError();
    return -1;
  }

  catch (std::exception &error) {
    std::cerr << error.what() << std::endl;
    return -1;
  }

  return 0;  // successfully terminated
}
Esempio n. 12
0
int mhdf_countOpenHandles( mhdf_FileHandle file_handle )
{
  return H5Fget_obj_count(((FileHandle*)file_handle)->hdf_handle,H5F_OBJ_ALL);
}
Esempio n. 13
0
int H5mdfile::get_open_objects(int argc, char **argv, Tcl_Interp *interp)
{
    ssize_t open_objects = H5Fget_obj_count(file_id, H5F_OBJ_ALL);
    std::cout << "There are " << open_objects << " open hdf5 objects!" << std::endl;
    return TCL_OK;
}
Esempio n. 14
0
/*
Read noise correlations from a file into a noise object.
Only read noise related to channels which are in the noise object's map,
so be sure to call SetChannelIndex before reading correlations into it.
*/
void ReadNoiseCorrelations(std::string filename, NoiseCorrelations& noise)
{
  hid_t fileID = H5Fopen(filename.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT);

  // Verify this is version 1.
  {
    unsigned char version;
    hid_t attID = H5Aopen(fileID, "version", H5P_DEFAULT);
    H5Aread(attID, H5T_NATIVE_UCHAR, reinterpret_cast<void*>(version));
    H5Aclose(attID);
    assert(version == 1);
  }

  // I have one index for the in-memory noise matrices; build another for the file.
  MapIndexHandler<unsigned char> FileChannelMap = HDF5Helper::ReadMapFromAttribute(fileID, "channel_list");
  for(size_t i = 0; i < noise.GetNoiseBlockIndex().MaxIndex(); i++) {
    // Verify that the file has correlation information for every channel we're requesting.
    assert(FileChannelMap.HasKey(noise.GetNoiseBlockIndex().KeyForIndex(i).second));
  }
  NoiseCorrelations::NoiseBlockIndexT FileBlockIndex(RangeIndexHandler<unsigned char>(0, 2),
                                                     FileChannelMap);

  // Retrieve the actual noise information.
  std::vector<double> PackedArray;
  for(size_t f = 0; f < 1024; f++) {
    NoiseMatrix& mat = noise.GetMatrixForIndex(f);
    const NoiseCorrelations::NoiseBlockIndexT& NoiseBlockIndex = noise.GetNoiseBlockIndex();
    assert(NoiseBlockIndex.MaxIndex() % 2 == 0 and NoiseBlockIndex.MaxIndex() > 0);

    // Create the name for this dataset.
    std::ostringstream strstream;
    strstream << "/noise_corr_" << std::setfill('0') << std::setw(4) << f;
    std::string dataset_name = strstream.str();

    // Get data from file.
    hid_t datasetID = H5Dopen2(fileID, dataset_name.c_str(), H5P_DEFAULT);
    hid_t dataspaceID = H5Dget_space(datasetID);
    size_t PackedSize = ExpectedPackedSize(f, FileBlockIndex.MaxIndex());
    assert(PackedSize == H5Sget_simple_extent_npoints(dataspaceID));
    PackedArray.resize(PackedSize);
    H5Dread(datasetID, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT,
            reinterpret_cast<void*>(&PackedArray[0]));

    // Fill noise matrix from packed array.
    for(size_t i = 0; i < NoiseBlockIndex.MaxIndex(); i++) {
      size_t rowInFile = FileBlockIndex.IndexForKey(NoiseBlockIndex.KeyForIndex(i));
      for(size_t j = i; j < NoiseBlockIndex.MaxIndex(); j++) {
        size_t colInFile = FileBlockIndex.IndexForKey(NoiseBlockIndex.KeyForIndex(j));

        std::pair<size_t, bool> packed_loc = PackedArrayIndexFor(f, rowInFile, colInFile, FileBlockIndex);
        if(packed_loc.first == size_t(-1)) mat.GetCorrByIndex(i, j) = 0;

        else {
          assert(packed_loc.first < PackedArray.size());
          if(packed_loc.second) mat.GetCorrByIndex(i, j) = -PackedArray[packed_loc.first];
          else mat.GetCorrByIndex(i, j) = PackedArray[packed_loc.first];
        }
      }
    }

    // Release HDF5 file resources.
    H5Sclose(dataspaceID);
    H5Dclose(datasetID);
  }

  assert(H5Fget_obj_count(fileID, H5F_OBJ_ALL) == 1); // The file should be the only object left.
  H5Fclose(fileID);
}
//-*****************************************************************************
ArImpl::~ArImpl()
{

    m_data.reset();

    if ( m_file >= 0 )
    {
        int dsetCount = H5Fget_obj_count( m_file,
            H5F_OBJ_LOCAL | H5F_OBJ_DATASET);
        int grpCount = H5Fget_obj_count( m_file,
            H5F_OBJ_LOCAL | H5F_OBJ_GROUP );
        int dtypCount = H5Fget_obj_count( m_file,
            H5F_OBJ_LOCAL | H5F_OBJ_DATATYPE );
        int attrCount = H5Fget_obj_count( m_file,
            H5F_OBJ_LOCAL | H5F_OBJ_ATTR );

        int objCount = dsetCount + grpCount + dtypCount + attrCount;

        if ( objCount != 0 )
        {
            std::stringstream strm;
            strm << "Open HDF5 handles detected during reading:" << std::endl
                 << "DataSets: " << dsetCount
                 << ", Groups: " << grpCount
                 << ", DataTypes: " << dtypCount
                 << ", Attributes: " << attrCount;

            std::vector< hid_t > objList;

            // when getting the name corresponding to a hid_t, the get_name
            // functions always append a NULL character, which we strip off
            // when injecting into out stream.
            std::string name;

            if ( dsetCount > 0 )
            {
                strm << std::endl << "DataSets: " << std::endl;
                objList.resize( dsetCount );
                H5Fget_obj_ids( m_file, H5F_OBJ_LOCAL | H5F_OBJ_DATASET,
                    dsetCount, &objList.front() );
                for ( int i = 0; i < dsetCount; ++i )
                {
                    int strLen = H5Iget_name( objList[i], NULL, 0 ) + 1;
                    name.resize( strLen );
                    H5Iget_name( objList[i], &(name[0]), strLen );
                    strm << name.substr(0, name.size() - 1) << std::endl;
                }
            }

            if ( grpCount > 0 )
            {
                strm << std::endl << std::endl << "Groups:" << std::endl;
                objList.resize( grpCount );
                H5Fget_obj_ids( m_file, H5F_OBJ_LOCAL | H5F_OBJ_GROUP,
                    grpCount, &objList.front() );
                for ( int i = 0; i < grpCount; ++i )
                {
                    int strLen = H5Iget_name( objList[i], NULL, 0 ) + 1;
                    name.resize( strLen );
                    H5Iget_name( objList[i], &(name[0]), strLen );
                    strm << std::endl << name.substr(0, name.size() - 1);
                }
            }

            if ( attrCount > 0 )
            {
                strm << std::endl << std::endl << "Attrs:" << std::endl;
                objList.resize( attrCount );
                H5Fget_obj_ids( m_file, H5F_OBJ_LOCAL | H5F_OBJ_ATTR,
                    attrCount, &objList.front() );
                for ( int i = 0; i < attrCount; ++i )
                {
                    int strLen = H5Aget_name( objList[i], 0, NULL ) + 1;
                    name.resize( strLen );
                    H5Aget_name( objList[i], strLen, &(name[0]) );
                    strm << std::endl << name.substr(0, name.size() - 1);
                }
            }

            // just for formatting purposes
            if ( dtypCount > 0 )
            {
                strm << std::endl;
            }

            m_file = -1;
            ABCA_THROW( strm.str() );
        }

        H5Fclose( m_file );
        m_file = -1;
    }
}
Esempio n. 16
0
int QH5File :: objectCount() const
{
  auto size = H5Fget_obj_count (fileId, H5F_OBJ_ALL);
  return size;
}