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 09456f48..7b066976 100644 --- a/sispo/sim/starcat.py +++ b/sispo/sim/starcat.py @@ -7,6 +7,10 @@ import sys from pathlib import Path +import astropy.coordinates as coord +import astropy.units as u +from astroquery.vizier import Vizier + class StarCatalogError(RuntimeError): """Generic error for star catalog module.""" @@ -18,14 +22,13 @@ class StarCatalog: def __init__(self, res_dir, ext_logger, starcat_dir=None): """.""" - 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: @@ -33,41 +36,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.") - - # 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) + return self.get_stardata_ucac4(ra, dec, width, height, filename) - 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") @@ -108,67 +114,22 @@ 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)) + 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) -# 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 -# + return star_res 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)