# バンド名を 'm' 形式に変換する関数
def convert_band_to_m(band):
if band.endswith("MHz"):
return BAND_MHZ_TO_M.get(band, None)
elif band in ALLOWED_BANDS:
return band
else:
print("Invalid band format.")
return None
def get_file_path():
if len(sys.argv) > 1:
return os.path.join(LOGS_DIR, sys.argv[1])
else:
file_name = input(f"Specify the ADIF log file path (default in {DEFAULT_ADIF_FILE}): ").strip()
file_path = file_name if file_name else DEFAULT_ADIF_FILE
if os.path.exists(file_path):
return file_path
else:
print(f"File not found: {file_path}")
return None
# タイムゾーンの選択を行う関数
def select_timezone():
while True:
try:
tz_input = input("Select timezone (Enter 'UTC', 'JST', 'PHT' or a custom UTC offset (e.g., UTC+8)): ").strip()
if not tz_input:
tz_input = "UTC"
if tz_input.upper() == "JST":
return pytz.timezone("Asia/Tokyo"), "JST"
elif tz_input.upper() == "PHT":
return pytz.timezone("Asia/Manila"), "PHT"
elif tz_input.upper() == "UTC":
return pytz.UTC, "UTC"
elif tz_input.upper().startswith("UTC"):
try:
offset = int(tz_input[3:])
return pytz.FixedOffset(offset * 60), f"UTC{tz_input[3:]}"
except ValueError:
print("Invalid UTC offset. Please enter a valid UTC offset like 'UTC+8'.")
else:
print("Invalid input. Please enter one of the following: 'UTC', 'JST', 'PHT' or a valid UTC offset (e.g., 'UTC+8').")
except KeyboardInterrupt:
print("\nProgram interrupted. Exiting...\n")
sys.exit(0) # プログラムを正常終了
for line in lines:
current_record += line.strip()
if line.strip() == "<EOR>":
qso_date = extract_field(current_record, "QSO_DATE")
time_on = extract_field(current_record, "TIME_ON")
band = extract_field(current_record, "BAND")
qth = extract_field(current_record, "QTH") or "Unknown"
if not qso_date or not time_on or not band:
current_record = ""
continue
if target_date and qso_date != target_date:
current_record = ""
continue
if target_month and not qso_date.startswith(target_month):
current_record = ""
continue
normalized_band = convert_band_to_m(band)
if target_band and normalized_band != target_band:
current_record = ""
continue
def display_results(results, target_band):
if not results:
print("No data found matching the criteria.")
return
total_contacts = 0
print(f"\nHourly QTH Counts for Band: {target_band}")
for hour_range, qths in results.items():
total_hour_contacts = sum(qths.values())
total_contacts += total_hour_contacts
print(f"{hour_range}:")
print(f" Total contacts: {total_hour_contacts}")
for qth, count in sorted(qths.items(), key=lambda x: x[1], reverse=True):
print(f" {count} {qth}")
print(f"\nTotal contacts for all hours: {total_contacts}")
def main():
try:
DEBUG_FLAG = False # デバッグフラグを追加
file_path = get_file_path()
if not file_path:
return
timezone, tz_label = select_timezone()
while True:
target_band = input("Enter the target band (e.g., 15m, 21MHz, or press Enter to skip): ").strip()
if not target_band or target_band in ALLOWED_BANDS or target_band in ALLOWED_MHz:
target_band = convert_band_to_m(target_band) if target_band else None
break
print("Invalid band. Please choose from:\n"
f"{', '.join(ALLOWED_BANDS)}\n"
f"{', '.join(ALLOWED_MHz)}")
target_month = input("Enter the search month\n(YYYY, YYYYMMDD, YYYY/MM, YYY/MM/DD, etc) or press Enter to search all months: ").strip()
if target_month:
if '/' in target_month:
target_month = target_month.replace('/', '')
elif len(target_month) == 6:
target_month = target_month
target_date = input("Enter the target date (YYYYMMDD) or press Enter to skip: ").strip()
# Read lines from the ADIF file
with open(file_path, 'r') as file:
lines = file.readlines()
save_results = input("Would you like to save the results to a TEXT file? (y/n): ").strip().lower()
if save_results == 'y':
result_file = input(f"Enter the file name (default: {DEFAULT_TEXT_FILE}): ").strip() or DEFAULT_TEXT_FILE
with open(result_file, 'w') as f:
f.write(f"Hourly QTH Counts for Band: {target_band}\n")
for hour_range, qths in results.items():
f.write(f"{hour_range}:\n")
for qth, count in qths.items():
f.write(f" {count} {qth}\n")
print(f"Results saved to {result_file}")
except KeyboardInterrupt:
print("\nProgram interrupted by user. Exiting...")
except Exception as e:
print(f"Error: {e}")