def __init__(self, parent):
        ExportModels.dlg_ServeToWEAP.__init__(self, parent)
        try:
            if not DB_Setup().get_session():
                msg = "\n\nWarning: Please connect to sqlite first."
                raise Exception(msg)
            ''' init combo model'''
            self.dataStructure = GetResourceStructure()
            self.datasets = self.dataStructure.GetResourceType()
            list_acromy = list()
            for index, row in self.datasets.iterrows():
                list_acromy.append(row[0])
            if list_acromy.__len__() > 0:
                self.Select_Model.SetItems(list_acromy)

            list_code = []
            self.WEAP = win32com.client.Dispatch("WEAP.WEAPApplication")
            for area in self.WEAP.Areas:
                list_code.append(area.name)

            self.comboBox_SelectArea.SetItems(list_code)

            list_scen = []
            for scen in self.WEAP.Scenarios:
                list_scen.append(scen.name)

            self.combo_selectWEAPScenario.SetItems(list_scen)
        except Exception as e:
            message = msg_somethigWrong(None, msg=e.message)
            message.Show()
            self.Destroy()
 def __init__(self, parent):
     WaMDaMWizard.dlg_ExportScenarioDataToExcel.__init__(self, parent)
     self.path = ''
     try:
         if not DB_Setup().get_session():
             msg = "\n\nWarning: Please connect to sqlite first."
             raise Exception(msg)
         ''' init combo model'''
         self.dataStructure = GetResourceStructure()
         self.datasets = self.dataStructure.GetResourceType()
         list_acromy = list()
         for index, row in self.datasets.iterrows():
             list_acromy.append(row[0])
         if list_acromy.__len__() > 0:
             self.comboBox_selectModel.SetItems(list_acromy)
     except Exception as e:
         message = msg_somethigWrong(None, msg=e.message)
         message.Show()
         self.Destroy()
    def __init__(self, parent):
        viewer.WaMDaMWizard.dlg_query_model.__init__(self, parent)
        self.path = ''
        try:
            if not DB_Setup().get_session():
                msg = "\n\nWarning: Please connect to sqlite first."
                raise Exception(msg)

            self.dataStructure = GetResourceStructure()
            self.datasets = self.dataStructure.GetResourceType()
            list_acromy = list()
            for row in self.datasets.values:
                list_acromy.append(row[0])
            if list_acromy.__len__() > 0:
                self.comboBox_selectModel.SetItems(list_acromy)
        except Exception as e:
            message = msg_somethigWrong(None, msg=e.message)
            message.ShowModal()
            self.Destroy()
Esempio n. 4
0
    def __init__(self, parent):
        WaMDaMWizard.dlg_compare_scenarios.__init__(self, parent)

        self.path = ''
        try:
            if not self.checkConnectingToSqlite():
                msg = "\n\nWarning: Please connect to sqlite first."
                raise Exception(msg)
            ''' init model combobox'''
            self.dataStructure = GetResourceStructure()
            self.instances = GetInstancesBySenario()
            self.datasets = self.dataStructure.GetResourceType()
            self.compareScenarios = GetComapreScenarios()

            list_acromy = list()
            for index, row in self.datasets.iterrows():
                list_acromy.append(row["ResourceTypeAcronym"])
            if list_acromy.__len__() > 0:
                self.comboBox_selectModel.SetItems(list_acromy)
        except Exception as e:
            message = msg_somethigWrong(None, msg=e.message)
            message.ShowModal()
            self.Destroy()
Esempio n. 5
0
    def Btn_LoginOnButtonClick(self, event):
        # TODO: Implement Btn_LoginOnButtonClick

        try:
            self.userName = self.m_textCtrl11.Value
            self.password = self.m_textCtrl12.Value

            if not self.m_textCtrl11.Value or not self.m_textCtrl12.Value:
                raise Exception("Provide a username and password")

            self.usern = self.m_textCtrl11.Value
            self.passw = self.m_textCtrl12.Value

            projects = self.hydra()._call('get_projects')
            project_names = []

            for p in projects:
                project_names.append(str(p['name']))

            self.m_SelectProject.SetItems(project_names)

            self.dataStructure = GetResourceStructure()

            self.datasets = self.dataStructure.GetResourceType()

            list_acromy = list()

            for index, row in self.datasets.iterrows():
                list_acromy.append(row[0])

            if list_acromy.__len__() > 0:
                self.m_SelectModel.SetItems(list_acromy)

        except Exception as e:
            messageDlg = msg_somethigWrong(None, msg=e.args[0])
            messageDlg.Show()
    def btn_Export_ScenarioDataOnButtonClick(self, event):
        selectedDataset = self.comboBox_selectModel.Value
        selectedMasterNetworkName = self.comboBox_selectNetwork.Value
        selectedScenarioName = self.comboBox_selectScenario.Value

        # Check whether user select needed items correctly
        message = ''
        if (selectedDataset == None or selectedDataset == ''):
            message = 'Select the resource type (e.g, model name) in WamDam.'
        elif selectedMasterNetworkName == None or selectedMasterNetworkName == '':
            message = 'Select the MasterNetworkName.'
        elif selectedScenarioName == None or selectedScenarioName == '':
            message = 'Select the ScenarioName.'
        elif not ['xls', 'xlsx', 'xlsm', 'xls'].__contains__(
                self.path.split('.')[-1]):
            message = 'Please provide a directory for the output excel file.'
        if message != '':
            messageDlg = msg_somethigWrong(None, msg=message)
            messageDlg.ShowModal()
            return

        print 'this is done'

        try:
            self.btn_Export_ScenarioData.Enabled = False
            exportTemplate = ExportTemplate(self.path)

            ####################################################

            # Rescour Type structure

            ####################################################

            ResourceType_Result_df = GetResourceStructure()
            ObjectTypes_Result_df = GetResourceStructure()
            Attributes_Result_df = GetResourceStructure()

            resources_result = ResourceType_Result_df.GetResourceType(
                selectedDataset)
            data_result = ObjectTypes_Result_df.GetObjectTypesByResource(
                selectedDataset)
            exportTemplate.exportResourcesType(resources_result, data_result)

            # data_result = ObjectTypes_Result_df.GetObjectTypesByResource(selectedDataset)
            # exportTemplate.exportObjecttypes(data_result)

            attributes_result = Attributes_Result_df.GetAttributesByResource(
                selectedDataset)
            print "Count of attrs data: {}".format(str(len(attributes_result)))
            exportTemplate.exportAttributes(attributes_result)

            ####################################################

            # Metadata

            ####################################################

            Organizations_Result_df = GetMetadataByScenario()
            People_Result_df = GetMetadataByScenario()
            Sources_Result_df = GetMetadataByScenario()
            Methods_Result_df = GetMetadataByScenario()

            organization_result = Organizations_Result_df.GetOrganizationsByScenario(
                selectedDataset, selectedMasterNetworkName,
                selectedScenarioName)
            exportTemplate.exportOrganizations(organization_result)

            people_result = People_Result_df.GetPeopleByScenario(
                selectedDataset, selectedMasterNetworkName,
                selectedScenarioName)
            exportTemplate.exportPeople(people_result)

            source_result = Sources_Result_df.GetSourcesByScenario(
                selectedDataset, selectedMasterNetworkName,
                selectedScenarioName)
            exportTemplate.exportSources(source_result)

            method_result = Methods_Result_df.GetMethodsByScenario(
                selectedDataset, selectedMasterNetworkName,
                selectedScenarioName)
            exportTemplate.exportMethods(method_result)

            ####################################################

            # Instances

            ####################################################
            Network_Result_df = GetInstancesBySenario()
            Scenarios_Result_df = GetInstancesBySenario()
            Nodes_Result_df = GetInstancesBySenario()
            Links_Result_df = GetInstancesBySenario()

            network_data_result = Network_Result_df.GetMasterNetworks(
                selectedDataset)
            exportTemplate.exportMasterNetwork(network_data_result)

            scenarios_data_result = Scenarios_Result_df.GetScenarios(
                selectedDataset, selectedMasterNetworkName)
            exportTemplate.exportScenario(scenarios_data_result)

            nodes_data_result = Nodes_Result_df.GetNodesByScenario(
                selectedDataset, selectedMasterNetworkName,
                selectedScenarioName)
            print "Count of Nodes data: {}".format(str(len(nodes_data_result)))
            exportTemplate.exportNodes(nodes_data_result)

            links_data_result = Links_Result_df.GetLinksByScenario(
                selectedDataset, selectedMasterNetworkName,
                selectedScenarioName)
            print "Count of links data: {}".format(str(len(links_data_result)))
            exportTemplate.exportLinkes(links_data_result)

            print 'Good'
            ####################################################

            # Data Values

            ####################################################

            NumericValues_Result_df = GetAllValuesByScenario()
            CategoricalValues_Result_df = GetAllValuesByScenario()
            TextFreeValues_Result_df = GetAllValuesByScenario()
            SeasonalNumericValues_Result_df = GetAllValuesByScenario()
            TimeSeries_Result_df = GetAllValuesByScenario()
            TimeSeriesValues_Result_df = GetAllValuesByScenario()
            MultiColumns_Result_df = GetAllValuesByScenario()

            Numeric_result_list = NumericValues_Result_df.GetAllNumericValues(
                selectedDataset, selectedMasterNetworkName,
                selectedScenarioName)
            print "Count of Numeric data: {}".format(
                str(len(Numeric_result_list)))
            exportTemplate.exportNumericValue(Numeric_result_list)

            result_list = CategoricalValues_Result_df.GetAllCategoricalValues(
                selectedDataset, selectedMasterNetworkName,
                selectedScenarioName)
            print "Count of Categorical data: {}".format(str(len(result_list)))
            exportTemplate.exportCategoricalValues(result_list)

            result_list = TextFreeValues_Result_df.GetAllTextFree(
                selectedDataset, selectedMasterNetworkName,
                selectedScenarioName)
            print "Count of TextFree data: {}".format(str(len(result_list)))
            exportTemplate.exportFreeText(result_list)

            result_list = SeasonalNumericValues_Result_df.GetAllSeasonalNumericValues(
                selectedDataset, selectedMasterNetworkName,
                selectedScenarioName)
            print "Count of SeasonalNumeric data: {}".format(
                str(len(result_list)))
            exportTemplate.exportSeasonal(result_list)

            result_list = TimeSeries_Result_df.GetAllTimeSeries(
                selectedDataset, selectedMasterNetworkName,
                selectedScenarioName)
            print "Count of Timeseries data: {}".format(str(len(result_list)))
            exportTemplate.exportTimeSeries(result_list)

            result_list = TimeSeriesValues_Result_df.GetAllTimeSeriesValues(
                selectedDataset, selectedMasterNetworkName,
                selectedScenarioName)

            result_list['DateTimeStamp'] = pd.to_datetime(
                result_list.DateTimeStamp).apply(
                    lambda x: x.strftime('%m/%d/%Y'))

            print "Count of Timeseriesvalues data: {}".format(
                str(len(result_list)))
            exportTemplate.exportTimeSeriesValues(result_list)

            # multiple columns

            up_table_column_result, bottom_table_result = MultiColumns_Result_df.GetAllMultiAttributeSeries(
                selectedDataset, selectedMasterNetworkName,
                selectedScenarioName)
            exportTemplate.exportMulti(up_table_column_result,
                                       bottom_table_result)

            from Messages_forms.msg_successLoadDatabase import msg_successLoadDatabase
            instance = msg_successLoadDatabase(None)
            instance.m_staticText1.SetLabel(
                "Successfully exported the data into the Excel template")
            instance.ShowModal()
            self.btn_Export_ScenarioData.Enabled = True
            self.Destroy()
        except Exception as e:
            messageDlg = msg_somethigWrong(None, msg=e.message)
            messageDlg.Show()
            self.btn_Export_ScenarioData.Enabled = True
class dlg_ExportScenarioDataToExcel(WaMDaMWizard.dlg_ExportScenarioDataToExcel
                                    ):
    def __init__(self, parent):
        WaMDaMWizard.dlg_ExportScenarioDataToExcel.__init__(self, parent)
        self.path = ''
        try:
            if not DB_Setup().get_session():
                msg = "\n\nWarning: Please connect to sqlite first."
                raise Exception(msg)
            ''' init combo model'''
            self.dataStructure = GetResourceStructure()
            self.datasets = self.dataStructure.GetResourceType()
            list_acromy = list()
            for index, row in self.datasets.iterrows():
                list_acromy.append(row[0])
            if list_acromy.__len__() > 0:
                self.comboBox_selectModel.SetItems(list_acromy)
        except Exception as e:
            message = msg_somethigWrong(None, msg=e.message)
            message.Show()
            self.Destroy()

    # Handlers for dlg_ExportScenarioDataToExcel events.
    def comboBox_selectModelOnCombobox(self, event):
        selectedDataset = self.comboBox_selectModel.Value
        result = GetInstancesBySenario()
        datas = result.GetMasterNetworks(selectedDataset)
        GotMasterNetworkNames = []
        for index, row in datas.iterrows():
            GotMasterNetworkNames.append(row[0])
        self.comboBox_selectNetwork.SetItems(GotMasterNetworkNames)

    def comboBox_selectNetworkOnCombobox(self, event):
        selectedMasterNetworkName = self.comboBox_selectNetwork.Value
        result = GetInstancesBySenario()
        datas = result.GetScenarios(self.comboBox_selectModel.Value,
                                    selectedMasterNetworkName)
        GotScenarioNames = []
        for index, row in datas.iterrows():
            GotScenarioNames.append(row[0])
        self.comboBox_selectScenario.SetItems(GotScenarioNames)

    def comboBox_selectScenarioOnCombobox(self, event):
        # TODO: Implement comboBox_selectScenarioOnCombobox
        pass

    def DirectoryPicker_ExportToExcelOnFileChanged(self, event):
        valid_extension = ['xls', 'xlsx']
        self.path = self.DirectoryPicker_ExportToExcel.GetPath()

        # Because use directory picker dialog, add file name.
        # Create file name to export containing "ResourceTypeAcronum" and "NetworkName".
        selectedDataset = self.comboBox_selectModel.Value
        selectedMasterNetworkName = self.comboBox_selectNetwork.Value
        file_name_to_export = "{}_{}".format(selectedDataset,
                                             selectedMasterNetworkName)
        self.path = "{}/{}.xlsx".format(self.path, file_name_to_export)

        print 'This is working just fine...'
        print self.path

    def btn_Export_ScenarioDataOnButtonClick(self, event):
        selectedDataset = self.comboBox_selectModel.Value
        selectedMasterNetworkName = self.comboBox_selectNetwork.Value
        selectedScenarioName = self.comboBox_selectScenario.Value

        # Check whether user select needed items correctly
        message = ''
        if (selectedDataset == None or selectedDataset == ''):
            message = 'Select the resource type (e.g, model name) in WamDam.'
        elif selectedMasterNetworkName == None or selectedMasterNetworkName == '':
            message = 'Select the MasterNetworkName.'
        elif selectedScenarioName == None or selectedScenarioName == '':
            message = 'Select the ScenarioName.'
        elif not ['xls', 'xlsx', 'xlsm', 'xls'].__contains__(
                self.path.split('.')[-1]):
            message = 'Please provide a directory for the output excel file.'
        if message != '':
            messageDlg = msg_somethigWrong(None, msg=message)
            messageDlg.ShowModal()
            return

        print 'this is done'

        try:
            self.btn_Export_ScenarioData.Enabled = False
            exportTemplate = ExportTemplate(self.path)

            ####################################################

            # Rescour Type structure

            ####################################################

            ResourceType_Result_df = GetResourceStructure()
            ObjectTypes_Result_df = GetResourceStructure()
            Attributes_Result_df = GetResourceStructure()

            resources_result = ResourceType_Result_df.GetResourceType(
                selectedDataset)
            data_result = ObjectTypes_Result_df.GetObjectTypesByResource(
                selectedDataset)
            exportTemplate.exportResourcesType(resources_result, data_result)

            # data_result = ObjectTypes_Result_df.GetObjectTypesByResource(selectedDataset)
            # exportTemplate.exportObjecttypes(data_result)

            attributes_result = Attributes_Result_df.GetAttributesByResource(
                selectedDataset)
            print "Count of attrs data: {}".format(str(len(attributes_result)))
            exportTemplate.exportAttributes(attributes_result)

            ####################################################

            # Metadata

            ####################################################

            Organizations_Result_df = GetMetadataByScenario()
            People_Result_df = GetMetadataByScenario()
            Sources_Result_df = GetMetadataByScenario()
            Methods_Result_df = GetMetadataByScenario()

            organization_result = Organizations_Result_df.GetOrganizationsByScenario(
                selectedDataset, selectedMasterNetworkName,
                selectedScenarioName)
            exportTemplate.exportOrganizations(organization_result)

            people_result = People_Result_df.GetPeopleByScenario(
                selectedDataset, selectedMasterNetworkName,
                selectedScenarioName)
            exportTemplate.exportPeople(people_result)

            source_result = Sources_Result_df.GetSourcesByScenario(
                selectedDataset, selectedMasterNetworkName,
                selectedScenarioName)
            exportTemplate.exportSources(source_result)

            method_result = Methods_Result_df.GetMethodsByScenario(
                selectedDataset, selectedMasterNetworkName,
                selectedScenarioName)
            exportTemplate.exportMethods(method_result)

            ####################################################

            # Instances

            ####################################################
            Network_Result_df = GetInstancesBySenario()
            Scenarios_Result_df = GetInstancesBySenario()
            Nodes_Result_df = GetInstancesBySenario()
            Links_Result_df = GetInstancesBySenario()

            network_data_result = Network_Result_df.GetMasterNetworks(
                selectedDataset)
            exportTemplate.exportMasterNetwork(network_data_result)

            scenarios_data_result = Scenarios_Result_df.GetScenarios(
                selectedDataset, selectedMasterNetworkName)
            exportTemplate.exportScenario(scenarios_data_result)

            nodes_data_result = Nodes_Result_df.GetNodesByScenario(
                selectedDataset, selectedMasterNetworkName,
                selectedScenarioName)
            print "Count of Nodes data: {}".format(str(len(nodes_data_result)))
            exportTemplate.exportNodes(nodes_data_result)

            links_data_result = Links_Result_df.GetLinksByScenario(
                selectedDataset, selectedMasterNetworkName,
                selectedScenarioName)
            print "Count of links data: {}".format(str(len(links_data_result)))
            exportTemplate.exportLinkes(links_data_result)

            print 'Good'
            ####################################################

            # Data Values

            ####################################################

            NumericValues_Result_df = GetAllValuesByScenario()
            CategoricalValues_Result_df = GetAllValuesByScenario()
            TextFreeValues_Result_df = GetAllValuesByScenario()
            SeasonalNumericValues_Result_df = GetAllValuesByScenario()
            TimeSeries_Result_df = GetAllValuesByScenario()
            TimeSeriesValues_Result_df = GetAllValuesByScenario()
            MultiColumns_Result_df = GetAllValuesByScenario()

            Numeric_result_list = NumericValues_Result_df.GetAllNumericValues(
                selectedDataset, selectedMasterNetworkName,
                selectedScenarioName)
            print "Count of Numeric data: {}".format(
                str(len(Numeric_result_list)))
            exportTemplate.exportNumericValue(Numeric_result_list)

            result_list = CategoricalValues_Result_df.GetAllCategoricalValues(
                selectedDataset, selectedMasterNetworkName,
                selectedScenarioName)
            print "Count of Categorical data: {}".format(str(len(result_list)))
            exportTemplate.exportCategoricalValues(result_list)

            result_list = TextFreeValues_Result_df.GetAllTextFree(
                selectedDataset, selectedMasterNetworkName,
                selectedScenarioName)
            print "Count of TextFree data: {}".format(str(len(result_list)))
            exportTemplate.exportFreeText(result_list)

            result_list = SeasonalNumericValues_Result_df.GetAllSeasonalNumericValues(
                selectedDataset, selectedMasterNetworkName,
                selectedScenarioName)
            print "Count of SeasonalNumeric data: {}".format(
                str(len(result_list)))
            exportTemplate.exportSeasonal(result_list)

            result_list = TimeSeries_Result_df.GetAllTimeSeries(
                selectedDataset, selectedMasterNetworkName,
                selectedScenarioName)
            print "Count of Timeseries data: {}".format(str(len(result_list)))
            exportTemplate.exportTimeSeries(result_list)

            result_list = TimeSeriesValues_Result_df.GetAllTimeSeriesValues(
                selectedDataset, selectedMasterNetworkName,
                selectedScenarioName)

            result_list['DateTimeStamp'] = pd.to_datetime(
                result_list.DateTimeStamp).apply(
                    lambda x: x.strftime('%m/%d/%Y'))

            print "Count of Timeseriesvalues data: {}".format(
                str(len(result_list)))
            exportTemplate.exportTimeSeriesValues(result_list)

            # multiple columns

            up_table_column_result, bottom_table_result = MultiColumns_Result_df.GetAllMultiAttributeSeries(
                selectedDataset, selectedMasterNetworkName,
                selectedScenarioName)
            exportTemplate.exportMulti(up_table_column_result,
                                       bottom_table_result)

            from Messages_forms.msg_successLoadDatabase import msg_successLoadDatabase
            instance = msg_successLoadDatabase(None)
            instance.m_staticText1.SetLabel(
                "Successfully exported the data into the Excel template")
            instance.ShowModal()
            self.btn_Export_ScenarioData.Enabled = True
            self.Destroy()
        except Exception as e:
            messageDlg = msg_somethigWrong(None, msg=e.message)
            messageDlg.Show()
            self.btn_Export_ScenarioData.Enabled = True
            # raise Exception(e.message)

    def write2excel(self, resutl_list, startRow, sheet):
        try:
            for row_id, row in enumerate(resutl_list):
                for col_id, cell in enumerate(row):
                    sheet.cell(row=row_id + startRow,
                               column=col_id + 1,
                               value=cell)

        except Exception as e:
            print e
            messageDlg = msg_somethigWrong(None, msg=e.message)
            messageDlg.Show()
            raise Exception(e.message)

    def btn_cancelOnButtonClick(self, event):
        # TODO: Implement btn_cancelOnButtonClick
        self.Close()
