python批量求遥感影像的均值

该Python脚本用于批量处理Tiff文件,对特定时间段内的多张图像进行平均处理。它遍历指定目录下的年、月、日、小时文件夹结构,使用arcpy模块的CellStatistics函数计算每天四个时间点的Tiff图像的平均值,生成一个新的平均Tiff文件。
摘要由CSDN通过智能技术生成
目标:批量处理tiff文件,进行多时相tiff平均
python2.7
参考:
代码
# -*- coding: UTF-8 -*-
"""
批量处理tiff文件,进行多时相tiff平均
针对一天产生的四次数据,进行平均处理,最终每一天得到一个tiff文件
YMJ 2022.05.15 14:30
"""
import os
import arcpy
from arcpy import env
from arcpy.sa import *

# 得到调用拓展文件的许可
arcpy.CheckOutExtension("Spatial")

# 设置工作环境、输入文件路径、输出文件路径
env.workspace = "D:\RUANJIAN\Python\YMJdata\Practice\_4_batch_tiff\_input_tiff"
input_file_path = "D:\RUANJIAN\Python\YMJdata\Practice\_4_batch_tiff\_input_tiff"
output_aver_path = "D:\RUANJIAN\Python\YMJdata\Practice\_4_batch_tiff\_output_aver_tiff"

# 将小时存放到一个列表中,之后调用
hour = ["00", "06", "12", "18"]
# 创建一个空的列表,存放每一天的四个时间的tiff,用于CellStatistics函数求均值
rasters = []

# 进行路径下的for循环,年、月、日、小时
for year in range(2018, 2020, 1):
    str1 = str(year)
    for month in range(1, 13, 1):
        if month < 10:
            str2 = "0" + str(month)
        else:
            str2 = str(month)
        for day in range(1, 32, 1):
            if day < 10:
                str3 = "0" + str(day)
            else:
                str3 = str(day)
            for i in range(0, 4, 1):

                # 得到循环下的名称
                file_name = "fnl" + "_" + str1 + str2 + str3 + "_" + hour[i] + "_00.tif"

                # 判断输入文件路径下是否存在这一文件
                if (os.path.exists(input_file_path + "\\" + file_name)):
                    # print(input_file_path + "\\" + file_name)

                    # 如果存在,这一文件将表示为:绝对路径+文件名称
                    raster = os.path.join(input_file_path, file_name)
                    # 然后被添加到列表里
                    rasters.append(raster)
                # 如果不存在,rasters列表将持续为空,如果没有这一步,就会报错
                else:
                    rasters = []

            # print(rasters)

            # MEAN均值;SUM总和;STD标准差;MINIMUM最小值;MAXIMUM最大值;
            # 判断如果列表不为空,就进行多图层的均值计算
            if len(rasters) != 0:
                # 函数
                outCellStatistics = CellStatistics(rasters, "MEAN", "DATA")
                # 输出结果影像的路径和名称
                outCellStatistics.save("D:\RUANJIAN\Python\YMJdata\Practice\_4_batch_tiff\_output_aver_tiff" +\
                                       "\\" + str1 + str2 + str3 + ".tif")

            # 清空列表,为了下一天的存储!能想到这一点的我,简直太聪明啦!!!
            rasters = []

# 最后也要夸夸自己
print("DONE! U are Soooooo great!")
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值