Read forecast dates which are already downloaded
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
package data
|
||||
|
||||
import cats.effect.IO
|
||||
import fs2.io.file.{Files, Path}
|
||||
import grib.{Grib, GribParser}
|
||||
|
||||
import java.time.{ZoneId, ZonedDateTime}
|
||||
import java.time.format.DateTimeFormatter
|
||||
import scala.io.Source
|
||||
import scala.util.Try
|
||||
|
||||
object DataService {
|
||||
val FOLDER = "data"
|
||||
|
||||
def getFileList(): IO[List[String]] =
|
||||
Files[IO]
|
||||
.list(Path(FOLDER))
|
||||
.map(_.toString)
|
||||
.filter(_.endsWith(".grib"))
|
||||
.map(_.replace(s"$FOLDER/", ""))
|
||||
.compile
|
||||
.toList
|
||||
|
||||
def getGribStucture(fileName: String): IO[List[Grib]] = {
|
||||
val filePath = Path(s"$FOLDER/$fileName")
|
||||
|
||||
Files[IO].exists(filePath).flatMap {
|
||||
case true => GribParser.parseFile(filePath)
|
||||
case false => IO.raiseError(new Exception(s"File not found: $fileName"))
|
||||
}
|
||||
}
|
||||
|
||||
def getBinaryChunk(offset: Int, length: Int, fileName: String): IO[Array[Byte]] = {
|
||||
IO {
|
||||
val source = Source.fromFile(s"$FOLDER/$fileName", "ISO-8859-1")
|
||||
try {
|
||||
source.slice(offset, offset + length).map(_.toByte).toArray
|
||||
} finally {
|
||||
source.close()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
def getForecasts(): IO[List[(ZonedDateTime, ZonedDateTime)]] = {
|
||||
getFileList()
|
||||
.map { fileNameList =>
|
||||
fileNameList.flatMap(getTimeFromName)
|
||||
}
|
||||
}
|
||||
|
||||
private def getTimeFromName(filename: String): Option[(ZonedDateTime, ZonedDateTime)] = {
|
||||
Try {
|
||||
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HHmm'Z'").withZone(ZoneId.of("UTC"))
|
||||
val start = filename.slice(9, 25) // gets "2025-02-05T1500Z"
|
||||
val end = filename.slice(26, 42) // gets "2025-02-05T1800Z"
|
||||
(
|
||||
ZonedDateTime.parse(start, formatter),
|
||||
ZonedDateTime.parse(end, formatter)
|
||||
)
|
||||
}.toOption
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package data
|
||||
|
||||
import cats.effect.IO
|
||||
import cats.effect.unsafe.implicits.global
|
||||
import io.circe.syntax.EncoderOps
|
||||
|
||||
object DataServiceTest {
|
||||
def main(args: Array[String]): Unit = {
|
||||
val program = for {
|
||||
forecasts <- DataService.getForecasts()
|
||||
_ <- IO.println(forecasts.asJson)
|
||||
} yield ()
|
||||
|
||||
program.unsafeRunSync()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user