kopia lustrzana https://github.com/NanoVNA-Saver/nanovna-saver
Use SITools for value parsing and formatting
rodzic
a49c2cc146
commit
722957f9f3
|
@ -15,9 +15,9 @@
|
|||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
import collections
|
||||
import math
|
||||
from NanoVNASaver.SITools import Value, Format
|
||||
|
||||
PREFIXES = ("", "k", "M", "G", "T")
|
||||
|
||||
Datapoint = collections.namedtuple('Datapoint', 'freq re im')
|
||||
|
||||
|
||||
|
@ -70,74 +70,22 @@ class RFTools:
|
|||
if im50 == 0 or freq == 0:
|
||||
return "- pF"
|
||||
capacitance = 10**12/(freq * 2 * math.pi * im50)
|
||||
if abs(capacitance) > 10000:
|
||||
return str(round(-capacitance/1000, 2)) + " nF"
|
||||
elif abs(capacitance) > 1000:
|
||||
return str(round(-capacitance/1000, 3)) + " nF"
|
||||
elif abs(capacitance) > 10:
|
||||
return str(round(-capacitance, 2)) + " pF"
|
||||
else:
|
||||
return str(round(-capacitance, 3)) + " pF"
|
||||
|
||||
return str(Value(-capacitance, "F", Format(max_nr_digits=5)))
|
||||
|
||||
@staticmethod
|
||||
def inductanceEquivalent(im50, freq) -> str:
|
||||
if freq == 0:
|
||||
return "- nH"
|
||||
inductance = im50 * 1000000000 / (freq * 2 * math.pi)
|
||||
if abs(inductance) > 10000:
|
||||
return str(round(inductance / 1000, 2)) + " μH"
|
||||
elif abs(inductance) > 1000:
|
||||
return str(round(inductance/1000, 3)) + " μH"
|
||||
elif abs(inductance) > 10:
|
||||
return str(round(inductance, 2)) + " nH"
|
||||
else:
|
||||
return str(round(inductance, 3)) + " nH"
|
||||
return str(Value(inductance, "H", Format(max_nr_digits=5)))
|
||||
|
||||
@staticmethod
|
||||
def formatFrequency(freq):
|
||||
return RFTools.formatFixedFrequency(
|
||||
round(freq), 7, True, True)
|
||||
return str(Value(freq, "Hz", Format(max_nr_digits=6)))
|
||||
|
||||
@staticmethod
|
||||
def formatShortFrequency(freq):
|
||||
return RFTools.formatFixedFrequency(
|
||||
round(freq), 5, True, True)
|
||||
|
||||
@staticmethod
|
||||
def formatFixedFrequency(freq: int,
|
||||
maxdigits: int = 6,
|
||||
appendHz: bool = True,
|
||||
insertSpace: bool = False,
|
||||
countDot: bool = True,
|
||||
assumeInfinity: bool = True) -> str:
|
||||
""" Format frequency with SI prefixes
|
||||
|
||||
maxdigits count include the dot by default, so that
|
||||
default leads to a maximum output of 9 characters
|
||||
"""
|
||||
freqstr = str(freq)
|
||||
freqlen = len(freqstr)
|
||||
|
||||
# sanity checks
|
||||
if freqlen > 15:
|
||||
if assumeInfinity:
|
||||
return "\N{INFINITY}"
|
||||
raise ValueError("Frequency too big. More than 15 digits!")
|
||||
if maxdigits < 3:
|
||||
raise ValueError("At least 3 digits are needed, given ({})".format(maxdigits))
|
||||
if not countDot:
|
||||
maxdigits += 1
|
||||
|
||||
if freq < 1:
|
||||
return " - " + (" " if insertSpace else "") + ("Hz" if appendHz else "")
|
||||
|
||||
freqstr = str(freq)
|
||||
freqlen = len(freqstr)
|
||||
si_index = (freqlen - 1) // 3
|
||||
dot_pos = freqlen % 3 or 3
|
||||
freqstr = freqstr[:dot_pos] + "." + freqstr[dot_pos:] + "00"
|
||||
retval = freqstr[:maxdigits] + (" " if insertSpace else "") + PREFIXES[si_index] + ("Hz" if appendHz else "")
|
||||
return retval
|
||||
return str(Value(freq, "Hz", Format(max_nr_digits=4)))
|
||||
|
||||
@staticmethod
|
||||
def formatSweepFrequency(freq: int,
|
||||
|
@ -176,29 +124,10 @@ class RFTools:
|
|||
|
||||
@staticmethod
|
||||
def parseFrequency(freq: str) -> int:
|
||||
freq = freq.replace(" ", "") # Ignore spaces
|
||||
if freq.isnumeric():
|
||||
return int(freq)
|
||||
|
||||
multiplier = 1
|
||||
freq = freq.lower()
|
||||
|
||||
if freq.endswith("hz"):
|
||||
freq = freq[:-2]
|
||||
|
||||
my_prefixes = [pfx.lower() for pfx in PREFIXES]
|
||||
if len(freq) and freq[-1] in my_prefixes:
|
||||
multiplier = 10 ** (my_prefixes.index(freq[-1]) * 3)
|
||||
freq = freq[:-1]
|
||||
|
||||
if freq.isnumeric():
|
||||
return int(freq) * multiplier
|
||||
|
||||
parser = Value(0, "Hz")
|
||||
try:
|
||||
f = float(freq)
|
||||
return int(round(multiplier * f))
|
||||
except ValueError:
|
||||
# Okay, we couldn't parse this however much we tried.
|
||||
return round(parser.parse(freq))
|
||||
except (ValueError, IndexError):
|
||||
return -1
|
||||
|
||||
@staticmethod
|
||||
|
|
Ładowanie…
Reference in New Issue