Skip to content

Commit

Permalink
Merge pull request #816 from gemini-hlsw/guide-star-name
Browse files Browse the repository at this point in the history
  • Loading branch information
toddburnside authored Aug 28, 2024
2 parents 461be54 + 37f11f8 commit ba4f7de
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 10 deletions.
1 change: 1 addition & 0 deletions .scalafix.conf
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
include ".scalafix-common.conf"

OrganizeImports.removeUnused = false
OrganizeImports.targetDialect = Scala3
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ lazy val kittensVersion = "3.4.0"

Global / onChangedBuildSource := ReloadOnSourceChanges

ThisBuild / tlBaseVersion := "0.47"
ThisBuild / tlBaseVersion := "0.48"
ThisBuild / tlCiReleaseBranches := Seq("master", "scala3")

ThisBuild / scalaVersion := "3.4.3"
Expand Down
11 changes: 2 additions & 9 deletions modules/ags/src/main/scala/lucuma/ags/GuideStarCandidate.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import coulomb.*
import coulomb.syntax.*
import eu.timepit.refined.*
import eu.timepit.refined.cats.*
import eu.timepit.refined.collection.NonEmpty
import eu.timepit.refined.types.string.NonEmptyString
import lucuma.catalog.BandsList
import lucuma.core.enums.Band
Expand Down Expand Up @@ -43,8 +42,7 @@ case class GuideStarCandidate private (
gBrightness: Option[(Band, BrightnessValue)]
) derives Eq {

def name: NonEmptyString =
refineV[NonEmpty](s"Gaia DR3 $id").getOrElse(sys.error("Cannot happen"))
def name: NonEmptyString = GuideStarName.gaiaSourceId.reverseGet(id).toNonEmptyString

// Reset the candidate to a given instant
// This can be used to calculate and cache the location base on proper motion
Expand Down Expand Up @@ -79,8 +77,6 @@ object GuideStarCandidate {

val UTC = ZoneId.of("UTC")

val GaiaNameRegex = """Gaia DR3 (-?\d+)""".r

val id: Lens[GuideStarCandidate, Long] =
Focus[GuideStarCandidate](_.id)

Expand All @@ -103,10 +99,7 @@ object GuideStarCandidate {
.map { case (b, v) => (b, v.value) }

new GuideStarCandidate(
st.name.value match {
case GaiaNameRegex(d) => d.toLong
case _ => -1
},
GuideStarName.from(st.name.value).toOption.flatMap(_.toGaiaSourceId).getOrElse(-1),
st.tracking,
gBrightness
)
Expand Down
30 changes: 30 additions & 0 deletions modules/ags/src/main/scala/lucuma/ags/GuideStarName.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (c) 2016-2023 Association of Universities for Research in Astronomy, Inc. (AURA)
// For license information see LICENSE or https://opensource.org/licenses/BSD-3-Clause

package lucuma.ags

import eu.timepit.refined.string.MatchesRegex
import eu.timepit.refined.types.string.NonEmptyString
import lucuma.core.util.RefinedNewType
import monocle.Prism

import scala.util.matching.Regex

// For now, at least, we only support Gaia.
// The same regex used in the odb for a database check. When changing one, both should be updated.
private val regexStr = """^Gaia DR3 (-?\d+)$"""
type GuideStarNamePred = MatchesRegex[regexStr.type]

object GuideStarName extends RefinedNewType[String, GuideStarNamePred]:
def gaiaSourceId: Prism[GuideStarName, Long] =
Prism[GuideStarName, Long](_.toGaiaSourceId)(id => GuideStarName.unsafeFrom(s"Gaia DR3 $id"))

extension (self: GuideStarName)
def toGaiaSourceId: Option[Long] =
val regex = Regex(regexStr)
self.value.value match
case regex(d) => d.toLongOption
case _ => None
def toNonEmptyString: NonEmptyString = NonEmptyString.unsafeFrom(self.value.value)

type GuideStarName = GuideStarName.Type
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) 2016-2023 Association of Universities for Research in Astronomy, Inc. (AURA)
// For license information see LICENSE or https://opensource.org/licenses/BSD-3-Clause

package lucuma.ags.arb

import lucuma.ags.GuideStarName
import org.scalacheck.Arbitrary
import org.scalacheck.Cogen
import org.scalacheck.Gen

trait ArbGuideStarName:

given Arbitrary[GuideStarName] =
Arbitrary {
Gen
.frequency(
// Get the occasional name with too many digits
1 -> s"9${Long.MaxValue}",
20 -> Arbitrary.arbLong.arbitrary
)
.map(a => GuideStarName.unsafeFrom(s"Gaia DR3 $a"))
}

given Cogen[GuideStarName] =
Cogen[String].contramap(_.value.value)

object ArbGuideStarName extends ArbGuideStarName
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) 2016-2023 Association of Universities for Research in Astronomy, Inc. (AURA)
// For license information see LICENSE or https://opensource.org/licenses/BSD-3-Clause

package lucuma.ags

import cats.kernel.Eq
import cats.kernel.laws.discipline.EqTests
import eu.timepit.refined.cats.*
import io.circe.*
import io.circe.refined.*
import io.circe.testing.CodecTests
import io.circe.testing.instances.arbitraryJson
import lucuma.ags.arb.ArbGuideStarName.given
import monocle.law.discipline.PrismTests

class GuideStarNameSuite extends munit.DisciplineSuite {
test("typeclasses") {
checkAll("GuideStarName", EqTests[GuideStarName].eqv)
checkAll("GuideStarNameCodec", CodecTests[GuideStarName].codec)
checkAll("GuideStarName.from", PrismTests(GuideStarName.from))
checkAll("GuideStarName.gaiaSourceId", PrismTests(GuideStarName.gaiaSourceId))
}

}

0 comments on commit ba4f7de

Please sign in to comment.