Пример #1
0
    def __init__(self, uri, **kwds):
        self.uri = uri
        self.dataset = self.connect(uri)

        ## construct interfaces
        self.spatial = SpatialInterface(self.dataset, **kwds)
        self.temporal = TemporalInterface(self.dataset, **kwds)
        try:
            self.level = LevelInterface(self.dataset, **kwds)
        except PolyElementNotFound:
            warn('No "level" variable found. Assuming NoneType.')
            self.level = None

        ## extract other keyword arguments -------------------------------------
        self.verbose = kwds.get('verbose')
        self.time_units = kwds.get(
            'time_units') or 'days since 1950-01-01 00:00:00'
        self.calendar = kwds.get('calendar') or 'proleptic_gregorian'
        self.level_name = kwds.get('level_name') or 'levels'

        ## extract the row and column bounds from the dataset
        self.row_bnds = self.spatial.rowbnds.value[:]
        self.col_bnds = self.spatial.colbnds.value[:]

        ## convert the time vector to datetime objects
        self.timevec = nc.netcdftime.num2date(self.temporal.time.value[:],
                                              self.time_units, self.calendar)
        self.timeidx = np.arange(0, len(self.timevec))
        self.tids = np.arange(1, len(self.timevec) + 1)

        ## pull levels if possible
        if self.level is not None:
            self.levelvec = np.arange(1, len(self.level.level.value[:]) + 1)
            self.levelidx = np.arange(0, len(self.levelvec))
        else:
            self.levelvec = np.array([1])
            self.levelidx = np.array([0])

        ## these are base numpy arrays used by spatial operations. -------------

        ## four numpy arrays one for each bounding coordinate of a polygon
        self.min_col, self.min_row = self.spatial.get_min_bounds()
        self.max_col, self.max_row = self.spatial.get_max_bounds()
        ## these are the original indices of the row and columns. they are
        ## referenced after the spatial subset to retrieve data from the dataset
        self.real_col, self.real_row = np.meshgrid(
            np.arange(0, len(self.col_bnds)), np.arange(0, len(self.row_bnds)))
        ## calculate approximate data resolution
        self.res = approx_resolution(self.min_col[0, :])
        ## generate unique id for each grid cell
        self.gids = np.arange(
            1, self.real_col.shape[0] * self.real_col.shape[1] + 1)
        self.gids = self.gids.reshape(self.real_col.shape)
        #        self.gids = np.empty(self.real_col.shape,dtype=int)
        #        curr_id = 1
        #        for i,j in itr_array(self.gids):
        #            self.gids[i,j] = curr_id
        #            curr_id += 1
        ## set the array shape.
        self.shape = self.real_col.shape