Skip to content

Commit 69b5512

Browse files
authored
Merge pull request #457 from pixlise/feature/em-import-fixes
EM importer: Better logging, logs BGT output too, don't overwrite RSI…
2 parents 8abf0eb + 0be45af commit 69b5512

File tree

6 files changed

+65
-20
lines changed

6 files changed

+65
-20
lines changed

api/dataimport/pre-import.go

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func ProcessEM(importId string, zipReader *zip.Reader, zippedData []byte, destBu
7878
isCalTarget := strings.Contains(lowerId, "cal-target") || strings.Contains(lowerId, "cal_target") || strings.Contains(lowerId, "caltarget")
7979

8080
// Create an RSI file from the sdf_raw file
81-
genFiles, rtts, err := sdfToRSI.ConvertSDFtoRSIs(sdfLocalPath, localTemp)
81+
genFiles, rtts, err := sdfToRSI.ConvertSDFtoRSIs(sdfLocalPath, localTemp, logger)
8282

8383
if err != nil {
8484
return fmt.Errorf("Failed to scan %v for RSI creation: %v", sdfLocalPath, err)
@@ -100,7 +100,7 @@ func ProcessEM(importId string, zipReader *zip.Reader, zippedData []byte, destBu
100100

101101
// Every second file is a HK file not an actual RSI file... make sure we have the right prefix here
102102
if !strings.HasPrefix(f, "RSI-") || !strings.HasPrefix(hkFile, "HK-") {
103-
logger.Errorf("ConvertSDFtoRSIs generated : %v. Error: %v", f, err)
103+
logger.Errorf("ConvertSDFtoRSIs generated: \"%v\". Error: %v", f, err)
104104
continue
105105
}
106106

@@ -112,7 +112,7 @@ func ProcessEM(importId string, zipReader *zip.Reader, zippedData []byte, destBu
112112
continue
113113
}
114114

115-
// Upoad the output files (beam locations, log and surface)
115+
// Upload the output files (beam locations, log and surface)
116116
files := []string{filepath.Join(localTemp, hkFile), rxlPath, logPath, surfPath, rsiLocalPath}
117117
name := []string{"housekeeping", "beam location", "log", "surface", "rsi"}
118118
for i, file := range files {
@@ -234,24 +234,45 @@ func createBeamLocation(isCalTarget bool, rsiPath string, rtt int64, outputBeamL
234234
if _, err := os.Stat(bgtPath + "BGT"); err != nil {
235235
// Try the path used in local testing
236236
bgtPath = ".." + string(os.PathSeparator) + ".." + string(os.PathSeparator) + "beam-tool" + string(os.PathSeparator)
237+
if _, err = os.Stat(bgtPath + "BGT"); err != nil {
238+
return "", "", "", errors.New("BGT tool not found")
239+
}
240+
}
241+
242+
// Ensure output files don't exist yet!
243+
toDel := []string{outSurfaceTop, outRXL, outLog}
244+
for _, f := range toDel {
245+
err := os.Remove(f)
246+
if err != nil {
247+
logger.Errorf("Error deleting existing \"%v\": %v", f, err)
248+
}
249+
}
250+
251+
// Find an absolute path because after Go 1.19 exec.Command doesn't work with relative paths... strange this wasn't noticed earlier!
252+
wd, err := os.Getwd()
253+
if err != nil {
254+
return "", "", "", fmt.Errorf("Failed to get working dir: %v", err)
237255
}
238256

239-
if _, err := os.Stat(bgtPath + "Geometry_PIXL_EM_Landing_25Jan2021.csv"); err != nil {
257+
calibPath := path.Join(wd, bgtPath, "Geometry_PIXL_EM_Landing_25Jan2021.csv")
258+
259+
if _, err := os.Stat(calibPath); err != nil {
240260
return "", "", "", errors.New("Calibration file not found")
241261
}
242262
if _, err := os.Stat(rsiPath); err != nil {
243263
return "", "", "", errors.New("RSI not found")
244264
}
245265

246-
args := []string{bgtPath + "Geometry_PIXL_EM_Landing_25Jan2021.csv", rsiPath, outSurfaceTop, outRXL}
266+
args := []string{calibPath, rsiPath, outSurfaceTop, outRXL}
247267
if isCalTarget {
248268
args = append(args, "-t")
249269
}
250270
args = append(args, outLog)
251271

252-
fmt.Printf("Executing: %v %v\n", bgtPath+"BGT", strings.Join(args, " "))
272+
exePath := path.Join(wd, bgtPath, "BGT")
253273

254-
cmd := exec.Command(bgtPath+"BGT", args...)
274+
fmt.Printf("Executing: %v %v\n", exePath, strings.Join(args, " "))
275+
cmd := exec.Command(exePath, args...)
255276

256277
// var out bytes.Buffer
257278
// var stderr bytes.Buffer
@@ -262,17 +283,30 @@ func createBeamLocation(isCalTarget bool, rsiPath string, rtt int64, outputBeamL
262283
// cmd.Stderr = os.Stderr
263284
cmd.Dir = bgtPath
264285

265-
if out, err := cmd.CombinedOutput(); err != nil {
286+
errOut := false
287+
if out, cmdErr := cmd.CombinedOutput(); cmdErr != nil {
266288
logger.Infof("CombinedOutput:\n%s", out)
289+
err = cmdErr
290+
errOut = true // Don't return just yet, we want to print the log file if it exists..
267291
//if err := cmd.Run(); err != nil {
268292
// Dump std out
269293
// logger.Infof("BGT stdout:\n" + out.String())
270294
// logger.Errorf("BGT stderr:\n" + stderr.String())
271-
return "", "", "", fmt.Errorf("BGT tool error: %v", err)
272295
} else {
273296
logger.Infof("CombinedOutput:\n%s", out)
274297
}
275298

299+
// Dump the log file to the log here
300+
if bgtLog, logErr := os.ReadFile(outLog); logErr != nil {
301+
logger.Infof("Failed to read BGT log \"%v\": %v", outLog, logErr)
302+
} else {
303+
logger.Infof("BGT Log Output:\n%s", bgtLog)
304+
}
305+
306+
if errOut {
307+
return "", "", "", fmt.Errorf("BGT tool error: %v", err)
308+
}
309+
276310
/*
277311
procAttr := new(os.ProcAttr)
278312
procAttr.Files = []*os.File{nil, nil, nil}

api/dataimport/pre-import_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func Example_dataimport_ProcessEM() {
4141

4242
localTemp, sdfLocalPath, _, _, err := startEMProcess("123", z, zFile, &l)
4343
fmt.Printf("startEMProcess err=%v\n", err)
44-
genFiles, rtts, err := sdfToRSI.ConvertSDFtoRSIs(sdfLocalPath, localTemp)
44+
genFiles, rtts, err := sdfToRSI.ConvertSDFtoRSIs(sdfLocalPath, localTemp, &l)
4545
fmt.Printf("genFiles: %v\nrtts: %v\nerr: %v\n", genFiles, rtts, err)
4646
}
4747
}

api/dataimport/sdfToRSI/scanSDF.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ func scanSDF(sdfPath string) ([]EventEntry, error) {
141141
pos := strings.Index(lineData, sciPlace)
142142
if pos > -1 {
143143
lineData = lineData[pos+len(sciPlace):]
144+
lineData = strings.TrimRight(lineData, "\"")
144145
refs = append(refs, EventEntry{Line: lineNo, What: "sci-place", Value: lineData})
145146
}
146147
}

api/dataimport/sdfToRSI/sdfToRSI.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@ import (
77
"path"
88
"strconv"
99
"strings"
10+
11+
"github.com/pixlise/core/v4/core/logger"
12+
"github.com/pixlise/core/v4/core/utils"
1013
)
1114

1215
// Given an SDF path and an output path, this generates RSI files for each scan mentioned in the SDF.
1316
// Returns the file names generated and an error if any
14-
func ConvertSDFtoRSIs(sdfPath string, outPath string) ([]string, []int64, error) {
17+
func ConvertSDFtoRSIs(sdfPath string, outPath string, logger logger.ILogger) ([]string, []int64, error) {
1518
files := []string{}
1619
rtts := []int64{}
1720

@@ -36,14 +39,19 @@ func ConvertSDFtoRSIs(sdfPath string, outPath string) ([]string, []int64, error)
3639
} else if ref.Value != "end" {
3740
return files, rtts, fmt.Errorf("End not found for science RTT: %v", rtt)
3841
} else {
39-
nameRSI := fmt.Sprintf("RSI-%v.csv", rtt)
40-
nameHK := fmt.Sprintf("HK-%v.csv", rtt)
41-
err = sdfToRSI(sdfPath, rtt, startLine, ref.Line, path.Join(outPath, nameRSI), path.Join(outPath, nameHK))
42-
if err != nil {
43-
return files, rtts, fmt.Errorf("Failed to generate files %v, %v: %v", nameRSI, nameHK, err)
42+
// If we've already seen an "end" for this rtt, ignore
43+
if utils.ItemInSlice(rtt, rtts) {
44+
logger.Infof("ConvertSDFtoRSIs \"%v\" [%v]: Already detected end of RTT %v - skipping...", sdfPath, ref.Line, rtt)
45+
} else {
46+
nameRSI := fmt.Sprintf("RSI-%v.csv", rtt)
47+
nameHK := fmt.Sprintf("HK-%v.csv", rtt)
48+
err = sdfToRSI(sdfPath, rtt, startLine, ref.Line, path.Join(outPath, nameRSI), path.Join(outPath, nameHK))
49+
if err != nil {
50+
return files, rtts, fmt.Errorf("Failed to generate files %v, %v: %v", nameRSI, nameHK, err)
51+
}
52+
files = append(files, nameRSI, nameHK)
53+
rtts = append(rtts, rtt)
4454
}
45-
files = append(files, nameRSI, nameHK)
46-
rtts = append(rtts, rtt)
4755
}
4856
}
4957
}

api/dataimport/sdfToRSI/sdfToRSI_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"log"
88
"os"
99
"path/filepath"
10+
11+
"github.com/pixlise/core/v4/core/logger"
1012
)
1113

1214
func Example_sdfToRSI_ConvertSDFtoRSI() {
@@ -16,7 +18,7 @@ func Example_sdfToRSI_ConvertSDFtoRSI() {
1618
wd, err := os.Getwd()
1719
fmt.Printf("Getwd: %v\n", err == nil)
1820
p := filepath.Join(wd, "output")
19-
files, rtts, err := ConvertSDFtoRSIs("./test-data/sdf_raw.txt", p)
21+
files, rtts, err := ConvertSDFtoRSIs("./test-data/sdf_raw.txt", p, &logger.StdOutLogger{})
2022
fmt.Printf("%v, %v: %v\n", files, rtts, err)
2123

2224
// Output:
@@ -30,7 +32,7 @@ func Example_sdfToRSI_ConvertSDFtoRSI_EndingPrematurely() {
3032
wd, err := os.Getwd()
3133
fmt.Printf("Getwd: %v\n", err == nil)
3234
p := filepath.Join(wd, "output")
33-
files, rtts, err := ConvertSDFtoRSIs("./test-data/sdf_raw_premature_end.txt", p)
35+
files, rtts, err := ConvertSDFtoRSIs("./test-data/sdf_raw_premature_end.txt", p, &logger.StdOutLogger{})
3436
fmt.Printf("%v, %v: %v\n", files, rtts, err)
3537

3638
// Output:

beam-tool/BGT

100644100755
File mode changed.

0 commit comments

Comments
 (0)