1
|
|
# Copyright (c) 2020 by Fraunhofer Institute for Energy Economics
|
2
|
|
# and Energy System Technology (IEE), Kassel. All rights reserved.
|
3
|
|
# Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
|
4
|
|
|
5
|
1
|
import json
|
6
|
1
|
import os
|
7
|
1
|
import pickle
|
8
|
1
|
from functools import partial
|
9
|
|
|
10
|
1
|
from pandapipes.create import create_empty_network
|
11
|
1
|
from pandapipes.io.convert_format import convert_format
|
12
|
1
|
from pandapipes.io.io_utils import isinstance_partial, FromSerializableRegistryPpipe
|
13
|
1
|
from pandapipes.pandapipes_net import pandapipesNet
|
14
|
1
|
from pandapower.io_utils import PPJSONEncoder, to_dict_with_coord_transform, \
|
15
|
|
get_raw_data_from_pickle, transform_net_with_df_and_geo, PPJSONDecoder
|
16
|
1
|
from pandapower.io_utils import pp_hook
|
17
|
|
|
18
|
|
|
19
|
1
|
def to_pickle(net, filename):
|
20
|
|
"""
|
21
|
|
Saves a pandapipes Network with the pickle library.
|
22
|
|
|
23
|
|
:param net: The pandapipes Network to save.
|
24
|
|
:type net: pandapipesNet
|
25
|
|
:param filename: The absolute or relative path to the output file or a writable file-like object
|
26
|
|
:type filename: str, file-object
|
27
|
|
:return: No output.
|
28
|
|
|
29
|
|
:Example:
|
30
|
|
>>> pandapipes.to_pickle(net, os.path.join("C:", "example_folder", "example1.p")) # absolute path
|
31
|
|
>>> pandapipes.to_pickle(net, "example2.p") # relative path
|
32
|
|
|
33
|
|
"""
|
34
|
1
|
if hasattr(filename, 'write'):
|
35
|
0
|
pickle.dump(dict(net), filename, protocol=2)
|
36
|
0
|
return
|
37
|
1
|
if not filename.endswith(".p"):
|
38
|
0
|
raise Exception("Please use .p to save pandapipes networks!")
|
39
|
1
|
save_net = to_dict_with_coord_transform(net, ["junction_geodata"], ["pipe_geodata"])
|
40
|
|
|
41
|
1
|
with open(filename, "wb") as f:
|
42
|
1
|
pickle.dump(save_net, f, protocol=2) # use protocol 2 for py2 / py3 compatibility
|
43
|
|
|
44
|
|
|
45
|
1
|
def to_json(net, filename=None):
|
46
|
|
"""
|
47
|
|
Saves a pandapipes Network in JSON format. The index columns of all pandas DataFrames will be
|
48
|
|
saved in ascending order. net elements which name begins with "_" (internal elements) will not
|
49
|
|
be saved. Std types will also not be saved.
|
50
|
|
|
51
|
|
:param net: The pandapipes Network to save.
|
52
|
|
:type net: pandapipesNet
|
53
|
|
:param filename: The absolute or relative path to the output file or a writable file-like \
|
54
|
|
object. If None, a JSON string is returned.
|
55
|
|
:type filename: str, file-object, default None
|
56
|
|
:return: JSON string of the Network (only if filename is None)
|
57
|
|
|
58
|
|
:Example:
|
59
|
|
|
60
|
|
>>> pandapipes.to_json(net, "example.json")
|
61
|
|
|
62
|
|
"""
|
63
|
1
|
if filename is None:
|
64
|
1
|
return json.dumps(net, cls=PPJSONEncoder, indent=2, isinstance_func=isinstance_partial)
|
65
|
1
|
if hasattr(filename, 'write'):
|
66
|
0
|
json.dump(net, fp=filename, cls=PPJSONEncoder, indent=2, isinstance_func=isinstance_partial)
|
67
|
|
else:
|
68
|
1
|
with open(filename, "w") as fp:
|
69
|
1
|
json.dump(net, fp=fp, cls=PPJSONEncoder, indent=2, isinstance_func=isinstance_partial)
|
70
|
|
|
71
|
|
|
72
|
1
|
def from_pickle(filename):
|
73
|
|
"""
|
74
|
|
Load a pandapipes format Network from pickle file.
|
75
|
|
|
76
|
|
:param filename: The absolute or relative path to the input file or file-like object
|
77
|
|
:type filename: str, file-object
|
78
|
|
:return: net - The pandapipes Network which was saved as pickle
|
79
|
|
:rtype: pandapipesNet
|
80
|
|
|
81
|
|
:Example:
|
82
|
|
|
83
|
|
>>> net1 = pandapipes.from_pickle(os.path.join("C:", "example_folder", "example1.p"))
|
84
|
|
>>> net2 = pandapipes.from_pickle("example2.p") #relative path
|
85
|
|
|
86
|
|
"""
|
87
|
1
|
net = pandapipesNet(get_raw_data_from_pickle(filename))
|
88
|
1
|
transform_net_with_df_and_geo(net, ["junction_geodata"], ["pipe_geodata"])
|
89
|
1
|
return net
|
90
|
|
|
91
|
|
|
92
|
1
|
def from_json(filename, convert=True):
|
93
|
|
"""
|
94
|
|
Load a pandapipes network from a JSON file or string.
|
95
|
|
The index of the returned network is not necessarily in the same order as the original network.
|
96
|
|
Index columns of all pandas DataFrames are sorted in ascending order.
|
97
|
|
|
98
|
|
:param filename: The absolute or relative path to the input file or file-like object
|
99
|
|
:type filename: str, file-object
|
100
|
|
:param convert: whether or not to convert the format from earlier versions
|
101
|
|
:type convert: bool
|
102
|
|
:return: net - The pandapipes network that was saved as JSON
|
103
|
|
:rtype: pandapipesNet
|
104
|
|
|
105
|
|
:Example:
|
106
|
|
|
107
|
|
>>> net = pandapipes.from_json("example.json")
|
108
|
|
|
109
|
|
"""
|
110
|
1
|
if hasattr(filename, 'read'):
|
111
|
0
|
json_string = filename.read()
|
112
|
1
|
elif not os.path.isfile(filename):
|
113
|
0
|
raise UserWarning("File {} does not exist!!".format(filename))
|
114
|
|
else:
|
115
|
1
|
with open(filename) as fp:
|
116
|
1
|
json_string = fp.read()
|
117
|
1
|
return from_json_string(json_string, convert=convert)
|
118
|
|
|
119
|
|
|
120
|
1
|
def from_json_string(json_string, convert=False):
|
121
|
|
"""
|
122
|
|
Load a pandapipes network from a JSON string.
|
123
|
|
The index of the returned network is not necessarily in the same order as the original network.
|
124
|
|
Index columns of all pandas DataFrames are sorted in ascending order.
|
125
|
|
|
126
|
|
:param json_string: The JSON string representation of the network
|
127
|
|
:type json_string: str
|
128
|
|
:param convert: whether or not to convert the format from earlier versions
|
129
|
|
:type convert: bool
|
130
|
|
:return: net - The pandapipes network that was contained in the JSON string
|
131
|
|
:rtype: pandapipesNet
|
132
|
|
|
133
|
|
:Example:
|
134
|
|
|
135
|
|
>>> net = pandapipes.from_json_string(json_str)
|
136
|
|
|
137
|
|
"""
|
138
|
1
|
net = json.loads(json_string, cls=PPJSONDecoder, object_hook=partial(pp_hook,
|
139
|
|
registry_class=FromSerializableRegistryPpipe))
|
140
|
|
|
141
|
1
|
if convert:
|
142
|
1
|
convert_format(net)
|
143
|
1
|
return net
|