File: module_aws_instruments.cpp

package info (click to toggle)
satdump 1.2.2%2Bgb79af48-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 81,648 kB
  • sloc: cpp: 276,768; ansic: 164,598; lisp: 1,219; sh: 283; xml: 106; makefile: 7
file content (227 lines) | stat: -rw-r--r-- 8,429 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include "module_aws_instruments.h"
#include <fstream>
#include "logger.h"
#include <filesystem>
#include "imgui/imgui.h"
#include "common/utils.h"
#include "products/image_products.h"
#include "common/ccsds/ccsds_tm/demuxer.h"
#include "common/ccsds/ccsds_tm/vcdu.h"
#include "products/dataset.h"
#include "resources.h"
#include "nlohmann/json_utils.h"

#define AWS_SCID 104
#define AWS_NORAD 60543

namespace aws
{
    AWSInstrumentsDecoderModule::AWSInstrumentsDecoderModule(std::string input_file, std::string output_file_hint, nlohmann::json parameters) : ProcessingModule(input_file, output_file_hint, parameters)
    {
    }

    void AWSInstrumentsDecoderModule::process()
    {
        filesize = getFilesize(d_input_file);
        std::ifstream data_in(d_input_file, std::ios::binary);

        logger->info("Using input frames " + d_input_file);

        time_t lastTime = 0;
        uint8_t cadu[1279];

        logger->info("Demultiplexing and deframing...");

        ccsds::ccsds_tm::Demuxer demuxer_vcid2(1109, false);
        ccsds::ccsds_tm::Demuxer demuxer_vcid3(1109, false);

        std::vector<uint8_t> aws_scids;

        while (!data_in.eof())
        {
            // Read buffer
            data_in.read((char *)cadu, 1279);

            // Parse this transport frame
            ccsds::ccsds_tm::VCDU vcdu = ccsds::ccsds_tm::parseVCDU(cadu);

            if (vcdu.spacecraft_id == AWS_SCID)
                aws_scids.push_back(vcdu.spacecraft_id);

            if (vcdu.vcid == 2) // Dump
            {
                std::vector<ccsds::CCSDSPacket> ccsdsFrames = demuxer_vcid2.work(cadu);
                for (ccsds::CCSDSPacket &pkt : ccsdsFrames)
                    if (pkt.header.apid == 100)
                        sterna_dump_reader.work(pkt);
                    else if (pkt.header.apid == 51)
                        navatt_reader.work(pkt);
            }
            else if (vcdu.vcid == 3) //  DB
            {
                std::vector<ccsds::CCSDSPacket> ccsdsFrames = demuxer_vcid3.work(cadu);
                for (ccsds::CCSDSPacket &pkt : ccsdsFrames)
                    if (pkt.header.apid == 100)
                        sterna_reader.work(pkt);
                    else if (pkt.header.apid == 51)
                        navatt_reader.work(pkt);
            }

            progress = data_in.tellg();

            if (time(NULL) % 10 == 0 && lastTime != time(NULL))
            {
                lastTime = time(NULL);
                logger->info("Progress " + std::to_string(round(((double)progress / (double)filesize) * 1000.0) / 10.0) + "%%");
            }
        }

        data_in.close();

        int scid = most_common(aws_scids.begin(), aws_scids.end(), 0);
        aws_scids.clear();

        std::string sat_name = "Unknown AWS";
        if (scid == AWS_SCID)
            sat_name = "AWS";

        int norad = 0;
        if (scid == AWS_SCID)
            norad = AWS_NORAD;

        // Products dataset
        satdump::ProductDataSet dataset;
        dataset.satellite_name = sat_name;
        dataset.timestamp = get_median(sterna_reader.timestamps);

        std::optional<satdump::TLE> satellite_tle = satdump::general_tle_registry->get_from_norad_time(norad, dataset.timestamp);

        // Satellite ID
        {
            logger->info("----------- Satellite");
            logger->info("NORAD : " + std::to_string(norad));
            logger->info("Name  : " + sat_name);
        }

        // Sterna DB
        {
            sterna_status = SAVING;
            std::string directory = d_output_file_hint.substr(0, d_output_file_hint.rfind('/')) + "/STERNA";

            if (!std::filesystem::exists(directory))
                std::filesystem::create_directory(directory);

            logger->info("----------- STERNA");
            logger->info("Lines : " + std::to_string(sterna_reader.lines));

            satdump::ImageProducts sterna_products;
            sterna_products.instrument_name = "sterna";
            sterna_products.has_timestamps = true;
            sterna_products.set_tle(satellite_tle);
            sterna_products.bit_depth = 16;
            sterna_products.timestamp_type = satdump::ImageProducts::TIMESTAMP_LINE;
            sterna_products.set_timestamps(sterna_reader.timestamps);

            nlohmann::json proj_cfg = loadJsonFile(resources::getResourcePath("projections_settings/aws_sterna.json"));
            if (d_parameters["use_ephemeris"].get<bool>())
                proj_cfg["ephemeris"] = navatt_reader.getEphem();
            sterna_products.set_proj_cfg(proj_cfg);

            for (int i = 0; i < 19; i++)
                sterna_products.images.push_back({"STERNA-" + std::to_string(i + 1), std::to_string(i + 1), sterna_reader.getChannel(i)});

            sterna_products.save(directory);
            dataset.products_list.push_back("STERNA");

            sterna_status = DONE;
        }

        // Sterna Dump
        {
            sterna_dump_status = SAVING;
            std::string directory = d_output_file_hint.substr(0, d_output_file_hint.rfind('/')) + "/STERNA_Dump";

            if (!std::filesystem::exists(directory))
                std::filesystem::create_directory(directory);

            logger->info("----------- STERNA Dump");
            logger->info("Lines : " + std::to_string(sterna_dump_reader.lines));

            satdump::ImageProducts sterna_dump_products;
            sterna_dump_products.instrument_name = "sterna";
            sterna_dump_products.has_timestamps = true;
            sterna_dump_products.set_tle(satellite_tle);
            sterna_dump_products.bit_depth = 16;
            sterna_dump_products.timestamp_type = satdump::ImageProducts::TIMESTAMP_LINE;
            sterna_dump_products.set_timestamps(sterna_dump_reader.timestamps);

            nlohmann::json proj_cfg = loadJsonFile(resources::getResourcePath("projections_settings/aws_sterna.json"));
            if (d_parameters["use_ephemeris"].get<bool>())
                proj_cfg["ephemeris"] = navatt_reader.getEphem();
            sterna_dump_products.set_proj_cfg(proj_cfg);

            for (int i = 0; i < 19; i++)
                sterna_dump_products.images.push_back({"STERNA-" + std::to_string(i + 1), std::to_string(i + 1), sterna_dump_reader.getChannel(i)});

            sterna_dump_products.save(directory);
            dataset.products_list.push_back("STERNA_Dump");

            sterna_dump_status = DONE;
        }

        dataset.save(d_output_file_hint.substr(0, d_output_file_hint.rfind('/')));
    }

