python对各站点批量进行TFPW-MK检验法

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}")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值