Skip to content

Commit 41b38f4

Browse files
committed
Add the ability to output as a csv back in
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.
1 parent 05ddc8f commit 41b38f4

File tree

6 files changed

+185
-9
lines changed

6 files changed

+185
-9
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ cmake_output*.log
1616

1717
# Output files from the example executable that uses the library
1818
masstable*.json
19+
masstable*.csv
1920

2021
# emacs related files
2122
*~

example/ndr.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ int main()
1212

1313
if (table.populateInternalMassTable())
1414
{
15-
[[maybe_unused]] auto ignore = table.outputTableToJSON();
15+
[[maybe_unused]] auto json_ignore = table.outputTableToJSON();
16+
[[maybe_unused]] auto csv_ignore = table.outputTableToCSV();
1617
}
1718
}
1819

include/nuclear-data-reader/isotope.hpp

+63-2
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,76 @@ class Isotope
4040
// All of the NUBASE data
4141
mutable NUBASE::Data nubase;
4242

43+
/**
44+
* Output all of the data as a csv string
45+
*
46+
* \param Nothing
47+
*
48+
* \return All of the members in csv format
49+
*/
50+
[[nodiscard]] std::string writeAsCSV() const;
4351

4452
/**
4553
* Output all of the data as a json string
4654
*
47-
* \param Nothing
55+
* \param A boolean flag to set if new lines are used within a json unit to make it human readable
4856
*
49-
* \return All of the members in the format of a json ... unit
57+
* \return All of the members in the format of a json unit
5058
*/
5159
[[nodiscard]] std::string writeAsJSON(const bool human_readable = true) const;
60+
61+
/**
62+
* Create the header line to be used when writing as a csv
63+
*
64+
* \param Nothing
65+
*
66+
* \return The variables names in csv formatted string
67+
*/
68+
[[nodiscard]] static std::string writeCSVHeader()
69+
{
70+
return std::string("A,"
71+
"Z,"
72+
"N,"
73+
"Symbol,"
74+
"Decay,"
75+
"Experimental,"
76+
"NubaseMassExcess,"
77+
"ErrorNubaseMassExcess,"
78+
"AMEMassExcess,"
79+
"ErrorAMEMassExcess,"
80+
"HalfLife,"
81+
"SingleNeutronSeparationEnergy,"
82+
"ErrorSingleNeutronSeparationEnergy,"
83+
"SingleProtonSeparationEnergy,"
84+
"ErrorSingleProtonSeparationEnergy,"
85+
"DoubleNeutronSeparationEnergy,"
86+
"ErrorDoubleNeutronSeparationEnergy,"
87+
"DoubleProtonSeparationEnergy,"
88+
"ErrorDoubleProtonSeparationEnergy,"
89+
"BindingEnergyPerA,"
90+
"ErrorBindingEnergyPerA,"
91+
"AtomicMass,"
92+
"ErrorAtomicMass,"
93+
"BetaDecayEnergy,"
94+
"ErrorBetaDecayEnergy,"
95+
"QAlpha,"
96+
"ErrorQAlpha,"
97+
"Q2B-,"
98+
"ErrorQ2B-,"
99+
"Qepsilon_p,"
100+
"ErrorQepsilon_p,"
101+
"QB-n,"
102+
"ErrorQB-n,"
103+
"Q4B-,"
104+
"ErrorQ4B-,"
105+
"QdAlpha,"
106+
"ErrorQdAlpha,"
107+
"QpAlpha,"
108+
"ErrorQpAlpha,"
109+
"QnAlpha,"
110+
"ErrorQnAlpha,"
111+
"Year");
112+
}
52113
};
53114

54115
#endif // ISOTOPE_HPP

include/nuclear-data-reader/massTable.hpp

+12-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*
55
* \brief Read the mass table
66
*
7-
* Read and store the deatils from the necessary mass table file
7+
* Read and store the details from the necessary mass table file
88
*/
99
#ifndef MASSTABLE_HPP
1010
#define MASSTABLE_HPP
@@ -13,8 +13,6 @@
1313
#include "nuclear-data-reader/isotope.hpp"
1414
#include "nuclear-data-reader/nubase_data.hpp"
1515

16-
#include <fmt/core.h>
17-
1816
#include <algorithm>
1917
#include <array>
2018
#include <cstdint>
@@ -43,7 +41,7 @@ class MassTable
4341
// but the user can still select the year 1997 without worry.
4442
if (year == 1997)
4543
{
46-
fmt::print(stderr, "**WARNING**: The 1997 data is the same as the 1995 data so setting the year to 1995.\n");
44+
// fmt::print(stderr, "**WARNING**: The 1997 data is the same as the 1995 data so setting the year to 1995.\n");
4745
year = 1995;
4846
}
4947

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

241239
/**
242-
* Convert ... singular file format to json
240+
* Convert singular file format to json
243241
*
244242
* \param Nothing
245243
*
246244
* \return Nothing
247245
*/
248246
[[nodiscard]] bool outputTableToJSON() const;
247+
248+
/**
249+
* Convert singular file format to csv
250+
*
251+
* \param Nothing
252+
*
253+
* \return Nothing
254+
*/
255+
[[nodiscard]] bool outputTableToCSV() const;
249256
};
250257

251258
#endif // MASSTABLE_HPP

src/isotope.cpp

+88
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,94 @@
77

88
#include <string>
99

