基于MOD09Q1数据批量计算NDVI

基于MOD09Q1数据批量计算NDVI

通过MRT处理好b01和b02波段后,分别存储至两个不同的文件夹(b01和b02)。

接下来打开在arcgis自带的python2.7中键入以下代码:(如果用pycharm和spyder的朋友需要把环境配置为python2.7并且安装arcpy包)

'''
# -*- coding: utf-8 -*-
import os,arcpy,datetime
from arcpy.sa import *

#存放文件夹的位置
Red_path = r'C:\Users\adminstor\Desktop\b01'#红色波段存放文件夹
NIR_path = r'C:\Users\adminstor\Desktop\b02'#近红外波段存放文件夹
path =r'C:\Users\adminstor\Desktop'#输出结果存放文件夹

# set the intermediate data folder
intermediateDataPath = path+"\\"+"IntermediateData"
# set result data folder
resultDataPath = path+"\\"+"Result"

# determine if the folder exists
if os.path.exists(intermediateDataPath):
    print "IntermediateData floder exists"
else:
    # create a intermediate data floder
    arcpy.CreateFolder_management(path, "IntermediateData")    
if os.path.exists(resultDataPath):
    print "Result floder exists"
else:   
    # create a result floder
    arcpy.CreateFolder_management(path, "Result")

print "Under calculation......"

arcpy.env.workspace = Red_path  #所在文件夹
rasters = arcpy.ListRasters("*", 'tif')  #栅格数据格式设置
for raster in rasters:
    print(raster)
    # set workspace
    arcpy.env.workspace = intermediateDataPath
    arcpy.env.overwriteOutput = True
    # Check out the ArcGIS 3D Analyst extension license
    arcpy.CheckOutExtension("3D")
    NIRraster = NIR_path +'\\'+raster[:-5]+"2.tif"
    NDVIoutPath = resultDataPath+'\\'+ raster[:-16]+ "_NDVI.tif"
    print(NIRraster)
    # Converted to floating-point data
    arcpy.Float_3d(Red_path+'\\'+ raster, "floatRedBand.tif")
    arcpy.Float_3d(NIRraster, "floatNIRBand.tif")
    #计算NDVI分子
    arcpy.Minus_3d("floatNIRBand.tif", "floatRedBand.tif", "outminus.tif")
    #计算NDVI分母
    arcpy.Plus_3d("floatNIRBand.tif", "floatRedBand.tif", "NDVIfenmu.tif")
    #计算NDVI
    arcpy.Divide_3d("outminus.tif", "NDVIfenmu.tif", NDVIoutPath)#计算NDVI
    print(raster+"  has done")  
print("All done")
#清理workspace中的缓存数据        
for i in os.listdir(intermediateDataPath):
    path_file = os.path.join(intermediateDataPath,i)
    if os.path.isfile(path_file):
        os.remove(path_file)

算好了以后就可以拿来批量裁剪后使用了,批量裁剪可以在arcgis中model building或arcpy中进行。

  • 5
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

HydroCoder

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值