Esempio n. 8
0
class dlg_VisulaizeWaMDaM(Publish_Viz.dlg_VisulaizeWaMDaM):
    def __init__(self, parent):
        Publish_Viz.dlg_VisulaizeWaMDaM.__init__(self, parent)

        if self.checkConnectingToSqlite():
            # self.m_textCtrl11.Value = '*****@*****.**'
            # self.m_textCtrl12.Value = 'OApass1'
            self.userName = self.m_textCtrl11.Value
            self.password = self.m_textCtrl12.Value

        else:
            msg = "Warning: Please connect to sqlite."
            msg_somethigWrong(self, msg).ShowModal()
            self.Destroy()

    def hydra(self):
        endpoint = 'https://www.openagua.org/api/v1/hydra/'

        return Hydra_OA(endpoint, self.m_textCtrl11.Value,
                        self.m_textCtrl12.Value)

    # Handlers for dlg_VisulaizeWaMDaM events.
    def Btn_LoginOnButtonClick(self, event):
        # TODO: Implement Btn_LoginOnButtonClick

        try:
            self.userName = self.m_textCtrl11.Value
            self.password = self.m_textCtrl12.Value

            if not self.m_textCtrl11.Value or not self.m_textCtrl12.Value:
                raise Exception("Provide a username and password")

            self.usern = self.m_textCtrl11.Value
            self.passw = self.m_textCtrl12.Value

            projects = self.hydra()._call('get_projects')
            project_names = []

            for p in projects:
                project_names.append(str(p['name']))

            self.m_SelectProject.SetItems(project_names)

            self.dataStructure = GetResourceStructure()

            self.datasets = self.dataStructure.GetResourceType()

            list_acromy = list()

            for index, row in self.datasets.iterrows():
                list_acromy.append(row[0])

            if list_acromy.__len__() > 0:
                self.m_SelectModel.SetItems(list_acromy)

        except Exception as e:
            messageDlg = msg_somethigWrong(None, msg=e.args[0])
            messageDlg.Show()

    # Handlers for dlg_VisulaizeWaMDaM events.
    def btn_UploadToOpenAguaOnButtonClick(self, event):
        # TODO: Implement btn_UploadToOpenAguaOnButtonClick

        userName = self.m_textCtrl11.Value
        password = self.m_textCtrl12.Value

        try:
            projectName = self.m_textCtrl7.Value
            if not projectName:
                if self.m_SelectProject.Value == "":
                    raise Exception("Provide an OpenAgua project name")
                else:
                    projectName = self.m_SelectProject.Value
            else:
                if self.m_SelectProject.Value != "":
                    raise Exception("Select only an OpenAgua project name")

            selectedResourceTypeAcro = self.m_SelectModel.Value
            if not selectedResourceTypeAcro:
                raise Exception("Select a Model")

            selectedMasterNetworkName = self.m_SelectNetwork.Value
            if not selectedResourceTypeAcro:
                raise Exception("Select a Network")

            if self.SelectScenario.GetSelectedItemCount() < 1:
                raise Exception("Select a Scenario")

            index = self.SelectScenario.GetFirstSelected()
            selectedScenarioNames = []

            while index != -1:
                item = self.SelectScenario.GetItem(itemId=index)
                selectedScenarioName = item.GetText()
                # store the many selected scenarios into a dict? then pass them for the for loop
                selectedScenarioNames.append(selectedScenarioName)
                # do something with the items
                index = self.SelectScenario.GetNextSelected(index)

            # add project here? then just pass its  my_new_project.id to the controller

            UploadToOpenAgua(selectedResourceTypeAcro,
                             selectedMasterNetworkName, selectedScenarioNames,
                             projectName, userName, password)

            from viewer.Messages_forms.msg_connSQLiteSuccs import msg_connSQLiteSuccs
            msgdlg = msg_connSQLiteSuccs(self)
            msgdlg.setMessage(
                "\n\nSuccessfully, Uploaded the model to OpenAgua  file.")
            msgdlg.ShowModal()

            self.Destroy()

        except Exception as e:
            # pass
            msg_somethigWrong(self, e.args[0]).ShowModal()

    def btn_cancelOnButtonClick(self, event):
        # TODO: Implement btn_cancelOnButtonClick
        self.Destroy()

    def m_SelectProjectOnCombobox(self, event):
        # TODO: Implement m_SelectProjectOnCombobox
        pass

    def m_SelectModelOnCombobox(self, event):
        selectedDataset = self.m_SelectModel.Value
        result = GetInstancesBySenario()
        datas = result.GetMasterNetworks(selectedDataset)
        GotMasterNetworkNames = []
        for index, row in datas.iterrows():
            GotMasterNetworkNames.append(row[0])
        self.m_SelectNetwork.SetItems(GotMasterNetworkNames)

    def m_SelectNetworkOnCombobox(self, event):
        selectedMasterNetworkName = self.m_SelectNetwork.Value
        result = GetInstancesBySenario()
        datas = result.GetScenarios(self.m_SelectModel.Value,
                                    selectedMasterNetworkName)
        GotScenarioNames = []
        self.SelectScenario.DeleteAllItems()
        for index, row in datas.iterrows():
            self.SelectScenario.InsertStringItem(index, row[0])
            GotScenarioNames.append(row[0])
        # self.SelectScenario.Append(GotScenarioNames)

    def checkConnectingToSqlite(self):
        db_setup = DB_Setup()
        if not db_setup.get_session():
            return False
        return True

    def SelectScenarioOnListBeginDrag(self, event):
        # TODO: Implement SelectScenarioOnListBeginDrag
        pass

    def SelectScenarioOnListBeginLabelEdit(self, event):
        # TODO: Implement SelectScenarioOnListBeginLabelEdit
        pass

    def SelectScenarioOnListBeginRDrag(self, event):
        # TODO: Implement SelectScenarioOnListBeginRDrag
        pass

    def SelectScenarioOnListCacheHint(self, event):
        # TODO: Implement SelectScenarioOnListCacheHint
        pass

    def SelectScenarioOnListColBeginDrag(self, event):
        # TODO: Implement SelectScenarioOnListColBeginDrag
        pass

    def SelectScenarioOnListColClick(self, event):
        # TODO: Implement SelectScenarioOnListColClick
        pass

    def SelectScenarioOnListColDragging(self, event):
        # TODO: Implement SelectScenarioOnListColDragging
        pass

    def SelectScenarioOnListColEndDrag(self, event):
        # TODO: Implement SelectScenarioOnListColEndDrag
        pass

    def SelectScenarioOnListColRightClick(self, event):
        # TODO: Implement SelectScenarioOnListColRightClick
        pass

    def SelectScenarioOnListDeleteAllItems(self, event):
        # TODO: Implement SelectScenarioOnListDeleteAllItems
        pass

    def SelectScenarioOnListDeleteItem(self, event):
        # TODO: Implement SelectScenarioOnListDeleteItem
        pass

    def SelectScenarioOnListEndLabelEdit(self, event):
        # TODO: Implement SelectScenarioOnListEndLabelEdit
        pass

    def SelectScenarioOnListInsertItem(self, event):
        # TODO: Implement SelectScenarioOnListInsertItem
        pass

    def SelectScenarioOnListItemActivated(self, event):
        # TODO: Implement SelectScenarioOnListItemActivated
        pass

    def SelectScenarioOnListItemDeselected(self, event):
        # TODO: Implement SelectScenarioOnListItemDeselected
        pass

    def SelectScenarioOnListItemFocused(self, event):
        # TODO: Implement SelectScenarioOnListItemFocused
        pass

    def SelectScenarioOnListItemMiddleClick(self, event):
        # TODO: Implement SelectScenarioOnListItemMiddleClick
        pass

    def SelectScenarioOnListItemRightClick(self, event):
        # TODO: Implement SelectScenarioOnListItemRightClick
        pass

    def SelectScenarioOnListItemSelected(self, event):
        # TODO: Implement SelectScenarioOnListItemSelected
        pass

    def SelectScenarioOnListKeyDown(self, event):
        # TODO: Implement SelectScenarioOnListKeyDown
        pass
