Skip to content

Commit

Permalink
add Query functions
Browse files Browse the repository at this point in the history
  • Loading branch information
aburousan committed Jun 21, 2024
1 parent 49e383a commit ad20390
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 5 deletions.
11 changes: 10 additions & 1 deletion Examples/Basic_cosmo_example.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
### A Pluto.jl notebook ###
# v0.19.36
# v0.19.41

using Markdown
using InteractiveUtils
Expand Down Expand Up @@ -51,6 +51,14 @@ H(c,100)#takes your cosmology model and z
# ╔═╡ 3aa50f1f-04e8-4d0d-a650-c0ef8e75089f
md"Let's try plotting $\frac{H(z)}{(1+z)}$ vs $z$."

# ╔═╡ 5156f459-79e4-467e-b894-9e7cb89d58e7
# ╠═╡ disabled = true
#=╠═╡
begin
Pkg.add("LaTeXStrings")
end
╠═╡ =#

# ╔═╡ 2ee6b0bf-3511-4fea-8132-9896f5f112ec
begin
z_vals = range(0, 2.5, length=1000)
Expand Down Expand Up @@ -132,6 +140,7 @@ plot!([13.807434539,13.807434539],[0,2.5],lc=:green,lw=2.5,label="t = 13.80743",
# ╟─5d11e724-326a-436f-94f4-211f99d30990
# ╠═7e4c3515-02f7-498b-829f-23e78997fb70
# ╟─3aa50f1f-04e8-4d0d-a650-c0ef8e75089f
# ╠═5156f459-79e4-467e-b894-9e7cb89d58e7
# ╠═1473e933-d04a-4a0b-90d8-ab33df9ff3ee
# ╠═2ee6b0bf-3511-4fea-8132-9896f5f112ec
# ╟─4984ac71-b8eb-4085-a20a-9a2ac5f439a4
Expand Down
7 changes: 7 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,17 @@ authors = ["Kazi Abu Rousan"]
version = "1.0.0-DEV"

[deps]
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
DifferentialEquations = "0c46a032-eb83-5123-abaf-570d42b7fbaa"
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
HTTP = "cd3eb016-35fb-5094-929b-558a96fad6f3"
LaTeXStrings = "b964fa9f-0449-5b57-a5c2-d3ea65f4040f"
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
Pluto = "c3e4b0f8-55cb-11ea-2926-15256bba5781"
QuadGK = "1fd47b50-473d-5c70-9696-f719f8f3bcdc"
ReverseDiff = "37e2e3b7-166d-5795-8a7a-e32c996b4267"
Revise = "295af30f-e4ad-537b-8983-00126c2a3abe"
Roots = "f2b01f46-fcfa-551c-844a-d8ac1e96c665"

[compat]
Expand Down
4 changes: 3 additions & 1 deletion src/Cosmic.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
module Cosmic

include("basic_cos.jl")
include("thermo.jl")
include("astroquery.jl")

export a, redshift, cosmology, Mpc_to_km, sec_to_year, H, my_f, my_g,
hubble_distance, χ, hubble_time, T, age, look_back_time, scale_fact,
da_dt, ageGyr,scalefact_part
da_dt, ageGyr,scalefact_part, fetch_gaia_data, filter_missing_values

end# module end
61 changes: 61 additions & 0 deletions src/astroquery.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using HTTP
using DataFrames
using CSV

function fetch_gaia_data(query::String)
url = "https://gea.esac.esa.int/tap-server/tap/sync"
params = Dict(
"REQUEST" => "doQuery",
"LANG" => "ADQL",
"FORMAT" => "csv",
"QUERY" => query
)
encoded_params = join(["$k=$(HTTP.escapeuri(v))" for (k, v) in params], "&")
response = HTTP.post(url, ["Content-Type" => "application/x-www-form-urlencoded"], encoded_params)
if response.status == 200
data = CSV.read(IOBuffer(response.body), DataFrame)
return data
else
error("Error: ", response.status, "\n", String(response.body))
end
end

function filter_missing_values(df::DataFrame)
clean_data = dropmissing(df)
return clean_data
end

query = """
SELECT TOP 10
source_id,
ra,
dec,
parallax,
phot_g_mean_mag
FROM gaiadr3.gaia_source
WHERE ra BETWEEN 0 AND 10
AND dec BETWEEN -10 AND 10
"""

query1 = """
SELECT
source_id,
ra,
dec,
parallax,
phot_g_mean_mag
FROM gaiadr3.gaia_source
WHERE ra BETWEEN 0 AND 10
AND dec BETWEEN -10 AND 10
"""

function filter_missing_values(df::DataFrame)
# Filter rows containing missing values
clean_data = dropmissing(df)
return clean_data
end


data = fetch_gaia_data(query1)
dat_fil = filter_missing_values(data)
println(dat_fil)
46 changes: 43 additions & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,51 @@
using Cosmic
using Test
using Plots

@testset "Cosmic.jl" begin
@test my_f(2,1) == 11
@test my_g(2) == 4
@test redshift(2.9e-4) 3447.27586
@test cosmology().h 0.6774
@test scalefact_part(cosmology(),100) 148.9412838881
@test ageGyr(cosmology(),0) 13.80743453918
end
end



query = """
SELECT source_id,
phot_g_mean_mag,
bp_rp,
phot_g_mean_flux,
parallax
FROM gaiadr3.gaia_source
WHERE bp_rp IS NOT NULL AND phot_g_mean_mag IS NOT NULL AND parallax > 50
ORDER BY phot_g_mean_mag ASC
"""

gaia_data = fetch_gaia_data(query)
data = filter_missing_values(gaia_data)

parallax = gaia_data[:, "parallax"]
distance = 1000.0 ./ parallax # distance in parsecs
abs_mag = gaia_data[:, "phot_g_mean_mag"] .+ 5 .* log10.(parallax / 100.0)

luminosity = 10 .^ ((4.83 .- abs_mag) ./ 2.5)

bp_rp = gaia_data[:, "bp_rp"]

normalized_bp_rp = (bp_rp .- minimum(bp_rp)) ./ (maximum(bp_rp) .- minimum(bp_rp))

cmap = cgrad(:plasma)

scatter(bp_rp, log10.(luminosity),
marker_z=normalized_bp_rp,
color=cmap,
xlabel="BP-RP",
ylabel="Log(Luminosity/Lsun)",
title="HR Diagram",
legend=false,
reverse=true)

ylims!((minimum(log10.(luminosity)) - 0.5, maximum(log10.(luminosity)) + 0.5))

plot!()

0 comments on commit ad20390

Please sign in to comment.