aboutsummaryrefslogblamecommitdiff
path: root/calctenna.py
blob: 8bf9bb35f214ae15d2be0c640b454d93470470f1 (plain) (tree)
1
2
3
4
5
6
7
8







                     




                               
                                                
                          










                                                                            


                                         
 



                                  







                                 












                                                                      

                                 

                               
 

               
                                            
        
                            
           

                                                          


                                             
                    

        
                                  

                                                     
                    


                                                                       
                    
 

               

                       
                                  




                                                                      


                          







                                                                         
                                                                    
                      

                      




                                                                          
                                                          
                                                    
#!/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 = frequency
        self.velocity_factor = velocity_factor
        # fill me
        self.wavelength = 0
        self.wavelength_2 = 0
        self.wavelength_4 = 0
        self.dipole_leg_length = 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 # speed of light
    antenna_data.wavelength = float(c) / float(antenna_data.frequency)
    antenna_data.wavelength_2 = antenna_data.wavelength/2
    antenna_data.wavelength_4 = antenna_data.wavelength/4
    antenna_data.dipole_leg_length = antenna_data.wavelength_4 \
            * float(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()

    antenna_data = AntennaData(args.frequency, args.velocity_factor)
    calc(antenna_data)

    if not args.quiet:
        print("Desired Frequency: " + str(antenna_data.frequency) + " Hz")
        print("Wavelength: %.3f m" % antenna_data.wavelength)
        print("Wavelength / 2: %.3f m" % antenna_data.wavelength_2)
        print("Wavelength / 4: %.3f m" % antenna_data.wavelength_4)
        print("Velocity factor: " + str(antenna_data.velocity_factor))
        print("-> Dipole leg length (lambda/4): ", end='')
    print("%.3f m" % antenna_data.dipole_leg_length)
in each repos: see "about"-tab (if existing) for more details / README.
mailto contact at omeckman dot net
all timestamps in UTC (German winter time: UTC+01:00, summer time: UTC+02:00)
dark theme is a modded version of: https://gist.github.com/Yoplitein/f4b671a2ec70c9e743fa