Example #1
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
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 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 = open_data_frames_from_zip(
                    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() == '.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() == '.m':
                self.circuit = parse_matpower_file(self.file_name)

            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)
                        else:
                            self.logger.append(
                                'Recognised as a gridCal compatible Json but the version is not supported'
                            )
                    else:
                        self.logger.append('Unknown json format')

                elif type(data) == list():
                    self.circuit = parse_json(self.file_name)

                else:
                    self.logger.append('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() == '.xml':
                parser = CIMImport()
                self.circuit = parser.load_cim_file(self.file_name)
                self.logger += parser.logger

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

        return self.circuit