Last active
January 10, 2025 13:58
-
-
Save jh4xsy/a45da2f1946e997fe09fb838f8157efd to your computer and use it in GitHub Desktop.
KASHIWAビーコン(SatNOGS DBの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 | |
| # | |
| # GARDENs衛星でSatNOGS CSVファイルからカメラ画像を生成 by JH4XSY/1 | |
| # 2025/1/10 | |
| def find_gaps(list) | |
| # Find the minimum and maximum values in the list. | |
| min_value = list.min | |
| max_value = list.max | |
| # Create an array to store the gaps. | |
| gaps = [] | |
| # Iterate over the range of values from the minimum to the maximum. | |
| (min_value..max_value).each do |value| | |
| # If the value is not in the list, then it is a gap. | |
| if !list.include?(value) | |
| gaps << value | |
| end | |
| end | |
| # Return the array of gaps. | |
| return gaps | |
| end | |
| # ---8<--- | |
| if ARGV.empty? | |
| puts "usage: ruby ext.rb ID" | |
| exit 1 | |
| end | |
| ID = ARGV[0] | |
| pattern = "4A4736594257304A4736594D58303EF0FFF0FF#{ID}" | |
| dummy = " " * 61 + "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" | |
| input = "wip.csv" | |
| output = "foo-#{ID}" | |
| # CSVデータをメモリに読み込む | |
| csv_data = File.readlines(input) | |
| # カメラ画像データを格納する配列 | |
| image_data = [] | |
| # シーケンス番号0から600までを検索 | |
| (0..599).each do |i| | |
| hex_id = format('%04X', i) | |
| target_line = nil | |
| # 各行を走査してパターンに一致する行を探す | |
| csv_data.each do |line| | |
| if line.include?(pattern + hex_id) | |
| target_line = line | |
| break # 一致する行が見つかったらループを抜ける | |
| end | |
| end | |
| # 一致する行がなかった場合はdummy文字列を追加 | |
| image_data << (target_line || dummy) | |
| # 欠落データの有無をO/Xで表示 | |
| print target_line ? "O" : "." | |
| # EOIを検出してループを抜ける | |
| if target_line&.include?("FFD9"+"00") | |
| puts "\nFFD9 found, stopping process." | |
| output = output + ".jpg" | |
| break | |
| end | |
| if target_line&.include?("49454E44") | |
| puts "\n49454E44 found, stopping process." | |
| output = output + ".png" | |
| break | |
| end | |
| end | |
| print("\n") | |
| # 抽出したデータをファイル書き出し | |
| f=open("foo.hex","w") | |
| f.puts image_data | |
| f.close | |
| # 抽出したデータのギャップを探す | |
| list = [] | |
| File.open("foo.hex", "r") do |f| | |
| f.each_line do |line| | |
| seq = line.slice(60, 4) | |
| num = seq.to_i(16) | |
| list << num | |
| end | |
| end | |
| gaps = find_gaps(list) | |
| puts "Gap detected : #{gaps}" | |
| # 配列の内容を1つの文字列に結合 | |
| foo_hex = image_data.join | |
| # 画像データだけ切り取る | |
| foo2_hex = foo_hex.lines.map { |line| | |
| line.strip.empty? ? nil : line[64..185] | |
| }.compact.join | |
| # 16進データをバイナリに変換してファイル出力 | |
| # EOIが見つからない場合、拡張子を.jpgに設定 | |
| output += ".jpg" unless output.include?(".") | |
| File.open(output, 'wb') do |file| | |
| file.write([foo2_hex].pack('H*')) | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment