Last active
March 31, 2021 21:11
-
-
Save annard/4d82076087197aba4866ac37843e3845 to your computer and use it in GitHub Desktop.
Helper program for SDRUno memory bank files provided by Mike KD2KOG. Converts antenna values in the S1B files provided in the input directory. Puts the resulting files in the output directory using Python 3. The definition is set in the global variables where you can define the frequency range for each antenna if you have an RSPDuo.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import getopt | |
import sys | |
import csv | |
# Atenna definitions as used by SDRuno | |
ANT_A = 'AntA' | |
ANT_B = 'AntB' | |
# Frquency ranges for the above antenna definitions. | |
ANT_A_BOUNDS = range(25000000 - 1) | |
ANT_B_BOUNDS = range(25000000, 2000000000) | |
def antenna_from_frequency(freq_str, ant_str): | |
if freq_str == '': | |
return '' | |
try: | |
freq = int(freq_str) | |
ant = map_frequency(freq) | |
except ValueError: | |
ant = ant_str | |
return ant | |
def map_frequency(freq): | |
if freq in ANT_A_BOUNDS: | |
return ANT_A | |
elif freq in ANT_B_BOUNDS: | |
return ANT_B | |
else: | |
return ant_str | |
def process_bank(file_path): | |
output_rows = [] | |
with open(file_path, newline='') as csvfile: | |
bank_reader = csv.reader(csvfile, delimiter=',') | |
for row in bank_reader: | |
last_index = len(row) - 1 | |
ant_value = antenna_from_frequency(row[0], row[last_index]) | |
row[last_index] = ant_value | |
output_rows.append(row) | |
return output_rows | |
def main(): | |
short_opts = "hi:o:" | |
long_opts = ["help", "input_dir=", "output_dir="] | |
try: | |
arguments, values = getopt.getopt(sys.argv[1:], short_opts, long_opts) | |
except getopt.GetoptError as err: | |
print(err) | |
sys.exit(2) | |
# Evaluate given options | |
for current_argument, current_value in arguments: | |
if current_argument in ("-h", "--help"): | |
print ("Converts antenna values in Steve's S1B files provided in the input directory. Puts the resulting files in the output directory.") | |
sys.exit(1) | |
elif current_argument in ("-i", "--input_dir"): | |
input_dir = current_value | |
elif current_argument in ("-o", "--output_dir"): | |
output_dir = current_value | |
else: | |
assert False, "unhandled option" | |
if os.path.exists(output_dir) == False: | |
os.mkdir(output_dir) | |
for entry in os.scandir(input_dir): | |
if entry.path.endswith(".s1b") and entry.is_file(): | |
filename = os.path.basename(entry.path) | |
print(f"Processing {filename}") | |
rows = process_bank(entry.path) | |
output_filepath = os.path.join(output_dir, filename) | |
print(f"Writing result to {output_filepath}") | |
with open(output_filepath, 'w', newline='') as csvfile: | |
bank_writer = csv.writer(csvfile, delimiter=',', quoting=csv.QUOTE_MINIMAL) | |
bank_writer.writerows(rows) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example of use (on Unix since I don't develop on a PC):
Output: