summaryrefslogtreecommitdiff
path: root/src/fisx_xrfconfig.cpp
blob: 7be58676986a1a720e14a2099e91de14cc6e7db9 (plain)
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
#/*##########################################################################
#
# The fisx library for X-Ray Fluorescence
#
# Copyright (c) 2014-2018 European Synchrotron Radiation Facility
#
# This file is part of the fisx X-ray developed by V.A. Sole
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
#############################################################################*/
#include "fisx_xrfconfig.h"
#include "fisx_simpleini.h"
#include <stdexcept>
#include <iostream>
#include <algorithm>

namespace fisx
{

XRFConfig::XRFConfig()
{
    this->setGeometry(45.0, 45.0, 90.);
}

void XRFConfig::setGeometry(const double & alphaIn, const double & alphaOut, const double & scatteringAngle)
{
    this->alphaIn = alphaIn;
    this->alphaOut = alphaOut;
    this->scatteringAngle = scatteringAngle;
}

void XRFConfig::readConfigurationFromFile(const std::string & fileName)
{
    SimpleIni iniFile = SimpleIni(fileName);
    std::map<std::string, std::string> sectionContents;
    std::string key;
    std::string content;
    std::locale loc;
    std::map<std::string, std::vector<double> > mapDoubles;
    std::map<std::string, std::vector<std::string> > mapStrings;
    std::vector<std::string> stringVector;
    std::vector<std::string>::size_type iStringVector;
    std::vector<double> doubleVector;
    std::vector<int> intVector, flagVector;
    std::vector<int>::size_type iIntVector;
    std::map<std::string, std::string>::const_iterator c_it;
    std::vector<Material>::size_type iMaterial;
    std::vector<std::string> splitString;
    Material material;
    Layer layer;
    long counter;
    bool multilayerSample;
    double value;


    // find out if it is a fix or a PyMca configuration file
    /* This not needed since a fisx file format is not defined yet
    bool fisxFile;
    sectionContents.clear();
    sectionContents = iniFile.readSection("fisx", false);
    fisxFile = true;
    if(!sectionContents.size())
    {
        fisxFile = false;
    }
    if (fisxFile)
    {
        std::cout << "Not implemented" << std::endl;
        throw std::invalid_argument("Only PyMca configuration files supported.");
    }
    */

    // Assume is a PyMca generated file.
    // TODO: Still to find out if it is a fit output file or a configuration file
    // Assume it is configuration file.
    // In case of fit file, the configuration is under [result.config]
    // GET BEAM
    sectionContents.clear();
    sectionContents = iniFile.readSection("fit", false);
    if(!sectionContents.size())
    {
        sectionContents = iniFile.readSection("result.config", false);
        if(!sectionContents.size())
        {
            throw std::invalid_argument("File not recognized as a PyMca configuration file.");
        }
        std::cout << "fit result file" << std::endl;
    }
    else
    {
        // In case of PyMca.ini file, the configuration is under [Fit.Configuration]
        ;
    }
    mapDoubles.clear();
    content = sectionContents["energy"];
    iniFile.parseStringAsMultipleValues(content, mapDoubles["energy"], -666.0);
    content = sectionContents["energyweight"];
    iniFile.parseStringAsMultipleValues(content, mapDoubles["weight"], -1.0);
    content = sectionContents["energyscatter"];
    iniFile.parseStringAsMultipleValues(content, intVector, -1);
    content = sectionContents["energyflag"];
    iniFile.parseStringAsMultipleValues(content, flagVector, 0);

    /*
    std::cout << "Passed" << std::endl;
    std::cout << "Energy size = " << mapDoubles["energy"].size() << std::endl;
    std::cout << "weight size = " << mapDoubles["weight"].size() << std::endl;
    std::cout << "scatter = " << intVector.size() << std::endl;
    std::cout << "falg = " << flagVector.size() << std::endl;
    */

    if (mapDoubles["weight"].size() == 0)
    {
        mapDoubles["weight"].resize(mapDoubles["energy"].size());
        std::fill(mapDoubles["weight"].begin(), mapDoubles["weight"].end(), 1.0);
    }
    if (intVector.size() == 0)
    {
        intVector.resize(mapDoubles["energy"].size());
        std::fill(intVector.begin(), intVector.end(), 1.0);
    }
    if (flagVector.size() == 0)
    {
        flagVector.resize(mapDoubles["energy"].size());
        std::fill(flagVector.begin(), flagVector.end(), 1.0);
    }

    counter = 0;
    iIntVector = flagVector.size();
    while(iIntVector > 0)
    {
        iIntVector--;
        if ((flagVector[iIntVector] > 0) && (mapDoubles["energy"][iIntVector] != -666.0))
        {
            if(mapDoubles["energy"][iIntVector] <= 0.0)
            {
                throw std::invalid_argument("Negative excitation beam photon energy");
            }
            if(mapDoubles["weight"][iIntVector] < 0.0)
            {
                throw std::invalid_argument("Negative excitation beam photon weight");
            }
            if(intVector[iIntVector] < 0)
            {
                std::cout << "WARNING: " << "Negative characteristic flag. ";
                std::cout << "Assuming not a characteristic photon energy." << std::endl;
                intVector[iIntVector] = 0;
            }
            counter++;
        }
        else
        {
            // index not to be considered
            mapDoubles["energy"].erase(mapDoubles["energy"].begin() + iIntVector);
            mapDoubles["weight"].erase(mapDoubles["weight"].begin() + iIntVector);
            intVector.erase(intVector.begin() + iIntVector);
        }
    }
    this->setBeam(mapDoubles["energy"], mapDoubles["weight"], intVector);
    // GET THE MATERIALS
    iniFile.getSubsections("Materials", stringVector, false);
    this->materials.clear();
    if (stringVector.size())
    {
        std::string comment;
        double density;
        double thickness;
        std::vector<std::string> compoundList;
        std::vector<double> compoundFractions;
        std::string key;
        std::string::size_type j;
        // Materials found
        for (iStringVector = 0; iStringVector < stringVector.size(); iStringVector++)
        {
            sectionContents.clear();
            sectionContents = iniFile.readSection(stringVector[iStringVector], true);
            splitString.clear();
            iniFile.parseStringAsMultipleValues(stringVector[iStringVector],
                                            splitString,
                                            std::string(),
                                            '.');
            for (c_it = sectionContents.begin(); c_it != sectionContents.end(); ++c_it)
            {
                key = c_it->first;
                for (j = 0; j < key.size(); j++)
                {
                    key[j] = std::toupper(key[j], loc);
                }
                if (key == "DENSITY")
                {
                    iniFile.parseStringAsSingleValue(c_it->second, density, -1.0);
                }
                else if (key == "THICKNESS")
                {
                    iniFile.parseStringAsSingleValue(c_it->second, thickness, -1.0);
                }
                else if (key == "COMPOUNDLIST")
                {
                    iniFile.parseStringAsMultipleValues(c_it->second, \
                                                        compoundList, std::string());
                }
                else if ((key == "COMPOUNDFRACTION") || (key == "COMPOUNDFRACTIONS"))
                {
                    iniFile.parseStringAsMultipleValues(c_it->second, \
                                                        compoundFractions, -1.0);
                }
                else
                {
                    comment = c_it->second;
                }
            }
            material = Material(splitString[1], density, thickness, comment);
            material.setComposition(compoundList, compoundFractions);
            this->materials.push_back(material);
        }
    }

    // GET BEAM FILTERS AND ATTENUATORS
    sectionContents.clear();
    sectionContents = iniFile.readSection("attenuators", false);
    mapDoubles.clear();
    doubleVector.clear();
    stringVector.clear();
    this->beamFilters.clear();
    this->attenuators.clear();
    this->sample.clear();
    this->detector = Detector();
    multilayerSample = false;
    for (c_it = sectionContents.begin(); c_it != sectionContents.end(); ++c_it)
    {
        // std::cout << c_it->first << " " << c_it->second << std::endl;
        content = c_it->second;
        iniFile.parseStringAsMultipleValues(content, doubleVector, -1.0);
        iniFile.parseStringAsMultipleValues(content, stringVector, std::string());
        if (doubleVector.size() == 0.0)
        {
            std::cout << "WARNING: Empty line in attenuators section. Offending key is: "<< std::endl;
            std::cout << "<" << c_it->first << ">" << std::endl;
            continue;
        }
        if (doubleVector[0] > 0.0)
        {
            if (c_it->first.substr(0, c_it->first.size() - 1) == "BeamFilter")
            {
                // BeamFilter0 = 0, -, 0.0, 0.0, 1.0
                // std::cout << "BEAMFILTER" << std::endl;
                layer = Layer(c_it->first, doubleVector[2], doubleVector[3], doubleVector[4]);
                layer.setMaterial(stringVector[1]);
                for(iMaterial = 0; iMaterial < this->materials.size(); iMaterial++)
                {
                    if(this->materials[iMaterial].getName() == stringVector[1])
                    {
                        layer.setMaterial(this->materials[iMaterial]);
                    }
                }
                this->beamFilters.push_back(layer);
            }
            else
            {
                // atmosphere = 0, -, 0.0, 0.0, 1.0
                // Matrix = 0, MULTILAYER, 0.0, 0.0, 45.0, 45.0, 0, 90.0
                if (stringVector.size() == 8 )
                {
                    // Matrix
                    if (doubleVector[6] > 0.0)
                    {
                        this->setGeometry(doubleVector[4], doubleVector[5], doubleVector[7]);
                    }
                    else
                    {
                        this->setGeometry(doubleVector[4], doubleVector[5], doubleVector[4] + doubleVector[5]);
                    }
                    if (stringVector[1] == "MULTILAYER")
                    {
                        multilayerSample = true;
                    }
                    else
                    {
                        // funny factor is not set for the sample
                        layer = Layer(c_it->first, doubleVector[2], doubleVector[3], 1.0);
                        layer.setMaterial(stringVector[1]);
                        for(iMaterial = 0; iMaterial < this->materials.size(); iMaterial++)
                        {
                            if(this->materials[iMaterial].getName() == stringVector[1])
                            {
                                layer.setMaterial(this->materials[iMaterial]);
                            }
                        }
                        this->sample.push_back(layer);
                    }
                }
                else
                {
                    if (c_it->first.substr(0, 8) == "Detector")
                    {
                        // DETECTOR
                        // std::cout << "DETECTOR " << std::endl;
                        this->detector = Detector(c_it->first, doubleVector[2], doubleVector[3], doubleVector[4]);
                        this->detector.setMaterial(stringVector[1]);
                        for(iMaterial = 0; iMaterial < this->materials.size(); iMaterial++)
                        {
                            if(this->materials[iMaterial].getName() == stringVector[1])
                            {
                                detector.setMaterial(this->materials[iMaterial]);
                            }
                        }
                    }
                    else
                    {
                        // Attenuator
                        // std::cout << "ATTENUATOR " << std::endl;
                        layer = Layer(c_it->first, doubleVector[2], doubleVector[3], doubleVector[4]);
                        layer.setMaterial(stringVector[1]);
                        for(iMaterial = 0; iMaterial < this->materials.size(); iMaterial++)
                        {
                            if(this->materials[iMaterial].getName() == stringVector[1])
                            {
                                layer.setMaterial(this->materials[iMaterial]);
                            }
                        }
                        this->attenuators.push_back(layer);
                    }
                }
            }
        }
    }
    // for the time being it is not yet in the file
    this->referenceLayer = 0;
    // GET MULTILAYER SAMPLE IF NEEDED
    if (multilayerSample)
    {
        sectionContents.clear();
        sectionContents = iniFile.readSection("multilayer", false);
        for (c_it = sectionContents.begin(); c_it != sectionContents.end(); ++c_it)
        {
            // std::cout << c_it->first << " " << c_it->second << std::endl;
            content = c_it->second;
            iniFile.parseStringAsMultipleValues(content, doubleVector, -1.0);
            iniFile.parseStringAsMultipleValues(content, stringVector, std::string());
            if (doubleVector.size() == 0.0)
            {
                std::cout << "WARNING: Empty line in multilayer section. Offending key is: "<< std::endl;
                std::cout << "<" << c_it->first << ">" << std::endl;
                continue;
            }
            if (doubleVector[0] > 0.0)
            {
                    //BeamFilter0 = 0, -, 0.0, 0.0, 1.0
                    layer = Layer(c_it->first, doubleVector[2], doubleVector[3]);
                    layer.setMaterial(stringVector[1]);
                    for(iMaterial = 0; iMaterial < this->materials.size(); iMaterial++)
                    {
                        if(this->materials[iMaterial].getName() == stringVector[1])
                        {
                            layer.setMaterial(this->materials[iMaterial]);
                        }
                    }
                    this->sample.push_back(layer);
            }
        }
    }

    // CONCENTATIONS SETUP
    sectionContents.clear();
    sectionContents = iniFile.readSection("concentrations", false);
    for (c_it = sectionContents.begin(); c_it != sectionContents.end(); ++c_it)
    {
        key = c_it->first;
        iniFile.toUpper(key);
        iniFile.parseStringAsSingleValue(c_it->second, value, -1.0);
        if (key == "DISTANCE")
        {
            this->detector.setDistance(value);
        }
        if (key == "AREA")
        {
            this->detector.setActiveArea(value);
        }
    }
}

void XRFConfig::setBeam(const std::vector<double> & energy, \
                        const std::vector<double> & weight, \
                        const std::vector<int> & characteristic, \
                        const std::vector<double> & divergency)
{
    this->beam.setBeam(energy, weight, characteristic, divergency);
}

void XRFConfig::setBeam(const double & energy, const double & divergency)
{
    this->beam.setBeam(energy, divergency);
}

void XRFConfig::setBeam(const Beam & beam)
{
    this->beam = beam;
}

const Beam & XRFConfig::getBeam() const
{
    return this->beam;
}


void XRFConfig::setBeamFilters(const std::vector<Layer> & filters)
{
    this->beamFilters = filters;
}

void XRFConfig::setSample(const std::vector<Layer> & layers, const int & referenceLayer)
{
    if (referenceLayer >= (int) layers.size())
    {
        throw std::invalid_argument("Reference layer must be smaller than number of layers");
    }
    this->sample = layers;
    this->referenceLayer = referenceLayer;
}

void XRFConfig::setAttenuators(const std::vector<Layer> & attenuators)
{
    this->attenuators = attenuators;
}

void XRFConfig::setDetector(const Detector & detector)
{
    this->detector = detector;
}

std::ostream& operator<< (std::ostream& o, XRFConfig const& config)
{
    std::vector<Layer>::size_type i;
    o << "BEAM" << std::endl;
    o << config.beam << std::endl;
    o << "BEAM FILTERS" << std::endl;
    for(i = 0; i < config.beamFilters.size(); i++)
    {
        o << config.beamFilters[i] << std::endl;
    }
    o << "SAMPLE" << std::endl;
    for(i = 0; i < config.sample.size(); i++)
    {
        o << config.sample[i] << std::endl;
    }
    o << "ATTENUATORS" << std::endl;
    for(i = 0; i < config.attenuators.size(); i++)
    {
        o << config.attenuators[i] << std::endl;
    }
    o << "DETECTOR" << std::endl;
    o << config.detector << std::endl;

    o << "GEOMETRY" << std::endl;
    o << "Alpha In(deg): "<< config.getAlphaIn() << std::endl;
    o << "Alpha In(deg): "<< config.getAlphaOut() << std::endl;
    return o;
}

} // namespace fisx