# How to detect & store lunar eclipse events?

**URL:** <https://discourse.julialang.org/t/how-to-detect-store-lunar-eclipse-events/27459>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [August 12, 2019, 1:53pm UTC](https://discourse.julialang.org/t/how-to-detect-store-lunar-eclipse-events/27459 "2019-08-12T13:53:03Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Calum\_H](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/calum_h/32/9748_2.png) [@Calum\_H](https://discourse.julialang.org/u/Calum_H)\
**Post date:** [August 12, 2019, 1:53pm UTC](https://discourse.julialang.org/t/how-to-detect-store-lunar-eclipse-events/27459/1 "2019-08-12T13:53:03Z")

</div>

Hi all, I am quite new, this is my first question on here so hope I have provided enough detail!

I am writing a script for detecting lunar eclipses across a spacecraft, using various Astrodynamics julia packages (AstroTime.jl, SPICE.jl etc.). I created a custom struct _Eclipse_ to store my individual eclipse events.

```julia
struct Eclipse
    "Start time (UTC)" 
    Ti::UTCEpoch 
    "Finish time (UTC)" 
    Tf::UTCEpoch 
    "Duration - seconds" 
    dur::AstroTime.Period 
    "Type - penumbral or umbral" 
    type::Symbol
    "Event number"
    num::Int64
end 

```

In the main I loop through the dates and at each time step detect the type of eclipse (or lack of eclipse). Now I want to store each eclipse as an instance of my struct _Eclipse_ and save them to an array with the start and stop times. However, I can’t figure out how to do this in a general way (e.g. one that doesn’t rely on knowing a priori the order of eclipse types).

A MWE:

```julia
eclipses = []

for i in eachindex(dates)
        # Check whether the spacecraft is in eclipse or not. Returns :none, :pen, or :umb. 
        eclipse_type = detect_eclipse(spacecraft_position[i], dates[i])
        

        ## DO some check conditions and identify the start and stop time of a single event (e.g. a series of uninterrupted :pen eclipse_types equates to one eclipse event). 

        # Once a full eclipse has been found, push this eclipse struct to the array. 
        push!(eclipses, Eclipse(t_i, t_f, t_f-t_i, eclipse_type, event_number))

end 
        

```

One idea i had was to push to another array at each time step so I have something like this

> Array{N, 1}[:none, :none, :pen, :pen, :pen, :umb, :umb, :umb, :none, :none, …] where N = length(dates)

then check at which indices the value changes as this indicates the start/stop times of an eclipse. However, I am unsure how to implement this and it seems very inefficient. Any other ideas for distinguishing events would be very appreciated, thanks!

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [August 12, 2019, 1:59pm UTC](https://discourse.julialang.org/t/how-to-detect-store-lunar-eclipse-events/27459/2 "2019-08-12T13:59:59Z")

</div>

How do you want to access the information, do you want to ask for the next event of some kind (or either kind) or do you want to have a daily state of affairs be shown to you or would you like to order the events by their duration or some combination of the above or something else entirely?

---

<div class="post-metadata">

**Author:** ![Calum\_H](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/calum_h/32/9748_2.png) [@Calum\_H](https://discourse.julialang.org/u/Calum_H)\
**Post date:** [August 12, 2019, 2:04pm UTC](https://discourse.julialang.org/t/how-to-detect-store-lunar-eclipse-events/27459/3 "2019-08-12T14:04:22Z")

</div>

Ultimately I would like to read a spacecraft trajectory file and output a complete chronological list of all eclipses across a given date range that I can write to a file or plot etc.

E.g write a file that is similar to this:  
row 1 = Ecclipse 1, start time, stoptime, eclipse type, duration, event number,  
row 2 = Ecclipse 2, start time, stoptime, eclipse type, duration, event number,  
…  
row N = Ecclipse 3, start time, stoptime, eclipse type, duration, event number,

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [August 12, 2019, 2:07pm UTC](https://discourse.julialang.org/t/how-to-detect-store-lunar-eclipse-events/27459/4 "2019-08-12T14:07:04Z")

</div>

So the spacecraft trajectory file gives you the span of dates of interest for that craft+path. You have a date interval and you want all events that happen to occur within that interval returned to you in date order. Is that correct?

---

<div class="post-metadata">

**Author:** ![Calum\_H](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/calum_h/32/9748_2.png) [@Calum\_H](https://discourse.julialang.org/u/Calum_H)\
**Post date:** [August 12, 2019, 2:08pm UTC](https://discourse.julialang.org/t/how-to-detect-store-lunar-eclipse-events/27459/5 "2019-08-12T14:08:34Z")

</div>

Yep!

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [August 12, 2019, 2:26pm UTC](https://discourse.julialang.org/t/how-to-detect-store-lunar-eclipse-events/27459/6 "2019-08-12T14:26:07Z")

</div>

There are two data management tools that are explicitly designed for time-sequenced information `TimeSeries.jl` and `Temporal.jl` in addtion, `https://github.com/JuliaComputing/IndexedTables.jl` is a very flexible data rearrainger and subset culler that would work well for this. At this time, all are light on the sorts of graduated examples that make learning fun. I suggest that read the docs that the first two offer and look through the examples and tests for all of them. If one feels more comfortable, use that. If none seems appropriate or speaks to you clearly, then you should see what `DataFrames.jl` can do for you (everything you need … and there is much more support and community familiarity with that package).

---

<div class="post-metadata">

**Author:** ![Calum\_H](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/calum_h/32/9748_2.png) [@Calum\_H](https://discourse.julialang.org/u/Calum_H)\
**Post date:** [August 13, 2019, 10:26am UTC](https://discourse.julialang.org/t/how-to-detect-store-lunar-eclipse-events/27459/7 "2019-08-13T10:26:49Z")

</div>

Thanks for pointing me to these packages JeffreySarnoff. Although I ended up figuring out a way to do it without creating extra tables, the TimeSeries docs led me to a conceptual method that solved the problem!
