Skip to content

Parsers

superfish.parsers

Classes

Functions:

superfish.parsers.parse_automesh
parse_automesh(file)

Read an automesh input file into lines.

Parameters:

Name Type Description Default
file str

Path to the automesh (.AM) input file.

required

Returns:

Type Description
list of str

Lines of the file, with line endings preserved.

Source code in superfish/parsers.py
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
def parse_automesh(file: str) -> list[str]:
    """
    Read an automesh input file into lines.

    Parameters
    ----------
    file : str
        Path to the automesh (.AM) input file.

    Returns
    -------
    list of str
        Lines of the file, with line endings preserved.
    """
    return open(file, "r").readlines()
superfish.parsers.parse_fish_t7
parse_fish_t7(t7file, geometry='cylindrical')

Parse a Fish T7 file.

The T7 header should have::

xmin(cm), xmax(cm), nx-1
freq(MHz)
ymin(cm), ymax(cm), ny-1

followed by 4 columns of data: Ez, Er, E, Hphi.

TODO: Poisson problems, detect rectangular or cylindrical coordinates

Parameters:

Name Type Description Default
t7file str

Path to the T7 file.

required
geometry str

Problem geometry. Only "cylindrical" is currently handled.

'cylindrical'

Returns:

Type Description
FishT7Data

Parsed data with keys:

  • geometry : str
  • problem : "fish"
  • zmin, zmax : float, z extent in cm.
  • nz : int, number of z points.
  • rmin, rmax : float, radial extent in cm.
  • nr : int, number of radius points.
  • freq : float, frequency in MHz.
  • Ez, Er, E, Hphi : ndarray of shape (nr, nz).
Source code in superfish/parsers.py
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
def parse_fish_t7(t7file: str, geometry: str = "cylindrical") -> FishT7Data:
    """
    Parse a Fish T7 file.

    The T7 header should have::

        xmin(cm), xmax(cm), nx-1
        freq(MHz)
        ymin(cm), ymax(cm), ny-1

    followed by 4 columns of data: Ez, Er, E, Hphi.

    TODO: Poisson problems, detect rectangular or cylindrical coordinates

    Parameters
    ----------
    t7file : str
        Path to the T7 file.
    geometry : str
        Problem geometry. Only ``"cylindrical"`` is currently handled.

    Returns
    -------
    FishT7Data
        Parsed data with keys:

        - ``geometry`` : str
        - ``problem`` : ``"fish"``
        - ``zmin``, ``zmax`` : float, z extent in cm.
        - ``nz`` : int, number of z points.
        - ``rmin``, ``rmax`` : float, radial extent in cm.
        - ``nr`` : int, number of radius points.
        - ``freq`` : float, frequency in MHz.
        - ``Ez``, ``Er``, ``E``, ``Hphi`` : ndarray of shape (nr, nz).
    """

    # Read header
    # xmin(cm), xmax(cm), nx-1
    # freq(MHz)
    # ymin(cm), ymax(cm), ny-1
    with open(t7file, "r") as f:
        line1 = f.readline().split()
        freq_MHz = float(f.readline())
        line3 = f.readline().split()

    nz = int(line1[2]) + 1
    nr = int(line3[2]) + 1

    # Read and reshape. Columns are Ez, Er, E, Hphi
    dat4 = np.loadtxt(t7file, skiprows=3).reshape(nr, nz, 4)

    return FishT7Data(
        geometry=geometry,
        problem="fish",
        zmin=float(line1[0]),
        zmax=float(line1[1]),
        nz=nz,
        freq=freq_MHz,
        rmin=float(line3[0]),
        rmax=float(line3[1]),
        nr=nr,
        Ez=dat4[:, :, 0],
        Er=dat4[:, :, 1],
        E=dat4[:, :, 2],
        Hphi=dat4[:, :, 3],
    )
superfish.parsers.parse_header_lines
parse_header_lines(lines)

Parse the SFO header lines.

Parameters:

Name Type Description Default
lines list of str

Lines of the header group.

required

Returns:

Type Description
SFOHeader

Parsed header with keys:

  • variable : dict of variable name to value.
  • description : dict of variable name to description.
  • in_automesh : dict of variable name to bool.
  • comments : str, lines preceding the variable table.
