Commit f8846673 authored by Purves, Murray's avatar Purves, Murray
Browse files

WIP Added SPE writer

parent a1cd5126
Pipeline #16152 failed with stages
in 8 minutes and 5 seconds
......@@ -227,12 +227,12 @@ void Spectrum::spectrumData(int i, std::string &title, std::string &source,
float &energyCalCubicTerm,
float &energyCalLowEnergyTerm, float &occupancyFlag,
float &totalNeutronCount, int &numberOfChannels,
std::vector<float> countsByChannel)
std::vector<float> &countsByChannel)
{
if (i > 0 && i < mSpectrumData.size())
{
size_t i_ = size_t(i);
SpectrumData spectrum = mSpectrumData[i];
SpectrumData spectrum = mSpectrumData[i_];
title = spectrum.title;
source = spectrum.source;
description = spectrum.description;
......@@ -248,7 +248,11 @@ void Spectrum::spectrumData(int i, std::string &title, std::string &source,
occupancyFlag = spectrum.occupancyFlag;
totalNeutronCount = spectrum.totalNeutronCount;
numberOfChannels = spectrum.numberOfChannels;
countsByChannel = spectrum.countsByChannel;
countsByChannel.clear();
for (size_t j = 0; j < spectrum.countsByChannel.size(); ++j)
{
countsByChannel.push_back(spectrum.countsByChannel[j]);
}
}
}
......@@ -276,7 +280,10 @@ void Spectrum::addSpectrumData(
spectrumData.occupancyFlag = occupancyFlag;
spectrumData.totalNeutronCount = totalNeutronCount;
spectrumData.numberOfChannels = numberOfChannels;
spectrumData.countsByChannel = countsByChannel;
for (size_t i = 0; i < countsByChannel.size(); ++i)
{
spectrumData.countsByChannel.push_back(countsByChannel[i]);
}
mSpectrumData.push_back(spectrumData);
}
}
......@@ -95,7 +95,7 @@ class RADIX_PUBLIC Spectrum
float &energyCalQuadraticTerm, float &energyCalCubicTerm,
float &energyCalLowEnergyTerm, float &occupancyFlag,
float &totalNeutronCount, int &numberOfChannels,
std::vector<float> countsByChannel);
std::vector<float> &countsByChannel);
void addSpectrumData(std::string title, std::string source,
std::string description, std::string dateTime,
std::string tag, float liveTime, float totalTime,
......
#ifndef RADIX_RADIXIO_SPECTRUMSPESTREAM_HH_
#define RADIX_RADIXIO_SPECTRUMSPESTREAM_HH_
#include <cstdio>
#include <fstream>
#include <string>
......@@ -34,7 +35,6 @@ bool SpectrumSPEStream<data_type>::read_from(const std::string &file)
// Read through the file
std::string line;
std::vector<std::string> parts;
while (std::getline(stream, line))
{
// Each time we see "$SPEC_ID:" we have a new set of spectrum data
......@@ -64,35 +64,46 @@ bool SpectrumSPEStream<data_type>::read_from(const std::string &file)
{
if (trim_string(line).compare("$MEAS_TIM:") == 0)
{
radix_line(" reading MEAS_TIM section");
readMeasTim(stream, liveTime, totalTime);
}
else if (trim_string(line).compare("$DATE_MEA:") == 0)
{
radix_line(" reading DATE_MEA section");
// Read date/time
readDateMea(stream, dateTime);
}
else if (trim_string(line).compare("$DATA:") == 0)
{
radix_line(" reading DATA section");
// Read data
readData(stream, firstChannel, totalEntries, countsByChannel);
radix_line(" DATA section read:");
radix_line(" first channel: " << firstChannel);
radix_line(" total entries: " << totalEntries);
radix_line(" size of counts vector: " << countsByChannel.size());
}
else if (trim_string(line).compare("$ROI:") == 0)
{
radix_line(" reading ROI section");
// Read ROI information
readRoi(stream, roi);
}
else if (trim_string(line).compare("$PRESETS:") == 0)
{
radix_line(" reading PRESETS section");
// Read preset lines
readPresets(stream, presetLines);
}
else if (trim_string(line).compare("$ENER_FIT:") == 0)
{
radix_line(" reading ENER_FIT section");
// Read the energy fit
readEnerFit(stream, totalEntries, energyCalGain, energyCalOffset);
}
else if (trim_string(line).compare("$MCA_CAL:") == 0)
{
radix_line(" reading MCA_CAL section");
// Read MCA calibration lines
readMcaCal(stream, calInt, mcaCal);
}
......@@ -118,6 +129,109 @@ bool SpectrumSPEStream<data_type>::write_to(const std::string &file) const
{
bool result = false;
radix_line("Writing data to SPE file: " << file);
// Open file stream to write
FILE *stream = nullptr;
stream = fopen(file.c_str(), "w");
if (!stream)
{
radix_line("Failed to open file - returning false");
return result;
}
// Spectrum data values
std::string title, source, description, dateTime, tag;
float liveTime, totalTime, energyCalOffset, energyCalGain,
energyCalQuadraticTerm, energyCalCubicTerm, energyCalLowEnergyTerm,
occupancyFlag, totalNeutronCount;
int numberOfChannels;
std::vector<float> countsByChannel;
// Write the SPE file
for (int i = 0; i < mData->spectrumDataCount(); ++i)
{
// Reset the error flag while we write a new spectrum
result = false;
radix_line(" Retrieving data for SpectrumData "
<< i + 1 << " of " << mData->spectrumDataCount());
// Get the spectrum
mData->spectrumData(i, title, source, description, dateTime, tag, liveTime,
totalTime, energyCalOffset, energyCalGain,
energyCalQuadraticTerm, energyCalCubicTerm,
energyCalLowEnergyTerm, occupancyFlag,
totalNeutronCount, numberOfChannels, countsByChannel);
radix_line(" Spectrum retrieved:");
radix_line(" title: " << title);
radix_line(" number of channels: " << numberOfChannels);
radix_line(" size of counts vector: " << countsByChannel.size());
// Write ID section
radix_line(" Writing ID section");
fprintf(stream, "$SPEC_ID:\n");
fprintf(stream, "%s\n", title.c_str());
// Write measurement time
radix_line(" Writing measurement time section");
fprintf(stream, "$MEAS_TIM:\n");
fprintf(stream, " %6.2f %6.2f\n", liveTime, totalTime);
// Write date measured
radix_line(" Writing date measured section");
fprintf(stream, "$DATE_MEA:\n");
fprintf(stream, "%s\n", dateTime.c_str());
// Write data
radix_line(" Writing data section");
radix_line(" number of channels: " << numberOfChannels);
radix_line(" counts by channel array size: " << countsByChannel.size());
fprintf(stream, "$DATA:\n");
fprintf(stream, "0 %d\n", numberOfChannels);
for (int i = 0; i < countsByChannel.size(); ++i)
{
fprintf(stream, "%d\n", int(countsByChannel[i]));
}
radix_line(" Wrote " << countsByChannel.size() << " data entries");
// Write region of interest
radix_line(" Writing region of interest section");
fprintf(stream, "$ROI:\n");
fprintf(stream, "0\n");
// Write presets
radix_line(" Writing presets section");
fprintf(stream, "$PRESETS:\n");
fprintf(stream, "Live Time\n");
fprintf(stream, " %6.2f\n", liveTime);
fprintf(stream, "0\n");
// Write energy fit
radix_line(" Writing energy fit section");
fprintf(stream, "$ENER_FIT:\n");
// Calculate energy fit vars
float energyFit1 =
energyCalOffset + (0.5 / float(numberOfChannels) * energyCalGain);
float energyFit2 = energyCalGain / float(numberOfChannels);
float energyFit3 = 0.f;
fprintf(stream, " %10.6e %10.6e %10.6e\n", energyFit1, energyFit2,
energyFit3);
// Write MCA calibration
radix_line(" Writing MCA calibration section");
fprintf(stream, "$MCA_CAL:\n");
fprintf(stream, "3\n");
fprintf(stream, " %10.6e %10.6e %10.6e keV\n", energyFit1,
energyFit2, energyFit3);
// End the record
radix_line(" Finished - ending the record");
fprintf(stream, "$ENDRECORD:\n");
result = true;
}
return result;
}
......
......@@ -46,13 +46,26 @@ TEST(RadixIO, SpectrumFromSPE)
EXPECT_EQ(30, testSpectrum->spectrumDataCount());
}
// TEST(RadixIO, SpectrumToSPE)
//{
// std::string testSPEFile = "test.spe";
// Spectrum::SP testSpectrum = std::make_shared<Spectrum>();
// // Load the spectrum
// SpectrumSPEStream<Spectrum::SP> stream(testSpectrum);
// ASSERT_TRUE(stream.write_to(testSPEFile));
//}
TEST(RadixIO, SpectrumToSPE)
{
// Read in a test spectrum
std::string testReadSPEFile =
radix::to_native_path(std::string(dirname(__FILE__) + "/data/235F.spe"));
Spectrum::SP testSpectrum1 = std::make_shared<Spectrum>();
SpectrumSPEStream<Spectrum::SP> testStream1(testSpectrum1);
ASSERT_TRUE(testStream1.read_from(testReadSPEFile));
// Write the spectrum back out to a file
std::string testWriteSPEFile = "test.spe";
ASSERT_TRUE(testStream1.write_to(testWriteSPEFile));
// Read it again and ensure the round trip is successful
Spectrum::SP testSpectrum2 = std::make_shared<Spectrum>();
SpectrumSPEStream<Spectrum::SP> testStream2(testSpectrum2);
ASSERT_TRUE(testStream2.read_from(testWriteSPEFile));
// Test the contents of the two spectra
EXPECT_EQ(30, testSpectrum2->spectrumDataCount());
EXPECT_EQ(testSpectrum1->spectrumDataCount(),
testSpectrum2->spectrumDataCount());
}
  • @jap for some reason it is not correctly retrieving the first spectrum data object for writing. I must be making some error in either the population or retrieval - can we go over when you are in?

Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment