Example #1
0
def open_sqlite(file_path):

    circuit = MultiCircuit()

    # make connection
    conn = sqlite3.connect(file_path)

    dfs = dict()

    # get the table names
    tables = conn.execute("SELECT name FROM sqlite_master WHERE type='table';")

    names = [t[0] for t in tables]

    check_names(names)

    for key in names:
        dfs[key] = pd.read_sql('select * from ' + key, conn)

    df = dfs['config']
    idx = df['Property'][df['Property'] == 'BaseMVA'].index
    if len(idx) > 0:
        dfs["baseMVA"] = np.double(df.values[idx, 1])
    else:
        dfs["baseMVA"] = 100

    idx = df['Property'][df['Property'] == 'Version'].index
    if len(idx) > 0:
        dfs["version"] = np.double(df.values[idx, 1])

    idx = df['Property'][df['Property'] == 'Name'].index
    if len(idx) > 0:
        dfs["name"] = df.values[idx[0], 1]
    else:
        dfs["name"] = 'Grid'

    idx = df['Property'][df['Property'] == 'Comments'].index
    if len(idx) > 0:
        dfs["Comments"] = df.values[idx[0], 1]
    else:
        dfs["Comments"] = ''

    # fill circuit data
    interpret_excel_v3(circuit, dfs)

    return circuit
Example #2
0
    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
Example #3
0
    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
        """
        logger = list()

        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)
                    # return self.circuit
                elif data_dictionary['version'] == 2.0:
                    interprete_excel_v2(self.circuit, data_dictionary)
                    # return self.circuit
                elif data_dictionary['version'] == 3.0:
                    interpret_excel_v3(self.circuit, data_dictionary)
                    # return self.circuit
                else:
                    warn('The file could not be processed')
                    # return self.circuit

            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
                interpret_excel_v3(self.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, logger = load_dpx(self.file_name)
                self.circuit.buses = circ.buses
                self.circuit.branches = circ.branches
                self.circuit.assign_circuit(circ)

            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:
                        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:
                    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)
                logger = parser.logger

            elif file_extension.lower() == '.xml':
                parser = CIMImport()
                circ = parser.load_cim_file(self.file_name)
                self.circuit.assign_circuit(circ)
                logger = parser.logger

        else:
            warn('The file does not exist.')
            logger.append(self.file_name + ' does not exist.')

        self.logger = logger

        return self.circuit