Esempio n. 9
0
class dlg_compare_scenarios(WaMDaMWizard.dlg_compare_scenarios):
    def __init__(self, parent):
        WaMDaMWizard.dlg_compare_scenarios.__init__(self, parent)

        self.path = ''
        try:
            if not self.checkConnectingToSqlite():
                msg = "\n\nWarning: Please connect to sqlite first."
                raise Exception(msg)
            ''' init model combobox'''
            self.dataStructure = GetResourceStructure()
            self.instances = GetInstancesBySenario()
            self.datasets = self.dataStructure.GetResourceType()
            self.compareScenarios = GetComapreScenarios()

            list_acromy = list()
            for index, row in self.datasets.iterrows():
                list_acromy.append(row["ResourceTypeAcronym"])
            if list_acromy.__len__() > 0:
                self.comboBox_selectModel.SetItems(list_acromy)
        except Exception as e:
            message = msg_somethigWrong(None, msg=e.message)
            message.ShowModal()
            self.Destroy()

    # Handlers for dlg_compare_scenarios events.
    def comboBox_selectModelOnCombobox(self, event):
        ''' init network combobox once model combobox change'''
        selectedDataset = self.comboBox_selectModel.Value
        result = GetInstancesBySenario()
        gotMasterNetwork = result.GetMasterNetworks(selectedDataset)
        list_Networks = list()
        for index, row in gotMasterNetwork.iterrows():
            list_Networks.append(row["MasterNetworkName"])
        if list_Networks.__len__() > 0:
            self.comboBox_selectNetwork.SetItems(list_Networks)

    def comboBox_selectNetworkOnCombobox(self, event):
        ''' init first scenario combobox once network combobox change'''
        selectedMasterNetworkName = self.comboBox_selectNetwork.Value
        selectedDataset = self.comboBox_selectModel.Value
        result = GetInstancesBySenario()
        gotScenarioNames = result.GetScenarios(selectedDataset,
                                               selectedMasterNetworkName)

        list_Scenarios = list()
        for index, row in gotScenarioNames.iterrows():
            list_Scenarios.append(row["ScenarioName"])

        if gotScenarioNames.__len__() <= 0:
            return
        self.comboBox_selectScenario1.SetItems(list_Scenarios)

    def comboBox_selectScenario1OnCombobox(self, event):
        ''' init second scenario combobox once first scenario change'''
        selectedMasterNetworkName = self.comboBox_selectNetwork.Value
        selectedDataset = self.comboBox_selectModel.Value
        result = GetInstancesBySenario()
        gotScenarios = result.GetScenarios(selectedDataset,
                                           selectedMasterNetworkName)

        list_Scenarios = list()
        for index, row in gotScenarios.iterrows():
            list_Scenarios.append(row["ScenarioName"])

        if list_Scenarios.__len__() <= 0:
            return
        selected_scenario1 = self.comboBox_selectScenario1.Value
        selected_index = list_Scenarios.index(selected_scenario1)
        list_Scenarios.pop(selected_index)
        self.comboBox_selectScenario2.SetItems(list_Scenarios)
        pass

    def comboBox_selectScenario2OnCombobox(self, event):
        # TODO: Implement comboBox_selectScenario2OnCombobox
        pass

    def FilePicker_compareScenariosOnFileChanged(self, event):
        valid_extension = ['xlsx']
        self.path = self.FilePicker_compareScenarios.GetPath()
        if not (self.path.split('.')[-1] in valid_extension):
            message = msg_somethigWrong(None,
                                        msg="Please select a valid Excel File")
            message.ShowModal()
            return
        print 'This is working just fine...'
        print self.path
        book2 = load_workbook(self.path)
        # bring all the checks below to up here?
        sheetnames = [
            "ChangeInTopology", "ChangeInMetadata_Topology",
            "ChangeInMetadata_Attributes", "ChangeInValues"
        ]
        for name in sheetnames:
            if not name in book2.sheetnames:
                book2.close()
                message = msg_somethigWrong(
                    None, msg="Please select a valid Excel File")
                message.ShowModal()
                return

        book2.close()

    def btn_compare_scenariosOnButtonClick(self, event):
        self.btn_compare_scenarios.Enabled = False
        ''' Get selected data(Dataset, MasterNetworkName, ScenarioName1, ScenarioName2) '''
        selectedDataset = self.comboBox_selectModel.Value
        selectedMasterNetworkName = self.comboBox_selectNetwork.Value
        selectedScenarioName1 = self.comboBox_selectScenario1.Value
        selectedScenarioName2 = self.comboBox_selectScenario2.Value
        ''' Check if needed names are correctly selected '''
        message = ''
        if (selectedDataset == None or selectedDataset == ''):
            message = 'Select the model name in WamDam.'
        elif selectedMasterNetworkName == None or selectedMasterNetworkName == '':
            message = 'Select the MasterNetworkName.'
        elif selectedScenarioName1 == None or selectedScenarioName1 == '':
            message = 'Select the ScenarioName1.'
        elif selectedScenarioName2 == None or selectedScenarioName2 == '':
            message = 'Select the ScenarioName2.'
        elif not ['xlsx'].__contains__(self.path.split('.')[-1]):
            message = 'please select a valid excel file.'
        '''if warning message exists, then show msg dialog and return.'''
        if message != '':
            messageDlg = msg_somethigWrong(None, msg=message)
            messageDlg.Show()
            return
        ''' Get data for changed topology by selected scenarios'''
        resultData = self.compareScenarios.GetComapreScenarios(
            selectedDataset, selectedMasterNetworkName, selectedScenarioName1,
            selectedScenarioName2)
        unique1ResultData = self.compareScenarios.GetUniqueTopology_Scenario2(
            selectedDataset, selectedMasterNetworkName, selectedScenarioName1,
            selectedScenarioName2)
        unique2ResultData = self.compareScenarios.GetUniqueTopology_Scenario1(
            selectedDataset, selectedMasterNetworkName, selectedScenarioName1,
            selectedScenarioName2)
        commonResultData = self.compareScenarios.GetCommonTopology(
            selectedDataset, selectedMasterNetworkName, selectedScenarioName1,
            selectedScenarioName2)

        i = 0
        k = 0
        ''' in data for changed topology, pop common data '''
        for i in range(0, len(unique1ResultData)):
            unique1Data = unique1ResultData[i - k]
            j = 0
            isSame = False
            for unique2Data in unique2ResultData:
                if unique1Data[0] == unique2Data[0] and unique1Data[
                        1] == unique2Data[1] and unique1Data[2] == unique2Data[
                            2]:
                    isSame = True
                    break
                j += 1
            if isSame:
                unique1ResultData.pop(i - k)
                unique2ResultData.pop(j)
                k += 1
        ''' pop common data in union result'''
        i = 0
        k = 0
        for i in range(0, len(commonResultData)):
            unique1Data = commonResultData[i - k]
            j = 0
            isSame = False
            for unique2Data in unique2ResultData:
                if unique1Data[0] == unique2Data[0] and unique1Data[
                        1] == unique2Data[1] and unique1Data[2] == unique2Data[
                            2]:
                    isSame = True
                    break
            if not isSame:
                for unique1Data0 in unique1ResultData:
                    if unique1Data[0] == unique1Data0[0] and unique1Data[
                            1] == unique1Data0[1] and unique1Data[
                                2] == unique1Data0[2]:
                        isSame = True
                        break

            if isSame:
                commonResultData.pop(i - k)
                k += 1
        '''get changed metadata'''
        changeInMetadataList = self.compareScenarios.GetChangeInMetadata_Topology(
            selectedDataset, selectedMasterNetworkName, selectedScenarioName1,
            selectedScenarioName2)

        # print the result to this excel sheet: ChangeInMetadata_Attributes
        changeInMetadataValueList = self.compareScenarios.GetChangeInMetaValues_Attributes(
            selectedDataset, selectedMasterNetworkName, selectedScenarioName1,
            selectedScenarioName2)
        ''' init metadata'''
        changeInMetadataList_result = []

        firstScenarioCount = 0
        commonScenarioCount = 0
        secondScenarioCount = 0
        totalcount = 0

        for unique1Data in changeInMetadataList:
            j = 0
            isSame = False
            for row in changeInMetadataList_result:
                if unique1Data[0] == row[0] and unique1Data[1] == row[
                        1] and unique1Data[2] == row[2] and unique1Data[
                            3] == row[3]:
                    isSame = True
                    if unique1Data[4] != row[4]:
                        if len(changeInMetadataList_result[j]) == 12:
                            '''second senario data append'''
                            changeInMetadataList_result[j][9] = unique1Data[4]
                            changeInMetadataList_result[j][10] = unique1Data[5]
                            changeInMetadataList_result[j][11] = unique1Data[6]
                        else:
                            changeInMetadataList_result[j].append(
                                unique1Data[4])
                            changeInMetadataList_result[j].append(
                                unique1Data[5])
                            changeInMetadataList_result[j].append(
                                unique1Data[6])
                j += 1
            if not isSame:
                changeInMetadataList_result.append(unique1Data)

            firstScenarioCount = len(unique2ResultData)
            secondScenarioCount = len(unique1ResultData)
            commonScenarioCount = len(commonResultData)
            totalcount = firstScenarioCount + secondScenarioCount + commonScenarioCount
        ''' print the result to this excel sheet: ChangeInMetadata_Attributes '''

        changeInMetadataAttributeList_result = []
        for unique1Data in changeInMetadataValueList:
            j = 0
            isSame = False
            for row in changeInMetadataAttributeList_result:
                if unique1Data[0] == row[0] and unique1Data[1] == row[
                        1] and unique1Data[2] == row[2] and unique1Data[
                            3] == row[3]:
                    isSame = True
                    if unique1Data[5] != row[5]:
                        if len(changeInMetadataAttributeList_result[j]) > 11:
                            '''second senario data append'''
                            changeInMetadataAttributeList_result[j][
                                10] = unique1Data[5]
                            changeInMetadataAttributeList_result[j][
                                11] = unique1Data[6]
                            changeInMetadataAttributeList_result[j][
                                12] = unique1Data[7]
                        else:
                            changeInMetadataAttributeList_result[j].append(
                                unique1Data[5])
                            changeInMetadataAttributeList_result[j].append(
                                unique1Data[6])
                            changeInMetadataAttributeList_result[j].append(
                                unique1Data[7])
                j += 1
            if not isSame:
                changeInMetadataAttributeList_result.append(unique1Data)

        print '**************'
        try:
            if self.path.split('.')[-1] == 'xlsx':
                book2 = load_workbook(self.path)
                additionsToTopologySheet = book2["ChangeInTopology"]
                changeInMetadataToTopologySheet = book2[
                    "ChangeInMetadata_Topology"]
                changeInMetavaluesTopologySheet = book2[
                    "ChangeInMetadata_Attributes"]
                changeInValuesSheet = book2["ChangeInValues"]

                additionsToTopologySheet.cell(row=3,
                                              column=2,
                                              value=firstScenarioCount)
                additionsToTopologySheet.cell(row=3,
                                              column=5,
                                              value=commonScenarioCount)
                additionsToTopologySheet.cell(row=3,
                                              column=8,
                                              value=secondScenarioCount)
                additionsToTopologySheet.cell(
                    row=4,
                    column=2,
                    value=str(
                        round(
                            float(firstScenarioCount) /
                            float(1 if totalcount == 0 else totalcount), 3) *
                        100.0) + "%")
                additionsToTopologySheet.cell(
                    row=4,
                    column=5,
                    value=str(
                        round(
                            float(commonScenarioCount) /
                            float(1 if totalcount == 0 else totalcount), 3) *
                        100.0) + "%")
                additionsToTopologySheet.cell(
                    row=4,
                    column=8,
                    value=str(
                        round(
                            float(secondScenarioCount) /
                            float(1 if totalcount == 0 else totalcount), 3) *
                        100.0) + "%")
                rowNumber = 7
                for row_id, row in enumerate(unique2ResultData):
                    for col_id, cell in enumerate(row):
                        additionsToTopologySheet.cell(row=rowNumber,
                                                      column=col_id + 1,
                                                      value=cell)
                    rowNumber += 1

                for row_id, row in enumerate(unique1ResultData):
                    for col_id, cell in enumerate(row):
                        additionsToTopologySheet.cell(row=rowNumber,
                                                      column=col_id + 7,
                                                      value=cell)
                    rowNumber += 1

                for row_id, row in enumerate(commonResultData):
                    for col_id, cell in enumerate(row):
                        additionsToTopologySheet.cell(row=rowNumber,
                                                      column=col_id + 4,
                                                      value=cell)
                    rowNumber += 1
                i = 0

                count_first_source = 0
                count_second_source = 0
                count_first_method = 0
                count_second_method = 0
                count_common_source = 0
                count_common_method = 0

                for row_id, row in enumerate(changeInMetadataList_result):
                    # if row.__len__() < 10:
                    # 	continue
                    if row.__len__(
                    ) > 10 and row[5] == row[10] and row[6] == row[11]:
                        continue
                    for col_id, cell in enumerate(row):
                        if col_id < 4:
                            changeInMetadataToTopologySheet.cell(
                                row=i + 6, column=col_id + 1, value=cell)
                            # changeInMetavaluesTopologySheet.cell(row=i + 6, column=col_id + 1, value=cell)
                        elif col_id == 4:
                            if cell == selectedScenarioName1:
                                changeInMetadataToTopologySheet.cell(
                                    row=i + 6, column=col_id + 1, value=row[5])
                                changeInMetadataToTopologySheet.cell(
                                    row=i + 6, column=7, value=row[6])
                                # changeInMetavaluesTopologySheet.cell(row=i + 6, column=col_id + 1, value=row[5])
                                # changeInMetavaluesTopologySheet.cell(row=i + 6, column=7, value=row[6])
                                if not row[5] is None and row[5] != '':
                                    count_first_source += 1
                                if not row[6] is None and row[6] != '':
                                    count_first_method += 1
                            else:
                                changeInMetadataToTopologySheet.cell(
                                    row=i + 6, column=col_id + 2, value=row[5])
                                changeInMetadataToTopologySheet.cell(
                                    row=i + 6, column=8, value=row[6])
                                # changeInMetavaluesTopologySheet.cell(row=i + 6, column=col_id + 2, value=row[5])
                                # changeInMetavaluesTopologySheet.cell(row=i + 6, column=8, value=row[6])
                                if not row[5] is None and row[5] != '':
                                    count_second_method += 1
                                if not row[6] is None and row[6] != '':
                                    count_second_source += 1
                            # if len(row) < 10:
                            # 	break
                        elif col_id == 9:
                            if cell == selectedScenarioName1:
                                changeInMetadataToTopologySheet.cell(
                                    row=i + 6, column=5, value=row[10])
                                changeInMetadataToTopologySheet.cell(
                                    row=i + 6, column=7, value=row[11])
                                # changeInMetavaluesTopologySheet.cell(row=i + 6, column=5, value=row[10])
                                # changeInMetavaluesTopologySheet.cell(row=i + 6, column=7, value=row[11])
                                if not row[10] is None and row[10] != '':
                                    count_first_source += 1
                                if not row[11] is None and row[11] != '':
                                    count_first_method += 1
                            else:
                                changeInMetadataToTopologySheet.cell(
                                    row=i + 6, column=6, value=row[10])
                                changeInMetadataToTopologySheet.cell(
                                    row=i + 6, column=8, value=row[11])
                                # changeInMetavaluesTopologySheet.cell(row=i + 6, column=6, value=row[10])
                                # changeInMetavaluesTopologySheet.cell(row=i + 6, column=8, value=row[11])
                                if not row[10] is None and row[10] != '':
                                    count_second_method += 1
                                if not row[11] is None and row[11] != '':
                                    count_second_source += 1
                            break
                    if row.__len__() > 10 and row[5] == row[10]:
                        changeInMetadataToTopologySheet.cell(row=i + 6,
                                                             column=9,
                                                             value=row[10])
                        changeInMetadataToTopologySheet.cell(row=i + 6,
                                                             column=5,
                                                             value="")
                        changeInMetadataToTopologySheet.cell(row=i + 6,
                                                             column=6,
                                                             value="")

                        # changeInMetavaluesTopologySheet.cell(row=i + 6, column=9, value=row[10])
                        # changeInMetavaluesTopologySheet.cell(row=i + 6, column=5, value="")
                        # changeInMetavaluesTopologySheet.cell(row=i + 6, column=6, value="")
                        count_common_source += 1
                        count_first_source -= 1
                        count_second_source -= 1
                    if row.__len__() > 10 and row[6] == row[11]:
                        changeInMetadataToTopologySheet.cell(row=i + 6,
                                                             column=10,
                                                             value=row[11])
                        changeInMetadataToTopologySheet.cell(row=i + 6,
                                                             column=7,
                                                             value="")
                        changeInMetadataToTopologySheet.cell(row=i + 6,
                                                             column=8,
                                                             value="")

                        # changeInMetavaluesTopologySheet.cell(row=i + 6, column=10, value=row[11])
                        # changeInMetavaluesTopologySheet.cell(row=i + 6, column=7, value="")
                        # changeInMetavaluesTopologySheet.cell(row=i + 6, column=8, value="")
                        count_first_method -= 1
                        count_second_method -= 1
                        count_common_method += 1
                    i += 1

                count_first_source_attr = 0
                count_second_source_attr = 0
                count_first_method_attr = 0
                count_second_method_attr = 0
                count_common_source_attr = 0
                count_common_method_attr = 0
                i = 0
                ''' print the result to this excel sheet: ChangeInMetadata_Attributes '''

                for row_id, row in enumerate(
                        changeInMetadataAttributeList_result):
                    # if row.__len__() < 10:
                    # 	continue
                    if row.__len__(
                    ) > 10 and row[6] == row[11] and row[7] == row[12]:
                        continue
                    for col_id, cell in enumerate(row):
                        if col_id < 4:
                            # changeInMetadataToTopologySheet.cell(row=i + 6, column=col_id + 1, value=cell)
                            changeInMetavaluesTopologySheet.cell(
                                row=i + 6, column=col_id + 1, value=cell)
                        elif col_id == 5:
                            if cell == selectedScenarioName1:
                                changeInMetavaluesTopologySheet.cell(
                                    row=i + 6, column=5, value=row[6])
                                changeInMetavaluesTopologySheet.cell(
                                    row=i + 6, column=7, value=row[7])
                                if not row[6] is None and row[6] != '':
                                    count_first_source_attr += 1
                                if not row[7] is None and row[7] != '':
                                    count_first_method_attr += 1
                            else:
                                changeInMetavaluesTopologySheet.cell(
                                    row=i + 6, column=6, value=row[6])
                                changeInMetavaluesTopologySheet.cell(
                                    row=i + 6, column=8, value=row[7])
                                if not row[6] is None and row[6] != '':
                                    count_second_method_attr += 1
                                if not row[7] is None and row[7] != '':
                                    count_second_source_attr += 1
                            # if len(row) < 10:
                            # 	break
                        elif col_id == 10:
                            if cell == selectedScenarioName1:
                                # changeInMetadataToTopologySheet.cell(row=i + 6, column=5, value=row[10])
                                # changeInMetadataToTopologySheet.cell(row=i + 6, column=7, value=row[11])
                                changeInMetavaluesTopologySheet.cell(
                                    row=i + 6, column=5, value=row[11])
                                changeInMetavaluesTopologySheet.cell(
                                    row=i + 6, column=7, value=row[12])
                                if not row[11] is None and row[11] != '':
                                    count_first_source_attr += 1
                                if not row[12] is None and row[12] != '':
                                    count_first_method_attr += 1
                            else:
                                # changeInMetadataToTopologySheet.cell(row=i + 6, column=6, value=row[11])
                                # changeInMetadataToTopologySheet.cell(row=i + 6, column=8, value=row[12])
                                changeInMetavaluesTopologySheet.cell(
                                    row=i + 6, column=6, value=row[11])
                                changeInMetavaluesTopologySheet.cell(
                                    row=i + 6, column=8, value=row[12])
                                if not row[11] is None and row[11] != '':
                                    count_second_method_attr += 1
                                if not row[12] is None and row[12] != '':
                                    count_second_source_attr += 1
                            break
                    if row.__len__() > 10 and row[6] == row[11]:
                        # changeInMetadataToTopologySheet.cell(row=i + 6, column=9, value=row[11])
                        # changeInMetadataToTopologySheet.cell(row=i + 6, column=5, value="")
                        # changeInMetadataToTopologySheet.cell(row=i + 6, column=6, value="")

                        changeInMetavaluesTopologySheet.cell(row=i + 6,
                                                             column=9,
                                                             value=row[11])
                        changeInMetavaluesTopologySheet.cell(row=i + 6,
                                                             column=5,
                                                             value="")
                        changeInMetavaluesTopologySheet.cell(row=i + 6,
                                                             column=6,
                                                             value="")
                        count_common_source_attr += 1
                        count_first_source_attr -= 1
                        count_second_source_attr -= 1
                    if row.__len__() > 10 and row[7] == row[12]:
                        # changeInMetadataToTopologySheet.cell(row=i + 6, column=10, value=row[11])
                        # changeInMetadataToTopologySheet.cell(row=i + 6, column=7, value="")
                        # changeInMetadataToTopologySheet.cell(row=i + 6, column=8, value="")

                        changeInMetavaluesTopologySheet.cell(row=i + 6,
                                                             column=10,
                                                             value=row[12])
                        changeInMetavaluesTopologySheet.cell(row=i + 6,
                                                             column=7,
                                                             value="")
                        changeInMetavaluesTopologySheet.cell(row=i + 6,
                                                             column=8,
                                                             value="")
                        count_common_method_attr += 1
                        count_second_method_attr -= 1
                        count_first_method_attr -= 1
                    i += 1

                first_scenario_value_count = 0
                second_scenario_value_count = 0
                list_changInValue = []
                for row_id, row in enumerate(changeInMetadataValueList):
                    macth_flag = False
                    for row_i, result in enumerate(list_changInValue):
                        if result[0] == row[0] and result[1] == row[
                                1] and result[2] == row[2] and result[
                                    3] == row[3]:
                            if row[5] == selectedScenarioName1:
                                first_scenario_value_count += 1
                                list_changInValue[row_i][5] = row[9]
                            else:
                                second_scenario_value_count += 1
                                list_changInValue[row_i][6] = row[9]
                            macth_flag = True
                            break
                    if not macth_flag:
                        items = []
                        firstScenarioFlag = True
                        for col_id, cell in enumerate(row):
                            if col_id < 5:
                                items.append(cell)
                            else:
                                if col_id == 5:
                                    if cell == selectedScenarioName2:
                                        firstScenarioFlag = False
                                elif col_id == 9:
                                    if firstScenarioFlag:
                                        first_scenario_value_count += 1
                                        items.append(cell)
                                        items.append('')
                                    else:
                                        second_scenario_value_count += 1
                                        items.append('')
                                        items.append(cell)
                        list_changInValue.append(items)

                for row_id, row in enumerate(list_changInValue):
                    for col_id, cell in enumerate(row):
                        changeInValuesSheet.cell(row=row_id + 6,
                                                 column=col_id + 1,
                                                 value=cell)

                # print the scenario names in F and G
                changeInValuesSheet.cell(row=4,
                                         column=6,
                                         value=selectedScenarioName1)
                changeInValuesSheet.cell(row=4,
                                         column=7,
                                         value=selectedScenarioName2)

                allValueCount = self.compareScenarios.AllValuesMapperCount(
                    selectedDataset, selectedMasterNetworkName)

                # print values to column F: Unique to Scenario 1
                changeInValuesSheet.cell(row=2,
                                         column=6,
                                         value=first_scenario_value_count)
                changeInValuesSheet.cell(
                    row=3,
                    column=6,
                    value=str(
                        round(
                            float(first_scenario_value_count) * 100.0 /
                            float(allValueCount), 2)) + '%')

                # print values to column G: Unique to Scenario 2
                changeInValuesSheet.cell(row=2,
                                         column=7,
                                         value=second_scenario_value_count)
                changeInValuesSheet.cell(
                    row=3,
                    column=7,
                    value=str(
                        round(
                            float(second_scenario_value_count) * 100 /
                            float(allValueCount), 2)) + '%')

                common_count = allValueCount - first_scenario_value_count - second_scenario_value_count

                # print values to column H: Common to both
                changeInValuesSheet.cell(row=2, column=8, value=common_count)
                changeInValuesSheet.cell(
                    row=3,
                    column=8,
                    value=str(
                        round(
                            float(common_count) * 100 / float(allValueCount),
                            2)) + '%')

                changeInValuesSheet.cell(row=2, column=9, value=allValueCount)
                changeInValuesSheet.cell(row=3, column=9, value='100%')

                additionsToTopologySheet.cell(row=2,
                                              column=2,
                                              value=selectedScenarioName1)
                additionsToTopologySheet.cell(row=2,
                                              column=8,
                                              value=selectedScenarioName2)

                changeInMetadataToTopologySheet.cell(
                    row=4, column=5, value=selectedScenarioName1)
                changeInMetadataToTopologySheet.cell(
                    row=4, column=6, value=selectedScenarioName2)
                changeInMetadataToTopologySheet.cell(
                    row=4, column=7, value=selectedScenarioName1)
                changeInMetadataToTopologySheet.cell(
                    row=4, column=8, value=selectedScenarioName2)

                changeInMetavaluesTopologySheet.cell(
                    row=4, column=5, value=selectedScenarioName1)
                changeInMetavaluesTopologySheet.cell(
                    row=4, column=6, value=selectedScenarioName2)
                changeInMetavaluesTopologySheet.cell(
                    row=4, column=7, value=selectedScenarioName1)
                changeInMetavaluesTopologySheet.cell(
                    row=4, column=8, value=selectedScenarioName2)

                allMetaValueCount = self.compareScenarios.AllTopologyMetadataCount(
                    selectedDataset, selectedMasterNetworkName)
                changeInMetadataToTopologySheet.cell(row=2,
                                                     column=5,
                                                     value=count_first_source)
                changeInMetadataToTopologySheet.cell(
                    row=3,
                    column=5,
                    value=str(
                        round(
                            float(count_first_source) * 100 /
                            float(allMetaValueCount - count_common_source), 2))
                    + '%')
                changeInMetadataToTopologySheet.cell(row=2,
                                                     column=6,
                                                     value=count_second_source)
                changeInMetadataToTopologySheet.cell(
                    row=3,
                    column=6,
                    value=str(
                        round(
                            float(count_second_source) * 100 /
                            float(allMetaValueCount - count_common_source), 2))
                    + '%')
                changeInMetadataToTopologySheet.cell(row=2,
                                                     column=7,
                                                     value=count_first_method)
                changeInMetadataToTopologySheet.cell(
                    row=3,
                    column=7,
                    value=str(
                        round(
                            float(count_first_method) * 100 /
                            float(allMetaValueCount - count_common_method), 2))
                    + '%')
                changeInMetadataToTopologySheet.cell(row=2,
                                                     column=8,
                                                     value=count_second_method)
                changeInMetadataToTopologySheet.cell(
                    row=3,
                    column=8,
                    value=str(
                        round(
                            float(count_second_method) * 100 /
                            float(allMetaValueCount - count_common_method), 2))
                    + '%')
                changeInMetadataToTopologySheet.cell(row=2,
                                                     column=9,
                                                     value=count_common_source)
                rest_source_count = allMetaValueCount - count_first_source - count_second_source
                changeInMetadataToTopologySheet.cell(
                    row=3,
                    column=9,
                    value=str(
                        round(
                            float(rest_source_count) * 100 /
                            float(allMetaValueCount), 2)) + '%')
                rest_method_count = allMetaValueCount - count_first_method - count_second_method
                changeInMetadataToTopologySheet.cell(row=2,
                                                     column=10,
                                                     value=count_common_method)
                changeInMetadataToTopologySheet.cell(
                    row=3,
                    column=10,
                    value=str(
                        round(
                            float(rest_method_count) * 100 /
                            float(allMetaValueCount), 2)) + '%')
                changeInMetadataToTopologySheet.cell(row=2,
                                                     column=11,
                                                     value=allMetaValueCount)
                changeInMetadataToTopologySheet.cell(row=3,
                                                     column=11,
                                                     value='100%')

                allAttrVauleCount = self.compareScenarios.AllMetadataAttributesCount(
                    selectedDataset, selectedMasterNetworkName)
                changeInMetavaluesTopologySheet.cell(
                    row=2, column=5, value=count_first_source_attr)
                changeInMetavaluesTopologySheet.cell(
                    row=3,
                    column=5,
                    value=str(
                        round(
                            float(count_first_source_attr) * 100 /
                            float(allAttrVauleCount -
                                  count_common_source_attr), 2)) + '%')
                changeInMetavaluesTopologySheet.cell(
                    row=2, column=6, value=count_second_source_attr)
                changeInMetavaluesTopologySheet.cell(
                    row=3,
                    column=6,
                    value=str(
                        round(
                            float(count_second_source_attr) * 100 /
                            float(allAttrVauleCount -
                                  count_common_source_attr), 2)) + '%')
                changeInMetavaluesTopologySheet.cell(
                    row=2, column=7, value=count_first_method_attr)
                changeInMetavaluesTopologySheet.cell(
                    row=3,
                    column=7,
                    value=str(
                        round(
                            float(count_first_method_attr) * 100 /
                            float(allAttrVauleCount -
                                  count_common_method_attr), 2)) + '%')
                changeInMetavaluesTopologySheet.cell(
                    row=2, column=8, value=count_second_method_attr)
                changeInMetavaluesTopologySheet.cell(
                    row=3,
                    column=8,
                    value=str(
                        round(
                            float(count_second_method_attr) * 100 /
                            float(allAttrVauleCount -
                                  count_common_method_attr), 2)) + '%')
                rest_source_att = allAttrVauleCount - count_first_source_attr - count_second_source_attr
                changeInMetavaluesTopologySheet.cell(row=2,
                                                     column=9,
                                                     value=count_common_source)
                changeInMetavaluesTopologySheet.cell(
                    row=3,
                    column=9,
                    value=str(
                        round(
                            float(rest_source_att) * 100 /
                            float(allAttrVauleCount), 2)) + '%')
                rest_method_attr = allAttrVauleCount - count_first_method_attr - count_second_method_attr
                changeInMetavaluesTopologySheet.cell(row=2,
                                                     column=10,
                                                     value=count_common_method)
                changeInMetavaluesTopologySheet.cell(
                    row=3,
                    column=10,
                    value=str(
                        round(
                            float(rest_method_attr) * 100 /
                            float(allAttrVauleCount), 2)) + '%')
                changeInMetavaluesTopologySheet.cell(row=2,
                                                     column=11,
                                                     value=allAttrVauleCount)
                changeInMetavaluesTopologySheet.cell(row=3,
                                                     column=11,
                                                     value='100%')

                try:
                    book2.save(self.path)
                except Exception as e:
                    raise Exception(
                        "Permission denied!\nFileName : {}\nThis file is open or used by another application. Please close it first"
                        .format(e.filename))
            from Messages_forms.msg_successLoadDatabase import msg_successLoadDatabase

            instance = msg_successLoadDatabase(None)
            instance.m_staticText1.SetLabel(
                "\n \n The comparison and writing to Excel where successful. \n Check the excel file you provided to see the results"
            )
            instance.ShowModal()
            self.Destroy()
        except Exception as e:
            print e
            messageDlg = msg_somethigWrong(None, msg=e.message)
            messageDlg.ShowModal()
            # raise Exception(e.message)

    def checkConnectingToSqlite(self):
        db_setup = DB_Setup()
        if not db_setup.get_session():
            return False
        return True

    def btn_cancelOnButtonClick(self, event):
        self.Close()