Source code in superfish/parsers.py
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
def parse_header_lines(lines: list[str]) -> SFOHeader:
    """
    Parse the SFO header lines.

    Parameters
    ----------
    lines : list of str
        Lines of the header group.

    Returns
    -------
    SFOHeader
        Parsed header with keys:

        - ``variable`` : dict of variable name to value.
        - ``description`` : dict of variable name to description.
        - ``in_automesh`` : dict of variable name to bool.
        - ``comments`` : str, lines preceding the variable table.
    """

    header = "Variable Code         Value     Description"

    d = {}
    description = {}
    in_automesh = {}

    comments = []

    in_header = False
    for line in lines:
        if line == header:
            in_header = True
            continue
        if not in_header:
            comments.append(line)
            continue

        key, val, descrip, in_am = parse_header_variable(line)

        d[key] = val
        description[key] = descrip
        in_automesh[key] = in_am

    return {
        "variable": d,
        "description": description,
        "in_automesh": in_automesh,
        "comments": "\n".join(comments),
    }
superfish.parsers.parse_header_variable
parse_header_variable(line)

Parse a single variable line from the SFO header table.

The line follows the table header::

Variable Code         Value     Description

Parameters:

Name Type Description Default
line str

Line from the header table.

required

Returns:

Name Type Description
key str

Variable name.

value int or float

Variable value.

description str

Variable description.

in_automesh bool

True if the variable was set in the automesh input (code A).

Source code in superfish/parsers.py
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
def parse_header_variable(line: str) -> tuple[str, int | float, str, bool]:
    """
    Parse a single variable line from the SFO header table.

    The line follows the table header::

        Variable Code         Value     Description

    Parameters
    ----------
    line : str
        Line from the header table.

    Returns
    -------
    key : str
        Variable name.
    value : int or float
        Variable value.
    description : str
        Variable description.
    in_automesh : bool
        True if the variable was set in the automesh input (code ``A``).
    """
    x = line.split()

    key = x[0]

    if x[1] == "A":
        in_automesh = True

        s = x[2]
        d = x[3:]
    else:
        in_automesh = False
        s = x[1]
        d = x[2:]

    descrip = " ".join(d)

    try:
        val = int(s)
    except ValueError:
        val = float(s)

    return key, val, descrip, in_automesh
superfish.parsers.parse_poisson_t7
parse_poisson_t7(t7file, type='electric', geometry='cylindrical')

Parse a Poisson T7 file.

The T7 header should have::

xmin(cm), xmax(cm), nx-1
ymin(cm), ymax(cm), ny-1

followed by 2 columns of data:

  • type == "electric" : Er, Ez in V/cm.
  • type == "magnetic" : Br, Bz in G.

Parameters:

Name Type Description Default
t7file str

Path to the T7 file.

required
type (electric, magnetic)

Type of field data in the file.

"electric"
geometry str

Problem geometry. Only "cylindrical" is currently handled.

'cylindrical'

Returns:

Type Description
PoissonT7Data

Parsed data with keys:

  • geometry : str
  • problem : "poisson"
  • zmin, zmax : float, z extent in cm.
  • nz : int, number of z points.
  • rmin, rmax : float, radial extent in cm.
  • nr : int, number of radius points.
  • Er, Ez or Br, Bz : ndarray of shape (nr, nz).
