#!/usr/bin/env python # calctenna # antenna calculator # 2020-12-19 # Oliver Meckmann import argparse import unittest import sys class Tests(unittest.TestCase): def test_calc(self): antenna_data = AntennaData(100e6, 0.951) calc(antenna_data) self.assertEqual(antenna_data.dipole_leg_length, 0.7127565688949999) class AntennaData(): def __init__(self, frequency, velocity_factor): self.frequency_hz = frequency self.velocity_factor = velocity_factor # fill me self.wavelength_m = 0 self.wavelength_2_m = 0 self.wavelength_4_m = 0 self.dipole_leg_length_m = 0 def run_argparse(): argparser = argparse.ArgumentParser() argparser.add_argument( '-V', '--velocity-factor', metavar='velocity_factor', default='0.951' ) argparser.add_argument( '-q', '--quiet', help='print only result', action='store_true' ) argparser.add_argument( 'frequency', metavar='frequency', help='desired frequency in Hz (e.g. 144800000 or 433e6, ...)', nargs='?' ) argparser.add_argument( '-t', '--tests', help='run unit tests and exit', action='store_true' ) args = argparser.parse_args() if not args.frequency: argparser.print_usage() return args def check_input(frequency, velocity_factor): try: f = float(frequency) except: print("Sorry, frequency input invalid or missing") return False if f <= 0.0: print("Sorry, frequency must be > 0") return False try: V = float(velocity_factor) except: print("Sorry, velocity factor input invalid") return False if V < 0.0 or V > 1.0: print("Sorry, velocity factor can only be between 0.0 and 1.0") return False return True # fill struct def calc(antenna_data): c = 299792458.0 # speed of light antenna_data.wavelength_m = c / antenna_data.frequency_hz antenna_data.wavelength_2_m = antenna_data.wavelength_m/2 antenna_data.wavelength_4_m = antenna_data.wavelength_m/4 antenna_data.dipole_leg_length_m = antenna_data.wavelength_4_m \ * antenna_data.velocity_factor if __name__ == '__main__': args = run_argparse() if args.tests: # override args for unittest: argv[0] to have name in test output unittest.main(argv=[sys.argv[0], '--verbose'], exit=True) if not check_input(args.frequency, args.velocity_factor): exit() # check_input OK -> float() antenna_data = AntennaData(float(args.frequency), float(args.velocity_factor)) calc(antenna_data) if not args.quiet: print("Desired Frequency: " + str(antenna_data.frequency_hz) + " Hz") print("Wavelength: %.3f m" % antenna_data.wavelength_m) print("Wavelength / 2: %.3f cm" % (antenna_data.wavelength_2_m*100)) print("Wavelength / 4: %.3f cm" % (antenna_data.wavelength_4_m*100)) print("Velocity factor: " + str(antenna_data.velocity_factor)) print("-> Dipole leg length (lambda/4): ", end='') print("%.3f cm" % (antenna_data.dipole_leg_length_m*100))