class dlg_ServeToWEAP(ExportModels.dlg_ServeToWEAP):
    def __init__(self, parent):
        ExportModels.dlg_ServeToWEAP.__init__(self, parent)
        try:
            if not DB_Setup().get_session():
                msg = "\n\nWarning: Please connect to sqlite first."
                raise Exception(msg)
            ''' init combo model'''
            self.dataStructure = GetResourceStructure()
            self.datasets = self.dataStructure.GetResourceType()
            list_acromy = list()
            for index, row in self.datasets.iterrows():
                list_acromy.append(row[0])
            if list_acromy.__len__() > 0:
                self.Select_Model.SetItems(list_acromy)

            list_code = []
            self.WEAP = win32com.client.Dispatch("WEAP.WEAPApplication")
            for area in self.WEAP.Areas:
                list_code.append(area.name)

            self.comboBox_SelectArea.SetItems(list_code)

            list_scen = []
            for scen in self.WEAP.Scenarios:
                list_scen.append(scen.name)

            self.combo_selectWEAPScenario.SetItems(list_scen)
        except Exception as e:
            message = msg_somethigWrong(None, msg=e.message)
            message.Show()
            self.Destroy()

    # Handlers for dlg_ServeToWEAP events.
    def Select_ModelOnCombobox(self, event):
        # TODO: Implement Select_ModelOnCombobox
        selectedDataset = self.Select_Model.Value
        result = GetInstancesBySenario()
        datas = result.GetMasterNetworks(selectedDataset)
        GotMasterNetworkNames = []
        for index, row in datas.iterrows():
            GotMasterNetworkNames.append(row[0])
        self.SelectWaMDAM_Network.SetItems(GotMasterNetworkNames)

    def SelectWaMDAM_NetworkOnCombobox(self, event):
        # TODO: Implement SelectWaMDAM_NetworkOnCombobox
        # pass
        selectedMasterNetworkName = self.SelectWaMDAM_Network.Value
        result = GetInstancesBySenario()
        datas = result.GetScenarios(self.Select_Model.Value,
                                    selectedMasterNetworkName)
        GotScenarioNames = []
        for index, row in datas.iterrows():
            GotScenarioNames.append(row[0])
        self.Seclect_WaMDaM_Scenrio.SetItems(GotScenarioNames)

    def comboBox_SelectAreaOnCombobox(self, event):
        # TODO: Implement comboBox_SelectAreaOnCombobox
        pass

    def Seclect_WaMDaM_ScenrioOnCombobox(self, event):
        # TODO: Implement Seclect_WaMDaM_ScenrioOnCombobox
        pass

    def btn_serveOnButtonClick(self, event):
        # TODO: Implement btn_serveOnButtonClick
        try:
            msg = ''
            Model = self.Select_Model.Value
            if not Model:
                msg += "Select a wamdam model"

            WaMDAM_Network = self.SelectWaMDAM_Network.Value
            if not WaMDAM_Network:
                msg += "\nSelect a wamdam Network"

            WaMDaM_Scenrio = self.Seclect_WaMDaM_Scenrio.Value
            if not WaMDaM_Scenrio:
                msg += "\nSelect a wamdam scenario"

            SelectArea = self.comboBox_SelectArea.Value
            if not SelectArea:
                msg += "\nSelect a weap area"

            WEAPScenario = self.combo_selectWEAPScenario.Value
            if not WEAPScenario:
                NewWEAPScenario = self.Value_NewWEAPScenario.Value
                if not NewWEAPScenario:
                    msg += "\nselect an WEAP scenario name or input new scenario"
            if msg:
                msg_somethigWrong(None, msg=msg).Show()

            else:

                WEAP = win32com.client.Dispatch("WEAP.WEAPApplication")
                WEAP.ActiveArea = SelectArea
                WEAP.ActiveScenario = WEAPScenario
                self.btn_serve1.Enabled = False

                serveWaMDaM_Data_to_WEAP = ServeWaMDaM_Data_to_WEAP(
                    self.WEAP, Model, WaMDAM_Network, WaMDaM_Scenrio,
                    SelectArea, WEAPScenario)
                serveWaMDaM_Data_to_WEAP.load_data()

                self.btn_serve1.Enabled = True
                from viewer.Messages_forms.msg_connSQLiteSuccs import msg_connSQLiteSuccs
                msgdlg = msg_connSQLiteSuccs(self)
                msgdlg.setMessage("\n\nSuccessfully, extracted csv files")
                msgdlg.ShowModal()

                self.Destroy()

        except Exception as e:
            msg_somethigWrong(self, e.message).ShowModal()

    def btn_cancelOnButtonClick(self, event):
        # TODO: Implement btn_cancelOnButtonClick
        pass
    def btn_find_model_reqsOnButtonClick(self, event):
        # TODO: Implement btn_find_model_reqsOnButtonClick
        print 'this is starting'
        selectedDataset = self.comboBox_selectModel.Value

        message = ''
        if (selectedDataset == None or selectedDataset
                == '') and not ['xlsx', 'xlsm', 'xls'].__contains__(
                    self.path.split('.')[-1]):
            message = 'Select the model name in WamDam.\nAnd please select a valid excel file.'
        elif selectedDataset == None or selectedDataset == '':
            message = 'Select the model name in WamDam!!!'
        elif not ['xlsx', 'xlsm', 'xls'].__contains__(
                self.path.split('.')[-1]):
            message = 'And please select a valid excel file.'

        if message != '':
            messageDlg = msg_somethigWrong(None, msg=message)
            messageDlg.Show()
            return
            # raise Exception(message)
        ''' get objecttypes, attibutes, datasetType'''
        result = GetResourceStructure()
        data_result = result.GetObjectTypesByResource(selectedDataset)
        attributes_result = result.GetAttributesByResource(selectedDataset)
        dataset_result = result.GetResourceType(selectedDataset)
        print '************8**'
        try:
            if self.path.split('.')[-1] == 'xls':
                wb = open_workbook(self.path)
                # workbook = copy(wb)
                workbook = wb
                sheet = workbook.get_sheet(4)
                attribute_sheet = workbook.get_sheet(5)

                for row_id, row in enumerate(dataset_result):
                    for col_id, cell in enumerate(row):
                        sheet.write(row_id + 9, col_id + 0, cell)

                for row_id, row in enumerate(data_result):
                    for col_id, cell in enumerate(row):
                        sheet.write(row_id + 17, col_id + 0, cell)

                for row_id, row in enumerate(attributes_result):
                    for col_id, cell in enumerate(row):
                        attribute_sheet.write(row_id + 10, col_id + 0, cell)

                workbook.save(self.path)
            else:
                book2 = load_workbook(self.path)
                try:
                    sheet = book2.get_sheet_by_name("2.1_Datasets&ObjectTypes")
                    attribute_sheet = book2.get_sheet_by_name("2.2_Attributes")
                except:
                    raise Exception(
                        'Output Sheet {} not found in Excel File \n\n Please select valid Excel File'
                        .format("2.1_Datasets&ObjectTypes"))

                for row_id, row in enumerate(dataset_result):
                    for col_id, cell in enumerate(row):
                        sheet.cell(row=row_id + 10,
                                   column=col_id + 1,
                                   value=cell)
                for row_id, row in enumerate(data_result):
                    for col_id, cell in enumerate(row):
                        sheet.cell(row=row_id + 18,
                                   column=col_id + 1,
                                   value=cell)
                for row_id, row in enumerate(attributes_result):
                    for col_id, cell in enumerate(row):
                        attribute_sheet.cell(row=row_id + 11,
                                             column=col_id + 1,
                                             value=cell)

                book2.save(self.path)
            from viewer.Messages_forms.msg_successLoadDatabase import msg_successLoadDatabase
            instance = msg_successLoadDatabase(None)
            instance.m_staticText1.SetLabel("Success export excel file")
            instance.Show()
            self.Destroy()
        except Exception as e:
            print e
            messageDlg = msg_somethigWrong(None, msg=e.message)
            messageDlg.Show()
            raise Exception(e.message)

        print 'Great so far....'
class dlg_query_model(viewer.WaMDaMWizard.dlg_query_model):
    def __init__(self, parent):
        viewer.WaMDaMWizard.dlg_query_model.__init__(self, parent)
        self.path = ''
        try:
            if not DB_Setup().get_session():
                msg = "\n\nWarning: Please connect to sqlite first."
                raise Exception(msg)

            self.dataStructure = GetResourceStructure()
            self.datasets = self.dataStructure.GetResourceType()
            list_acromy = list()
            for row in self.datasets.values:
                list_acromy.append(row[0])
            if list_acromy.__len__() > 0:
                self.comboBox_selectModel.SetItems(list_acromy)
        except Exception as e:
            message = msg_somethigWrong(None, msg=e.message)
            message.ShowModal()
            self.Destroy()

    # Handlers for dlg_query_model events.
    def comboBox_selectModelOnCombobox(self, event):

        # TODO: Implement comboBox_selectModelOnCombobox
        pass

    def FilePicker_queryModelOnFileChanged(self, event):
        # TODO: Implement FilePicker_queryModelOnFileChanged
        valid_extension = ['xls', 'xlsx']
        self.path = self.FilePicker_queryModel.GetPath()
        if not (self.path.split('.')[-1] in valid_extension):
            self.Destroy()
            message = msg_somethigWrong(
                None,
                msg="A non excel file was selected, \n\n"
                " Please select a valid Excel File")
            message.Show()
        print 'This is working just fine...'
        print self.path

    def btn_find_model_reqsOnButtonClick(self, event):
        # TODO: Implement btn_find_model_reqsOnButtonClick
        print 'this is starting'
        selectedDataset = self.comboBox_selectModel.Value

        message = ''
        if (selectedDataset == None or selectedDataset
                == '') and not ['xlsx', 'xlsm', 'xls'].__contains__(
                    self.path.split('.')[-1]):
            message = 'Select the model name in WamDam.\nAnd please select a valid excel file.'
        elif selectedDataset == None or selectedDataset == '':
            message = 'Select the model name in WamDam!!!'
        elif not ['xlsx', 'xlsm', 'xls'].__contains__(
                self.path.split('.')[-1]):
            message = 'And please select a valid excel file.'

        if message != '':
            messageDlg = msg_somethigWrong(None, msg=message)
            messageDlg.Show()
            return
            # raise Exception(message)
        ''' get objecttypes, attibutes, datasetType'''
        result = GetResourceStructure()
        data_result = result.GetObjectTypesByResource(selectedDataset)
        attributes_result = result.GetAttributesByResource(selectedDataset)
        dataset_result = result.GetResourceType(selectedDataset)
        print '************8**'
        try:
            if self.path.split('.')[-1] == 'xls':
                wb = open_workbook(self.path)
                # workbook = copy(wb)
                workbook = wb
                sheet = workbook.get_sheet(4)
                attribute_sheet = workbook.get_sheet(5)

                for row_id, row in enumerate(dataset_result):
                    for col_id, cell in enumerate(row):
                        sheet.write(row_id + 9, col_id + 0, cell)

                for row_id, row in enumerate(data_result):
                    for col_id, cell in enumerate(row):
                        sheet.write(row_id + 17, col_id + 0, cell)

                for row_id, row in enumerate(attributes_result):
                    for col_id, cell in enumerate(row):
                        attribute_sheet.write(row_id + 10, col_id + 0, cell)

                workbook.save(self.path)
            else:
                book2 = load_workbook(self.path)
                try:
                    sheet = book2.get_sheet_by_name("2.1_Datasets&ObjectTypes")
                    attribute_sheet = book2.get_sheet_by_name("2.2_Attributes")
                except:
                    raise Exception(
                        'Output Sheet {} not found in Excel File \n\n Please select valid Excel File'
                        .format("2.1_Datasets&ObjectTypes"))

                for row_id, row in enumerate(dataset_result):
                    for col_id, cell in enumerate(row):
                        sheet.cell(row=row_id + 10,
                                   column=col_id + 1,
                                   value=cell)
                for row_id, row in enumerate(data_result):
                    for col_id, cell in enumerate(row):
                        sheet.cell(row=row_id + 18,
                                   column=col_id + 1,
                                   value=cell)
                for row_id, row in enumerate(attributes_result):
                    for col_id, cell in enumerate(row):
                        attribute_sheet.cell(row=row_id + 11,
                                             column=col_id + 1,
                                             value=cell)

                book2.save(self.path)
            from viewer.Messages_forms.msg_successLoadDatabase import msg_successLoadDatabase
            instance = msg_successLoadDatabase(None)
            instance.m_staticText1.SetLabel("Success export excel file")
            instance.Show()
            self.Destroy()
        except Exception as e:
            print e
            messageDlg = msg_somethigWrong(None, msg=e.message)
            messageDlg.Show()
            raise Exception(e.message)

        print 'Great so far....'

    def btn_cancelOnButtonClick(self, event):
        # TODO: Implement btn_cancelOnButtonClick
        self.Close()
