Skip to content

Commit

Permalink
update examples to using new fov.jd
Browse files Browse the repository at this point in the history
Also fix negative indexing for FOVList and SimStates
  • Loading branch information
dahlend committed Sep 21, 2024
1 parent c0913c0 commit 4969a07
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 33 deletions.
6 changes: 3 additions & 3 deletions docs/tutorials/palomar.rst
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ the glass plate itself so not visible.
wcs = kete.irsa.plot_fits_image(frame, percentiles=(40, 99))
for idx in range(len(vis)):
vec = vis.obs_vecs()[idx]
vec = vis.obs_vecs[idx]
kete.irsa.annotate_plot(wcs, vec, style='o', px_gap=10, text=vis[idx].desig)
plt.title("Dec 6 1950 - Annotated")
Expand All @@ -210,12 +210,12 @@ position causes the alignment to match within the width of the blur.
wcs = kete.irsa.plot_fits_image(frame, percentiles=(30, 99.9))
kete.irsa.annotate_plot(wcs,
vis.obs_vecs()[1],
vis.obs_vecs[1],
style='L',
px_gap=5,
length=10,
text=" " + vis[1].desig)
kete.irsa.zoom_plot(wcs, vis.obs_vecs()[1])
kete.irsa.zoom_plot(wcs, vis.obs_vecs[1])
plt.title(f"Dec 6 1950 - Annotated Zoom");
plt.savefig("data/full_frame_annotated_zoom.png")
plt.close()
Expand Down
4 changes: 2 additions & 2 deletions docs/tutorials/wise.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ frame in the mission phase we have selected.
.. code-block:: python
# Time of the first exposure.
jd = fovs[0].observer.jd
jd = fovs[0].jd
# now we propagate the NEOs to that time, including the effects of the 5 largest
# main belt asteroids to include more precision. This may take a few minutes.
Expand Down Expand Up @@ -117,7 +117,7 @@ Here is a codeblock which prints the first `n_show=100` objects.
for vis in visible[:n_show]:
for state in vis:
vec = (state.pos - vis.fov.observer.pos).as_equatorial
mjd = kete.Time(vis.fov.observer.jd).mjd
mjd = kete.Time(vis.fov.jd).mjd
print((f"{state.desig:<15s},{mjd:<15.6f},{vec.ra_hms:<15s},"
f"{vec.dec_dms:<15s},{vis.fov.scan_id}-{str(vis.fov.frame_num)}"))
Expand Down
2 changes: 1 addition & 1 deletion src/examples/getting_started.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@
# there is a function on the `SimultaneousState` which provides the vectors from the
# observer to the objects.

obs_vecs = visible.obs_vecs()[0]
obs_vecs = visible.obs_vecs[0]
obs_vecs = obs_vecs.as_equatorial
obs_vecs.ra, obs_vecs.dec

Expand Down
14 changes: 7 additions & 7 deletions src/examples/plot_comet.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,19 +106,19 @@ def plot_syndyne(wcs, state, fov, beta, back_days=90, day_step=1, **kwargs):
model = kete.propagation.NonGravModel.new_dust(beta)

# working backward, calculate the position of the comet at each time step
dust_state = kete.propagate_n_body([state], fov.observer.jd - back_days)[0]
dust_state = kete.propagate_n_body([state], fov.jd - back_days)[0]
dust_states = []
for jd in np.arange(dust_state.jd, fov.observer.jd, day_step):
for jd in np.arange(dust_state.jd, fov.jd, day_step):
dust_state = kete.propagate_n_body([dust_state], jd)[0]
dust_states.append(dust_state)

# Now treat all of those points as though they are release dust, and
# propagated to the current epoch.
cur_state = kete.propagate_n_body(
dust_states, fov.observer.jd, non_gravs=[model] * len(dust_states)
dust_states, fov.jd, non_gravs=[model] * len(dust_states)
)
# apply a light delay correction
cur_state = kete.propagate_two_body(cur_state, fov.observer.jd, fov.observer.pos)
cur_state = kete.propagate_two_body(cur_state, fov.jd, fov.observer.pos)