Source code in superfish/parsers.py
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
def parse_poisson_t7(
    t7file: str,
    type: str = "electric",
    geometry: str = "cylindrical",
) -> PoissonT7Data:
    """
    Parse a Poisson T7 file.

    The T7 header should have::

        xmin(cm), xmax(cm), nx-1
        ymin(cm), ymax(cm), ny-1

    followed by 2 columns of data:

    - ``type == "electric"`` : Er, Ez in V/cm.
    - ``type == "magnetic"`` : Br, Bz in G.

    Parameters
    ----------
    t7file : str
        Path to the T7 file.
    type : {"electric", "magnetic"}
        Type of field data in the file.
    geometry : str
        Problem geometry. Only ``"cylindrical"`` is currently handled.

    Returns
    -------
    PoissonT7Data
        Parsed data with keys:

        - ``geometry`` : str
        - ``problem`` : ``"poisson"``
        - ``zmin``, ``zmax`` : float, z extent in cm.
        - ``nz`` : int, number of z points.
        - ``rmin``, ``rmax`` : float, radial extent in cm.
        - ``nr`` : int, number of radius points.
        - ``Er``, ``Ez`` or ``Br``, ``Bz`` : ndarray of shape (nr, nz).
    """
    assert geometry == "cylindrical", "TODO: other geometries"

    if type not in ("electric", "magnetic"):
        raise ValueError(f"Unknown type: {type}. Allowed: 'electric' or 'magnetic'")

    # Read header
    # xmin(cm), xmax(cm), nx-1    # r in cylindrical geometry
    # ymin(cm), ymax(cm), ny-1    # z in cylindrical geometry
    with open(t7file, "r") as f:
        xline = f.readline().split()
        yline = f.readline().split()

    nr = int(xline[2]) + 1
    nz = int(yline[2]) + 1

    d: PoissonT7Data = {
        "geometry": geometry,
        "problem": "poisson",
        "rmin": float(xline[0]),
        "rmax": float(xline[1]),
        "nr": nr,
        "zmin": float(yline[0]),
        "zmax": float(yline[1]),
        "nz": nz,
    }

    # Read and reshape
    dat = np.loadtxt(t7file, skiprows=2).reshape(nz, nr, 2)

    if type == "electric":
        d["Er"] = dat[:, :, 0].T
        d["Ez"] = dat[:, :, 1].T
    else:
        d["Br"] = dat[:, :, 0].T
        d["Bz"] = dat[:, :, 1].T

    return d
superfish.parsers.parse_sfo
parse_sfo(filename, verbose=False)

Master parser for the SFO file.

Parameters:

Name Type Description Default
filename str

Path to the SFO file.

required
verbose bool

Print a message for groups that have no parser.

False

Returns:

Type Description
SFOOutput

Parsed output with keys:

  • wall_segments : list of dict, one per wall segment.
  • summary : dict with data and units for the summary quantities (frequency, Q, ...), if present.
  • BeamEnergy : dict with data and units, if present.
  • header : dict of problem variables, descriptions, and comments.
  • other : dict of unparsed groups, keyed by their raw type line.
Source code in superfish/parsers.py
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
def parse_sfo(filename: str, verbose: bool = False) -> SFOOutput:
    """
    Master parser for the SFO file.

    Parameters
    ----------
    filename : str
        Path to the SFO file.
    verbose : bool
        Print a message for groups that have no parser.

    Returns
    -------
    SFOOutput
        Parsed output with keys:

        - ``wall_segments`` : list of dict, one per wall segment.
        - ``summary`` : dict with ``data`` and ``units`` for the summary
          quantities (frequency, Q, ...), if present.
        - ``BeamEnergy`` : dict with ``data`` and ``units``, if present.
        - ``header`` : dict of problem variables, descriptions, and comments.
        - ``other`` : dict of unparsed groups, keyed by their raw type line.
    """

    groups = parse_sfo_into_groups(filename)

    d: SFOOutput = {"wall_segments": [], "other": {}}

    for g in groups:
        dat = process_group(g, verbose=verbose)
        gtype = dat["type"]

        if gtype == "wall_segment":
            d["wall_segments"].append(cast(WallSegment, dat))
        elif gtype == "summary":
            d["summary"] = cast(SFOSummary, dat)
        elif gtype == "BeamEnergy":
            d["BeamEnergy"] = cast(SFOSummary, dat)
        elif gtype == "header":
            d["header"] = parse_header_lines(cast(UnparsedGroup, dat)["lines"])

        else:
            d["other"][gtype] = cast(UnparsedGroup, dat)

    # update Kinetic energy in 'summary' with the right value
    if "summary" in d and "BeamEnergy" in d:
        d["summary"]["data"]["kinetic_energy"] = (
            d["BeamEnergy"]["data"]["BeamEnergy"] / 1e6
        )
        d["summary"]["units"]["kinetic_energy"] = "MeV"

    return d
superfish.parsers.parse_sfo_beam_energy
parse_sfo_beam_energy(lines)

Parse the beam energy group.

Extracts the V0 value from the field normalization group.

Parameters:

Name Type Description Default
lines list of str

Lines of the group.

required

Returns:

Name Type Description
values dict

{"BeamEnergy": value}.

units dict

{"BeamEnergy": unit}.

