From 85f1ae2b686d7165964eb718c048e99d298ee742 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20J=C3=B6rg=20Schwarzkopf?= Date: Fri, 29 Jan 2021 17:15:40 +0100 Subject: [PATCH 1/8] Introduce astroquery and vizier --- sispo/sim/starcat.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/sispo/sim/starcat.py b/sispo/sim/starcat.py index 09456f48..bfadf713 100644 --- a/sispo/sim/starcat.py +++ b/sispo/sim/starcat.py @@ -7,6 +7,9 @@ import sys from pathlib import Path +from astroquery.vizier import Vizier +import astropy.coordinates as coord +import astropy.units as u class StarCatalogError(RuntimeError): """Generic error for star catalog module.""" @@ -19,6 +22,9 @@ class StarCatalog: def __init__(self, res_dir, ext_logger, starcat_dir=None): """.""" + self.catalog = Vizier(catalog="UCAC4", row_limit=1000000) + return + self.logger = ext_logger self.root_dir = Path(__file__).parent.parent.parent @@ -108,6 +114,20 @@ def get_stardata(self, ra, dec, width, height, filename="ucac4.txt"): return star_data + def get_stardata_vizier(self, ra, dec, width, height): + """ + Retrieves star data from Vizier + """ + + crds = coord.SkyCoord( + ra=ra, dec=dec, unit=(u.deg, u.deg), frame='icrs') + result = self.catalog.query_region( + crds, width=width*u.deg, height=height*u.deg)[0] + + star_data = zip(result['RAJ2000'], result['DEJ2000'], result['f.mag']) + + return [(ra, de, mag) for ra, de, mag in star_data] + # class StarCache: # """Handling stars in field of view, for rendering of scene.""" From 74e027d71f5ed17f99ce533844f8db7e175cf110 Mon Sep 17 00:00:00 2001 From: YgabrielsY <33376611+YgabrielsY@users.noreply.github.com> Date: Fri, 29 Jan 2021 17:32:44 +0100 Subject: [PATCH 2/8] Fix utils in compositor --- sispo/sim/compositor.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/sispo/sim/compositor.py b/sispo/sim/compositor.py index f942eca4..7c6b0e40 100644 --- a/sispo/sim/compositor.py +++ b/sispo/sim/compositor.py @@ -16,7 +16,7 @@ from astropy import constants as const from astropy import units as u -from . import utils +from . import utilities #Astrometric calibrations #https://www.cfa.harvard.edu/~dfabricant/huchra/ay145/mags.html @@ -114,16 +114,16 @@ def read_complete_frame(self, frame_id, image_dir): self.metadata = self.read_meta_file(frame_id, image_dir) filename = frame_fmt_str.format("Stars") - self.stars = utils.read_openexr_image(filename) + self.stars = utilities.read_openexr_image(filename) filename = frame_fmt_str.format("SssbOnly") - self.sssb_only = utils.read_openexr_image(filename) + self.sssb_only = utilities.read_openexr_image(filename) filename = frame_fmt_str.format("SssbConstDist") - self.sssb_const_dist = utils.read_openexr_image(filename) + self.sssb_const_dist = utilities.read_openexr_image(filename) filename = frame_fmt_str.format("LightRef") - self.light_ref = utils.read_openexr_image(filename) + self.light_ref = utilities.read_openexr_image(filename) def read_meta_file(self, frame_id, image_dir): """Reads metafile of a frame.""" From 34a942adffcfa4060f1a406a47260182c2a5d311 Mon Sep 17 00:00:00 2001 From: YgabrielsY <33376611+YgabrielsY@users.noreply.github.com> Date: Thu, 18 Feb 2021 20:43:19 +0100 Subject: [PATCH 3/8] Remove return --- sispo/sim/starcat.py | 1 - 1 file changed, 1 deletion(-) diff --git a/sispo/sim/starcat.py b/sispo/sim/starcat.py index bfadf713..f221cd0f 100644 --- a/sispo/sim/starcat.py +++ b/sispo/sim/starcat.py @@ -23,7 +23,6 @@ def __init__(self, res_dir, ext_logger, starcat_dir=None): """.""" self.catalog = Vizier(catalog="UCAC4", row_limit=1000000) - return self.logger = ext_logger From 4ce4b7a5c7de25bb8a52e492e3425defe188b3ba Mon Sep 17 00:00:00 2001 From: YgabrielsY <33376611+YgabrielsY@users.noreply.github.com> Date: Fri, 19 Feb 2021 23:19:09 +0100 Subject: [PATCH 4/8] Adaptive both starcat versions, default vizier --- sispo/sim/starcat.py | 84 ++++++++++++++++++++++++-------------------- 1 file changed, 45 insertions(+), 39 deletions(-) diff --git a/sispo/sim/starcat.py b/sispo/sim/starcat.py index f221cd0f..5b433165 100644 --- a/sispo/sim/starcat.py +++ b/sispo/sim/starcat.py @@ -21,16 +21,13 @@ class StarCatalog: def __init__(self, res_dir, ext_logger, starcat_dir=None): """.""" - - self.catalog = Vizier(catalog="UCAC4", row_limit=1000000) - self.logger = ext_logger - self.root_dir = Path(__file__).parent.parent.parent - if starcat_dir is None: - self.starcat_dir = self.root_dir / "data" / "UCAC4" + self.catalog = Vizier(catalog="UCAC4", row_limit=1000000) else: + self.root_dir = Path(__file__).parent.parent.parent + starcat_dir = Path(starcat_dir) try: @@ -38,41 +35,44 @@ def __init__(self, res_dir, ext_logger, starcat_dir=None): except OSError as e: raise StarCatalogError(e) - if not starcat_dir.is_dir(): - starcat_dir = self.models_dir / starcat_dir.name - starcat_dir = starcat_dir.resolve() - if not starcat_dir.is_dir(): raise StarCatalogError("Given star cat dir does not exist.") - self.starcat_dir = starcat_dir - - self.res_dir = res_dir - - exe_dir = self.root_dir / "software" / "star_cats" - - if (exe_dir / "u4test").is_file() or (exe_dir / "u4test.exe").is_file(): - self.exe = exe_dir / "u4test" - elif ((exe_dir / "star_cats" / "u4test").is_file() or - (exe_dir / "star_cats" / "u4test.exe").is_file()): - self.exe = exe_dir / "star_cats" / "u4test" - elif ((exe_dir / "build_star_cats" / "u4test").is_file() or - (exe_dir / "build_star_cats" / "u4test.exe").is_file()): - self.exe = exe_dir / "build_star_cats" / "u4test" + self.starcat_dir = starcat_dir + + self.res_dir = res_dir + + exe_dir = self.root_dir / "software" / "star_cats" + + if (exe_dir / "u4test").is_file() or (exe_dir / "u4test.exe").is_file(): + self.exe = exe_dir / "u4test" + elif ((exe_dir / "star_cats" / "u4test").is_file() or + (exe_dir / "star_cats" / "u4test.exe").is_file()): + self.exe = exe_dir / "star_cats" / "u4test" + elif ((exe_dir / "build_star_cats" / "u4test").is_file() or + (exe_dir / "build_star_cats" / "u4test.exe").is_file()): + self.exe = exe_dir / "build_star_cats" / "u4test" + else: + raise StarCatalogError("UCAC4 interface could not be found.") + + # Don't display the Windows GPF dialog if the invoked program dies. + # See comp.os.ms-windows.programmer.win32 + # How to suppress crash notification dialog?, Jan 14,2004 - + # Raymond Chen"s response [1] + if sys.platform.startswith("win"): + self.logger.debug("Windows system, surrpressing GPF dialog.") + import ctypes + + SEM_NOGPFAULTERRORBOX = 0x0002 # From MSDN + ctypes.windll.kernel32.SetErrorMode(SEM_NOGPFAULTERRORBOX) + + def get_stardata(self, ra, dec, width, height, filename="ucac4.txt"): + """.""" + if hasattr(self, "catalog"): + return self.get_stardata_vizier(ra, dec, width, height) else: - raise StarCatalogError("UCAC4 interface could not be found.") + return self.get_stardata_ucac4(ra, dec, width, height, filename) - # Don't display the Windows GPF dialog if the invoked program dies. - # See comp.os.ms-windows.programmer.win32 - # How to suppress crash notification dialog?, Jan 14,2004 - - # Raymond Chen"s response [1] - if sys.platform.startswith("win"): - self.logger.debug("Windows system, surrpressing GPF dialog.") - import ctypes - - SEM_NOGPFAULTERRORBOX = 0x0002 # From MSDN - ctypes.windll.kernel32.SetErrorMode(SEM_NOGPFAULTERRORBOX) - - def get_stardata(self, ra, dec, width, height, filename="ucac4.txt"): + def get_stardata_ucac4(self, ra, dec, width, height, filename="ucac4.txt"): """Retrieve star data from given field of view using UCAC4 catalog.""" res_file = self.res_dir / filename res_file = res_file.with_suffix(".txt") @@ -119,13 +119,19 @@ def get_stardata_vizier(self, ra, dec, width, height): """ crds = coord.SkyCoord( - ra=ra, dec=dec, unit=(u.deg, u.deg), frame='icrs') + ra=ra, dec=dec, unit=(u.deg, u.deg)) result = self.catalog.query_region( crds, width=width*u.deg, height=height*u.deg)[0] star_data = zip(result['RAJ2000'], result['DEJ2000'], result['f.mag']) + star_data = [(ra, de, mag) for ra, de, mag in star_data] + + star_res = [] + for star in star_data: + if star[0] >= ra - width/2 and star[0] <= ra + width/2 and star[1] >= dec - height/2 and star[1] <= dec + height/2: + star_res.append(star) - return [(ra, de, mag) for ra, de, mag in star_data] + return star_res # class StarCache: From c7668207b0baa472e19c4d4269d576d686c5b6f8 Mon Sep 17 00:00:00 2001 From: YgabrielsY <33376611+YgabrielsY@users.noreply.github.com> Date: Wed, 3 Mar 2021 22:22:37 +0100 Subject: [PATCH 5/8] Add "None" option to paths --- sispo/sispo.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/sispo/sispo.py b/sispo/sispo.py index 6f8dcb71..1374fd4e 100644 --- a/sispo/sispo.py +++ b/sispo/sispo.py @@ -190,7 +190,9 @@ def _parse_paths(settings): """ for key in settings.keys(): if "dir" in key: - if "res" in key: + if "None" in settings[key]: + path = None + elif "res" in key: path = utils.check_dir(settings[key]) else: path = utils.check_dir(settings[key], False) From 718b7faab45544b1d9cb44170a9ee29db0ea377f Mon Sep 17 00:00:00 2001 From: YgabrielsY <33376611+YgabrielsY@users.noreply.github.com> Date: Wed, 3 Mar 2021 22:26:34 +0100 Subject: [PATCH 6/8] Fix import names --- sispo/sim/compositor.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/sispo/sim/compositor.py b/sispo/sim/compositor.py index 7c6b0e40..f942eca4 100644 --- a/sispo/sim/compositor.py +++ b/sispo/sim/compositor.py @@ -16,7 +16,7 @@ from astropy import constants as const from astropy import units as u -from . import utilities +from . import utils #Astrometric calibrations #https://www.cfa.harvard.edu/~dfabricant/huchra/ay145/mags.html @@ -114,16 +114,16 @@ def read_complete_frame(self, frame_id, image_dir): self.metadata = self.read_meta_file(frame_id, image_dir) filename = frame_fmt_str.format("Stars") - self.stars = utilities.read_openexr_image(filename) + self.stars = utils.read_openexr_image(filename) filename = frame_fmt_str.format("SssbOnly") - self.sssb_only = utilities.read_openexr_image(filename) + self.sssb_only = utils.read_openexr_image(filename) filename = frame_fmt_str.format("SssbConstDist") - self.sssb_const_dist = utilities.read_openexr_image(filename) + self.sssb_const_dist = utils.read_openexr_image(filename) filename = frame_fmt_str.format("LightRef") - self.light_ref = utilities.read_openexr_image(filename) + self.light_ref = utils.read_openexr_image(filename) def read_meta_file(self, frame_id, image_dir): """Reads metafile of a frame.""" From 3d791af0cc3b65df525b2935c441c65550e16185 Mon Sep 17 00:00:00 2001 From: YgabrielsY <33376611+YgabrielsY@users.noreply.github.com> Date: Wed, 3 Mar 2021 22:31:45 +0100 Subject: [PATCH 7/8] Fix import order --- sispo/sim/compositor.py | 2 +- sispo/sim/render.py | 6 +++--- sispo/sim/starcat.py | 3 ++- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/sispo/sim/compositor.py b/sispo/sim/compositor.py index f942eca4..1dcf2aa6 100644 --- a/sispo/sim/compositor.py +++ b/sispo/sim/compositor.py @@ -8,8 +8,8 @@ import json import threading -from pathlib import Path from datetime import datetime +from pathlib import Path import cv2 import numpy as np diff --git a/sispo/sim/render.py b/sispo/sim/render.py index 0ba3e3be..143d5302 100644 --- a/sispo/sim/render.py +++ b/sispo/sim/render.py @@ -4,11 +4,11 @@ This implementation uses the blender python module bpy. """ -import math import json +import math import struct -import time import threading +import time import zlib from pathlib import Path @@ -16,7 +16,7 @@ import cv2 import numpy as np from astropy import units as u -from mathutils import Vector, Quaternion # pylint: disable=import-error +from mathutils import Quaternion, Vector # pylint: disable=import-error from . import compositor as cp from . import starcat, utils diff --git a/sispo/sim/starcat.py b/sispo/sim/starcat.py index 5b433165..83c0ae63 100644 --- a/sispo/sim/starcat.py +++ b/sispo/sim/starcat.py @@ -7,9 +7,10 @@ import sys from pathlib import Path -from astroquery.vizier import Vizier import astropy.coordinates as coord import astropy.units as u +from astroquery.vizier import Vizier + class StarCatalogError(RuntimeError): """Generic error for star catalog module.""" From a0d25e954e281874415f3fbb2b77337a790dfc2f Mon Sep 17 00:00:00 2001 From: YgabrielsY <33376611+YgabrielsY@users.noreply.github.com> Date: Wed, 3 Mar 2021 22:48:13 +0100 Subject: [PATCH 8/8] Remove useless old code --- sispo/sim/starcat.py | 65 -------------------------------------------- 1 file changed, 65 deletions(-) diff --git a/sispo/sim/starcat.py b/sispo/sim/starcat.py index 83c0ae63..7b066976 100644 --- a/sispo/sim/starcat.py +++ b/sispo/sim/starcat.py @@ -133,68 +133,3 @@ def get_stardata_vizier(self, ra, dec, width, height): star_res.append(star) return star_res - - -# class StarCache: -# """Handling stars in field of view, for rendering of scene.""" -# -# def __init__(self, template=None, parent=None): -# """Initialise StarCache.""" -# self.template = template -# self.star_array = [] -# self.parent = parent -# -# def set_stars(self, stardata, star_template, cam_direction, sat_pos_rel, R, pixelsize_at_R, scene_names): -# """Set current stars in the field of view.""" -# if len(self.star_array) < len(stardata): -# for _ in range(0, len(stardata) - len(self.star_array)): -# new_obj = self.template.copy() -# new_obj.data = self.template.data.copy() -# new_obj.animation_data_clear() -# new_mat = star_template.material_slots[0].material.copy() # TODO: check for star_template use, changed to input for now -# new_obj.material_slots[0].material = new_mat -# self.star_array.append(new_obj) -# if self.parent is not None: -# new_obj.parent = self.parent -# total_flux = 0. -# -# for i in range(0, len(stardata)): -# star = self.star_array[i] -# star_data = copy.copy(stardata[i]) -# star_data[0] = math.radians(star_data[0]) -# star_data[1] = math.radians(star_data[1]) -# -# z_star = math.sin(star_data[1]) -# x_star = math.cos(star_data[1]) * math.cos(star_data[0] - math.pi) -# y_star = -math.cos(star_data[1]) * math.sin(star_data[0] - math.pi) -# vec = [x_star, y_star, z_star] -# vec2 = [x_star, -y_star, z_star] -# if np.dot(vec, cam_direction) < np.dot(vec2, cam_direction): -# vec = vec2 -# -# pixel_factor = 10 -# # Always keep satellite in center to emulate large distances -# star.location = np.asarray(vec) * R + sat_pos_rel -# star.scale = (pixelsize_at_R / pixel_factor, pixelsize_at_R / pixel_factor, -# pixelsize_at_R / pixel_factor) -# -# flux = math.pow(10, -0.4 * (star_data[2] - 10.)) -# flux0 = math.pow(10, -0.4 * (star_data[2])) -# total_flux += flux0 -# -# star.material_slots[0].material.node_tree.nodes.get("Emission").inputs[1].default_value = flux * pixel_factor * pixel_factor -# -# for scene_name in scene_names: -# scene = bpy.data.scenes[scene_name] -# if star.name not in scene.objects: -# scene.objects.link(star) -# print("{} stars set, buffer len {}".format(i, len(self.star_array))) -# if len(self.star_array) > len(stardata): -# for scene_name in scene_names: -# scene = bpy.data.scenes[scene_name] -# for i in range(len(stardata), len(self.star_array)): -# if self.star_array[i].name in scene.objects: -# scene.objects.unlink(self.star_array[i]) -# -# return total_flux -#