Fetch list of harmonie files
This commit is contained in:
@@ -1,9 +1,10 @@
|
|||||||
package fetchDMI
|
package fetchDMI
|
||||||
|
|
||||||
import cats.effect._
|
import cats.effect._
|
||||||
import fs2.io.file.{Files, Path}
|
import cats.implicits.toTraverseOps
|
||||||
|
import fs2.io.file.{CopyFlag, CopyFlags, Files, Path}
|
||||||
|
import grib.GribParser
|
||||||
import org.http4s._
|
import org.http4s._
|
||||||
import org.http4s.client.Client
|
|
||||||
import org.http4s.ember.client.EmberClientBuilder
|
import org.http4s.ember.client.EmberClientBuilder
|
||||||
import org.typelevel.log4cats.Logger
|
import org.typelevel.log4cats.Logger
|
||||||
import org.typelevel.log4cats.slf4j.Slf4jLogger
|
import org.typelevel.log4cats.slf4j.Slf4jLogger
|
||||||
@@ -41,11 +42,14 @@ class FetchService(log: Logger[IO]) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
private val baseUrl: IO[Uri] = IO(Uri.unsafeFromString(harmonieServerConfig.url))
|
private val baseUrl: IO[Uri] = IO(Uri.unsafeFromString(harmonieServerConfig.url))
|
||||||
private def makeRequest(client: Client[IO], url: Uri) = {
|
|
||||||
|
|
||||||
|
def fetchFromList(timeList: List[ZonedDateTime]): IO[List[String]] = {
|
||||||
|
timeList.traverse { time =>
|
||||||
|
fetchFromDateTime(time)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
def fetchFromDateTime(time: ZonedDateTime) = {
|
private def fetchFromDateTime(time: ZonedDateTime): IO[String] = {
|
||||||
EmberClientBuilder.default[IO].build.use { client =>
|
EmberClientBuilder.default[IO].build.use { client =>
|
||||||
for {
|
for {
|
||||||
base <- baseUrl
|
base <- baseUrl
|
||||||
@@ -55,18 +59,21 @@ class FetchService(log: Logger[IO]) {
|
|||||||
"api-key" -> harmonieServerConfig.api_key,
|
"api-key" -> harmonieServerConfig.api_key,
|
||||||
)
|
)
|
||||||
urlWithParams = Uri.unsafeFromString(s"${base.toString}?${queryParams.toString}")
|
urlWithParams = Uri.unsafeFromString(s"${base.toString}?${queryParams.toString}")
|
||||||
_ <- IO.println(urlWithParams)
|
|
||||||
|
|
||||||
request = Request[IO](Method.GET, urlWithParams)
|
request = Request[IO](Method.GET, urlWithParams)
|
||||||
|
|
||||||
_ <- Files[IO].createDirectories(Path("data"))
|
tmpPath = Path("data/tmp.grib")
|
||||||
|
|
||||||
_ <- client.stream(request)
|
_ <- client.stream(request)
|
||||||
.flatMap(_.body)
|
.flatMap(_.body)
|
||||||
.through(Files[IO].writeAll(Path("data/tmp.grib")))
|
.through(Files[IO].writeAll(tmpPath))
|
||||||
.compile
|
.compile
|
||||||
.drain
|
.drain
|
||||||
} yield ()
|
gribList <- GribParser.parseFile(tmpPath)
|
||||||
|
gribTime = gribList.head.time
|
||||||
|
fileName = Path(s"data/harmonie_${gribTime.referenceTime}_${gribTime.forecastTime}.grib".replace(":", ""))
|
||||||
|
_ <- Files[IO].move(tmpPath, fileName, CopyFlags.apply(CopyFlag.ReplaceExisting))
|
||||||
|
_ <- log.info(fileName.toString)
|
||||||
|
fileNameStr = fileName.toString
|
||||||
|
} yield fileNameStr
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,8 +10,10 @@ object FetchServiceTest {
|
|||||||
val program = for {
|
val program = for {
|
||||||
nowUTC <- IO(ZonedDateTime.now(ZoneOffset.UTC))
|
nowUTC <- IO(ZonedDateTime.now(ZoneOffset.UTC))
|
||||||
referenceTime = FileName.getClosestReferenceTime(nowUTC)
|
referenceTime = FileName.getClosestReferenceTime(nowUTC)
|
||||||
|
timeList = FileName.generateTimeList(referenceTime)
|
||||||
fetch <- FetchService.of
|
fetch <- FetchService.of
|
||||||
_ <- fetch.fetchFromDateTime(referenceTime)
|
nameList <- fetch.fetchFromList(timeList)
|
||||||
|
_ <- IO.println(nameList)
|
||||||
} yield ()
|
} yield ()
|
||||||
|
|
||||||
program.unsafeRunSync()
|
program.unsafeRunSync()
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ object GribParser {
|
|||||||
loop(0L, List.empty)
|
loop(0L, List.empty)
|
||||||
}
|
}
|
||||||
|
|
||||||
def parseGrib(path: Path, ptr: Long): IO[Grib] = {
|
private def parseGrib(path: Path, ptr: Long): IO[Grib] = {
|
||||||
for {
|
for {
|
||||||
res0 <- parse0(path, ptr)
|
res0 <- parse0(path, ptr)
|
||||||
(version, discipline, gribLength, len0) = res0
|
(version, discipline, gribLength, len0) = res0
|
||||||
|
|||||||
Reference in New Issue
Block a user