MDIF.pm untested

filter: Statementstmt 0.0% 0 / 90 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 12 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 129 CC 8 SCAR 26.7
CC
8
Cov
0%
rmdif
30.0
wmdif
20.0
BEGIN
2.0
CRAP 14.5
1 # This program is free software; you can redistribute it and/or modify
2 # it under the terms of the GNU General Public License as published by
3 # the Free Software Foundation; either version 3 of the License, or
4 # (at your option) any later version.
5 #
6 # This program is distributed in the hope that it will be useful,
7 # but WITHOUT ANY WARRANTY; without even the implied warranty of
8 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 # GNU Library General Public License for more details.
10 #
11 # You should have received a copy of the GNU General Public License
12 # along with this program; if not, write to the Free Software
13 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
14 #
15 # Copyright (C) 2022- eWheeler, Inc. L<https://www.linuxglobal.com/>
16 # Originally written by Eric Wheeler, KJ7LNW
17 # All rights reserved.
18 #
19 # All tradmarks, product names, logos, and brands are property of their
20 # respective owners and no grant or license is provided thereof.
21  
22 package PDL::IO::MDIF;
23  
24 # Use PDL::IO::Touchstone's version. See https://stackoverflow.com/a/74264406/14055985 .
25 use PDL::IO::Touchstone; our $VERSION = $PDL::IO::Touchstone::VERSION;
26  
27 use 5.010;
28 use strict;
29 use warnings;
30 use Carp;
31  
32 use PDL::IO::Touchstone qw/rsnp_fh wsnp_fh n_ports/;
33  
34 BEGIN {
35 use Exporter;
36 our @ISA = ( @ISA, qw(Exporter) );
37 our @EXPORT = qw/ rmdif rmdf wmdif wmdf /;
38 our @EXPORT_OK = ();
39  
40 our %EXPORT_TAGS = (ALL => [ @EXPORT, @EXPORT_OK ]);
41 }
42  
43 sub rmdif
44 {
45 my ($filename, $args) = @_;
46  
47 open(my $in, $filename) or croak "$filename: $!";
48  
49 # Clone the input args and add to it:
50 $args = \%{ $args // {} };
51 $args->{filename} = $filename;
52 $args->{EOF_REGEX} = qr/^END$/;
53  
54 my %vars;
55 my @ret;
56 while (defined(my $line = <$in>))
57 {
58 chomp $line;
59  
60 if ($line =~ /^\s*!\s*(.*?)\s*$/)
61 {
62 push @{ $vars{_comments} }, $1;
63 }
64 elsif ($line =~ /^\s*VAR\s+([^=]+?)\s*=\s*"?(.+?)"?\s*$/i)
65 {
66 $vars{$1} = $2;
67 }
68 elsif ($line =~ /^\s*BEGIN\s+ACDATA\s*$/)
69 {
70 my @rsnp = rsnp_fh($in, $args);
71 push @ret, { %vars, _data => \@rsnp };
72 %vars = ();
73 }
74 elsif ($line =~ /^\s*BEGIN\s*NFDATA/)
75 {
76 carp "$filename: NFDATA (noise data) is not supported, skipping.";
77 while (defined($line = <$in>) && $line !~ /^\s*END/) {}
78 }
79 }
80  
81 close($in) or carp "$filename: $!";
82  
83 return \@ret;
84 }
85 *rmdf = \&rmdif;
86  
87 sub wmdif
88 {
89 my ($filename, $components) = @_;
90  
91 #my ($components, $fmt, $type, $fd) = @opts{qw/components format type/};
92  
93 open(my $out, ">", $filename) or croak "$filename: $!";
94  
95 my $n_ports;
96 my $n = 1;
97 foreach my $c (@$components)
98 {
99 croak "$filename: component $n does not define _data!" if !defined $c->{_data};
100  
101 if ($c->{_comments})
102 {
103 my $comments = join("\n", map { "! $_" } @{ $c->{_comments} });
104 print $out "$comments\n";
105 }
106
107 # Iterate vars that don't start with an underscore:
108 foreach my $var (grep { !/^_/ } keys(%$c))
109 {
110 my $val = $c->{$var};
111  
112 # Quote if its not a number:
113 $val = "\"$val\"" if $val !~ /^[-+]?[0-9]*\.?[0-9]+$/;
114  
115 print $out "VAR $var=$val\n";
116 }
117  
118 print $out "BEGIN ACDATA\n";
119  
120 my ($f, $m, $param_type, $z0, $comments, $fmt, $funit, $orig_f_unit) = @{ $c->{_data} };
121 my $n_ports = n_ports($m);
122  
123 # In the AWR and Keysight example formats the %F line comes
124 # _after_ the "# HZ" line from the original s2p. Fixing this
125 # requires hooking the touchstone code somewhere or
126 # re-processing the MDF; since it works in AWR we'll leave it
127 # for now. If you have trouble with the MDF output then swap
128 # the #HZ and %F lines. If it works after the swap then open a
129 # bug to fix this. References:
130 # https://awrcorp.com/download/faq/english/docs/users_guide/data_file_formats.html#d0e5542
131 # https://edadocs.software.keysight.com/display/ads2009/Working+with+Data+Files#WorkingwithDataFiles-1135104
132 my $pct_line = "% F";
133 for (my $i = 1; $i <= $n_ports; $i++)
134 {
135 for (my $j = 1; $j <= $n_ports; $j++)
136 {
137 $pct_line .= sprintf(" %s[%d,%d](Complex)", $param_type, $j, $i);
138 }
139 }
140
141 print $out "$pct_line\n";
142  
143 wsnp_fh($out, @{ $c->{_data} });
144 print $out "END\n\n";
145 }
146  
147 close($out);
148 }
149 *wmdf = \&wmdif;
150  
151 1;
152  
153 __END__