File: ReadWriteINT.cpp

package info (click to toggle)
bornagain 23.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 103,936 kB
  • sloc: cpp: 423,131; python: 40,997; javascript: 11,167; awk: 630; sh: 318; ruby: 173; xml: 130; makefile: 51; ansic: 24
file content (203 lines) | stat: -rw-r--r-- 6,838 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
//  ************************************************************************************************
//
//  BornAgain: simulate and fit reflection and scattering
//
//! @file      Device/IO/ReadWriteINT.cpp
//! @brief     Implements functions read|writeBAInt.
//!
//! @homepage  http://www.bornagainproject.org
//! @license   GNU General Public License v3 or higher (see COPYING)
//! @copyright Forschungszentrum Jülich GmbH 2018
//! @authors   Scientific Computing Group at MLZ (see CITATION, AUTHORS)
//
//  ************************************************************************************************

#include "Device/IO/ReadWriteINT.h"
#include "Base/Axis/MakeScale.h"
#include "Base/Axis/Scale.h"
#include "Base/Math/Numeric.h"
#include "Base/Util/Assert.h"
#include "Base/Util/StringUtil.h"
#include "Device/Data/DataUtil.h"
#include "Device/Data/Datafield.h"
#include <iomanip>

namespace {

void writeBlock(const std::vector<double>& values, std::ostream& output_stream, size_t nrows,
                size_t ncols)
{
    output_stream.imbue(std::locale::classic());
    output_stream << std::scientific << std::setprecision(16);

    ASSERT(values.size() == nrows * ncols);
    for (size_t col = 0; col < ncols; ++col) {
        for (size_t row = 0; row < nrows; ++row) {
            if (row)
                output_stream << " ";
            output_stream << Numeric::ignoreDenormalized(values[row * ncols + col]);
        }
        output_stream << std::endl;
    }
}

//! Reads ListScan and GenericScale lines, already parsed as 'type(text)'.
std::vector<double> parse_x_list(std::string text, const std::string& type)
{
    if (text.substr(0, 1) == ",")
        throw std::runtime_error("file format from BornAgain <= 20 is obsolete");
    if (text.substr(0, 1) == "[" && text.substr(text.size() - 1, 1) == "]")
        text = text.substr(1, text.size() - 2);
    std::vector<std::string> arr = Base::String::split(text, ",");
    std::vector<double> result;
    for (std::string e : arr) {
        e = Base::String::trim(e);
        if (e.empty())
            continue;
        double x;
        if (!(Base::String::to_double(e, &x)))
            throw std::runtime_error("Reading " + type + ": cannot read entry '" + e + "'");
        result.push_back(x);
    }
    if (result.empty())
        throw std::runtime_error("Reading " + type + ": found empty list");
    return result;
}

//! Creates axis of certain type from input stream.
Scale* parseScale(std::istream& input_stream)
{
    std::string line;
    std::getline(input_stream, line);

    size_t j = line.find_first_of('(');
    if (j == std::string::npos)
        throw std::runtime_error("Scale constructor has no '('");
    std::string type = line.substr(0, j);

    line = line.substr(j + 1);
    if (line.back() != ')')
        throw std::runtime_error("Scale constructor call not ending with ')'");
    line = line.substr(0, line.size() - 1);

    if (line[0] != '"')
        throw std::runtime_error("Scale constructor arg 1 does not start with \"");
    j = 1 + line.substr(1).find_first_of('"');
    if (j == std::string::npos)
        throw std::runtime_error("Scale constructor arg 1 has no closing \"");
    std::string name = line.substr(1, j - 1);

    if (line[j + 1] != ',')
        throw std::runtime_error("Scale constructor arg 1 not followed by comma");
    std::string body = line.substr(j + 2);
    body = Base::String::trimFront(body, " ");

    if (type == "EquiDivision") {
        std::vector<std::string> arr = Base::String::split(body, ",");
        int nbins;
        double xmi, xma;
        if (!(Base::String::to_int(arr[0], &nbins) && Base::String::to_double(arr[1], &xmi)
              && Base::String::to_double(arr[2], &xma)))
            throw std::runtime_error("Reading EquiDivision: cannot read parameters");
        return newEquiDivision(name, nbins, xmi, xma);
    }
    if (type == "ListScan" || type == "DiscreteAxis")
        return newListScan(name, parse_x_list(body, type));

    if (type == "GenericScale")
        return newGenericScale(name, parse_x_list(body, type));

    throw std::runtime_error("Unknown axis type '" + type + "'");
}

std::vector<double> readBlock(std::istream& input_stream, size_t nrows, size_t ncols)
{
    std::string line;
    std::vector<double> invec;
    while (std::getline(input_stream, line)) {
        line = Base::String::trim(line);
        if (line.empty())
            break;
        if (line[0] == '#')
            continue;
        for (double x : Base::String::parse_doubles(line))
            invec.emplace_back(x);
    }
    if (invec.empty())
        return {};
    if (invec.size() != nrows * ncols) {
        std::stringstream msg;
        msg << "data block has size " << invec.size() << " instead of expected " << nrows << " x "
            << ncols;
        throw std::runtime_error(msg.str());
    }

    std::vector<double> result(nrows * ncols);
    for (size_t row = 0; row < nrows; ++row)
        for (size_t col = 0; col < ncols; ++col)
            result[col * nrows + row] = invec[row * ncols + col];
    return result;
}

} // namespace


Datafield Util::RW::readBAInt(std::istream& input_stream)
{
    std::string line;

    std::vector<const Scale*> axes;
    while (std::getline(input_stream, line)) {
        line = Base::String::trim(line);

        if (line.find("axis") != std::string::npos)
            axes.emplace_back(::parseScale(input_stream));

        if (line.find("data") != std::string::npos)
            break;
    }
    size_t nrows;
    size_t ncols;
    if (axes.size() == 1) {
        nrows = 1;
        ncols = axes[0]->size();
    } else if (axes.size() == 2) {
        nrows = axes[0]->size();
        ncols = axes[1]->size();
    } else
        ASSERT_NEVER;

    std::vector<double> datvec = ::readBlock(input_stream, nrows, ncols);

    // skip lines before errorbars
    while (std::getline(input_stream, line))
        if (line[0] == '#')
            break;

    std::vector<double> errvec = ::readBlock(input_stream, nrows, ncols);

    return {axes, datvec, errvec};
}

void Util::RW::writeBAInt(const Datafield& data, std::ostream& output_stream)
{
    output_stream << "# BornAgain Intensity Data\n";

    for (size_t i = 0; i < data.rank(); ++i) {
        const Scale& axis = data.axis(i);
        output_stream << std::endl;
        output_stream << "# axis-" << i << "\n";
        output_stream << axis << "\n";
    }
    size_t ncols = data.axis(0).size();
    size_t nrows = data.rank() == 1 ? 1 : data.axis(1).size();

    output_stream << "\n# data\n";
    ::writeBlock(data.flatVector(), output_stream, nrows, ncols);

    if (data.hasErrorSigmas()) {
        output_stream << "\n# errorbars\n";
        ::writeBlock(data.errorSigmas(), output_stream, nrows, ncols);
    }
    output_stream << std::endl;
}