Skip to content

Commit

Permalink
✨ (Web) Minor refactor in functions (now can return img file buffer) …
Browse files Browse the repository at this point in the history
…and made web

web render route work, v basic rn
  • Loading branch information
djmango committed Sep 14, 2024
1 parent 698099a commit c926562
Show file tree
Hide file tree
Showing 6 changed files with 1,697 additions and 64 deletions.
14 changes: 10 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,29 @@ pub mod parsing_utils;
pub mod rendering;
pub mod types;

use crate::parse_and_render::parse_and_render;
use crate::parse_and_render::render_to_file;
use crate::types::StarCatalogArgs;

use pyo3::prelude::*;

#[pyfunction]
fn process_star_catalog_py(args: StarCatalogArgs) -> PyResult<()> {
parse_and_render(&args)
fn render_to_file_py(args: StarCatalogArgs) -> PyResult<()> {
render_to_file(&args)
.map_err(|e| PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(e.to_string()))
}

// #[pyfunction]
// fn read_and_render_py(args: StarCatalogArgs) -> PyResult<ImageBuffer<Rgb<u8>, Vec<u8>>> {
// parse_and_render::read_and_render(&args)
// .map_err(|e| PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(e.to_string()))
// }

/// A Python module implemented in Rust. The name of this function must match
/// the `lib.name` setting in the `Cargo.toml`, else Python will not be able to
/// import the module.
#[pymodule]
fn starfinder(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<StarCatalogArgs>()?;
m.add_function(wrap_pyfunction!(process_star_catalog_py, m)?)?;
m.add_function(wrap_pyfunction!(render_to_file_py, m)?)?;
Ok(())
}
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use anyhow::Result;
use clap::Parser;

use starfinder::parse_and_render::parse_and_render;
use starfinder::parse_and_render::render_to_file;
use starfinder::types::StarCatalogArgs;

/// CLI Arguments
Expand Down Expand Up @@ -83,5 +83,5 @@ fn main() -> Result<()> {
output: cmd_args.output,
};

parse_and_render(&args)
render_to_file(&args)
}
68 changes: 42 additions & 26 deletions src/parse_and_render.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use anyhow::Result;
use directories::BaseDirs;
use image::ImageBuffer;
use image::Rgb;
use reqwest;
use std::fs;
use std::path::PathBuf;
Expand All @@ -14,6 +16,7 @@ use crate::parsing_utils::read_stars;
use crate::rendering::render_stars;
use crate::types::{EquatorialCoords, StarCatalogArgs};

/// Download the Tycho2 catalog from the ESO archive
pub fn download_tycho2(tycho2_path: &PathBuf) -> Result<()> {
let tycho2_url = "https://archive.eso.org/ASTROM/TYC-2/data/catalog.dat";
// Create all parent directories
Expand Down Expand Up @@ -41,10 +44,23 @@ pub fn download_tycho2(tycho2_path: &PathBuf) -> Result<()> {
Ok(())
}

pub fn parse_and_render(args: &StarCatalogArgs) -> Result<()> {
let tycho2_path = BaseDirs::new().unwrap().cache_dir().join(TYCHO2_CATALOG);
/// Read stars from the optimized Tycho2 catalog, filter against the FOV, and render the starfield
pub fn read_and_render(
center_ra: f64,
center_dec: f64,
roll: f64,
fov_w: f64,
fov_h: f64,
max_magnitude: f64,
width: u32,
height: u32,
fov_max: f64,
) -> Result<ImageBuffer<Rgb<u8>, Vec<u8>>> {
let run_start = Instant::now();

let tycho2_path_optimized = BaseDirs::new().unwrap().cache_dir().join(TYCHO2_OPTIMIZED);
let tycho2_path = BaseDirs::new().unwrap().cache_dir().join(TYCHO2_CATALOG);
let fov_path = TYCHO2_OPTIMIZED.replace("FOV", fov_max.to_string().as_str());
let tycho2_path_optimized = BaseDirs::new().unwrap().cache_dir().join(fov_path);

// TODO: a hash function to check if the file that is in the cache is the same as the one that we expect

Expand All @@ -57,16 +73,9 @@ pub fn parse_and_render(args: &StarCatalogArgs) -> Result<()> {
Some(17),
24,
25,
args.fov_max,
fov_max,
)?;

let run_start = Instant::now();
let center_ra = args.center_ra.to_radians();
let center_dec = args.center_dec.to_radians();
let roll = args.roll.to_radians();
let fov_w = args.fov_w.to_radians();
let fov_h = args.fov_h.to_radians();

// 1) Rotate FOV by specified roll
let get_fov_start = Instant::now();
let center = EquatorialCoords {
Expand All @@ -78,37 +87,44 @@ pub fn parse_and_render(args: &StarCatalogArgs) -> Result<()> {

// 2) Read stars and filter against rolled_fov to create subset of stars in view of the image
let read_stars_start = Instant::now();
let stars_in_fov = read_stars(&tycho2_path_optimized, rolled_fov, args.max_magnitude)?;
let stars_in_fov = read_stars(&tycho2_path_optimized, rolled_fov, max_magnitude)?;
info!(
"Total time to read and parse stars: {:?}",
read_stars_start.elapsed()
);

// 3) Render stars in FOV
let render_stars_start = Instant::now();
let img = render_stars(
stars_in_fov,
let img = render_stars(stars_in_fov, width, height, center, fov_w, fov_h, roll);

info!(
"Total parse and write stars: {:?}",
render_stars_start.elapsed()
);
info!("Total run time elapsed: {:?}", run_start.elapsed());
Ok(img)
}

/// Run the full pipeline to render a starfield to a file
pub fn render_to_file(args: &StarCatalogArgs) -> Result<()> {
let img = read_and_render(
args.center_ra,
args.center_dec,
args.roll,
args.fov_w,
args.fov_h,
args.max_magnitude,
args.width,
args.height,
center,
fov_w,
fov_h,
roll,
);
args.fov_max,
)?;

// Create all parent directories
if let Some(parent) = PathBuf::from(&args.output).parent() {
fs::create_dir_all(parent)?;
}
img.save(&args.output)?;

info!(
"Total parse and write stars: {:?}",
render_stars_start.elapsed()
);

info!("Total run time elapsed: {:?}", run_start.elapsed());
info!("Starfield render complete! File output at: {}", args.output);

Ok(())
}
Loading

0 comments on commit c926562

Please sign in to comment.