#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#define MAXRINEX 1024
#define MAX_SATELLITES 200
// 卫星结构体
typedef struct {
char prn[10]; // 卫星PRN号 (G01, C01, E01等)
char system; // 系统标识 (G, C, E)
double freq1_north, freq1_east, freq1_up; // 频率1的PCO
double freq2_north, freq2_east, freq2_up; // 频率2的PCO
int freq1_found; // 频率1是否找到
int freq2_found; // 频率2是否找到
} SatellitePCO;
// 时间结构体
typedef struct {
int year, month, day, hour, min;
double sec;
} DateTime;
// 全局变量
SatellitePCO satellites[MAX_SATELLITES];
int sat_count = 0;
DateTime start_time = { 2024, 6, 7, 2, 0, 0.0 }; // 开始时间: 2024-06-07 02:00:00
DateTime end_time = { 2024, 6, 7, 6, 0, 0.0 }; // 结束时间: 2024-06-07 06:00:00
// 安全的字符串拷贝函数
void safe_strcpy(char* dest, const char* src, size_t dest_size) {
if (dest_size > 0) {
strncpy(dest, src, dest_size - 1);
dest[dest_size - 1] = '\0';
}
}
// 去除字符串首尾空格
char* trim(char* str) {
if (!str) return str;
char* end;
// 跳过前导空格
while (isspace((unsigned char)*str)) str++;
if (*str == 0) return str;
// 去除尾部空格
end = str + strlen(str) - 1;
while (end > str && isspace((unsigned char)*end)) end--;
end[1] = '\0';
return str;
}
// 解析时间字符串
int parse_time(const char* line, DateTime* time) {
if (!line || !time) return 0;
return sscanf(line, "%d %d %d %d %d %lf",
&time->year, &time->month, &time->day,
&time->hour, &time->min, &time->sec);
}
// 比较时间
int compare_time(const DateTime* t1, const DateTime* t2) {
if (!t1 || !t2) return 0;
if (t1->year != t2->year) return t1->year - t2->year;
if (t1->month != t2->month) return t1->month - t2->month;
if (t1->day != t2->day) return t1->day - t2->day;
if (t1->hour != t2->hour) return t1->hour - t2->hour;
if (t1->min != t2->min) return t1->min - t2->min;
if (t1->sec < t2->sec) return -1;
if (t1->sec > t2->sec) return 1;
return 0;
}
// 判断时间是否在有效期内
int is_time_valid(const DateTime* valid_from, const DateTime* valid_until, int has_valid_until) {
// 检查开始时间是否在有效期内
if (compare_time(valid_from, &start_time) > 0) {
return 0; // 有效开始时间晚于我们的开始时间
}
// 如果有结束时间,检查结束时间是否在有效期内
if (has_valid_until) {
if (compare_time(valid_until, &end_time) < 0) {
return 0; // 有效结束时间早于我们的结束时间
}
}
// 如果没有结束时间,只要开始时间早于我们的开始时间就认为有效
// 因为这意味着该记录从valid_from开始一直有效到现在
return 1; // 时间有效
}
// 查找卫星索引
int find_satellite(const char* prn) {
if (!prn) return -1;
for (int i = 0; i < sat_count; i++) {
if (strcmp(satellites[i].prn, prn) == 0) {
return i;
}
}
return -1;
}
// 添加新卫星
int add_satellite(const char* prn, char system) {
if (sat_count >= MAX_SATELLITES) return -1;
if (!prn) return -1;
safe_strcpy(satellites[sat_count].prn, prn, sizeof(satellites[sat_count].prn));
satellites[sat_count].system = system;
satellites[sat_count].freq1_found = 0;
satellites[sat_count].freq2_found = 0;
satellites[sat_count].freq1_north = 0.0;
satellites[sat_count].freq1_east = 0.0;
satellites[sat_count].freq1_up = 0.0;
satellites[sat_count].freq2_north = 0.0;
satellites[sat_count].freq2_east = 0.0;
satellites[sat_count].freq2_up = 0.0;
return sat_count++;
}
// 解析PCO值
int parse_pco(const char* line, double* north, double* east, double* up) {
if (!line || !north || !east || !up) return 0;
return sscanf(line, "%lf %lf %lf", north, east, up);
}
// 获取频率标识
void get_frequency_bands(char system, char* freq1, char* freq2, size_t buf_size) {
if (!freq1 || !freq2 || buf_size == 0) return;
if (system == 'G') { // GPS
safe_strcpy(freq1, "G01", buf_size);
safe_strcpy(freq2, "G02", buf_size);
}
else if (system == 'C') { // BDS
safe_strcpy(freq1, "C01", buf_size); // B1I (L1)
safe_strcpy(freq2, "C06", buf_size); // B3I (L6)
}
else if (system == 'E') { // Galileo
safe_strcpy(freq1, "E01", buf_size); // E1 (L1)
safe_strcpy(freq2, "E05", buf_size); // E5a (L5)
}
else {
freq1[0] = '\0';
freq2[0] = '\0';
}
}
// 计算无电离层组合系数
void calculate_ionofree_coeff(char system, double* alpha, double* beta) {
if (!alpha || !beta) return;
double f1, f2;
if (system == 'G') { // GPS
f1 = 1575.42; // L1 (MHz)
f2 = 1227.60; // L2 (MHz)
}
else if (system == 'C') { // BDS
f1 = 1575.42; // B1I (MHz)
f2 = 1268.52; // B3I (MHz)
}
else if (system == 'E') { // Galileo
f1 = 1575.42; // E1 (MHz)
f2 = 1176.45; // E5a (MHz)
}
else {
*alpha = *beta = 0.0;
return;
}
double f1_sq = f1 * f1;
double f2_sq = f2 * f2;
double denom = f1_sq - f2_sq;
if (fabs(denom) < 1e-9) {
*alpha = *beta = 0.0;
return;
}
*alpha = f1_sq / denom;
*beta = -f2_sq / denom;
}
// 处理ATX文件
void process_atx_file(const char* input_file, const char* output_file) {
if (!input_file || !output_file) {
printf("无效的文件路径!\n");
return;
}
FILE* fp_in = fopen(input_file, "r");
FILE* fp_out = fopen(output_file, "w");
if (!fp_in) {
printf("无法打开输入文件: %s\n", input_file);
return;
}
if (!fp_out) {
printf("无法创建输出文件: %s\n", output_file);
fclose(fp_in);
return;
}
char line[MAXRINEX];
int in_antenna_block = 0;
int valid_satellite = 0;
char current_prn[20] = "";
char current_system = '\0';
DateTime valid_from, valid_until;
int has_valid_until = 0;
char current_freq[10] = "";
// 写入输出文件头
fprintf(fp_out, "PRN 系统 北方向PCO1 东方向PCO1 天顶方向PCO1 北方向PCO2 东方向PCO2 天顶方向PCO2 北方向IFC 东方向IFC 天顶方向IFC\n");
fprintf(fp_out, " (mm) (mm) (mm) (mm) (mm) (mm) (mm) (mm) (mm)\n");
fprintf(fp_out, "时间段: 2024-06-07 02:00:00 至 2024-06-07 06:00:00\n\n");
// 跳过文件头
while (fgets(line, sizeof(line), fp_in)) {
if (strstr(line + 60, "END OF HEADER")) break;
}
// 处理天线数据块
while (fgets(line, sizeof(line), fp_in)) {
if (strstr(line + 60, "START OF ANTENNA")) {
in_antenna_block = 1;
valid_satellite = 0;
current_prn[0] = '\0';
current_system = '\0';
has_valid_until = 0;
continue;
}
if (strstr(line + 60, "END OF ANTENNA")) {
in_antenna_block = 0;
continue;
}
if (!in_antenna_block) continue;
// 解析TYPE / SERIAL NO行
if (strstr(line + 60, "TYPE / SERIAL NO")) {
char type[50], serial[50], cospar[50];
if (sscanf(line, "%49s %49s %49s", type, serial, cospar) == 3) {
safe_strcpy(current_prn, serial, sizeof(current_prn));
current_system = serial[0];
// 只处理G、C、E系统
if (current_system == 'G' || current_system == 'C' || current_system == 'E') {
valid_satellite = 1;
}
else {
valid_satellite = 0;
}
}
continue;
}
// 解析VALID FROM行
if (strstr(line + 60, "VALID FROM")) {
if (parse_time(line, &valid_from) == 6) {
// 检查时间有效性
if (!is_time_valid(&valid_from, &valid_until, has_valid_until)) {
valid_satellite = 0;
}
}
else {
valid_satellite = 0;
}
continue;
}
// 解析VALID UNTIL行
if (strstr(line + 60, "VALID UNTIL")) {
if (parse_time(line, &valid_until) == 6) {
has_valid_until = 1;
// 检查时间有效性
if (!is_time_valid(&valid_from, &valid_until, has_valid_until)) {
valid_satellite = 0;
}
}
else {
valid_satellite = 0;
}
continue;
}
// 处理频率数据
if (strstr(line + 60, "START OF FREQUENCY") && valid_satellite) {
// 如果没有VALID UNTIL行,但需要检查时间有效性
if (!has_valid_until) {
if (!is_time_valid(&valid_from, NULL, 0)) {
valid_satellite = 0;
continue;
}
}
if (sscanf(line, "%9s", current_freq) == 1) {
// 去除空格
char* trimmed_freq = trim(current_freq);
char expected_freq1[10], expected_freq2[10];
get_frequency_bands(current_system, expected_freq1, expected_freq2, sizeof(expected_freq1));
int sat_index = find_satellite(current_prn);
if (sat_index == -1) {
sat_index = add_satellite(current_prn, current_system);
if (sat_index == -1) {
printf("无法添加新卫星,已达到最大数量限制\n");
continue;
}
}
// 读取下一行的PCO值
if (fgets(line, sizeof(line), fp_in)) {
double north, east, up;
if (parse_pco(line, &north, &east, &up) == 3) {
if (strcmp(trimmed_freq, expected_freq1) == 0) {
satellites[sat_index].freq1_north = north;
satellites[sat_index].freq1_east = east;
satellites[sat_index].freq1_up = up;
satellites[sat_index].freq1_found = 1;
}
else if (strcmp(trimmed_freq, expected_freq2) == 0) {
satellites[sat_index].freq2_north = north;
satellites[sat_index].freq2_east = east;
satellites[sat_index].freq2_up = up;
satellites[sat_index].freq2_found = 1;
}
}
}
}
continue;
}
}
// 计算并输出结果
int valid_sat_count = 0;
for (int i = 0; i < sat_count; i++) {
if (satellites[i].freq1_found && satellites[i].freq2_found) {
double alpha, beta;
calculate_ionofree_coeff(satellites[i].system, &alpha, &beta);
// 计算无电离层组合
double ifc_north = alpha * satellites[i].freq1_north + beta * satellites[i].freq2_north;
double ifc_east = alpha * satellites[i].freq1_east + beta * satellites[i].freq2_east;
double ifc_up = alpha * satellites[i].freq1_up + beta * satellites[i].freq2_up;
fprintf(fp_out, "%-4s %c %9.2f %9.2f %9.2f %9.2f %9.2f %9.2f %9.2f %9.2f %9.2f\n",
satellites[i].prn, satellites[i].system,
satellites[i].freq1_north, satellites[i].freq1_east, satellites[i].freq1_up,
satellites[i].freq2_north, satellites[i].freq2_east, satellites[i].freq2_up,
ifc_north, ifc_east, ifc_up);
valid_sat_count++;
}
}
fclose(fp_in);
fclose(fp_out);
printf("处理完成! 结果保存到: %s\n", output_file);
printf("时间段: 2024-06-07 02:00:00 至 2024-06-07 06:00:00\n");
printf("共处理了 %d 颗卫星的数据,其中 %d 颗卫星在指定时间段内有效\n", sat_count, valid_sat_count);
}
int main() {
const char* input_file = "D:\\igs20.atx";
const char* output_file = "D:\\five_atx_output.txt";
// 初始化卫星数组
for (int i = 0; i < MAX_SATELLITES; i++) {
satellites[i].prn[0] = '\0';
satellites[i].system = '\0';
satellites[i].freq1_found = 0;
satellites[i].freq2_found = 0;
}
process_atx_file(input_file, output_file);
return 0;
}完善一下代码,现在只能输出CE卫星的,G卫星有符合要求的也不输出,比如:BLOCK IIR-M G01 G049 2009-014A TYPE / SERIAL NO
0 29-JAN-17 METH / BY / # / DATE
0.0 DAZI
0.0 17.0 1.0 ZEN1 / ZEN2 / DZEN
2 # OF FREQUENCIES
2024 4 22 0 0 0.0000000 VALID FROM
2024 10 7 23 59 59.9999999 VALID UNTIL
IGS20_2375 SINEX CODE
G01 START OF FREQUENCY
0.00 0.00 865.42 NORTH / EAST / UP
NOAZI 10.70 10.10 8.00 4.60 0.50 -3.80 -7.50 -9.70 -10.30 -9.50 -7.40 -4.10 0.30 6.00 12.10 22.00 30.40 40.60
G01 END OF FREQUENCY
G02 START OF FREQUENCY
0.00 0.00 865.42 NORTH / EAST / UP
NOAZI 10.70 10.10 8.00 4.60 0.50 -3.80 -7.50 -9.70 -10.30 -9.50 -7.40 -4.10 0.30 6.00 12.10 22.00 30.40 40.60
G02 END OF FREQUENCY
END OF ANTENNA 这是符合要求的,但是没输出