Skip to content

Commit

Permalink
Background points are now saved in fits header
Browse files Browse the repository at this point in the history
  • Loading branch information
Steffenhir committed Sep 29, 2022
1 parent b928b49 commit 7b1d41c
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 23 deletions.
44 changes: 39 additions & 5 deletions src/astroimage.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import json
import numpy as np
from xisf import XISF
from astropy.io import fits
Expand All @@ -7,6 +8,7 @@
from skimage.util import img_as_ubyte, img_as_uint
from PIL import Image, ImageEnhance
from stretch import stretch
from preferences import app_state_2_fitsheader

class AstroImage:
def __init__(self, stretch_option, saturation):
Expand All @@ -16,7 +18,7 @@ def __init__(self, stretch_option, saturation):
self.img_format = None
self.fits_header = None
self.xisf_metadata = {}
self.image_metadata = {}
self.image_metadata = {"FITSKeywords": {}}
self.stretch_option = stretch_option
self.saturation = saturation
self.width = 0
Expand All @@ -43,6 +45,8 @@ def set_from_file(self, directory):
xisf = XISF(directory)
self.xisf_metadata = xisf.get_file_metadata()
self.image_metadata = xisf.get_images_metadata()[0]
self.fits_header = fits.Header()
self.xisf_imagedata_2_fitsheader()
img_array = xisf.read_image(0)
else:
img_array = io.imread(directory)
Expand Down Expand Up @@ -142,16 +146,18 @@ def crop(self, startx, endx, starty, endy):
self.height = self.img_array.shape[0]
return

def update_fits_header(self, original_header, background_mean):
if(original_header is None):
def update_fits_header(self, original_header, background_mean, app, app_state):
if(self.fits_header is None):
self.fits_header = fits.Header()
else:
self.fits_header = original_header

self.fits_header["BG-EXTR"] = "GraXpert"
self.fits_header["CBG-1"] = background_mean
self.fits_header["CBG-2"] = background_mean
self.fits_header["CBG-3"] = background_mean
self.fits_header = app_state_2_fitsheader(app, app_state, self.fits_header)


if "ROWORDER" in self.fits_header:
self.roworder = self.fits_header["ROWORDER"]
Expand All @@ -169,6 +175,7 @@ def save(self, dir, saveas_type):
io.imsave(dir, image_converted)

elif(saveas_type == "16 bit XISF" or saveas_type == "32 bit XISF"):
self.update_xisf_imagedata()
XISF.write(dir, image_converted, creator_app = "GraXpert", image_metadata = self.image_metadata, xisf_metadata = self.xisf_metadata)
else:
if(image_converted.shape[-1] == 3):
Expand Down Expand Up @@ -214,4 +221,31 @@ def update_saturation(self):
self.img_display_saturated = ImageEnhance.Color(self.img_display)
self.img_display_saturated = self.img_display_saturated.enhance(self.saturation.get())

return
return

def update_xisf_imagedata(self):
for key in self.fits_header.keys():
if key == "BG-PTS":
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": ""}]
else:

value = str(self.fits_header[key])
comment = str(self.fits_header.comments[key])
self.image_metadata["FITSKeywords"][key] = [{"value": value, "comment":comment}]

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"]))

value = self.image_metadata["FITSKeywords"][key][0]["value"]
comment = self.image_metadata["FITSKeywords"][key][0]["comment"]

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

if len(bg_pts) > 0:
self.fits_header["BG-PTS"] = str(bg_pts)
26 changes: 9 additions & 17 deletions src/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
from localization import _
from parallel_processing import executor
from preferences import (app_state_2_prefs, load_preferences,
prefs_2_app_state, save_preferences)
prefs_2_app_state, save_preferences,
app_state_2_fitsheader, fitsheader_2_app_state)
from stretch import stretch_all
from ui_scaling import get_scaling_factor
from version import release, version
Expand Down Expand Up @@ -461,6 +462,10 @@ def menu_open_clicked(self, event=None):
self.prefs["width"] = width
self.prefs["height"] = height