Esempio n. 13
0
def UploadToOpenAgua(selectedResourceTypeAcro, selectedMasterNetworkName,
                     selectedScenarioNames, projectName):
    # selectedResourceTypeAcro='WASH'
    # selectedMasterNetworkName='Lower Bear River Network'
    # selectedScenarioName='base case scenario 2003'

    # default_network_name = ''
    # selectedScenarioName = ''

    # pass these as paramters input
    # selectedDataset, selectedMasterNetworkName, selectedScenarioName

    getDataStructure = GetResourceStructure()
    getInstances = GetInstancesBySenario()
    getValuesAll = GetAllValuesByScenario()

    log = logging.getLogger(__name__)

    # STEP 1: connect to the Hydra server
    # $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

    # Connect to the Hydra server on the local machine
    # More info: http://umwrg.github.io/HydraPlatform/tutorials/plug-in/tutorial_json.html#creating-a-client
    # url = "http://127.0.0.1:8080"
    # conn = JsonConnection(url)
    # # connects by default to 'localhost:8080'
    # conn.login("root", "")

    # Connect to the Hydra server on the local machine
    # More info: http://umwrg.github.io/HydraPlatform/tutorials/plug-in/tutorial_json.html#creating-a-client
    ur = "https://data.openagua.org"
    conn = JsonConnection(ur)
    login_response = conn.login('*****@*****.**',
                                'TestOpenAgua!')

    get_all_dimensions = conn.call('get_all_dimensions', ({}))

    # STEP 2: Import the WaMDaM workbook sheets
    # $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

    # type_sheet = wamdam_data['2.1_ResourceTypes&ObjectTypes']
    type_sheet_resourceTypes = getDataStructure.GetResourceType(
        selectedResourceTypeAcro)
    type_sheet_objectTypes = getDataStructure.GetObjectTypesByResource(
        selectedResourceTypeAcro)

    # Import the attributes
    # attr_sheet = wamdam_data['2.2_Attributes']
    attr_sheet = getDataStructure.GetAttributesByResource(
        selectedResourceTypeAcro)
    UnitsTable = GetUnits()
    UnitsTable_df = UnitsTable.GetUnits_dims()
    # STEP 3: Define a project in Hydra. Add the template "dataset name", Object Types and Attributes in Hydra
    # $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

    # Use the 'get_projects' call to check for an existing project of this name by listing all available projects.
    # The project concept does not exist in WaMDaM but it is needed in Hydra. We define it here
    projects = conn.call('get_projects', {})

    ## Load the new Project name to the Hydra db
    my_new_project = None
    for p in projects:
        if projectName == p.name:
            my_new_project = p
            projectID = p.id
            break
    if not my_new_project:
        my_new_project = conn.call(
            'add_project', {
                'project': {
                    'name': projectName,
                    'description ': 'add pro description'
                }
            })
        projectID = my_new_project.id

    # Add the attribute:
    # Attributes in Hydra are independent of ObjectTypes or templates types (they can be shared across object types)

    # Look all the unique attributes in 2.2_Attributes sheet.  Get the AttributeUnit for each attribute.

    # The "UnitType" in WaMDaM is equivalent to "dimension" in Hydra
    # my_new_attr_list = []
    # my_new_attr = conn.call('add_attribute', {'attr': {'name': ['attr'], 'dimension': ['Volume']}})

    all_attributes = conn.call('get_all_attributes', ({}))
    all_attr_dict = {}
    for a in all_attributes:
        all_attr_dict[(a.name, a.dimen)] = {'id': a.id, 'dimension': a.dimen}

    pd.DataFrame(all_attr_dict.items())

    # -------------------------

    # Create a new template (dataset)
    # This will fail second time around due to a unique naming restriction.
    # You should call 'get_templates' first and update an existing one if one with the same name is already there.

    # 2.1_Datasets&ObjectTypes sheet, look in the Datasets_table
    # DatasetName which is cell A10 in 2.1_Datasets&ObjectTypes sheet

    template = {
        'name': type_sheet_resourceTypes.values[0][1],
        'description': 'add description here',
        'types': []
    }  # insert the value of the "DatasetName" from excel
    # A template is equivalent to a dataset in wamdam

    # my_templates lists available templates. A template equates to the 'Dataset' in WaMDaM.
    # Go through this worksheet, building a hydra template
    my_templates = conn.call('get_template_attributes', {})

    # -----------------------------
    # Go through the excel sheet and pull out the template type definitions...
    # a template type in Hydra is equivalent to an Object Type in WaMDaM
    # resource_type in Hydra is equivalent to an ObjectTypology in WaMDaM
    # typeAttrs (the same as Template Type Attributes) links attributes to their template

    # 2.1_Datasets&ObjectTypes sheet, look in the ObjectTypes_table

    # iterate to get the object types and their attributes
    # start reading from row 16 because value is staring from 16 row.
    Dataset_attr_Name_Dim_list = {}
    for i in range(len(type_sheet_objectTypes.values)):

        if type_sheet_objectTypes.values[i][0] == "" or str(
                type_sheet_objectTypes.values[i][0]) == "None":
            break
            """"Raise an error message if the Unit Type is missing for any attribute"""

        #  type_sheet.values[i + 16][0]--ObjectType
        #  type_sheet.values[i + 16][1]--ObjectTypology

    #  Based on the link below, add a layout =Icon
    # http://umwrg.github.io/HydraPlatform/devdocs/HydraServer/index.html?highlight=typeattrs#HydraServer.soap_server.hydra_complexmodels.TemplateType

    # layout: {svg: "<svg>...</svg>"}
    # read value of layout from  "Layout" column in 2.1_Datasets&ObjectTypes.

        value_of_layout_in_type_sheet = {}
        #  type_sheet.values[i + 16][4]--Layout
        if type_sheet_objectTypes.values[i][4] != "" and str(
                type_sheet_objectTypes.values[i][4]) != "None":
            try:
                value_of_layout_in_type_sheet = loads(
                    '{' + type_sheet_objectTypes.values[i][4] + '}')
            except:
                raise Exception(
                    'JSON parse ERROR:\n Can not parse layout data of {} column, {} row in 2.1_Datasets&ObjectTypes to JSON data. Please check.'
                    .format('4', str(i + 16)))
        #----------reading end-------------------------#

        mytemplatetype = {
            'resource_type': type_sheet_objectTypes.values[i][1].upper(),
            'name': type_sheet_objectTypes.values[i][0],
            'typeattrs': [],
            'type_id': i + 1
        }
        #  insert the value of the ObjectTypology from excel. also insert the value of the ObjectType from excel

        # add value of layout in mytemplatetype.
        mytemplatetype['layout'] = value_of_layout_in_type_sheet
        #-------- adding end ------------------#

        # -------------------------------------

        for j in range(len(attr_sheet.values)):

            if type_sheet_objectTypes.values[i][0] == attr_sheet.values[j][0]:

                if attr_sheet.values[j][6] == 'AttributeSeries':
                    continue  # dont upload this attribute to the template. Its used in WaMDaM to keep track of the indiviual attributes
                    # for an array. But Hydra does not have it. Uploading it confused users.

                ObjectType = attr_sheet.values[j][0]  #ObjectType
                attr_name = attr_sheet.values[j][1]  #AttributeName

                AttributeUnitCV = attr_sheet.values[j][5]

                try:
                    # AttributeUnitCV = attr_sheet.values[j][5]
                    attr_dimension = UnitsTable_df.loc[
                        UnitsTable_df[0] == AttributeUnitCV].iloc[0, 1]

                    attr_unit = UnitsTable_df.loc[UnitsTable_df[0] ==
                                                  AttributeUnitCV].iloc[0, 2]

                    # attr_dimension = UnitsTable_df.loc[UnitsTable_df[2]==AttributeUnit].iloc[0,1]

                except:
                    msg = "There is a problem with looking up units \n. Invalid Key : {} \n Objectype:{}".format(
                        AttributeUnitCV, ObjectType)
                    raise Exception(msg)
                    # attr_dimension = ''

                # attr_unit=AttributeUnitCV
                # attr_unit=AttributeUnit

                # if not all_attr_dict.get(name,dimension) :
                # if not all_attr_dict.get(attr_name,attr_dimension) :
                if not (attr_name, attr_dimension) in all_attr_dict.keys(
                ):  #and all_attr_dict[(attr_name, attr_dimension)]['dimension'] == attr_dimension):
                    attr_id = conn.call(
                        'add_attribute',
                        {'attr': {
                            'name': attr_name,
                            'dimen': attr_dimension
                        }})['id']

                else:
                    tem = all_attr_dict[attr_name, attr_dimension]
                    # x=all_attr_dict.keys()
                    attr_id = tem['id']

                # Build a list that has attribute id, name, dimension to use it later to look up dimensions for each atrribute below.
                # Dataset_attr_Name_Dim=[ObjectType,attr_dimension,attr_name]
                Dataset_attr_Name_Dim_list[(ObjectType,
                                            attr_name)] = attr_dimension

                # connect the Template Type (ObjectType) with its Attributes
                # Based on the link below, add a unit =AttributeUnit, and a datatype=AttributeDataTypeCV
                # http://umwrg.github.io/HydraPlatform/devdocs/HydraServer/index.html?highlight=typeattrs#HydraServer.soap_server.hydra_complexmodels.TypeAttr

                # read value of unit from  "AttributeUnit" column in 2.2_Attributes.
                # attr_unit = attr_sheet.values[j][4]

                AttributeCategory = attr_sheet.values[j][7]

                # read value of datatype from  "AttributeDataTypeCV" column in 2.2_Attributes.
                attr_datatype = attr_sheet.values[j][6]
                if not attr_datatype:
                    attr_datatype = ''
                elif attr_datatype == 'MultiAttributeSeries':
                    attr_datatype = 'array'

                elif attr_datatype == 'SeasonalNumericValues':
                    attr_datatype = 'periodic timeseries'

                elif attr_datatype == 'NumericValues':
                    attr_datatype = 'scalar'

                elif attr_datatype == 'TimeSeries':
                    attr_datatype = 'timeseries'

                elif attr_datatype == 'FreeText':
                    attr_datatype = 'descriptor'

                elif attr_datatype == 'CategoricalValues':
                    attr_datatype = 'descriptor'
                #
                elif attr_datatype == 'AttributeSeries':
                    attr_datatype = 'array'

                AttributeScale = int(float(attr_sheet.values[j][10]))

                mytemplatetype['typeattrs'].append({
                    'type_id': i + 1,
                    'attr_is_var': True,
                    'attr_id': attr_id,
                    'data_type': attr_datatype,
                    'unit': attr_unit,
                    'properties': {
                        'category': AttributeCategory,
                        'scale': AttributeScale
                    }
                })
                # ,
            # --------------------------------------------
            # ,

            # Add some object types to the Template Type  (resource type can be NODE, LINK, GROUP, NETWORK)
        template['types'].append(mytemplatetype)
        # print mytemplatetype

    ## Load the Template name and types to the Hydra db
    tempDB = conn.call('get_templates', {})
    flag_exist_template = False
    for template_item in tempDB:
        if template_item['name'] == template['name']:
            print 'The template already exists in OpenAgua'

            flag_exist_template = True
            new_template = conn.call('get_template',
                                     {'template_id': template_item['id']})
            break

    #print template
    if not flag_exist_template:
        # if not flag_exist_template:
        # save template to csv to check if it has duplicates
        new_template = conn.call('add_template', {'tmpl': template})
        # print new_template
    print 'new_template is uploaded to the server'

    # STEP 4: Import WaMDaM Network, Nodes, and links
    # $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

    network_sheet = getInstances.GetMasterNetworks(selectedResourceTypeAcro)

    scenario_sheet = getInstances.GetScenarios(selectedResourceTypeAcro,
                                               selectedMasterNetworkName)

    for selectedScenarioName in selectedScenarioNames:
        networkName = network_sheet.values[0][0] + '_' + selectedScenarioName

        for templateType in new_template['types']:
            if templateType['resource_type'] == 'NETWORK':
                type_id = templateType['id']
                break

        network_template = {
            'template': template['name'],
            'name': networkName,
            'description': network_sheet.values[0][4],
            'project_id': projectID,
            'types': [{
                'id': type_id
            }]
        }
        networkName = network_sheet.values[0][0]
        # network_template
        # add_nodes
        # nodes_sheet = wamdam_data['3.2_Nodes']
        nodes_sheet = getInstances.GetNodesByScenario(
            selectedResourceTypeAcro, selectedMasterNetworkName,
            selectedScenarioName)

        list_node = []

        node_lookup = {}

        resource_attr_lookup = {}
        dict_res_attr = {}
        type_id = None
        res_id = -1

        # /////make dict_res_attr for network//////////

        network_res_attr = []
        for j in range(len(attr_sheet.values)):
            # if j < 9: continue  # Avoid headers before line 9 in the attribute sheet
            for templateType in new_template['types']:
                if templateType['resource_type'] == 'NETWORK':
                    if attr_sheet.values[j][0] == templateType['name']:
                        name = attr_sheet.values[j][1]
                        AttributeUnitCV = attr_sheet.values[j][5]
                        attr_dimension = UnitsTable_df.loc[
                            UnitsTable_df[0] == AttributeUnitCV].iloc[0, 1]
                        dimension = attr_dimension

                        res_attr = {
                            'ref_key': 'NETWORK',
                            'attr_id': all_attr_dict[(name, dimension)]['id'],
                            'id': res_id,
                        }
                        res_id -= 1
                        # resource_attr_lookup[('NETWORK', res_id)] = res_attr
                        network_res_attr.append(res_attr)
                        dict_res_attr[(networkName, name)] = res_attr
        network_template['attributes'] = network_res_attr
        # ///////////////////////////////////////////////////////////////////////////////////////

        # Iterate over the node instances and assign the parent Object Attributes to each node instance = ResourceAttribute (as in Hydra)
        for i in range(len(nodes_sheet.values)):

            # Look up the type_id in Hydra for each type
            for templateType in new_template['types']:
                if nodes_sheet.values[i][0] == templateType['name']:
                    type_id = templateType['id']
                    break

            if type_id is None:
                raise Exception(
                    "Unable to find a type in the template for %s" %
                    nodes_sheet[i][0])

            flag = False
            for node_item in list_node:
                if node_item['name'] == nodes_sheet.values[i][1]:
                    flag = True
            if flag: continue

            description = str(nodes_sheet.values[i][9])
            if description == "None":
                description = ""

            fullname = nodes_sheet.values[i][1]
            nameShort = nodes_sheet.values[i][1]

            node = {
                'id': i * -1,
                'name': nameShort,
                'description': description,
                'layout': {
                    'display_name': fullname
                },
                'x': str(nodes_sheet.values[i][7]),
                'y': str(nodes_sheet.values[i][8]),
                'types': [{
                    'id': type_id
                }]
            }
            node_res_attr = []
            for j in range(len(attr_sheet)):
                if nodes_sheet.values[i][0] == attr_sheet.values[j][0]:
                    name = attr_sheet.values[j][1]
                    AttributeUnitCV = attr_sheet.values[j][5]
                    attr_dimension = UnitsTable_df.loc[
                        UnitsTable_df[0] == AttributeUnitCV].iloc[0, 1]
                    dimension = attr_dimension

                    # res_id = (len(list_res_attr) + 1) * -1
                    if (name, dimension) in all_attr_dict.keys():
                        # look up the attribute id on the server based on the both name,dimension together
                        attr_id = all_attr_dict[(name, dimension)]['id']
                    else:
                        attr_id = conn.call(
                            'add_attribute',
                            {'attr': {
                                'name': name,
                                'dimen': dimension
                            }})['id']
                    res_attr = {
                        'ref_key': 'NODE',
                        'attr_id': attr_id,
                        'id': res_id,
                    }

                    res_id -= 1
                    resource_attr_lookup[('NODE', res_id)] = res_attr
                    node_res_attr.append(res_attr)
                    dict_res_attr[(nodes_sheet.values[i][1], name)] = res_attr

            node['attributes'] = node_res_attr
            list_node.append(node)
            node_lookup[node['name']] = node
        network_template['nodes'] = list_node

        link_lookup = {}

        links_sheet = getInstances.GetLinksByScenario(
            selectedResourceTypeAcro, selectedMasterNetworkName,
            selectedScenarioName)

        list_link = []
        type_id = None
        lst_name = []
        for i in range(len(links_sheet)):
            # if i < 8: continue  # Avoid headers before line 9 in the links sheet

            for templateType in new_template['types']:
                if links_sheet.values[i][0] == templateType['name']:
                    type_id = templateType['id']
                    break

            if type_id is None:
                raise Exception(
                    "Unable to find a type in the template for %s" %
                    links_sheet.values[i][0])
            description = str(links_sheet.values[i][9])
            if description == "None":
                description = ""

            name = links_sheet.values[i][1]
            fullname = links_sheet.values[i][1]

            if name in lst_name:
                # print name
                continue
            else:
                lst_name.append(name)

            link = {
                'id': i * -1,
                'name': name,
                'description': description,
                'layout': {
                    'display_name': fullname
                },
                'types': [{
                    'id': type_id
                }]
            }
            node_a = node_lookup.get(links_sheet.values[i][6])
            if node_a is None:
                raise Exception("Node %s could not be found" %
                                (links_sheet.values[i][6]))
            link['node_1_id'] = node_a['id']
            node_b = node_lookup.get(links_sheet.values[i][7])
            if node_b is None:
                raise Exception("Node %s could not be found" %
                                (links_sheet.values[i][7]))
            link['node_2_id'] = node_b['id']

            # ///// links resource atttribute///////
            link_res_attr = []
            for j in range(len(attr_sheet)):
                if links_sheet.values[i][0] == attr_sheet.values[j][0]:
                    name = attr_sheet.values[j][1]
                    AttributeUnitCV = attr_sheet.values[j][5]
                    attr_dimension = UnitsTable_df.loc[
                        UnitsTable_df[0] == AttributeUnitCV].iloc[0, 1]
                    dimension = attr_dimension

                    # res_id = (len(list_res_attr) + 1) * -1

                    res_attr = {
                        'ref_key': 'LINK',
                        'attr_id': all_attr_dict[(name, dimension)]['id'],
                        'id': res_id,
                    }
                    res_id -= 1
                    # resource_attr_lookup[('NODE', res_id)] = res_attr
                    link_res_attr.append(res_attr)
                    dict_res_attr[(links_sheet.values[i][1], name)] = res_attr
            link['attributes'] = link_res_attr

            list_link.append(link)
            link_lookup[link['name']] = link

        network_template['links'] = list_link
        network_template['resourcegroups'] = []

        # STEP 5: Import Scenarios and Data Values of Attributes for Nodes and links
        # $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

        # 5.1 add the scenario
        list_scenario = []
        for i in range(len(scenario_sheet)):
            # only add the selected scenario, one at a time.
            if scenario_sheet.values[i][0] == selectedScenarioName:
                description = str(scenario_sheet.values[i][8])
                if description == "None":
                    description = ""
        ###############################################################################################################

                startdate = scenario_sheet.values[i][4].replace('-', '/')

                try:

                    ScenarioStartDate = datetime.datetime.strptime(
                        str(startdate), "%Y/%m/%d").isoformat()
                except:
                    ScenarioStartDate = startdate.isoformat()

                # get month value from date (either numeric month like 1 or text month like January)
                # we use the value later as a flag to check
                ScenarioStartMonth = datetime.datetime.strptime(
                    ScenarioStartDate, '%Y-%m-%dT%H:%M:%S').month
                print ScenarioStartMonth
                ###############################################################################################################

                enddate = scenario_sheet.values[i][5].replace('-', '/')

                try:
                    ScenarioEndDate = datetime.datetime.strptime(
                        str(enddate), "%Y/%m/%d").isoformat()
                except:
                    ScenarioEndDate = enddate.isoformat()

                TimeStep = str(scenario_sheet.values[i][7])

                scenario = {
                    'name': selectedScenarioName,
                    'start_time': ScenarioStartDate,
                    'end_time': ScenarioEndDate,
                    'time_step': TimeStep,
                    'description': description,
                    'resourcescenarios': []
                }

        # Working with Datasets in Hydra which are equivalent to DataValues tables in WaMDaM
        # http://umwrg.github.io/HydraPlatform/tutorials/webservice/datasets.html?highlight=datasets

        # **************************************************
        # 5.2 Numeric Values
        numerical_sheet = getValuesAll.GetAllNumericValues(
            selectedResourceTypeAcro, selectedMasterNetworkName,
            selectedScenarioName)

        list_rs_num = []

        # Iterate over the rows in the Numeric Values sheet [scalars dataset] and associate the value with resource attribute (node instance and attribute)
        for j in range(len(numerical_sheet)):  # //8: reall value row in sheet
            Attr_name = numerical_sheet.values[j][3]
            ObjectType = numerical_sheet.values[j][0]
            Source = numerical_sheet.values[j][4]
            Method = numerical_sheet.values[j][5]

            # look up the dimension using Dataset_attr_Name_Dim based on (attr_name, ObjectType)
            dimension = Dataset_attr_Name_Dim_list[ObjectType, Attr_name]

            if (numerical_sheet.values[j][1],
                    numerical_sheet.values[j][3]) in dict_res_attr.keys():
                rs_num = {
                    'resource_attr_id':
                    dict_res_attr[(numerical_sheet.values[j][1],
                                   numerical_sheet.values[j][3])]['id']
                }
            else:
                raise Exception(
                    "Either the node or link names or the attribute provided in the numeric sheet are not defined earlier\n "
                    "Unable to find resource_attr_id in numerical_sheet for %s"
                    % numerical_sheet.values[j][3])

            metadata = {'source': Source, 'method': Method}

            dataset = {
                'type': 'scalar',
                'name': Attr_name,
                'metadata': json.dumps(metadata, ensure_ascii=True),
                'unit': attr_unit,
                'dimension': dimension,
                'hidden': 'N',
                'value': str(numerical_sheet.values[j][6])
            }
            # The provided dimension here must match the attribute as defined earlier.

            rs_num['value'] = dataset
            list_rs_num.append(rs_num)
        # associate the values, resources attributes to their scenario
        scenario['resourcescenarios'] = list_rs_num
        list_scenario.append(scenario)
        print 'Done with numeric values'

        # network_template['scenarios'] = list_scenario
        # #
        # network = conn.call('add_network', {'net':network_template})

        # ****************************************************
        # 5.3 4_FreeText Values (4_FreeText )
        # Iterate over the rows in the 4_FreeText sheet and associate the value with its scenario, and resource attribute
        Descriptor_sheet = getValuesAll.GetAllTextFree(
            selectedResourceTypeAcro, selectedMasterNetworkName,
            selectedScenarioName)
        # Descriptor_sheet = wamdam_data['4_FreeText']

        list_rs_desc = []
        # Iterate over the rows in the Numeric Values sheet [scalars dataset] and associate the value with resource attribute (node instance and attribute)
        for j in range(len(Descriptor_sheet.values)):
            ObjectType = Descriptor_sheet.values[j][0]  #ObjectType
            Attr_name = Descriptor_sheet.values[j][3]  #AttributeName
            dimension = Dataset_attr_Name_Dim_list[ObjectType, Attr_name]
            Source = numerical_sheet.values[j][4]
            Method = numerical_sheet.values[j][5]
            metadata = {'source': Source, 'method': Method}

            if (Descriptor_sheet.values[j][1],
                    Descriptor_sheet.values[j][3]) in dict_res_attr.keys():
                rs_desc = {
                    'resource_attr_id':
                    dict_res_attr[(Descriptor_sheet.values[j][1],
                                   Descriptor_sheet.values[j][3])]['id']
                }
            else:
                raise Exception(
                    "Either the node or link names or the attribute provided in the free text sheet are not defined earlier\n"
                    "Unable to find resource_attr_id in Free Text sheet for %s"
                    % Descriptor_sheet.values[j][3])

            dataset = {
                'type': 'descriptor',
                'name': Attr_name,
                'unit': attr_unit,
                'dimension': dimension,
                'metadata': json.dumps(metadata, ensure_ascii=True),
                'hidden': 'N',
                'value': str(Descriptor_sheet.values[j][6])
            }
            # print dataset
            # The provided dimension here must match the attribute as defined earlier.

            rs_desc['value'] = dataset
            list_rs_desc.append(rs_desc)
        # associate the values, resources attributes to their scenario

        list_scenario[0]['resourcescenarios'].extend(list_rs_desc)
        print 'Done with Free text'

        # ******************************************************************************************************************

        # 5.7 Seasonal

        # http://umwrg.github.io/HydraPlatform/devdocs/techdocs/timeseries.html?highlight=seasonal#normal-time-series-and-seasonal-time-series

        # SeasonalNumericValues_sheet = wamdam_data['4_SeasonalNumericValues']
        SeasonalNumericValues_sheet = getValuesAll.GetAllSeasonalNumericValues(
            selectedResourceTypeAcro, selectedMasterNetworkName,
            selectedScenarioName)

        # add new script here (see time series above)

        seasonal_list = {}
        list_rs_seas = []
        for k in range(len(SeasonalNumericValues_sheet)):
            attr_name = SeasonalNumericValues_sheet.values[k][3]
            if (SeasonalNumericValues_sheet.values[k][1],
                    attr_name) in seasonal_list.keys():
                seasonal_list[(SeasonalNumericValues_sheet.values[k][1],
                               attr_name)].append(
                                   (SeasonalNumericValues_sheet.values[k][7],
                                    SeasonalNumericValues_sheet.values[k][8]))
            else:
                values = []
                values.append((SeasonalNumericValues_sheet.values[k][7],
                               SeasonalNumericValues_sheet.values[k][8]))
                seasonal_list[(SeasonalNumericValues_sheet.values[k][1],
                               attr_name)] = values
        # df = pd.DataFrame()
        for key in seasonal_list.keys():
            seasonals = {"Header": {}, "0": {}}
            time_date = ''
            for time, value in seasonal_list[key]:
                try:

                    if time == 'October' and ScenarioStartMonth == 10:
                        time_date = '9998/10/1'

                    elif time == 'October' and ScenarioStartMonth == 1:
                        time_date = '9999/10/1'

                    elif time == 'November' and ScenarioStartMonth == 10:
                        time_date = '9998/11/1'

                    elif time == 'November' and ScenarioStartMonth == 1:
                        time_date = '9999/11/1'

                    elif time == 'December' and ScenarioStartMonth == 10:
                        time_date = '9998/12/1'

                    elif time == 'December' and ScenarioStartMonth == 1:
                        time_date = '9999/12/1'

                    elif time == 'January':
                        time_date = '9999/01/1'

                    elif time == 'February':
                        time_date = '9999/02/1'

                    elif time == 'March':
                        time_date = '9999/03/1'

                    elif time == 'April':
                        time_date = '9999/04/1'

                    elif time == 'May':
                        time_date = '9999/05/1'

                    elif time == 'June':
                        time_date = '9999/06/1'

                    elif time == 'July':
                        time_date = '9999/07/1'

                    elif time == 'August':
                        time_date = '9999/08/1'

                    elif time == 'September':
                        time_date = '9999/09/1'

                    t = datetime.datetime.strptime(str(time_date),
                                                   "%Y/%m/%d").isoformat()
                except:
                    t = time_date.isoformat()
                seasonals["0"][t] = value

            # dimension = all_attr_dict[key[1]]['dimension']
            Attr_name = SeasonalNumericValues_sheet.values[0][3]
            ObjectType = SeasonalNumericValues_sheet.values[0][0]

            dimension = Dataset_attr_Name_Dim_list[ObjectType, Attr_name]
            Source = numerical_sheet.values[j][4]
            Method = numerical_sheet.values[j][5]
            metadata = {'source': Source, 'method': Method}

            if key in dict_res_attr.keys():
                rs_seas = {'resource_attr_id': dict_res_attr[key]['id']}
            else:
                raise Exception(
                    "Either the node or link names or the attribute provided in the seasonal sheet are not defined earlier\n"
                    "Unable to find resource_attr_id in seasonal sheet for %s"
                    % key)

            # rs = {'resource_attr_id': all_attr_dict[attr_name]['id']}

            # print 'done with '+ Attr_name
            dataset = {
                'type': 'timeseries',
                'name': Attr_name,
                'unit': attr_unit,
                'dimension': dimension,
                'metadata': json.dumps(metadata, ensure_ascii=True),
                'hidden': 'N',
                'value': json.dumps(seasonals)
            }
            # The provided dimension here must match the attribute as defined earlier.

            rs_seas['value'] = dataset
            list_rs_seas.append(rs_seas)
        # associate the values, resources attributes to their scenario

        list_scenario[0]['resourcescenarios'].extend(list_rs_seas)
        print 'Done with seasonal values'

        # 5.5 Time Series
        # Iterate over the rows in the 4_TimeSeriesValues sheet and associate the value with its scenario, and resource attribute
        # Reference for time series in Hydra: follow this logic
        # http://umwrg.github.io/HydraPlatform/devdocs/techdocs/timeseries.html#an-example-in-python

        # TimeSeriesValues_sheet = wamdam_data['4_TimeSeriesValues']
        TimeSeriesValues_sheet = getValuesAll.GetAllTimeSeriesValues(
            selectedResourceTypeAcro, selectedMasterNetworkName,
            selectedScenarioName)

        # Iterate over the rows in the TimeSeriesValues sheet [dataset] and associate the value with resource attribute
        # (node instance and attribute)
        timeseries_list = {}
        list_rs_ts = []

        for j in range(len(TimeSeriesValues_sheet.values)):
            if not TimeSeriesValues_sheet.values[j][4]:
                continue
            attr_name = TimeSeriesValues_sheet.values[j][3]
            if (TimeSeriesValues_sheet.values[j][1],
                    attr_name) in timeseries_list.keys():
                timeseries_list[(TimeSeriesValues_sheet.values[j][1],
                                 attr_name)].append(
                                     (TimeSeriesValues_sheet.values[j][4],
                                      TimeSeriesValues_sheet.values[j][5]))
            else:
                values = []
                values.append((TimeSeriesValues_sheet.values[j][4],
                               TimeSeriesValues_sheet.values[j][5]))
                timeseries_list[(TimeSeriesValues_sheet.values[j][1],
                                 attr_name)] = values

        for key in timeseries_list.keys():
            timeseries = {"Header": {}, "0": {}}
            for time, value in timeseries_list[key]:
                if not time:
                    continue
                if value == -9999:
                    value = ''
                try:
                    if isinstance(time, datetime.datetime):
                        ts = time.isoformat()
                    else:
                        ts = datetime.datetime.strptime(
                            str(time), "%d/%m/%y").isoformat()
                    timeseries["0"][ts] = value
                except:
                    ts = datetime.datetime.strptime(str(time),
                                                    "%Y-%m-%d").isoformat()

                    timeseries["0"][ts] = value

            ObjectType = TimeSeriesValues_sheet.values[j][0]
            Attr_name = TimeSeriesValues_sheet.values[j][3]
            dimension = Dataset_attr_Name_Dim_list[ObjectType, Attr_name]

            Source = TimeSeriesValues_sheet.values[j][6]
            Method = TimeSeriesValues_sheet.values[j][7]

            metadata = {'source': Source, 'method': Method}

            if key in dict_res_attr.keys():
                rs_ts = {'resource_attr_id': dict_res_attr[key]['id']}
            else:
                raise Exception(
                    "Either the node or link names or the attribute provided in the time series sheet are not defined earlier\n"
                    "Unable to find resource_attr_id in time series values sheet for %s"
                    % key)

            dataset = {
                'type': 'timeseries',
                'name': Attr_name,
                'unit': attr_unit,
                'dimension': dimension,
                'metadata': json.dumps(metadata, ensure_ascii=True),
                'hidden': 'N',
                'value': json.dumps(timeseries)
            }
            # The provided dimension here must match the attribute as defined earlier.

            rs_ts['value'] = dataset
            list_rs_ts.append(rs_ts)
        # associate the values, resources attributes to their scenario

        list_scenario[0]['resourcescenarios'].extend(list_rs_ts)
        print 'Done with Time Series'

        # ********************************************************
        # 5.6 Arrays
        # Iterate over the rows in the MultiColumns Series sheet and associate the value with its scenario, and resource attribute

        # Reference for arrays in Hydra (not clear to me yet, an example would help)
        #	http://umwrg.github.io/HydraPlatform/tutorials/webservice/datasets.html?highlight=arrays#array-format

        # Will add the array values for each attribute
        # Will add the array values for each attribute
        # Here Column D in excel starting row 19 has the attribute name that for the whole array (its just like the attribute for the descriptor)
        # Columns G, H,....L, etc stwh have the names of the Array "items" or sub-attributes
        # so each value belongs to an Attribute (array name) and a sub-attribute (array item) under an ObjectType and Instance name

        multiAttr_sheet_up_df, multiAttr_sheet_bottom_df = getValuesAll.GetAllMultiAttributeSeries(
            selectedResourceTypeAcro, selectedMasterNetworkName,
            selectedScenarioName)

        list_rs_multi = []

        if not multiAttr_sheet_bottom_df.empty:

            subsets = multiAttr_sheet_bottom_df.groupby(
                ['ObjectType', 'InstanceName', 'AttributeName'])

            for subset in subsets.groups.keys():

                dt = subsets.get_group(name=subset)
                ObjectType = dt['ObjectType'].values[0]
                InstanceName = dt['InstanceName'].values[0]
                Attribute_name = dt['AttributeName'].values[0]
                Source = dt['SourceName'].values[0]
                Method = dt['MethodName'].values[0]
                metadata = {'source': Source, 'method': Method}

                ValuesNumColumns = len(dt.columns)

                Values_df = dt[dt.columns[6:ValuesNumColumns]]
                Values_df = Values_df.dropna(axis=1, how='all')
                templist = Values_df.values.T.tolist()

                if key in dict_res_attr.keys():
                    rs_ts = {'resource_attr_id': dict_res_attr[key]['id']}
                else:
                    raise Exception(
                        "Either the node or link names or the attribute provided in the multicolumns sheet are not defined earlier\n"
                        "Unable to find resource_attr_id in multicolumns values sheet for %s"
                        % key)

                if (InstanceName, Attribute_name) in dict_res_attr.keys():
                    dimension = Dataset_attr_Name_Dim_list[ObjectType,
                                                           Attribute_name]

                    rs_multi = {
                        'resource_attr_id':
                        dict_res_attr[(InstanceName, Attribute_name)]['id']
                    }

                    dataset = {
                        'type': 'array',
                        'name': Attribute_name,
                        'unit': attr_unit,
                        'dimension': dimension,
                        'metadata': json.dumps(metadata, ensure_ascii=True),
                        'hidden': 'N',
                        'value': json.dumps(templist)
                    }
                    rs_multi['value'] = dataset
                    list_rs_multi.append(rs_multi)

        list_scenario[0]['resourcescenarios'].extend(list_rs_multi)
        print 'Done with multi column arrays'

        network_template['scenarios'] = list_scenario

        network = conn.call('add_network', {'net': network_template})

        print 'successfully uploaded a WaMDaM network and its data to OpenAgua'