    void AWSInstrumentsDecoderModule::drawUI(bool window)
    {
        ImGui::Begin("AWS Instruments Decoder", NULL, window ? 0 : NOWINDOW_FLAGS);

        if (ImGui::BeginTable("##sawsinstrumentstable", 3, ImGuiTableFlags_Borders | ImGuiTableFlags_RowBg))
        {
            ImGui::TableNextRow();
            ImGui::TableSetColumnIndex(0);
            ImGui::Text("Instrument");
            ImGui::TableSetColumnIndex(1);
            ImGui::Text("Lines / Frames");
            ImGui::TableSetColumnIndex(2);
            ImGui::Text("Status");

            ImGui::TableNextRow();
            ImGui::TableSetColumnIndex(0);
            ImGui::Text("Sterna");
            ImGui::TableSetColumnIndex(1);
            ImGui::TextColored(style::theme.green, "%d", sterna_reader.lines);
            ImGui::TableSetColumnIndex(2);
            drawStatus(sterna_status);

            ImGui::TableNextRow();
            ImGui::TableSetColumnIndex(0);
            ImGui::Text("Sterna Dump");
            ImGui::TableSetColumnIndex(1);
            ImGui::TextColored(style::theme.green, "%d", sterna_dump_reader.lines);
            ImGui::TableSetColumnIndex(2);
            drawStatus(sterna_dump_status);

            ImGui::EndTable();
        }

        ImGui::ProgressBar((double)progress / (double)filesize, ImVec2(ImGui::GetContentRegionAvail().x, 20 * ui_scale));

        ImGui::End();
    }

    std::string AWSInstrumentsDecoderModule::getID()
    {
        return "aws_instruments";
    }

    std::vector<std::string> AWSInstrumentsDecoderModule::getParameters()
    {
        return {};
    }

    std::shared_ptr<ProcessingModule> AWSInstrumentsDecoderModule::getInstance(std::string input_file, std::string output_file_hint, nlohmann::json parameters)
    {
        return std::make_shared<AWSInstrumentsDecoderModule>(input_file, output_file_hint, parameters);
    }
}