From 98de368b162303b0671b4bf40f8fd1ee1cb0de16 Mon Sep 17 00:00:00 2001 From: Layton Miller Date: Tue, 10 Sep 2024 15:37:22 -0700 Subject: [PATCH] Rebase master --- src/bin/main.rs | 9 ++-- src/fov.rs | 2 +- src/lib.rs | 65 ++---------------------- src/parsing_utils.rs | 1 - src/render.rs | 41 ---------------- src/rendering.rs | 2 +- src/star_catalog.rs | 114 ------------------------------------------- src/types.rs | 26 ---------- 8 files changed, 9 insertions(+), 251 deletions(-) delete mode 100644 src/render.rs delete mode 100644 src/star_catalog.rs delete mode 100644 src/types.rs diff --git a/src/bin/main.rs b/src/bin/main.rs index 9f320c9..85082b5 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -1,9 +1,8 @@ -// #![allow(warnings)] use clap::Parser; -use starfield_renderer::coords::{EquatorialCoords}; -use starfield_renderer::fov::{get_fov}; -use starfield_renderer::parsing_utils::{read_stars}; -use starfield_renderer::rendering::{render_stars}; +use starfinder::coords::{EquatorialCoords}; +use starfinder::fov::{get_fov}; +use starfinder::parsing_utils::{read_stars}; +use starfinder::rendering::{render_stars}; use std::path::PathBuf; use std::time::Instant; diff --git a/src/fov.rs b/src/fov.rs index f378a23..f05337e 100644 --- a/src/fov.rs +++ b/src/fov.rs @@ -89,7 +89,7 @@ pub fn get_fov( c_cartesian.z.powi(2) * (1.0 - roll.cos()) + roll.cos(), ); - let transform = z_roll * y_roll * x_roll; + let transform = x_roll * y_roll * z_roll; let mut final_grid: HashSet = HashSet::new(); for p in scatter_shot { let vec: Vector3 = Vector3::new(p.x, p.y, p.z); diff --git a/src/lib.rs b/src/lib.rs index 2cddd12..def4ab3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,20 +1,12 @@ +use clap::Parser; +use pyo3::prelude::*; + pub mod coords; pub mod fov; pub mod parsing_utils; pub mod rendering; pub mod star; -pub mod render; -pub mod star_catalog; -pub mod types; - -use clap::Parser; -use pyo3::prelude::*; -use std::time::Instant; - -use crate::render::render_stars; -use crate::star_catalog::read_stars; - #[pyclass] #[derive(Parser, Debug, Clone)] pub struct StarCatalogArgs { @@ -70,57 +62,6 @@ impl StarCatalogArgs { } } -pub fn process_star_catalog(args: StarCatalogArgs) -> Result<(), Box> { - println!("Reading stars from: {}", args.file); - println!("RA range: {} to {}", args.min_ra, args.max_ra); - println!("Dec range: {} to {}", args.min_dec, args.max_dec); - println!("Max magnitude: {}", args.max_magnitude); - - let start = Instant::now(); - let stars = read_stars( - &args.file, - args.min_ra, - args.max_ra, - args.min_dec, - args.max_dec, - args.max_magnitude, - )?; - let read_duration = start.elapsed(); - - println!("Time taken to read and filter stars: {:?}", read_duration); - println!("Total stars after filtering: {}", stars.len()); - - println!("\nFirst {} stars:", args.display_count); - for (i, star) in stars.iter().enumerate() { - if i >= args.display_count && args.display_count != 0 { - break; - } - println!( - "Star {}: RA={:.2}, Dec={:.2}, Mag={:.2}", - i, star.ra_deg, star.de_deg, star.mag - ); - } - - let render_start = Instant::now(); - let img = render_stars( - &stars, - args.width, - args.height, - args.min_ra, - args.max_ra, - args.min_dec, - args.max_dec, - ); - img.save(&args.output)?; - let render_duration = render_start.elapsed(); - - println!("Time taken to render and save image: {:?}", render_duration); - println!("Image saved as: {}", args.output); - println!("Total time elapsed: {:?}", start.elapsed()); - - Ok(()) -} - #[pyfunction] fn process_star_catalog_py(args: StarCatalogArgs) -> PyResult<()> { process_star_catalog(args) diff --git a/src/parsing_utils.rs b/src/parsing_utils.rs index 2f9bdea..d136841 100644 --- a/src/parsing_utils.rs +++ b/src/parsing_utils.rs @@ -22,7 +22,6 @@ pub enum CatalogError { MissingMagnitude, } - /// Reads and filters stars from the Tycho-2 catalog file. /// https://heasarc.gsfc.nasa.gov/w3browse/all/tycho2.html /// http://tdc-www.harvard.edu/catalogs/tycho2.format.html diff --git a/src/render.rs b/src/render.rs deleted file mode 100644 index 5f8707b..0000000 --- a/src/render.rs +++ /dev/null @@ -1,41 +0,0 @@ -use crate::types::Star; -use image::{ImageBuffer, Rgb}; - -pub fn render_stars( - stars: &[Star], - width: u32, - height: u32, - min_ra: f64, - max_ra: f64, - min_dec: f64, - max_dec: f64, -) -> ImageBuffer, Vec> { - let mut img = ImageBuffer::new(width, height); - - // Find the minimum and maximum magnitudes in the dataset - let min_mag = stars.iter().map(|s| s.mag).fold(f64::INFINITY, f64::min); - let max_mag = stars - .iter() - .map(|s| s.mag) - .fold(f64::NEG_INFINITY, f64::max); - - println!("Magnitude range: {:.3} to {:.3}", min_mag, max_mag); - - for star in stars { - let x = ((star.ra_deg - min_ra) / (max_ra - min_ra) * width as f64) as u32; - let y = ((star.de_deg - min_dec) / (max_dec - min_dec) * height as f64) as u32; - - if x < width && y < height { - // Inverse the magnitude scale (brighter stars have lower magnitudes) - let normalized_mag = (max_mag - star.mag) / (max_mag - min_mag); - - // Apply a non-linear scaling to emphasize brighter stars - let brightness = (normalized_mag.powf(2.5) * 255.0) as u8; - - let color = Rgb([brightness, brightness, brightness]); - img.put_pixel(x, y, color); - } - } - - img -} diff --git a/src/rendering.rs b/src/rendering.rs index 3d1e334..60b9c70 100644 --- a/src/rendering.rs +++ b/src/rendering.rs @@ -49,7 +49,7 @@ pub fn render_stars( // Inverse the magnitude scale (brighter stars have lower magnitudes) let normalized_mag = (max_mag - star.mag) / (max_mag - min_mag); // Apply a non-linear scaling to emphasize brighter stars - let brightness = (normalized_mag.powf(2.5) * 255.0) as u8; + let brightness = (normalized_mag.powf(2.0) * 255.0) as u8; let color = Rgb([brightness, brightness, brightness]); img.put_pixel(x as u32, y as u32, color); diff --git a/src/star_catalog.rs b/src/star_catalog.rs deleted file mode 100644 index 3d2638b..0000000 --- a/src/star_catalog.rs +++ /dev/null @@ -1,114 +0,0 @@ -use csv::ReaderBuilder; -use std::fs::File; -use std::io::{self}; - -use crate::types::{CatalogError, Star}; - -/// Reads and filters stars from the Tycho-2 catalog file. -/// https://heasarc.gsfc.nasa.gov/w3browse/all/tycho2.html -/// http://tdc-www.harvard.edu/catalogs/tycho2.format.html -pub fn read_stars>( - path: P, - min_ra: f64, - max_ra: f64, - min_dec: f64, - max_dec: f64, - max_magnitude: f64, -) -> Result, CatalogError> { - let file = File::open(path)?; - let reader = io::BufReader::new(file); - let mut csv_reader = ReaderBuilder::new() - .delimiter(b'|') - .has_headers(false) - .from_reader(reader); - - let mut stars = Vec::new(); - let mut skipped_rows = 0; - - for (i, result) in csv_reader.records().enumerate() { - let record = result?; - match parse_star_record(&record) { - Ok(star) => { - if star.ra_deg >= min_ra - && star.ra_deg <= max_ra - && star.de_deg >= min_dec - && star.de_deg <= max_dec - && star.mag <= max_magnitude - { - if i % 10000 == 0 { - println!( - "Star {}: RA={}, Dec={}, Mag={}", - i, star.ra_deg, star.de_deg, star.mag - ); - } - stars.push(star); - } - } - Err(e) => { - skipped_rows += 1; - if skipped_rows <= 10 { - println!("Skipping row {} due to error: {:?}", i, e); - println!("Problematic row: {:?}", record); - } else if skipped_rows == 11 { - println!("Further skipped rows will not be printed..."); - } - } - } - } - - println!("Total stars read and filtered: {}", stars.len()); - println!("Total rows skipped: {}", skipped_rows); - - Ok(stars) -} - -/// Parses a single record from the catalog into a Star struct. -fn parse_star_record(record: &csv::StringRecord) -> Result { - let ra = parse_field(record, 24, "RA")?; - let dec = parse_field(record, 25, "Dec")?; - let mag = parse_magnitude(record)?; - - Ok(Star { - ra_deg: ra, - de_deg: dec, - mag, - }) -} - -/// Parses a field from the record, returning a helpful error if parsing fails. -fn parse_field( - record: &csv::StringRecord, - index: usize, - field_name: &str, -) -> Result { - record - .get(index) - .ok_or_else(|| CatalogError::MissingField(field_name.to_string()))? - .trim() - .parse() - .map_err(|_| CatalogError::Parse(format!("Failed to parse {}", field_name))) -} - -fn parse_magnitude(record: &csv::StringRecord) -> Result { - let bt_mag = parse_field(record, 17, "BT magnitude").ok(); - let vt_mag = parse_field(record, 19, "VT magnitude").ok(); - - // println!("Debug: BT_Mag = {:?}, VT_Mag = {:?}", bt_mag, vt_mag); - - match (bt_mag, vt_mag) { - (Some(bt), Some(vt)) => { - let v_mag = vt - 0.090 * (bt - vt); - // println!("Debug: Calculated V_Mag = {:.3}", v_mag); - Ok(v_mag) - } - (None, Some(vt)) => { - // println!("Debug: Using VT_Mag as V_Mag = {:.3}", vt); - Ok(vt) - } - (Some(bt), None) => { - // println!("Debug: Using BT_Mag as V_Mag = {:.3}", bt); - Ok(bt) - } - (None, None) => Err(CatalogError::MissingMagnitude), - } -} diff --git a/src/types.rs b/src/types.rs deleted file mode 100644 index edbabc0..0000000 --- a/src/types.rs +++ /dev/null @@ -1,26 +0,0 @@ -use serde::{Deserialize, Serialize}; -use std::io; -use thiserror::Error; - -/// Represents a star with its right ascension, declination, and magnitude. -#[derive(Debug, Deserialize, Serialize, Clone)] -pub struct Star { - pub ra_deg: f64, - pub de_deg: f64, - pub mag: f64, -} - -/// Errors that can occur during star catalog reading. -#[derive(Error, Debug)] -pub enum CatalogError { - #[error("IO error: {0}")] - Io(#[from] io::Error), - #[error("CSV parsing error: {0}")] - Csv(#[from] csv::Error), - #[error("Missing field: {0}")] - MissingField(String), - #[error("Parse error: {0}")] - Parse(String), - #[error("Missing magnitude")] - MissingMagnitude, -}