00001 #include <cstdio>
00002 #include "psddl_hdf2psana/generic1d.h"
00003 #include "hdf5pp/ArrayType.h"
00004 #include "hdf5pp/CompoundType.h"
00005 #include "hdf5pp/Utils.h"
00006 #include "psddl_hdf2psana/Exceptions.h"
00007
00008 namespace {
00009 using namespace psddl_hdf2psana;
00010
00011
00012 typedef enum Psana::Generic1D::ConfigV0::Sample_Type Sample_Type;
00013
00014 std::vector<Sample_Type> getChannelTypeList(const Psana::Generic1D::DataV0 & obj) {
00015
00016 std::vector<Sample_Type> channelTypeList;
00017
00018 int32_t channel = -1;
00019 while (true) {
00020 channel += 1;
00021
00022 ndarray<const uint8_t, 1> arr_u8 = obj.data_u8((uint32_t)channel);
00023 if (not arr_u8.empty()) {
00024 channelTypeList.push_back(Psana::Generic1D::ConfigV0::UINT8);
00025 continue;
00026 }
00027 ndarray<const uint16_t, 1> arr_u16 = obj.data_u16((uint32_t)channel);
00028 if (not arr_u16.empty()) {
00029 channelTypeList.push_back(Psana::Generic1D::ConfigV0::UINT16);
00030 continue;
00031 }
00032 ndarray<const uint32_t, 1> arr_u32 = obj.data_u32((uint32_t)channel);
00033 if (not arr_u32.empty()) {
00034 channelTypeList.push_back(Psana::Generic1D::ConfigV0::UINT32);
00035 continue;
00036 }
00037 ndarray<const float, 1> arr_float = obj.data_f32((uint32_t)channel);
00038 if (not arr_float.empty()) {
00039 channelTypeList.push_back(Psana::Generic1D::ConfigV0::FLOAT32);
00040 continue;
00041 }
00042 ndarray<const double, 1> arr_double = obj.data_f64((uint32_t)channel);
00043 if (not arr_double.empty()) {
00044 channelTypeList.push_back(Psana::Generic1D::ConfigV0::FLOAT64);
00045 continue;
00046 }
00047 break;
00048 };
00049 return channelTypeList;
00050 }
00051
00052 std::string channelDatasetName(uint32_t channel) {
00053 static char datasetName[128];
00054 sprintf(datasetName, "channel:%2.2u", channel);
00055 return std::string(datasetName);
00056 }
00057
00058 template<class T>
00059 void createChannelDataset(ndarray<const T, 1> arr, uint32_t channel,
00060 hdf5pp::Group group, const ChunkPolicy &chunkPolicy,
00061 int deflate, bool shuffle)
00062 {
00063 hdf5pp::Type dstype = hdf5pp::ArrayType::arrayType(hdf5pp::TypeTraits<T>::stored_type(), arr.shape()[0]);
00064 hdf5pp::Utils::createDataset(group, channelDatasetName(channel), dstype, chunkPolicy.chunkSize(dstype), chunkPolicy.chunkCacheSize(dstype), deflate, shuffle);
00065 }
00066
00067 void storeChannelsDatasetsAt(hdf5pp::Group group,
00068 const Psana::Generic1D::DataV0 & obj,
00069 long index) {
00070 std::vector<Sample_Type> channelTypeList = getChannelTypeList(obj);
00071 for (uint32_t channel = 0; channel < channelTypeList.size(); ++channel) {
00072 switch (channelTypeList[channel]) {
00073 case Psana::Generic1D::ConfigV0::UINT8:
00074 hdf5pp::Utils::storeNDArrayAt(group, channelDatasetName(channel), obj.data_u8(channel), index);
00075 break;
00076 case Psana::Generic1D::ConfigV0::UINT16:
00077 hdf5pp::Utils::storeNDArrayAt(group, channelDatasetName(channel), obj.data_u16(channel), index);
00078 break;
00079 case Psana::Generic1D::ConfigV0::UINT32:
00080 hdf5pp::Utils::storeNDArrayAt(group, channelDatasetName(channel), obj.data_u32(channel), index);
00081 break;
00082 case Psana::Generic1D::ConfigV0::FLOAT32:
00083 hdf5pp::Utils::storeNDArrayAt(group, channelDatasetName(channel), obj.data_f32(channel), index);
00084 break;
00085 case Psana::Generic1D::ConfigV0::FLOAT64:
00086 hdf5pp::Utils::storeNDArrayAt(group, channelDatasetName(channel), obj.data_f64(channel), index);
00087 break;
00088 }
00089 }
00090 }
00091
00092 void resizeChannelsDatasets(hdf5pp::Group group,
00093 long index) {
00094 uint32_t channel = 0;
00095 while (group.hasChild(channelDatasetName(channel))) {
00096 hdf5pp::Utils::resizeDataset(group, channelDatasetName(channel), index < 0 ? index : index + 1);
00097 }
00098 }
00099
00100 void storeChannelsDatasets(hdf5pp::Group group,
00101 const Psana::Generic1D::DataV0 & obj) {
00102 std::vector<Sample_Type> channelTypeList = getChannelTypeList(obj);
00103 for (uint32_t channel = 0; channel < channelTypeList.size(); ++channel) {
00104 switch (channelTypeList[channel]) {
00105 case Psana::Generic1D::ConfigV0::UINT8:
00106 hdf5pp::Utils::storeNDArray(group, channelDatasetName(channel), obj.data_u8(channel));
00107 break;
00108 case Psana::Generic1D::ConfigV0::UINT16:
00109 hdf5pp::Utils::storeNDArray(group, channelDatasetName(channel), obj.data_u16(channel));
00110 break;
00111 case Psana::Generic1D::ConfigV0::UINT32:
00112 hdf5pp::Utils::storeNDArray(group, channelDatasetName(channel), obj.data_u32(channel));
00113 break;
00114 case Psana::Generic1D::ConfigV0::FLOAT32:
00115 hdf5pp::Utils::storeNDArray(group, channelDatasetName(channel), obj.data_f32(channel));
00116 break;
00117 case Psana::Generic1D::ConfigV0::FLOAT64:
00118 hdf5pp::Utils::storeNDArray(group, channelDatasetName(channel), obj.data_f64(channel));
00119 break;
00120 }
00121 }
00122 }
00123
00124 }
00125
00126
00127 namespace psddl_hdf2psana {
00128 namespace Generic1D {
00129
00130 template <typename Config>
00131 bool DataV0_v0<Config>::doesnt_store_type(uint32_t channel, enum Config::Sample_Type sample_type) const {
00132 if (channel > m_cfg->NChannels()) return true;
00133 if (m_cfg->SampleType()[channel] != sample_type) return true;
00134 return false;
00135 }
00136
00137 template <typename Config>
00138 ndarray<const uint8_t, 1> DataV0_v0<Config>::data_u8(uint32_t channel) const {
00139 if (doesnt_store_type(channel, Config::UINT8)) return ndarray<const uint8_t, 1>();
00140 if (m_data_u8.find(channel) == m_data_u8.end()) read_data_u8(channel);
00141 return m_data_u8[channel];
00142 }
00143
00144 template <typename Config>
00145 ndarray<const uint16_t, 1> DataV0_v0<Config>::data_u16(uint32_t channel) const {
00146 if (doesnt_store_type(channel, Config::UINT16)) return ndarray<const uint16_t, 1>();
00147 if (m_data_u16.find(channel) == m_data_u16.end()) read_data_u16(channel);
00148 return m_data_u16[channel];
00149 }
00150
00151 template <typename Config>
00152 ndarray<const uint32_t, 1> DataV0_v0<Config>::data_u32(uint32_t channel) const {
00153 if (doesnt_store_type(channel, Config::UINT32)) return ndarray<const uint32_t, 1>();
00154 if (m_data_u32.find(channel) == m_data_u32.end()) read_data_u32(channel);
00155 return m_data_u32[channel];
00156 }
00157
00158 template <typename Config>
00159 ndarray<const float, 1> DataV0_v0<Config>::data_f32(uint32_t channel) const {
00160 if (doesnt_store_type(channel, Config::FLOAT32)) return ndarray<const float, 1>();
00161 if (m_data_f32.find(channel) == m_data_f32.end()) read_data_f32(channel);
00162 return m_data_f32[channel];
00163 }
00164
00165 template <typename Config>
00166 ndarray<const double, 1> DataV0_v0<Config>::data_f64(uint32_t channel) const {
00167 if (doesnt_store_type(channel, Config::FLOAT64)) return ndarray<const double, 1>();
00168 if (m_data_f64.find(channel) == m_data_f64.end()) read_data_f64(channel);
00169 return m_data_f64[channel];
00170 }
00171
00172 template <typename Config>
00173 void DataV0_v0<Config>::read_data_u8(uint32_t channel) const {
00174 m_data_u8[channel] = hdf5pp::Utils::readNdarray<uint8_t, 1>(m_group, channelDatasetName(channel), m_idx);
00175 }
00176
00177 template <typename Config>
00178 void DataV0_v0<Config>::read_data_u16(uint32_t channel) const {
00179 m_data_u16[channel] = hdf5pp::Utils::readNdarray<uint16_t, 1>(m_group, channelDatasetName(channel), m_idx);
00180 }
00181
00182 template <typename Config>
00183 void DataV0_v0<Config>::read_data_u32(uint32_t channel) const {
00184 m_data_u32[channel] = hdf5pp::Utils::readNdarray<uint32_t, 1>(m_group, channelDatasetName(channel), m_idx);
00185 }
00186
00187 template <typename Config>
00188 void DataV0_v0<Config>::read_data_f32(uint32_t channel) const {
00189 m_data_f32[channel] = hdf5pp::Utils::readNdarray<float, 1>(m_group, channelDatasetName(channel), m_idx);
00190 }
00191
00192 template <typename Config>
00193 void DataV0_v0<Config>::read_data_f64(uint32_t channel) const {
00194 m_data_f64[channel] = hdf5pp::Utils::readNdarray<double, 1>(m_group, channelDatasetName(channel), m_idx);
00195 }
00196
00197
00198
00199 template class DataV0_v0<Psana::Generic1D::ConfigV0>;
00200
00201
00202 void make_datasets_DataV0_v0(const Psana::Generic1D::DataV0& obj,
00203 hdf5pp::Group group,
00204 const ChunkPolicy& chunkPolicy, int deflate, bool shuffle)
00205 {
00206 std::vector<Sample_Type> channelTypeList = getChannelTypeList(obj);
00207 for (uint32_t channel = 0; channel < channelTypeList.size(); ++channel) {
00208 switch (channelTypeList[channel]) {
00209 case Psana::Generic1D::ConfigV0::UINT8:
00210 createChannelDataset<uint8_t>(obj.data_u8(channel), (uint32_t)channel, group, chunkPolicy, deflate, shuffle);
00211 break;
00212 case Psana::Generic1D::ConfigV0::UINT16:
00213 createChannelDataset<uint16_t>(obj.data_u16(channel), (uint32_t)channel, group, chunkPolicy, deflate, shuffle);
00214 break;
00215 case Psana::Generic1D::ConfigV0::UINT32:
00216 createChannelDataset<uint32_t>(obj.data_u32(channel), (uint32_t)channel, group, chunkPolicy, deflate, shuffle);
00217 break;
00218 case Psana::Generic1D::ConfigV0::FLOAT32:
00219 createChannelDataset<float>(obj.data_f32(channel), (uint32_t)channel, group, chunkPolicy, deflate, shuffle);
00220 break;
00221 case Psana::Generic1D::ConfigV0::FLOAT64:
00222 createChannelDataset<double>(obj.data_f64(channel), (uint32_t)channel, group, chunkPolicy, deflate, shuffle);
00223 break;
00224 }
00225 }
00226 }
00227
00228
00229 void store_DataV0_v0(const Psana::Generic1D::DataV0* obj, hdf5pp::Group group, long index, bool append)
00230 {
00231 if (append) {
00232 if (obj) {
00233 storeChannelsDatasetsAt(group, *obj, index);
00234 } else {
00235 resizeChannelsDatasets(group, index);
00236 }
00237 } else {
00238 storeChannelsDatasets(group, *obj);
00239 }
00240 }
00241
00242 }
00243 }