File: DataLoader.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 (165 lines) | stat: -rw-r--r-- 6,465 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
//  ************************************************************************************************
//
//  BornAgain: simulate and fit reflection and scattering
//
//! @file      GUI/View/IO/DataLoader.cpp
//! @brief     Implements functions in namespace RW.
//!
//! @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 "GUI/View/IO/DataLoader.h"
#include "Base/Util/Assert.h"
#include "Device/Data/Datafield.h"
#include "Device/IO/IOFactory.h"
#include "Device/IO/ZipUtil.h"
#include "GUI/Model/Data/DataItem.h"
#include "GUI/Model/File/DatafileItem.h"
#include "GUI/Model/Util/Path.h"
#include "GUI/View/IO/DatafileInspector.h"
#include "GUI/View/IO/ImportDialogs.h"
#include "GUI/View/Info/MessageBox.h"
#include "GUI/View/Widget/AppConfig.h"
#include <QFileDialog>
#include <QFileInfo>

namespace {

// The following two functions are templated on T in {IO::Filetype1D, IO::Filetype2D}.

template <typename T>
QString join_filterkeys(std::vector<std::pair<const QString, T>> _nomap, const QString& _separator)
{
    QString result;
    for (const auto& it : _nomap) {
        if (!result.isEmpty())
            result += _separator;
        result += it.first;
    }
    return result;
}

template <typename T>
T filterkey2type(std::vector<std::pair<const QString, T>> _nomap, const QString& _key)
{
    for (const auto& it : _nomap)
        if (it.first == _key)
            return it.second;
    ASSERT_NEVER;
}

} // namespace


std::vector<DatafileItem*> IO::importData1D()
{
    static const std::vector<std::pair<const QString, IO::Filetype1D>> filters1D{
        {"Legacy table / interactive configuration (*.csv *.dat *.tab *.txt *.dat.gz)", IO::csv1D},
        {"Motofit (*.mft)", IO::mft},
        {"BornAgain (*.int *.int.gz)", IO::bornagain1D},
        {"all (*.*)", IO::unknown1D}};

    static const QString filters = ::join_filterkeys(filters1D, ";;");

    const QStringList fnames = QFileDialog::getOpenFileNames(
        Q_NULLPTR, "Open Intensity Files", gApp->data_import_dir, filters, &gApp->import_filter_1D);
    if (fnames.isEmpty())
        return {};

    gApp->data_import_dir = GUI::Path::fileDir(fnames[0]); // all in list have same dir
    const IO::Filetype1D global_ftype = ::filterkey2type(filters1D, gApp->import_filter_1D);

    std::vector<DatafileItem*> result;
    for (const QString& fname : fnames) {
        try {
            const std::string ext = ZipUtil::uncompressedExtension(fname.toStdString());
            if (ext == ".001" || ext == ".tif" || ext == ".tiff")
                throw std::runtime_error("File \"" + ext + "\" seems to be 2D");

            const ImportSettings1D* settings = nullptr;

            IO::Filetype1D ftype = global_ftype;
            if (ftype == IO::unknown1D)
                ftype = IO::filename2type1D(fname.toStdString());
            if (ftype == IO::csv1D) {
                DatafileInspector viewer(nullptr, fname);
                Import1dDialog dialog(&viewer);
                const auto result = dialog.exec();
                if (result != QDialog::Accepted)
                    return {};
                settings = &Import1dDialog::Msettings;
            }

            Datafield df = IO::readData1D(fname.toStdString(), ftype, settings);
            if (df.rank() != 1)
                throw std::runtime_error("File does not contain a 1d data set");
            result.push_back(new DatafileItem(QFileInfo(fname).baseName(), df));
        } catch (std::exception& ex) {
            const QString message = QString("Cannot read file %1:\n\n%2")
                                        .arg(fname)
                                        .arg(QString::fromStdString(std::string(ex.what())));
            GUI::Message::warning("File import", message);
            return {};
        }
    }
    return result;
}

std::vector<DatafileItem*> IO::importData2D()
{
    static const std::vector<std::pair<const QString, IO::Filetype2D>> filters2D{
        {"CSV (*.csv *.dat *.tab *.txt *.dat.gz)", IO::csv2D},
        {"all (*.*)", IO::unknown2D},
        {"TIFF (*.tif *.tiff *.tif.gz)", IO::tiff},
        {"Nicos/SANSDRaw (*.001)", IO::nicos2D},
        {"BornAgain (*.int *.int.gz)", IO::bornagain2D}};

    static const QString filters = ::join_filterkeys(filters2D, ";;");

    const QStringList fnames = QFileDialog::getOpenFileNames(
        Q_NULLPTR, "Open Intensity Files", gApp->data_import_dir, filters, &gApp->import_filter_2D);
    if (fnames.isEmpty())
        return {};

    gApp->data_import_dir = GUI::Path::fileDir(fnames[0]); // all in list have same dir
    const IO::Filetype2D global_ftype = ::filterkey2type(filters2D, gApp->import_filter_2D);

    std::vector<DatafileItem*> result;
    for (const QString& fname : fnames) {
        try {
            const std::string ext = ZipUtil::uncompressedExtension(fname.toStdString());
            if (ext == ".mft")
                throw std::runtime_error("File \"" + ext + "\" seems to be 1D");

            const ImportSettings2D* settings = nullptr;

            IO::Filetype2D ftype = global_ftype;
            if (ftype == IO::unknown2D)
                ftype = IO::filename2type2D(fname.toStdString());
            if (ftype == IO::csv2D) {
                DatafileInspector viewer(nullptr, fname);
                Import2dDialog dialog(&viewer);
                const auto result = dialog.exec();
                if (result != QDialog::Accepted)
                    return {};
                settings = &Import2dDialog::Msettings;
            }

            Datafield df = IO::readData2D(fname.toStdString(), ftype, settings);
            if (df.rank() != 2)
                throw std::runtime_error("File does not contain a 2d data set");
            result.push_back(new DatafileItem(QFileInfo(fname).baseName(), df));
        } catch (std::exception& ex) {
            const QString message = QString("Cannot read file %1\n\n%2")
                                        .arg(fname)
                                        .arg(QString::fromStdString(std::string(ex.what())));
            GUI::Message::warning("File import", message);
            return {};
        }
    }
    return result;
}