Skip to content

Commit 622a560

Browse files
committed
ASDF Cutout Progress
1 parent 210aa26 commit 622a560

File tree

8 files changed

+840
-14
lines changed

8 files changed

+840
-14
lines changed

astrocut/ASDFCutout.py

Lines changed: 412 additions & 0 deletions
Large diffs are not rendered by default.

astrocut/Cutout.py

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
from abc import abstractmethod, ABC
22
from pathlib import Path
33
from typing import List, Union, Tuple
4+
import warnings
45

56
from astropy import wcs
67
import astropy.units as u
78
from s3path import S3Path
89
from astropy.coordinates import SkyCoord
910
import numpy as np
1011

11-
from astrocut.exceptions import InvalidInputError, InvalidQueryError
12+
from astrocut.exceptions import InputWarning, InvalidInputError, InvalidQueryError
1213

1314
from . import log
14-
from .utils.utils import _handle_verbose, parse_size_input
15+
from .utils.utils import _handle_verbose
1516

1617

1718
class Cutout(ABC):
@@ -60,13 +61,15 @@ def __init__(self, input_files: List[Union[str, Path, S3Path]], coordinates: Uni
6061
self._input_files = input_files
6162

6263
# Get coordinates as a SkyCoord object
63-
if coordinates and not isinstance(coordinates, SkyCoord):
64+
if isinstance(coordinates, tuple):
65+
coordinates = SkyCoord(*coordinates, unit='deg')
66+
elif not isinstance(coordinates, SkyCoord):
6467
coordinates = SkyCoord(coordinates, unit='deg')
6568
self._coordinates = coordinates
6669
log.debug('Coordinates: %s', self._coordinates)
6770

6871
# Turning the cutout size into an array of two values
69-
self._cutout_size = parse_size_input(cutout_size)
72+
self._cutout_size = self.parse_size_input(cutout_size)
7073
log.debug('Cutout size: %s', self._cutout_size)
7174

7275
# Assigning other attributes
@@ -84,6 +87,54 @@ def __init__(self, input_files: List[Union[str, Path, S3Path]], coordinates: Uni
8487
self._output_dir = output_dir
8588
self._verbose = verbose
8689

90+
def parse_size_input(self, cutout_size):
91+
"""
92+
Makes the given cutout size into a length 2 array.
93+
94+
Parameters
95+
----------
96+
cutout_size : int, array-like, `~astropy.units.Quantity`
97+
The size of the cutout array. If ``cutout_size`` is a scalar number or a scalar
98+
`~astropy.units.Quantity`, then a square cutout of ``cutout_size`` will be created.
99+
If ``cutout_size`` has two elements, they should be in ``(ny, nx)`` order. Scalar numbers
100+
in ``cutout_size`` are assumed to be in units of pixels. `~astropy.units.Quantity` objects
101+
must be in pixel or angular units.
102+
103+
Returns
104+
-------
105+
response : array
106+
Length two cutout size array, in the form [ny, nx].
107+
"""
108+
109+
# Making size into an array [ny, nx]
110+
if np.isscalar(cutout_size):
111+
cutout_size = np.repeat(cutout_size, 2)
112+
113+
if isinstance(cutout_size, u.Quantity):
114+
cutout_size = np.atleast_1d(cutout_size)
115+
if len(cutout_size) == 1:
116+
cutout_size = np.repeat(cutout_size, 2)
117+
elif not isinstance(cutout_size, np.ndarray):
118+
cutout_size = np.array(cutout_size)
119+
120+
if len(cutout_size) > 2:
121+
warnings.warn('Too many dimensions in cutout size, only the first two will be used.',
122+
InputWarning)
123+
cutout_size = cutout_size[:2]
124+
125+
126+
for dim in cutout_size:
127+
# Raise error if either dimension is not a positive number
128+
if dim <= 0:
129+
raise InvalidInputError('Cutout size dimensions must be greater than zero. '
130+
f'Provided size: ({cutout_size[0]}, {cutout_size[1]})')
131+
132+
# Raise error if either dimension is not an pixel or angular Quantity
133+
if isinstance(dim, u.Quantity) and dim.unit != u.pixel and dim.unit.physical_type != 'angle':
134+
raise InvalidInputError(f'Cutout size unit {dim.unit.aliases[0]} is not supported.')
135+
136+
return cutout_size
137+
87138
def _get_cutout_limits(self, img_wcs: wcs.WCS) -> np.ndarray:
88139
"""
89140
Returns the x and y pixel limits for the cutout.

astrocut/FITSCutout.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,10 @@ class FITSCutout(ImageCutout):
8484
Cutout an image file.
8585
_construct_fits_from_hdus()
8686
Make one or more cutout HDUs into a single HDUList object.
87-
_write_to_memory()
88-
Write the cutouts to memory.
8987
_write_as_fits()
90-
Write the cutouts to a file in FITS format.
88+
Write the cutouts to disk or memory in FITS format.
9189
_write_as_asdf()
92-
Write the cutouts to a file in ASDF format.
90+
Write the cutouts to disk or memory in ASDF format.
9391
"""
9492

9593
def __init__(self, input_files: List[Union[str, Path, S3Path]], coordinates: Union[SkyCoord, str],
@@ -461,7 +459,7 @@ def _write_as_fits(self) -> Union[str, List[str]]:
461459
Returns
462460
-------
463461
cutout_paths : str | list
464-
The path(s) to the cutout file(s).
462+
The path(s) to the cutout file(s) or the cutout memory objects.
465463
"""
466464
if self._single_outfile:
467465
log.debug('Returning cutout as a single FITS file.')

astrocut/ImageCutout.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,10 @@ def __init__(self, input_files: List[Union[str, Path, S3Path]], coordinates: Uni
102102
self._output_format = f'.{out_lower}' if not output_format.startswith('.') else out_lower
103103

104104
# Warn if image processing parameters are provided for FITS output
105-
if (self._output_format == '.fits') and (stretch or minmax_percent or
106-
minmax_value or invert or colorize):
105+
if ((self._output_format == '.fits' or self._output_format == '.asdf') and
106+
(stretch or minmax_percent or minmax_value or invert or colorize)):
107107
warnings.warn('Stretch, minmax_percent, minmax_value, invert, and colorize are not supported '
108-
'for FITS output and will be ignored.', InputWarning)
108+
'for FITS or ASDF output and will be ignored.', InputWarning)
109109

110110
# Assign attributes with defaults if not provided
111111
stretch = stretch or 'asinh'

0 commit comments

Comments
 (0)