143 lines
4.7 KiB
Python
143 lines
4.7 KiB
Python
#!/usr/bin/env python3
|
||
"""
|
||
GasFlux 完整处理示例脚本
|
||
|
||
这个脚本演示了完整的GasFlux处理流程:
|
||
1. 使用data_processor.py处理原始Excel数据
|
||
2. GasFlux数据验证和处理:
|
||
- 背景校正
|
||
- 空间处理
|
||
- 克里金插值
|
||
- 生成报告
|
||
|
||
运行方法:
|
||
python run_example.py input.xlsx
|
||
python run_example.py input.xlsx --output processed_data.csv
|
||
"""
|
||
|
||
import sys
|
||
import argparse
|
||
from pathlib import Path
|
||
|
||
# 导入同一包内的模块
|
||
# 优先尝试相对导入(当作为包的一部分运行时)
|
||
# 如果失败,则尝试绝对导入(当直接运行脚本时)
|
||
try:
|
||
# 尝试相对导入
|
||
from .processing_pipelines import process_main
|
||
from .data_processor import process_file
|
||
print("✅ 成功导入GasFlux模块(相对导入)")
|
||
except ImportError:
|
||
try:
|
||
# 相对导入失败,尝试绝对导入
|
||
from src.gasflux.processing_pipelines import process_main
|
||
from src.gasflux.data_processor import process_file
|
||
print("✅ 成功导入GasFlux模块(绝对导入)")
|
||
except ImportError as e:
|
||
print(f"❌ 导入GasFlux模块失败: {e}")
|
||
print("请确保GasFlux包结构完整,或从项目根目录运行")
|
||
sys.exit(1)
|
||
|
||
|
||
def main():
|
||
"""主函数:运行完整的数据处理和GasFlux分析流程"""
|
||
|
||
parser = argparse.ArgumentParser(
|
||
description="GasFlux完整处理流程:Excel数据预处理 + 通量分析",
|
||
formatter_class=argparse.RawDescriptionHelpFormatter,
|
||
epilog="""
|
||
使用示例:
|
||
python run_example.py data.xlsx # 基本使用
|
||
python run_example.py data.xlsx --output result.csv # 指定输出文件名
|
||
python run_example.py data.xlsx --no-gasflux # 仅预处理,不进行通量分析
|
||
|
||
处理步骤:
|
||
1. 使用data_processor.py处理Excel文件
|
||
2. 对处理后的数据进行GasFlux通量分析
|
||
"""
|
||
)
|
||
|
||
parser.add_argument('input_file', help='输入的Excel文件路径 (.xlsx 或 .xls)')
|
||
parser.add_argument('-o', '--output', help='预处理后的CSV输出文件名(可选)')
|
||
parser.add_argument('--no-gasflux', action='store_true',
|
||
help='仅执行数据预处理,跳过GasFlux通量分析')
|
||
parser.add_argument('-c', '--config', help='GasFlux配置文件路径(可选,默认使用内置配置)')
|
||
|
||
args = parser.parse_args()
|
||
|
||
print("=== GasFlux 完整处理流程 ===\n")
|
||
|
||
# 检查输入文件
|
||
input_path = Path(args.input_file)
|
||
if not input_path.exists():
|
||
print(f"❌ 错误:输入文件不存在: {input_path}")
|
||
return
|
||
|
||
if input_path.suffix.lower() not in ['.xlsx', '.xls']:
|
||
print(f"❌ 错误:输入文件必须是Excel格式: {input_path}")
|
||
return
|
||
|
||
# 确定输出文件名
|
||
if args.output:
|
||
processed_csv = Path(args.output)
|
||
else:
|
||
processed_csv = input_path.with_suffix('.processed.csv')
|
||
|
||
# 确定配置文件
|
||
if args.config:
|
||
config_file = Path(args.config)
|
||
else:
|
||
config_file = Path(__file__).parent / "data" / "gasflux_config.yaml"
|
||
|
||
print(f"输入Excel文件: {input_path}")
|
||
print(f"预处理输出CSV: {processed_csv}")
|
||
if not args.no_gasflux:
|
||
print(f"GasFlux配置文件: {config_file}")
|
||
print()
|
||
|
||
try:
|
||
# 第一步:数据预处理
|
||
print("🔄 第一步:数据预处理...")
|
||
processed_df = process_file(str(input_path), str(processed_csv))
|
||
print(f"✅ 数据预处理完成,输出文件: {processed_csv}")
|
||
print()
|
||
|
||
# 如果不需要进行GasFlux分析,直接返回
|
||
if args.no_gasflux:
|
||
print("⏭️ 跳过GasFlux通量分析")
|
||
return
|
||
|
||
# 第二步:GasFlux通量分析
|
||
print("🔄 第二步:GasFlux通量分析...")
|
||
|
||
# 检查配置文件
|
||
if not config_file.exists():
|
||
print(f"❌ 错误:GasFlux配置文件不存在: {config_file}")
|
||
return
|
||
|
||
print(f"使用数据文件: {processed_csv}")
|
||
print(f"使用配置文件: {config_file}")
|
||
print()
|
||
|
||
# 运行GasFlux处理流程
|
||
process_main(processed_csv, config_file)
|
||
print("\n✅ GasFlux通量分析完成!")
|
||
|
||
# 显示输出信息
|
||
output_dir = Path("../../examples/basic_usage/5m") / processed_csv.stem / "processing_run"
|
||
if output_dir.exists():
|
||
print(f"\n📁 GasFlux输出目录: {output_dir}")
|
||
print("📊 生成的文件:")
|
||
for file in output_dir.rglob("*"):
|
||
if file.is_file():
|
||
print(f" - {file.name}")
|
||
|
||
except Exception as e:
|
||
print(f"\n❌ 处理失败: {e}")
|
||
import traceback
|
||
traceback.print_exc()
|
||
|
||
|
||
if __name__ == "__main__":
|
||
main()
|