-
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.
fits_file_reader and output_handler classes, changes to operations, t…
…ests
- Loading branch information
Showing
10 changed files
with
140 additions
and
87 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
datalab/datalab_session/data_operations/fits_file_reader.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,25 @@ | ||
from astropy.io import fits | ||
|
||
from datalab.datalab_session.s3_utils import get_fits | ||
from datalab.datalab_session.file_utils import get_hdu | ||
|
||
class FITSFileReader: | ||
|
||
basename = None | ||
fits_file = None | ||
hdu_list = None | ||
|
||
def __init__(self, basename: str, source: str = None) -> None: | ||
self.basename = basename | ||
self.fits_file = get_fits(basename, source) | ||
self.hdu_list = fits.open(self.fits_file) | ||
|
||
def __str__(self) -> str: | ||
return f"{self.basename}@{self.fits_file}\nHDU List\n{self.hdu_list.info()}" | ||
|
||
@property | ||
def sci_data(self): | ||
return self.hdu_list['SCI'].data | ||
|
||
def hdu(self, extension: str): | ||
return get_hdu(self.fits_file, extension) |
35 changes: 35 additions & 0 deletions
35
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,35 @@ | ||
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(): | ||
|
||
def __init__(self, key: str, data: np.array, comment: str=None) -> None: | ||
self.key = key | ||
self.primary_hdu = fits.PrimaryHDU(header=fits.Header([('KEY', key)])) | ||
self.image_hdu = fits.ImageHDU(data=data, name='SCI') | ||
if comment: self.set_comment(comment) | ||
|
||
def __str__(self) -> str: | ||
return f"Key: {self.key}\nData:\n{self.data}" | ||
|
||
def set_comment(self, comment: str): | ||
self.primary_hdu.header.add_comment(comment) | ||
|
||
def set_sci_data(self, new_data: np.array): | ||
self.image_hdu.data = new_data | ||
|
||
def create_save_fits(self, index: int=None, large_jpg: str=None, small_jpg: str=None): | ||
hdu_list = fits.HDUList([self.primary_hdu, self.image_hdu]) | ||
fits_output_path = tempfile.NamedTemporaryFile(suffix=f'{self.key}.fits').name | ||
hdu_list.writeto(fits_output_path, overwrite=True) | ||
|
||
# allow for operations to pregenerate the jpgs, ex. RGB stacking | ||
if not large_jpg or not small_jpg: | ||
large_jpg, small_jpg = create_jpgs(self.key, fits_output_path) | ||
|
||
return save_fits_and_thumbnails(self.key, fits_output_path, large_jpg, small_jpg, index) |
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.
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