Skip to content

Commit

Permalink
[api]: Implement Siril Plate Solver
Browse files Browse the repository at this point in the history
  • Loading branch information
tiagohm committed Jun 12, 2024
1 parent 4136c21 commit 17f5b9f
Show file tree
Hide file tree
Showing 16 changed files with 370 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package nebulosa.astrometrynet.platesolver

import nebulosa.common.concurrency.cancel.CancellationToken
import nebulosa.common.exec.LineReadListener
import nebulosa.common.exec.CommandLineListener
import nebulosa.common.exec.commandLine
import nebulosa.image.Image
import nebulosa.log.loggerFor
Expand Down Expand Up @@ -61,7 +61,7 @@ data class LocalAstrometryNetPlateSolver(private val executablePath: Path) : Pla

try {
cancellationToken.listen(cmd)
cmd.registerLineReadListener(solution)
cmd.registerCommandLineListener(solution)
cmd.start()
LOG.info("astrometry.net exited. code={}", cmd.get())
return solution.get()
Expand All @@ -74,14 +74,14 @@ data class LocalAstrometryNetPlateSolver(private val executablePath: Path) : Pla
}
}

private class PlateSolutionLineReader : LineReadListener.OnInput, Supplier<PlateSolution> {
private class PlateSolutionLineReader : CommandLineListener.OnLineRead, Supplier<PlateSolution> {

@Volatile private var fieldCenter: DoubleArray? = null
@Volatile private var fieldRotation: Angle = 0.0
@Volatile private var pixelScale: Angle = 0.0
@Volatile private var fieldSize: DoubleArray? = null

override fun onInputRead(line: String) {
override fun onLineRead(line: String) {
fieldCenter(line)?.also { fieldCenter = it }
?: fieldRotation(line)?.also { fieldRotation = it }
?: pixelScale(line)?.also { pixelScale = it }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ data class CommandLine internal constructor(

process.destroyForcibly()
process.waitFor()

listeners.clear()
}

fun get(timeout: Duration): Int {
Expand Down
22 changes: 12 additions & 10 deletions nebulosa-fits/src/main/kotlin/nebulosa/fits/FitsHeader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import nebulosa.io.SeekableSource
import nebulosa.io.source
import nebulosa.log.loggerFor
import java.util.*
import java.util.function.Predicate

open class FitsHeader : AbstractHeader {

Expand Down Expand Up @@ -54,15 +55,6 @@ open class FitsHeader : AbstractHeader {
FitsHeaderCard.create(key, value, comment).also(::add)
}

override fun add(card: HeaderCard) {
if (!card.isKeyValuePair) cards.add(card)
else {
val index = cards.indexOfFirst { it.key == card.key }
if (index >= 0) cards[index] = card
else cards.add(card)
}
}

open class ReadOnly : FitsHeader {

constructor() : super(LinkedList<HeaderCard>())
Expand All @@ -87,10 +79,20 @@ open class FitsHeader : AbstractHeader {

final override fun add(key: String, value: String, comment: String) = Unit

final override fun add(card: HeaderCard) = Unit
final override fun add(element: HeaderCard) = false

final override fun addAll(cards: Iterable<HeaderCard>) = Unit

final override fun addAll(elements: Collection<HeaderCard>) = false

final override fun removeIf(filter: Predicate<in HeaderCard>) = false

final override fun remove(element: HeaderCard) = false

final override fun removeAll(elements: Collection<HeaderCard>) = false

final override fun retainAll(elements: Collection<HeaderCard>) = false

final override fun delete(key: HeaderKey) = false

final override fun delete(key: String) = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,27 @@ import java.io.Serializable
import java.util.*

abstract class AbstractHeader protected constructor(@JvmField protected val cards: LinkedList<HeaderCard>) :
Header, Collection<HeaderCard> by cards, Serializable {
Header, MutableCollection<HeaderCard> by cards, Serializable {

constructor() : this(LinkedList<HeaderCard>())

constructor(cards: Collection<HeaderCard>) : this(LinkedList(cards))

abstract fun readOnly(): Header

override fun clear() {
cards.clear()
override fun add(element: HeaderCard): Boolean {
return if (!element.isKeyValuePair) {
cards.add(element)
} else {
val index = cards.indexOfFirst { it.key == element.key }

if (index >= 0) {
cards[index] = element
true
} else {
cards.add(element)
}
}
}

override fun delete(key: String): Boolean {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package nebulosa.image.format

import java.util.function.Predicate

interface Header : ReadableHeader, WritableHeader, Cloneable {

public override fun clone(): Header

data object Empty : Header, Iterator<HeaderCard> {
data object Empty : Header, MutableIterator<HeaderCard> {

override fun clone() = this

Expand All @@ -20,6 +22,12 @@ interface Header : ReadableHeader, WritableHeader, Cloneable {

override fun clear() = Unit

override fun remove(element: HeaderCard) = false

override fun removeAll(elements: Collection<HeaderCard>) = false

override fun retainAll(elements: Collection<HeaderCard>) = false

override fun add(key: String, value: Boolean, comment: String) = Unit

override fun add(key: String, value: Int, comment: String) = Unit
Expand All @@ -28,12 +36,18 @@ interface Header : ReadableHeader, WritableHeader, Cloneable {

override fun add(key: String, value: String, comment: String) = Unit

override fun add(card: HeaderCard) = Unit
override fun add(element: HeaderCard) = false

override fun addAll(elements: Collection<HeaderCard>) = false

override fun delete(key: String) = false

override fun hasNext() = false

override fun next() = TODO("Unsupported operation")

override fun remove() = Unit

override fun removeIf(filter: Predicate<in HeaderCard>) = false
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package nebulosa.image.format

interface WritableHeader {

fun clear()
interface WritableHeader : MutableCollection<HeaderCard> {

fun add(key: HeaderKey, value: Boolean) = add(key.key, value, key.comment)

Expand All @@ -20,8 +18,6 @@ interface WritableHeader {

fun add(key: String, value: String, comment: String = "")

fun add(card: HeaderCard)

fun addAll(cards: Iterable<HeaderCard>) = cards.forEach(::add)

fun delete(key: HeaderKey) = delete(key.key)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ import nebulosa.image.format.ReadableHeader
import nebulosa.log.loggerFor
import nebulosa.math.*
import nebulosa.wcs.computeCdMatrix
import kotlin.math.abs
import kotlin.math.atan2
import kotlin.math.cos
import kotlin.math.hypot
import kotlin.math.*

data class PlateSolution(
@JvmField val solved: Boolean = false,
Expand All @@ -22,8 +19,8 @@ data class PlateSolution(
@JvmField val height: Angle = 0.0,
@JvmField val parity: Parity = Parity.NORMAL,
@JvmField val radius: Angle = hypot(width, height).rad / 2.0,
@JvmField val widthInPixels: Double = width / scale,
@JvmField val heightInPixels: Double = height / scale,
@JvmField val widthInPixels: Double = truncate(width / scale),
@JvmField val heightInPixels: Double = truncate(height / scale),
private val header: Collection<HeaderCard> = emptyList(),
) : FitsHeader.ReadOnly(header) {

Expand Down
1 change: 1 addition & 0 deletions nebulosa-siril/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ dependencies {
api(project(":nebulosa-common"))
api(project(":nebulosa-math"))
api(project(":nebulosa-livestacker"))
api(project(":nebulosa-platesolver"))
implementation(project(":nebulosa-log"))
testImplementation(project(":nebulosa-test"))
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package nebulosa.siril.command

import nebulosa.common.concurrency.latch.CountUpDownLatch
import nebulosa.common.exec.CommandLineListener
import nebulosa.fits.FitsHeader
import nebulosa.fits.FitsHeaderCard
import nebulosa.image.format.Header
import nebulosa.log.debug
import nebulosa.log.loggerFor
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicBoolean

data class DumpHeader(private val header: Header = FitsHeader()) : SirilCommand<Header>, CommandLineListener {

private val latch = CountUpDownLatch(1)
private val started = AtomicBoolean()
private val finished = AtomicBoolean()

override fun onLineRead(line: String) {
if (finished.get()) return

if (started.get()) {
val card = line.replaceFirst("log: ", "").trimStart()

try {
with(FitsHeaderCard.from(card)) {
header.add(this)

LOG.debug { line }

if (key == "END") {
finished.set(true)
latch.reset()
}
}
} catch (ignored: Throwable) {
}
} else if (line.contains("FITS header for currently loaded image", true)) {
started.set(true)
}
}

override fun write(commandLine: SirilCommandLine): Header {
return try {
commandLine.registerCommandLineListener(this)
commandLine.write("dumpheader")
latch.await(15, TimeUnit.SECONDS)
header
} finally {
commandLine.unregisterCommandLineListener(this)
}
}

companion object {

@JvmStatic private val LOG = loggerFor<DumpHeader>()
}
}
3 changes: 3 additions & 0 deletions nebulosa-siril/src/main/kotlin/nebulosa/siril/command/Exit.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package nebulosa.siril.command

/**
* Quits the application.
*/
data object Exit : SirilCommand<Unit> {

override fun write(commandLine: SirilCommandLine) {
Expand Down
Loading

0 comments on commit 17f5b9f

Please sign in to comment.