Skip to content

Instantly share code, notes, and snippets.

@erwiese
Created November 15, 2018 12:52
Show Gist options
  • Save erwiese/8f559b6fa8a55c39f95e4af98b221ae0 to your computer and use it in GitHub Desktop.
Save erwiese/8f559b6fa8a55c39f95e4af98b221ae0 to your computer and use it in GitHub Desktop.
run command and pip stdout to file
// Rnx2crx creates a copy of the file that is Hatanaka-compressed (compact RINEX).
// see http://terras.gsi.go.jp/ja/crx2rnx.html
func Rnx2crx(obsFil string) (string, error) {
tool, err := exec.LookPath("RNX2CRX")
if err != nil {
return "", err
}
if !strings.HasSuffix(obsFil, ".rnx") {
return "", fmt.Errorf("file %s with invalid extension", obsFil)
}
// compact RINEX file to create
newPath := obsFil + ".xxx"
newFil, err := os.Create(newPath)
if err != nil {
return "", fmt.Errorf("Could not create file %s: %v", newPath, err)
}
defer newFil.Close()
writer := bufio.NewWriter(newFil)
cmd := exec.Command(tool, obsFil, "-")
stdout, err := cmd.StdoutPipe()
if err != nil {
log.Fatal(err)
}
var stderr bytes.Buffer
cmd.Stderr = &stderr
err = cmd.Start() // as new OS process
if err != nil {
return "", fmt.Errorf("starting %s failed: %v", tool, err)
}
go io.Copy(writer, stdout)
//see https://stackoverflow.com/questions/10385551/get-exit-code-go
if err := cmd.Wait(); err != nil { // no timeout, see context
return "", fmt.Errorf("cmd %s failed: %v: %s", tool, err, stderr.Bytes())
}
writer.Flush()
log.Printf("file %s created", newPath)
return newPath, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment