Skip to content

Commit

Permalink
Add the ability to output as a csv back in
Browse files Browse the repository at this point in the history
I'm playing with something that needs the table as a csv file so adding
the functionality back. Example usage file has been updated to run the
csv output.

The inclusion of fmt as dependency of the headers slipped in when the
1995 and 1997 data files were added. This has been removed.
  • Loading branch information
php1ic committed Jun 8, 2024
1 parent 05ddc8f commit 41b38f4
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ cmake_output*.log

# Output files from the example executable that uses the library
masstable*.json
masstable*.csv

# emacs related files
*~
Expand Down
3 changes: 2 additions & 1 deletion example/ndr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ int main()

if (table.populateInternalMassTable())
{
[[maybe_unused]] auto ignore = table.outputTableToJSON();
[[maybe_unused]] auto json_ignore = table.outputTableToJSON();
[[maybe_unused]] auto csv_ignore = table.outputTableToCSV();
}
}

Expand Down
65 changes: 63 additions & 2 deletions include/nuclear-data-reader/isotope.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,76 @@ class Isotope
// All of the NUBASE data
mutable NUBASE::Data nubase;

/**
* Output all of the data as a csv string
*
* \param Nothing
*
* \return All of the members in csv format
*/
[[nodiscard]] std::string writeAsCSV() const;

/**
* Output all of the data as a json string
*
* \param Nothing
* \param A boolean flag to set if new lines are used within a json unit to make it human readable
*
* \return All of the members in the format of a json ... unit
* \return All of the members in the format of a json unit
*/
[[nodiscard]] std::string writeAsJSON(const bool human_readable = true) const;

/**
* Create the header line to be used when writing as a csv
*
* \param Nothing
*
* \return The variables names in csv formatted string
*/
[[nodiscard]] static std::string writeCSVHeader()
{
return std::string("A,"
"Z,"
"N,"
"Symbol,"
"Decay,"
"Experimental,"
"NubaseMassExcess,"
"ErrorNubaseMassExcess,"
"AMEMassExcess,"
"ErrorAMEMassExcess,"
"HalfLife,"
"SingleNeutronSeparationEnergy,"
"ErrorSingleNeutronSeparationEnergy,"
"SingleProtonSeparationEnergy,"
"ErrorSingleProtonSeparationEnergy,"
"DoubleNeutronSeparationEnergy,"
"ErrorDoubleNeutronSeparationEnergy,"
"DoubleProtonSeparationEnergy,"
"ErrorDoubleProtonSeparationEnergy,"
"BindingEnergyPerA,"
"ErrorBindingEnergyPerA,"
"AtomicMass,"
"ErrorAtomicMass,"
"BetaDecayEnergy,"
"ErrorBetaDecayEnergy,"
"QAlpha,"
"ErrorQAlpha,"
"Q2B-,"
"ErrorQ2B-,"
"Qepsilon_p,"
"ErrorQepsilon_p,"
"QB-n,"
"ErrorQB-n,"
"Q4B-,"
"ErrorQ4B-,"
"QdAlpha,"
"ErrorQdAlpha,"
"QpAlpha,"
"ErrorQpAlpha,"
"QnAlpha,"
"ErrorQnAlpha,"
"Year");
}
};

#endif // ISOTOPE_HPP
17 changes: 12 additions & 5 deletions include/nuclear-data-reader/massTable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*
* \brief Read the mass table
*
* Read and store the deatils from the necessary mass table file
* Read and store the details from the necessary mass table file
*/
#ifndef MASSTABLE_HPP
#define MASSTABLE_HPP
Expand All @@ -13,8 +13,6 @@
#include "nuclear-data-reader/isotope.hpp"
#include "nuclear-data-reader/nubase_data.hpp"

#include <fmt/core.h>

#include <algorithm>
#include <array>
#include <cstdint>
Expand Down Expand Up @@ -43,7 +41,7 @@ class MassTable
// but the user can still select the year 1997 without worry.
if (year == 1997)
{
fmt::print(stderr, "**WARNING**: The 1997 data is the same as the 1995 data so setting the year to 1995.\n");
// fmt::print(stderr, "**WARNING**: The 1997 data is the same as the 1995 data so setting the year to 1995.\n");
year = 1995;
}

Expand Down Expand Up @@ -239,13 +237,22 @@ class MassTable
[[nodiscard]] bool readNUBASE(const std::filesystem::path& nubaseTable);

/**
* Convert ... singular file format to json
* Convert singular file format to json
*
* \param Nothing
*
* \return Nothing
*/
[[nodiscard]] bool outputTableToJSON() const;

/**
* Convert singular file format to csv
*
* \param Nothing
*
* \return Nothing
*/
[[nodiscard]] bool outputTableToCSV() const;
};

#endif // MASSTABLE_HPP
88 changes: 88 additions & 0 deletions src/isotope.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,94 @@

#include <string>

