Skip to content

Commit

Permalink
Add data export functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
fwitte committed Apr 26, 2024
1 parent 4296abc commit b3d8535
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 6 deletions.
8 changes: 8 additions & 0 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -438,4 +438,12 @@ planned in future versions of TESPy.

The values for plotting must be passed in the diagrams unit system.

Export the underlying data
^^^^^^^^^^^^^^^^^^^^^^^^^^^
You can export the underlying data in :code:`json` format:

.. code-block:: python
>>> diagram.to_json("diagram.json")
.. [1] Witte, F.; Tuschy, I. (2020). TESPy: Thermal Engineering Systems in Python. Journal of Open Source Software, 5(49), 2178, https://doi.org/10.21105/joss.02178.
64 changes: 58 additions & 6 deletions src/fluprodia/fluid_property_diagram.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@
SPDX-License-Identifier: MIT
"""
import json
import os

import CoolProp as CP
import numpy as np

from fluprodia import __version__


def _beautiful_unit_string(unit):
r"""Convert unit fractions to latex.
Expand Down Expand Up @@ -150,6 +154,10 @@ class FluidPropertyDiagram:
... )
>>> plt.tight_layout()
>>> fig.savefig('Ts_Diagramm.pdf')
It is also possible to export the data of a diagram:
>>> diagram.to_json("./tmp/water.json")
"""

def __init__(self, fluid):
Expand Down Expand Up @@ -597,6 +605,29 @@ def _isentropic(self):
iterator, np.ones(len(iterator)) * s
)

def to_json(self, path):
data = {
prop: {
f"{isoline}": {
subprop: list(getattr(self, prop)[isoline][subprop].astype(float))
for subprop in getattr(self, prop)[isoline]
} for isoline in getattr(self, prop)["isolines"]
} for prop in self.properties.values()
}

data["META"] = {
"fluid": self.fluid,
"CoolProp-version": CP.__version__,
"fluprodia-version": __version__
}

directory = os.path.dirname(path)
if directory != "" and not os.path.exists(directory):
os.makedirs(directory)

with open(path, "w", encoding="utf-8") as f:
f.write(json.dumps(data, indent=2))

def calc_individual_isoline(
self, isoline_property=None,
isoline_value=None,
Expand Down Expand Up @@ -996,17 +1027,35 @@ def _insert_Q_crossings(self, datapoints, property, data):

return datapoints

def draw_isolines(self, fig, ax, diagram_type, x_min, x_max, y_min, y_max, isoline_data={}):
"""Draw the isolines of a specific diagram type.
def draw_isolines(self, fig, ax, diagram_type, x_min, x_max, y_min, y_max, isoline_data=None):
"""_summary_
Parameters
----------
fig : matplotlib.pyplot.figure
Figure to draw into
ax : matplotlib.pyplot.axes
Axes to draw into
diagram_type : str
Which type of diagram should be drawn.
Name of the diagram
x_min : number
Minimum for x range
isoline_data : dict
Dictionary holding additional data on the isolines to be drawn.
These are
x_max : number
Maximum for x range
y_min : number
Minimum for y range
y_max : number
Maximum for y range
isoline_data : dict, optional
Dictionary holding additional data on the isolines to be drawn,
by default None. These are
- the isoline values with key :code:`values` and
- the isoline style with key :code:`style`.
Expand All @@ -1016,6 +1065,9 @@ def draw_isolines(self, fig, ax, diagram_type, x_min, x_max, y_min, y_max, isoli
https://matplotlib.org/api/_as_gen/matplotlib.lines.Line2D.html#matplotlib.lines.Line2D
for more information.
"""
if isoline_data is None:
isoline_data = {}

self._check_diagram_types(diagram_type)

x_scale = self.supported_diagrams[diagram_type]['x_scale']
Expand Down

0 comments on commit b3d8535

Please sign in to comment.