-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
293 lines (225 loc) · 8.61 KB
/
main.go
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
/*****************************************************************************************************************/
// @author Michael Roberts <[email protected]>
// @package @observerly/skysolve
// @license Copyright © 2021-2025 observerly
/*****************************************************************************************************************/
package main
/*****************************************************************************************************************/
import (
"fmt"
"image"
"image/color"
"image/png"
"math"
"os"
"time"
"github.com/fogleman/gg"
"github.com/observerly/iris/pkg/fits"
"github.com/observerly/skysolve/pkg/astrometry"
"github.com/observerly/skysolve/pkg/catalog"
"github.com/observerly/skysolve/pkg/fov"
"github.com/observerly/skysolve/pkg/solve"
)
/*****************************************************************************************************************/
func main() {
// Attempt to open the file from the given filepath:
file, err := os.Open("./samples/Rosetta_Nebula_[Ha]_Monochrome_M_300s_2024-11-26T17_20_00Z.fits")
if err != nil {
fmt.Printf("failed to open file: %v", err)
return
}
// Defer closing the file:
defer file.Close()
// Assume an image of 2x2 pixels with 16-bit depth, and no offset:
fit := fits.NewFITSImage(2, 0, 0, 65535)
// Read in our exposure data into the image:
err = fit.Read(file)
if err != nil {
fmt.Printf("failed to read file: %v", err)
return
}
// Attempt to get the RA header from the FITS file:
ra, exists := fit.Header.Floats["RA"]
if !exists {
fmt.Printf("ra header not found")
return
}
fmt.Println("RA:", ra)
// Attempt to get the Dec header from the FITS file:
dec, exists := fit.Header.Floats["DEC"]
if !exists {
fmt.Printf("dec header not found")
return
}
fmt.Println("Dec:", dec)
pixelScaleX := 0.000540 // 0.000540 degrees per pixel in the x-axis
pixelScaleY := 0.000540 // 0.000540 degrees per pixel in the y-axis
radius := fov.GetRadialExtent(float64(fit.Header.Naxis1), float64(fit.Header.Naxis2), fov.PixelScale{X: pixelScaleX, Y: pixelScaleY})
// Create a new SIMBAD service client:
service := catalog.NewCatalogService(catalog.SIMBAD, catalog.Params{
Limit: 100, // Limit the number of records to 48
Threshold: 16, // Limiting Magntiude, filter out any stars that are magnitude 16 or above (fainter)
})
fmt.Println("Radius:", radius)
// Attempt to create a new PlateSolver:
solver, err := solve.NewPlateSolver(solve.Params{
Data: fit.Data, // The exposure data from the fits image
Width: int(fit.Header.Naxis1), // The width of the image
Height: int(fit.Header.Naxis2), // The height of the image
PixelScaleX: pixelScaleX, // The pixel scale in the x-axis
PixelScaleY: pixelScaleY, // The pixel scale in the y-axis
ADU: fit.ADU, // The analog-to-digital unit of the image
ExtractionThreshold: 16, // Extract a minimum of 20 of the brightest stars
Radius: 16, // 16 pixels radius for the star extraction
Sigma: 2.5, // 8 pixels sigma for the Gaussian kernel
})
if err != nil {
fmt.Printf("there was an error while creating the plate solver: %v", err)
return
}
// Define the tolerances for the solver, we can adjust these as needed:
tolerance := solve.ToleranceParams{
QuadTolerance: 0.02,
EuclidianPixelTolerance: 10,
}
// Whilst we have no matches, and whilst we are within 1 degree of the initial { ra, dec } guess, keep solving:
eq := astrometry.ICRSEquatorialCoordinate{
RA: 98.6,
Dec: 2.5,
}
// Perform a radial search with the given center and radius, for all sources with a magnitude less than 10:
sources, err := service.PerformRadialSearch(eq, radius)
if err != nil {
fmt.Printf("there was an error while performing the SIMBAD radial search: %v", err)
return
}
// Record the start time
startTime := time.Now()
solver.Sources = append(solver.Sources, sources...)
fmt.Println("Number of Sources:", len(solver.Sources))
wcs, matches, err := solver.Solve(tolerance, 3)
fmt.Println("Number of Matches:", len(matches))
// Calculate the elapsed time
elapsedTime := time.Since(startTime)
fmt.Println(elapsedTime)
if err != nil {
fmt.Println("an error occured while plate solving:", err)
}
if wcs == nil {
fmt.Println("no WCS solution found")
}
if wcs != nil {
fmt.Println(wcs)
eq = wcs.PixelToEquatorialCoordinate(float64(solver.Width)/2, float64(solver.Height)/2)
fmt.Println(float64(solver.Width)/2, float64(solver.Height)/2)
fmt.Println(
"Expected RA 98.2746 (deg), got:", eq.RA,
"Expected Dec 2.637267 (deg), got:", eq.Dec,
)
eq = wcs.PixelToEquatorialCoordinate(578.234176636, 485.578643799)
fmt.Println(
"Expected RA 98.647 (deg), got:", eq.RA,
"Expected Dec 2.537 (deg), got:", eq.Dec,
)
eq = wcs.PixelToEquatorialCoordinate(0, 0)
fmt.Println("Top Left Corner:", eq)
eq = wcs.PixelToEquatorialCoordinate(float64(solver.Width), float64(solver.Height))
fmt.Println("Bottom Right Corner:", eq)
}
height := int(solver.Height)
width := int(solver.Width)
// // 2. Validate Image Dimensions
if width <= 0 || height <= 0 {
fmt.Println("invalid image dimensions: width and height must be greater than zero")
return
}
expectedDataLength := width * height
if len(solver.Data) < expectedDataLength {
fmt.Printf("invalid data length: expected at least %d pixels, got %d\n", expectedDataLength, len(solver.Data))
return
}
// a. Find min and max values for normalization
minVal, maxVal := solver.Data[0], solver.Data[0]
for _, pixel := range solver.Data {
if pixel < minVal {
minVal = pixel
}
if pixel > maxVal {
maxVal = pixel
}
}
if maxVal == minVal {
maxVal = minVal + 1 // Prevent division by zero
}
// Debugging Statements
fmt.Printf("Pixel Value Range: min=%f, max=%f\n", minVal, maxVal)
// Create a new grayscale image
imgGray := image.NewGray(image.Rect(0, 0, width, height))
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
index := y*width + x
if index >= len(solver.Data) {
// This should not happen due to earlier validation
break
}
normalized := (solver.Data[index] - minVal) / (maxVal - minVal)
if math.IsNaN(float64(normalized)) || math.IsInf(float64(normalized), 0) {
normalized = 0
}
gray := uint8(math.Round(float64(normalized) * 255))
imgGray.SetGray(x, y, color.Gray{Y: gray})
}
}
fmt.Println("Image Dimensions:", width, height)
fmt.Println("Initializing Drawing Context...")
// // c. Initialize the drawing context
dc := gg.NewContext(width, height)
fmt.Println("Drawing Image...")
// d. Draw the grayscale image onto the context
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
gray := imgGray.GrayAt(x, y).Y
dc.SetRGB(float64(gray)/255, float64(gray)/255, float64(gray)/255)
dc.SetPixel(x, y)
}
}
// fmt.Println("Drawing Stars...")
for _, star := range solver.Stars {
dc.SetColor(color.RGBA{R: 241, G: 245, B: 249, A: 255})
dc.DrawCircle(float64(star.X), float64(star.Y), 16.0)
dc.SetLineWidth(2)
dc.Stroke()
}
for _, match := range matches {
dc.SetColor(color.RGBA{R: 129, G: 140, B: 248, A: 255})
dc.DrawCircle(float64(match.Quad.A.X), float64(match.Quad.A.Y), 20.0)
dc.DrawCircle(float64(match.Quad.B.X), float64(match.Quad.B.Y), 20.0)
dc.DrawCircle(float64(match.Quad.C.X), float64(match.Quad.C.Y), 20.0)
dc.DrawCircle(float64(match.Quad.D.X), float64(match.Quad.D.Y), 20.0)
dc.SetLineWidth(2)
dc.Stroke()
dc.SetColor(color.RGBA{R: 255, G: 255, B: 255, A: 255})
dc.DrawString(match.Quad.A.Designation, float64(match.Quad.A.X), float64(match.Quad.A.Y)-30)
dc.DrawString(match.Quad.B.Designation, float64(match.Quad.B.X), float64(match.Quad.B.Y)-30)
dc.DrawString(match.Quad.C.Designation, float64(match.Quad.C.X), float64(match.Quad.C.Y)-30)
dc.DrawString(match.Quad.D.Designation, float64(match.Quad.D.X), float64(match.Quad.D.Y)-30)
dc.SetLineWidth(10)
dc.Stroke()
}
fmt.Println("Saving Image...")
// Save the annotated image as PNG:
outputPath := "samples/Rosetta_Nebula_[Ha]_Monochrome_M_300s_2024-11-26T17_20_00Z.png"
outputFile, err := os.Create(outputPath)
if err != nil {
fmt.Println("error creating output file:", err)
return
}
defer outputFile.Close()
err = png.Encode(outputFile, dc.Image())
if err != nil {
fmt.Println("error encoding PNG:", err)
return
}
fmt.Printf("Annotated image saved as %s\n", outputPath)
}
/*****************************************************************************************************************/