Skip to content

Commit

Permalink
apply two fixes to the handling of background points
Browse files Browse the repository at this point in the history
- do not store redundant BG_PTS information in .fits headers
- do not fail when stored background points cannot be loaded from images
  • Loading branch information
schmelly committed Jan 2, 2024
1 parent 8684dc0 commit 77a1e75
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 17 deletions.
40 changes: 24 additions & 16 deletions graxpert/astroimage.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import logging
import os

import numpy as np
Expand Down Expand Up @@ -271,10 +272,13 @@ def update_xisf_imagedata(self):

for key in unique_keys:
if key == "BG-PTS":
bg_pts = json.loads(self.fits_header["BG-PTS"])
try:
bg_pts = json.loads(self.fits_header["BG-PTS"])

for i in range(len(bg_pts)):
self.image_metadata["FITSKeywords"]["BG-PTS" + str(i)] = [{"value": bg_pts[i], "comment": ""}]
for i in range(len(bg_pts)):
self.image_metadata["FITSKeywords"]["BG-PTS" + str(i)] = [{"value": bg_pts[i], "comment": ""}]
except:
logging.warning("Could not transfer background points from fits header to xisf image metadata", stack_info=True)
else:
value = str(self.fits_header[key]).splitlines()
comment = str(self.fits_header.comments[key]).splitlines()
Expand Down Expand Up @@ -303,23 +307,27 @@ def xisf_imagedata_2_fitsheader(self):
bg_pts = []
for key in self.image_metadata["FITSKeywords"].keys():
if key.startswith("BG-PTS"):
bg_pts.append(json.loads(self.image_metadata["FITSKeywords"][key][0]["value"]))
try:
bg_pts.append(json.loads(self.image_metadata["FITSKeywords"][key][0]["value"]))
except:
logging.warning(f"Could not load background points from xisf image metadata. Affected entry: {self.image_metadata['FITSKeywords'][key]}", stack_info=True)

for i in range(len(self.image_metadata["FITSKeywords"][key])):
value = self.image_metadata["FITSKeywords"][key][i]["value"]
comment = self.image_metadata["FITSKeywords"][key][i]["comment"]
else:
for i in range(len(self.image_metadata["FITSKeywords"][key])):
value = self.image_metadata["FITSKeywords"][key][i]["value"]
comment = self.image_metadata["FITSKeywords"][key][i]["comment"]

# Commentary cards have to comments in Fits standard
if key in commentary_keys:
if value == "":
value = comment
# Commentary cards have to comments in Fits standard
if key in commentary_keys:
if value == "":
value = comment

if value.isdigit():
value = int(value)
elif value.isdecimal():
value = float(value)
if value.isdigit():
value = int(value)
elif value.isdecimal():
value = float(value)

self.fits_header[key] = (value, comment)
self.fits_header[key] = (value, comment)

if len(bg_pts) > 0:
self.fits_header["BG-PTS"] = str(bg_pts)
5 changes: 4 additions & 1 deletion graxpert/preferences.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,10 @@ def app_state_2_fitsheader(prefs: Prefs, app_state: AppState, fits_header):

def fitsheader_2_app_state(prefs: Prefs, app_state: AppState, fits_header):
if "BG-PTS" in fits_header.keys():
app_state.background_points = [np.array(p) for p in json.loads(fits_header["BG-PTS"])]
try:
app_state.background_points = [np.array(p) for p in json.loads(fits_header["BG-PTS"])]
except:
logging.warning("Could not transfer background points from fits header to application state", stack_info=True)

if "INTP-OPT" in fits_header.keys():
prefs.interpol_type_option = fits_header["INTP-OPT"]
Expand Down
1 change: 1 addition & 0 deletions graxpert/ui/canvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def __init__(self, master, **kwargs):
self.endx = 0
self.endy = 0
self.crop_mode = False
self.clicked_inside_pt = False

self.create_children()
self.setup_layout()
Expand Down

0 comments on commit 77a1e75

Please sign in to comment.