-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from LCOGT/refactor/image-data-class
fits_file_reader and output_handler classes, changes to operations, tests
- Loading branch information
Showing
10 changed files
with
200 additions
and
89 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
datalab/datalab_session/data_operations/fits_output_handler.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import tempfile | ||
import numpy as np | ||
from astropy.io import fits | ||
|
||
from datalab.datalab_session.file_utils import create_jpgs | ||
from datalab.datalab_session.s3_utils import save_fits_and_thumbnails | ||
|
||
|
||
class FITSOutputHandler(): | ||
"""A class to handle FITS output files and create jpgs. | ||
Class handles the creation of Datalab output for developers. | ||
The class inits with a cache_key and data, and creates a FITS file with the data. | ||
The FITS file is then saved to the cache and the large and small jpgs are created. | ||
Attributes: | ||
datalab_id (str): The cache key for the FITS file. | ||
primary_hdu (fits.PrimaryHDU): The primary HDU for the FITS file. | ||
image_hdu (fits.ImageHDU): The image HDU for the FITS file. | ||
data (np.array): The data for the image HDU. | ||
""" | ||
|
||
def __init__(self, cache_key: str, data: np.array, comment: str=None) -> None: | ||
"""Inits FITSOutputHandler with cache_key and data. | ||
Args: | ||
cache_key (str): The cache key for the FITS file, used as an ID when stored in S3. | ||
data (np.array): The data that will create the image HDU. | ||
comment (str): Optionally add a comment to add to the FITS file. | ||
""" | ||
self.datalab_id = cache_key | ||
self.primary_hdu = fits.PrimaryHDU(header=fits.Header([('KEY', cache_key)])) | ||
self.image_hdu = fits.ImageHDU(data=data, name='SCI') | ||
if comment: self.set_comment(comment) | ||
|
||
def __str__(self) -> str: | ||
return f"Key: {self.datalab_id}\nData:\n{self.data}" | ||
|
||
def set_comment(self, comment: str): | ||
"""Add a comment to the FITS file.""" | ||
self.primary_hdu.header.add_comment(comment) | ||
|
||
def create_and_save_data_products(self, index: int=None, large_jpg_path: str=None, small_jpg_path: str=None): | ||
"""Create the FITS file and save it to S3. | ||
This function can be called when you're done with the operation and would like to save the FITS file and jpgs in S3. | ||
It returns a datalab output dictionary that is formatted to be readable by the frontend. | ||
Args: | ||
index (int): Optionally add an index to the FITS file name. Appended to cache_key for multiple outputs. | ||
large_jpg (str): Optionally add a path to a large jpg to save, will not create a new jpg. | ||
small_jpg (str): Optionally add a path to a small jpg to save, will not create a new jpg. | ||
""" | ||
hdu_list = fits.HDUList([self.primary_hdu, self.image_hdu]) | ||
fits_output_path = tempfile.NamedTemporaryFile(suffix=f'{self.datalab_id}.fits').name | ||
hdu_list.writeto(fits_output_path, overwrite=True) | ||
|
||
# allow for operations to pregenerate the jpgs, ex. RGB stacking | ||
if not large_jpg_path or not small_jpg_path: | ||
large_jpg_path, small_jpg_path = create_jpgs(self.datalab_id, fits_output_path) | ||
|
||
return save_fits_and_thumbnails(self.datalab_id, fits_output_path, large_jpg_path, small_jpg_path, index) |
44 changes: 44 additions & 0 deletions
44
datalab/datalab_session/data_operations/input_data_handler.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from astropy.io import fits | ||
|
||
from datalab.datalab_session.s3_utils import get_fits | ||
from datalab.datalab_session.file_utils import get_hdu | ||
|
||
class InputDataHandler(): | ||
"""A class to read FITS files and provide access to the data. | ||
The class inits with a basename and source, and reads the FITS file | ||
this data is then stored in the class attributes for easy access. | ||
Attributes: | ||
basename (str): The basename of the FITS file. | ||
fits_file (str): The path to the FITS file. | ||
sci_data (np.array): The data from the 'SCI' extension of the FITS file. | ||
""" | ||
|
||
def __init__(self, basename: str, source: str = None) -> None: | ||
"""Inits InputDataHandler with basename and source. | ||
Uses the basename to query the archive for the matching FITS file. | ||
Also can take a source argument to specify a different source for the FITS file. | ||
At the time of writing two common sources are 'datalab' and 'archive'. | ||
New sources will need to be added in the get_fits function in s3_utils.py. | ||
Args: | ||
basename (str): The basename of the FITS file. | ||
source (str): Optionally add a source to the FITS file in case it's not the LCO archive. | ||
""" | ||
self.basename = basename | ||
self.fits_file = get_fits(basename, source) | ||
self.sci_data = get_hdu(self.fits_file, 'SCI').data | ||
|
||
def __str__(self) -> str: | ||
with fits.open(self.fits_file) as hdul: | ||
return f"{self.basename}@{self.fits_file}\nHDU List\n{self.hdul.info()}" | ||
|
||
def get_hdu(self, extension: str=None): | ||
"""Return an HDU from the FITS file. | ||
Args: | ||
extension (str): The extension to return from the FITS file. Default is 'SCI'. | ||
""" | ||
return get_hdu(self.fits_file, extension) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file modified
BIN
+0 Bytes
(100%)
datalab/datalab_session/tests/test_files/median/median_1_2.fits
Binary file not shown.
Binary file modified
BIN
+0 Bytes
(100%)
datalab/datalab_session/tests/test_files/rgb_stack/rgb_stack.fits
Binary file not shown.
Oops, something went wrong.