Example #1
0
 def getvarsage(self, variable):
     # try getting data
     file = self.directory + self.filename
     try:
         f = netCDF4.Dataset(file, "r")
         data = np.asarray(f.variables[variable][:])
         # check for additional attributes
         try:
             units = f.variables[variable].units
         except AttributeError:
             units = None
         try:
             missing = f.variables[variable].missing_value
         except AttributeError:
             missing = None
         f.close()
     # otherwise, print Error
     except Error:
         print Error
     data = np.asarray(data)
     data = calc.removemissing(data, missing)
     self.variables[variable] = data
     return data
Example #2
0
    def getvargiss(self, variable, control=None):
        # check if we already have the variable
        if variable in self.variables:
            return np.array(self.variables[variable], copy=True)
        elif variable == "pr3d":
            return self.__getpr3d(control=control)
        data2return = []

        # iterate over time
        for year in self.years:
            for month in self.months:

                # open file
                file = (
                    self.directory
                    + self.filename
                    + "/"
                    + month
                    + year
                    + "."
                    + datatypes[variable]
                    + self.filename
                    + ".nc"
                )

                # try getting data
                try:
                    f = netCDF4.Dataset(file, "r")
                    data = np.asarray(f.variables[variable][:])
                    # check for additional attributes
                    try:
                        units = f.variables[variable].units
                    except AttributeError as error:
                        units = None
                    try:
                        missing = f.variables[variable].missing_value
                    except AttributeError as error:
                        missing = None
                    f.close()

                # otherwise, fill with control or NaN
                except RuntimeError as error:
                    if control is not None:
                        data = control.getvarmonth(variable, month, str(int(year) - 173))
                    else:
                        if variable == "plm":
                            data = np.zeros((40))
                            data.fill(np.nan)
                        elif datatypes[variable][-1] == "l":
                            data = np.zeros((40, 90, 144))
                            data.fill(np.nan)
                        else:
                            data = np.zeros((90, 144))
                            data.fill(np.nan)
                    units = None
                    missing = None
                data2return.append(data)

        # clean and convert data before returning
        data2return = np.asarray(data2return)
        data2return = calc.removemissing(data2return, missing)
        data2return = calc.convert(data2return, units, variable, self, control=control)

        # store for later use
        self.variables[variable] = np.array(data2return, copy=True)

        return np.array(data2return, copy=True)