From 82ed88086b6aedc2ccd75b4138dc8145bf597431 Mon Sep 17 00:00:00 2001
From: Oceguera Conchas <no67wuwu@idiv.de>
Date: Wed, 23 Nov 2022 11:12:49 +0100
Subject: [PATCH] the function showInfo has been fixed

---
 EBVCubeVisualizer_funtionality.py | 351 ++++++++++++++++++++----------
 1 file changed, 237 insertions(+), 114 deletions(-)

diff --git a/EBVCubeVisualizer_funtionality.py b/EBVCubeVisualizer_funtionality.py
index 5d72936..a772da7 100644
--- a/EBVCubeVisualizer_funtionality.py
+++ b/EBVCubeVisualizer_funtionality.py
@@ -87,7 +87,6 @@ class maskAndFuntionality (BASE, WIDGET):
         self.btn_load.clicked.connect(self.loadNetCDF)
         self.btn_load.clicked.connect(self.setMapData)
         self.btn_remove_sel.clicked.connect(self.removeSelection)
-        #here we set the clicked signal for the tree widget
         self.tree_data.itemClicked.connect(self.showInfo)
         self.btn_plot.clicked.connect(self.displayData)
         
@@ -264,177 +263,301 @@ class maskAndFuntionality (BASE, WIDGET):
         ncFile.close()
         
     def showInfo(self):
-        """this function shows first the name of the file and the global attributes and then if a varible is clicked delete the info and add the attributes of the selected variable"""
+        """show the attributes of the scenarios, metrics and variables"""
         self.text_info.clear()
         path = self.text_set.text() #we get the path from the text space
         ncFile = nc.Dataset(path, 'r', format='NETCDF4') #we open the netCDF file
         ncFileName = os.path.basename(path) 
         ncFileTitle = ncFile.title 
         globalAttributesName = list(ncFile.ncattrs())
-
+        print(self.tree_data.currentItem().text(0))
+        
         # here remove the info and add the attributes of the selected variable
         globalAttributesName.remove('title')
         globalAttributesName.remove('history')
         globalAttributesName.remove('Conventions')
         globalAttributesName.remove('date_issued')
-
-
-        #groups could be scenarios or metrics
-        groups = list(ncFile.groups.keys())
         
-        #if the first group is scenarios we get the groups of the scenarios
-        groupsOfGroups = []
-        for i in range(len(groups)):
-            groupsOfGroups.append(list(ncFile.groups[groups[i]].groups.keys()))
 
-        #if the groups are metrics we get the variables of the metrics
-        variablesOfGroups = [] 
-        for i in range(len(groups)):
-            variablesOfGroups.append(list(ncFile.groups[groups[i]].variables.keys()))
+        #groups could be scenarios or metrics
+        scenarioOrMetric = list(ncFile.groups.keys())
+        
+        #first group is scenarios we get the metrics
+        metrics = []
+        for i in range(len(scenarioOrMetric)):
+            metrics.append(list(ncFile.groups[scenarioOrMetric[i]].groups.keys()))
+
+        #first group is metric we get the variables
+        metricsVariables1 = [] 
+        for i in range(len(scenarioOrMetric)):
+            variables = list(ncFile.groups[scenarioOrMetric[i]].variables.keys())
+            metricsVariables1.append(variables)
+        
+        #first group is scenario and second metric than we get the variables of metric
+        metricsVariables2 = []
+        for i in range(len(scenarioOrMetric)):
+            metricsVariables2.append([])
+            for j in range(len(metrics[i])):
+                variables = list(ncFile.groups[scenarioOrMetric[i]].groups[metrics[i][j]].variables.keys())
+                metricsVariables2[i].append(variables)
+        
+        #we print everthing
+        # print('ScenarioOrMetric')
+        # print(scenarioOrMetric)
+        # print(metricsVariables1)
+        # print('metrics')
+        # print(metrics)
+        # print(metricsVariables2)
+        # print('len')
+        # print(len(metricsVariables2))
+        # print(len(metricsVariables2[0]))
+        # #print('metricsVariables1[0]')
+        # #print(metricsVariables1[0][0])
+        # print('current item')
+        # print(self.tree_data.currentItem().text(0))
         
