使用Python批量下载IGS测站GNSS原始观测数据,并转换为.rnx格式
前言
随着全球PPP-AR/PPP-RTK技术的日渐成熟,扩大了大规模使用IGS测站数据的需求,因此有必要开发一种可以直接批量获取IGS原始观测文件的工具
一、IGS测站网络
- IGS Network
可以通过访问IGS官网的“NETWORK RESOURCES”板块查看测站分布及数量(https://igs.org/network-resources),截至2023年08月28日,共有513个IGS测站。 - 常用的IGS测站原始观测数据下载地址:
1)武汉大学:ftp://igs.gnsswhu.cn/pub/gps/data/daily/
2)CDDIS:https://cddis.nasa.gov/archive/gnss/data/daily/ (需要注册账户)
3)IGS: ftp://igs.ign.fr/pub/igs/data/campaign/mgex/daily/
更多下载地址可见:
1)https://zhuanlan.zhihu.com/p/619226024?utm_id=0
2)https://blog.csdn.net/qq_35099602/article/details/108183367
二、批量获取IGS测站原始观测数据代码
1.文件准备
根据需要的测站名,构建批量下载文件列表,如下图所示:
2. crx2rnx.exe准备
准备crx2rnx.exe,便于将下载得到的.crx.gz文件解压后转换为.rnx格式的观测文件。注意:crx2rnx.exe可放置于与.py文件同级目录下(无需再改下面的代码),也可能通过指定路径访问(见第三部分"convert_to_rnx"函数注释)。
3. 源代码
代码如下(示例):
from ftplib import FTP
from datetime import datetime
import os
import subprocess
import shutil
import gzip
def write_to_log(message, log_file):
log_file.write(message + "\n")
def extract_file(input_file_path):#解压并提取文件
try:
# 解压 .gz 文件
output_file_path = input_file_path[:-3] # 移除 .gz 扩展名
with gzip.open(input_file_path, "rb") as gz_file, open(output_file_path, "wb") as output_file:
shutil.copyfileobj(gz_file, output_file)
return output_file_path
except Exception as e:
print(f"Error during extraction: {str(e)}")
return None
def rename_file_to_xxd(input_file_path,year):#重命名文件
try:
new_file_path = input_file_path[:-4]
new_file_path = new_file_path+"." + year+"d"
os.rename(input_file_path, new_file_path)
return new_file_path
except Exception as e:
print(f"Error during renaming: {str(e)}")
return None
def convert_to_rnx(input_file_path):#转换为.rnx格式文件
try:
subprocess.run(["crx2rnx.exe", input_file_path])#可以更改.exe的路径
return True
except Exception as e:
print(f"Error during conversion: {str(e)}")
return False
def download_file_from_ftp(ftp_host, remote_file_path, local_file_path):#通过访问ftp下载文件
with FTP(ftp_host) as ftp:
ftp.login()
try:
with open(local_file_path, "wb") as local_file:
ftp.retrbinary("RETR " + remote_file_path, local_file.write)
#write_to_log(f"Downloaded {remote_file_path} successfully.",log_file)
return True
except Exception as e:
#write_to_log(f"Downloaded {remote_file_path} failed.",log_file)
return False
def download_files_from_list(ftp_host, file_list_path, output_folder,log_file):#根据文件列表下载文件
with open(file_list_path, "r") as file_list:
logfile=open(log_file, "w")
for line in file_list:
file_name = line.strip()
year=file_name[12:16];
doy=file_name[16:19];
remote_file_path = f"/pub/gps/data/daily/{year}/{doy}/{year[2:4]}d/{file_name}" # 替换为实际的远程目录和文件名
local_file_path = os.path.join(output_folder, file_name)
success = download_file_from_ftp(ftp_host, remote_file_path, local_file_path)
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") # 获取当前时间戳
if success:
write_to_log(f"{timestamp} Downloaded {remote_file_path} successfully.",logfile)
extracted_file_path = extract_file(local_file_path)
if extracted_file_path:
write_to_log(f"Processed {file_name} successfully.",logfile)
renamed_file_path = rename_file_to_xxd(extracted_file_path,year[2:4])
if renamed_file_path:
write_to_log(f"Renamed {extracted_file_path} to {renamed_file_path}.",logfile)
if convert_to_rnx(renamed_file_path):
write_to_log(f"Converted {renamed_file_path} to RINEX format.",logfile)
else:
write_to_log(f"Conversion of {renamed_file_path} failed.",logfile)
else:
write_to_log(f"Renaming of {extracted_file_path} failed.",logfile)
else:
write_to_log(f"Processing of {file_name} failed.",logfile)
else:
write_to_log(f"{timestamp} Downloaded {remote_file_path} failed.",log_file)
if __name__ == "__main__":
ftp_host = "igs.gnsswhu.cn" # 替换为实际的FTP主机名或IP地址
file_list_path = "C:/Users/DELL/Desktop/data/file.txt" # 替换为实际的文件名列表文本文件路径
output_folder = "C:/Users/DELL/Desktop/data/" # 替换为实际的本地保存文件夹路径
log_file = "C:/Users/DELL/Desktop/data/download_log.txt"
download_files_from_list(ftp_host, file_list_path, output_folder,log_file)
4. 结果
总结
以上就是一个简单的、可直接运行的IGS测站观测数据批量下载小工具,欢迎交流讨论!