Commit 88cd2502 authored by Mohamed, Fawzi Roberto (fawzi)'s avatar Mohamed, Fawzi Roberto (fawzi)
Browse files

h5Tool: FHI-aims fixes

parent 831ddd86
Pipeline #37583 failed with stages
in 37 minutes and 33 seconds
......@@ -27,8 +27,10 @@ import java.nio.file.FileVisitResult
import java.nio.file.attribute.BasicFileAttributes
import com.typesafe.scalalogging.StrictLogging
import eu.nomad_lab.LocalEnv
import eu.nomad_lab.H5Lib
import scala.util.control.NonFatal
import scala.io.Source
import ucar.ma2
object H5Tool extends StrictLogging {
......@@ -217,8 +219,62 @@ object H5Tool extends StrictLogging {
println("to do")
}
def dosFix(calc: CalculationH5): Unit = {
val pName = calc.valueTable(Seq("section_calculation_info", "parser_id"))
if (!pName.isEmpty() && pName.first.stringValue == "FhiAimsParser2.0.0-24-gca640f0") {
val dosVals = calc.valueTable(Seq("section_run", "section_single_configuration_calculation", "section_dos", "dos_values"))
if (!dosVals.isEmpty()) {
for (dosV <- dosVals) {
val sectionDos: SectionH5 = dosV.parentSection
val singleConf: SectionH5 = sectionDos.parentSection.get
val sysTable: SectionTableH5 = singleConf.parentSection.get.table.subSectionTable("section_system")
val sectionSys: Option[SectionH5] = singleConf.maybeValue("single_configuration_calculation_to_system_ref").map { v =>
sysTable(v.longValue)
}
sectionSys.map { v =>
val cell: Option[Seq[Double]] = (v.maybeValue("simulation_cell") match {
case Some(c) => Some(c)
case None =>
v.maybeValue("lattice_vectors")
}).map { vv =>
vv.seqDoubleValue()
}
val cellVol: Option[Double] = cell.map { c =>
Math.abs(c(0) * c(4) * c(8) + c(1) * c(5) * c(6) + c(2) * c(3) * c(7)
- c(2) * c(4) * c(6) - c(1) * c(3) * c(8) - c(0) * c(5) * c(7))
}
for (vol <- cellVol) {
val dosA = dosV.arrayValue()
for (i <- 0 to (dosA.getSize.toInt)) {
dosA.setDouble(i, dosA.getDouble(i) / vol)
}
val dosT = dosV.table
val indexDatasetId = dosT.openIndexDataset(create = false)
try {
val (valDatasetName, valDatasetId) = dosT.openValueDataset(dosA.getShape.map(_.toLong).toSeq, create = false)
try {
val idx = dosT.resolveGlobalIndex(indexDatasetId, dosV.gIndex)
dosV.table.valueSetLocal(valDatasetId, idx.localIndex, dosA)
} finally {
H5Lib.datasetClose(valDatasetId)
}
} finally {
H5Lib.datasetClose(indexDatasetId)
}
}
}
}
}
}
}
def calcFixes(calc: CalculationH5): Unit = {
dosFix(calc)
}
private val renameCmdUsage = """Usage:
|h5Tool rename
| [--fixes]
| [--renames=<renamesFile>]
| [--source-path=<pathToH5Archive>]
| [--source-paths-file=<pathToFileWithH5ArchivesPaths>]
......@@ -252,6 +308,7 @@ object H5Tool extends StrictLogging {
var sourcePaths: Seq[Path] = Seq()
var targetDir: Option[Path] = None
var flatDir: Boolean = false
var doFixes: Boolean = false
while (!list.isEmpty) {
val arg = list.head
list = list.tail
......@@ -259,6 +316,8 @@ object H5Tool extends StrictLogging {
case "--help" | "-h" =>
println(renameCmdUsage)
return
case "--fixes" =>
doFixes = true
case renamesRe(rFile) =>
replacements = replacements ++ Replacements.fromFile(Paths.get(rFile))
case targetDirRe(targetD) =>
......@@ -297,7 +356,13 @@ object H5Tool extends StrictLogging {
}
val targetF = FileH5.open(targetPath, write = true)
try {
H5Rename.renameStr(targetF, replacements)
for (arch <- targetF.archives()) {
for (calculation <- arch.calculations()) {
if (doFixes)
calcFixes(calculation)
H5Rename.renameStr(calculation, replacements)
}
}
} catch {
case NonFatal(e) =>
logger.error(s"Failure while doing replacements in file at $targetPath", e)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment