Beispiel #1
0
// MatfileNew2Old converts a new mat file to the old mat file format
//  convertsymbols -- convert back symbols with Greek characters to UTF-8
func MatfileNew2Old(dirout string, fnold, fnnew string, convertsymbols bool) {

	// read file
	b, err := io.ReadFile(fnnew)
	if err != nil {
		io.PfRed("cannot open file: %v", err.Error())
		return
	}

	// decode
	var mats_new MatDb
	err = json.Unmarshal(b, &mats_new)
	if err != nil {
		io.PfRed("cannot unmarshal file: %v", err.Error())
		return
	}

	// oldmatdata implements the old data format
	type oldmatdata struct {
		Name  string
		Desc  string
		Model string
		Prms  []string
		Vals  []float64
		Units []string
		Extra string
	}

	// convert
	var mats_old []oldmatdata
	for _, m := range mats_new.Materials {
		var oldmat oldmatdata
		oldmat.Name = m.Name
		oldmat.Desc = m.Desc
		oldmat.Model = m.Model
		oldmat.Extra = m.Extra
		for _, prm := range m.Prms {
			name := prm.N
			if convertsymbols {
				if n, ok := invertedconversiontable[prm.N]; ok {
					name = n
				}
			}
			oldmat.Prms = append(oldmat.Prms, name)
			oldmat.Vals = append(oldmat.Vals, prm.V)
			if len(prm.U) > 0 {
				oldmat.Units = append(oldmat.Units, prm.U)
			}
		}
		mats_old = append(mats_old, oldmat)
	}

	// encode
	buf, err := json.MarshalIndent(mats_old, "", "  ")
	if err != nil {
		return
	}

	// save file
	if dirout == "" {
		io.WriteFileS(fnold, string(buf))
		return
	}
	io.WriteFileSD(dirout, fnold, string(buf))
}
Beispiel #2
0
// MatfileOld2New converts an old mat file to new mat file format
//  convertsymbols -- convert symbols with Greek characters to ANSI
func MatfileOld2New(dirout string, fnnew, fnold string, convertsymbols bool) {

	// oldmatdata implements the old data format
	type oldmatdata struct {
		Name  string
		Desc  string
		Model string
		Prms  []string
		Vals  []float64
		Units []string
		Extra string
	}

	// data holder
	var mats_old []oldmatdata

	// read file
	b, err := io.ReadFile(fnold)
	if err != nil {
		chk.Panic("cannot open file: %v", err.Error())
	}

	// decode
	err = json.Unmarshal(b, &mats_old)
	if err != nil {
		chk.Panic("cannot unmarshal file: %v", err.Error())
	}

	// new data holder
	var mats_new MatDb
	for _, m := range mats_old {
		prms := []*fun.Prm{}
		has_units := len(m.Units) == len(m.Prms)
		for i, prm := range m.Prms {
			name := prm
			if convertsymbols {
				if n, ok := conversiontable[prm]; ok {
					name = n
				}
			}
			if has_units {
				prms = append(prms, &fun.Prm{N: name, V: m.Vals[i], U: m.Units[i]})
			} else {
				prms = append(prms, &fun.Prm{N: name, V: m.Vals[i]})
			}
		}
		mat_new := Material{
			Name:  m.Name,
			Desc:  m.Desc,
			Model: m.Model,
			Extra: m.Extra,
			Prms:  prms,
		}
		mats_new.Materials = append(mats_new.Materials, &mat_new)
	}

	// save file
	if dirout == "" {
		io.WriteFileS(fnnew, mats_new.String())
		return
	}
	io.WriteFileSD(dirout, fnnew, mats_new.String())
}