Skip to content

Commit

Permalink
Merge pull request #36 from pkgw/add-target-shape
Browse files Browse the repository at this point in the history
Add `target_shape` argument to `AVM.to_wcs()`
  • Loading branch information
astrofrog committed Aug 6, 2020
2 parents dfd98ce + daf908d commit 5a39d19
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Expand Up @@ -4,8 +4,9 @@ sudo: false

python:
- 2.7
- 3.5
- 3.6
- 3.7
- 3.8

env:
matrix:
Expand Down
20 changes: 12 additions & 8 deletions pyavm/avm.py
Expand Up @@ -436,7 +436,7 @@ def from_xml(cls, xml):

return self

def to_wcs(self, use_full_header=False, target_image=None):
def to_wcs(self, use_full_header=False, target_image=None, target_shape=None):
"""
Convert AVM projection information into a Astropy WCS object.
Expand All @@ -451,6 +451,11 @@ def to_wcs(self, use_full_header=False, target_image=None):
the AVM was defined. The `target_image` option can be used to pass
the path of an image from which the size will be used to re-scale
the WCS.
target_shape : tuple of ``(nx: int, ny: int)``, optional
Serves the same function as `target_image`, but for cases where the
target dimensions are known without having to open and read an image
file. If both this and `target_image` are specified, `target_image`
takes precedence.
"""

if not astropy_installed:
Expand Down Expand Up @@ -548,17 +553,16 @@ def to_wcs(self, use_full_header=False, target_image=None):
# If `target_image` is set, we have to rescale the reference pixel and
# the scale
if target_image is not None:

# Find target image size
from PIL import Image
nx, ny = Image.open(target_image).size
target_shape = Image.open(target_image).size

if target_shape is not None:
if self.Spatial.ReferenceDimension is None:
raise ValueError("Spatial.ReferenceDimension should be set in order to determine scale in target image")

# Find scale in x and y
scale_x = nx / float(wcs_naxis1)
scale_y = ny / float(wcs_naxis2)
scale_x = target_shape[0] / float(wcs_naxis1)
scale_y = target_shape[1] / float(wcs_naxis2)

# Check that scales are consistent
if abs(scale_x - scale_y) / (scale_x + scale_y) * 2. < 0.01:
Expand All @@ -570,8 +574,8 @@ def to_wcs(self, use_full_header=False, target_image=None):
wcs.wcs.crpix *= scale

if hasattr(wcs, 'naxis1'): # PyWCS and Astropy < 0.4
wcs.naxis1 = nx
wcs.naxis2 = ny
wcs.naxis1 = target_shape[0]
wcs.naxis2 = target_shape[1]

return wcs

Expand Down
9 changes: 9 additions & 0 deletions pyavm/tests/test_main.py
Expand Up @@ -58,6 +58,15 @@ def test_to_wcs_target_image(filename, tmpdir):
a.to_wcs(target_image=image_file)


@pytest.mark.parametrize('filename', XML_FILES_WCS)
def test_to_wcs_target_shape(filename, tmpdir):
pytest.importorskip('PIL')
pytest.importorskip('astropy')
a = AVM.from_xml_file(filename)
a.Spatial.ReferenceDimension = (30, 30)
a.to_wcs(target_shape=(2, 2))


@pytest.mark.parametrize('filename', NO_WCS)
def test_to_wcs_nowcs(filename):
pytest.importorskip('astropy')
Expand Down

0 comments on commit 5a39d19

Please sign in to comment.