polaroid-pp

Schlieren and contour plot tool
git clone https://git.0xfab.ch/polaroid-pp.git
Log | Files | Refs | Submodules | README | LICENSE

PhotoHDF5.cpp (4185B)


      1 // File       : PhotoHDF5.cpp
      2 // Date       : Wed Apr 27 07:52:09 2016
      3 // Author     : Fabian Wermelinger
      4 // Description: HDF5 Photo Implementation
      5 // Copyright 2016 ETH Zurich. All Rights Reserved.
      6 #ifdef _USE_HDF_
      7 #include <cassert>
      8 #include <cstdio>
      9 #include "PhotoHDF5.h"
     10 
     11 using namespace std;
     12 
     13 void PhotoHDF5::_open_hdf_file()
     14 {
     15     hsize_t tmp[4] = { 1, static_cast<hsize_t>(m_height), static_cast<hsize_t>(m_width), 1 };
     16     for (int i = 0; i < 4; ++i)
     17     {
     18         m_hdf5_count[i] = tmp[i];
     19         m_hdf5_dims[i]  = tmp[i];
     20         m_hdf5_offset[i]= 0;
     21     }
     22     H5open();
     23     hid_t fapl_id = H5Pcreate(H5P_FILE_ACCESS);
     24     m_hdf5_fileid = H5Fcreate((m_fname+this->suffix()).c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, fapl_id);
     25     H5Pclose(fapl_id);
     26 }
     27 
     28 void PhotoHDF5::_close_hdf_file()
     29 {
     30     H5Fclose(m_hdf5_fileid);
     31     H5close();
     32 }
     33 
     34 void PhotoHDF5::_dump_xmf() const
     35 {
     36     FILE *xmf = 0;
     37     xmf = fopen((m_fname+".xmf").c_str(), "w");
     38     fprintf(xmf, "<?xml version=\"1.0\" ?>\n");
     39     fprintf(xmf, "<!DOCTYPE Xdmf SYSTEM \"Xdmf.dtd\" []>\n");
     40     fprintf(xmf, "<Xdmf Version=\"2.0\">\n");
     41     fprintf(xmf, " <Domain>\n");
     42     fprintf(xmf, "   <Grid GridType=\"Uniform\">\n");
     43     fprintf(xmf, "     <Time Value=\"%e\"/>\n", m_time);
     44     fprintf(xmf, "     <Topology TopologyType=\"3DCORECTMesh\" Dimensions=\"%d %d %d\"/>\n", (int)m_hdf5_dims[0], (int)m_hdf5_dims[1], (int)m_hdf5_dims[2]);
     45     fprintf(xmf, "     <Geometry GeometryType=\"ORIGIN_DXDYDZ\">\n");
     46     fprintf(xmf, "       <DataItem Name=\"Origin\" Dimensions=\"3\" NumberType=\"Float\" Precision=\"4\" Format=\"XML\">\n");
     47     fprintf(xmf, "        %e %e %e\n", 0.,0.,0.);
     48     fprintf(xmf, "       </DataItem>\n");
     49     fprintf(xmf, "       <DataItem Name=\"Spacing\" Dimensions=\"3\" NumberType=\"Float\" Precision=\"4\" Format=\"XML\">\n");
     50     fprintf(xmf, "        %e %e %e\n", 1./(Real)m_hdf5_dims[0],1./(Real)m_hdf5_dims[0],1./(Real)m_hdf5_dims[0]);
     51     fprintf(xmf, "       </DataItem>\n");
     52     fprintf(xmf, "     </Geometry>\n");
     53     fprintf(xmf, "     <Attribute Name=\"%s\" AttributeType=\"%s\" Center=\"Node\">\n", m_description.c_str(), "Scalar");
     54     fprintf(xmf, "       <DataItem Dimensions=\"%d %d %d %d\" NumberType=\"Float\" Precision=\"4\" Format=\"HDF\">\n", (int)m_hdf5_dims[0], (int)m_hdf5_dims[1], (int)m_hdf5_dims[2], (int)m_hdf5_dims[3]);
     55     fprintf(xmf, "        %s:/%s\n",(m_fname+".h5").c_str(), m_description.c_str());
     56     fprintf(xmf, "       </DataItem>\n");
     57     fprintf(xmf, "     </Attribute>\n");
     58     fprintf(xmf, "   </Grid>\n");
     59     fprintf(xmf, " </Domain>\n");
     60     fprintf(xmf, "</Xdmf>\n");
     61     fclose(xmf);
     62 }
     63 
     64 void PhotoHDF5::make_new(const string name, const int width, const int height)
     65 {
     66     m_fname = name;
     67     resize(width, height);
     68 }
     69 
     70 void PhotoHDF5::resize(const int width, const int height)
     71 {
     72     if (m_open)
     73     {
     74         _close_hdf_file();
     75         if (m_hdfraw) delete [] m_hdfraw;
     76     }
     77     m_hdfraw = new Real[width*height];
     78 
     79     m_width = width;
     80     m_height= height;
     81 
     82     _open_hdf_file();
     83     m_open = true;
     84 }
     85 
     86 
     87 void PhotoHDF5::write()
     88 {
     89     if (m_open)
     90     {
     91         hid_t fapl_id = H5Pcreate(H5P_DATASET_XFER);
     92         hid_t fspace_id = H5Screate_simple(4, m_hdf5_dims, NULL);
     93         hid_t dataset_id = H5Dcreate2(m_hdf5_fileid, m_description.c_str(), HDF_PRECISION, fspace_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
     94         fspace_id = H5Dget_space(dataset_id);
     95         H5Sselect_hyperslab(fspace_id, H5S_SELECT_SET, m_hdf5_offset, NULL, m_hdf5_count, NULL);
     96         hid_t mspace_id = H5Screate_simple(4, m_hdf5_count, NULL);
     97         H5Dwrite(dataset_id, HDF_PRECISION, mspace_id, fspace_id, fapl_id, m_hdfraw);
     98 
     99         H5Sclose(mspace_id);
    100         H5Sclose(fspace_id);
    101         H5Dclose(dataset_id);
    102         H5Pclose(fapl_id);
    103 
    104         _close_hdf_file();
    105 
    106         _dump_xmf();
    107 
    108         delete [] m_hdfraw;
    109         m_hdfraw = nullptr;
    110         m_open = false;
    111     }
    112 }
    113 
    114 void PhotoHDF5::set_pixel(const double phi, const int ix, const int iy)
    115 {
    116     if (m_open)
    117     {
    118         assert(ix >= 0); assert(ix < m_width);
    119         assert(iy >= 0); assert(iy < m_height);
    120         m_hdfraw[iy*m_width + ix] = static_cast<Real>(phi);
    121     }
    122 }
    123 #endif /* _USE_HDF_ */