Skip to content

To/#181 reduced grid model #185

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,35 @@ dmypy.json
# profiling data
.prof

### macOS ###
# General
.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon


# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk

# End of https://www.toptal.com/developers/gitignore/api/python

# Ignore Gradle project-specific cache directory
Expand Down
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ dependencies {
// Linter Scala //
implementation "com.sksamuel.scapegoat:scalac-scapegoat-plugin_${scalaBinaryVersion}:1.4.11" // scala scapegoat
scalaCompilerPlugin "com.sksamuel.scapegoat:scalac-scapegoat-plugin_${scalaBinaryVersion}:1.4.11" // scala scapegoat

implementation 'com.opencsv:opencsv:5.5.2'
}

/* scapegoat hook configuration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,5 @@ case object GridConverter {
Set.empty[StorageInput].asJava,
statGenModelContainer.wecInputs.toSet.asJava
)

}
}
131 changes: 131 additions & 0 deletions src/main/scala/edu/ie3/powerFactory2psdm/io/IoUtils.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
* © 2021. TU Dortmund University,
* Institute of Energy Systems, Energy Efficiency and Energy Economics,
* Research group Distribution grid planning and operation
*/

package edu.ie3.powerFactory2psdm.io

import com.opencsv.CSVWriter
import com.typesafe.scalalogging.LazyLogging
import edu.ie3.datamodel.exceptions.SourceException
import edu.ie3.datamodel.io.naming.FileNamingStrategy
import edu.ie3.datamodel.io.source.csv.{
CsvGraphicSource,
CsvRawGridSource,
CsvTypeSource
}
import edu.ie3.powerFactory2psdm.model.RawPfGridModel
import io.circe.parser.decode
import io.circe.generic.auto._
import edu.ie3.datamodel.io.source.csv.CsvSystemParticipantSource
import edu.ie3.datamodel.io.source.csv.CsvThermalSource
import edu.ie3.datamodel.models.input.container.JointGridContainer
import scala.io.Source
import java.io.{BufferedWriter, File, FileWriter}
import scala.jdk.CollectionConverters.IterableHasAsJava
import scala.util.{Failure, Try}
import io.circe.parser._

object IoUtils extends LazyLogging {

/** Reads a json export of a PowerFactory grid and builds a
* [[RawPfGridModel]].
*
* @param gridFile
* path to json export
* @return
* a [[RawPfGridModel]] instance
*/
def parsePfGrid(gridFile: String): Option[RawPfGridModel] = {
val source =
Source.fromFile(gridFile)
val jsonString =
try source.mkString
finally source.close

decode[RawPfGridModel](jsonString) match {
case Left(error) =>
logger.error(error.getMessage())
None
case Right(decodingResult) =>
Some(decodingResult)
}
}

@deprecated("should be implemented within PSDM")
def parsePsdmGrid(
gridName: String,
csvSep: String,
folderPath: String,
namingStrategy: FileNamingStrategy
): JointGridContainer = {

/* Instantiating sources */
val typeSource = new CsvTypeSource(csvSep, folderPath, namingStrategy)
val rawGridSource =
new CsvRawGridSource(csvSep, folderPath, namingStrategy, typeSource)

val thermalSource =
new CsvThermalSource(csvSep, folderPath, namingStrategy, typeSource)
val systemParticipantSource = new CsvSystemParticipantSource(
csvSep,
folderPath,
namingStrategy,
typeSource,
thermalSource,
rawGridSource
)
val graphicsSource = new CsvGraphicSource(
csvSep,
folderPath,
namingStrategy,
typeSource,
rawGridSource
)

/* Loading models */
val rawGridElements = rawGridSource.getGridData.orElseThrow(() =>
new SourceException("Error during reading of raw grid data.")
)
val systemParticipants =
systemParticipantSource.getSystemParticipants.orElseThrow(() =>
new SourceException("Error during reading of system participant data.")
)
val graphicElements = graphicsSource.getGraphicElements.orElseThrow(() =>
new SourceException("Error during reading of graphic elements.")
)

new JointGridContainer(
gridName,
rawGridElements,
systemParticipants,
graphicElements
)
}

/** Credits to isomarcte:
* https://stackoverflow.com/questions/52666231/how-to-write-to-a-csv-file-in-scala
*/
def writeCsvFile(
fileName: String,
header: List[String],
rows: List[List[String]]
): Try[Unit] =
Try(new CSVWriter(new BufferedWriter(new FileWriter(fileName)))).flatMap(
(csvWriter: CSVWriter) =>
Try {
csvWriter.writeAll(
(header +: rows).map(_.toArray).asJava
)
csvWriter.close()
} match {
case f @ Failure(_) =>
Try(csvWriter.close()).recoverWith { case _ =>
f
}
case success =>
success
}
)
}
35 changes: 0 additions & 35 deletions src/main/scala/edu/ie3/powerFactory2psdm/io/PfGridParser.scala

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import edu.ie3.datamodel.io.sink.CsvFileSink
import edu.ie3.powerFactory2psdm.config.ConversionConfig
import edu.ie3.powerFactory2psdm.config.validate.ConfigValidator
import edu.ie3.powerFactory2psdm.converter.GridConverter
import edu.ie3.powerFactory2psdm.io.PfGridParser
import edu.ie3.powerFactory2psdm.io.IoUtils

import java.io.File
import edu.ie3.powerFactory2psdm.exception.io.GridParsingException
Expand All @@ -38,8 +38,8 @@ object RunConversion extends LazyLogging {
val exportedGridFile =
s"${new File(".").getCanonicalPath}/src/main/python/pfGridExport/pfGrid.json"
logger.info("Parsing the json grid file.")
val pfGrid: RawPfGridModel = PfGridParser
.parse(exportedGridFile)
val pfGrid: RawPfGridModel = IoUtils
.parsePfGrid(exportedGridFile)
.getOrElse(
throw GridParsingException("Parsing the Json grid file failed")
)
Expand Down
Loading