astropy / astroplan

Observation planning package for astronomers – maintainer @bmorris3
https://astroplan.readthedocs.io/en/latest/
BSD 3-Clause "New" or "Revised" License
199 stars 109 forks source link

proposed: Timespan class #516

Closed edose closed 2 years ago

edose commented 2 years ago

Timespans (simply a start time to end time) are integral to astro planning. But I don't see such in astroplan, other than the Slot class which doesn't strike me as overly developed or much used.

[EDIT: I see now that a lot of the Slot functionality is actually over in class Schedule. Maybe this proposal should reduce to an extension and refactoring of Slot.]

I'm now converting my existing astro planning code to be more astropy- and astroplan-based, and I realized that my (currently in-revision) Timespan class would be a natural fit with astroplan. As synopsis, I propose something like this:

class Timespan(start_time, end_time) # parms as py datetime in UTC, or scalar astropy Time

Properties or attributes:

start, end (as Time) duration (as TimeDelta) seconds (duration as float) midpoint (as Time)

Methods:

.copy() # perhaps not needed, as Timespan objects should be treated as immutable

.__eq__(other_timespan) # for timespan1 == timespan2

.delay_by(TimeDelta) # new timespan, shifted by a timedelta, positive or negative

.expand_by(TimeDelta) # new timespan, extended or shortened at each end by a timedelta

.intersect(other_timespan) # new timespan, including only times common to both timespans

.remove(other_timespan) # new timespan(s), with other_timespan removed

.contains(other_timespan or time) # True iff this timespan contains other timespan or time

.split_at(time) # 2 new timespans, split at time (return same timespan, if time not wholly contained)

.longer(timespan1, timespan2) # returns longer of two timespans (class method)

.periodic_events(ref_time, period, max_events) # returns array Time object with all event times within timespan, the series passing through ref_time (which need not be within the timespan), and spaced by exactly period (a DeltaTime). For variable star ephemerides and the like.

The method syntax is designed to support method chaining, e.g., ts.split_at(t)[0].remove(ts2).duration where convenient.

Similar code has worked for me for some years. But this would be my first pull request (anywhere), so it could take a while for me to get that part right. I'm willing to give it a go if you're interested.

edose commented 2 years ago

This was not a good first code proposal, and I withdraw it. Mods, you may certainly delete it if you wish.

I have a much more concise and more useful one in mind--I will submit a PR for that later.