-        #we get the variables of the groups of the scenarios
-        variablesOfGroupsOfGroups = []
-        for i in range(len(groups)):
-            variablesOfGroupsOfGroups.append([])
-            for j in range(len(groupsOfGroups[i])):
-                variablesOfGroupsOfGroups[i].append(list(ncFile.groups[groups[i]].groups[groupsOfGroups[i][j]].variables.keys()))
         
         
         #set global attributes from the netCDF file
         if  self.tree_data.currentItem().text(0) == ncFileName:
+            #print('global atts')
             self.text_info.clear()
             self.text_info.append("<b><font size=4>" + "File name: " + ncFileName + "</font></b>")
-            self.text_info.append("<b><font size=3>" + "Title: " + ncFileTitle + "</font></b>")
+            self.text_info.append("<b><font size=4>" + "Title: " + ncFileTitle + "</font></b>")
+            self.text_info.append("____________________________________________________________________________")
             self.text_info.append(" ")
-            self.text_info.append("<b><font size=3>" +"Global attributes: " + "</font></b>")
+            self.text_info.append("<b><font size=4>" +"Global attributes: " + "</font></b>")
             self.text_info.append(" ")
             for i in range(len(globalAttributesName)):
-                sttributes = ncFile.getncattr(globalAttributesName[i])
-                self.text_info.append("<b><font size=3>" + globalAttributesName[i] + ": " + "</font></b>" + str(sttributes))
+                attributes = ncFile.getncattr(globalAttributesName[i])
+                #print(attributes)
+                self.text_info.append("<b><font size=3>" + "-" + globalAttributesName[i] + ": " + "</font></b>" + str(attributes))
         
         #attributes of the groups. The groups are scenarios or metrics
-        elif self.tree_data.currentItem().text(0) in groups:
+        elif self.tree_data.currentItem().text(0) in scenarioOrMetric:
+            #print('group atts: metric or scenario')
             self.text_info.append("<b><font size=4>" + "File name: " + ncFileName + "</font></b>")
-            self.text_info.append("<b><font size=3>" + "Title: " + ncFileTitle + "</font></b>")
+            self.text_info.append("<b><font size=4>" + "Title: " + ncFileTitle + "</font></b>")
+            self.text_info.append("________________________________________________________________________________")
             self.text_info.append(" ")
 
+            #Scenario or Metric
             #name and standard name of the groups (scenarios or metrics)
             groupName = self.tree_data.currentItem().text(0)
-            standardName = ncFile.groups[groups[0]].getncattr('standard_name')
+            #print ('scenarioName' + groupName)
+            groupStandardName = ncFile.groups[groupName].getncattr('standard_name')
+            #print ('scenarioStandardName: ' + groupStandardName)
 
-            self.text_info.append("<b><font size=3>" + "Group: " + "</font></b>" + groupName )
-            self.text_info.append("<b><font size=3>" + "Standard name: " + "</font></b>" + standardName)
-            self.text_info.append(" ")
-            self.text_info.append("<b><font size=3>" + "Attributes" + "</font></b>")
+            #attributes of the groups (scenarios or metrics)
+            self.text_info.append("<b><font size=4>" + "Attributes of the Scenario: " + "</font></b>" + groupStandardName)
             self.text_info.append(" ")
-            
-            #name of the attributes of the groups
-            attributesNameScenario = ncFile.groups[self.tree_data.currentItem().text(0)].ncattrs()
-            attributesNameScenario.remove('standard_name')
+
+            #name attributes of the groups (scenarios or metrics)
+            groupAttributesName = list(ncFile.groups[groupName].ncattrs())
+            groupAttributesName.remove('standard_name')
+            #print ('groupAttributesName: ' + str(groupAttributesName))
 
             #value of the attributes of the groups
