io.py 4.24 KB
Newer Older
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
# 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 .process import Process
from est.core.types import XASObject
33
from est.io.utils.information import InputInformation
34
35
36
37
import pkg_resources


class DumpXasObject(Process):
38

39
    _INPUT_NAMES = set(["xas_obj"])
40

41
    _OUTPUT_NAMES = set(["result"])
42

43
44
    def __init__(self, varinfo=None, **inputs):
        super().__init__(name="dump xas object", varinfo=varinfo, **inputs)
45
        self._output_file = varinfo.get("output_file", None)
46
47
48
49
50
51
52
53
54

    @property
    def output_file(self):
        return self._output_file

    @output_file.setter
    def output_file(self, output_file):
        self._output_file = output_file

55
    def program_name(self):
56
57
58
59
60
61
62
63
64
65
66
        """Name of the program used for this processing"""
        return "xas writer"

    def program_version(self):
        """version of the program used for this processing"""
        return pkg_resources.get_distribution("est").version

    def definition(self):
        """definition of the process"""
        return "write xas object to a file"

67
68
    def run(self):
        xas_object = self.inputs.xas_obj
69
        if xas_object is None:
70
            raise ValueError("xas_obj should be provided")
71
72
73
74
75
76
77
78
79
80
81
        if isinstance(xas_object, dict):
            xas_object = XASObject.from_dict(xas_object)
        if not isinstance(xas_object, XASObject):
            raise TypeError(
                "xas_object should be a convertable dict or an" "instance of XASObject"
            )

        if self.output_file is None:
            raise ValueError("output file not provided")
        else:
            xas_object.dump(self.output_file)
82
        self.outputs.result = self.output_file
83
84
85
86
87
88

    def set_properties(self, properties):
        self.setConfiguration(properties)

    def setConfiguration(self, configuration):
        super().setConfiguration(configuration=configuration)
89
90
91
92
93
94
95
        self._read_output_file()

    def update_properties(self, properties):
        super().update_properties(properties=properties)
        self._read_output_file()

    def _read_output_file(self):
96
97
98
99
100
        if "output_file" in self._settings:
            self.output_file = self._settings["output_file"]
        elif "_output_file_setting" in self._settings:
            self.output_file = self._settings["_output_file_setting"]

101
    __call__ = run
102
103
104
105


class ReadXasObject(Process):

106
107
    _INPUT_NAMES = set()

108
    _OUTPUT_NAMES = set(["xas_obj"])
109
110
111

    def __init__(self, varinfo=None, **inputs):
        super().__init__(name="read xas", varinfo=varinfo, **inputs)
112
113
114
        if "input_information" not in varinfo:
            raise ValueError("Input information should be provided")
        self.setConfiguration(varinfo["input_information"])
115

116
    def run(self):
117
118
119
120
121
        from est.core.io import XASReader

        config = self.getConfiguration()
        input_information = InputInformation.from_dict(config)
        xas_obj = XASReader.read_frm_url(input_information)
122
        self.outputs.xas_obj = xas_obj.to_dict()
123
        return xas_obj