# Setup plotting
pos = [(x.pos - fov.observer.pos).as_equatorial for x in cur_state]
Expand Down Expand Up @@ -148,13 +148,13 @@ def plot_synchrone(
models = [kete.propagation.NonGravModel.new_dust(beta) for beta in betas]

# propagate the comet back to the release date
dust_state = kete.propagate_n_body([state], fov.observer.jd + days_back)[0]
dust_state = kete.propagate_n_body([state], fov.jd + days_back)[0]
dust_states = [dust_state] * len(betas)

# release dust and propagate foward to the current epoch.
cur_state = kete.propagate_n_body(dust_states, fov.observer.jd, non_gravs=models)
cur_state = kete.propagate_n_body(dust_states, fov.jd, non_gravs=models)
# apply a light delay correction
cur_state = kete.propagate_two_body(cur_state, fov.observer.jd, fov.observer.pos)
cur_state = kete.propagate_two_body(cur_state, fov.jd, fov.observer.pos)

# setup plotting
pos = [(x.pos - fov.observer.pos).as_equatorial for x in cur_state]
Expand Down
11 changes: 8 additions & 3 deletions src/kete/rust/fovs/collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,14 @@ impl FOVList {
self.0.len()
}

pub fn __getitem__(&self, idx: isize) -> PyResult<AllowedFOV> {
if idx as usize >= self.0.len() {
return Err(PyErr::new::<exceptions::PyIndexError, _>(""));
pub fn __getitem__(&self, mut idx: isize) -> PyResult<AllowedFOV> {
if idx < 0 {
idx += self.0.len() as isize;
}
if (idx < 0) || (idx as usize >= self.__len__()) {
return Err(PyErr::new::<exceptions::PyIndexError, _>(
"index out of range",
));
}
Ok(self.0[idx as usize].clone())
}
Expand Down
31 changes: 14 additions & 17 deletions src/kete/rust/simult_states.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use pyo3::exceptions;
use pyo3::prelude::*;
use pyo3::{pyclass, pymethods, PyResult};

use crate::vector::Vector;
use crate::vector::{Vector, VectorLike};
use crate::{fovs::AllowedFOV, frame::PyFrames, state::PyState};

/// Representation of a collection of [`State`] at a single point in time.
Expand Down Expand Up @@ -118,23 +118,22 @@ impl PySimultaneousStates {
self.0.states.len()
}

pub fn __getitem__(&self, idx: usize) -> PyResult<PyState> {
if idx >= self.__len__() {
return Err(PyErr::new::<exceptions::PyIndexError, _>(""));
pub fn __getitem__(&self, mut idx: isize) -> PyResult<PyState> {
if idx < 0 {
idx += self.0.states.len() as isize;
}
Ok(self.0.states[idx].clone().into())
if (idx < 0) || (idx as usize >= self.__len__()) {
return Err(PyErr::new::<exceptions::PyIndexError, _>(
"index out of range",
));
}
Ok(self.0.states[idx as usize].clone().into())
}

/// If a FOV is present, calculate all vectors from the observer position to the
/// position of the objects.
///
/// Parameters
/// ----------
/// final_frame :
/// Optional final reference frame. If not provided this will be the frame of
/// the observer.
#[pyo3(signature = (final_frame=None))]
pub fn obs_vecs(&self, final_frame: Option<PyFrames>) -> PyResult<Vec<Vector>> {
#[getter]
pub fn obs_vecs(&self) -> PyResult<Vec<Vector>> {
let fov = self
.fov()
.ok_or(PyErr::new::<exceptions::PyValueError, _>(
Expand All @@ -143,13 +142,11 @@ impl PySimultaneousStates {
.unwrap();
let obs = fov.observer();

let final_frame = final_frame.unwrap_or(obs.frame.into());

let mut vecs = Vec::with_capacity(self.__len__());
for state in &self.0.states {
let diff = Vector::new(state.pos, state.frame.into())
.__sub__(crate::vector::VectorLike::Arr(obs.pos));
vecs.push(diff.change_frame(final_frame));
.__sub__(VectorLike::Vec(Vector::new(obs.pos, obs.frame.into())));
vecs.push(diff);
}
Ok(vecs)
}
Expand Down

0 comments on commit 4969a07

Please sign in to comment.