std::string Isotope::writeAsCSV() const
{
// One item per line as I find it simpler to use
return fmt::format("{0},"
"{1},"
"{2},"
"{3},"
"{4},"
"{5},"
"{6},"
"{7},"
"{8},"
"{9},"
"{10:.3e},"
"{11},"
"{12},"
"{13},"
"{14},"
"{15},"
"{16},"
"{17},"
"{18},"
"{19},"
"{20},"
"{21},"
"{22},"
"{23},"
"{24},"
"{25},"
"{26},"
"{27},"
"{28},"
"{29},"
"{30},"
"{31},"
"{32},"
"{33},"
"{34},"
"{35},"
"{36},"
"{37},"
"{38},"
"{39},"
"{40},"
"{41}",
ame.A,
ame.Z,
ame.N,
nubase.symbol,
nubase.decay,
(nubase.exp == NUBASE::Measured::EXPERIMENTAL) ? 0 : 1,
Converter::FloatToNdp(nubase.mass_excess.amount, NDP),
Converter::FloatToNdp(nubase.mass_excess.uncertainty.value_or(-1.0), NDP),
Converter::FloatToNdp(ame.mass_excess.amount, NDP),
Converter::FloatToNdp(ame.mass_excess.uncertainty.value_or(-1.0), NDP),
nubase.hl.count(),
Converter::FloatToNdp(ame.s_n.amount, NDP),
Converter::FloatToNdp(ame.s_n.uncertainty.value_or(-1.0), NDP),
Converter::FloatToNdp(ame.s_p.amount, NDP),
Converter::FloatToNdp(ame.s_p.uncertainty.value_or(-1.0), NDP),
Converter::FloatToNdp(ame.s_2n.amount, NDP),
Converter::FloatToNdp(ame.s_2n.uncertainty.value_or(-1.0), NDP),
Converter::FloatToNdp(ame.s_2p.amount, NDP),
Converter::FloatToNdp(ame.s_2p.uncertainty.value_or(-1.0), NDP),
Converter::FloatToNdp(ame.binding_energy_per_A.amount, NDP),
Converter::FloatToNdp(ame.binding_energy_per_A.uncertainty.value_or(-1.0), NDP),
Converter::FloatToNdp(ame.atomic_mass.amount, NDP),
Converter::FloatToNdp(ame.atomic_mass.uncertainty.value_or(-1.0), NDP),
Converter::FloatToNdp(ame.beta_decay_energy.amount, NDP),
Converter::FloatToNdp(ame.beta_decay_energy.uncertainty.value_or(-1.0), NDP),
Converter::FloatToNdp(ame.q_a.amount, NDP),
Converter::FloatToNdp(ame.q_a.uncertainty.value_or(-1.0), NDP),
Converter::FloatToNdp(ame.q_2bm.amount, NDP),
Converter::FloatToNdp(ame.q_2bm.uncertainty.value_or(-1.0), NDP),
Converter::FloatToNdp(ame.q_ep.amount, NDP),
Converter::FloatToNdp(ame.q_ep.uncertainty.value_or(-1.0), NDP),
Converter::FloatToNdp(ame.q_bm_n.amount, NDP),
Converter::FloatToNdp(ame.q_bm_n.uncertainty.value_or(-1.0), NDP),
Converter::FloatToNdp(ame.q_4bm.amount, NDP),
Converter::FloatToNdp(ame.q_4bm.uncertainty.value_or(-1.0), NDP),
Converter::FloatToNdp(ame.q_da.amount, NDP),
Converter::FloatToNdp(ame.q_da.uncertainty.value_or(-1.0), NDP),
Converter::FloatToNdp(ame.q_pa.amount, NDP),
Converter::FloatToNdp(ame.q_pa.uncertainty.value_or(-1.0), NDP),
Converter::FloatToNdp(ame.q_na.amount, NDP),
Converter::FloatToNdp(ame.q_na.uncertainty.value_or(-1.0), NDP),
nubase.year);
}

std::string Isotope::writeAsJSON(const bool human_readable) const
{
Expand Down
20 changes: 19 additions & 1 deletion src/massTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ bool MassTable::outputTableToJSON() const
{
const auto outfile = fmt::format("masstable_{}.json", year);

fmt::print("New file: {}\n", outfile);
fmt::print("New json formatted file: {}\n", outfile);
auto out = fmt::output_file(outfile);

out.print("[\n");
Expand All @@ -537,3 +537,21 @@ bool MassTable::outputTableToJSON() const

return true;
}


bool MassTable::outputTableToCSV() const
{
const auto outfile = fmt::format("masstable_{}.csv", year);

fmt::print("New csv formatted file: {}\n", outfile);
auto out = fmt::output_file(outfile);

out.print("{}\n", Isotope::writeCSVHeader());

for (const auto& isotope : fullDataTable)
{
out.print("{}\n", isotope.writeAsCSV());
}

return true;
}

0 comments on commit 41b38f4

Please sign in to comment.