psddl_hdf2psana/src/pnccd.cpp

Go to the documentation of this file.
00001 //--------------------------------------------------------------------------
00002 // File and Version Information:
00003 //      $Id: pnccd.cpp 7035 2013-10-31 19:57:40Z salnikov@SLAC.STANFORD.EDU $
00004 //
00005 // Description:
00006 //      Hand-written supporting types for DDL-HDF5 mapping.
00007 //
00008 //------------------------------------------------------------------------
00009 
00010 //-----------------------
00011 // This Class's Header --
00012 //-----------------------
00013 #include "psddl_hdf2psana/pnccd.h"
00014 
00015 //-----------------
00016 // C/C++ Headers --
00017 //-----------------
00018 #include <iterator>
00019 
00020 //-------------------------------
00021 // Collaborating Class Headers --
00022 //-------------------------------
00023 #include "hdf5pp/ArrayType.h"
00024 #include "hdf5pp/CompoundType.h"
00025 #include "hdf5pp/EnumType.h"
00026 #include "hdf5pp/VlenType.h"
00027 #include "hdf5pp/Utils.h"
00028 #include "MsgLogger/MsgLogger.h"
00029 
00030 //-----------------------------------------------------------------------
00031 // Local Macros, Typedefs, Structures, Unions and Forward Declarations --
00032 //-----------------------------------------------------------------------
00033 
00034 namespace {
00035 
00036   struct Compare_ts {
00037     bool operator()(const psddl_hdf2psana::PNCCD::ns_FrameV1_v0::dataset_data& lhs,
00038         const psddl_hdf2psana::PNCCD::ns_FrameV1_v0::dataset_data& rhs) const
00039     {
00040       if (lhs.timeStampHi < rhs.timeStampHi) return true;
00041       if (lhs.timeStampHi > rhs.timeStampHi) return false;
00042       if (lhs.timeStampLo < rhs.timeStampLo) return true;
00043       return false;
00044     }
00045   };
00046 
00047 }
00048 
00049 //              ----------------------------------------
00050 //              -- Public Function Member Definitions --
00051 //              ----------------------------------------
00052 
00053 
00054 namespace psddl_hdf2psana {
00055 namespace PNCCD {
00056 
00057 
00058 hdf5pp::Type ns_FrameV1_v0_dataset_data_stored_type()
00059 {
00060   typedef ns_FrameV1_v0::dataset_data DsType;
00061   hdf5pp::CompoundType type = hdf5pp::CompoundType::compoundType<DsType>();
00062   type.insert("specialWord", offsetof(DsType, specialWord), hdf5pp::TypeTraits<uint32_t>::stored_type());
00063   type.insert("frameNumber", offsetof(DsType, frameNumber), hdf5pp::TypeTraits<uint32_t>::stored_type());
00064   type.insert("timeStampHi", offsetof(DsType, timeStampHi), hdf5pp::TypeTraits<uint32_t>::stored_type());
00065   type.insert("timeStampLo", offsetof(DsType, timeStampLo), hdf5pp::TypeTraits<uint32_t>::stored_type());
00066   return type;
00067 }
00068 
00069 hdf5pp::Type ns_FrameV1_v0::dataset_data::stored_type()
00070 {
00071   static hdf5pp::Type type = ns_FrameV1_v0_dataset_data_stored_type();
00072   return type;
00073 }
00074 
00075 hdf5pp::Type ns_FrameV1_v0_dataset_data_native_type()
00076 {
00077   typedef ns_FrameV1_v0::dataset_data DsType;
00078   hdf5pp::CompoundType type = hdf5pp::CompoundType::compoundType<DsType>();
00079   type.insert("specialWord", offsetof(DsType, specialWord), hdf5pp::TypeTraits<uint32_t>::native_type());
00080   type.insert("frameNumber", offsetof(DsType, frameNumber), hdf5pp::TypeTraits<uint32_t>::native_type());
00081   type.insert("timeStampHi", offsetof(DsType, timeStampHi), hdf5pp::TypeTraits<uint32_t>::native_type());
00082   type.insert("timeStampLo", offsetof(DsType, timeStampLo), hdf5pp::TypeTraits<uint32_t>::native_type());
00083   return type;
00084 }
00085 
00086 hdf5pp::Type ns_FrameV1_v0::dataset_data::native_type()
00087 {
00088   static hdf5pp::Type type = ns_FrameV1_v0_dataset_data_native_type();
00089   return type;
00090 }
00091 
00092 ns_FrameV1_v0::dataset_data::dataset_data(const Psana::PNCCD::FrameV1& psanaobj)
00093   : specialWord(psanaobj.specialWord())
00094   , frameNumber(psanaobj.frameNumber())
00095   , timeStampHi(psanaobj.timeStampHi())
00096   , timeStampLo(psanaobj.timeStampLo())
00097 {
00098 }
00099 
00100 uint32_t
00101 FrameV1_v0::specialWord() const {
00102   return uint32_t(m_ds_data.specialWord);
00103 }
00104 
00105 uint32_t
00106 FrameV1_v0::frameNumber() const {
00107   return uint32_t(m_ds_data.frameNumber);
00108 }
00109 
00110 uint32_t
00111 FrameV1_v0::timeStampHi() const {
00112   return uint32_t(m_ds_data.timeStampHi);
00113 }
00114 
00115 uint32_t
00116 FrameV1_v0::timeStampLo() const {
00117   return uint32_t(m_ds_data.timeStampLo);
00118 }
00119 
00120 ndarray<const uint16_t, 2>
00121 FrameV1_v0::data() const {
00122   return make_ndarray(m_ds_frameData.data_ptr(), 512, 512);
00123 }
00124 
00125 ndarray<const uint16_t, 1>
00126 FrameV1_v0::_data() const
00127 {
00128   return m_ds_frameData;
00129 }
00130 
00131 /** Number of frames is determined by numLinks() method. */
00132 template <typename Config>
00133 const Psana::PNCCD::FrameV1&
00134 FramesV1_v0<Config>::frame(uint32_t i0) const
00135 {
00136   if (m_frames.empty()) read_frames();
00137   return m_frames[i0];
00138 }
00139 
00140 /** Method which returns the shape (dimensions) of the data returned by frame() method. */
00141 template <typename Config>
00142 std::vector<int>
00143 FramesV1_v0<Config>::frame_shape() const
00144 {
00145   if (m_frames.empty()) read_frames();
00146   return std::vector<int>(1, m_frames.shape()[0]);
00147 }
00148 
00149 template <typename Config>
00150 void
00151 FramesV1_v0<Config>::read_frames() const
00152 {
00153   ndarray<const ns_FrameV1_v0::dataset_data, 1> mdata = hdf5pp::Utils::readNdarray<ns_FrameV1_v0::dataset_data, 1>(m_group, "frame", m_idx);
00154   ndarray<const uint16_t, 2> frdata = hdf5pp::Utils::readNdarray<uint16_t, 2>(m_group, "data", m_idx);
00155 
00156   unsigned const nlinks = mdata.shape()[0];
00157 
00158   m_frames = make_ndarray<FrameV1_v0>(nlinks);
00159   for (unsigned int i = 0; i != nlinks; ++ i) {
00160     m_frames[i] = FrameV1_v0(mdata[i], frdata[i]);
00161   }
00162 }
00163 
00164 template class FramesV1_v0<Psana::PNCCD::ConfigV1>;
00165 template class FramesV1_v0<Psana::PNCCD::ConfigV2>;
00166 
00167 void make_datasets_FramesV1_v0(const Psana::PNCCD::FramesV1& obj,
00168       hdf5pp::Group group, const ChunkPolicy& chunkPolicy, int deflate, bool shuffle)
00169 {
00170   const unsigned nLinks = obj.numLinks();
00171   const unsigned data_size = obj.frame(0)._data().shape()[0];
00172   {
00173     hdf5pp::Type dstype = hdf5pp::ArrayType::arrayType(hdf5pp::TypeTraits<ns_FrameV1_v0::dataset_data>::stored_type(), nLinks);
00174     hdf5pp::Utils::createDataset(group, "frame", dstype, chunkPolicy.chunkSize(dstype), chunkPolicy.chunkCacheSize(dstype), deflate, shuffle);
00175   }
00176   {
00177     hsize_t dims[2] = {nLinks, data_size};
00178     hdf5pp::Type dstype = hdf5pp::ArrayType::arrayType(hdf5pp::TypeTraits<uint16_t>::stored_type(), 2, dims);
00179     hdf5pp::Utils::createDataset(group, "data", dstype, chunkPolicy.chunkSize(dstype), chunkPolicy.chunkCacheSize(dstype), deflate, shuffle);
00180   }
00181 }
00182 
00183 void store_FramesV1_v0(const Psana::PNCCD::FramesV1* obj, hdf5pp::Group group, long index, bool append)
00184 {
00185   if (not obj) {
00186     if (append) {
00187       hdf5pp::Utils::resizeDataset(group, "frame", index < 0 ? index : index + 1);
00188       hdf5pp::Utils::resizeDataset(group, "data", index < 0 ? index : index + 1);
00189     }
00190     return;
00191   }
00192 
00193   unsigned nLinks = obj->numLinks();
00194   ndarray<ns_FrameV1_v0::dataset_data, 1> frames_ds = make_ndarray<ns_FrameV1_v0::dataset_data>(nLinks);
00195   for (unsigned i = 0; i != nLinks; ++ i) {
00196     frames_ds[i] = ns_FrameV1_v0::dataset_data(obj->frame(i));
00197   }
00198 
00199   // need to make 2-d array, high dimension is the number of links,
00200   // low dimension is the size of data. We do not store images as images,
00201   // but as 1-d data
00202   unsigned data_size = obj->frame(0)._data().shape()[0];
00203   ndarray<uint16_t, 2> data = make_ndarray<uint16_t>(nLinks, data_size);
00204 
00205   // copy the data
00206   for (unsigned i = 0; i != nLinks; ++ i) {
00207     ndarray<const uint16_t, 1> small = obj->frame(i)._data();
00208     std::copy(small.begin(), small.end(), &data[i][0]);
00209   }
00210 
00211   if (append) {
00212     hdf5pp::Utils::storeNDArrayAt(group, "frame", frames_ds, index);
00213     hdf5pp::Utils::storeNDArrayAt(group, "data", data, index);
00214   } else {
00215     hdf5pp::Utils::storeNDArray(group, "frame", frames_ds);
00216     hdf5pp::Utils::storeNDArray(group, "data", data);
00217   }
00218 
00219 }
00220 
00221 
00222 /** Special values */
00223 uint32_t
00224 FullFrameV1_v0::specialWord() const
00225 {
00226   if (m_frame.empty()) read_frame();
00227   return m_specialWord;
00228 }
00229 
00230 /** Frame number */
00231 uint32_t
00232 FullFrameV1_v0::frameNumber() const
00233 {
00234   if (m_frame.empty()) read_frame();
00235   return m_frameNumber;
00236 }
00237 
00238 /** Most significant part of timestamp */
00239 uint32_t
00240 FullFrameV1_v0::timeStampHi() const
00241 {
00242   if (m_frame.empty()) read_frame();
00243   return m_timeStampHi;
00244 }
00245 
00246 /** Least significant part of timestamp */
00247 uint32_t
00248 FullFrameV1_v0::timeStampLo() const
00249 {
00250   if (m_frame.empty()) read_frame();
00251   return m_timeStampLo;
00252 }
00253 
00254 /** Full frame data, image size is 1024x1024. */
00255 ndarray<const uint16_t, 2>
00256 FullFrameV1_v0::data() const
00257 {
00258   if (m_frame.empty()) read_frame();
00259   return m_frame;
00260 }
00261 
00262 void
00263 FullFrameV1_v0::read_frame() const
00264 {
00265   ndarray<const ns_FrameV1_v0::dataset_data, 1> mdata = hdf5pp::Utils::readNdarray<ns_FrameV1_v0::dataset_data, 1>(m_group, "frame", m_idx);
00266   ndarray<const uint16_t, 2> frdata = hdf5pp::Utils::readNdarray<uint16_t, 2>(m_group, "data", m_idx);
00267 
00268   unsigned const nlinks = mdata.shape()[0];
00269   if (nlinks != 4) {
00270     MsgLog("FullFrameV1_v0", error, "read_frame: number of links in PNCCD::FrameV1 is not equal 4: " << nlinks);
00271     return;
00272   }
00273 
00274   // copy metadata from the first frame, they all should be identical
00275   m_specialWord = mdata[0].specialWord;
00276   m_frameNumber = mdata[0].frameNumber;
00277 
00278   // take the lowest timestamp of four frames
00279   ndarray<const ns_FrameV1_v0::dataset_data, 1>::iterator it = std::min_element(mdata.begin(), mdata.end(), ::Compare_ts());
00280   m_timeStampHi = it->timeStampHi;
00281   m_timeStampLo = it->timeStampLo;
00282 
00283   // make large image out of four small images
00284   m_frame = make_ndarray<uint16_t>(1024, 1024);
00285   uint16_t* dest = &m_frame[0][0];
00286   const uint16_t* src0 = &frdata[0][0];
00287   const uint16_t* src3 = &frdata[3][0];
00288   for (int iY = 0; iY < 512; ++ iY, src0 += 512, src3 += 512) {
00289     dest = std::copy(src0, src0+512, dest);
00290     dest = std::copy(src3, src3+512, dest);
00291   }
00292 
00293   typedef std::reverse_iterator<const uint16_t*> RevIter;
00294   RevIter src1(&frdata[1][512*512]);
00295   RevIter src2(&frdata[2][512*512]);
00296   for (int iY = 0; iY < 512; ++ iY, src1 += 512, src2 += 512) {
00297     dest = std::copy(src1, src1+512, dest);
00298     dest = std::copy(src2, src2+512, dest);
00299   }
00300 }
00301 
00302 void make_datasets_FullFrameV1_v0(const Psana::PNCCD::FullFrameV1& obj,
00303       hdf5pp::Group group, const ChunkPolicy& chunkPolicy, int deflate, bool shuffle)
00304 {
00305   // we do not want to save FullFrame in HDF5
00306   MsgLog("PNCCD::make_datasets_FullFrameV1_v0", error, "type is not supported");
00307 }
00308 
00309 void store_FullFrameV1_v0(const Psana::PNCCD::FullFrameV1* obj, hdf5pp::Group group, long index, bool append)
00310 {
00311   // we do not want to save FullFrame in HDF5
00312   MsgLog("PNCCD::store_FullFrameV1_v0", error, "type is not supported");
00313 }
00314 
00315 } // namespace PNCCD
00316 } // namespace psddl_hdf2psana

Generated on 19 Dec 2016 for PSDMSoftware by  doxygen 1.4.7