Ejemplo n.º 1
0
 def addSpecialVar(self):
     eq = str(self.specialvar_field.text())
     eqVars = getVars(eq)
     if not (eq.count("(") == eq.count(")")):
         print "invalid equation"
         return
     contains = np.product([var in self.data.variables for var in eqVars])
     if not contains:
         print "invalid variable"
         return
     ndims = [len(np.squeeze(self.data.variables[key]).shape) for key in eqVars]
     samedims = ndims[1:] == ndims[:-1]
     if not samedims:
         print "evaluating variables of different dimension"
         return
     if not self.data:
         print "no data"
     else:
         varnames1d = self.textFromListWidget(self.var1d_list, allNames=True)
         varnames2d = self.textFromListWidget(self.var2d_list, allNames=True)
         varnames3d = self.textFromListWidget(self.var3d_list, allNames=True)
         dim = ndims[0]
         if dim == 1 and not eq in varnames1d:
             self.var1d_list.addItem(eq)
         elif dim == 2 and not eq in varnames2d:
             self.var2d_list.addItem(eq)
         elif dim == 3 and not eq in varnames3d:
             self.var3d_list.addItem(eq)
         else:
             print "unknown dimension:", eq, dim
Ejemplo n.º 2
0
    def __init__(self, data, equation):
        self.data = data
        self.time = data.variables["time"]
        self.timeunit = data.variables["time"].units
        vardict = {key: var[:] for key, var in data.variables.iteritems()}
        self.var = np.squeeze(evalEq(equation, vardict))
        self.varcpy = copy(self.var)
        self.varunit = "combined"
        self.varlabel = equation
        self.longname = equation
        self.ndim = len(self.var.shape)
        self.ndimtot = len(self.var.shape)
        if self.ndimtot > 1:
            self.nlvl = self.var.shape[1]
        else:
            self.nlvl = 0
        try:
            self.height = data.variables["lev"][:]
            self.heightunit = "pressure [Pa]"
        except:
            self.height = np.linspace(0, self.nlvl - 1, self.nlvl)
            self.heightunit = "model level"
        variables = getVars(equation)
        if len(variables) == 1:
            varname = variables[0]
            self.varunit = ""
            self.varlabel = varname
            self.longname = varname
            try:
                self.varunit = data.variables[varname].units
            except:
                print "variable is dimensionless or unit not given"
            try:
                self.longname = data.variables[varname].long_name
            except:
                print "no variable information."

        self.hasTimeVal = False
        self.hasHeightVal = False
        self.defaultPlotType = ""
        if self.ndim == 1 and len(self.time) == len(self.var):
            self.hasTimeVal = True
            self.defaultPlotType = "TIME"
        elif self.ndim == 1:
            self.hasHeightVal = True
            self.defaultPlotType = "HEIGHT"
        elif self.ndim == 2:
            self.hasTimeVal = True
            self.hasHeightVal = True
            self.defaultPlotType = "TIMEHEIGHT"