-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ENH: Add CIM/XML support from zipped archive
- Loading branch information
1 parent
6e4fa62
commit 3e3d52f
Showing
9 changed files
with
194 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.github.statnett.loadflowservice | ||
|
||
import com.powsybl.commons.datasource.DataSourceUtil | ||
import com.powsybl.commons.datasource.ReadOnlyMemDataSource | ||
import java.io.ByteArrayInputStream | ||
import java.security.MessageDigest | ||
import java.util.zip.ZipInputStream | ||
|
||
class FileContent(val name: String, val bytes: ByteArray) { | ||
fun contentHash(): String { | ||
val md = MessageDigest.getInstance("MD5") | ||
return md.digest(this.bytes).joinToString(separator = "") { eachByte -> "%02x".format(eachByte) } | ||
} | ||
|
||
fun contentAsStream(): ByteArrayInputStream { | ||
return ByteArrayInputStream(this.bytes) | ||
} | ||
|
||
fun asReadOnlyMemDataSource(): ReadOnlyMemDataSource { | ||
if (name.endsWith(".zip")) { | ||
return zippedArchiveReadOnlyMemDataSource() | ||
} | ||
return singleFileReadOnlyMemDataSource() | ||
} | ||
|
||
private fun singleFileReadOnlyMemDataSource(): ReadOnlyMemDataSource { | ||
val dataSource = ReadOnlyMemDataSource(DataSourceUtil.getBaseName(name)) | ||
dataSource.putData(name, bytes) | ||
return dataSource | ||
} | ||
|
||
private fun zippedArchiveReadOnlyMemDataSource(): ReadOnlyMemDataSource { | ||
val dataSource = ReadOnlyMemDataSource(DataSourceUtil.getBaseName((name))) | ||
ZipInputStream(contentAsStream()) | ||
.use { stream -> | ||
generateSequence { stream.nextEntry } | ||
.filterNot { entry -> entry.isDirectory } | ||
.forEach { entry -> | ||
dataSource.putData(entry.name, stream.readAllBytes()) | ||
} | ||
} | ||
return dataSource | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import com.powsybl.ieeecdf.converter.IeeeCdfNetworkFactory | ||
import io.ktor.client.request.forms.* | ||
import io.ktor.http.* | ||
import io.ktor.http.content.* | ||
import java.io.File | ||
import java.io.FileOutputStream | ||
import java.nio.file.Paths | ||
import java.util.* | ||
import java.util.zip.ZipEntry | ||
import java.util.zip.ZipOutputStream | ||
|
||
fun ieeeCdfNetwork14CgmesFile(): File { | ||
val file = File.createTempFile("network_cgmes", ".zip") | ||
file.deleteOnExit() | ||
|
||
IeeeCdfNetworkFactory.create14().write("CGMES", null, Paths.get(file.path.replace(".zip", ""))) | ||
|
||
// Read the produced files in EQ/SSH/SV/TP and create one zip archive | ||
val withOutExtension = file.path.toString().replace(".zip", "") | ||
val cimXmlFiles = listOf("EQ", "TP", "SV", "SSH").map { profile -> "${withOutExtension}_${profile}.xml" } | ||
|
||
val fileOutputStream = FileOutputStream(file) | ||
val zipOutputStream = ZipOutputStream(fileOutputStream) | ||
|
||
|
||
cimXmlFiles.forEach { cimXmlFile -> addToArchiveAndDeleteFile(cimXmlFile, zipOutputStream) } | ||
zipOutputStream.close() | ||
return file | ||
} | ||
|
||
fun addToArchiveAndDeleteFile(filename: String, outStream: ZipOutputStream) { | ||
val pattern = Regex(pattern = """([^/]+$)""") // Extract everything after the last slash | ||
val match = pattern.find(filename)!! | ||
val baseName = match.groupValues[1] | ||
val zipEntry = ZipEntry(baseName) | ||
outStream.putNextEntry(zipEntry) | ||
val cimFile = File(filename) | ||
val bytes = cimFile.readBytes() | ||
outStream.write(bytes, 0, bytes.size) | ||
outStream.closeEntry() | ||
cimFile.delete() | ||
} | ||
|
||
fun formDataMinimalNetworkRawx(): List<PartData> { | ||
return formData { | ||
append( | ||
"network", | ||
minimalRawx(), | ||
Headers.build { | ||
append(HttpHeaders.ContentDisposition, "filename=network.rawx") | ||
} | ||
) | ||
} | ||
} | ||
|
||
fun ieeeCdfNetwork14File(): File { | ||
// Initialize temporary file | ||
val file = File.createTempFile("network", ".xiidm") | ||
file.deleteOnExit() | ||
|
||
IeeeCdfNetworkFactory.create14().write("XIIDM", Properties(), Paths.get(file.path)) | ||
return file | ||
} | ||
|
||
fun minimalRawx(): ByteArray { | ||
return ("{\"network\":{\"caseid\":{" + | ||
"\"fields\":[\"ic\",\"sbase\",\"rev\",\"xfrrat\",\"nxfrat\",\"basfrq\",\"title1\"]," + | ||
"\"data\":[0,100.00,35,0,0,60.00,\"PSS(R)EMinimumRAWXCase\"]}," + | ||
"\"bus\":{\"fields\":[\"ibus\",\"name\",\"baskv\",\"ide\"]," + | ||
"\"data\":[[1,\"Slack-Bus\",138.0,3],[2,\"Load-Bus\",138.01]]}," + | ||
"\"load\":{\"fields\":[\"ibus\",\"loadid\",\"stat\",\"pl\",\"ql\"]," + | ||
"\"data\":[[2,\"1\",1,40.0,15.0]]}," + | ||
"\"generator\":{\"fields\":[\"ibus\",\"machid\",\"pg\",\"qg\"]," + | ||
"\"data\":[[1,\"1\",\"40.35\",\"10.87\"]]}," + | ||
"\"acline\":{\"fields\":[\"ibus\",\"jbus\",\"ckt\",\"rpu\",\"xpu\",\"bpu\"]," + | ||
"\"data\":[[1,2,\"1\",0.01938,0.05917,0.05280]]}}}").toByteArray() | ||
} | ||
|
||
fun formDataWithEmptyNetwork(): List<PartData> { | ||
return formData { | ||
append( | ||
"network", | ||
byteArrayOf(), | ||
Headers.build { | ||
append(HttpHeaders.ContentDisposition, "filename=emptyFile.xiidm") | ||
} | ||
) | ||
} | ||
} | ||
|
||
fun formDataFromFile(file: File): List<PartData> { | ||
return formData { | ||
append( | ||
"network", | ||
file.readBytes(), | ||
Headers.build { | ||
append(HttpHeaders.ContentDisposition, "filename=${file.name}") | ||
}, | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters