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 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 COPY """ 您好! 这个python文件, 是用来解析RINEX文件的,提供了解析导航电文文件(.N)和卫星观测数据文件(.O) 里面用到的第三方库有os, sys, json,(为了函数可移植性,将import字段直接放到函数里面了) 数据解析函数返回值为字典格式数据 数据解析完还可导出为txt,json等文件 """ def read_navigation_data (file_path: str ): """读取并解析导航电文文件""" def change_d19_12_to_float (src ): if src[0 :2 ] == ' ' : qian = '0' elif src[0 :2 ] == ' -' : qian = '-0' else : qian = src[0 :2 ] return float (qian + '.' + src[3 :-4 ] + 'e' + src[-3 :]) """读取RINEX格式的GPS导航电文文件,保存到一个字典返回""" result = {} all_navigation_datas = [] with open (file_path, 'r' ) as f: all_lines = list (f.readlines()) for i in range (len (all_lines)): all_lines[i] = all_lines[i].replace('\n' , '' ) data_start_line = 0 for i in range (len (all_lines)): if all_lines[i].find('END OF HEADER' ) != -1 : data_start_line = i + 1 break nums = int ((len (all_lines) - data_start_line) / 8 ) if nums < 1 : print ("未找到GPS导航数据,请检查文件格式是否正确!" ) import sys sys.exit(-1 ) for one_nav_data in range (nums): nav_data_dic = {} nav_data_list = [] nav_data_dic['序号' ] = str (one_nav_data + 1 ).zfill(len (str (nums))) """ 开始解析数据区域!""" """ 明白了吗?,Let's Go! """ for i in range (8 ): nav_data_list.append(all_lines[data_start_line + 8 * one_nav_data + i]) data_content = nav_data_list[0 ] nav_data_dic['卫星PRN号' ] = int (data_content[0 :2 ].replace(' ' , '' ).zfill(2 )) nav_data_dic['历元' ] = data_content[2 :22 ] nav_data_dic['卫星钟的偏差' ] = change_d19_12_to_float(data_content[22 :41 ]) nav_data_dic['卫星钟漂移' ] = change_d19_12_to_float(data_content[41 :60 ]) nav_data_dic['卫星钟漂移速度' ] = change_d19_12_to_float(data_content[60 :79 ]) broadcast_tracks = [ ['IODE' , 'Crs' , 'n' , 'M0' ], ['Cuc' , 'e' , 'Cus' , 'sqrt(A)' ], ['TOE' , 'Cic' , 'OMEGA' , 'Cis' ], ['i0' , 'Crc' , 'w' , 'OMEGA_DOT' ], ['i' , 'L2_code' , 'GPS_week_num' , 'L2_P_code' ], ['卫星精度' , '卫星健康状态' , 'TGD' , 'IODC' ], ['电文发送时刻' , '拟合区间(h)' , '备用1' , '备用2' ] ] for i in range (1 , 8 ): data_content = nav_data_list[i][3 :79 ] data_content = [data_content[k:k + 19 ] for k in range (0 , len (data_content), 19 )] for j in range (len (data_content)): nav_data_dic[broadcast_tracks[i - 1 ][j]] = change_d19_12_to_float(data_content[j]) all_navigation_datas.append(nav_data_dic) pass result['导航电文文件' ] = all_navigation_datas return result pass def read_observation_data (file_path: str ): """读取并解析观测数值文件""" """读取RINEX格式的观测值文件,并解析成字典返回""" with open (file_path, 'r' ) as f: all_lines = list (f.readlines()) for i in range (len (all_lines)): all_lines[i] = all_lines[i].replace('\n' , '' ) """解析观测值数据文件,保存到一个字典返回""" result = {} all_observation_datas = [] types_of_observ = {} for i in range (len (all_lines)): if all_lines[i].find('TYPES OF OBSERV' ) != -1 : types_line_content = all_lines[i] data_num = int (types_line_content[:6 ].replace(' ' , '' )) types_of_observ['观测类型数量' ] = data_num rows_of_types = data_num // 9 if data_num % 9 == 0 else data_num // 9 + 1 base_line_of_types = i types_line_content = '' for row_num in range (rows_of_types): types_line_content += all_lines[base_line_of_types][6 :60 ] base_line_of_types += 1 types_line_content = types_line_content.split() for j in range (data_num): types_of_observ['观测类型' + str (j + 1 )] = types_line_content[j] break pass datas_of_observ = {} data_start_line = 0 for i in range (len (all_lines)): if all_lines[i].find('END OF HEADER' ) != -1 : data_start_line = i + 1 break if int (len (all_lines) - data_start_line) < 1 : print ("未找到观测值数据,请检查文件格式是否正确!" ) import sys sys.exit(-1 ) """ 开始解析数据区域!""" """ 明白了吗?,Let's Go! """ Epoch_serial_number: int = 0 Epoch_serial_row: int = data_start_line while Epoch_serial_row < len (all_lines): """每次进入这个循环,都是一个新的历元""" one_Epoch_dic = {} part_one = all_lines[Epoch_serial_row] Epoch_serial_number += 1 one_Epoch_dic['历元序号' ] = Epoch_serial_number one_Epoch_dic['观测历元时刻' ] = part_one[1 :26 ] one_Epoch_dic['历元标志' ] = int (part_one[28 :29 ]) satellite_num = int (part_one[29 :32 ].strip(' ' )) one_Epoch_dic['卫星数量' ] = satellite_num satellites_observ_data = [] rows_of_satellites = satellite_num // 12 if satellite_num % 12 == 0 else satellite_num // 12 + 1 base_line_of_sat = Epoch_serial_row part_one = '' for row_num in range (rows_of_satellites): part_one += all_lines[base_line_of_sat][32 :68 ] base_line_of_sat += 1 part_one = [part_one[i:i + 3 ] for i in range (0 , len (part_one), 3 )] for j in range (satellite_num): one_satellite_dic = {'卫星{}的PRN号' .format (str (j + 1 )): part_one[j]} satellites_observ_data.append(one_satellite_dic) pass rows_of_each_satellite_data = types_of_observ['观测类型数量' ] // 5 if types_of_observ['观测类型数量' ] % 5 == 0 else \ types_of_observ['观测类型数量' ] // 5 + 1 base_line_of_data_type = base_line_of_sat for sn in range (satellite_num): """再次使用上面那个厉害的算法(核心:循环+切片),循环读取该卫星所有观测类型的数据~~""" part_two = '' for row_num in range (rows_of_each_satellite_data): temp = all_lines[base_line_of_data_type][0 :80 ] if row_num < rows_of_each_satellite_data - 1 : if len (temp) == 0 : for jj in range (5 ): part_two += ' ' if len (temp) % 16 == 14 : part_two += temp + ' ' elif len (temp) % 16 == 15 : part_two += temp + ' ' else : part_two += temp else : if len (temp) < 16 *(types_of_observ['观测类型数量' ] - row_num * 5 ) -2 : if len (temp) % 16 == 14 : temp += temp + ' ' elif len (temp) % 16 == 15 : temp += temp + ' ' part_two += temp for jj in range (int (types_of_observ['观测类型数量' ]) - row_num * 5 -len (temp)//16 ): part_two += ' ' else : if len (temp) % 16 == 14 : part_two += temp + ' ' elif len (temp) % 16 == 15 : part_two += temp + ' ' else : part_two += temp base_line_of_data_type += 1 dd = part_two part_two = [part_two[i:i + 16 ] for i in range (0 , len (part_two), 16 )] for each_type_num in range (0 , types_of_observ['观测类型数量' ]): try : one_record = { '观测值' : '空' if part_two[each_type_num][0 :14 ] == ' ' else float ( part_two[each_type_num][0 :14 ].strip()), 'LLI失锁标识符' : '空' if part_two[each_type_num][14 ] == ' ' else int ( part_two[each_type_num][14 ]), '信号强度' : '空' if part_two[each_type_num][15 ] == ' ' else int ( part_two[each_type_num][15 ]) } satellites_observ_data[sn][types_of_observ['观测类型' + str (each_type_num + 1 )]] = one_record except Exception as e: print ('源文件格式错误?请检查文件{}的第{}行附近有没有格式错误!具体错误代码:{}' .format (file_name, base_line_of_data_type-1 , e)) pass one_Epoch_dic['所有卫星数据' ] = satellites_observ_data all_observation_datas.append(one_Epoch_dic) Epoch_serial_row = base_line_of_data_type pass result['卫星观测数据文件' ] = all_observation_datas return result pass def write_dict_to_file (dict_data, file_path, file_name ): """将读到内存中的字典类型数据保存到各种文件中""" import os if not os.path.exists(file_path): os.makedirs(file_path) output_type = file_name.split('.' )[-1 ] if output_type == 'txt' : with open (file_path + file_name, 'w' , encoding='utf-8' ) as f_c: f_c.write(str (dict_data)) else : import json with open (file_path + file_name.replace(file_name.split('.' )[-1 ], '' ) + 'json' , 'w' , encoding='utf-8' ) as f: json.dump(dict_data, f, indent=4 , ensure_ascii=False ) if __name__ == '__main__' : print ("Hello, 这个程序的作用是:\n解析RINEX格式的导航电文和卫星观测数据到内存中(字典类型),并导出到txt或json格式文件" ) print ("\n-------------------------- 这是一个分割线 --------------------------------\n" ) file_name = input ("输入要解析的导航电文文件地址和名字:(例如:./原始数据/00052731.98N)\n" ) nav_data = read_navigation_data(file_name) print ("解析完毕,是否需要打印到控制台(1:是 2: 否)" ) is_print = input ("请输入您的选择:" ) if is_print == "1" : print (nav_data) print ("是否需要保存到文件(1:是 2: 否)" ) is_write = input ("请输入您的选择:" ) if is_write == "1" : path = input ("保存地址:(例如:./解析结果/)\n" ) name = file_name.split('/' )[-1 ] write_dict_to_file(nav_data, path, name + '.txt' ) write_dict_to_file(nav_data, path, name + '.json' ) print ("保存完毕,结果已保存到:" , path) print ("\n-------------------------- 这是一个分割线 --------------------------------\n" ) file_name = input ("输入要解析的卫星观测数据文件地址和名字:(例如:./原始数据/00052731.98O)\n" ) obs_data = read_observation_data(file_name) print ("解析完毕,是否需要打印到控制台(1:是 2: 否)" ) is_print2 = input ("请输入您的选择:" ) if is_print2 == "1" : print (obs_data) print ("是否需要保存到文件(1:是 2: 否)" ) is_write2 = input ("请输入您的选择:" ) if is_write2 == "1" : path = input ("保存地址:(例如:./解析结果/)\n" ) name = file_name.split('/' )[-1 ] write_dict_to_file(obs_data, path, name + '.txt' ) write_dict_to_file(obs_data, path, name + '.json' ) print ("保存完毕,结果已保存到:" , path) print ("\n-------------------------- 这是一个分割线 --------------------------------\n" ) print ("很高兴为您服务,下次再见~~" )