I am a beginner to R. For one of my project I was trying to find the position of stars in R language. The data are available in Gaia data archive and can be downloaded. Now, the thing is in Python I can import the data directly in Python code (https://astroquery.readthedocs.io/en/latest/gaia/gaia.html), is it possible to do the same in R?
I am currently running this code:
library(httr)
library(jsonlite)
library(xml2)
get_gaia_star_data <- function(star_name) {
# URL encode the star name for use in the query
star_name_encoded <- URLencode(star_name, reserved = TRUE)
# Construct the Gaia TAP query to find the star by name
query <- paste0(
"SELECT TOP 1 source_id, ra, dec ",
"FROM gaiadr2.gaia_source ",
"WHERE CONTAINS(POINT('ICRS', ra, dec), ",
"CIRCLE('ICRS', 0, 0, 180)) = 1 AND ",
"source_id IN (SELECT source_id FROM gaiadr2.gaia_source WHERE ",
"CONTAINS(POINT('ICRS', ra, dec), ",
"CIRCLE('ICRS', ", star_name_encoded, ")) = 1"
)
# URL for the Gaia Archive TAP service
base_url <- "https://gea.esac.esa.int/tap-server/tap"
# Construct the request body
body <- list(
REQUEST = "doQuery",
LANG = "ADQL",
FORMAT = "json",
PHASE = "RUN",
QUERY = query
)
# Perform the query
response <- tryCatch({
POST(paste0(base_url, "/sync"), body = body, encode = "form")
}, error = function(e) {
stop("Failed to connect to Gaia Archive. Error: ", e$message)
})
# Check if the request was successful
if (status_code(response) != 200) {
stop(paste("Failed to query Gaia Archive. HTTP status code:", status_code(response)))
}
# Parse the response content
content <- content(response, "text", encoding = "UTF-8")
data <- tryCatch({
fromJSON(content)
}, error = function(e) {
stop("Failed to parse JSON response.")
})
# Check if the response contains data
if (length(data$data) == 0) {
stop("No results found for the star name.")
}
# Extract RA and Dec from the response
ra <- as.numeric(data$data[[1]]$ra)
dec <- as.numeric(data$data[[1]]$dec)
# Return the coordinates as a list
return(list(
RA = ra,
Dec = dec
))
}
# Example usage
star_name <- "Sirius"
star_location <- get_gaia_star_data(star_name)
print(star_location)
but the HTTP status is 400 no matter what I do.
I have figured it out and also thanks to @Andre for his help. I have tried to keep it as similar as possible as we see in the case of python. Here is the code :
get_gaia_data <- function(query, col_n) {
# URL for the Gaia Archive TAP service
base_url <- "https://gea.esac.esa.int/tap-server/tap/sync"
# Construct the request body
body <- list(
REQUEST = "doQuery",
LANG = "ADQL",
FORMAT = "json",
PHASE = "RUN",
QUERY = query
)
# Perform the query
response <- tryCatch({
POST(base_url, body = body, encode = "form")
}, error = function(e) {
stop("Failed to connect to Gaia Archive. Error: ", e$message)
})
# Check if the request was successful
if (status_code(response) != 200) {
stop(paste("Failed to query Gaia Archive. HTTP status code:", status_code(response)))
}
# Parse the response content
content <- content(response, "text", encoding = "UTF-8")
data <- tryCatch({
fromJSON(content, flatten = TRUE)
}, error = function(e) {
stop("Failed to parse JSON response.")
})
# Check if the response contains data
if (length(data$data) == 0) {
stop("No results found for the query.")
}
# Convert to data frame and set correct column names
df <- as.data.frame(data$data)
colnames(df) <- col_n
return(df)
}
This code takes two argument. The first one is the ADQL string and the second one for the data column name. Here is an working example for HR diagram data.
query <- paste0(
"SELECT source_id, ra, dec, phot_bp_mean_mag, phot_rp_mean_mag, phot_g_mean_mag, parallax ",
"FROM external.gaiaedr3_gcns_main_1 ",
"WHERE parallax >50"
)
# Example usage: Query Gaia data around a specific RA, Dec, and radius, then plot the HR diagram
col_n <- c("source_id", "ra", "dec", "phot_bp_mean_mag", "phot_rp_mean_mag", "phot_g_mean_mag", "parallax")
gaia_data <- get_gaia_data(query, col_n)
print(head(gaia_data))
The example ADQL is taken from : See the first talk. If you plot as the rules you will find same plot.