normalization.py 3.37 KB
Newer Older
payno's avatar
payno committed
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
# coding: utf-8
# /*##########################################################################
#
# Copyright (c) 2016-2017 European Synchrotron Radiation Facility
#
# 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.
#
# ###########################################################################*/

__authors__ = ["H. Payno"]
__license__ = "MIT"
__date__ = "06/11/2019"


from PyMca5.PyMcaPhysics.xas.XASClass import XASClass
32
from xas.core.types import PyMcaXAS
payno's avatar
payno committed
33
34
35
36
import logging
_logger = logging.getLogger(__name__)


37
def pymca_normalization(xas_obj):
payno's avatar
payno committed
38
    """
39

40
41
42
43
44
    :param xas_obj: object containing the configuration and spectrum to process
    :type: Union[XASBase, dict]. If is a dict, should contain configuration or
                                 spectrum keys. Otherwise is simply the spectrum
    :return: spectrum dict
    :rtype: dict
payno's avatar
payno committed
45
46
    """
    pymca_xas = XASClass()
47
48
49
50
51
    if isinstance(xas_obj, dict):
        _xas_obj = PyMcaXAS().load_frm_dict(xas_obj)
    else:
        _xas_obj = xas_obj
    if 'Energy' not in _xas_obj.spectrum or 'Mu' not in _xas_obj.spectrum:
payno's avatar
payno committed
52
53
54
        _logger.error('Energy and or Mu is/are not specified, unable to '
                      'normalize')
        return
55
56
57
    pymca_xas.setSpectrum(energy=_xas_obj.spectrum['Energy'],
                          mu=_xas_obj.spectrum['Mu'])
    pymca_xas.setConfiguration(_xas_obj.configuration)
58
    _xas_obj.configuration = pymca_xas.getConfiguration()
59
    pymca_xas.processSpectrum()
payno's avatar
payno committed
60
    res = pymca_xas.normalize()
61
    _xas_obj.spectrum.update(res)
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
    return _xas_obj


class PyMca_normalization(object):
    def __init__(self):
        self._settings = None

    def setProperties(self, properties):
        if '_pymcaSettings' in properties:
            self._settings = properties['_pymcaSettings']

    def process(self, xas_obj):
        """

        :param xas_obj: object containing the configuration and spectrum to process
        :type: Union[XASBase, dict]. If is a dict, should contain configuration or
                                     spectrum keys. Otherwise is simply the spectrum
        :return: spectrum dict
        :rtype: dict
        """
        if isinstance(xas_obj, dict):
            _xas_obj = PyMcaXAS().load_frm_dict(xas_obj)
        else:
            _xas_obj = xas_obj
        if self._settings:
            _xas_obj.configuration['Normalization'] = self._settings

        return pymca_normalization(_xas_obj)

    __call__ = process