Source code in superfish/parsers.py
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
def parse_sfo_beam_energy(
    lines: list[str],
) -> tuple[dict[str, float], dict[str, str]]:
    """
    Parse the beam energy group.

    Extracts the ``V0`` value from the field normalization group.

    Parameters
    ----------
    lines : list of str
        Lines of the group.

    Returns
    -------
    values : dict
        ``{"BeamEnergy": value}``.
    units : dict
        ``{"BeamEnergy": unit}``.
    """
    d_vals = {}
    d_units = {}
    for line in lines:
        line = line.strip()
        if line.startswith("V0"):
            parts = line.split("=")[-1].strip().split(" ")
            data = float(parts[0])
            unit = parts[1]
    d_vals["BeamEnergy"] = data
    d_units["BeamEnergy"] = unit
    return d_vals, d_units
superfish.parsers.parse_sfo_into_groups
parse_sfo_into_groups(filename)

Parse an SFO file into groups.

Groups are delimited by separator lines that start with -------------------.

Parameters:

Name Type Description Default
filename str

Path to the SFO file.

required

Returns:

Type Description
list of SFOGroup

One dict per group, with keys:

  • raw_type : str, the first line of the group.
  • lines : list of str, the remaining lines.
Source code in superfish/parsers.py
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
def parse_sfo_into_groups(filename: str) -> list[SFOGroup]:
    """
    Parse an SFO file into groups.

    Groups are delimited by separator lines that start with
    ``-------------------``.

    Parameters
    ----------
    filename : str
        Path to the SFO file.

    Returns
    -------
    list of SFOGroup
        One dict per group, with keys:

        - ``raw_type`` : str, the first line of the group.
        - ``lines`` : list of str, the remaining lines.
    """

    with open(filename, "r") as f:
        lines = f.readlines()

    sep = "-------------------"

    g: SFOGroup = {"raw_type": "header", "lines": []}
    groups: list[SFOGroup] = [g]

    new_group = False

    for line in lines:
        line = line.strip()

        # Skip empty lines
        if not line:
            continue

        # Look for new group
        if line.startswith(sep):
            new_group = True
            continue

        # Check for new group
        if new_group:
            gname = line
            new_group = False
            g = {"raw_type": gname, "lines": []}
            groups.append(g)
            continue

        # regular line
        g["lines"].append(line)

    return groups
superfish.parsers.parse_sfo_segment
parse_sfo_segment(lines)

Parse a wall segment group.

The group starts with a line like Power and fields on wall segment.

Parameters:

Name Type Description Default
lines list of str

Lines of the wall segment group, including the first (type) line.

required

Returns:

Type Description
WallSegmentData

Parsed segment with keys:

  • wall : dict of column name to ndarray of values.
  • info : dict of segment info (segment number, K/L range, and any key = value lines).
  • units : dict of column name to unit string.
Source code in superfish/parsers.py
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
def parse_sfo_segment(lines: list[str]) -> WallSegmentData:
    """
    Parse a wall segment group.

    The group starts with a line like
    ``Power and fields on wall segment``.

    Parameters
    ----------
    lines : list of str
        Lines of the wall segment group, including the first (type) line.

    Returns
    -------
    WallSegmentData
        Parsed segment with keys:

        - ``wall`` : dict of column name to ndarray of values.
        - ``info`` : dict of segment info (segment number, K/L range, and
          any ``key = value`` lines).
        - ``units`` : dict of column name to unit string.
    """

    # key = value lines

    info: dict[str, Any] = dict(parse_wall_segment_line1(lines[0]))

    inside = False
    fields: dict[str, list[float]] | None = None
    units: dict[str, str] | None = None

    for L in lines[1:]:
        L = L.strip()

        # Look for key=value
        if "=" in L:
            key, val = L.split("=")
            info[key.strip()] = val
            continue

        if L.startswith("K    L"):
            nskip = 2
            fields = {name.strip("|"): [] for name in L.split()[nskip:]}
            continue
        elif L.startswith("m     K     L"):
            nskip = 3
            fields = {name.strip("|"): [] for name in L.split()[nskip:]}
            continue

        if not fields:
            continue

        # Look for units
        if fields and not units:
            unit_labels = L.split()
            assert len(unit_labels) == len(fields), print(unit_labels)
            # make dict
            units = dict(zip(list(fields), unit_labels))
            inside = True
            continue

        # This might come at the end
        if L.startswith("Summary"):
            inside = False

        # Must be inside. Add data
        if inside:
            x = [float(y) for y in L.split()]
            # Special care if there are blanks for the skip columns
            if len(x) == len(fields) + nskip:
                x = x[nskip:]
            for i, name in enumerate(fields):
                fields[name].append(x[i])

    # Exiting
    assert fields is not None and units is not None, "No field table found"
    wall = {k: np.array(v) for k, v in fields.items()}

    return {"wall": wall, "info": info, "units": units}
