Skip to content

Commit

Permalink
Merge pull request #724 from ie3-institute/ms/#180-refactor-WeatherSo…
Browse files Browse the repository at this point in the history
…urce-and-Wrapper

Refactor WeatherSource and WeatherSourceWrapper.
  • Loading branch information
danielfeismann authored Jul 11, 2024
2 parents 8486315 + fdef525 commit b714917
Show file tree
Hide file tree
Showing 10 changed files with 427 additions and 470 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Rewrote PVModelTest from groovy to scala [#646](https://github.com/ie3-institute/simona/issues/646)
- Making configuration of `RefSystem` via config optional [#769](https://github.com/ie3-institute/simona/issues/769)
- Updated PSDM to version 5.1.0 [#835](https://github.com/ie3-institute/simona/issues/835)
- Refactor `WeatherSource` and `WeatherSourceWrapper` [#180](https://github.com/ie3-institute/simona/issues/180)

### Fixed
- Removed a repeated line in the documentation of vn_simona config [#658](https://github.com/ie3-institute/simona/issues/658)
Expand Down
127 changes: 124 additions & 3 deletions src/main/scala/edu/ie3/simona/config/ConfigFailFast.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,26 @@ package edu.ie3.simona.config

import com.typesafe.config.{Config, ConfigException}
import com.typesafe.scalalogging.LazyLogging
import edu.ie3.simona.config.SimonaConfig.Simona.Input.Weather.Datasource.{
CouchbaseParams,
InfluxDb1xParams,
SampleParams,
SqlParams,
}
import edu.ie3.simona.config.SimonaConfig.Simona.Output.Sink.InfluxDb1x
import edu.ie3.simona.config.SimonaConfig._
import edu.ie3.simona.exceptions.InvalidConfigParameterException
import edu.ie3.simona.io.result.ResultSinkType
import edu.ie3.simona.model.participant.load.{LoadModelBehaviour, LoadReference}
import edu.ie3.simona.service.primary.PrimaryServiceProxy
import edu.ie3.simona.service.weather.WeatherSource
import edu.ie3.simona.service.weather.WeatherSource.WeatherScheme
import edu.ie3.simona.util.CollectionUtils
import edu.ie3.simona.util.ConfigUtil.CsvConfigUtil.checkBaseCsvParams
import edu.ie3.simona.util.ConfigUtil.DatabaseConfigUtil.{
checkCouchbaseParams,
checkInfluxDb1xParams,
checkKafkaParams,
checkSqlParams,
}
import edu.ie3.simona.util.ConfigUtil.{CsvConfigUtil, NotifierIdentifier}
import edu.ie3.util.scala.ReflectionTools
Expand Down Expand Up @@ -539,8 +548,120 @@ case object ConfigFailFast extends LazyLogging {
PrimaryServiceProxy.checkConfig(primary)

private def checkWeatherDataSource(
dataSourceConfig: SimonaConfig.Simona.Input.Weather.Datasource
): Unit = WeatherSource.checkConfig(dataSourceConfig)
weatherDataSourceCfg: SimonaConfig.Simona.Input.Weather.Datasource
): Unit = {
// check coordinate source
val definedCoordinateSource: String = checkCoordinateSource(
weatherDataSourceCfg.coordinateSource
)

/* Check, if the column scheme is supported */
if (!WeatherScheme.isEligibleInput(weatherDataSourceCfg.scheme))
throw new InvalidConfigParameterException(
s"The weather data scheme '${weatherDataSourceCfg.scheme}' is not supported. Supported schemes:\n\t${WeatherScheme.values
.mkString("\n\t")}"
)

// check weather source parameters
val supportedWeatherSources =
Set("influxdb1x", "csv", "sql", "couchbase", "sample")
val definedWeatherSources = Vector(
weatherDataSourceCfg.sampleParams,
weatherDataSourceCfg.csvParams,
weatherDataSourceCfg.influxDb1xParams,
weatherDataSourceCfg.couchbaseParams,
weatherDataSourceCfg.sqlParams,
).filter(_.isDefined)

// check that only one source is defined
if (definedWeatherSources.size > 1)
throw new InvalidConfigParameterException(
s"Multiple weather sources defined: '${definedWeatherSources.map(_.getClass.getSimpleName).mkString("\n\t")}'." +
s"Please define only one source!\nAvailable sources:\n\t${supportedWeatherSources.mkString("\n\t")}"
)

definedWeatherSources.headOption.flatten match {
case Some(baseCsvParams: BaseCsvParams) =>
checkBaseCsvParams(baseCsvParams, "WeatherSource")
case Some(params: CouchbaseParams) =>
checkCouchbaseParams(params)
case Some(InfluxDb1xParams(database, _, url)) =>
checkInfluxDb1xParams("WeatherSource", url, database)
case Some(params: SqlParams) =>
checkSqlParams(params)
case Some(_: SampleParams) =>
// sample weather, no check required
// coordinate source must be sample coordinate source
if (weatherDataSourceCfg.coordinateSource.sampleParams.isEmpty) {
// cannot use sample weather source with other combination of weather source than sample weather source
throw new InvalidConfigParameterException(
s"Invalid coordinate source " +
s"'$definedCoordinateSource' defined for SampleWeatherSource. " +
"Please adapt the configuration to use sample coordinate source for weather data!"
)
}
case None | Some(_) =>
throw new InvalidConfigParameterException(
s"No weather source defined! This is currently not supported! Please provide the config parameters for one " +
s"of the following weather sources:\n\t${supportedWeatherSources.mkString("\n\t")}"
)
}
}

/** Check the provided coordinate id data source configuration to ensure its
* validity. For any invalid configuration parameters exceptions are thrown.
*
* @param coordinateSourceConfig
* the config to be checked
* @return
* the name of the defined
* [[edu.ie3.datamodel.io.source.IdCoordinateSource]]
*/
private def checkCoordinateSource(
coordinateSourceConfig: SimonaConfig.Simona.Input.Weather.Datasource.CoordinateSource
): String = {
val supportedCoordinateSources = Set("csv", "sql", "sample")
val definedCoordSources = Vector(
coordinateSourceConfig.sampleParams,
coordinateSourceConfig.csvParams,
coordinateSourceConfig.sqlParams,
).filter(_.isDefined)

// check that only one source is defined
if (definedCoordSources.size > 1)
throw new InvalidConfigParameterException(
s"Multiple coordinate sources defined: '${definedCoordSources.map(_.getClass.getSimpleName).mkString("\n\t")}'." +
s"Please define only one source!\nAvailable sources:\n\t${supportedCoordinateSources.mkString("\n\t")}"
)

definedCoordSources.headOption.flatten match {
case Some(baseCsvParams: BaseCsvParams) =>
checkBaseCsvParams(baseCsvParams, "CoordinateSource")

// check the grid model configuration
val gridModel = coordinateSourceConfig.gridModel.toLowerCase
if (gridModel != "icon" && gridModel != "cosmo") {
throw new InvalidConfigParameterException(
s"Grid model '$gridModel' is not supported!"
)
}

"csv"
case Some(sqlParams: SqlParams) =>
checkSqlParams(sqlParams)
"sql"
case Some(
_: SimonaConfig.Simona.Input.Weather.Datasource.CoordinateSource.SampleParams
) =>
"sample"
case None | Some(_) =>
throw new InvalidConfigParameterException(
s"No coordinate source defined! This is currently not supported! Please provide the config parameters for one " +
s"of the following coordinate sources:\n\t${supportedCoordinateSources.mkString("\n\t")}"
)
}

}

/** Check the config sub tree for output parameterization
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,13 @@ object SampleWeatherSource {
}

override def findCornerPoints(
point: Point,
coordinate: Point,
distance: ComparableQuantity[Length],
): util.List[CoordinateDistance] = {
// just a dummy implementation, because this is just a sample weather source
getClosestCoordinates(point, 4, distance)
}
): util.List[CoordinateDistance] =
findCornerPoints(
coordinate,
getClosestCoordinates(coordinate, 9, distance),
)

override def validate(): Unit = {
/* nothing to do here */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,7 @@ final case class WeatherService(
): Try[(WeatherInitializedStateData, Option[Long])] =
initServiceData match {
case InitWeatherServiceStateData(sourceDefinition) =>
val weatherSource =
WeatherSource(sourceDefinition, simulationStart)
val weatherSource = WeatherSource(sourceDefinition)

/* What is the first tick to be triggered for? And what are further activation ticks */
val (maybeNextTick, furtherActivationTicks) = SortedDistinctSeq(
Expand Down
Loading

0 comments on commit b714917

Please sign in to comment.