Skip to content
This repository has been archived by the owner on Nov 20, 2023. It is now read-only.

Commit

Permalink
Add basic geometry correction
Browse files Browse the repository at this point in the history
And a bit of a refactor...
  • Loading branch information
Xerbo committed Jun 20, 2021
1 parent 3f61413 commit e1c1464
Show file tree
Hide file tree
Showing 9 changed files with 316 additions and 186 deletions.
26 changes: 13 additions & 13 deletions src/fingerprint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ Satellite fingerprint_ccsds_frames(std::istream &stream) {
for (size_t i = 0; i < 50; i++) {
stream.read(reinterpret_cast<char *>(frame), 1024);
if (frame[0] != 0x1A || frame[1] != 0xCF || frame[2] != 0xFC || frame[3] != 0x1D) {
return Satellite_Unknown;
return Satellite::Unknown;
}
uint8_t VCID = frame[5] & 0x3f; // 0b111111
virtual_channels[VCID]++;
}

// Perform virtual channel analysis
if (virtual_channels[5] > 12) {
return Satellite_Fengyun;
return Satellite::FengYun;
} else if (virtual_channels[9] > 8) {
return Satellite_MetOp;
return Satellite::MetOp;
} else {
return Satellite_Meteor;
return Satellite::Meteor;
}
}

Expand All @@ -44,7 +44,7 @@ Satellite fingerprint_ccsds_data(std::istream &stream) {
stream.read(reinterpret_cast<char *>(buffer), 1024);
if (deframer.work(buffer, frame, 1024)) {
if (frame[10] == 0 && frame[11] == 0 && frame[12] == 0 && frame[13] == 0) {
return Satellite_Meteor;
return Satellite::Meteor;
}

frames++;
Expand All @@ -56,13 +56,13 @@ Satellite fingerprint_ccsds_data(std::istream &stream) {

if (frames > 50) {
if (virtual_channels[5] > frames/10) {
return Satellite_Fengyun;
return Satellite::FengYun;
} else if (virtual_channels[9] > frames/10) {
return Satellite_MetOp;
return Satellite::MetOp;
}
}

return Satellite_Unknown;
return Satellite::Unknown;
}

bool is_noaa(std::istream &stream) {
Expand All @@ -89,29 +89,29 @@ bool is_noaa(std::istream &stream) {
Satellite fingerprint(std::string filename) {
std::filebuf file = std::filebuf();
if (!file.open(filename, std::ios::in | std::ios::binary)) {
return Satellite_Unknown;
return Satellite::Unknown;
}
std::istream stream(&file);

Satellite satellite = fingerprint_ccsds_frames(stream);
if (satellite != Satellite_Unknown) {
if (satellite != Satellite::Unknown) {
file.close();
return satellite;
}

stream.seekg(stream.beg);
satellite = fingerprint_ccsds_data(stream);
if (satellite != Satellite_Unknown) {
if (satellite != Satellite::Unknown) {
file.close();
return satellite;
}

stream.seekg(stream.beg);
if (is_noaa(stream)) {
file.close();
return Satellite_NOAA;
return Satellite::NOAA;
}

file.close();
return Satellite_Unknown;
return Satellite::Unknown;
}
3 changes: 2 additions & 1 deletion src/fingerprint.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#ifndef LEANHRPT_FINGERPRINT_H
#define LEANHRPT_FINGERPRINT_H

#include "mainwindow.h"
#include "satinfo.h"
#include <string>

Satellite fingerprint(std::string filename);

Expand Down
56 changes: 56 additions & 0 deletions src/geometry.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* LeanHRPT Decode
* Copyright (C) 2021 Xerbo
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

#include "geometry.h"

#include <cmath>

// Convert from a internal angle of a circle to the viewing angle of a point above the circle.
float earth2sat_angle(float radius, float height, float angle) {
return -std::atan(std::sin(angle)*radius / (std::cos(angle)*radius - (radius+height)));
}

// Based off https://github.com/Xerbo/meteor_corrector
QImage correct_geometry(QImage image, Satellite satellite) {
const SatelliteInfo info = satellite_info.at(satellite);
const size_t output_width = info.swath/info.resolution;

size_t *lut = new size_t[output_width];

float view_angle = info.swath / EARTH_RADIUS;
float sat_edge = earth2sat_angle(EARTH_RADIUS, info.orbit_height, view_angle/2);

// Compute a look up table of pixel positions
for (size_t x = 0; x < output_width; x++) {
float angle = (static_cast<float>(x)/static_cast<float>(output_width) - 0.5f) * view_angle;
angle = earth2sat_angle(EARTH_RADIUS, info.orbit_height, angle);

lut[x] = (angle/sat_edge + 1.0f)/2.0f * static_cast<float>(image.width());
}

// Copy pixels over from the source to the corrected image
QImage corrected(output_width, image.height(), image.format());
for (size_t y = 0; y < static_cast<size_t>(image.height()); y++) {
for (size_t x = 0; x < output_width; x++) {
corrected.setPixel(x, y, image.pixel(lut[x], y));
}
}

delete[] lut;
return corrected;
}
28 changes: 28 additions & 0 deletions src/geometry.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* LeanHRPT Decode
* Copyright (C) 2021 Xerbo
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

#ifndef LEANHRPT_GEOMETRY_H
#define LEANHRPT_GEOMETRY_H

#include <QImage>

#include "satinfo.h"

QImage correct_geometry(QImage image, Satellite satellite);

#endif
Loading

0 comments on commit e1c1464

Please sign in to comment.