From 8ae787014bd18df96363e32375ccb17e9a6d4f5e Mon Sep 17 00:00:00 2001
From: Evan Pratten <evan@ewpratten.com>
Date: Fri, 14 May 2021 10:51:00 -0400
Subject: [PATCH] Import baofeng2gqrx

---
 scripts/baofeng2gqrx | 95 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 95 insertions(+)
 create mode 100644 scripts/baofeng2gqrx

diff --git a/scripts/baofeng2gqrx b/scripts/baofeng2gqrx
new file mode 100644
index 0000000..d21bff9
--- /dev/null
+++ b/scripts/baofeng2gqrx
@@ -0,0 +1,95 @@
+import argparse
+import sys
+import csv
+import os
+
+
+def main() -> int:
+
+    # Handle program arguments
+    ap = argparse.ArgumentParser(
+        prog="baofeng2gqrx",
+        description="Converts Baofeng configuration CSV files to GQRX bookmarks",
+    )
+    ap.add_argument("infile", help="Input radio CSV file")
+    ap.add_argument(
+        "-o",
+        "--outfile",
+        help="Optional output file (if not set, stdout is used)",
+        default="-",
+        required=False,
+    )
+    ap.add_argument(
+        "--simplex-overrides",
+        help="Comma-seperated list of channel prefixes to specify as SIMPLEX",
+        default="VCALL,UCALL,SMPX,ARES",
+        required=False,
+    )
+    args = ap.parse_args()
+
+    # Ensure valid infile
+    if not os.path.isfile(args.infile):
+        print(f"Invalid input file: {args.infile}")
+        return 1
+
+    # Load the Baofeng file
+    baofeng_reader = csv.reader(open(args.infile, "r"))
+
+    # Create an output buffer
+    output = []
+
+    # Parse the baofeng file
+    for i, row in enumerate(baofeng_reader):
+        if i > 0:
+            output.append(
+                {
+                    "Name": row[1],
+                    "Frequency": int(float(row[2]) * 1000000),
+                    # These are the same for all FM channels
+                    "Modulation": "Narrow FM",
+                    "Bandwidth": 10000,
+                    # The tag depends on the name
+                    "Tags": (
+                        "HAM: Simplex"
+                        if any(
+                            [
+                                True
+                                for prefix in args.simplex_overrides.split(",")
+                                if row[1].startswith(prefix)
+                            ]
+                        )
+                        else "HAM: Repeaters"
+                    ),
+                }
+            )
+
+    # Build file contents
+    file = """
+# AUTO-GENERATED
+# Tag name          ;  color
+HAM: Simplex; #0394fc
+HAM: Repeaters; #fcc203
+
+# Frequency ; Name                     ; Modulation          ;  Bandwidth; Tags
+"""
+    for entry in output:
+        file += "{frequency}; {name}; {modulation}; {bandwidth}; {tags}\n".format(
+            frequency=entry["Frequency"],
+            name=entry["Name"],
+            modulation=entry["Modulation"],
+            bandwidth=entry["Bandwidth"],
+            tags=entry["Tags"],
+        )
+
+    # Handle output
+    if args.outfile == "-":
+        print(file)
+    else:
+        with open(args.outfile, "r") as fp:
+            fp.write(file)
+
+    return 0
+
+
+if __name__ == "__main__":
+    sys.exit(main())