Exemple #1
0
def save_touchstone(db, filename, sep="\t", format="RI"):
    """Write a Datablock to a touchstone-format file with name filename.
    """
    fmt = format.upper()

    if fmt not in ["RI", "MA", "DB"]:
        raise TouchstoneError(f'Format {fmt} unknown, use "RI", "MA" or "DB"')
    with open(filename, "w") as fil:
        for comment in db.comments.fullcomments:
            print(f"!{comment}", file=fil)
        for k, v in db.vardata.items():
            if v.shape == ():
                print(f"!{k}: {v.outputformat}", file=fil)
        print(f"#HZ S {fmt} R 50", file=fil)

        header, columns = make_col_from_matrix([db.S.dims[0].name, "S"],
                                               [hfarray(db.S.dims[0]), db.S],
                                               "%s[%s,%s]",
                                               fortranorder=True)
        fmts = [x.outputformat for x in columns]
        out = []
        for row in zip(*columns):
            rowout = []
            for elem, numfmt in zip(row, fmts):
                if iscomplexobj(elem):
                    if fmt == "RI":
                        rowout.append(numfmt % elem.real)
                        rowout.append(numfmt % elem.imag)
                    elif fmt == "MA":
                        rowout.append(numfmt % abs(elem))
                        rowout.append(numfmt % np.angle(elem, deg=True))
                    elif fmt == "DB":
                        rowout.append(numfmt % dB(elem))
                        rowout.append(numfmt % np.angle(elem, deg=True))
                    else:
                        raise TouchstoneError(
                            "Format error unknown format {fmt}")
                else:
                    rowout.append(numfmt % elem)
            out.append(rowout)
        for rad in pad_columns(out):
            print(*rad, sep=sep, file=fil)
Exemple #2
0
def save_touchstone(db, filename, sep="\t", format="RI"):
    """Write a Datablock to a touchstone-format file with name filename.
    """
    fmt = format.upper()
 
    if fmt not in ["RI", "MA", "DB"]:
        raise TouchstoneError(f'Format {fmt} unknown, use "RI", "MA" or "DB"')
    with open(filename, "w") as fil:
        for comment in db.comments.fullcomments:
            print(f"!{comment}", file=fil)
        for k, v in db.vardata.items():
            if v.shape == ():
                print(f"!{k}: {v.outputformat}", file=fil)
        print(f"#HZ S {fmt} R 50", file=fil)


        header, columns = make_col_from_matrix([db.S.dims[0].name, "S"], [hfarray(db.S.dims[0]), db.S],
                                               "%s[%s,%s]", fortranorder=True)
        fmts = [x.outputformat for x in columns]
        out = []
        for row in zip(*columns):
            rowout = []
            for elem, numfmt in zip(row, fmts):
                if iscomplexobj(elem):
                    if fmt == "RI":
                        rowout.append(numfmt % elem.real)
                        rowout.append(numfmt % elem.imag)
                    elif fmt == "MA":
                        rowout.append(numfmt % abs(elem))
                        rowout.append(numfmt % np.angle(elem, deg=True))
                    elif fmt == "DB":
                        rowout.append(numfmt % dB(elem))
                        rowout.append(numfmt % np.angle(elem, deg=True))
                    else:
                        raise TouchstoneError("Format error unknown format {fmt}")
                else:
                    rowout.append(numfmt % elem)
            out.append(rowout)
        for rad in pad_columns(out):
            print(*rad, sep=sep, file=fil)
Exemple #3
0
 def test_2(self):
     self.assertEqual(hfmath.dB(100), 40)
Exemple #4
0
 def test_1(self):
     self.assertEqual(hfmath.dB(.01 + 0j), -40)