设计Rinex O文件和Rinex N文件读取类,实现对Rinex文件内容的读取和显示.h.cpp.main
时间: 2023-06-20 13:06:25 浏览: 88
以下是 Rinex 文件读取类的头文件(RinexReader.h):
```c++
#ifndef RINEX_READER_H
#define RINEX_READER_H
#include <fstream>
#include <vector>
#include <string>
class RinexReader {
public:
RinexReader(std::string filename);
bool is_valid() const;
std::string get_rinex_version() const;
std::vector<std::string> get_satellite_systems() const;
std::vector<std::string> get_observation_types(std::string system) const;
std::vector<std::string> get_observation_types() const;
std::vector<std::string> get_prn_list(std::string system) const;
std::vector<std::string> get_prn_list() const;
std::vector<std::vector<double>> get_observation_data(std::string prn, std::string type) const;
private:
std::ifstream m_file;
std::string m_rinex_version;
std::vector<std::string> m_satellite_systems;
std::vector<std::vector<std::string>> m_observation_types;
std::vector<std::vector<std::string>> m_prn_list;
std::vector<std::vector<std::vector<double>>> m_observation_data;
bool read_header();
bool read_observation_data();
};
#endif // RINEX_READER_H
```
以下是 Rinex 文件读取类的实现文件(RinexReader.cpp):
```c++
#include "RinexReader.h"
#include <sstream>
#include <iostream>
RinexReader::RinexReader(std::string filename) : m_file(filename) {
if (!is_valid()) {
std::cerr << "Error opening file: " << filename << std::endl;
return;
}
if (!read_header()) {
std::cerr << "Error reading header in file: " << filename << std::endl;
return;
}
if (!read_observation_data()) {
std::cerr << "Error reading observation data in file: " << filename << std::endl;
return;
}
}
bool RinexReader::is_valid() const {
return m_file.is_open();
}
bool RinexReader::read_header() {
std::string line;
while (std::getline(m_file, line)) {
if (line.find("RINEX VERSION / TYPE") != std::string::npos) {
m_rinex_version = line.substr(0, 9);
} else if (line.find("SYS / # / OBS TYPES") != std::string::npos) {
m_satellite_systems.push_back(line.substr(0, 1));
std::stringstream ss(line.substr(2));
int num_obs_types;
ss >> num_obs_types;
std::vector<std::string> obs_types;
for (int i = 0; i < num_obs_types; ++i) {
std::string obs_type;
ss >> obs_type;
obs_types.push_back(obs_type);
}
m_observation_types.push_back(obs_types);
} else if (line.find("END OF HEADER") != std::string::npos) {
return true;
}
}
return false;
}
bool RinexReader::read_observation_data() {
std::string line;
while (std::getline(m_file, line)) {
std::stringstream ss(line);
std::string prn;
ss >> prn;
if (prn.find("COMMENT") != std::string::npos) {
continue;
}
if (prn.find("MARKER NAME") != std::string::npos) {
continue;
}
if (prn.find("RINEX VERSION / TYPE") != std::string::npos) {
continue;
}
if (prn.find("SYS / # / OBS TYPES") != std::string::npos) {
continue;
}
std::vector<std::vector<double>> obs_data;
for (int i = 0; i < m_observation_types.size(); ++i) {
std::vector<double> obs_types_data;
for (int j = 0; j < m_observation_types[i].size(); ++j) {
double data;
ss >> data;
obs_types_data.push_back(data);
}
obs_data.push_back(obs_types_data);
}
m_prn_list.push_back({prn});
m_observation_data.push_back(obs_data);
}
return true;
}
std::string RinexReader::get_rinex_version() const {
return m_rinex_version;
}
std::vector<std::string> RinexReader::get_satellite_systems() const {
return m_satellite_systems;
}
std::vector<std::string> RinexReader::get_observation_types(std::string system) const {
int index = -1;
for (int i = 0; i < m_satellite_systems.size(); ++i) {
if (m_satellite_systems[i] == system) {
index = i;
break;
}
}
if (index == -1) {
return {};
}
return m_observation_types[index];
}
std::vector<std::string> RinexReader::get_observation_types() const {
std::vector<std::string> obs_types;
for (int i = 0; i < m_observation_types.size(); ++i) {
for (int j = 0; j < m_observation_types[i].size(); ++j) {
obs_types.push_back(m_observation_types[i][j]);
}
}
return obs_types;
}
std::vector<std::string> RinexReader::get_prn_list(std::string system) const {
int index = -1;
for (int i = 0; i < m_satellite_systems.size(); ++i) {
if (m_satellite_systems[i] == system) {
index = i;
break;
}
}
if (index == -1) {
return {};
}
std::vector<std::string> prn_list;
for (int i = 0; i < m_prn_list.size(); ++i) {
prn_list.push_back(m_prn_list[i][0]);
}
return prn_list;
}
std::vector<std::string> RinexReader::get_prn_list() const {
std::vector<std::string> prn_list;
for (int i = 0; i < m_prn_list.size(); ++i) {
prn_list.push_back(m_prn_list[i][0]);
}
return prn_list;
}
std::vector<std::vector<double>> RinexReader::get_observation_data(std::string prn, std::string type) const {
int prn_index = -1;
for (int i = 0; i < m_prn_list.size(); ++i) {
if (m_prn_list[i][0] == prn) {
prn_index = i;
break;
}
}
if (prn_index == -1) {
return {};
}
int type_index = -1;
for (int i = 0; i < m_observation_types.size(); ++i) {
for (int j = 0; j < m_observation_types[i].size(); ++j) {
if (m_observation_types[i][j] == type) {
type_index = j;
break;
}
}
if (type_index != -1) {
break;
}
}
if (type_index == -1) {
return {};
}
return m_observation_data[prn_index][type_index];
}
```
以下是 Rinex 文件读取类的使用示例(main.cpp):
```c++
#include "RinexReader.h"
#include <iostream>
int main() {
RinexReader reader("sample.rinex");
if (!reader.is_valid()) {
std::cerr << "Error opening file" << std::endl;
return 1;
}
std::cout << "RINEX Version: " << reader.get_rinex_version() << std::endl;
std::cout << "Satellite Systems: ";
auto systems = reader.get_satellite_systems();
for (auto& system : systems) {
std::cout << system << " ";
}
std::cout << std::endl;
std::cout << "Observation Types: ";
auto obs_types = reader.get_observation_types();
for (auto& type : obs_types) {
std::cout << type << " ";
}
std::cout << std::endl;
std::cout << "PRN List: ";
auto prn_list = reader.get_prn_list();
for (auto& prn : prn_list) {
std::cout << prn << " ";
}
std::cout << std::endl;
std::cout << "Observation Data for PRN G01, Type C1C: " << std::endl;
auto obs_data = reader.get_observation_data("G01", "C1C");
for (auto& data : obs_data) {
std::cout << data << " ";
}
std::cout << std::endl;
return 0;
}
```
阅读全文