def _get_cells_recursive(self, outcells, data_path, bounds_xy, bounds_t, pattern, x = 0., y = 0.): """ Helper for get_cells(). See documentation of get_cells() for usage """ lev = bhpix.get_pixel_level(x, y) dx = bhpix.pix_size(lev) # Check for nonzero overlap box = self._cell_bounds_xy(x, y, dx) bounds_xy = bounds_xy & box if not bounds_xy: return # Check if the cell directory exists (give up if it doesn't) path = data_path + '/' + self._path_to_cell_base_xy(x, y, lev) if not os.path.isdir(path): return # If re reached the bottom of the hierarchy if self._is_valid_cell_xy(x, y): # Get the cell_ids for leaf cells matching pattern xybounds = None if(bounds_xy.area() == box.area()) else bounds_xy sibling_cells = list(self._get_temporal_siblings(x, y, path, pattern)) for cell_id in sibling_cells: # Filter on time, add bounds x, y, t = self._xyt_from_cell_id(cell_id) if t != self.t0: # Cut on the time component tival = intervalset((t, t+self.dt)) tolap = bounds_t & tival if len(tolap): (l, r) = tolap[-1] # Get the right-most interval component if l == r == t+self.dt: # Is it a single point? tolap = intervalset(*tolap[:-1]) # Since objects in this cell have time in [t, t+dt), remove the t+dt point if len(tolap) == 0: # No overlap between the intervals -- skip this cell continue; # Return None if the cell is fully contained in the requested interval tbounds = None if tival == tolap else tolap else: # Static cell tbounds = bounds_t assert len(sibling_cells) == 1, "There can be only one static cell (cell_id=%s)" % cell_id # Add to output if tbounds is None: outcells[cell_id][xybounds] = None elif xybounds not in outcells[cell_id]: outcells[cell_id][xybounds] = tbounds elif outcells[cell_id][xybounds] is not None: outcells[cell_id][xybounds] |= tbounds else: # Recursively subdivide the four subpixels dx = dx / 2 for d in np.array([(0.5, 0.5), (-0.5, 0.5), (-0.5, -0.5), (0.5, -0.5)]): (x2, y2) = (x, y) + dx*d self._get_cells_recursive(outcells, data_path, bounds_xy & box, bounds_t, pattern, x2, y2)
def _path_to_cell_base_xy(self, x, y, level = None): # The only place in the code where we map from # (x, y) to cell base path if level is None: level = bhpix.get_pixel_level(x, y) path = []; for lev in xrange(1, level+1): (cx, cy) = bhpix.xy_center(x, y, lev) subpath = "%+g%+g" % (cx, cy) path.append(subpath) return '/'.join(path)
def _path_to_cell_base_xy(self, x, y, level=None): # The only place in the code where we map from # (x, y) to cell base path if level is None: level = bhpix.get_pixel_level(x, y) path = [] for lev in xrange(1, level + 1): (cx, cy) = bhpix.xy_center(x, y, lev) subpath = "%+g%+g" % (cx, cy) path.append(subpath) return '/'.join(path)
def _cell_bounds_xy(self, x, y, dx = None): """ Return a bounding polygon for a given cell center x,y """ if dx is None: lev = bhpix.get_pixel_level(x, y) dx = bhpix.pix_size(lev) ##dx = bhpix.pix_size(self.level) bounds = Polygon.Shapes.Rectangle(dx) bounds.shift(x - 0.5*dx, y - 0.5*dx); if fabs(fabs(x) - fabs(y)) == 0.5: # If it's a "halfpixel", return a triangle # by clipping agains the sky bounds &= bn.ALLSKY return bounds
def _cell_bounds_xy(self, x, y, dx=None): """ Return a bounding polygon for a given cell center x,y """ if dx is None: lev = bhpix.get_pixel_level(x, y) dx = bhpix.pix_size(lev) ##dx = bhpix.pix_size(self.level) bounds = Polygon.Shapes.Rectangle(dx) bounds.shift(x - 0.5 * dx, y - 0.5 * dx) if fabs(fabs(x) - fabs(y)) == 0.5: # If it's a "halfpixel", return a triangle # by clipping agains the sky bounds &= bn.ALLSKY return bounds
def _is_valid_cell_xy(self, x, y): return bhpix.get_pixel_level(x, y) == self.level
def _get_cells_recursive(self, outcells, data_path, bounds_xy, bounds_t, pattern, x=0., y=0.): """ Helper for get_cells(). See documentation of get_cells() for usage """ lev = bhpix.get_pixel_level(x, y) dx = bhpix.pix_size(lev) # Check for nonzero overlap box = self._cell_bounds_xy(x, y, dx) bounds_xy = bounds_xy & box if not bounds_xy: return # Check if the cell directory exists (give up if it doesn't) path = data_path + '/' + self._path_to_cell_base_xy(x, y, lev) if not os.path.isdir(path): return # If re reached the bottom of the hierarchy if self._is_valid_cell_xy(x, y): # Get the cell_ids for leaf cells matching pattern xybounds = None if (bounds_xy.area() == box.area()) else bounds_xy sibling_cells = list( self._get_temporal_siblings(x, y, path, pattern)) for cell_id in sibling_cells: # Filter on time, add bounds x, y, t = self._xyt_from_cell_id(cell_id) if t != self.t0: # Cut on the time component tival = intervalset((t, t + self.dt)) tolap = bounds_t & tival if len(tolap): (l, r ) = tolap[-1] # Get the right-most interval component if l == r == t + self.dt: # Is it a single point? tolap = intervalset( *tolap[:-1] ) # Since objects in this cell have time in [t, t+dt), remove the t+dt point if len( tolap ) == 0: # No overlap between the intervals -- skip this cell continue # Return None if the cell is fully contained in the requested interval tbounds = None if tival == tolap else tolap else: # Static cell tbounds = bounds_t assert len( sibling_cells ) == 1, "There can be only one static cell (cell_id=%s)" % cell_id # Add to output if tbounds is None: outcells[cell_id][xybounds] = None elif xybounds not in outcells[cell_id]: outcells[cell_id][xybounds] = tbounds elif outcells[cell_id][xybounds] is not None: outcells[cell_id][xybounds] |= tbounds else: # Recursively subdivide the four subpixels dx = dx / 2 for d in np.array([(0.5, 0.5), (-0.5, 0.5), (-0.5, -0.5), (0.5, -0.5)]): (x2, y2) = (x, y) + dx * d self._get_cells_recursive(outcells, data_path, bounds_xy & box, bounds_t, pattern, x2, y2)