Skip to content

Commit da0c7e5

Browse files
authored
Merge pull request #95 from mstcyr2/fits-cut-cloud
Cloud functionality for astrocut.fits_cut()
2 parents ac8fad9 + 3db61e6 commit da0c7e5

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

astrocut/cutouts.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,9 @@ def fits_cut(input_files, coordinates, cutout_size, correct_wcs=False, extension
230230
set the number of returned `~astropy.io.fits.HDUList` objects.
231231
verbose : bool
232232
Default False. If true intermediate information is printed.
233+
fsspec_kwargs : any
234+
Default value {"anon": True}. This parameter should be used to provide cloud credentials to
235+
access private data buckets (e.g. {"key": "YOUR-SECRET-KEY-ID", "secret": "YOUR-SECRET-KEY"}).
233236
234237
Returns
235238
-------
@@ -267,12 +270,16 @@ def fits_cut(input_files, coordinates, cutout_size, correct_wcs=False, extension
267270
cutout_hdu_dict = {}
268271
num_empty = 0
269272
num_cutouts = 0
273+
fsspec_kwargs = None
270274
for in_fle in input_files:
271275
if verbose:
272276
print("\nCutting out {}".format(in_fle))
273277

278+
if "s3://" in in_fle:
279+
fsspec_kwargs = {"anon": True}
280+
274281
warnings.filterwarnings("ignore", category=wcs.FITSFixedWarning)
275-
with fits.open(in_fle, mode='denywrite', memmap=True) as hdulist:
282+
with fits.open(in_fle, mode='denywrite', memmap=True, fsspec_kwargs=fsspec_kwargs) as hdulist:
276283

277284
# Sorting out which extension(s) to cutout
278285
all_inds = np.where([x.is_image and (x.data is not None) for x in hdulist])[0]

astrocut/tests/test_cutouts.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,17 @@ def test_fits_cut(tmpdir, capsys, ffi_type):
182182
assert "10-x-15" in cutout_file
183183
assert "x-20" not in cutout_file
184184

185+
# Test single cloud image
186+
test_s3_uri = "s3://stpubdata/hst/public/j8pu/j8pu0y010/j8pu0y010_drc.fits"
187+
center_coord = SkyCoord("150.4275416667 2.42155", unit='deg')
188+
cutout_size = [10, 15]
189+
cutout_file = cutouts.fits_cut(test_s3_uri, center_coord, cutout_size, output_dir=tmpdir)
190+
assert isinstance(cutout_file, str)
191+
assert "10-x-15" in cutout_file
192+
193+
with fits.open(cutout_file) as cutout_hdulist:
194+
assert cutout_hdulist[1].data.shape == (15, 10)
195+
185196

186197
def test_normalize_img():
187198

docs/astrocut/index.rst

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,29 @@ The cutout(s) can also be returned in memory as `~astropy.io.fits.HDUList` objec
8484
2 CUTOUT 1 ImageHDU 44 (200, 300) float32
8585
8686
87-
87+
`~astrocut.fits_cut` also has the ability to take FITS files from the cloud using S3 URIs.
88+
89+
.. code-block:: python
90+
91+
>>> from astrocut import fits_cut
92+
>>> from astropy.io import fits
93+
>>> from astropy.coordinates import SkyCoord
94+
95+
>>> s3_uri = "s3://stpubdata/hst/public/j8pu/j8pu0y010/j8pu0y010_drz.fits"
96+
97+
>>> center_coord = SkyCoord("150.42838 2.421421", unit='deg')
98+
>>> cutout_size = [100,100]
99+
100+
>>> cutout_file = fits_cut(input_files, center_coord, cutout_size,
101+
... single_outfile=True, memory_only=True) #doctest: +SKIP
102+
>>> cutout_hdulist = fits.open(cutout_file) #doctest: +SKIP
103+
>>> cutout_hdulist.info() #doctest: +SKIP
104+
Filename: ./cutout_150.428380_2.421421_100-x-100_astrocut.fits
105+
No. Name Ver Type Cards Dimensions Format
106+
0 PRIMARY 1 PrimaryHDU 11 ()
107+
1 CUTOUT 1 ImageHDU 97 (100, 100) float32
108+
109+
88110
Creating image cutouts
89111
----------------------
90112

0 commit comments

Comments
 (0)