satdump/plugins/stereo_support/stereo/module_stereo_instruments.cpp

196 lines
6.9 KiB
C++
Raw Permalink Normal View History

2023-07-01 18:11:08 +02:00
#include "module_stereo_instruments.h"
#include "common/ccsds/ccsds_tm/demuxer.h"
#include "common/ccsds/ccsds_tm/vcdu.h"
2023-07-01 18:11:08 +02:00
#include "common/utils.h"
#include "image/io.h"
#include "imgui/imgui.h"
#include "logger.h"
#include <cstdint>
#include <filesystem>
#include <fstream>
2023-07-01 18:11:08 +02:00
#include "core/resources.h"
2023-07-02 00:43:35 +02:00
2023-07-01 18:11:08 +02:00
namespace stereo
{
StereoInstrumentsDecoderModule::StereoInstrumentsDecoderModule(std::string input_file, std::string output_file_hint, nlohmann::json parameters)
: satdump::pipeline::base::FileStreamToFileStreamModule(input_file, output_file_hint, parameters)
2023-07-01 18:11:08 +02:00
{
fsfsm_enable_output = false;
2023-07-01 18:11:08 +02:00
}
2024-05-09 01:16:08 +02:00
image::Image StereoInstrumentsDecoderModule::decompress_icer_tool(uint8_t *data, int dsize, int size)
2023-07-01 18:11:08 +02:00
{
std::ofstream("./stereo_secchi_raw.tmp").write((char *)data, dsize);
if (std::filesystem::exists("./stereo_secchi_out.tmp"))
std::filesystem::remove("./stereo_secchi_out.tmp");
std::string icer_path = d_parameters["icer_path"];
std::string cmd = icer_path + " -vv -i ./stereo_secchi_raw.tmp -o ./stereo_secchi_out.tmp";
if (!std::filesystem::exists(icer_path))
{
logger->error("No ICER Decompressor provided. Can't decompress SECCHI!");
2024-05-09 01:16:08 +02:00
return image::Image();
2023-07-01 18:11:08 +02:00
}
int ret = system(cmd.data());
if (ret == 0 && std::filesystem::exists("./stereo_secchi_out.tmp"))
{
logger->trace("SECCHI Decompression OK!");
std::ifstream data_in("./stereo_secchi_out.tmp", std::ios::binary);
uint16_t *buffer = new uint16_t[size * size];
data_in.read((char *)buffer, sizeof(uint16_t) * size * size);
2024-05-09 01:16:08 +02:00
image::Image img(buffer, 16, size, size, 1);
2023-07-01 18:11:08 +02:00
delete[] buffer;
if (std::filesystem::exists("./stereo_secchi_out.tmp"))
std::filesystem::remove("./stereo_secchi_out.tmp");
return img;
}
else
{
logger->error("Failed decompressing SECCHI!");
if (std::filesystem::exists("./stereo_secchi_out.tmp"))
std::filesystem::remove("./stereo_secchi_out.tmp");
2024-05-09 01:16:08 +02:00
return image::Image();
2023-07-01 18:11:08 +02:00
}
}
void StereoInstrumentsDecoderModule::process()
{
uint8_t cadu[1119];
2023-07-09 17:11:39 +02:00
// std::ofstream output("file.ccsds");
2023-07-01 18:11:08 +02:00
int s_waves_lines = 0;
std::vector<uint8_t> s_waves_data;
std::string directory = d_output_file_hint.substr(0, d_output_file_hint.rfind('/'));
2023-07-02 00:43:35 +02:00
std::filesystem::create_directories(directory + "/SECCHI/");
2023-07-01 18:11:08 +02:00
2023-07-12 00:48:36 +02:00
secchi_reader = new secchi::SECCHIReader(d_parameters["icer_path"], directory + "/SECCHI");
2023-07-02 00:43:35 +02:00
while (should_run())
2023-07-01 18:11:08 +02:00
{
// Read buffer
read_data((uint8_t *)&cadu, 1119);
2023-07-01 18:11:08 +02:00
// Parse this transport frame
ccsds::ccsds_tm::VCDU vcdu = ccsds::ccsds_tm::parseVCDU(cadu);
2023-07-01 18:11:08 +02:00
// logger->info(pkt.header.apid);
// logger->info(vcdu.vcid);
2023-08-06 17:22:09 +02:00
// uint8_t *tttt = &cadu[4 + 7];
// double timestamp = (tttt[0] << 24 |
// tttt[1] << 16 |
// tttt[2] << 8 |
// tttt[3]) +
// tttt[4] / 256.0 +
// 1161734400 + 43200;
2023-07-02 00:43:35 +02:00
// logger->trace(timestamp_to_string(timestamp));
2023-07-01 18:11:08 +02:00
if (vcdu.vcid == 7) // Space Beacon VCID
{
for (int i = 0; i < 4; i++) // Extract all 4 packets. Hacky but hey works.
{
ccsds::CCSDSPacket pkt;
pkt.header = ccsds::parseCCSDSHeader(&cadu[25 + i * 272]);
pkt.payload.insert(pkt.payload.end(), &cadu[25 + i * 272 + 6], &cadu[25 + i * 272 + 6 + 266]);
if (pkt.header.apid == 1393) // S/WAVES
{
s_waves_data.insert(s_waves_data.end(), (char *)&pkt.payload[20], (char *)&pkt.payload[20 + 162]);
s_waves_lines++;
}
else if (pkt.header.apid == 1140) // SECCHI SUV
{
2023-07-02 00:43:35 +02:00
secchi_reader->work(pkt);
2023-07-01 18:11:08 +02:00
}
else if (pkt.header.apid == 1139) // SECCHI NotSureYet?
{
2023-07-02 00:43:35 +02:00
secchi_reader->work(pkt);
2023-07-01 18:11:08 +02:00
}
else if (pkt.header.apid == 1138) // SECCHI NotSureYet?
{
2023-07-02 00:43:35 +02:00
secchi_reader->work(pkt);
2023-07-01 18:11:08 +02:00
}
else if (pkt.header.apid == 1137) // SECCHI Occulted?
{
2023-07-02 00:43:35 +02:00
secchi_reader->work(pkt);
2023-07-01 18:11:08 +02:00
}
else if (pkt.header.apid == 880) {}
else if (pkt.header.apid == 624) {}
2023-07-01 18:11:08 +02:00
else if (pkt.header.apid == 0) // IDK
{
}
else if (pkt.header.apid != 2047)
{
2023-07-09 17:11:39 +02:00
// printf("APID %d\n", pkt.header.apid);
2023-07-01 18:11:08 +02:00
}
}
}
}
2023-07-02 00:43:35 +02:00
delete secchi_reader;
cleanup();
2023-07-01 18:11:08 +02:00
{
logger->info("----------- S/WAVES");
logger->info("Lines : " + std::to_string(s_waves_lines));
2024-05-09 01:16:08 +02:00
image::Image image_s_waves(s_waves_data.data(), 8, 162, s_waves_lines, 1);
image::save_img(image_s_waves, directory + "/S_WAVES");
2023-07-01 18:11:08 +02:00
}
}
void StereoInstrumentsDecoderModule::drawUI(bool window)
{
ImGui::Begin("Stereo Instruments Decoder", NULL, window ? 0 : NOWINDOW_FLAGS);
/*if (ImGui::BeginTable("##aiminstrumentstable", 3, ImGuiTableFlags_Borders | ImGuiTableFlags_RowBg))
{
ImGui::TableNextRow();
ImGui::TableSetColumnIndex(0);
ImGui::Text("Instrument");
ImGui::TableSetColumnIndex(1);
ImGui::Text("Images / Frames");
ImGui::TableSetColumnIndex(2);
ImGui::Text("Status");
for (int i = 0; i < 4; i++)
{
ImGui::TableNextRow();
ImGui::TableSetColumnIndex(0);
ImGui::Text("CIPS %d", i + 1);
ImGui::TableSetColumnIndex(1);
2024-01-21 14:57:41 -05:00
ImGui::TextColored(style::theme.green, "%d", (int)cips_readers[i].images.size());
2023-07-01 18:11:08 +02:00
ImGui::TableSetColumnIndex(2);
drawStatus(cips_status[i]);
}
ImGui::EndTable();
}*/
drawProgressBar();
2023-07-01 18:11:08 +02:00
ImGui::End();
}
std::string StereoInstrumentsDecoderModule::getID() { return "stereo_instruments"; }
2023-07-01 18:11:08 +02:00
std::shared_ptr<satdump::pipeline::ProcessingModule> StereoInstrumentsDecoderModule::getInstance(std::string input_file, std::string output_file_hint, nlohmann::json parameters)
2023-07-01 18:11:08 +02:00
{
return std::make_shared<StereoInstrumentsDecoderModule>(input_file, output_file_hint, parameters);
}
} // namespace stereo