This commit is contained in:
che
2026-07-12 20:42:21 +05:00
parent b815cde04c
commit e3490e5d2e
23 changed files with 1568 additions and 538 deletions
+75
View File
@@ -2,7 +2,9 @@ package control_case
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"regexp"
"strconv"
"strings"
@@ -70,6 +72,68 @@ func ReadNearestFieldMap(filePath string, requestedT float64) (*FieldMapResponse
return result, nil
}
func CopyLastStateStorageH5(sourcePath, outputPath string) error {
if strings.TrimSpace(outputPath) == "" {
return fmt.Errorf("output path is empty")
}
sourceAbs, err := filepath.Abs(sourcePath)
if err != nil {
return err
}
outputAbs, err := filepath.Abs(outputPath)
if err != nil {
return err
}
if sourceAbs == outputAbs {
return fmt.Errorf("output path must be different from source path")
}
stageGroups, err := listStageGroups(sourcePath)
if err != nil {
return err
}
if len(stageGroups) == 0 {
return fmt.Errorf("no stage_t groups found")
}
latest := stageGroups[0]
for _, stage := range stageGroups[1:] {
if stage.stageT > latest.stageT {
latest = stage
}
}
if err := os.MkdirAll(filepath.Dir(outputPath), os.ModePerm); err != nil {
return err
}
tmpFile, err := os.CreateTemp(filepath.Dir(outputPath), ".storage-*.h5")
if err != nil {
return err
}
tmpPath := tmpFile.Name()
if err := tmpFile.Close(); err != nil {
_ = os.Remove(tmpPath)
return err
}
if err := os.Remove(tmpPath); err != nil {
return err
}
if err := runH5Copy(sourcePath, tmpPath, latest.path, "/map/stage_t=0", "-p"); err != nil {
_ = os.Remove(tmpPath)
return err
}
if err := os.Rename(tmpPath, outputPath); err != nil {
_ = os.Remove(tmpPath)
return err
}
return nil
}
type stageFieldMap struct {
path string
stageT float64
@@ -231,6 +295,17 @@ func runH5Dump(filePath string, args ...string) ([]byte, error) {
return out, nil
}
func runH5Copy(sourceFile, outputFile, sourcePath, outputPath string, args ...string) error {
cmdArgs := []string{"-i", sourceFile, "-o", outputFile, "-s", sourcePath, "-d", outputPath}
cmdArgs = append(cmdArgs, args...)
cmd := exec.Command("h5copy", cmdArgs...)
out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("h5copy %v failed: %w: %s", cmdArgs, err, strings.TrimSpace(string(out)))
}
return nil
}
func absFloat64(value float64) float64 {
if value < 0 {
return -value