superfish.parsers.parse_sfo_summary_group
parse_sfo_summary_group(lines)

Parse the summary group lines.

Parameters:

Name Type Description Default
lines list of str

Lines of the summary group.

required

Returns:

Name Type Description
values dict

Summary quantity name to value.

units dict

Summary quantity name to unit string.

Source code in superfish/parsers.py
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
def parse_sfo_summary_group(
    lines: list[str],
) -> tuple[dict[str, float], dict[str, str]]:
    """
    Parse the summary group lines.

    Parameters
    ----------
    lines : list of str
        Lines of the summary group.

    Returns
    -------
    values : dict
        Summary quantity name to value.
    units : dict
        Summary quantity name to unit string.
    """
    d_vals = {}
    d_units = {}
    for line in lines:
        if line == "":
            break
        else:
            d_val, d_unit = parse_sfo_summary_group_line(line)
            d_vals.update(d_val)
            d_units.update(d_unit)
    return d_vals, d_units
superfish.parsers.parse_sfo_summary_group_line
parse_sfo_summary_group_line(line)

Parse a single summary group line.

Handles the special multi-value lines (field normalization, integration path, beta, Q, Rs*Q, r/Q, average/maximum fields); anything else falls back to :func:parse_simple_summary_line.

Parameters:

Name Type Description Default
line str

Summary line.

required

Returns:

Name Type Description
values dict

Quantity name to value.

units dict

Quantity name to unit string.

