star_catalog/
lib.rs

1//!
2//! # Star catalog library
3//!
4//! This library provides types that describe a star, and catalogs of
5//! stars. The data maintained for a star is relatively simple: its
6//! position with respect to the sun, approximate magnitude and color.
7//!
8//! The purpose of the library is to allow simple applications such as
9//! star databases, constellation viewers, or sky map generation. The aim
10//! is not to support a full-blown astronomical dataset.
11//!
12//! # Included star catalogs
13//!
14//! The library includes the complete set of Hipparcos stars that have all
15//! the required information (116,812 stars). (The original Hipparcos
16//! catalog has a total of 118,218 stars, but some do not have parallax or
17//! visual magnitude values).
18//!
19//! There is no standard naming for stars; however, the IAU has some
20//! standard names, and the [iau] module includes the naming from
21//! <https://www.iau.org/public/themes/naming_stars> - for those
22//! approved up to Jan 2021. This database includes the IAU right
23//! ascension and declination, which is used to find the closest star
24//! in the Hipparcos database. Note that some IAU named stars are
25//! *not* in the Hipparcos database.
26//!
27//! If the `hipp_bright` feature is used then the Hipparcos catalog
28//! stars of magnitude 8.0 or brighter are included (41,013 stars) as
29//! a postcard string, as [hipparcos::HIPP_BRIGHT_PST]; also 430 'common'
30//! names of these stars are included as
31//! [hipparcos::HIP_COLLATED_ALIASES].
32//!
33//! # [Catalog], [Star], cube and [Subcube]
34//!
35//! Stars themselves are described by right ascension and declination;
36//! the [Star] type keeps these in radians. Distance to a star is kept
37//! in light years; it is likely to be known to a relatively poor
38//! accuracy compared to the position of the star in the sky.
39//!
40//! A star also contains an 'id'; this is a usize whose value is up to
41//! the catalog or user. For the Hipparcos catalog this is the
42//! Hipparcos catalog number; it is provided as a usize, rather than a
43//! generic, to provide for simple serialization of catalogs. The id
44//! of each star must be unique within a catalog (so it can be used
45//! for identifying a specific star).
46//!
47//! Each star maintains a unit vector in its direction, which can be
48//! viewed as placing the star on the unit sphere centered on the
49//! origin.
50//!
51//! A catalog is a collection of stars, with optional names; its
52//! supports indexing and searching by id or name, and by geometry.
53//!
54//! A catalog can only be searched once it has been fully populated;
55//! at this point the internal structures can be set up (and the stars
56//! sorted, for example, by id).
57//!
58//! To enable efficient searching by geometry the cube centred on the
59//! origin with corners at +-1 is subdivided in each dimension; this
60//! yields the [Subcube]. Each star's vector is then within one of the
61//! subcubes. The [Catalog] maintains list of stars within each
62//! subcube, but only once the catalog has been fully populated. The
63//! subcubes can then be used for the efficient geographical
64//! searching.
65//!
66//! # Precision
67//!
68//! The naked eye has a resolution of the order of 1 arcminute; this
69//! resolution easily fits in a single precision (f32) value for
70//! angles in radians.
71//!
72//! For photographic images using standard lenses the field of view
73//! will be, at smallest, in the order of a single degree. With modern
74//! cameras at 10,000 pixels across a sensor frame a single pixel is
75//! about 0.3 arcseconds; storing angles in f32 in radians just about
76//! suffices for this.
77//!
78//! For telescopic images, though, the precision required to specify
79//! the difference between two pixels in an image that could be from
80//! any part of the sky will exceed that provided by f32 (which is
81//! only roughly one part in 8E6).
82//!
83//! Hence the [Star] precision for position must be held as f64 values.
84//!
85//! # Fundamental types
86//!
87//! Stars maintain their right ascension and declination as f64
88//! values, but the less precise magnitude, distance, etc can be held
89//! as f32.
90//!
91//! Given the star's position is held as f64, the unit vector for the
92//! star (which the [Vec3] type provides) is held with f64
93//! components.
94//!
95//! # How to use
96//!
97//! First, create a catalog. Usually a catalog is loaded from a file:
98//!
99//! ```rust,ignore
100//!    let s = std::fs::read_to_string("hipparcos.json")?;
101//!    let mut catalog: Catalog = serde_json::from_str(&s)?;
102//! ```
103//!
104//! Before adding names the catalog must be sorted; the names will
105//! refer to *sorted* entries in the catalog (this restriction will be
106//! removed in the future, just not yet)
107//!
108//! ```rust,ignore
109//!    catalog.sort();
110//! ```
111//!
112//! Stars in the catalog can then be named; this applies names to the *id*s of {Star]s in the catalog.
113//!
114//! (The aliases in hipparcos::HIP_ALIASES are somewhat developer-specified...)
115//!
116//! ```rust,ignore
117//!    catalog.add_names(hipparcos::HIP_ALIASES)?;
118//! ```
119//!
120//! Before performing geometric searching
121//!
122//! ```rust,ignore
123//!    catalog.derive_data();
124//! ```
125//!
126//! Find a star by id
127//!
128//! ```rust,ignore
129//!   let polaris : CatalogIndex = catalog.find_sorted(11767).expect("Should have found Polaris");
130//! ```
131//!
132//! Find a star by name
133//!
134//! ```rust,ignore
135//!   let polaris_by_name = catalog.find_name("Polaris").expect("Should have found Polaris");
136//!   assert_eq!(catalog[polaris_by_name].id(), 111767);
137//! ```
138//!
139//! Find a star closest to a right-ascension and declination (and
140//! return the cosine of the angle offset):
141//!
142//! ```rust,ignore
143//!   let subcube_iter = Subcube::iter_all();
144//!   let (_,polaris_by_ra_de) = catalog.closest_to_ra_de(subcube_iter, 0.66, 1.555).expect("Should have found Polaris");
145//!   assert_eq!(catalog[polaris_by_ra_de].id(), 111767);
146//! ```
147//!
148//! Find possible sets of three stars (A, B C) where the three angles
149//! between at A, at B, and at C are given - to within an
150//! angular tolerance of delta:
151//!
152//! ```rust,ignore
153//!   let candidate_tris = catalog.find_star_triangles(catalog.iter_all(), &[0.1, 0.15, 0.05], 0.003);
154//! ```
155//!
156//! # A full-blown example
157//!
158//! ```rust
159//!    use star_catalog::{hipparcos, Catalog, CatalogIndex, Subcube};
160//!
161//! # fn main() -> Result<(),Box<dyn std::error::Error>> {
162//!    let s = std::fs::read_to_string("hipparcos_mag7.json")?;
163//!    let mut catalog: Catalog = serde_json::from_str(&s)?;
164//!    catalog.sort();
165//!    catalog.add_names(hipparcos::HIP_ALIASES, true)?;
166//!    catalog.derive_data();
167//!    let polaris : CatalogIndex = catalog.find_sorted(11767).expect("Should have found Polaris");
168//!    let polaris_by_name = catalog.find_name("Polaris").expect("Should have found Polaris");
169//!    assert_eq!(catalog[polaris_by_name].id(), 11767);
170//!    let subcube_iter = Subcube::iter_all();
171//!    let (_,polaris_by_ra_de) = catalog.closest_to_ra_de(subcube_iter, 0.66, 1.555).expect("Should have found Polaris");
172//!    assert_eq!(catalog[polaris_by_ra_de].id(), 11767);
173//! # Ok(())
174//! # }
175//! ```
176//! # Crate Feature Flags
177//!
178//! The following crate feature flags are available. They are configured in your Cargo.toml.
179//!
180//! * csv
181//!
182//!    * Optional, compatible with Rust stable
183//!
184//!    * Allows reading of CSV catalog files (such as hipparcos::read_to_catalog)
185//!
186//! * image
187//!
188//!    * Optional, compatible with Rust stable
189//!
190//!    * Module to provide means to create images, and to add skymap
191//!      images and cubemap to star_catalog binary
192//!
193//! * postcard
194//!
195//!    * Optional, compatible with Rust stable
196//!    * Allows reading and writing catalog files in Postcard format
197//!
198//! * hipp_bright
199//!
200//!    * Optional, compatible with Rust stable
201//!    * Includes constants for the Hipparcos catalog
202//!
203
204/// An XY vector used for star positions within an image
205pub type Vec2 = geo_nd::FArray<f64, 2>;
206
207/// An XYZ vector used for star directions
208pub type Vec3 = geo_nd::FArray<f64, 3>;
209
210/// A vector required by the Quat type - otherwise unused in the star catalog
211pub type Vec4 = geo_nd::FArray<f64, 4>;
212
213/// A quaternion that represents orientations of views of a sky map;
214/// this includes the direction and 'up' for a camera, for example
215pub type Quat = geo_nd::QArray<f64>;
216
217mod catalog;
218mod error;
219mod star;
220mod star_filter;
221mod subcube;
222
223pub mod cmdline;
224pub mod constellations;
225pub mod hipparcos;
226pub mod iau;
227
228#[cfg(feature = "image")]
229mod image;
230
231pub use catalog::{Catalog, CatalogIndex};
232pub use error::Error;
233pub use star::Star;
234pub use star_filter::{StarFilter, StarFilterFn};
235pub use subcube::Subcube;
236
237#[cfg(feature = "image")]
238pub use image::{ImageView, StarDrawStyle};