Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add (g *GAIAServiceClient) PerformRadialSearch() to catalog module in @observerly/skysolve #12

Merged
merged 1 commit into from
Oct 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 134 additions & 0 deletions pkg/catalog/gaia.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,15 @@ package catalog

import (
"bytes"
"encoding/csv"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
"text/template"

"github.com/observerly/skysolve/pkg/astrometry"
)

/*****************************************************************************************************************/
Expand Down Expand Up @@ -93,3 +101,129 @@ func (g *GAIAServiceClient) Build() (string, error) {
}

/*****************************************************************************************************************/

func (g *GAIAServiceClient) PerformRadialSearch(eq astrometry.ICRSEquatorialCoordinate, radius float64, limit float64) ([]Source, error) {
// Set the query parameters:
g.Query.RA = eq.RA
g.Query.Dec = eq.Dec
g.Query.Radius = radius
g.Query.Limit = limit

// Construct the ADQL query from the template:
adqlQuery, err := g.Build()
if err != nil {
return nil, err
}

// Prepare the POST form data for the HTTP request:
formData := url.Values{}
formData.Set("REQUEST", "doQuery")
formData.Set("LANG", "ADQL")
formData.Set("FORMAT", "csv")
formData.Set("QUERY", adqlQuery)

// Send the HTTP request to the GAIA TAP service:
resp, err := http.PostForm(g.URI, formData)
if err != nil {
return nil, err
}
defer resp.Body.Close()

// Read the response body:
bodyBytes, _ := io.ReadAll(resp.Body)

// Check for HTTP errors and return the response body if not OK:
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("GAIA TAP query failed: %s", string(bodyBytes))
}

// Parse the CSV data from the response body:
records, err := csv.NewReader(bytes.NewReader(bodyBytes)).ReadAll()
if err != nil {
return nil, err
}

// Unmarshal the CSV data into our struct array:
var stars []Source

// Iterate over the records and extract the source star data, skipping the header row:
for _, record := range records[1:] {
ra, err := strconv.ParseFloat(fmt.Sprintf("%v", record[2]), 64)
if err != nil {
continue
}

dec, err := strconv.ParseFloat(fmt.Sprintf("%v", record[3]), 64)
if err != nil {
continue
}

pmra, err := strconv.ParseFloat(fmt.Sprintf("%v", record[4]), 64)
if err != nil {
continue
}

pmdec, err := strconv.ParseFloat(fmt.Sprintf("%v", record[5]), 64)
if err != nil {
continue
}

parallax, err := strconv.ParseFloat(fmt.Sprintf("%v", record[6]), 64)
if err != nil {
continue
}

flux, err := strconv.ParseFloat(fmt.Sprintf("%v", record[7]), 64)
if err != nil {
continue
}

mag, err := strconv.ParseFloat(fmt.Sprintf("%v", record[8]), 64)
if err != nil {
continue
}

// Create a new source star object:
star := Source{
UID: record[0],
Designation: record[1],
RA: ra,
Dec: dec,
ProperMotionRA: pmra,
ProperMotionDec: pmdec,
Parallax: parallax,
PhotometricGMeanFlux: flux,
PhotometricGMeanMagnitude: mag,
}

// Append the source star to the array:
stars = append(stars, star)
}

// Convert string fields to float64 for RA, Dec, and Magnitude:
for i, star := range stars {
ra, err := strconv.ParseFloat(fmt.Sprintf("%v", star.RA), 64)
if err != nil {
continue
}

dec, err := strconv.ParseFloat(fmt.Sprintf("%v", star.Dec), 64)
if err != nil {
continue
}

mag, err := strconv.ParseFloat(fmt.Sprintf("%v", star.PhotometricGMeanMagnitude), 64)
if err != nil {
continue
}

stars[i].RA = ra
stars[i].Dec = dec
stars[i].PhotometricGMeanMagnitude = mag
}

// Return the extracted source star data:
return stars, nil
}

/*****************************************************************************************************************/
65 changes: 65 additions & 0 deletions pkg/catalog/gaia_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*****************************************************************************************************************/

// @author Michael Roberts <michael@observerly.com>
// @package @observerly/skysolve
// @license Copyright © 2021-2025 observerly

/*****************************************************************************************************************/

package catalog

import (
"math"
"testing"

"github.com/observerly/skysolve/pkg/astrometry"
)

/*****************************************************************************************************************/

func radians(degrees float64) float64 {
return degrees * math.Pi / 180.0
}

/*****************************************************************************************************************/

func IsWithinICRSPolarRadius(ra, dec, r float64) bool {
// Clamp cosine to the valid range [-1, 1] to prevent NaN from math.Acos and
// calculate the angular distance in radians:
d := math.Acos(math.Max(-1.0, math.Min(1.0, math.Cos(radians(dec))*math.Cos(radians(ra)))))
// Determine if the angular distance is within the radius R
return d <= radians(r)
}

/*****************************************************************************************************************/

func TestQueryExecutedSuccessfully(t *testing.T) {
var q = NewGAIAServiceClient()

stars, err := q.PerformRadialSearch(astrometry.ICRSEquatorialCoordinate{
RA: 0,
Dec: 0,
}, 2.5, 10)

if err != nil {
t.Errorf("Failed to execute query: %v", err)
}

if len(stars) == 0 {
t.Errorf("No stars returned")
}

for _, star := range stars {
// Test that the star is within the search radius:
if !IsWithinICRSPolarRadius(star.RA, star.Dec, 2.5) {
t.Errorf("Star is not within the search radius")
}
}

// The GAIA catalog is expected to return a maximum of 116 stars for this query:
if len(stars) > 116 {
t.Errorf("Too many stars returned")
}
}

/*****************************************************************************************************************/