-
Notifications
You must be signed in to change notification settings - Fork 2
/
OctofitterRadialVelocity.jl
403 lines (325 loc) · 17.8 KB
/
OctofitterRadialVelocity.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
module OctofitterRadialVelocity
using Octofitter
using PlanetOrbits
using Tables, TypedTables
using Distributions
using DataDeps
using RecipesBase
using AbstractGPs
# using TemporalGPs
using Distances: Distances
using FITSIO
using StaticArrays
using LinearAlgebra
export jd2mjd, mjd2jd
# Radial Velocity data type
const rv_cols = (:epoch, :rv, :σ_rv)
function rvpostplot end
function rvpostplot! end
"""
StarAbsoluteRVLikelihood(
(;inst_idx=1, epoch=5000.0, rv=−6.54, σ_rv=1.30),
(;inst_idx=1, epoch=5050.1, rv=−3.33, σ_rv=1.09),
(;inst_idx=1, epoch=5100.2, rv=7.90, σ_rv=.11),
)
Represents a likelihood function of relative astometry between a host star and a secondary body.
`:epoch` (mjd), `:rv` (km/s), and `:σ_rv` (km/s), and `:inst_idx` are all required.
`:inst_idx` is used to distinguish RV time series between insturments so that they may optionally
be fit with different zero points and jitters.
In addition to the example above, any Tables.jl compatible source can be provided.
"""
struct StarAbsoluteRVLikelihood{TTable<:Table,GP} <: Octofitter.AbstractLikelihood
table::TTable
instrument_names::Vector{String}
gaussian_process::GP
function StarAbsoluteRVLikelihood(observations...; instrument_names=nothing, gaussian_process=nothing)
table = Table(observations...)
if !issubset(rv_cols, Tables.columnnames(table))
error("Expected columns $rv_cols")
end
# Check instrument indexes are contiguous
if length(unique(table.inst_idx)) != maximum(table.inst_idx)
error("instrument indexes must run from 1:N without gaps")
end
# We sort the data first by instrument index then by epoch to make some later code faster
m = maximum(table.epoch)
ii = sortperm(table.inst_idx .* (10m) .+ table.epoch)
table = table[ii,:]
if isnothing(instrument_names)
instrument_names = string.(1:maximum(table.inst_idx))
end
return new{typeof(table),typeof(gaussian_process)}(table, instrument_names, gaussian_process)
end
end
StarAbsoluteRVLikelihood(observations::NamedTuple...;kwargs...) = StarAbsoluteRVLikelihood(observations; kwargs...)
export StarAbsoluteRVLikelihood
"""
Radial velocity likelihood.
"""
function Octofitter.ln_like(
rvlike::StarAbsoluteRVLikelihood,
θ_system,
elements,
num_epochs::Val{L}=Val(length(rvlike.table))
) where L
T = typeof(θ_system.M)
ll = zero(T)
# Each RV instrument index can have it's own barycentric RV offset and jitter.
# We support up to 10 insturments. Grab the offsets (if defined) and store in
# a tuple. Then we can index by inst_idxs to look up the right offset and jitter.
barycentric_rv_inst = (
hasproperty(θ_system, :rv0_1) ? θ_system.rv0_1 : convert(T, NaN),
hasproperty(θ_system, :rv0_2) ? θ_system.rv0_2 : convert(T, NaN),
hasproperty(θ_system, :rv0_3) ? θ_system.rv0_3 : convert(T, NaN),
hasproperty(θ_system, :rv0_4) ? θ_system.rv0_4 : convert(T, NaN),
hasproperty(θ_system, :rv0_5) ? θ_system.rv0_5 : convert(T, NaN),
hasproperty(θ_system, :rv0_6) ? θ_system.rv0_6 : convert(T, NaN),
hasproperty(θ_system, :rv0_7) ? θ_system.rv0_7 : convert(T, NaN),
hasproperty(θ_system, :rv0_8) ? θ_system.rv0_8 : convert(T, NaN),
hasproperty(θ_system, :rv0_9) ? θ_system.rv0_9 : convert(T, NaN),
hasproperty(θ_system, :rv0_10) ? θ_system.rv0_10 : convert(T, NaN),
)
jitter_inst = (
hasproperty(θ_system, :jitter_1) ? θ_system.jitter_1 : convert(T, NaN),
hasproperty(θ_system, :jitter_2) ? θ_system.jitter_2 : convert(T, NaN),
hasproperty(θ_system, :jitter_3) ? θ_system.jitter_3 : convert(T, NaN),
hasproperty(θ_system, :jitter_4) ? θ_system.jitter_4 : convert(T, NaN),
hasproperty(θ_system, :jitter_5) ? θ_system.jitter_5 : convert(T, NaN),
hasproperty(θ_system, :jitter_6) ? θ_system.jitter_6 : convert(T, NaN),
hasproperty(θ_system, :jitter_7) ? θ_system.jitter_7 : convert(T, NaN),
hasproperty(θ_system, :jitter_8) ? θ_system.jitter_8 : convert(T, NaN),
hasproperty(θ_system, :jitter_9) ? θ_system.jitter_9 : convert(T, NaN),
hasproperty(θ_system, :jitter_10) ? θ_system.jitter_10 : convert(T, NaN),
)
# Vector of radial velocity of the star at each epoch. Go through and sum up the influence of
# each planet and put it into here.
rv_star_buf_all_inst = zeros(T, (L))
noise_var_all_inst = zeros(T, (L))
# Work through RV data and model one instrument at a time
for inst_idx in 1:maximum(rvlike.table.inst_idx)
if isnan(jitter_inst[inst_idx]) || isnan(barycentric_rv_inst[inst_idx])
@warn("`jitter_$inst_idx` and `rv0_$inst_idx` must be provided (were NaN)", maxlog=1)
end
istart = findfirst(==(inst_idx), rvlike.table.inst_idx)
iend = findlast(==(inst_idx), rvlike.table.inst_idx)
epochs = vec(@view rvlike.table.epoch[istart:iend])
σ_rvs = vec(@view rvlike.table.σ_rv[istart:iend])
rvs = vec(@view rvlike.table.rv[istart:iend])
istart = istart[1]
iend = iend[1]
noise_var = @view noise_var_all_inst[istart:iend]
rv_star_buf = @view rv_star_buf_all_inst[istart:iend]
rv_star_buf .+= rvs
rv_star_buf .+= barycentric_rv_inst[inst_idx]
# # Zygote version:
# rv_star_buf = @view(rv_star_buf_all_inst[istart:iend]) .+ barycentric_rv_inst[inst_idx]
# rv_star_buf .+= barycentric_rv_inst[inst_idx]
# rv_star_buf += barycentric_rv_inst[inst_idx]
# Go through all "influences" on the RV signal, and subtract their models.
# You could consider `rv_star` as the residuals after subtracting these.
for planet_i in eachindex(elements)
orbit = elements[planet_i]
# Need to structarrays orbit if we want this to SIMD
planet_mass = θ_system.planets[planet_i].mass
Threads.@threads for epoch_i in eachindex(epochs)
rv_star_buf[epoch_i] -= radvel(orbit, epochs[epoch_i], planet_mass*Octofitter.mjup2msol)
end
# Zygote version:
# rv_star_buf -= radvel.(orbit, epochs, planet_mass*Octofitter.mjup2msol)
end
noise_var .= σ_rvs.^2 .+ jitter_inst[inst_idx]^2
if !isnothing(rvlike.gaussian_process)
# Fit a GP
local gp
try
gp = @inline rvlike.gaussian_process(θ_system)
catch err
if err isa PosDefException
@warn "err" exception=(err, catch_backtrace()) maxlog=1
return -Inf
elseif err isa ArgumentError
@warn "err" exception=(err, catch_backtrace()) maxlog=1
return -Inf
else
rethrow(err)
end
end
fx = gp(epochs, noise_var)
else
# Don't fit a GP
fx = MvNormal(Diagonal((noise_var)))
end
try
ll += logpdf(fx, rv_star_buf)
catch err
if err isa PosDefException || err isa DomainError
return -Inf
else
rethrow(err)
end
end
end
return ll
end
# We have inst: Normal(inst jitter)
# Or we have : GP(inst parameters)
# Can we fold these into one interface?
# The GP has to be fit to all the residuals and then evaluated at each epoch.
# The Normal(jitter) could be fit to all data as an MVNormal()
# (epochs, residuals) -> logpdf(Normal(0, (σ_rvs[i]^2 + jitter_inst^2)))
# Now calculate the likelihoods (Gaussian Proccess over residuals)
# Per((len_scale, per_scale)) =
# # with_lengthscale(SqExponentialKernel(), len_scale) ∘ PeriodicTransform(1 / per_scale)
# # with_lengthscale(SqExponentialKernel(), len_scale) ∘ PeriodicTransform(1 / per_scale)
# with_lengthscale(SqExponentialKernel(), len_scale) ∘ PeriodicTransform(1 / per_scale)
# # f = GP(PeriodicKernel() * SqExponentialKernel())
# f = GP(Per([3,1.2]))
# Way to unify this interface:
# RVLike(data, noise_models=(
# (system, epochs) -> Normal()
# ))
# Generate new radial velocity observations for a planet
function Octofitter.generate_from_params(like::StarAbsoluteRVLikelihood, θ_planet, elem::PlanetOrbits.AbstractOrbit)
# Get epochs and uncertainties from observations
epochs = like.table.epoch
σ_rvs = like.table.σ_rv
# Generate new planet radial velocity data
rvs = DirectOribts.radvel.(elem, epochs)
radvel_table = Table(epoch=epochs, rv=rvs, σ_rv=σ_rvs)
return StarAbsoluteRVLikelihood(radvel_table)
end
# Generate new radial velocity observations for a star
function Octofitter.generate_from_params(like::StarAbsoluteRVLikelihood, θ_system, orbits::Vector{<:Visual{KepOrbit}})
# Get epochs, uncertainties, and planet masses from observations and parameters
epochs = like.table.epoch
σ_rvs = like.table.σ_rv
planet_masses = [θ_planet.mass for θ_planet in θ_system.planets] .* 0.000954588 # Mjup -> Msun
# Generate new star radial velocity data
rvs = radvel.(reshape(orbits, :, 1), epochs, transpose(planet_masses))
# TODO: Question: is adding jitter like this appropriate in a generative model? I think so.
noise = randn(length(epochs)) .* θ_system.jitter
rvs = sum(rvs, dims=2)[:,1] .+ θ_system.rv .+ noise
radvel_table = Table(epoch=epochs, rv=rvs, σ_rv=σ_rvs)
return StarAbsoluteRVLikelihood(radvel_table)
end
mjd2jd(jd) = jd + 2400000.5
jd2mjd(jd) = jd - 2400000.5
include("harps_rvbank.jl")
include("harps_dr1.jl")
include("hires.jl")
include("lick.jl")
include("ces.jl")
include("radvel.jl")
# Plot recipe for astrometry data
@recipe function f(rv::StarAbsoluteRVLikelihood)
xguide --> "time (mjd)"
yguide --> "radvel (m/s)"
multiple_instruments = hasproperty(rv.table,:inst_idx) &&
length(unique(rv.table.inst_idx)) > 1
if !multiple_instruments
@series begin
color --> :black
label := nothing
seriestype := :scatter
markersize--> 0
yerr := rv.table.σ_rv
rv.table.epoch, rv.table.rv
end
else
for inst_idx in sort(unique(rv.table.inst_idx))
@series begin
label := nothing
seriestype := :scatter
markersize--> 0
color-->inst_idx
markerstrokecolor-->inst_idx
yerr := rv.table.σ_rv[rv.table.inst_idx.==inst_idx]
rv.table.epoch[rv.table.inst_idx.==inst_idx], rv.table.rv[rv.table.inst_idx.==inst_idx]
end
end
end
end
function __init__()
register(DataDep("ESOHARPS_DR1_rvs",
"""
Dataset: ESO/HARPS Radial Velocities Catalog
Author: Barbieri, M.
License: CC0-1.0
Publication: https://arxiv.org/abs/2312.06586
Website: https://archive.eso.org/dataset/ADP.2023-12-04T15:16:53.464
The first public data release of the HARPS radial velocities catalog. This data release aims to provide the astronomical community with a catalog of radial velocities obtained with spectroscopic observations acquired from 2003 to 2023 with the High Accuracy Radial Velocity Planet Searcher (HARPS) spectrograph installed at the ESO 3.6m telescope in La Silla Observatory (Chile), and spanning wavelengths from 3800 to 6900 Angstrom. The catalog comprises 289843 observations of 6488 unique astronomical objects.
Radial velocities reported in this catalog are obtained using the HARPS pipeline, with a typical precision of 0.5 m/s, which is essential for the search and validation of exoplanets. Additionally, independent radial velocities measured on the Hα spectral line are included, with a typical error of around 300 m/s suitable for various astrophysical applications where high precision is not critical. This catalog includes 282294 radial velocities obtained through the HARPS pipeline and 288972 derived from the Hα line, collectively forming a time-series dataset that provides a historical record of measurements for each object.
Further, each object has been cross-referenced with the SIMBAD astronomical database to ensure accurate identification, enabling users to locate and verify objects with existing records in astronomical literature. Information provided for each object includes: astrometric parameters (coordinates, parallaxes, proper motions, radial velocities), photometric parameters (apparent magnitudes in the visible and near-infrared), spectral types and object classifications.
File size: 160MiB
""",
"https://dataportal.eso.org/dataPortal/file/ADP.2023-12-04T15:16:53.464",
"9cff9058cb126e76eb9841d2e3fe3f385c1ebe386662633f21e7db78d2ba6b14"
))
register(DataDep("HARPS_RVBank",
"""
Dataset: A public HARPS radial velocity database corrected for systematic errors
Author: Trifonov et al.
License: CC0-1.0
Publication: https://www.aanda.org/articles/aa/full_html/2020/04/aa36686-19/aa36686-19.html
Website: https://www2.mpia-hd.mpg.de/homes/trifonov/HARPS_RVBank.html
A public HARPS radial velocity database corrected for systematic errors. (2020)
Updated in 2023 to coincide with the ESO/HARPS Radial Velocities Catalog release.
Publication: https://arxiv.org/abs/2312.06586
Website: https://archive.eso.org/dataset/ADP.2023-12-04T15:16:53.464
File size: 38MiB
""",
"https://github.com/3fon3fonov/HARPS_RVBank/raw/master/HARPS_RVBank_ver02.csv.zip",
"bc32ee889fba6cb0871efe8b4278b0c994cf302c7538aeb6aca630e27bdbb6d8",
post_fetch_method=unpack
))
register(DataDep("HIRES_rvs",
"""
Dataset: A public HIRES radial velocity database corrected for systematic errors
Author: Butler et al.
License:
Publication: https://ui.adsabs.harvard.edu/abs/2017yCat..51530208B/abstract
Website: https://ebps.carnegiescience.edu/data/hireskeck-data
File size: 3.7MiB
""",
"https://drive.google.com/uc?id=10xCy8UIH8wUAnNJ8zCN8kfFzazWnw-f_&export=download",
"ad68c2edb69150318e8d47e34189fe104f2a5194a4fcd363c78c741755893251",
post_fetch_method=unpack
))
register(DataDep("Lick_rvs",
"""
Dataset: The Twenty-Five Year Lick Planet Search
Author: Fischer et al.
License:
Publication: https://iopscience.iop.org/article/10.1088/0067-0049/210/1/5#apjs488421t2
A public Lick radial velocity database.
File size: 780k
""",
"https://content.cld.iop.org/journals/0067-0049/210/1/5/revision1/apjs488421t2_mrt.txt?Expires=1698868925&Signature=YyKJ4p64PeQg2sh~VAYj6aXxH8b-lH0F0lS6GF0YP07V7oaZWzM4sthpMRldUE7cHQZbMkwoW0R-Jq2FymIYqIlAnT1-qs-y~JifD1A1WThaBOEP2gl5JGgDOGXXMCLK4VuKM3ZucSUu9TWIb3vbNqrG7l~V9LIs-K2bW~KcM-syfRzJ1YC6TSiej1PHJVhoxN-SUQRAw2lkLVQ-eea30IFOw9RSmYFqrqUQGnwx7fdkbTd5ZSvQ~BmB0HZjsav890rZpEVWlCs8ITLpKab3aEysIptlezpS90boNDi3CR-p7We2M9WfibcsemIa72HH7cZS~S1Ri8QTQra5nTY8eQ__&Key-Pair-Id=KL1D8TIY3N7T8",
))
register(DataDep("CES_rvs",
"""
Dataset: The planet search programme at the ESO Coude Echelle spectrometer and HARPS.IV. The search for Jupiter analogues around solar-like stars
Author:
License:
Publication: 10.1051/0004-6361/201116551 (https://www.aanda.org/10.1051/0004-6361/201116551)
The planet search programme at the ESO Coude Echelle spectrometer and HARPS.IV. The search for Jupiter analogues around solar-like stars. (2013)
Extracted file size: 2.3M
""",
"http://cdsarc.u-strasbg.fr/viz-bin/nph-Cat/tar.gz?J/A+A/552/A78",
"24d1ce0356fb6b62ec6a131c3d9f55ef3aced37660c635d87f33d697e074cdfb",
post_fetch_method=unpack
))
register(DataDep("UVES_rvs",
"""
Dataset: A Reanalysis of the UVES M Dwarf Planet Search Program
Author: R. P. Butler, H. R. A. Jones, F. Feng, M. Tuomi, G. Anglada-Escudé, and Sandy Keiser
License:
Publication: 10.3847/1538-3881/ab4905 (https://iopscience.iop.org/article/10.3847/1538-3881/ab4905)
The UVES (Ultraviolet and Visible Spectrometer) M Dwarf Planet Search program surveyed 40 M dwarfs and 1 M giant from 2000 through 2007 March. Two of the M dwarfs were double-lined spectroscopic binaries. The 38 single-lined M dwarfs in this survey are among the nearest and brightest M dwarfs. Starting with the reduced 1D spectra provided by the UVES team, we reanalyzed the UVES velocities of Proxima Cen as part of the “Pale Red Dot” program. The velocity rms decreased from 3.6 to 2.3 m s−1. Motivated by this result, we have harvested all of the raw data from the UVES M Dwarf Planet Search from the European Southern Observatory (ESO) archives and have written custom packages to generate 1D spectra from the raw data, and velocities from the 1D spectra. The median improvement in the velocity rms from the new analysis is 1.8 m s−1. Six of the 38 M dwarfs from the original study had a velocity rms < 4 m s−1. In the reanalysis presented here, 22 of these stars have a velocity rms < 4 m s−1. We improve the upper limits on possible planets orbiting these stars by a factor of typically two to three. For many of these M dwarfs, these observations represent the first epoch of high-precision velocity measurements.
""",
"https://content.cld.iop.org/journals/1538-3881/158/6/251/revision1/ajab4905t2_mrt.txt",
))
#
return
end
end