Commit 1a4a2749 authored by Mohamed, Fawzi Roberto (fawzi)'s avatar Mohamed, Fawzi Roberto (fawzi)
Browse files

adding missing files for h5Tool (Replacements, H5Renamer)

parent c61aab9e
Pipeline #36853 failed with stages
in 50 minutes and 51 seconds
/*
Copyright 2016-2017 The NOMAD Developers Group
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package eu.nomad_lab.h5;
import ucar.ma2
import eu.nomad_lab.H5Lib
object H5Rename {
/**
* Renames the values of the value table at repl.metaPath of all calculations in the archive according to the repl.replacements mapping
*/
def renameStr(file: FileH5, repl: Replacements): Unit = {
for (arch <- file.archives()) {
renameStr(arch, repl)
}
}
/**
* Renames the values of the value table at repl.metaPath of all calculations in the archive according to the repl.replacements mapping
*/
def renameStr(archive: ArchiveH5, repl: Replacements): Unit = {
for (calculation <- archive.calculations()) {
renameStr(calculation, repl)
}
}
/**
* Renames the values of the value table at repl.metaPath of the given calculation
* according to the repl.replacements mapping
*/
def renameStr(calculation: CalculationH5, repl: Replacements): Unit = {
val valueTablePath = repl.metaPath
val valueTable = calculation.sectionTable(valueTablePath.dropRight(1)).subValueTable(valueTablePath.last)
renameStr(valueTable, repl.replacements)
}
/**
* Renames the values of the value table according to the renames mapping
*/
def renameStr(valueTable: ValueTableH5, renames: Map[String, String]): Unit = {
val nStrings = valueTable.lengthL
if (nStrings > 1000000000L)
throw new Exception(s"Excessive size in $valueTable ($nStrings) batch rename should be implemented")
if (nStrings > 0L) {
val (valueDatasetName, valueDatasetId) = valueTable.openValueDataset(Seq(1), false)
try {
val vals = new ma2.ArrayString.D1(nStrings.toInt)
val strings = H5Lib.datasetRead(
datasetId = valueDatasetId,
dtypeStr = "C",
dimsToRead = Seq(), offset = Seq(),
value = Some(vals)
)
var hasRenames: Boolean = false
{
for (ii <- 0 until nStrings.toInt) {
val sNow = vals.get(ii)
renames.get(sNow) match {
case Some(repl) =>
vals.set(ii, repl)
hasRenames = true
case None =>
}
}
}
if (hasRenames)
H5Lib.datasetWrite(valueDatasetId, value = vals, dtypeStr = "C")
} finally {
H5Lib.datasetClose(valueDatasetId)
}
}
}
}
/*
Copyright 2016-2017 The NOMAD Developers Group
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package eu.nomad_lab.h5;
import java.nio.file.Path
import scala.io.Source
import eu.nomad_lab.JsonUtils
/**
* Allow loading replacements fom a file with the following format
*
* # meta/path
* from -> to
* "from " -> " to"
*/
object Replacements {
def fromFile(path: Path): Seq[Replacements] = {
val f = Source.fromFile(path.toFile, "UTF-8")
var metaPath: Option[Seq[String]] = None
val metaPathRe = """^#\s+([a-zA-Z0-9_/]+)\s*$""".r
val replPathRe = """^\s+("(?:[^\"]|\.)"|[+-a-zA-Z0-9]+)\s+->\s+("(?:[^\"]|\.)"|[+-a-zA-Z0-9]+)\s*$""".r
var repl: Map[String, String] = Map()
var res: Seq[Replacements] = Seq()
for (line <- f.getLines()) {
line match {
case replPathRe(rawFromStr, rawToStr) =>
if (metaPath.isEmpty)
throw new Exception(s"replacement '$line' without a meta path")
val fromStr = if (!rawFromStr.isEmpty && rawFromStr(0) == '"')
JsonUtils.unescapeQuotedString(rawFromStr)
else
rawFromStr
val toStr = if (!rawToStr.isEmpty && rawToStr(0) == '"')
JsonUtils.unescapeQuotedString(rawToStr)
else
rawToStr
repl += (fromStr -> toStr)
case metaPathRe(mPath) =>
metaPath match {
case Some(p) =>
res = res :+ Replacements(p, repl)
metaPath = Some(mPath.split("/"))
repl = Map()
case None =>
metaPath = Some(mPath.split("/"))
}
case l =>
if (!l.foldLeft(true)((x: Boolean, y: Char) => x && y.isWhitespace))
throw new Exception(s"Invalid line '$line' in replacements file $path")
}
}
metaPath match {
case Some(p) =>
res :+ Replacements(p, repl)
case None =>
res
}
}
}
/**
* Describes replacements to perform on string values in an Hdf5 file
*
* see H5Rename to apply them
*/
case class Replacements(
metaPath: Seq[String] = Seq(),
replacements: Map[String, String] = Map()
)
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