-            for i in range(len(attributesNameScenario)):
-                sttributesScenario = ncFile.groups[self.tree_data.currentItem().text(0)].getncattr(attributesNameScenario[i])
-                self.text_info.append("<b><font size=3>" + attributesNameScenario[i] + ": " + "</font></b>" + str(sttributesScenario))
-                
+            for i in range(len(groupAttributesName)):
+                groupAttributes = ncFile.groups[groupName].getncattr(groupAttributesName[i])
+                #print ('groupAttributes: ' + str(groupAttributes))
+                self.text_info.append("<b><font size=3>" + "-" + groupAttributesName[i] + ": " + "</font></b>" + str(groupAttributes))
+            
         
-        #groups of the scenarios
-        elif self.tree_data.currentItem().text(0) in groupsOfGroups[0]:
+        #groups of the scenarios are always metric
+        elif self.tree_data.currentItem().text(0) in metrics[0]:
+            #print('test metric')
             self.text_info.clear()
             self.text_info.append("<b><font size=4>" + "File name: " + ncFileName + "</font></b>")
-            self.text_info.append("<b><font size=3>" + "Title: " + ncFileTitle + "</font></b>")
+            self.text_info.append("<b><font size=4>" + "Title: " + ncFileTitle + "</font></b>")
+            self.text_info.append("________________________________________________________________________________")
             self.text_info.append(" ")
 
-            standardName = ncFile.groups[groups[0]].groups[groupsOfGroups[0][0]].getncattr('standard_name')
-            groupName = self.tree_data.currentItem().text(0)
+            #Scenarios
+            #get the standard name of the scenario
+            scenarioName = self.tree_data.currentItem().parent().text(0)
+            #print ("standard name: " + scenarioName)            
+            scenarioStandardName = ncFile.groups[scenarioName].getncattr('standard_name')
+            #print ("scenario standard name: " + scenarioStandardName)
             
-            self.text_info.append("<b><font size=3>" + "Group: " + "</font></b>" + groupName )
-            self.text_info.append("<b><font size=3>" + "Standard name: " + "</font></b>" + standardName)
+            #attributes of the scenario
+            self.text_info.append("<b><font size=4>" + "Attributes of the Scenario: " + "</font></b>" + scenarioStandardName)
             self.text_info.append(" ")
-            self.text_info.append("<b><font size=3>" + "Attributes" + "</font></b>")
-            self.text_info.append(" ")
-            
-            # here we get the name of the attributes from the group
-
-            attributesNameMetrics = ncFile.groups[groups[0]].groups[groupsOfGroups[0][0]].ncattrs()
-            attributesNameMetrics.remove('standard_name')
-
-            for i in range(len(attributesNameMetrics)):
-                sttributesMetrics = ncFile.groups[groups[0]].groups[groupsOfGroups[0][0]].getncattr(attributesNameMetrics[i])
-                self.text_info.append("<b><font size=3>" + attributesNameMetrics[i] + ": " + "</font></b>" + str(sttributesMetrics))
             
-        #we get te variables of the groups of the groups but not the variables of the groups
-        elif self.tree_data.currentItem().text(0) in variablesOfGroupsOfGroups[0][0]:
-            self.text_info.clear()
-            self.text_info.append("<b><font size=4>" + "File name: " + ncFileName + "</font></b>")
-            self.text_info.append("<b><font size=3>" + "Title: " + ncFileTitle + "</font></b>")
-            self.text_info.append(" ")
-
-            #standard name and nams of the variables of the groups of the groups
-            standardName = ncFile.groups[groups[0]].groups[groupsOfGroups[0][0]].getncattr('standard_name')
-            groupName = self.tree_data.currentItem().parent().text(0)
-
-            self.text_info.append("<b><font size=3>" + "Group: " + "</font></b>" +  groupName) 
-            self.text_info.append("<b><font size=3>" + "Standard name: " + "</font></b>" + standardName)
-            self.text_info.append(" ")
-
-            #name of the variable
-            variableName = self.tree_data.currentItem().text(0)
+            #name of the attributes of the scenario
+            attributesNameScenario = ncFile.groups[scenarioName].ncattrs()
+            attributesNameScenario.remove('standard_name')
+            #print ("attributes name: " + str(attributesNameScenario))
 
-            self.text_info.append("<b><font size=3>" + "Variable: " + "</font></b>" + variableName)
+            #value of the attributes of the scenario
+            for i in range(len( attributesNameScenario)):
+                attributes = ncFile.groups[scenarioName].getncattr(attributesNameScenario[i])
+                self.text_info.append("<b><font size=3>" + "-" + attributesNameScenario[i] + ": " + "</font></b>" + str(attributes))
+            self.text_info.append("________________________________________________________________________________")
             self.text_info.append(" ")
-            self.text_info.append("<b><font size=3>" + "Attributes" + "</font></b>")
+            
+            #Metrics
+            #get name and standard name of the metric
+            metricName = self.tree_data.currentItem().text(0)
+            print ("metric name: " + metricName)
+            metricStandardName = ncFile.groups[scenarioName].groups[metricName].getncattr('standard_name')
+            print ("metric standard name: " + metricStandardName)
+    
+            self.text_info.append("<b><font size=4>" + "Attributes of the Metric: " + "</font></b>" + metricStandardName)
             self.text_info.append(" ")
-
-            # here we get the the attributes from the variable
             
-            attributesNameCubes = ncFile.groups[groups[0]].groups[groupsOfGroups[0][0]].variables[variableName].ncattrs()
-            attributesNameCubes.remove('_FillValue')
-            print(attributesNameCubes)
-
-            for i in range(len(attributesNameCubes)):
-                sttributesCubes = ncFile.groups[groups[0]].groups[groupsOfGroups[0][0]].variables[variableName].getncattr(attributesNameCubes[i])
-                self.text_info.append("<b><font size=3>" + attributesNameCubes[i] + ": " + "</font></b>" + str(sttributesCubes))
+            #get the name of the attributes of the metric
+            attributesNameMetric = ncFile.groups[scenarioName].groups[metricName].ncattrs()
+            print ("attributes name metric: " + str(attributesNameMetric))
+            #attributesNameMetric.remove('standard_name')
+            
+            for i in range(len(attributesNameMetric)):
+                attributes = ncFile.groups[scenarioName].groups[metricName].getncattr(attributesNameMetric[i])
+                print ("attributes: " + str(attributes))
+                self.text_info.append("<b><font size=3>" + "-" + attributesNameMetric[i] + ": " + "</font></b>" + str(attributes))
+            
+        
+        #varibles of the metric, when there is scenarios
+        elif len(metricsVariables2[0])!= 0 :
+            if self.tree_data.currentItem().text(0) in metricsVariables2[0][0]:
+                #print('test')
+                self.text_info.clear()
+                self.text_info.append("<b><font size=4>" + "File name: " + ncFileName + "</font></b>")
+                self.text_info.append("<b><font size=4>" + "Title: " + ncFileTitle + "</font></b>")
+                self.text_info.append("________________________________________________________________________________")
+                self.text_info.append(" ")
+                
+                #Scenario
+                #get the name of the scenario and 
+                scenarioName = self.tree_data.currentItem().parent().parent().text(0)
+                #print('scenario Name: ' + str(scenarioName))
+                scenarioStandardName = ncFile.groups[scenarioName].getncattr('standard_name')
+                #print('scenario Standard Name: ' + str(scenarioStandardName))
+                
+                #attributes of the scenario
+                self.text_info.append("<b><font size=4>" + "Attributes of the Scenario: " + "</font></b>" + scenarioStandardName)
+                self.text_info.append(" ")
+
+                #get the name of the attributes of the scenario
+                attributesNameScenario = ncFile.groups[scenarioName].ncattrs()
+                attributesNameScenario.remove('standard_name')
+                #print(attributesNameScenario)
+                
+                #get the value of the attributes of the scenario
+                for i in range(len(attributesNameScenario)):
+                    attributesScenario = ncFile.groups[scenarioName].getncattr(attributesNameScenario[i])
+                    #print(attributesScenario)
+                    self.text_info.append("<b><font size=3>" + "-" + attributesNameScenario[i] + ": " + "</font></b>" + str(attributesScenario))
+                self.text_info.append("________________________________________________________________________________")
+                self.text_info.append(" ")
+                
+                #Metric
+                #get the name of the metric
+                metricName = self.tree_data.currentItem().parent().text(0)
+                #print('metric Name: ', metricName)
+                metricStandardName = ncFile.groups[scenarioName].groups[metricName].getncattr('standard_name')
+                #print('metric standard name: ', metricStandardName)
+                
+                #attributes of the metric
+                self.text_info.append("<b><font size=4>" + "Attribuites of the Metric: " + "</font></b>" + metricStandardName) 
+                self.text_info.append(" ")
+
+                #get the name of the attributes of the metric
+                attributesNameMetric = ncFile.groups[scenarioName].groups[metricName].ncattrs()
+                attributesNameMetric.remove('standard_name')
+                #print('Attributes name metric: ', attributesNameMetric)
+      
+                #get the value of the attributes of the metric
+                for i in range(len(attributesNameMetric)):
+                    attributesMetric = ncFile.groups[scenarioName].groups[metricName].getncattr(attributesNameMetric[i])
+                    #print ('Attributes metric: ', attributesMetric)
+                    self.text_info.append("<b><font size=3>" + "-" + attributesNameMetric[i] + ": " + "</font></b>" + str(attributesMetric))
+                self.text_info.append("___________________________________________________________________________")
+                self.text_info.append(" ")
+                
+                #EBV Cube
+                #attributes of the EBV Cube
+                self.text_info.append("<b><font size=4>" + "Attributes of the EBV Cube:" + "</font></b>")
+                self.text_info.append(" ")
+                
+                #get the name of the attributes of the EBV Cube
+                variableName = self.tree_data.currentItem().text(0)
+                #print(variableName)
+                attributesNameCube = ncFile.groups[scenarioName].groups[metricName].variables[variableName].ncattrs()
+                attributesNameCube.remove('_FillValue')
+                attributesNameCube.remove('grid_mapping')
+                attributesNameCube.remove('coordinate')
+                #print('attributes name EBV Cube: ', attributesNameCube)
+                
+                #get the value of the attributes of the EBV Cube
+                for i in range(len(attributesNameCube)):
+                    attributesCube = ncFile.groups[scenarioName].groups[metricName].variables[variableName].getncattr(attributesNameCube[i])
+                    #print('attributes cube: ', attributesCube)
+                    self.text_info.append("<b><font size=3>" + "-" + attributesNameCube[i] + ": " + "</font></b>" + str(attributesCube))
+    
+                #we get the _FillValue
+                fillValue = ncFile.groups[scenarioName].groups[metricName].variables[variableName].getncattr('_FillValue')
+                #print('fill value: ', fillValue)
+                self.text_info.append("<b><font size=3>" + "-" + "Nodata_value: " + "</font></b>" + str(fillValue))
+                
 