10+
std::string Isotope::writeAsCSV() const
11+
{
12+
// One item per line as I find it simpler to use
13+
return fmt::format("{0},"
14+
"{1},"
15+
"{2},"
16+
"{3},"
17+
"{4},"
18+
"{5},"
19+
"{6},"
20+
"{7},"
21+
"{8},"
22+
"{9},"
23+
"{10:.3e},"
24+
"{11},"
25+
"{12},"
26+
"{13},"
27+
"{14},"
28+
"{15},"
29+
"{16},"
30+
"{17},"
31+
"{18},"
32+
"{19},"
33+
"{20},"
34+
"{21},"
35+
"{22},"
36+
"{23},"
37+
"{24},"
38+
"{25},"
39+
"{26},"
40+
"{27},"
41+
"{28},"
42+
"{29},"
43+
"{30},"
44+
"{31},"
45+
"{32},"
46+
"{33},"
47+
"{34},"
48+
"{35},"
49+
"{36},"
50+
"{37},"
51+
"{38},"
52+
"{39},"
53+
"{40},"
54+
"{41}",
55+
ame.A,
56+
ame.Z,
57+
ame.N,
58+
nubase.symbol,
59+
nubase.decay,
60+
(nubase.exp == NUBASE::Measured::EXPERIMENTAL) ? 0 : 1,
61+
Converter::FloatToNdp(nubase.mass_excess.amount, NDP),
62+
Converter::FloatToNdp(nubase.mass_excess.uncertainty.value_or(-1.0), NDP),
63+
Converter::FloatToNdp(ame.mass_excess.amount, NDP),
64+
Converter::FloatToNdp(ame.mass_excess.uncertainty.value_or(-1.0), NDP),
65+
nubase.hl.count(),
66+
Converter::FloatToNdp(ame.s_n.amount, NDP),
67+
Converter::FloatToNdp(ame.s_n.uncertainty.value_or(-1.0), NDP),
68+
Converter::FloatToNdp(ame.s_p.amount, NDP),
69+
Converter::FloatToNdp(ame.s_p.uncertainty.value_or(-1.0), NDP),
70+
Converter::FloatToNdp(ame.s_2n.amount, NDP),
71+
Converter::FloatToNdp(ame.s_2n.uncertainty.value_or(-1.0), NDP),
72+
Converter::FloatToNdp(ame.s_2p.amount, NDP),
73+
Converter::FloatToNdp(ame.s_2p.uncertainty.value_or(-1.0), NDP),
74+
Converter::FloatToNdp(ame.binding_energy_per_A.amount, NDP),
75+
Converter::FloatToNdp(ame.binding_energy_per_A.uncertainty.value_or(-1.0), NDP),
76+
Converter::FloatToNdp(ame.atomic_mass.amount, NDP),
77+
Converter::FloatToNdp(ame.atomic_mass.uncertainty.value_or(-1.0), NDP),
78+
Converter::FloatToNdp(ame.beta_decay_energy.amount, NDP),
79+
Converter::FloatToNdp(ame.beta_decay_energy.uncertainty.value_or(-1.0), NDP),
80+
Converter::FloatToNdp(ame.q_a.amount, NDP),
81+
Converter::FloatToNdp(ame.q_a.uncertainty.value_or(-1.0), NDP),
82+
Converter::FloatToNdp(ame.q_2bm.amount, NDP),
83+
Converter::FloatToNdp(ame.q_2bm.uncertainty.value_or(-1.0), NDP),
84+
Converter::FloatToNdp(ame.q_ep.amount, NDP),
85+
Converter::FloatToNdp(ame.q_ep.uncertainty.value_or(-1.0), NDP),
86+
Converter::FloatToNdp(ame.q_bm_n.amount, NDP),
87+
Converter::FloatToNdp(ame.q_bm_n.uncertainty.value_or(-1.0), NDP),
88+
Converter::FloatToNdp(ame.q_4bm.amount, NDP),
89+
Converter::FloatToNdp(ame.q_4bm.uncertainty.value_or(-1.0), NDP),
90+
Converter::FloatToNdp(ame.q_da.amount, NDP),
91+
Converter::FloatToNdp(ame.q_da.uncertainty.value_or(-1.0), NDP),
92+
Converter::FloatToNdp(ame.q_pa.amount, NDP),
93+
Converter::FloatToNdp(ame.q_pa.uncertainty.value_or(-1.0), NDP),
94+
Converter::FloatToNdp(ame.q_na.amount, NDP),
95+
Converter::FloatToNdp(ame.q_na.uncertainty.value_or(-1.0), NDP),
96+
nubase.year);
97+
}
1098

1199
std::string Isotope::writeAsJSON(const bool human_readable) const
12100
{

src/massTable.cpp

+19-1
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ bool MassTable::outputTableToJSON() const
524524
{
525525
const auto outfile = fmt::format("masstable_{}.json", year);
526526

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

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

538538
return true;
539539
}
540+
541+
542+
bool MassTable::outputTableToCSV() const
543+
{
544+
const auto outfile = fmt::format("masstable_{}.csv", year);
545+
546+
fmt::print("New csv formatted file: {}\n", outfile);
547+
auto out = fmt::output_file(outfile);
548+
549+
out.print("{}\n", Isotope::writeCSVHeader());
550+
551+
for (const auto& isotope : fullDataTable)
552+
{
553+
out.print("{}\n", isotope.writeAsCSV());
554+
}
555+
556+
return true;
557+
}

0 commit comments

Comments
 (0)