import math import numpy as np import matplotlib.pyplot as plt class BaseGrid(object): _valid_grid_types = ['RectGrid', 'HexGrid'] _valid_mixed_types = ['MixedGrid'] class Grid(BaseGrid): def __init__(self, grid_spacing, wcs, Npix_x=10000, Npix_y=10000, pixelscale=0.074, rot_angle=None, pos_offset=None, angle_unit='rad'): self.grid_spacing = grid_spacing self.im_gs = grid_spacing * (1.0 / pixelscale) # pixels self.pixelscale = pixelscale self.Npix_x, self.Npix_y = Npix_x, Npix_y self.wcs = wcs self.rot_angle = rot_angle # rotation angle, in rad self.angle_unit = angle_unit if pos_offset: self.pos_offset = np.array(pos_offset) else: self.pos_offset = np.array([0., 0.]) # May have to modify grid corners if there is a rotation if rot_angle: dx = Npix_x / 2. dy = Npix_y / 2. if angle_unit == 'deg': theta = np.deg2rad(rot_angle) else: theta = rot_angle self.startx = (0.-dx) * np.cos(theta) - (Npix_y-dy) * np.sin(theta) + dx self.endx = (Npix_x-dx) * np.cos(theta) - (0.-dy) * np.sin(theta) + dx self.starty = (0.-dx) * np.cos(theta) + (0.-dy) * np.sin(theta) + dx self.endy = (Npix_x-dx) * np.cos(theta) + (Npix_y-dy) * np.sin(theta) + dx else: self.startx, self.endx= 0., Npix_x self.starty, self.endy= 0., Npix_y def rotate_grid(self, theta, offset=None, angle_unit='rad'): if angle_unit == 'deg': theta = np.deg2rad(theta) elif angle_unit != 'rad': raise ValueError('`angle_unit` can only be `deg` or `rad`! ' + 'Passed unit of {}'.format(angle_unit)) if not offset: offset = [0., 0.] c, s = np.cos(theta), np.sin(theta) R = np.array(((c,-s), (s, c))) offset_grid = np.array([self.im_ra - offset[0], self.im_dec - offset[1]]) translate = np.empty_like(offset_grid) translate[0,:] = offset[0] translate[1,:] = offset[1] rotated_grid = np.dot(R, offset_grid) + translate self.im_pos = rotated_grid.T self.im_ra, self.im_dec = self.im_pos[0,:], self.im_pos[1,:] def cut2buffer(self): ''' Remove objects outside of tile (and buffer). We must sample points in the buffer zone in the beginning due to possible rotations. ''' b = self.im_gs in_region = np.where( (self.im_pos[:,0]>b) & (self.im_pos[:,0]b) & (self.im_pos[:,1]