-            #we get the _FillValue
-            fillValue = ncFile.groups[groups[0]].groups[groupsOfGroups[0][0]].variables[variableName].getncattr('_FillValue')
-            self.text_info.append("<b><font size=3>" + "NoData_value: " + "</font></b>" + str(fillValue))
 
         #if the groups are just metrics we get the variables of the metrics
-        elif self.tree_data.currentItem().text(0) == groups[1]:
+        elif self.tree_data.currentItem().text(0) == metricsVariables1[0][0]:
+            #print('ebv cube when ther is just metric')
             self.text_info.clear()
             self.text_info.append("<b><font size=4>" + "File name: " + ncFileName + "</font></b>")
-            self.text_info.append("<b><font size=3>" + "Title: " + ncFileTitle + "</font></b>")
+            self.text_info.append("<b><font size=4>" + "Title: " + ncFileTitle + "</font></b>")
+            self.text_info.append("___________________________________________________________________________")
             self.text_info.append(" ")
 
-            #we get the name of the group and the standard name
-            groupName = self.tree_data.currentItem().parent().text(0)
-            standardName = ncFile.groups[groups[0]].getncattr('standard_name')
-
-            self.text_info.append("<b><font size=3>" + "Group: " + "</font></b>" + groupName )
-            self.text_info.append("<b><font size=3>" + "Standard name: " + "</font></b>" + standardName)
+            #Metric
+            #name of the metrics and the standard name
+            metricName = self.tree_data.currentItem().parent().text(0)
+            #print('metric name: ' + metricName)
+            standardMetricName = ncFile.groups[metricName].getncattr('standard_name')
+            #print('standard Metric Name: ' + standardMetricName)
+            
+            #attributes of the metric
+            self.text_info.append("<b><font size=4>" + "Attributes of Metric: " + "</font></b>" + standardMetricName )
             self.text_info.append(" ")
 