Esempio n. 14
0
def UploadToOpenAgua(selectedResourceTypeAcro, selectedMasterNetworkName,
                     selectedScenarioNames, projectName, username_oa,
                     password_oa):
    # selectedResourceTypeAcro='WASH'
    # selectedMasterNetworkName='Lower Bear River Network'
    # selectedScenarioName='base case scenario 2003'
    endpoint = 'https://www.openagua.org/api/v1/hydra/'

    hydra = Hydra_OA(endpoint, username=username_oa, password=password_oa)

    # default_network_name = ''
    # selectedScenarioName = ''

    # pass these as paramters input
    # selectedDataset, selectedMasterNetworkName, selectedScenarioName

    getDataStructure = GetResourceStructure()
    getInstances = GetInstancesBySenario()

    # STEP 1: connect to the Hydra server
    # $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

    # Connect to the Hydra server on the local machine
    # More info: http://umwrg.github.io/HydraPlatform/tutorials/plug-in/tutorial_json.html#creating-a-client
    # url = "http://127.0.0.1:8080"
    # conn = JsonConnection(url)
    # # connects by default to 'localhost:8080'
    # conn.login("root", "")

    # Connect to the Hydra server on the local machine
    # More info: http://umwrg.github.io/HydraPlatform/tutorials/plug-in/tutorial_json.html#creating-a-client

    # Network = hydra_call(username_oa, password_oa, 'get_network',
    #                     {'network_id': Selected_network_id, 'scenario_id': Selected_scenario_id, 'include_values': 'N',
    #                      'summary': 'Y'})

    # get_all_dimensions=hydra_call(username_oa, password_oa,'get_all_dimensions',{})

    Server_all_dimensions = hydra.call('get_dimensions')

    # Server_all_dimensions_df=pd.DataFrame(Server_all_dimensions)

    # STEP 2: Import the WaMDaM workbook sheets
    # $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

    # type_sheet = wamdam_data['2.1_ResourceTypes&ObjectTypes']
    type_sheet_resourceTypes = getDataStructure.GetResourceType(
        selectedResourceTypeAcro)
    type_sheet_objectTypes = getDataStructure.GetObjectTypesByResource(
        selectedResourceTypeAcro)

    # Import the attributes
    # attr_sheet = wamdam_data['2.2_Attributes']
    attr_sheet = getDataStructure.GetAttributesByResource(
        selectedResourceTypeAcro)
    UnitsTable = GetUnits()
    UnitsTable_df = UnitsTable.GetUnits_dims()

    # STEP 3: Define a project in Hydra. Add the template "dataset name", Object Types and Attributes in Hydra
    # $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

    # Use the 'get_projects' call to check for an existing project of this name by listing all available projects.
    # The project concept does not exist in WaMDaM but it is needed in Hydra. We define it here
    projects = hydra.call('get_projects')

    ## Load the new Project name to the Hydra db
    my_new_project = None

    User5char = username_oa[0:4]
    for p in projects:
        if projectName == p['name']:
            my_new_project = p
            projectID = p['id']
            break

    if not my_new_project:
        my_new_project = hydra._call('add_project', {
            'name': projectName,
            'description ': 'add pro description'
        })
        projectID = my_new_project['id']

    # Add the attribute:
    # Attributes in Hydra are independent of ObjectTypes or templates types (they can be shared across object types)

    # Look all the unique attributes in 2.2_Attributes sheet.  Get the AttributeUnit for each attribute.

    # The "Category" in WaMDaM is equivalent to "dimension" in Hydra
    # my_new_attr_list = []
    # my_new_attr = conn.call('add_attribute', {'attr': {'name': ['attr'], 'dimension': ['Volume']}})

    # all_attributes = hydra.call('get_attributes')
    #
    # all_attr_dict = {}
    # for a in all_attributes:
    #     dimension_data = hydra.call('get_dimension',{'dimension_id': a['dimension_id']})
    #     all_attr_dict[(a['name'], dimension_data['name'])] = {'id': a['id'], 'dimension': dimension_data['name']}
    #
    # pd.DataFrame(all_attr_dict.items())

    # -------------------------

    # Create a new template (dataset)
    # This will fail second time around due to a unique naming restriction.
    # You should call 'get_templates' first and update an existing one if one with the same name is already there.

    # 2.1_Datasets&ObjectTypes sheet, look in the Datasets_table
    # DatasetName which is cell A10 in 2.1_Datasets&ObjectTypes sheet

    # concatnate part of the user name into the project name to make globally unique in HydraPlatform
    GlobalTemplate = type_sheet_resourceTypes.values[0][1]  #+'_'+User5char

    template = {
        'name': GlobalTemplate,
        'description': 'add description here',
        'templatetypes': []
    }  # insert the value of the "DatasetName" from excel
    # A template is equivalent to a dataset in wamdam

    # my_templates lists available templates. A template equates to the 'Dataset' in WaMDaM.
    # Go through this worksheet, building a hydra template
    # my_templates = hydra._call('get_template_attributes', {})

    # -----------------------------
    # Go through the excel sheet and pull out the template type definitions...
    # a template type in Hydra is equivalent to an Object Type in WaMDaM
    # resource_type in Hydra is equivalent to an ObjectTypology in WaMDaM
    # typeAttrs (the same as Template Type Attributes) links attributes to their template

    # 2.1_Datasets&ObjectTypes sheet, look in the ObjectTypes_table

    # iterate to get the object types and their attributes
    # start reading from row 16 because value is staring from 16 row.
    Dataset_attr_Name_Dim_list = {}
    Dataset_attr_Name_Dim_unit = {}
    attr_ID_by_Unit_attrName_list = {}

    # use the unites later in the code
    HydraUnits = hydra.call('get_units')

    print 'looping through the object types'
    for i in range(len(type_sheet_objectTypes.values)):

        if type_sheet_objectTypes.values[i][0] == "" or str(
                type_sheet_objectTypes.values[i][0]) == "None":
            break
            """"Raise an error message if the Unit Type is missing for any attribute"""

        #  type_sheet.values[i + 16][0]--ObjectType
        #  type_sheet.values[i + 16][1]--ObjectTypology

    #  Based on the link below, add a layout =Icon
    # http://umwrg.github.io/HydraPlatform/devdocs/HydraServer/index.html?highlight=typeattrs#HydraServer.soap_server.hydra_complexmodels.TemplateType

    # layout: {svg: "<svg>...</svg>"}
    # read value of layout from  "Layout" column in 2.1_Datasets&ObjectTypes.

        value_of_layout_in_type_sheet = {}
        #  type_sheet.values[i + 16][4]--Layout
        if type_sheet_objectTypes.values[i][4] != "" and str(
                type_sheet_objectTypes.values[i][4]) != "None":
            try:
                value_of_layout_in_type_sheet = loads(
                    '{' + type_sheet_objectTypes.values[i][4] + '}')
            except:
                raise Exception(
                    'JSON parse ERROR:\n Can not parse layout data of {} column, {} row in 2.1_Datasets&ObjectTypes to JSON data. Please check.'
                    .format('4', str(i + 16)))
        #----------reading end-------------------------#

        mytemplatetype = {
            'resource_type': type_sheet_objectTypes.values[i][1].upper(),
            'name': type_sheet_objectTypes.values[i][0],
            'typeattrs': [],
            'type_id': i + 1
        }
        #  insert the value of the ObjectTypology from excel. also insert the value of the ObjectType from excel

        # add value of layout in mytemplatetype.
        mytemplatetype['layout'] = value_of_layout_in_type_sheet
        #-------- adding end ------------------#

        # -------------------------------------

        for j in range(len(attr_sheet.values)):

            if type_sheet_objectTypes.values[i][0] == attr_sheet.values[j][0]:

                # if attr_sheet.values[j][6] == 'AttributeSeries':
                #     continue # dont upload this attribute to the template. Its used in WaMDaM to keep track of the individual attributes
                # for an array. But Hydra does not have it. Uploading it confused users.

                ObjectType = attr_sheet.values[j][0]  #ObjectType
                attr_name = attr_sheet.values[j][1]  #AttributeName

                # if attr_name =='Elevation-Curve' or attr_name =='Volume-Curve':
                #     continue

                AttributeUnitCV = attr_sheet.values[j][5]

                try:
                    for server_dim in Server_all_dimensions:
                        units_on_server = server_dim['units']
                        for each_units_on_server in units_on_server:
                            server_unit_name = each_units_on_server['name']

                            if AttributeUnitCV == None:
                                print attr_name + 'for' + ObjectType + 'is required but not provided'
                            elif AttributeUnitCV == server_unit_name:
                                server_dim_name = server_dim['name']

                                server_unit_id = each_units_on_server['id']
                                server_dim_id = each_units_on_server[
                                    'dimension_id']

                                # if server_unit_id == 243:
                                print ObjectType
                                print server_unit_name
                                print attr_name
                                print server_dim_name
                                print '------------------'
                                break
                                # if server_unit_id==243:
                                #     print server_unit_name
                                #     print attr_name
                                #     break
                except:
                    print 'there is a problem in units. The provided unit in WaMDaM does not exist in the Hydra Server'

                # u'{\'message\': \'Unit 243 (abbreviation=unk) has dimension_id 23(name=dimensionless), but attribute has no dimension\', \'code\': \'0000\'}'

                # try:
                #     # AttributeUnitCV = attr_sheet.values[j][5]
                #     # attr_dimension = UnitsTable_df.loc[UnitsTable_df[0]==AttributeUnitCV].iloc[0,1]
                #     #
                #     # attr_unit = UnitsTable_df.loc[UnitsTable_df[0]==AttributeUnitCV].iloc[0,0]
                #     #
                #     # # look up dimension id from its name
                #     # attr_dimension_id=Server_all_dimensions_df.loc[Server_all_dimensions_df['name'] == attr_dimension]['id'].item()
                #
                #     # print attr_dimension_id
                #     # attr_dimension = UnitsTable_df.loc[UnitsTable_df[2]==AttributeUnit].iloc[0,1]
                #
                # # https://stackoverflow.com/questions/30787901/how-to-get-a-value-from-a-pandas-dataframe-and-not-the-index-and-object-type
                #
                # except:
                #     msg = "There is a problem with looking up units \n. Invalid Key : {} \n Objectype:{}".format(AttributeUnitCV , ObjectType)
                #     raise Exception(msg)
                # attr_dimension = ''

                # attr_unit=AttributeUnitCV
                # attr_unit=AttributeUnit

                # if not all_attr_dict.get(name,dimension) :
                # if not all_attr_dict.get(attr_name,attr_dimension) :

                # change this
                # We need to add a try statement here. try to add an attribute. if it exists, then we look up the existing attribute id and its dimension

                # try:
                # https: // github.com / openagua / hydra - base / blob / master / hydra_base / lib / attributes.py  # L120

                # if not (attr_name, attr_dimension) in all_attr_dict.keys(): #and all_attr_dict[(attr_name, attr_dimension)]['dimension'] == attr_dimension):

                try:
                    # add the attribute. If it exists, then get its id and reuse it

                    resp = hydra._call('add_attribute', {
                        'name': attr_name,
                        'dimension_id': server_dim_id
                    })
                    if resp and 'id' in resp.keys():
                        server_attr_id = resp['id']

                    else:
                        server_attr = hydra.call(
                            'get_attribute_by_name_and_dimension', {
                                'name': attr_name,
                                'dimen': server_dim_name
                            })
                        server_attr_id = server_attr['id']

                except:
                    server_attr = hydra.call(
                        'get_attribute_by_name_and_dimension', {
                            'name': attr_name,
                            'dimen': server_dim_name
                        })
                    server_attr_id = server_attr['id']

                # # append thr attribute, its diminsion and id to the all_attr_dict so we can use it later to look up attribute in the resource
                # all_attr_dict

                # Build a list that has attribute id, name, dimension to use it later to look up dimensions for each atrribute below.
                # Dataset_attr_Name_Dim=[ObjectType,attr_dimension,attr_name]
                Dataset_attr_Name_Dim_list[(ObjectType,
                                            attr_name)] = server_dim_name

                Dataset_attr_Name_Dim_unit[(ObjectType,
                                            attr_name)] = server_unit_name

                # create a dataframe for all these colums together. Now there is an issue in looking up them in these two seperate lists

                attr_ID_by_Unit_attrName_list[(AttributeUnitCV,
                                               attr_name)] = server_attr_id

                # server_unit_id
                # server_dim_id
                # server_unit_name
                # server_dim_name

                # connect the Template Type (ObjectType) with its Attributes
                # Based on the link below, add a unit =AttributeUnit, and a datatype=AttributeDataTypeCV
                # http://umwrg.github.io/HydraPlatform/devdocs/HydraServer/index.html?highlight=typeattrs#HydraServer.soap_server.hydra_complexmodels.TypeAttr

                # read value of unit from  "AttributeUnit" column in 2.2_Attributes.
                # attr_unit = attr_sheet.values[j][4]

                AttributeCategory = attr_sheet.values[j][7]

                # read value of datatype from  "AttributeDataTypeCV" column in 2.2_Attributes.
                attr_datatype = attr_sheet.values[j][6]
                if not attr_datatype:
                    attr_datatype = ''
                elif attr_datatype == 'MultiAttributeSeries':
                    attr_datatype = 'array'

                elif attr_datatype == 'SeasonalNumericValues':
                    attr_datatype = 'periodic timeseries'

                elif attr_datatype == 'NumericValues':
                    attr_datatype = 'scalar'

                elif attr_datatype == 'TimeSeries':
                    attr_datatype = 'timeseries'

                elif attr_datatype == 'FreeText':
                    attr_datatype = 'descriptor'

                elif attr_datatype == 'CategoricalValues':
                    attr_datatype = 'descriptor'
                #
                elif attr_datatype == 'AttributeSeries':
                    attr_datatype = 'array'

                AttributeScale = int(float(attr_sheet.values[j][10]))

                ModelInputOrOutput = attr_sheet.values[j][8]
                if ModelInputOrOutput == 'Output':
                    ModelInputOrOutput = 'Y'  # "Y" (output)
                    Display_data = True
                else:
                    ModelInputOrOutput = 'N'  #"N" (input) By default its input
                    Display_data = False

                #Get the unit_id from the Hydra server
                # for uni in HydraUnits:
                #     if uni['name']==AttributeUnitCV and attr_dimension_id==uni['dimension_id']:
                #         unit_id=uni['id']
                # else:
                #     print "this unit does not exist in Hydra"
                #     print  AttributeUnitCV

                mytemplatetype['typeattrs'].append({
                    'type_id': i + 1,
                    'is_var': ModelInputOrOutput,
                    'attr_id': server_attr_id,
                    'data_type': attr_datatype,
                    'unit_id': server_unit_id,
                    'properties': {
                        'category': AttributeCategory,
                        'scale': AttributeScale,
                        'save': Display_data
                    }
                })
                # ,
            # --------------------------------------------
            # ,

            # Add some object types to the Template Type  (resource type can be NODE, LINK, GROUP, NETWORK)
        if 'templatetypes' in template.keys():
            template['templatetypes'].append(mytemplatetype)
        else:
            print('there is no key- templatetypes.')
        # print mytemplatetype

    ## Load the Template name and types to the Hydra db
    tempDB = hydra._call('get_templates')
    flag_exist_template = False

    # here I want a dataframe from the above for loop that has all the columns with values together.
    # Then, later in code, I need to look up these values based on one or two I know.
    # now there is a confusion in values because I'm looking things up differently.
    # temp = copy.deepcopy(Dataset_attr_Name_Dim_list)
    # temp.update(Dataset_attr_Name_Dim_unit)
    # [ObjectType, attr_name, server_dim_name, server_unit_name, server_attr_id]

    for template_item in tempDB:
        if template_item['name'] == template['name']:
            template_item_id = template_item['id']
            print 'The template already exists in OpenAgua'

            flag_exist_template = True
            new_template = hydra.call('get_template',
                                      {'template_id': template_item['id']})
            break

    #print template
    if not flag_exist_template:
        # if not flag_exist_template:
        template['project_id'] = projectID

        # u'{\'message\': \'Unit 6 (abbreviation=ft) has dimension_id 1(name=Length), but attribute has dimension_id 23(name=dimensionless)\', \'code\': \'0000\'}'
        # u'{\'message\': \'Unit 243 (abbreviation=unk) has dimension_id 23(name=dimensionless), but attribute has no dimension\', \'code\': \'0000\'}'

        new_template = hydra._call(
            'add_template', {
                'name': template['name'],
                'project_id': projectID,
                'templatetypes': template['templatetypes'],
                'description': template['description']
            })
        print new_template
        print new_template['name']
        template_item_id = new_template['id']
        print 'is uploaded to the server as a new one'

    # STEP 4: Import WaMDaM Network, Nodes, and links
    # $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

    network_sheet = getInstances.GetMasterNetworks(selectedResourceTypeAcro)
    NetworkDescription = ''
    for i in range(len(
            network_sheet.values)):  # get the number of rows (i.e., networks)
        if selectedMasterNetworkName == network_sheet.values[i][0]:
            NetworkDescription = network_sheet.values[i][4]

    # network_template
    # add_nodes
    # nodes_sheet = wamdam_data['3.2_Nodes']
    for templateType in new_template['templatetypes']:
        if templateType['resource_type'] == 'NETWORK':
            type_id = templateType['id']
            network_template = {
                'name': selectedMasterNetworkName,
                'description': NetworkDescription,
                'project_id': projectID,
                'types': [{
                    'id': type_id
                }],
                'layout': {
                    'active_template_id': template_item_id
                }
            }
            break

    list_node = []

    node_lookup = {}

    resource_attr_lookup = {}
    dict_res_attr = {}
    type_id = None
    res_id = -1

    # /////make dict_res_attr for network//////////

    network_res_attr = []
    for j in range(len(attr_sheet.values)):
        for templateType in new_template['templatetypes']:
            if templateType['resource_type'] == 'NETWORK':
                ObjectType = attr_sheet.values[j][0]
                if ObjectType == templateType['name']:
                    name = attr_sheet.values[j][1]
                    AttributeUnitCV = attr_sheet.values[j][5]

                    attribute_server_id = attr_ID_by_Unit_attrName_list[(
                        AttributeUnitCV, name)]

                    res_attr = {
                        'ref_key': 'NETWORK',
                        'attr_id':
                        attribute_server_id,  # this look up is not working
                        'id': res_id,
                        'attr_is_var': 'Y'
                    }
                    res_id -= 1
                    # resource_attr_lookup[('NETWORK', res_id)] = res_attr
                    network_res_attr.append(res_attr)
                    dict_res_attr[(selectedMasterNetworkName, name)] = res_attr
    network_template['attributes'] = network_res_attr
    # ///////////////////////////////////////////////////////////////////////////////////////

    # upload the network from one scenario only
    # WaMDaM allows changing the network for its scenarios but Hydra does not.
    # as a work around, we upload the network from the basline scenario in WaMDaM
    # this solution only works if all the scenario networks are the same in WaMDaM
    # otherwise, the user has to load the data into WaMDaM as many network

    scenario_sheet = getInstances.GetScenarios(selectedResourceTypeAcro,
                                               selectedMasterNetworkName)

    print 'looping through scenarios and their networks (nodes and links)'
    ScenarioType_flag = False
    for selectedScenarioName in selectedScenarioNames:

        for s in range(len(scenario_sheet)):
            # only add the selected scenario, one at a time.
            ScenarioParentName = str(scenario_sheet.values[s][8])

            # TO DO: handle a case where two scenarios with "baseline" exist. Maybe raise an exception

            if scenario_sheet.values[s][0] == selectedScenarioName and (
                    ScenarioParentName == 'Self'
                    or ScenarioParentName == 'self'):
                nodes_sheet = getInstances.GetNodesByScenario(
                    selectedResourceTypeAcro, selectedMasterNetworkName,
                    selectedScenarioName)
                # Iterate over the node instances and assign the parent Object Attributes to each node instance = ResourceAttribute (as in Hydra)
                print 'looping through the nodes'
                for n in range(len(nodes_sheet.values)):

                    # Look up the type_id in Hydra for each type
                    for templateType in new_template['templatetypes']:
                        if nodes_sheet.values[n][0] == templateType['name']:
                            type_id = templateType['id']
                            break

                    if type_id is None:
                        raise Exception(
                            "Unable to find a type in the template for %s" %
                            nodes_sheet[i][0])

                    flag = False
                    for node_item in list_node:
                        if node_item['name'] == nodes_sheet.values[n][1]:
                            flag = True
                    if flag: continue

                    description = str(nodes_sheet.values[n][9])
                    if description == "None":
                        description = ""

                    fullname = nodes_sheet.values[n][1]
                    nameShort = nodes_sheet.values[n][1]

                    node = {
                        'id': n * -1,
                        'name': nameShort,
                        'description': description,
                        'layout': {
                            'display_name': fullname
                        },
                        'x': str(nodes_sheet.values[n][7]),
                        'y': str(nodes_sheet.values[n][8]),
                        'types': [{
                            'id': type_id
                        }]
                    }
                    node_res_attr = []

                    for tn in range(len(attr_sheet)):
                        if nodes_sheet.values[n][0] == attr_sheet.values[tn][
                                0]:
                            name = attr_sheet.values[tn][1]
                            AttributeUnitCV = attr_sheet.values[tn][5]
                            # server_dim_name = UnitsTable_df.loc[UnitsTable_df[0] == AttributeUnitCV].iloc[0, 1]
                            # dimension = server_dim_name
                            attribute_server_id = attr_ID_by_Unit_attrName_list[
                                (AttributeUnitCV, name)]
                            #
                            # # res_id = (len(list_res_attr) + 1) * -1
                            # if (dimension, name) in Dataset_attr_ID_by_Dim_and_Name_list.keys():
                            #     # look up the attribute id on the server based on the both name,dimension together
                            #     attr_id = Dataset_attr_ID_by_Dim_and_Name_list[(dimension, name)]
                            # else:
                            #     attr_id = hydra._call('add_attribute', {'name': name, 'dimen': dimension})['id']
                            res_attr = {
                                'ref_key': 'NODE',
                                'attr_id': attribute_server_id,
                                'id': res_id,
                                'attr_is_var': 'Y'
                            }

                            res_id -= 1
                            resource_attr_lookup[('NODE', res_id)] = res_attr
                            node_res_attr.append(res_attr)
                            dict_res_attr[(nodes_sheet.values[n][1],
                                           name)] = res_attr

                    node['attributes'] = node_res_attr
                    list_node.append(node)
                    node_lookup[node['name']] = node
                network_template['nodes'] = list_node

                link_lookup = {}

                links_sheet = getInstances.GetLinksByScenario(
                    selectedResourceTypeAcro, selectedMasterNetworkName,
                    selectedScenarioName)

                list_link = []
                type_id = None
                lst_name = []

                print 'looping through the links'

                for l in range(len(links_sheet)):

                    for templateType in new_template['templatetypes']:
                        if links_sheet.values[l][0] == templateType['name']:
                            type_id = templateType['id']
                            break

                    if type_id is None:
                        print "there is a problem here at line 548"
                        # raise Exception("Unable to find a type in the template for %s" % links_sheet.values[i][0])
                    description = str(links_sheet.values[l][9])

                    if description == "None":
                        description = ""

                    name = links_sheet.values[l][1]
                    fullname = links_sheet.values[l][1]

                    if name in lst_name:
                        # print name
                        continue
                    else:
                        lst_name.append(name)

                    link = {
                        'id': l * -1,
                        'name': name,
                        'description': description,
                        'layout': {
                            'display_name': fullname
                        },
                        'types': [{
                            'id': type_id
                        }]
                    }
                    node_a = node_lookup.get(links_sheet.values[l][6])
                    if node_a is None:
                        print "there is a problem here at line 572"
                        # raise Exception("Node %s could not be found" % (links_sheet.values[i][6]))
                    link['node_1_id'] = node_a['id']

                    node_b = node_lookup.get(links_sheet.values[l][7])

                    if node_b is None:
                        print "there is a problem here at line 577"
                        # raise Exception("Node %s could not be found" % (links_sheet.values[i][7]))
                    link['node_2_id'] = node_b['id']

                    # ///// links resource attribute///////

                    link_res_attr = []
                    try:

                        for tl in range(len(attr_sheet)):  # issue in l=11
                            if links_sheet.values[l][0] == attr_sheet.values[
                                    tl][0]:
                                name = attr_sheet.values[tl][1]
                                AttributeUnitCV = attr_sheet.values[tl][5]

                                attribute_server_id = attr_ID_by_Unit_attrName_list[
                                    (AttributeUnitCV, name)]

                                res_attr = {
                                    'ref_key': 'LINK',
                                    'attr_id': attribute_server_id,
                                    'id': res_id,
                                    'attr_is_var': 'Y'
                                }
                                res_id -= 1
                                # resource_attr_lookup[('NODE', res_id)] = res_attr
                                link_res_attr.append(res_attr)
                                dict_res_attr[(links_sheet.values[l][1],
                                               name)] = res_attr
                        link['attributes'] = link_res_attr

                        list_link.append(link)
                        link_lookup[link['name']] = link
                    except:
                        print ' there is an issue in creating links resource attribute part'

                network_template['links'] = list_link
                network_template['resourcegroups'] = []
                ScenarioType_flag = True
                break
    if not ScenarioType_flag:
        print "there is a problem here at line 613"
        # raise Exception("""None of the selected Scenarios types has a "Self" ScenarioParentName
        #                  \n
        #                   Make sure that one of the selected scenarios has is a parent scenario
        #                  """)
        # else:
        #     raise Exception("""None of the selected Scenarios types has a "Baseline" Scenario type \n
        #      Make sure that one of the selected scenarios has is a baseline scenario
        #     """)

    print 'go through the scenarios data'
    from Upload_ScenarioData import PrepareScenarioData

    list_scenario = PrepareScenarioData(selectedResourceTypeAcro,
                                        selectedMasterNetworkName,
                                        selectedScenarioNames, scenario_sheet,
                                        dict_res_attr,
                                        Dataset_attr_Name_Dim_list,
                                        Dataset_attr_Name_Dim_unit, HydraUnits)

    # join all the scenarios into one indexed list [0][xxxxxxxxxxxx], [1][yyyyyyyyyyy], etc based on the # of scenarios jjj
    #
    network_template['scenarios'] = list_scenario

    network = hydra._call('add_network', network_template)

    print network

    print 'Successfully uploaded a WaMDaM data to OpenAgua'

    # active_template_id

    # append the child scenarios to their parent

    # first call just uploaded network and scenarios and get their IDs, names, and description

    # commeted out all this block below
    GetNetworks_metadata = hydra.call('get_networks', {
        'project_id': projectID,
        'include_data': 'N'
    })
    GetNetworks_metadata_df = json_normalize(GetNetworks_metadata)

    for network_row in GetNetworks_metadata_df.iterrows():
        if network_row[1]['name'] == selectedMasterNetworkName:
            network_id = network_row[1]['id']
            break

    # Get all the scenarios inside the uploaded network
    Get_scenarios_metadata = hydra.call('get_scenarios',
                                        {'network_id': network_id})

    Get_scenarios_metadata_df = json_normalize(Get_scenarios_metadata)

    # Use the scenario name to look up its ID
    print 'ready to relate scenarios'

    if len(
            scenario_sheet
    ) > 1:  # if only once scenario is selected, then dont worry about the parent/child stuff

        for i in range(len(scenario_sheet)):
            for row in Get_scenarios_metadata_df.iterrows():
                ScenarioName_sheet = scenario_sheet.values[i][0]
                ScenarioParentName = scenario_sheet.values[i][8]
                if ScenarioParentName == 'self' or ScenarioParentName == 'Self':
                    if ScenarioName_sheet == row[1][
                            'name']:  # matching the spreadsheet with the online uploaded scenario
                        ScenarioParentID = row[1]['id']

            for row in Get_scenarios_metadata_df.iterrows():
                ScenarioName_sheet = scenario_sheet.values[i][0]
                ScenarioParentName = scenario_sheet.values[i][8]
                if not ScenarioParentName == 'self' and not ScenarioParentName == 'Self':
                    if ScenarioName_sheet == row[1][
                            'name']:  # matching the spreadsheet with the online uploaded scenario
                        ChildScenarioID = row[1]['id']
                        ScenarioName_sheet = row[1]['name']
                        # Scenario_description = row[1]['description']

                        # ScenarioType = scenario_sheet.values[i][9].lower()

                        # if ScenarioType == '':
                        #     ScenarioType = 'scenario'

                        scenario = hydra._call('get_scenario', ChildScenarioID)

                        scenario['parent_id'] = ScenarioParentID
                        # scenario['layout': {'class': ScenarioType }] = ScenarioType

                        result = hydra._call('update_scenario', scenario)

                        # print result

                        print 'updated scenario ' + ScenarioName_sheet

    print 'done'
    def btn_find_network_nodesLinksOnButtonClick(self, event):
        # TODO: Implement btn_find_network_nodesLinksOnButtonClick
        print 'this is starting'
        selectedDataset = self.comboBox_selectModel.Value
        selectedMasterNetworkName = self.comboBox_selectNetwork.Value
        selectedScenarioName = self.comboBox_selectScenario.Value

        message = ''
        if (selectedDataset == None or selectedDataset == ''):
            message = 'Select the model name in WamDam.'
        elif selectedMasterNetworkName == None or selectedMasterNetworkName == '':
            message = 'Select the MasterNetworkName.'
        elif selectedScenarioName == None or selectedScenarioName == '':
            message = 'Select the ScenarioName.'
        elif not ['xls', 'xlsx', 'xlsm', 'xls'].__contains__(
                self.path.split('.')[-1]):
            message = 'And please select a valid excel file.'

        if message != '':
            messageDlg = msg_somethigWrong(None, msg=message)
            messageDlg.Show()
            raise Exception(message)
        ''' get nework, scenario, nodes, links'''
        result = GetResourceStructure()
        names, network_data_result = result.GetMasterNetworks(selectedDataset)
        names, scenarios_data_result = result.GotScenario(
            selectedDataset, selectedMasterNetworkName)
        names, nodes_data_result = result.GotNodes(selectedDataset,
                                                   selectedMasterNetworkName,
                                                   selectedScenarioName)
        links_data_result = result.GotLinks(selectedDataset,
                                            selectedMasterNetworkName,
                                            selectedScenarioName)
        print '************8**'
        try:
            if self.path.split('.')[-1] == 'xls':
                wb = open_workbook(self.path)
                # workbook = copy(wb)
                workbook = wb
                try:
                    sheet = workbook.get_sheet(6)
                    sheetnodes = workbook.get_sheet(7)
                    sheetlinks = workbook.get_sheet(8)
                except:
                    message = msg_somethigWrong(
                        None, msg="Please select a valid Excel File")
                    message.Show()
                    return

                for row_id, row in enumerate(network_data_result):
                    for col_id, cell in enumerate(row):
                        sheet.write(row_id + 9, col_id + 0, cell)

                for row_id, row in enumerate(scenarios_data_result):
                    for col_id, cell in enumerate(row):
                        sheet.write(row_id + 17, col_id + 0, cell)

                for row_id, row in enumerate(nodes_data_result):
                    for col_id, cell in enumerate(row):
                        sheetnodes.write(row_id + 10, col_id + 0, cell)

                for row_id, row in enumerate(links_data_result):
                    for col_id, cell in enumerate(row):
                        sheetlinks.write(row_id + 10, col_id + 0, cell)

                workbook.save(self.path)
            else:
                book2 = load_workbook(self.path)
                try:
                    sheet = book2.get_sheet_by_name("3.1_Networks&Scenarios")
                    nodes_sheet = book2.get_sheet_by_name("3.2_Nodes")
                    links_sheet = book2.get_sheet_by_name("3.3_Links")
                except:
                    message = msg_somethigWrong(
                        None, msg="Please select a valid Excel File")
                    message.Show()
                    return
                    # raise Exception('Output Sheet {} not found in Excel File \n\n Please select valid Excel File'.format("3.1_Networks&Scenarios"))

                for row_id, row in enumerate(network_data_result):
                    for col_id, cell in enumerate(row):
                        sheet.cell(row=row_id + 10,
                                   column=col_id + 1,
                                   value=cell)

                for row_id, row in enumerate(scenarios_data_result):
                    for col_id, cell in enumerate(row):
                        sheet.cell(row=row_id + 20,
                                   column=col_id + 1,
                                   value=cell)

                for row_id, row in enumerate(nodes_data_result):
                    for col_id, cell in enumerate(row):
                        nodes_sheet.cell(row=row_id + 11,
                                         column=col_id + 1,
                                         value=cell)

                for row_id, row in enumerate(links_data_result):
                    for col_id, cell in enumerate(row):
                        links_sheet.cell(row=row_id + 11,
                                         column=col_id + 1,
                                         value=cell)
                book2.save(self.path)
            from viewer.Messages_forms.msg_successLoadDatabase import msg_successLoadDatabase
            instance = msg_successLoadDatabase(None)
            instance.m_staticText1.SetLabel("Success: exported excel file")
            instance.ShowModal()
        except Exception as e:
            print e
            messageDlg = msg_somethigWrong(None, msg=e.message)
            messageDlg.Show()
            raise Exception(e.message)

        print 'Great so far....'
