Skip to content

Instantly share code, notes, and snippets.

@StuartLittlefair
StuartLittlefair / helio_bary.py
Created October 17, 2019 10:07
Quick snippet to use astropy to convert from heliocentric times to barycentric and vice-versa
from astropy.coordinates import SkyCoord, EarthLocation
from astropy import units as u
from astropy.time import Time
def helio_to_bary(coords, hjd, obs_name):
helio = Time(hjd, scale='utc', format='jd')
obs = EarthLocation.of_site(obs_name)
star = SkyCoord(coords, unit=(u.hour, u.deg))
ltt = helio.light_travel_time(star, 'heliocentric', location=obs)
@StuartLittlefair
StuartLittlefair / astropy_speedups.py
Last active May 18, 2020 17:10
Speedups for astropy coordinate transformations with >1000s of obstimes (works best for roughly regular spaced grid of obstimes)
"""
Contains some faster coordinate transformations than the ones currently used in astropy.
This is based on an idea put forward by @bwinkel in the pull request located at
at https://github.com/astropy/astropy/pull/6068. This may be merged into the astropy
master at some point. If this happens, this module can be removed.
Simply import into code to experience the speedups; the astropy coordinate transforms are
overwritten on modeul import.
"""
@StuartLittlefair
StuartLittlefair / example_timeseries.py
Last active November 19, 2018 00:43
A highly incomplete prototype for a TimeSeries class
from collections import OrderedDict
import copy
import operator
from astropy.time import Time
class TimeSeries:
def __init__(self, time, data=None, **kwargs):
if not isinstance(time, Time):
raise ValueError('time must be an astropy Time')
@StuartLittlefair
StuartLittlefair / quick_altaz.py
Created May 31, 2018 13:52
Use broadcasting to transform many SkyCoords into frames with vector properties
from astropy.coordinates import SkyCoord, EarthLocation
from astropy.coordinates.tests.utils import randomly_sample_sphere
from astropy.time import Time
import numpy as np
# 1000 random locations on the sky
ra, dec, _ = randomly_sample_sphere(1000)
coos = SkyCoord(ra, dec)
@StuartLittlefair
StuartLittlefair / sunmoon.py
Created January 24, 2018 17:10
quick-ish calculation of sun and moon location
from astropy import coordinates as coord
from astropy.time import Time
from astropy import units as u
lapalma = coord.EarthLocation.of_site('lapalma')
times = Time.now() + u.minute*np.linspace(-720, 720, 1440)
altaz_frame = coord.AltAz(location=lapalma, obstime=times)
sun = coord.get_body('sun', location=lapalma, time=times)
moon = coord.get_body('moon', location=lapalma, time=times)
@StuartLittlefair
StuartLittlefair / autoSchedule.py
Created May 8, 2017 12:01
Autoscheduling for ultracam runs
import numpy as np
from trm import observing
from astropy.coordinates import SkyCoord, EarthLocation
from astropy import units as u
from astropy.time import Time
from astroplan import Observer, ObservingBlock, FixedTarget
from astroplan.scheduling import (Schedule, Transitioner, TransitionBlock,
PriorityScheduler, SequentialScheduler)
@StuartLittlefair
StuartLittlefair / test_bcorr.py
Created January 31, 2017 14:54
Comparison of proposed barycentric corrections in astropy to Jason Eastman's web applets for barycentric velocity correction
from astropy import units as u
from astropy import coordinates as coord
from astropy.time import Time
from astropy.coordinates.solar_system import get_body_barycentric_posvel
from astropy.coordinates.matrix_utilities import matrix_product
from astropy.coordinates.representation import CartesianRepresentation ,UnitSphericalRepresentation
from astropy.coordinates.builtin_frames import GCRS
from astropy.coordinates import SkyCoord, solar_system, EarthLocation, ICRS
KPS = u.km/u.s
@StuartLittlefair
StuartLittlefair / corrs.py
Last active July 18, 2023 17:00
How to do barycentric velocity correction with astropy v1.3
from astropy.time import Time
from astropy.coordinates import SkyCoord, solar_system, EarthLocation, ICRS
from astropy import units as u
def velcorr(time, skycoord, location=None):
"""Barycentric velocity correction.
Uses the ephemeris set with ``astropy.coordinates.solar_system_ephemeris.set`` for corrections.
For more information see `~astropy.coordinates.solar_system_ephemeris`.
@StuartLittlefair
StuartLittlefair / style_cells.js
Created July 28, 2015 11:27
How to add bootstrap style panels to markdown cells in the Jupyter notebook
/*global define*/
/**
* To load this extension, add the following to your custom.js:
*
* require(['base/js/events'], function (events) {
* events.on('app_initialized.NotebookApp', function() {
* IPython.load_extensions('nbgrader');
* });
* });
*