-            #we get the name of the variable
-            variableName = self.tree_data.currentItem().text(0)
-
-            self.text_info.append("<b><font size=3>" + "Variable: " + "</font></b>" + variableName)
-            self.text_info.append(" ")
-            self.text_info.append("<b><font size=3>" + "Attributes:" + "</font></b>")
+            #name of the attributes of the metric
+            attributesNameMetric = ncFile.groups[metricName].ncattrs()
+            #print('attributesNameCube: ', attributesNameMetric)
+            attributesNameMetric.remove('standard_name')
+            
+            for i in range(len(attributesNameMetric)):
+                attributesMetric = ncFile.groups[metricName].getncattr(attributesNameMetric[i])
+                self.text_info.append("<b><font size=3>" + "-" + attributesNameMetric[i] + ": " + "</font></b>" + str(attributesMetric))
+            self.text_info.append("___________________________________________________________________________")
             self.text_info.append(" ")
-        
-            # here we get the name of the attributes from the variable
-            attributesNameVariable = ncFile.groups[groups[0]].variables[self.tree_data.currentItem().text(0)].ncattrs()
-            attributesNameVariable.remove('_FillValue')
 
-            for i in range(len(attributesNameVariable)):
-                attributesVariable = ncFile.groups[groups[0]].variables[self.tree_data.currentItem().text(0)].getncattr(attributesNameVariable[i])
-                self.text_info.append("<b><font size=3>" + attributesNameVariable[i] + ": " + "</font></b>" + str(attributesVariable))
+            #EBV Cube
+            #Attributes of the EBV Cube
+            self.text_info.append("<b><font size=4>" + "Attributes of the EBV Cube: " + "</font></b>")
+            self.text_info.append(" ")
 
-            #set NoData value
-            fillValue = ncFile.groups[groups[0]].variables[self.tree_data.currentItem().text(0)].getncattr('_FillValue')
-            self.text_info.append("<b><font size=3>" + "NoData_value: " + "</font></b>" + str(fillValue))
-        
+            #name of the EBV Cube
+            cubeName = self.tree_data.currentItem().text(0)
+            #print('variable name: ' + cubeName)
+            attributesNameCube = ncFile.groups[metricName].variables[cubeName].ncattrs()
+            attributesNameCube.remove('_FillValue')
+            attributesNameCube.remove('grid_mapping')
+            attributesNameCube.remove('coordinate')
+            #print('attributesNameCube: ', attributesNameCube)
+
+            #get the value of the attributes of the EBV Cube
+            for i in range(len(attributesNameCube)):
+                attributesCube = ncFile.groups[metricName].variables[cubeName].getncattr(attributesNameCube[i])
+                #print ('attributesCube: ' + attributesCube)
+                self.text_info.append("<b><font size=3>" + "-" + attributesNameCube[i] + ": " + "</font></b>" + str(attributesCube))
+
+            #set NoData_value
+            fillValue = ncFile.groups[metricName].variables[cubeName].getncattr('_FillValue')
+            #print ('fillValue: ', fillValue)
+            self.text_info.append("<b><font size=3>" + "-" + "nodata_value: " + "</font></b>" + str(fillValue))
+            #print ('Eres la mera verga!! sigue asi. Todo sladrá bien en el futuro')
         
         # we close the netCDF file
         ncFile.close()
-- 
GitLab