Skip to content

Commit 357c1b6

Browse files
committed
Adding cube_cut function
1 parent b523278 commit 357c1b6

File tree

3 files changed

+101
-25
lines changed

3 files changed

+101
-25
lines changed

astrocut/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
# Import key submodules and functions if not in setup mode
3030
if not _ASTROPY_SETUP_: # noqa
3131
from .make_cube import CubeFactory, TicaCubeFactory # noqa
32-
from .cube_cut import CutoutFactory # noqa
32+
from .cube_cut import CutoutFactory, cube_cut # noqa
3333
from .cutouts import fits_cut, img_cut, normalize_img # noqa
3434
from .cutout_processing import ( # noqa
3535
path_to_footprints, center_on_path, CutoutsCombiner, build_default_combine_function # noqa

astrocut/cube_cut.py

Lines changed: 88 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22

33
"""This module implements the cutout functionality."""
44

5-
from typing import Literal, Union
5+
from pathlib import Path
6+
from typing import Literal, Optional, Union, List, Tuple
7+
8+
import astropy.units as u
9+
import numpy as np
10+
from astropy.coordinates import SkyCoord
11+
from s3path import S3Path
612

713
from .TessCubeCutout import TessCubeCutout
814

@@ -20,18 +26,11 @@ class CutoutFactory():
2026
`~astrocut.TessCubeCutout` class.
2127
"""
2228

23-
def cube_cut(
24-
self,
25-
cube_file,
26-
coordinates,
27-
cutout_size,
28-
product="SPOC",
29-
target_pixel_file=None,
30-
output_path=".",
31-
memory_only=False,
32-
threads: Union[int, Literal["auto"]] = 1,
33-
verbose=False,
34-
):
29+
def cube_cut(self, cube_file: Union[str, Path, S3Path], coordinates: Union[SkyCoord, str],
30+
cutout_size: Union[int, np.ndarray, u.Quantity, List[int], Tuple[int]],
31+
product: str = 'SPOC', target_pixel_file: Optional[str] = None,
32+
output_path: Union[str, Path] = '.', memory_only: bool = False,
33+
threads: Union[int, Literal["auto"]] = 1, verbose: bool = False):
3534
"""
3635
Takes a cube file (as created by `~astrocut.CubeFactory`), and makes a cutout target pixel
3736
file of the given size around the given coordinates. The target pixel file is formatted like
@@ -79,9 +78,10 @@ def cube_cut(
7978
8079
Returns
8180
-------
82-
response: string or None
83-
If successful, returns the path to the target pixel file,
84-
if unsuccessful returns None.
81+
response: string or `~astropy.io.fits.HDUList` or None
82+
If successful, returns the target pixel file as an `~astropy.io.fits.HDUList` object,
83+
or the path to the target pixel file if saved to disk.
84+
If unsuccessful returns None.
8585
"""
8686
cube_cutout = TessCubeCutout(input_files=cube_file,
8787
coordinates=coordinates,
@@ -102,3 +102,75 @@ def cube_cut(
102102

103103
return cube_cutout.write_as_tpf(output_dir=output_path,
104104
output_file=target_pixel_file)[0]
105+
106+
107+
108+
def cube_cut(cube_file: Union[str, Path, S3Path], coordinates: Union[SkyCoord, str],
109+
cutout_size: Union[int, np.ndarray, u.Quantity, List[int], Tuple[int]],
110+
product: str = 'SPOC', target_pixel_file: Optional[str] = None,
111+
output_path: Union[str, Path] = '.', memory_only: bool = False,
112+
threads: Union[int, Literal["auto"]] = 1, verbose: bool = False):
113+
"""
114+
Takes a cube file (as created by `~astrocut.CubeFactory`), and makes a cutout target pixel
115+
file of the given size around the given coordinates. The target pixel file is formatted like
116+
a TESS pipeline target pixel file.
117+
118+
This function is maintained for backwards compatibility. For maximum flexibility, we recommend using the
119+
`~astrocut.TessCubeCutout` class.
120+
121+
Parameters
122+
----------
123+
cube_file : str
124+
The cube file containing all the images to be cutout.
125+
Must be in the format returned by ~astrocut.make_cube.
126+
coordinates : str or `astropy.coordinates.SkyCoord` object
127+
The position around which to cutout.
128+
It may be specified as a string ("ra dec" in degrees)
129+
or as the appropriate `~astropy.coordinates.SkyCoord` object.
130+
cutout_size : int, array-like, `~astropy.units.Quantity`
131+
The size of the cutout array. If ``cutout_size``
132+
is a scalar number or a scalar `~astropy.units.Quantity`,
133+
then a square cutout of ``cutout_size`` will be created. If
134+
``cutout_size`` has two elements, they should be in ``(ny, nx)``
135+
order. Scalar numbers in ``cutout_size`` are assumed to be in
136+
units of pixels. `~astropy.units.Quantity` objects must be in pixel or
137+
angular units.
138+
product : str
139+
The product type to make the cutouts from.
140+
Can either be 'SPOC' or 'TICA' (default is 'SPOC').
141+
target_pixel_file : str
142+
Optional. The name for the output target pixel file.
143+
If no name is supplied, the file will be named:
144+
``<cube_file_base>_<ra>_<dec>_<cutout_size>_astrocut.fits``
145+
output_path : str
146+
Optional. The path where the output file is saved.
147+
The current directory is default.
148+
memory_only : bool
149+
Optional. If true, the cutout is made in memory only and not saved to disk.
150+
Default is False.
151+
threads : int, "auto", default=1
152+
Number of threads to use when making remote (e.g. s3) cutouts, will not use threads for local access
153+
<=1 disables the threadpool, >1 sets threadpool to the specified number of threads,
154+
"auto" uses `concurrent.futures.ThreadPoolExecutor`'s default: cpu_count + 4, limit to max of 32
155+
verbose : bool
156+
Optional. If true intermediate information is printed.
157+
158+
Returns
159+
-------
160+
response: string or `~astropy.io.fits.HDUList` or None
161+
If successful, returns the target pixel file as an `~astropy.io.fits.HDUList` object,
162+
or the path to the target pixel file if saved to disk.
163+
If unsuccessful, returns None.
164+
"""
165+
cube_cutout = TessCubeCutout(input_files=cube_file,
166+
coordinates=coordinates,
167+
cutout_size=cutout_size,
168+
product=product,
169+
threads=threads,
170+
verbose=verbose)
171+
172+
if memory_only:
173+
return cube_cutout.tpf_cutouts[0]
174+
175+
return cube_cutout.write_as_tpf(output_dir=output_path,
176+
output_file=target_pixel_file)[0]

astrocut/tests/test_cube_cut.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from astropy.time import Time
1313
from astropy.wcs import FITSFixedWarning
1414

15-
from ..cube_cut import CutoutFactory
15+
from ..cube_cut import CutoutFactory, cube_cut
1616
from ..exceptions import InputWarning
1717
from ..make_cube import CubeFactory, TicaCubeFactory
1818
from .utils_for_test import create_test_ffis
@@ -123,17 +123,15 @@ def check_flux(flux, x1, x2, y1, y2, ecube, label, hdulist):
123123

124124

125125
@pytest.mark.parametrize("ffi_type", ["SPOC", "TICA"])
126-
def test_cube_cutout(cube_file, ffi_files, ffi_type, tmp_path):
126+
@pytest.mark.parametrize("use_factory", [True, False])
127+
def test_cube_cutout(cube_file, ffi_files, ffi_type, use_factory, tmp_path):
127128
"""
128129
Testing the cube cutout functionality.
129130
"""
130131
tmpdir = str(tmp_path)
131-
132132
img_sz = 10
133133
num_im = 100
134134

135-
cutout_maker = CutoutFactory()
136-
137135
# Read one of the input images to get the WCS
138136
n = 1 if ffi_type == 'SPOC' else 0
139137
img_header = fits.getheader(ffi_files[0], n)
@@ -159,8 +157,14 @@ def test_cube_cutout(cube_file, ffi_files, ffi_type, tmp_path):
159157
csize[-1] = img_sz+5
160158
for i, v in enumerate(world_coords):
161159
coord = SkyCoord(v[0], v[1], frame='icrs', unit='deg')
162-
cutout_maker.cube_cut(cube_file, coord, csize[i], ffi_type, target_pixel_file=cutlist[i],
163-
output_path=tmpdir, verbose=False)
160+
if use_factory:
161+
cutout_maker = CutoutFactory()
162+
cutout_maker.cube_cut(cube_file, coord, csize[i], ffi_type, target_pixel_file=cutlist[i],
163+
output_path=tmpdir, verbose=False)
164+
else:
165+
cube_cut(cube_file, coord, csize[i], ffi_type, target_pixel_file=cutlist[i],
166+
output_path=tmpdir, verbose=False)
167+
164168

165169
# expected values for cube
166170
ecube = np.zeros((img_sz, img_sz, num_im, 2))
@@ -470,7 +474,7 @@ def test_tica_cutout_error(tmp_path):
470474

471475
ffi_type = 'TICA'
472476
# Test cutouts from TICA cube with error array
473-
test_cube_cutout(tica_cube_error_file, tica_ffi_files, ffi_type, tmp_path)
477+
test_cube_cutout(tica_cube_error_file, tica_ffi_files, ffi_type, True, tmp_path)
474478
test_header_keywords_quality(tica_cube_error_file, ffi_type, tmp_path)
475479
test_header_keywords_diffs(tica_cube_error_file, ffi_type, tmp_path)
476480
test_target_pixel_file(tica_cube_error_file, ffi_type, tmp_path)

0 commit comments

Comments
 (0)