I’m not much of a programmer. I’ll be the first to admit that. But, now and then, I manage to cobble together a useful script using the Python programming language to make short work of repetitive, tedious tasks. Here’s an example.
I described my process for logging contacts in a previous post. Part of that process is keeping track of where I was at the time and what rig I was using. My main log in N3FJP’s ACLog uses the other fields feature to implement the ADIF tags, MY_QTH and MY_RIG. Adding that information to ADIF files being imported into my main logging program was time-consuming.
I had been using ADIF Master to add these fields and populate them. I still highly recommend ADIF Master but I wanted to see if I could automate things using Python.
What I ended up with is a script that prompts me to enter my location and rig. If the ADIF file doesn’t already include a tag for TX_PWR, the script prompts me to enter my transmit power also. It then generates these new tags and inserts them in each record in the file. It takes me about 15 seconds to execute the script.
As I noted earlier, I’m no programmer. So, this code might not be the most elegant—or “Pythonic,” as they say—way to approach the problem. However, it does exactly what I needed.
__author__ = "Craig LaBarge WB3GCK" __email__ = "wb3gck at arrl.net" __contact__ = "https://wb3gck.com/contact/" __date__ = "5/03/2019" __version__ = "v1.1.0" # This script is used to add my custom tags to ADIFs exported from SKCCLogger or other programs. # The script expects: # Files in ADIF located in the same working directory as the script # ADIF files must have a ".adi" or ".adif" extension. import os import sys def listfiles(): """This function finds all ADIF files in the working directory and prints out a listing with index numbers. It returns a list of available ADIF files.""" logs =  count = 1 for filename in (os.listdir('.')): if '.adi' in filename: logs.append(filename) print(str(count) + ' - ' + filename) count = count + 1 if not logs: # Check for empty list; e.g., no PDFs found print('') print('Ooops! No ADIF files found!! \nTerminating script...\n') sys.exit() else: print('') return logs print('\n*** add_tags.py ' + __version__ + ' by WB3GCK ***\n') # Prompt the user for the integer number corresponding to the desired message file. # Check to make sure the input is within the proper range good_file = False file_choice = '' file_list = listfiles() # Get a list of ADIF files file_in = '' while not good_file: file_in = input('Enter the ADIF file number: ') if not file_in.isdecimal(): # Check for non-numeric input print('Input must be a numeral! Try again, Pal!') continue else: file_nr = int(file_in) - 1 if file_nr < 0 or file_nr > (len(file_list) - 1): # Check for out of range input print('Input out of range. Better try that again, Bucko.') else: file_choice = file_list[file_nr] # Sets the selected file name good_file = True # Check the selected file for the presence of a TX_PWR tag if ('TX_PWR' in open(file_choice).read()) or ('tx_pwr' in open(file_choice).read()): has_pwr = True else: has_pwr = False # Prompt user for the MY_QTH value my_qth = input(r'Enter QTH: ') qth_tag = '<MY_QTH:' + str(len(my_qth)) + '>' + my_qth + ' ' # Prompt user for MY_RIG value my_rig = input(r'Enter Rig: ') rig_tag = '<MY_RIG:' + str(len(my_rig)) + '>' + my_rig + ' ' # Prompt user for TX_PWR if there is no TX_PWR in the selected file good_pwr = False tx_pwr = '' pwr_tag = '' if not has_pwr: while not good_pwr: tx_pwr = input('Enter TX_PWR: ') # Check for numerals and decimal points only try: val = float(tx_pwr) good_pwr = True except ValueError: print("Hey Bucko! Numerals and decimal points only!!") pwr_tag = '<TX_PWR:' + str(len(tx_pwr)) + '>' + tx_pwr + ' ' # Create the replacement tag if not has_pwr: new_tags = qth_tag + rig_tag + pwr_tag + '<EOR>' else: new_tags = qth_tag + rig_tag + '<EOR>' # Modify the selected ADIF file file = open(file_choice, 'r') filedata = file.read() # Replace the target string if '<EOR>' in open(file_choice).read(): filedata = filedata.replace('<EOR>', new_tags) elif '<eor>' in open(file_choice).read(): filedata = filedata.replace('<eor>', new_tags) else: print('Are you sure this is a valid ADIF file?? \nTerminating script...') sys.exit() file = open(file_choice, 'w') file.write(filedata) print('\nGreat success! ' + file_choice + ' modified.\n')
How to Run It
I run this script on a Windows machine, so this section is focused on that operating system. For Linux and Mac, you might need to do some research.
- Make sure you have Python 3 installed on your computer. If not, you can get it for free at python.org.
- Download the script file here and unzip it.
- Place the ADIF file you want to modify in the same folder as the script file.
- Open a command window and navigate to the directory containing the script and ADIF file.
- Start the script with the command: python add_tags.py. I use a Windows batch file to save having to save some typing. Depending on how Python is installed on your system, you might be able to just double-click the script file.
- Following the prompts, enter the necessary information. That’s all there is to it.
- This script doesn’t check to see if you have already modified the ADIF file. If you have, you’ll end up with redundant fields in your ADIF file. Your logging program probably won’t like that.
- Always keep a backup of your ADIF file or have an easy way to regenerate it. If you make a mistake in one of your inputs, just start over with another copy of the ADIF file.
- I’ve only included a minimal amount of error checking in the script, so it’s far from bullet-proof. Double-check each input carefully before pressing <ENTER>.
Of course, no article on software is complete without a disclaimer or two.
- Use this script at your own risk. It works for me, but I make no guarantees that it will work for you.
- I can’t offer any technical support or tailor it to your application.
So there you have it. If you find yourself having to routinely add fields to an ADIF file—maybe not, but you never know—feel free to modify this script to suit your needs. It should be easy to adapt it to add other ADIF tags.
If you want to dabble in Python programming, Google is your friend. There are tons of resources out there for learning Python.
73, Craig WB3GCK