tmp_state = fitsheader_2_app_state(self.cmd.app_state, self.images["Original"].fits_header)
self.cmd: Command = Command(INIT_HANDLER, background_points=tmp_state["background_points"])
self.cmd.execute()

self.zoom_fit(width, height)
self.redraw_image()
self.loading_frame.end()
Expand Down Expand Up @@ -670,8 +675,8 @@ def calculate(self):

# Update fits header and metadata
background_mean = np.mean(self.images["Background"].img_array)
self.images["Processed"].update_fits_header(self.images["Original"].fits_header, background_mean)
self.images["Background"].update_fits_header(self.images["Original"].fits_header, background_mean)#
self.images["Processed"].update_fits_header(self.images["Original"].fits_header, background_mean, self, self.cmd.app_state)
self.images["Background"].update_fits_header(self.images["Original"].fits_header, background_mean, self, self.cmd.app_state)

self.images["Processed"].copy_metadata(self.images["Original"])
self.images["Background"].copy_metadata(self.images["Original"])
Expand Down Expand Up @@ -1131,20 +1136,7 @@ def switch_display(self, event):

def on_closing(self, logging_thread):

self.prefs = app_state_2_prefs(self.prefs, self.cmd.app_state)
self.prefs["bg_pts_option"] = self.bg_pts.get()
self.prefs["stretch_option"] = self.stretch_option_current.get()
self.prefs["saturation"] = self.saturation.get()
self.prefs["bg_tol_option"] = self.bg_tol.get()
self.prefs["interpol_type_option"] = self.interpol_type.get()
self.prefs["smoothing_option"] = self.smoothing.get()
self.prefs["saveas_option"] = self.saveas_type.get()
self.prefs["sample_size"] = self.sample_size.get()
self.prefs["sample_color"] = self.sample_color.get()
self.prefs["RBF_kernel"] = self.RBF_kernel.get()
self.prefs["spline_order"] = self.spline_order.get()
self.prefs["lang"] = self.lang.get()
self.prefs["corr_type"] = self.corr_type.get()
self.prefs = app_state_2_prefs(self.prefs, self.cmd.app_state, self)
prefs_filename = os.path.join(user_config_dir(appname="GraXpert"), "preferences.json")
save_preferences(prefs_filename, self.prefs)
try:
Expand Down
31 changes: 30 additions & 1 deletion src/preferences.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,22 @@ class Prefs(TypedDict):
}


def app_state_2_prefs(prefs: Prefs, app_state: AppState) -> Prefs:
def app_state_2_prefs(prefs: Prefs, app_state: AppState, app) -> Prefs:
if "background_points" in app_state:
prefs["background_points"] = [p.tolist() for p in app_state["background_points"]]
prefs["bg_pts_option"] = app.bg_pts.get()
prefs["stretch_option"] = app.stretch_option_current.get()
prefs["saturation"] = app.saturation.get()
prefs["bg_tol_option"] = app.bg_tol.get()
prefs["interpol_type_option"] = app.interpol_type.get()
prefs["smoothing_option"] = app.smoothing.get()
prefs["saveas_option"] = app.saveas_type.get()
prefs["sample_size"] = app.sample_size.get()
prefs["sample_color"] = app.sample_color.get()
prefs["RBF_kernel"] = app.RBF_kernel.get()
prefs["spline_order"] = app.spline_order.get()
prefs["lang"] = app.lang.get()
prefs["corr_type"] = app.corr_type.get()
return prefs


Expand Down Expand Up @@ -124,3 +137,19 @@ def save_preferences(prefs_filename, prefs):
json.dump(prefs, f)
except OSError as err:
logging.exception("error serializing preferences")


def app_state_2_fitsheader(app, app_state, fits_header):
prefs = Prefs()
prefs = app_state_2_prefs(prefs, app_state, app)
fits_header["BG-PTS"] = str(prefs["background_points"])

return fits_header


def fitsheader_2_app_state(app_state, 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"])]

return app_state

0 comments on commit 7b1d41c

Please sign in to comment.