2012-02-02 09:20:49 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2011 Thorsten Liebig (Thorsten.Liebig@gmx.de)
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
#include "hdf5_file_writer.h"
|
|
|
|
#include <boost/algorithm/string.hpp>
|
|
|
|
#include <hdf5.h>
|
|
|
|
|
|
|
|
#include <sstream>
|
|
|
|
#include <iostream>
|
|
|
|
#include <iomanip>
|
|
|
|
|
|
|
|
HDF5_File_Writer::HDF5_File_Writer(string filename)
|
|
|
|
{
|
|
|
|
m_filename = filename;
|
|
|
|
m_Group = "/";
|
|
|
|
hid_t hdf5_file = H5Fcreate(m_filename.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
|
|
|
|
if (hdf5_file<0)
|
|
|
|
{
|
|
|
|
cerr << "HDF5_File_Writer::HDF5_File_Writer: Error, creating the given file """ << m_filename << """ failed" << endl;
|
|
|
|
}
|
|
|
|
H5Fclose(hdf5_file);
|
|
|
|
}
|
|
|
|
|
|
|
|
HDF5_File_Writer::~HDF5_File_Writer()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
hid_t HDF5_File_Writer::OpenGroup(hid_t hdf5_file, string group)
|
|
|
|
{
|
|
|
|
if (hdf5_file<0)
|
|
|
|
{
|
|
|
|
cerr << "HDF5_File_Writer::CreateGroup: Error, invalid file id" << endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<string> results;
|
|
|
|
boost::split(results, group, boost::is_any_of("/"));
|
|
|
|
|
|
|
|
hid_t grp=H5Gopen(hdf5_file,"/");
|
|
|
|
if (grp<0)
|
|
|
|
{
|
|
|
|
cerr << "HDF5_File_Writer::OpenGroup: Error, opening root group " << endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t n=0;n<results.size();++n)
|
|
|
|
{
|
2012-02-06 13:09:51 +00:00
|
|
|
hid_t old_grp = grp;
|
2012-02-02 09:20:49 +00:00
|
|
|
if (!results.at(n).empty())
|
|
|
|
{
|
|
|
|
if (H5Lexists(grp, results.at(n).c_str(), H5P_DEFAULT))
|
|
|
|
{
|
|
|
|
grp = H5Gopen(grp, results.at(n).c_str());
|
2012-02-06 13:09:51 +00:00
|
|
|
H5Gclose(old_grp);
|
2012-02-02 09:20:49 +00:00
|
|
|
if (grp<0)
|
|
|
|
{
|
|
|
|
cerr << "HDF5_File_Writer::OpenGroup: Error, failed to open existing group" << endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
grp = H5Gcreate(grp,results.at(n).c_str(),0);
|
2012-02-06 13:09:51 +00:00
|
|
|
H5Gclose(old_grp);
|
2012-02-02 09:20:49 +00:00
|
|
|
if (grp<0)
|
|
|
|
{
|
|
|
|
cerr << "HDF5_File_Writer::OpenGroup: Error, creating group """ << group << """ failed" << endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return grp;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HDF5_File_Writer::WriteRectMesh(unsigned int const* numLines, double const* const* discLines, int MeshType, double scaling)
|
|
|
|
{
|
|
|
|
float* array[3];
|
|
|
|
for (int n=0; n<3; ++n)
|
|
|
|
{
|
|
|
|
array[n] = new float[numLines[n]];
|
|
|
|
for (unsigned int i=0; i<numLines[n]; ++i)
|
|
|
|
array[n][i]=discLines[n][i];
|
|
|
|
|
|
|
|
}
|
|
|
|
bool success = WriteRectMesh(numLines,array,MeshType,scaling);
|
|
|
|
for (int n=0; n<3; ++n)
|
|
|
|
delete[] array[n];
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HDF5_File_Writer::WriteRectMesh(unsigned int const* numLines, float const* const* discLines, int MeshType, float scaling)
|
|
|
|
{
|
|
|
|
hid_t hdf5_file = H5Fopen( m_filename.c_str(), H5F_ACC_RDWR, H5P_DEFAULT );
|
|
|
|
if (hdf5_file<0)
|
|
|
|
{
|
|
|
|
cerr << "HDF5_File_Writer::WriteRectMesh: Error, opening the given file """ << m_filename << """ failed" << endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (H5Lexists(hdf5_file, "/Mesh", H5P_DEFAULT))
|
|
|
|
{
|
|
|
|
cerr << "HDF5_File_Writer::WriteRectMesh: Error, group ""/Mesh"" already exists" << endl;
|
2012-02-06 13:09:51 +00:00
|
|
|
H5Fclose(hdf5_file);
|
2012-02-02 09:20:49 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
hid_t mesh_grp = H5Gcreate(hdf5_file,"/Mesh",0);
|
|
|
|
if (mesh_grp<0)
|
|
|
|
{
|
|
|
|
cerr << "HDF5_File_Writer::WriteRectMesh: Error, creating group ""/Mesh"" failed" << endl;
|
2012-02-06 13:09:51 +00:00
|
|
|
H5Fclose(hdf5_file);
|
2012-02-02 09:20:49 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
string names[] = {"x","y","z"};
|
|
|
|
if (MeshType==1)
|
|
|
|
{
|
|
|
|
names[0]="rho";
|
|
|
|
names[1]="alpha";
|
|
|
|
}
|
|
|
|
if (MeshType==2)
|
|
|
|
{
|
|
|
|
names[0]="r";
|
|
|
|
names[1]="theta";
|
|
|
|
names[2]="phi";
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int n=0; n<3; ++n)
|
|
|
|
{
|
|
|
|
hsize_t dims[1]={numLines[n]};
|
|
|
|
hid_t space = H5Screate_simple(1, dims, NULL);
|
|
|
|
hid_t dataset = H5Dcreate(mesh_grp, names[n].c_str(), H5T_NATIVE_FLOAT, space, H5P_DEFAULT);
|
|
|
|
float* array = new float[numLines[n]];
|
|
|
|
for (unsigned int i=0; i<numLines[n]; ++i)
|
|
|
|
{
|
|
|
|
if ((MeshType==1) && (n==1)) //check for alpha-direction
|
|
|
|
array[i] = discLines[n][i];
|
|
|
|
else if ((MeshType==2) && (n>0)) //check for theta/phi-direction
|
|
|
|
array[i] = discLines[n][i];
|
|
|
|
else
|
|
|
|
array[i] = discLines[n][i] * scaling;
|
|
|
|
}
|
|
|
|
if (H5Dwrite(dataset, H5T_NATIVE_FLOAT, space, H5P_DEFAULT, H5P_DEFAULT, array))
|
|
|
|
{
|
|
|
|
cerr << "HDF5_File_Writer::WriteRectMesh: Error, writing to dataset failed" << endl;
|
|
|
|
delete[] array;
|
2012-02-06 13:09:51 +00:00
|
|
|
H5Dclose(dataset);
|
|
|
|
H5Sclose(space);
|
|
|
|
H5Gclose(mesh_grp);
|
|
|
|
H5Fclose(hdf5_file);
|
2012-02-02 09:20:49 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
delete[] array;
|
2012-02-06 13:09:51 +00:00
|
|
|
H5Dclose(dataset);
|
|
|
|
H5Sclose(space);
|
2012-02-02 09:20:49 +00:00
|
|
|
}
|
2012-02-06 13:09:51 +00:00
|
|
|
H5Gclose(mesh_grp);
|
2012-02-02 09:20:49 +00:00
|
|
|
H5Fclose(hdf5_file);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HDF5_File_Writer::WriteScalarField(std::string dataSetName, float const* const* const* field, size_t datasize[3])
|
|
|
|
{
|
|
|
|
size_t pos = 0;
|
|
|
|
size_t size = datasize[0]*datasize[1]*datasize[2];
|
|
|
|
size_t n_size[3]={datasize[2],datasize[1],datasize[0]};
|
|
|
|
float* buffer = new float[size];
|
|
|
|
for (size_t k=0;k<datasize[2];++k)
|
|
|
|
for (size_t j=0;j<datasize[1];++j)
|
|
|
|
for (size_t i=0;i<datasize[0];++i)
|
|
|
|
{
|
|
|
|
buffer[pos++]=field[i][j][k];
|
|
|
|
}
|
|
|
|
bool success = WriteData(dataSetName,buffer,3,n_size);
|
|
|
|
delete[] buffer;
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HDF5_File_Writer::WriteScalarField(std::string dataSetName, std::complex<float> const* const* const* field, size_t datasize[3])
|
|
|
|
{
|
|
|
|
size_t pos = 0;
|
|
|
|
size_t size = datasize[0]*datasize[1]*datasize[2];
|
|
|
|
size_t n_size[3]={datasize[2],datasize[1],datasize[0]};
|
|
|
|
float* buffer = new float[size];
|
|
|
|
for (size_t k=0;k<datasize[2];++k)
|
|
|
|
for (size_t j=0;j<datasize[1];++j)
|
|
|
|
for (size_t i=0;i<datasize[0];++i)
|
|
|
|
{
|
|
|
|
buffer[pos++]=real(field[i][j][k]);
|
|
|
|
}
|
|
|
|
bool success = WriteData(dataSetName + "_real",buffer,3,n_size);
|
|
|
|
|
|
|
|
pos = 0;
|
|
|
|
for (size_t k=0;k<datasize[2];++k)
|
|
|
|
for (size_t j=0;j<datasize[1];++j)
|
|
|
|
for (size_t i=0;i<datasize[0];++i)
|
|
|
|
{
|
|
|
|
buffer[pos++]=imag(field[i][j][k]);
|
|
|
|
}
|
|
|
|
success &= WriteData(dataSetName + "_imag",buffer,3,n_size);
|
|
|
|
|
|
|
|
delete[] buffer;
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HDF5_File_Writer::WriteVectorField(std::string dataSetName, float const* const* const* const* field, size_t datasize[3])
|
|
|
|
{
|
|
|
|
size_t pos = 0;
|
|
|
|
size_t size = datasize[0]*datasize[1]*datasize[2]*3;
|
|
|
|
float* buffer = new float[size];
|
|
|
|
for (int n=0;n<3;++n)
|
|
|
|
for (size_t k=0;k<datasize[2];++k)
|
|
|
|
for (size_t j=0;j<datasize[1];++j)
|
|
|
|
for (size_t i=0;i<datasize[0];++i)
|
|
|
|
{
|
|
|
|
buffer[pos++]=field[n][i][j][k];
|
|
|
|
}
|
|
|
|
size_t n_size[4]={3,datasize[2],datasize[1],datasize[0]};
|
|
|
|
bool success = WriteData(dataSetName,buffer,4,n_size);
|
|
|
|
delete[] buffer;
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HDF5_File_Writer::WriteVectorField(std::string dataSetName, std::complex<float> const* const* const* const* field, size_t datasize[3])
|
|
|
|
{
|
|
|
|
size_t pos = 0;
|
|
|
|
size_t size = datasize[0]*datasize[1]*datasize[2]*3;
|
|
|
|
size_t n_size[4]={3,datasize[2],datasize[1],datasize[0]};
|
|
|
|
float* buffer = new float[size];
|
|
|
|
for (int n=0;n<3;++n)
|
|
|
|
for (size_t k=0;k<datasize[2];++k)
|
|
|
|
for (size_t j=0;j<datasize[1];++j)
|
|
|
|
for (size_t i=0;i<datasize[0];++i)
|
|
|
|
{
|
|
|
|
buffer[pos++]=real(field[n][i][j][k]);
|
|
|
|
}
|
|
|
|
bool success = WriteData(dataSetName + "_real",buffer,4,n_size);
|
|
|
|
|
|
|
|
pos = 0;
|
|
|
|
for (int n=0;n<3;++n)
|
|
|
|
for (size_t k=0;k<datasize[2];++k)
|
|
|
|
for (size_t j=0;j<datasize[1];++j)
|
|
|
|
for (size_t i=0;i<datasize[0];++i)
|
|
|
|
{
|
|
|
|
buffer[pos++]=imag(field[n][i][j][k]);
|
|
|
|
}
|
|
|
|
success &= WriteData(dataSetName + "_imag",buffer,4,n_size);
|
|
|
|
|
|
|
|
delete[] buffer;
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool HDF5_File_Writer::WriteData(std::string dataSetName, float const* field_buf, size_t dim, size_t* datasize)
|
|
|
|
{
|
|
|
|
hid_t hdf5_file = H5Fopen( m_filename.c_str(), H5F_ACC_RDWR, H5P_DEFAULT );
|
|
|
|
if (hdf5_file<0)
|
|
|
|
{
|
|
|
|
cerr << "HDF5_File_Writer::WriteData: Error, opening the given file """ << m_filename << """ failed" << endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
hid_t group = OpenGroup(hdf5_file,m_Group);
|
2012-02-06 13:09:51 +00:00
|
|
|
if (group<0)
|
|
|
|
{
|
|
|
|
cerr << "HDF5_File_Writer::WriteData: Error opening group" << endl;
|
|
|
|
H5Fclose(hdf5_file);
|
|
|
|
return false;
|
|
|
|
}
|
2012-02-02 09:20:49 +00:00
|
|
|
|
|
|
|
hsize_t dims[dim];
|
|
|
|
for (size_t n=0;n<dim;++n)
|
|
|
|
dims[n]=datasize[n];
|
|
|
|
hid_t space = H5Screate_simple(dim, dims, NULL);
|
|
|
|
hid_t dataset = H5Dcreate(group, dataSetName.c_str(), H5T_NATIVE_FLOAT, space, H5P_DEFAULT);
|
|
|
|
if (H5Dwrite(dataset, H5T_NATIVE_FLOAT, space, H5P_DEFAULT, H5P_DEFAULT, field_buf))
|
|
|
|
{
|
|
|
|
cerr << "HDF5_File_Writer::WriteData: Error, writing to dataset failed" << endl;
|
2012-02-06 13:09:51 +00:00
|
|
|
H5Dclose(dataset);
|
|
|
|
H5Sclose(space);
|
|
|
|
H5Gclose(group);
|
|
|
|
H5Fclose(hdf5_file);
|
2012-02-02 09:20:49 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-02-06 13:09:51 +00:00
|
|
|
H5Dclose(dataset);
|
|
|
|
H5Sclose(space);
|
|
|
|
H5Gclose(group);
|
2012-02-02 09:20:49 +00:00
|
|
|
H5Fclose(hdf5_file);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HDF5_File_Writer::WriteAtrribute(std::string locName, std::string attr_name, float const* value, hsize_t size)
|
|
|
|
{
|
|
|
|
return WriteAtrribute(locName,attr_name, value, size, H5T_NATIVE_FLOAT);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HDF5_File_Writer::WriteAtrribute(std::string locName, std::string attr_name, void const* value, hsize_t size, hid_t mem_type)
|
|
|
|
{
|
|
|
|
hid_t hdf5_file = H5Fopen( m_filename.c_str(), H5F_ACC_RDWR, H5P_DEFAULT );
|
|
|
|
if (hdf5_file<0)
|
|
|
|
{
|
|
|
|
cerr << "HDF5_File_Writer::WriteAtrribute: Error, opening the given file """ << m_filename << """ failed" << endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (H5Lexists(hdf5_file, locName.c_str(), H5P_DEFAULT)<0)
|
|
|
|
{
|
|
|
|
cerr << "HDF5_File_Writer::WriteAtrribute: Error, failed to find location: """ << locName << """" << endl;
|
2012-02-06 13:09:51 +00:00
|
|
|
H5Fclose(hdf5_file);
|
2012-02-02 09:20:49 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
hid_t loc = H5Oopen(hdf5_file, locName.c_str(), H5P_DEFAULT);
|
|
|
|
if (loc<0)
|
|
|
|
{
|
|
|
|
cerr << "HDF5_File_Writer::WriteAtrribute: Error, failed to open location: """ << locName << """" << endl;
|
2012-02-06 13:09:51 +00:00
|
|
|
H5Fclose(hdf5_file);
|
2012-02-02 09:20:49 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
hid_t dataspace_id = H5Screate_simple(1, &size, NULL);
|
|
|
|
|
|
|
|
/* Create a dataset attribute. */
|
|
|
|
hid_t attribute_id = H5Acreate(loc, attr_name.c_str(), mem_type, dataspace_id,H5P_DEFAULT);
|
|
|
|
if (attribute_id<0)
|
|
|
|
{
|
|
|
|
cerr << "HDF5_File_Writer::WriteAtrribute: Error, failed to create the attrbute" << endl;
|
2012-02-06 13:09:51 +00:00
|
|
|
H5Sclose(dataspace_id);
|
|
|
|
H5Oclose(loc);
|
|
|
|
H5Fclose(hdf5_file);
|
2012-02-02 09:20:49 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write the attribute data. */
|
|
|
|
if (H5Awrite(attribute_id, mem_type, value)<0)
|
|
|
|
{
|
|
|
|
cerr << "HDF5_File_Writer::WriteAtrribute: Error, failed to write the attrbute" << endl;
|
2012-02-06 13:09:51 +00:00
|
|
|
H5Aclose(attribute_id);
|
|
|
|
H5Sclose(dataspace_id);
|
|
|
|
H5Oclose(loc);
|
|
|
|
H5Fclose(hdf5_file);
|
2012-02-02 09:20:49 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-02-06 13:09:51 +00:00
|
|
|
H5Aclose(attribute_id);
|
|
|
|
H5Sclose(dataspace_id);
|
|
|
|
H5Oclose(loc);
|
|
|
|
H5Fclose(hdf5_file);
|
2012-02-02 09:20:49 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HDF5_File_Writer::WriteAtrribute(std::string locName, std::string attr_name, vector<float> values)
|
|
|
|
{
|
|
|
|
float val[values.size()];
|
|
|
|
for (size_t n=0;n<values.size();++n)
|
|
|
|
val[n]=values.at(n);
|
|
|
|
return HDF5_File_Writer::WriteAtrribute(locName, attr_name,val,values.size());
|
|
|
|
}
|