Source code in superfish/parsers.py
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
def parse_sfo_summary_group_line(
    line: str,
) -> tuple[dict[str, float], dict[str, str]]:
    """
    Parse a single summary group line.

    Handles the special multi-value lines (field normalization,
    integration path, beta, Q, Rs*Q, r/Q, average/maximum fields);
    anything else falls back to :func:`parse_simple_summary_line`.

    Parameters
    ----------
    line : str
        Summary line.

    Returns
    -------
    values : dict
        Quantity name to value.
    units : dict
        Quantity name to unit string.
    """
    d_val = {}
    d_unit = {}
    if line.startswith("Field normalization"):
        parts = line.split("=")
        val = parts[-1]
        val = val.strip()
        val = val.split(" ")
        d_val["Enorm"] = float(val[0])
        d_unit["Enorm"] = val[1]
        return d_val, d_unit
    # 'for the integration path from point Z1,R1 =     50.50000 cm,   0.00000 cm',
    if line.startswith("for the integration path"):
        parts = line.split("=")
        val = parts[-1]
        val = val.strip()
        val = val.split(",")
        v1 = val[0].split(" ")
        v1 = v1[0]
        v2 = val[1].strip()
        v2 = v2.split(" ")
        v2 = v2[0]
        d_val["integration_Z1"] = float(v1)
        d_unit["integration_Z1"] = "cm"
        d_val["integration_R1"] = float(v2)
        d_unit["integration_R1"] = "cm"
        return d_val, d_unit
    #  'to ending point                     Z2,R2 =     50.51000 cm,   0.00000 cm',
    if line.startswith("to ending point"):
        parts = line.split("=")
        val = parts[-1]
        val = val.strip()
        val = val.split(",")
        v1 = val[0].split(" ")
        v1 = v1[0]
        v2 = val[1].strip()
        v2 = v2.split(" ")
        v2 = v2[0]
        d_val["integration_Z2"] = float(v1)
        d_unit["integration_Z2"] = "cm"
        d_val["integration_R2"] = float(v2)
        d_unit["integration_R2"] = "cm"
        return d_val, d_unit
    if line.startswith("Beta "):
        # custom parse the beta line
        parts = line.split("=")
        beta = parts[1].strip()
        beta = beta.split(" ")
        beta = beta[0]
        ke = parts[-1]
        ke = ke.strip()
        ke = ke.split(" ")
        ke = ke[0]
        d_val["beta"] = float(beta)
        d_val["kinetic_energy"] = float(ke)
        d_unit["beta"] = ""
        d_unit["kinetic_energy"] = "MeV"
        return d_val, d_unit
    #   'Q    =  0.334933E+10      Shunt impedance =  2001715.397 MOhm/m',
    if line.startswith("Q "):
        # Q factor line
        parts = line.split("=")
        v1 = parts[1].strip()
        v1 = v1.split(" ")
        v1 = v1[0]
        v2 = parts[-1]
        v2 = v2.strip()
        v2 = v2.split(" ")
        v2 = v2[0]
        d_val["Q"] = float(v1)
        d_val["Shunt impedance"] = float(v2)
        d_unit["Q"] = ""
        d_unit["Shunt impedance"] = "MOhm/m"
        return d_val, d_unit
    #          'Rs*Q =    81.364 Ohm                Z*T*T =  1956056.397 MOhm/m',
    if line.startswith("Rs*Q "):
        # Q factor line
        parts = line.split("=")
        v1 = parts[1].strip()
        v1 = v1.split(" ")
        v1 = v1[0]
        v2 = parts[-1]
        v2 = v2.strip()
        v2 = v2.split(" ")
        v2 = v2[0]
        d_val["Rs*Q"] = float(v1)
        d_val["Z*T*T"] = float(v2)
        d_unit["Rs*Q"] = "Ohm"
        d_unit["Z*T*T"] = "MOhm/m"
        return d_val, d_unit
    # 'r/Q  =   134.323 Ohm  Wake loss parameter =      0.04044 V/pC',
    if line.startswith("r/Q "):
        # r/Q line
        parts = line.split("=")
        v1 = parts[1].strip()
        v1 = v1.split(" ")
        v1 = v1[0]
        v2 = parts[-1]
        v2 = v2.strip()
        v2 = v2.split(" ")
        v2 = v2[0]
        d_val["r/Q"] = float(v1)
        d_val["Wake loss parameter"] = float(v2)
        d_unit["r/Q"] = "Ohm"
        d_unit["Wake loss parameter"] = "V/pC"
        return d_val, d_unit
    #  'Average magnetic field on the outer wall  =      16678.8 A/m, 0.337887 mW/cm^2',
    if line.startswith("Average magnetic "):
        # Average magnetic field line
        parts = line.split("=")
        val = parts[-1]
        val = val.strip()
        val = val.split(",")
        v1 = val[0].split(" ")
        v1 = v1[0]
        d_val["AvgH"] = float(v1)
        d_unit["AvgH"] = "A/m"
        return d_val, d_unit
    #  'Maximum H (at Z,R = 25.1487,18.4727)      =       41189. A/m, 2.06066 mW/cm^2',
    if line.startswith("Maximum H "):
        # Maximum H line
        parts = line.split("=")
        val = parts[1].strip()
        val = val.split(",")
        v1 = val[0]
        v2 = val[1].split(")")
        v2 = v2[0]
        v3 = parts[-1]
        v3 = v3.strip()
        v3 = v3.split(",")
        v3 = v3[0].split(" ")
        v3 = v3[0]
        d_val["MaxH_z"] = float(v1)
        d_val["MaxH_r"] = float(v2)
        d_val["MaxH"] = float(v3)
        d_unit["MaxH_z"] = "cm"
        d_unit["MaxH_r"] = "cm"
        d_unit["MaxH"] = "A/m"
        return d_val, d_unit
    #  'Maximum E (at Z,R = 49.9969,0.624269)     =      41.1564 MV/m, 2.84161 Kilp.',
    if line.startswith("Maximum E "):
        # Maximum E line
        parts = line.split("=")
        val = parts[1].strip()
        val = val.split(",")
        v1 = val[0]
        v2 = val[1].split(")")
        v2 = v2[0]
        v3 = parts[-1]
        v3 = v3.strip()
        v3 = v3.split(",")
        v3 = v3[0].split(" ")
        v3 = v3[0]
        d_val["MaxE_z"] = float(v1)
        d_val["MaxE_r"] = float(v2)
        d_val["MaxE"] = float(v3)
        d_unit["MaxE_z"] = "cm"
        d_unit["MaxE_r"] = "cm"
        d_unit["MaxE"] = "MV/m"
        return d_val, d_unit
    else:
        d_val, d_unit = parse_simple_summary_line(line)
    return d_val, d_unit
superfish.parsers.parse_simple_summary_line
parse_simple_summary_line(line)

Parse a simple summary line with one key and one value.

Parameters:

Name Type Description Default
line str

Line of the form key = value [unit].

required

Returns:

Name Type Description
values dict

Key to value. Empty if the line has no =.

units dict

