|
| 1 | +import astropy.units as u |
| 2 | +from astropy.coordinates import SkyCoord, Angle |
| 3 | +from astropy.wcs import WCS |
| 4 | +from gwcs.wcs import WCS as GWCS |
| 5 | + |
| 6 | +from jdaviz.utils import data_has_valid_wcs, get_top_layer_index |
| 7 | + |
| 8 | + |
| 9 | +class AID: |
| 10 | + """ |
| 11 | + Common API methods for image viewers in astronomy, called |
| 12 | + the Astro Image Display API (AIDA)[1]_. |
| 13 | +
|
| 14 | + References |
| 15 | + ---------- |
| 16 | + .. [1] https://github.com/astropy/astro-image-display-api/ |
| 17 | +
|
| 18 | + """ |
| 19 | + |
| 20 | + def __init__(self, viewer): |
| 21 | + self.viewer = viewer |
| 22 | + self.app = viewer.jdaviz_app |
| 23 | + |
| 24 | + def _get_image_glue_data(self, image_label): |
| 25 | + if image_label is None: |
| 26 | + i_top = get_top_layer_index(self.viewer) |
| 27 | + image = self.viewer.layers[i_top].layer |
| 28 | + |
| 29 | + else: |
| 30 | + for lyr in self.viewer.layers: |
| 31 | + image = lyr.layer |
| 32 | + if image.label == image_label: |
| 33 | + break |
| 34 | + else: |
| 35 | + raise ValueError(f"No data with data_label {image_label}` found in viewer.") |
| 36 | + |
| 37 | + return image, image.label |
| 38 | + |
| 39 | + def set_viewport(self, center=None, fov=None, image_label=None, **kwargs): |
| 40 | + """ |
| 41 | + Parameters |
| 42 | + ---------- |
| 43 | + center : `~astropy.coordinates.SkyCoord` or tuple of floats |
| 44 | + Center the viewer on this coordinate. |
| 45 | +
|
| 46 | + fov : `~astropy.units.Quantity` or tuple of floats |
| 47 | + Set the width of the viewport to span `field_of_view`. |
| 48 | +
|
| 49 | + image_label : str |
| 50 | + Set the viewport with respect to the image |
| 51 | + with the data label: ``image_label``. |
| 52 | + """ |
| 53 | + image, image_label = self._get_image_glue_data(image_label) |
| 54 | + |
| 55 | + if center is None: |
| 56 | + # get the current center in the pixel coords on reference data |
| 57 | + x_min, x_max, y_min, y_max = self.viewer.get_limits() |
| 58 | + center_x = 0.5 * (x_min + x_max) |
| 59 | + center_y = 0.5 * (y_min + y_max) |
| 60 | + center = (center_x, center_y) |
| 61 | + |
| 62 | + if isinstance(center, SkyCoord): |
| 63 | + reference_wcs = self.viewer.state.reference_data.coords |
| 64 | + |
| 65 | + if isinstance(reference_wcs, GWCS): |
| 66 | + reference_wcs = WCS(reference_wcs.to_fits_sip()) |
| 67 | + |
| 68 | + reference_center_pix = reference_wcs.world_to_pixel(center) |
| 69 | + |
| 70 | + elif hasattr(center, '__len__') and isinstance(center[0], (float, int)): |
| 71 | + reference_center_pix = center |
| 72 | + |
| 73 | + current_width = self.viewer.state.x_max - self.viewer.state.x_min |
| 74 | + current_height = self.viewer.state.y_max - self.viewer.state.y_min |
| 75 | + |
| 76 | + if fov is None: |
| 77 | + new_width = current_width |
| 78 | + new_height = current_height |
| 79 | + else: |
| 80 | + if isinstance(fov, (u.Quantity, Angle)): |
| 81 | + current_fov = self._get_current_fov('sky') |
| 82 | + scale_factor = float(fov / current_fov) |
| 83 | + |
| 84 | + elif isinstance(fov, (float, int)): |
| 85 | + current_fov = self._get_current_fov('pixel') |
| 86 | + scale_factor = float(fov / current_fov) |
| 87 | + |
| 88 | + new_width = current_width * scale_factor |
| 89 | + new_height = current_height * scale_factor |
| 90 | + |
| 91 | + new_xmin = reference_center_pix[0] - (new_width * 0.5) |
| 92 | + new_ymin = reference_center_pix[1] - (new_height * 0.5) |
| 93 | + |
| 94 | + self.viewer.set_limits( |
| 95 | + x_min=new_xmin, |
| 96 | + x_max=new_xmin + new_width, |
| 97 | + y_min=new_ymin, |
| 98 | + y_max=new_ymin + new_height |
| 99 | + ) |
| 100 | + |
| 101 | + def _get_current_fov(self, sky_or_pixel): |
| 102 | + x_min, x_max, y_min, y_max = self.viewer.get_limits() |
| 103 | + |
| 104 | + if sky_or_pixel == 'sky': |
| 105 | + wcs = self.viewer.state.reference_data.coords |
| 106 | + |
| 107 | + # for now, convert GWCS to FITS SIP so pixel to world |
| 108 | + # transformations can be done outside of the bounding box |
| 109 | + if isinstance(wcs, GWCS): |
| 110 | + wcs = WCS(wcs.to_fits_sip()) |
| 111 | + |
| 112 | + lower_left, lower_right, upper_left = wcs.pixel_to_world( |
| 113 | + [x_min, y_min, x_min], [x_max, y_min, y_max] |
| 114 | + ) |
| 115 | + |
| 116 | + return lower_left.separation(lower_right) |
| 117 | + else: |
| 118 | + return x_max - x_min |
| 119 | + |
| 120 | + def get_viewport(self, sky_or_pixel=None, image_label=None, **kwargs): |
| 121 | + """ |
| 122 | + sky_or_pixel : str, optional |
| 123 | + If 'sky', the center will be returned as a `SkyCoord` object. |
| 124 | + If 'pixel', the center will be returned as a tuple of pixel coordinates. |
| 125 | + If `None`, the default behavior is to return the center as a `SkyCoord` if |
| 126 | + possible, or as a tuple of floats if the image is in pixel coordinates and has |
| 127 | + no WCS information. |
| 128 | +
|
| 129 | + image_label : str, optional |
| 130 | + The label of the image to get the viewport for. If not given and there is only one |
| 131 | + image loaded, the viewport for that image is returned. If there are multiple images |
| 132 | + and no label is provided, an error is raised. |
| 133 | +
|
| 134 | + Returns |
| 135 | + ------- |
| 136 | + dict |
| 137 | + A dictionary containing the current viewport settings. |
| 138 | + The keys are 'center', 'fov', and 'image_label'. |
| 139 | + - 'center' is an `astropy.coordinates.SkyCoord` object or a tuple of floats. |
| 140 | + - 'fov' is an `astropy.units.Quantity` object or a float. |
| 141 | + - 'image_label' is a string representing the label of the image. |
| 142 | + """ |
| 143 | + # viewer_aligned_by_wcs = self.app._align_by == 'wcs' |
| 144 | + |
| 145 | + image, image_label = self._get_image_glue_data(image_label) |
| 146 | + reference_data = self.viewer.state.reference_data |
| 147 | + reference_wcs = reference_data.coords |
| 148 | + |
| 149 | + x_min, x_max, y_min, y_max = self.viewer.get_limits() |
| 150 | + center_x = 0.5 * (x_min + x_max) |
| 151 | + center_y = 0.5 * (y_min + y_max) |
| 152 | + |
| 153 | + # default to 'sky' if sky/pixel not specified and WCS is available: |
| 154 | + if sky_or_pixel is None and data_has_valid_wcs(image): |
| 155 | + sky_or_pixel = 'sky' |
| 156 | + |
| 157 | + # if the image data have WCS, get the center sky coordinate: |
| 158 | + if sky_or_pixel == 'sky': |
| 159 | + if self.app._align_by == 'wcs': |
| 160 | + center = self.viewer._get_center_skycoord() |
| 161 | + else: |
| 162 | + center = reference_wcs.pixel_to_world(center_x, center_y) |
| 163 | + |
| 164 | + fov = self._get_current_fov(sky_or_pixel) |
| 165 | + |
| 166 | + else: |
| 167 | + center = (center_x, center_y) |
| 168 | + fov = x_max - x_min |
| 169 | + |
| 170 | + return dict(center=center, fov=fov, image_label=image_label) |
0 commit comments