class dlg_query_network(viewer.WaMDaMWizard.dlg_query_network):
    def __init__(self, parent):
        viewer.WaMDaMWizard.dlg_query_network.__init__(self, parent)

        self.path = ''
        try:
            if not DB_Setup().get_session():
                msg = "\n\nWarning: Please connect to sqlite first."
                raise Exception(msg)

            self.dataStructure = GetResourceStructure()
            self.datasets = self.dataStructure.GetResourceType()
            list_acromy = list()
            for row in self.datasets.values:
                list_acromy.append(row[0])
            if list_acromy.__len__() > 0:
                self.comboBox_selectModel.SetItems(list_acromy)
        except Exception as e:
            message = msg_somethigWrong(None, msg=e.message)
            message.ShowModal()
            self.Destroy()

    # Handlers for dlg_query_network events.
    def comboBox_selectModelOnCombobox(self, event):
        # TODO: Implement comboBox_selectModelOnCombobox
        selectedDataset = self.comboBox_selectModel.Value
        result = GetInstancesBySenario()
        gotMasterNetwork = result.GetMasterNetworks(selectedDataset)
        list_Networks = list()
        for index, row in gotMasterNetwork.iterrows():
            list_Networks.append(row["MasterNetworkName"])
        if list_Networks.__len__() > 0:
            self.comboBox_selectNetwork.SetItems(list_Networks)

    def comboBox_selectNetworkOnCombobox(self, event):
        selectedMasterNetworkName = self.comboBox_selectNetwork.Value
        selectedDataset = self.comboBox_selectModel.Value
        result = GetInstancesBySenario()
        gotScenarioNames = result.GetScenarios(selectedDataset,
                                               selectedMasterNetworkName)

        list_Scenarios = list()
        for index, row in gotScenarioNames.iterrows():
            list_Scenarios.append(row["ScenarioName"])

        if gotScenarioNames.__len__() <= 0:
            return
        self.comboBox_selectScenario.SetItems(list_Scenarios)

    def comboBox_selectScenarioOnCombobox(self, event):
        # TODO: Implement comboBox_selectScenarioOnCombobox
        pass

    def FilePicker_QueryNetworkOnFileChanged(self, event):
        # TODO: Implement FilePicker_QueryNetworkOnFileChanged
        valid_extension = ['xls', 'xlsx']
        self.path = self.FilePicker_QueryNetwork.GetPath()
        if not (self.path.split('.')[-1] in valid_extension):
            message = msg_somethigWrong(None,
                                        msg="Please select a valid Excel File")
            message.Show()
            return
        print 'This is working just fine...'
        print self.path

    def btn_find_network_nodesLinksOnButtonClick(self, event):
        # TODO: Implement btn_find_network_nodesLinksOnButtonClick
        print 'this is starting'
        selectedDataset = self.comboBox_selectModel.Value
        selectedMasterNetworkName = self.comboBox_selectNetwork.Value
        selectedScenarioName = self.comboBox_selectScenario.Value

        message = ''
        if (selectedDataset == None or selectedDataset == ''):
            message = 'Select the model name in WamDam.'
        elif selectedMasterNetworkName == None or selectedMasterNetworkName == '':
            message = 'Select the MasterNetworkName.'
        elif selectedScenarioName == None or selectedScenarioName == '':
            message = 'Select the ScenarioName.'
        elif not ['xls', 'xlsx', 'xlsm', 'xls'].__contains__(
                self.path.split('.')[-1]):
            message = 'And please select a valid excel file.'

        if message != '':
            messageDlg = msg_somethigWrong(None, msg=message)
            messageDlg.Show()
            raise Exception(message)
        ''' get nework, scenario, nodes, links'''
        result = GetResourceStructure()
        names, network_data_result = result.GetMasterNetworks(selectedDataset)
        names, scenarios_data_result = result.GotScenario(
            selectedDataset, selectedMasterNetworkName)
        names, nodes_data_result = result.GotNodes(selectedDataset,
                                                   selectedMasterNetworkName,
                                                   selectedScenarioName)
        links_data_result = result.GotLinks(selectedDataset,
                                            selectedMasterNetworkName,
                                            selectedScenarioName)
        print '************8**'
        try:
            if self.path.split('.')[-1] == 'xls':
                wb = open_workbook(self.path)
                # workbook = copy(wb)
                workbook = wb
                try:
                    sheet = workbook.get_sheet(6)
                    sheetnodes = workbook.get_sheet(7)
                    sheetlinks = workbook.get_sheet(8)
                except:
                    message = msg_somethigWrong(
                        None, msg="Please select a valid Excel File")
                    message.Show()
                    return

                for row_id, row in enumerate(network_data_result):
                    for col_id, cell in enumerate(row):
                        sheet.write(row_id + 9, col_id + 0, cell)

                for row_id, row in enumerate(scenarios_data_result):
                    for col_id, cell in enumerate(row):
                        sheet.write(row_id + 17, col_id + 0, cell)

                for row_id, row in enumerate(nodes_data_result):
                    for col_id, cell in enumerate(row):
                        sheetnodes.write(row_id + 10, col_id + 0, cell)

                for row_id, row in enumerate(links_data_result):
                    for col_id, cell in enumerate(row):
                        sheetlinks.write(row_id + 10, col_id + 0, cell)

                workbook.save(self.path)
            else:
                book2 = load_workbook(self.path)
                try:
                    sheet = book2.get_sheet_by_name("3.1_Networks&Scenarios")
                    nodes_sheet = book2.get_sheet_by_name("3.2_Nodes")
                    links_sheet = book2.get_sheet_by_name("3.3_Links")
                except:
                    message = msg_somethigWrong(
                        None, msg="Please select a valid Excel File")
                    message.Show()
                    return
                    # raise Exception('Output Sheet {} not found in Excel File \n\n Please select valid Excel File'.format("3.1_Networks&Scenarios"))

                for row_id, row in enumerate(network_data_result):
                    for col_id, cell in enumerate(row):
                        sheet.cell(row=row_id + 10,
                                   column=col_id + 1,
                                   value=cell)

                for row_id, row in enumerate(scenarios_data_result):
                    for col_id, cell in enumerate(row):
                        sheet.cell(row=row_id + 20,
                                   column=col_id + 1,
                                   value=cell)

                for row_id, row in enumerate(nodes_data_result):
                    for col_id, cell in enumerate(row):
                        nodes_sheet.cell(row=row_id + 11,
                                         column=col_id + 1,
                                         value=cell)

                for row_id, row in enumerate(links_data_result):
                    for col_id, cell in enumerate(row):
                        links_sheet.cell(row=row_id + 11,
                                         column=col_id + 1,
                                         value=cell)
                book2.save(self.path)
            from viewer.Messages_forms.msg_successLoadDatabase import msg_successLoadDatabase
            instance = msg_successLoadDatabase(None)
            instance.m_staticText1.SetLabel("Success: exported excel file")
            instance.ShowModal()
        except Exception as e:
            print e
            messageDlg = msg_somethigWrong(None, msg=e.message)
            messageDlg.Show()
            raise Exception(e.message)

        print 'Great so far....'

    def btn_cancelOnButtonClick(self, event):
        # TODO: Implement btn_cancelOnButtonClick
        self.Close()