Key to unit string (empty string if the line has no unit).

Source code in superfish/parsers.py
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
def parse_simple_summary_line(
    line: str,
) -> tuple[dict[str, float], dict[str, str]]:
    """
    Parse a simple summary line with one key and one value.

    Parameters
    ----------
    line : str
        Line of the form ``key = value [unit]``.

    Returns
    -------
    values : dict
        Key to value. Empty if the line has no ``=``.
    units : dict
        Key to unit string (empty string if the line has no unit).
    """
    d_val = {}
    d_unit = {}
    parts = line.split("=")
    if len(parts) == 1:
        return d_val, d_unit
    key = parts[0].strip()
    val = parts[-1].strip().split(" ")
    d_val[key] = float(val[0])
    if len(val) > 1:
        d_unit[key] = val[1]
    else:
        d_unit[key] = ""
    return d_val, d_unit
superfish.parsers.parse_wall_segment_line1
parse_wall_segment_line1(line)

Parse the first line of a wall segment group.

Helper for :func:parse_sfo_segment.

Parameters:

Name Type Description Default
line str

Line of the form Power and fields on wall segment 1 K,L = 1,2 to 3,4.

required

Returns:

Type Description
dict

Keys segment_number, K_beg, L_beg, K_end, L_end.

Source code in superfish/parsers.py
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
def parse_wall_segment_line1(line: str) -> dict[str, int]:
    """
    Parse the first line of a wall segment group.

    Helper for :func:`parse_sfo_segment`.

    Parameters
    ----------
    line : str
        Line of the form
        ``Power and fields on wall segment 1   K,L = 1,2 to 3,4``.

    Returns
    -------
    dict
        Keys ``segment_number``, ``K_beg``, ``L_beg``, ``K_end``, ``L_end``.
    """
    d = {}
    ix, x = line.split("segment")[1].split(" K,L =")
    d["segment_number"] = int(float((ix)))
    kl0, kl1 = x.split("to")
    k0, l0 = kl0.split(",")
    d["K_beg"], d["L_beg"] = int(k0), int(l0)
    k1, l1 = kl1.split(",")
    d["K_end"], d["L_end"] = int(k1), int(l1)
    return d
superfish.parsers.process_group
process_group(group, verbose=False)

Process a single output group dict into usable data.

Parameters:

Name Type Description Default
group SFOGroup

Group dict with raw_type and lines keys, as returned by :func:parse_sfo_into_groups.

required
verbose bool

Print a message when no parser exists for the group type.

False

Returns:

Type Description
SFOSummary or WallSegment or UnparsedGroup

Parsed group with a type key identifying the group (summary, wall_segment, BeamEnergy, or the raw type line for unparsed groups) and type-dependent data keys.

Source code in superfish/parsers.py
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
def process_group(
    group: SFOGroup,
    verbose: bool = False,
) -> SFOSummary | WallSegment | UnparsedGroup:
    """
    Process a single output group dict into usable data.

    Parameters
    ----------
    group : SFOGroup
        Group dict with ``raw_type`` and ``lines`` keys, as returned by
        :func:`parse_sfo_into_groups`.
    verbose : bool
        Print a message when no parser exists for the group type.

    Returns
    -------
    SFOSummary or WallSegment or UnparsedGroup
        Parsed group with a ``type`` key identifying the group
        (``summary``, ``wall_segment``, ``BeamEnergy``, or the raw type
        line for unparsed groups) and type-dependent data keys.
    """

    rtype = group["raw_type"]
    lines = group["lines"]

    if rtype.startswith("All calculated values below refer to the mesh geometry only"):
        data, units = parse_sfo_summary_group(lines)
        return SFOSummary(type="summary", data=data, units=units)

    if rtype.startswith("Power and fields on wall segment") or rtype.startswith(
        "Fields on segment"
    ):
        line1 = rtype  # This should be parsed fully
        seg = parse_sfo_segment([line1] + lines)
        return WallSegment(
            type="wall_segment",
            wall=seg["wall"],
            info=seg["info"],
            units=seg["units"],
        )

    if rtype.startswith(
        "The field normalization factor ASCALE for this problem is based"
    ):
        data, units = parse_sfo_beam_energy(lines)
        return SFOSummary(type="BeamEnergy", data=data, units=units)

    # No parser yet:
    if verbose:
        print("No parser for:", rtype)

    return UnparsedGroup(type=rtype, lines=lines)