Last active
October 14, 2024 00:46
-
-
Save jh4xsy/b7f2aa32ec6389ad20e57c941ae7050f to your computer and use it in GitHub Desktop.
adiファイルからHamlog用CSVファイルに変換するスクリプト
This file contains hidden or 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
| #!/usr/bin/env ruby | |
| # | |
| # WSJT-Xから抽出したadiファイルからHamlogに食わせるCSVファイルを作成するスクリプト | |
| # 2024/10/14 JH4XSY/1 Iwamoto | |
| # | |
| require 'time' | |
| require 'csv' | |
| # 日本のコールサインを検出するメソッド | |
| def japanese_callsign?(callsign) | |
| # 日本のコールサインのパターン | |
| pattern = /\A(J[A-Z]|7[J-N]|8[J-M])[0-9][A-Z]{1,3}\z/ | |
| !!(callsign =~ pattern) | |
| end | |
| # 日付を整形するメソッド | |
| def format_date(date) | |
| year = date[2..3] | |
| month = date[4..5] | |
| day = date[6..7] | |
| "#{year}/#{month}/#{day}" | |
| end | |
| # 日付を整形するメソッド(JST) | |
| def format_date1(date, time) | |
| year = date[2..3] | |
| month = date[4..5] | |
| day = date[6..7] | |
| # GMTの時刻をパース | |
| gmt_time = Time.strptime(time, "%H%M%S") | |
| # JSTに変換 | |
| jst_time = gmt_time + 9 * 60 * 60 | |
| # JSTの時刻がUTCの日付を超えている場合、日付を1日進める | |
| if jst_time.day != gmt_time.day | |
| day = (day.to_i + 1).to_s.rjust(2, '0') | |
| end | |
| "#{year}/#{month}/#{day}" | |
| end | |
| # 時間を整形するメソッド(GMT) | |
| def format_time(time) | |
| hour = time[0..1] | |
| minute = time[2..3] | |
| "#{hour}:#{minute}U" | |
| end | |
| # 時間を整形するメソッド(JST) | |
| def format_time1(time) | |
| # GMTの時刻をパース | |
| gmt_time = Time.strptime(time, "%H%M%S") | |
| # JSTに変換 | |
| jst_time = gmt_time + 9 * 60 * 60 | |
| # フォーマットして表示 | |
| jst_time.strftime("%H:%MJ") | |
| end | |
| # 周波数を整形するメソッド | |
| def format_freq(freq) | |
| truncated_str = freq.sub(/\..*/, '') # 小数点以下を削除 | |
| end | |
| # ADIファイルを読み込み、CSVファイルに出力 | |
| f = open("foo.adi", "r") | |
| csv = CSV.open("foo.csv", "w", force_quotes: true, encoding: "SJIS") | |
| while f.gets() | |
| file_content = $_ | |
| # 正規表現を使ってタグと値を抽出 | |
| data = {} | |
| file_content.scan(/<(\w+):\d+>([^<]+)/) do |tag, value| | |
| data[tag.to_sym] = value.strip | |
| end | |
| # 日時間の変換 | |
| if japanese_callsign?(data[:call]) | |
| date1 = format_date1(data[:qso_date], data[:time_on]) | |
| time1 = format_time1(data[:time_on]) | |
| else | |
| date1 = format_date(data[:qso_date]) | |
| time1 = format_time(data[:time_on]) | |
| end | |
| # 周波数を丸める | |
| freq1 = format_freq(data[:freq]) | |
| # CSV出力 | |
| csv << [data[:call], date1, time1, data[:rst_sent], data[:rst_rcvd], freq1, data[:mode], nil ,data[:gridsquare], "J *", nil, nil, "%埼玉県草加市移動%", nil, "0"] | |
| end | |
| f.close | |
| csv.close |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment