Skip to content

Commit

Permalink
get_tif analysis action
Browse files Browse the repository at this point in the history
  • Loading branch information
LTDakin committed Aug 15, 2024
1 parent 5de3961 commit e9e456c
Show file tree
Hide file tree
Showing 4 changed files with 516 additions and 481 deletions.
24 changes: 24 additions & 0 deletions datalab/datalab_session/analysis/get_tif.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from datalab.datalab_session.util import create_tif, get_fits, add_file_to_bucket, key_exists, get_s3_url

def get_tif(input: dict):
"""
Checks bucket for tif file and returns the url
if the file doesn't exist, generates a new tif file
input: dict
basename: str
source: str
"""

basename = input["basename"]
file_key = f'{basename}/{basename}.tif'

# Check in bucket for tif file
if(key_exists(file_key)):
tif_url = get_s3_url(file_key)
else:
# If tif file doesn't exist, generate a new tif file
fits_path = get_fits(basename)
tif_path = create_tif(basename, fits_path)
tif_url = add_file_to_bucket(file_key, tif_path)

return {"tif_url": tif_url}
12 changes: 11 additions & 1 deletion datalab/datalab_session/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from botocore.exceptions import ClientError

from django.conf import settings
from fits2image.conversions import fits_to_jpg
from fits2image.conversions import fits_to_jpg, fits_to_tif

log = logging.getLogger()
log.setLevel(logging.INFO)
Expand Down Expand Up @@ -176,6 +176,16 @@ def create_fits(key: str, image_arr: np.ndarray) -> str:

return fits_path

def create_tif(key: str, fits_path: np.ndarray) -> str:
"""
Creates a full sized TIFF file from a FITs
"""
height, width = get_fits_dimensions(fits_path)
tif_path = tempfile.NamedTemporaryFile(suffix=f'{key}.tif').name
fits_to_tif(fits_path, tif_path, width=width, height=height)

return tif_path

def create_jpgs(cache_key, fits_paths: str, color=False) -> list:
"""
Create jpgs from fits files and save them to S3
Expand Down
4 changes: 3 additions & 1 deletion datalab/datalab_session/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from datalab.datalab_session.data_operations.utils import available_operations
from datalab.datalab_session.analysis.line_profile import line_profile
from datalab.datalab_session.analysis.source_catalog import source_catalog

from datalab.datalab_session.analysis.get_tif import get_tif

class OperationOptionsApiView(RetrieveAPIView):
""" View to retrieve the set of operations available, for the UI to use """
Expand All @@ -31,6 +31,8 @@ def post(self, request, action):
output = line_profile(input)
case 'source-catalog':
output = source_catalog(input)
case 'get-tif':
output = get_tif(input)
case _:
raise Exception(f'Analysis action {action} not found')

Expand Down
Loading

0 comments on commit e9e456c

Please sign in to comment.