def open_h5(file_path, text_func=None, prog_func=None): """ :param file_path: :param text_func: :param prog_func: :return: """ store = pd.HDFStore(file_path) dfs = dict() n = len(list(store.root)) i = 0 for group in store.root: if text_func: text_func('Loading ' + group._v_name + '...') dfs[group._v_name] = pd.read_hdf(store, group._v_pathname) if prog_func: prog_func((i + 1) / n * 100) i += 1 store.close() circuit = data_frames_to_circuit(dfs) return circuit
def open(self, text_func=None, progress_func=None): """ Load GridCal compatible file :param text_func: pointer to function that prints the names :param progress_func: pointer to function that prints the progress 0~100 :return: logger with information """ self.logger = Logger() if os.path.exists(self.file_name): name, file_extension = os.path.splitext(self.file_name) # print(name, file_extension) if file_extension.lower() in ['.xls', '.xlsx']: data_dictionary = load_from_xls(self.file_name) # Pass the table-like data dictionary to objects in this circuit if 'version' not in data_dictionary.keys(): interpret_data_v1(self.circuit, data_dictionary) elif data_dictionary['version'] == 2.0: interprete_excel_v2(self.circuit, data_dictionary) elif data_dictionary['version'] == 3.0: interpret_excel_v3(self.circuit, data_dictionary) elif data_dictionary['version'] == 4.0: self.circuit = data_frames_to_circuit(data_dictionary) else: self.logger.add('The file could not be processed') elif file_extension.lower() == '.gridcal': # open file content data_dictionary = open_data_frames_from_zip( self.file_name, text_func=text_func, progress_func=progress_func) # interpret file content self.circuit = data_frames_to_circuit(data_dictionary) elif file_extension.lower() == '.dgs': circ = dgs_to_circuit(self.file_name) self.circuit.buses = circ.buses self.circuit.branches = circ.branches self.circuit.assign_circuit(circ) elif file_extension.lower() == '.m': circ = parse_matpower_file(self.file_name) self.circuit.buses = circ.buses self.circuit.branches = circ.branches self.circuit.assign_circuit(circ) elif file_extension.lower() == '.dpx': circ, log = load_dpx(self.file_name) self.circuit.buses = circ.buses self.circuit.branches = circ.branches self.circuit.assign_circuit(circ) self.logger += log elif file_extension.lower() == '.json': # the json file can be the GridCAl one or the iPA one... data = json.load(open(self.file_name)) if type(data) == dict(): if 'Red' in data.keys(): circ = load_iPA(self.file_name) self.circuit.buses = circ.buses self.circuit.branches = circ.branches self.circuit.assign_circuit(circ) else: self.logger.append('Unknown json format') elif type(data) == list(): circ = parse_json(self.file_name) self.circuit.buses = circ.buses self.circuit.branches = circ.branches self.circuit.assign_circuit(circ) else: self.logger.append('Unknown json format') elif file_extension.lower() == '.raw': parser = PSSeParser(self.file_name) circ = parser.circuit self.circuit.buses = circ.buses self.circuit.branches = circ.branches self.circuit.assign_circuit(circ) self.logger += parser.logger elif file_extension.lower() == '.xml': parser = CIMImport() circ = parser.load_cim_file(self.file_name) self.circuit.assign_circuit(circ) self.logger += parser.logger else: # warn('The file does not exist.') self.logger.append(self.file_name + ' does not exist.') return self.circuit
def open(self, text_func=None, progress_func=None): """ Load GridCal compatible file :param text_func: pointer to function that prints the names :param progress_func: pointer to function that prints the progress 0~100 :return: logger with information """ self.logger = Logger() if isinstance(self.file_name, list): for f in self.file_name: name, file_extension = os.path.splitext(f) if file_extension.lower() not in ['.xml', '.zip']: raise Exception( 'Loading multiple files that are not XML/Zip (xml or zip is for CIM)' ) parser = CIMImport(text_func=text_func, progress_func=progress_func) self.circuit = parser.load_cim_file(self.file_name) self.logger += parser.logger else: if os.path.exists(self.file_name): name, file_extension = os.path.splitext(self.file_name) # print(name, file_extension) if file_extension.lower() in ['.xls', '.xlsx']: data_dictionary = load_from_xls(self.file_name) # Pass the table-like data dictionary to objects in this circuit if 'version' not in data_dictionary.keys(): interpret_data_v1(self.circuit, data_dictionary) elif data_dictionary['version'] == 2.0: interprete_excel_v2(self.circuit, data_dictionary) elif data_dictionary['version'] == 3.0: interpret_excel_v3(self.circuit, data_dictionary) elif data_dictionary['version'] == 4.0: if data_dictionary is not None: self.circuit = data_frames_to_circuit( data_dictionary) else: self.logger.add("Error while reading the file :(") return None else: self.logger.add('The file could not be processed') elif file_extension.lower() == '.gridcal': # open file content data_dictionary = get_frames_from_zip( self.file_name, text_func=text_func, progress_func=progress_func, logger=self.logger) # interpret file content if data_dictionary is not None: self.circuit = data_frames_to_circuit(data_dictionary) else: self.logger.add("Error while reading the file :(") return None elif file_extension.lower() == '.sqlite': # open file content data_dictionary = open_data_frames_from_sqlite( self.file_name, text_func=text_func, progress_func=progress_func) # interpret file content if data_dictionary is not None: self.circuit = data_frames_to_circuit(data_dictionary) else: self.logger.add("Error while reading the file :(") return None elif file_extension.lower() == '.dgs': self.circuit = dgs_to_circuit(self.file_name) elif file_extension.lower() == '.gch5': self.circuit = open_h5(self.file_name, text_func=text_func, prog_func=progress_func) elif file_extension.lower() == '.m': self.circuit, log = parse_matpower_file(self.file_name) self.logger += log elif file_extension.lower() == '.dpx': self.circuit, log = load_dpx(self.file_name) self.logger += log elif file_extension.lower() == '.json': # the json file can be the GridCal one or the iPA one... data = json.load(open(self.file_name)) if isinstance(data, dict): if 'Red' in data.keys(): self.circuit = load_iPA(self.file_name) elif sum([ x in data.keys() for x in [ 'version', 'software', 'units', 'devices', 'profiles' ] ]) == 5: # version 2 of the json parser version = int(float(data['version'])) if version == 2: self.circuit = parse_json_data_v2( data, self.logger) elif version == 3: self.circuit = parse_json_data_v3( data, self.logger) else: self.logger.add_error( 'Recognised as a gridCal compatible Json ' 'but the version is not supported') else: self.logger.add_error('Unknown json format') elif type(data) == list(): self.circuit = parse_json(self.file_name) else: self.logger.add_error('Unknown json format') elif file_extension.lower() == '.raw': parser = PSSeParser(self.file_name) self.circuit = parser.circuit self.logger += parser.logger elif file_extension.lower() in ['.xml', '.zip']: parser = CIMImport(text_func=text_func, progress_func=progress_func) self.circuit = parser.load_cim_file( self.file_name) # file_name might be a list of files self.logger += parser.logger else: # warn('The file does not exist.') self.logger.add_error('Does not exist', self.file_name) return self.circuit
text_func('select * from ' + key) dfs[key] = pd.read_sql('select * from ' + key, conn) # parse the configuration dfs = parse_config_df(dfs['config'], dfs) return dfs if __name__ == '__main__': import time from GridCal.Engine.IO.file_handler import * from GridCal.Engine.IO.pack_unpack import create_data_frames, data_frames_to_circuit # fname = '/home/santi/Documentos/GitHub/GridCal/Grids_and_profiles/grids/1354 Pegase.xlsx' fname = '/home/santi/Documentos/GitHub/GridCal/Grids_and_profiles/grids/IEEE39.gridcal' a = time.time() circuit_ = FileOpen(fname).open() print('native based open:', time.time() - a) print('Saving .sqlite ...') dfs = dfs = create_data_frames(circuit=circuit_) save_data_frames_to_sqlite(dfs, file_path=circuit_.name + '.sqlite') a = time.time() data = open_data_frames_from_sqlite(circuit_.name + '.sqlite') circuit2 = data_frames_to_circuit(data) print('sql based open:', time.time() - a)