-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathconverter.py
255 lines (195 loc) · 8.36 KB
/
converter.py
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
# coding: utf8
from __future__ import absolute_import, division, print_function
from builtins import super, range, zip, round, map
#Imports
import logging
import importlib
import os
import datetime
import traceback
from ditto.store import Store
logger = logging.getLogger(__name__)
class converter:
'''Converter class. Use to convert from one format to another through DiTTo.
**Usage:**
>>> converter=converter(feeder_list, from_format, to_format, verbose)
:param feeder_list: List of feeder names
:type feeder_list: List[str]
:param from_format: Format to use as input
:type from_format: str
:param to_format: Format to use as ouput
:type to_format: str
:param verbose: Set verbose mode
:type verbose: bool
.. note::
- Feeders should be located in ./inputs/{from_format}/{feeder_name}
- Output will be located in ./outputs/{from_format}/{to_format}/{feeder_name}
- Readers should be located in ditto/reader/{from_format}/read.py
- Writers should be located in ditto/writers/{to_format}/write.py
.. warnings::
- Names should be consistent (be carefull with lower/upper case...)
- Readers should have a parse method responsible for parsing and a consistent constructor
- Writers should have a write method responsible for parsing and a consistent constructor
Author: Nicolas Gensollen. October 2017
'''
def __init__(self, feeder_list, from_format, to_format, verbose=True):
'''Converter class CONSTRUCTOR.
'''
#ATHORIZED FORMAT
#
#We authorized everything in ditto/readers for readers
#and everything in ditto/writers for writers
try:
authorized_format_reader = next(os.walk('../readers'))[1] #Carefull, relative path...
except:
raise ValueError('Unable to get reader format list in ../readers')
try:
authorized_format_writer = next(os.walk('../writers'))[1] #Carefull, relative path...
except:
raise ValueError('Unable to get writer format list in ../writers')
#FROM_FORMAT
#
#Check that from_format is known
if from_format not in authorized_format_reader:
raise ValueError('Unknown format {}'.format(from_format))
else:
#Import the right reader
try:
self.reader_class = importlib.import_module('ditto.readers.{format}.read'.format(format=from_format))
except:
traceback.print_exc()
raise ValueError('Unable to import ditto.reader.{format}.read'.format(format=from_format))
self._from = from_format
#TO_FORMAT
#
#Check that to_format is known
if to_format not in authorized_format_writer:
raise ValueError('Unknown format {}'.format(to_format))
else:
#Import the right writer
try:
self.writer_class = importlib.import_module('ditto.writers.{format}.write'.format(format=to_format))
except:
traceback.print_exc()
raise ValueError('Unable to import ditto.writers.{format}.write'.format(format=to_format))
self._to = to_format
self.feeder_list = feeder_list
self.verbose = verbose
#Create a store object
try:
self.m = Store()
except:
raise ValueError('Unable to create Store object.')
#Set time format for log files
self.time_format = '%H_%M_%d_%m_%Y'
def get_inputs(self, feeder):
'''Configure Inputs.
'''
#Inputs are different accross the format:
#
#OpenDSS
#
if self._from == 'opendss':
inputs = {
'master_file': './inputs/{format}/{feeder}/master.dss'.format(format=self._from, feeder=feeder),
'buscoordinates_file': './inputs/{format}/{feeder}/buscoords.dss'.format(format=self._from, feeder=feeder),
}
#CYME
#
elif self._from == 'cyme':
inputs = {
'data_folder_path': './inputs/{format}/{feeder}/'.format(format=self._from, feeder=feeder),
'network_filename': 'network.txt',
'equipment_filename': 'equipment.txt',
'load_filename': 'loads.txt'
}
#GRIDLABD
#
elif self._from == 'gridlabd':
inputs = {'input_file': './inputs/{format}/{feeder}/{feeder}.glm'.format(format=self._from, feeder=feeder)}
#DEW
#
elif self._from == 'dew':
inputs = {
'input_file_path': './inputs/{format}/{feeder}/{feeder}.dew'.format(format=self._from, feeder=feeder),
'databasepath': '../readers/dew/DataBase/DataBase.xlsx'
}
else:
raise NotImplementedError('Format {} not imlemented.'.format(self._from))
#Add log information
log_path = './logs/reader/{format}/{feeder}/'.format(format=self._from, feeder=feeder)
#If log path does not exist, create it
if not os.path.exists(log_path):
self.build_path(log_path)
#Add filename to the log path
log_path += '/log_{time}.log'.format(time=self.current_time_string)
inputs.update({'log_file': log_path})
return inputs
def build_path(self, path):
'''Take a path as input and check that all folders exists as in the path.
If folders are missing, they are created.
.. warning:: Expects a path in the form './folder1/folder2/folder3/'
'''
#First we need to get all the directories in the given path
dirs = path.split('/')
#Loop over the directories and check for existance
for k, _dir in enumerate(dirs):
if k != 0:
_tmp = reduce(lambda x, y: x + '/' + y, dirs[:k])
if not os.path.exists(_tmp):
os.makedirs(_tmp)
#Test that path exists
if not os.path.exists(path):
raise ValueError('Unable to create path {path}'.format(path=path))
def get_output(self, feeder):
'''Configure outputs.
'''
output_path = './outputs/from_{format_from}/to_{format_to}/{feeder}/'.format(format_from=self._from, format_to=self._to, feeder=feeder)
#If path does not exist yet, create it
if not os.path.exists(output_path):
self.build_path(output_path)
#Add log information
log_path = './logs/writer/{format}/{feeder}/'.format(format=self._to, feeder=feeder)
#If log path does not exist, create it
if not os.path.exists(log_path):
self.build_path(log_path)
#Add filename to log path\
log_path += '/log_{time}.log'.format(time=self.current_time_string)
return {'output_path': output_path, 'log_file': log_path}
def configure_reader(self, inputs):
'''Configure the reader.
'''
try:
self.reader = self.reader_class.reader(**inputs)
except:
traceback.print_exc()
raise ValueError('Unable to instanciate reader.')
def configure_writer(self, output):
'''Configure the writer.
'''
try:
self.writer = self.writer_class.writer(**output)
except:
traceback.print_exc()
raise ValueError('Unable to instanciate writer.')
def convert(self):
'''Run the conversion: from_format--->DiTTo--->to_format on all the feeders in feeder_list.
'''
#Get the time for the log files (all log files created during the same call to convert()
#will have the same timestamp which makes it easier to analyse them later)
self.current_time_string = datetime.datetime.now().strftime(self.time_format)
for feeder in self.feeder_list:
if self.verbose:
logger.debug('*' * 60)
logger.debug(feeder)
logger.debug('*' * 60)
if feeder in os.listdir('./inputs/{format}/'.format(format=self._from)):
inputs = self.get_inputs(feeder)
self.configure_reader(inputs)
output = self.get_output(feeder)
self.configure_writer(output)
self.reader.parse(self.m)
self.writer.write(self.m, verbose=self.verbose)
else:
logger.debug('Input files not available for feeder {feeder} and format {format}'.format(feeder=feeder, format=self._from))
logger.debug('Skip...')