Last active
April 6, 2020 11:14
-
-
Save wiless/fcf6e558fd9cc6dd1dd02dc3f52938f6 to your computer and use it in GitHub Desktop.
Sample code to read and write XCorr and SqrtXcorr files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"io/ioutil" | |
"os" | |
"github.com/Sirupsen/logrus" | |
"gonum.org/v1/gonum/mat" | |
) | |
func main() { | |
SaveExample("Xcorr-RU-LOS.json", "SqrtXcorr-RU-LOS.json") | |
// LoadExample("Xcorr-RU-LOS.json", "SqrtXcorr-RU-LOS.json") | |
} | |
func SaveExample(xcorrfile, Lfile string) { | |
input := xcorrfile | |
output := Lfile | |
// Create & Save to file.. | |
Rc := mat.NewSymDense(3, []float64{1, 2, 3, 2, 10, 4, 3, 4, 20}) | |
Save(Rc.RawSymmetric(), input) | |
fmt.Printf("\nRc= %v \n\n", mat.Formatted(Rc, mat.Prefix(" "))) | |
/// Create & Save the L matrix, (where L*L'=Rc ) | |
var sqRC mat.Cholesky | |
var Lmat1 mat.TriDense | |
if ok := sqRC.Factorize(Rc); !ok { | |
fmt.Println("a matrix is not positive semi-definite... Cannot Factorize") | |
return | |
} | |
sqRC.LTo(&Lmat1) | |
Save(Lmat1.RawTriangular(), output) | |
fmt.Printf("\nL = %v \n\n", mat.Formatted(&Lmat1, mat.Prefix(" "))) | |
return | |
} | |
func LoadExample(xcorrfile, Lfile string) { | |
input := xcorrfile | |
output := Lfile | |
// Load Rc from file .. | |
Rc := mat.NewSymDense(3, nil) // empty one | |
xx := Rc.RawSymmetric() | |
Load(input, &xx) | |
Rc.SetRawSymmetric(xx) | |
fmt.Printf("\nRc= %v \n\n", mat.Formatted(Rc, mat.Prefix(" "))) | |
/// Load L from output file (where L*L'=Rc ) | |
var Lmat1 mat.TriDense | |
yy := Lmat1.RawTriangular() | |
Load(output, &yy) | |
Lmat1.SetRawTriangular(yy) | |
fmt.Printf("\nL = %v \n\n", mat.Formatted(&Lmat1, mat.Prefix(" "))) | |
return | |
} | |
func Save(a interface{}, fname string) { | |
// fmt.Printf("Saving ..", mat.Formatted(a, mat.Prefix(" "))) | |
b, e := json.Marshal(a) | |
if e != nil { | |
logrus.Println("Error ", e) | |
} else { | |
f, er := os.Create(fname) | |
if er != nil { | |
return | |
} | |
f.Write(b) | |
logrus.Info("Saving .. ", string(b)) | |
f.Close() | |
} | |
} | |
func Load(fname string, a interface{}) { | |
f, er := os.Open(fname) | |
if er != nil { | |
logrus.Error("Error ", er) | |
} else { | |
data, er := ioutil.ReadAll(f) | |
logrus.Info("Read .. ", string(data)) | |
if er == nil { | |
er := json.Unmarshal(data, a) | |
if er != nil { | |
fmt.Printf("Error json ", er) | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"html/template" | |
"log" | |
"net/http" | |
"os" | |
"path/filepath" | |
) | |
func main() { | |
fs := http.FileServer(http.Dir("./")) | |
http.Handle("/static/", http.StripPrefix("/static/", fs)) | |
http.HandleFunc("/", serveTemplate) | |
log.Println("Listening on :3000...") | |
err := http.ListenAndServe(":3000", nil) | |
if err != nil { | |
log.Fatal(err) | |
} | |
} | |
func serveTemplate(w http.ResponseWriter, r *http.Request) { | |
lp := filepath.Join("./", "layout.html") | |
fp := filepath.Join("./", filepath.Clean(r.URL.Path)) | |
fmt.Println("Looking for ?", r.URL.Path) | |
// Return a 404 if the template doesn't exist | |
info, err := os.Stat(fp) | |
if err != nil { | |
if os.IsNotExist(err) { | |
http.NotFound(w, r) | |
return | |
} | |
} | |
// Return a 404 if the request is for a directory | |
if info.IsDir() { | |
http.NotFound(w, r) | |
return | |
} | |
tmpl, err := template.ParseFiles(lp, fp) | |
if err != nil { | |
// Log the detailed error | |
log.Println(err.Error()) | |
// Return a generic "Internal Server Error" message | |
http.Error(w, http.StatusText(500), 500) | |
return | |
} | |
err = tmpl.ExecuteTemplate(w, "layout", nil) | |
if err != nil { | |
log.Println(err.Error()) | |
http.Error(w, http.StatusText(500), 500) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment