import pymannkendall as mk import pandas as pd from openpyxl import Workbook import os def mk_test_tfpw(data): trend, h, p, z, Tau, s, var_s, slope, intercept = mk.trend_free_pre_whitening_modification_test(data, alpha=0.1) return trend, h, p, z, slope def process_excel_file(file): data_folder = 'D:\TY\大论文\黄河数据\气象数据\降水_new\全流域年数据' filepath = os.path.join(data_folder, file) df = pd.read_excel(filepath, header=None) years = df.iloc[:, 0] data = df.iloc[:, 1] trend, h, p, z, slope = mk_test_tfpw(data) return trend, h, p, z, slope def process_multiple_excel(file_list): results = [] for file in file_list: try: result = process_excel_file(file) results.append(result) except Exception as e: print(f"Error processing file {file}: {str(e)}") return results if __name__ == "__main__": folder_path = 'D:\TY\大论文\黄河数据\气象数据\降水_new\全流域年数据' file_names = os.listdir(folder_path) excel_files = [file for file in file_names if file.endswith('.xlsx') or file.endswith('.xls')] results = process_multiple_excel(excel_files) #存储检验结果 wb = Workbook() ws = wb.active ws.append(["File Name", "Trend", "H", "P-value", "Z-value", "Slope"]) for i, result in enumerate(results): trend, h, p, z, slope = result ws.append([excel_files[i], trend, h, p, z, slope]) output_file = "trend_analysis_results.xlsx" wb.save(output_file) print(f"Results saved to {output_file}")
python对各站点批量进行TFPW-MK检验法
最新推荐文章于 2024-11-08 13:43:44 发布