MOD/MYD11A2是8天平均合成的产品,在求季节尺度时,不能提取到一季的数据,故在季节节点(比如MOD11A2089:89-96天,春季只到90/91天)处需要下载MOD/MYD11A1(日产品)来替代。
数据预处理:下载好的MOD/MYD 11A1和A2拼接转投影。
所有数据在一个I:\DR\UHI\1MOD11_DAY_Data文件夹内。
程序思路:
path1 = r"I:\DR\UHI\1MOD11_DAY_Data"
path2 = r"I:\DR\UHI\2MOD11_DAY_Clip"
path3 = r"I:\DR\UHI\3MOD11_DAY_Setn"
path4 = r"I:\DR\UHI\4MOD11_DAY_Setn_Season"
path5 = r"I:\DR\UHI\5MOD11_DAY_Avg_Season"
path6 = r"I:\DR\UHI\6MOD11_DAY_UHI_Season"
1.将path1内的文件裁剪,输出至path2。
2.path2内的文件去除无效值,输出至path3。
3.path3的文件建立所有年的子文件夹,并再建立四季的“孙子”文件夹。
4.若需要计算年/四季/月不同尺度的结果,则需要保存path3数据,这里将path3数据复制到path4。
遍历path4所有文件,按照“孙子”文件夹计算LST均值,输出至path5。
5.遍历path5所有文件,按照“孙子”文件夹计算UHI均值,输出至path6。
import glob
import os
import shutil
from arcpy.sa import *
from arcpy.sa import Con
arcpy.CheckOutExtension('Spatial')
path1 = r"I:\DR\UHI\1MOD11_DAY_Data"
path2 = r"I:\DR\UHI\2MOD11_DAY_Clip"
path3 = r"I:\DR\UHI\3MOD11_DAY_Setn"
path4 = r"I:\DR\UHI\4MOD11_DAY_Setn_Season"
path5 = r"I:\DR\UHI\5MOD11_DAY_Avg_Season"
path6 = r"I:\DR\UHI\6MOD11_DAY_UHI_Season"
mask = "I:/DR/GIS_HF/合肥市区县(面).shp"
#path3复制到path4,因为path3的文件还要做年、月的计算。若只做季节,可直接在path3上操作。
##1.将文件夹内的子文件夹的文件进行批量裁剪##
rasters = glob.glob(os.path.join(path1, "*.tif"))
for ras in rasters:
# nameT:输出文件名
nameT = "MOSAIC_TMP_" + os.path.basename(ras).split(".")[0][11:] + "mod_day_clip.tif"
#os.path.basename(ras).split(".")[0][11:] split(".")[0]分割“."[0]第一个.前面的名字 [11:]再选择从第11个字符开始到最后的
outname = os.path.join(outws, nameT) # 合并输出文件名+输出路径
out_extract = arcpy.sa.ExtractByMask(ras, mask) # 执行按掩模提取操作
out_extract.save(outname) # 保存数据
print os.path.basename(ras) + " ---- 裁剪完成"
print " ---- 全部裁剪完成 ---- "
##2.批量去除异常值###
# 利用glob包,将inws下的所有tif文件读存放到rasters中
rasters2 = glob.glob(os.path.join(path2, "*.tif"))
# 循环rasters中的所有影像,进行按掩模提取操作
for ras2 in rasters2:
nameT2 = os.path.basename(ras2).split(".")[0][11:18] + "mod_day_setn.tif"
outname2 = os.path.join(path3, nameT2) # 合并输出文件名+输出路径
outSetNull = SetNull(ras2, ras2, whereClause)*0.02-273.15 # 去除异常值
outSetNull.save(outname2) # 保存数据
print "---- 全部去除异常值 ----"
##3.path4下的每个文件夹下再创建4个季度的文件夹##
for j in range(2000,2022):
path_season = path4 + "\\" + str(j)
for i in range(4):
# *定义一个变量判断文件是否存在,path指代路径,str(i)指代文件夹的名字*1234分别代表春夏秋冬
isExists = os.path.exists(path_season+"\\"+str(i+1))
if not isExists: #判断如果文件不存在,则创建
os.makedirs(path_season+"\\"+str(i+1))
print("%s 目录创建成功"%(i+1))
else:
print("%s 目录已经存在"%(i+1))
continue
##4.path4下的每个文件夹下按日期放进不同季节的文件夹内##
##path1:代表需要遍历的根文件夹
##root 表示正在遍历的文件夹的名字
##dirs 记录正在遍历的文件夹下的子文件夹集合
##files 记录正在遍历的文件夹中的文件集合
for j in range(2000,2022):
path_season = path4 + "\\" + str(j)
if int(j)%4==0:
for (root, dirs, files) in os.walk(path_season):
for filename in files:
singleFile = os.path.join(root, filename)
res = os.path.splitext(filename)
doy = res[0][4:7]
if int(doy) < 92:
shutil.move(singleFile, path_season + "/" + str(1) + "\\" + filename)
elif int(doy) < 183:
shutil.move(singleFile, path_season + "/" + str(2) + "\\" + filename)
elif int(doy) < 273:
shutil.move(singleFile, path_season + "/" + str(3) + "\\" + filename)
else:
shutil.move(singleFile, path_season + "/" + str(4) + "\\" + filename)
else:
for (root, dirs, files) in os.walk(path_season):
for filename in files:
singleFile = os.path.join(root, filename)
res = os.path.splitext(filename)
doy = res[0][4:7]
if int(doy) < 91:
shutil.move(singleFile, path_season + "/" + str(1) + "\\" + filename)
elif int(doy) < 182:
shutil.move(singleFile, path_season + "/" + str(2) + "\\" + filename)
elif int(doy) < 273:
shutil.move(singleFile, path_season + "/" + str(3) + "\\" + filename)
else:
shutil.move(singleFile, path_season + "/" + str(4) + "\\" + filename)
##5.二级目录计算季度LST平均值##
for k in range(2000,2022):
path_season = path5 + "\\" + str(k)
for i in range(4):
# *定义一个变量判断文件是否存在,path指代路径,str(i)指代文件夹的名字*1234分别代表春夏秋冬
isExists = os.path.exists(path_season+"\\"+str(i+1))
if not isExists: #判断如果文件不存在,则创建
os.makedirs(path_season+"\\"+str(i+1))
print("%s 目录创建成功"%(i+1))
else:
print("%s 目录已经存在"%(i+1))
continue
for j in range(2000,2022):
path_season2 = path4 + "\\" + str(j)#因为是文件夹的三级目录,所以先定义变量
# print(path_season2)
path_season3 = os.listdir(path_season2 + "\\")
# print(path_season3)
target_path_list2 = path5 + "\\" + str(j)
# print(target_path_list2)
target_path_list3 = os.listdir(target_path_list2 + "\\")
# print(target_path_list3)
for l in range(len(path_season3)):
inpath3 = path_season2 + "\\" + path_season3[l]
# print(inpath3)
target_path3 = target_path_list2 + "\\" + target_path_list3[l]
# print(target_path3)
rasters3 = glob.glob(os.path.join(inpath3, "*.tif"))
# print(rasters3)
nameT3 = inpath3[33:37] + "_" + inpath3[38:] + "_" + "mod_day_ave.tif"
# print(nameT3)
outname3 = os.path.join(target_path3, nameT3)
# print(outname3)
outCellStats = CellStatistics(rasters3, "MEAN", "DATA")
outCellStats.save(outname3)
print path_season3[l] + "--- 平均值计算完成 ----"
##6.二级目录计算季度UHI##
for k in range(2000,2022):
path_season = path6 + "\\" + str(k)
for i in range(4):
# *定义一个变量判断文件是否存在,path指代路径,str(i)指代文件夹的名字*1234分别代表春夏秋冬
isExists = os.path.exists(path_season+"\\"+str(i+1))
if not isExists: #判断如果文件不存在,则创建
os.makedirs(path_season+"\\"+str(i+1))
print("%s 目录创建成功"%(i+1))
else:
print("%s 目录已经存在"%(i+1))
continue
for j in range(2000,2022):
path_season4 = path5 + "\\" + str(j)
# print(path_season4)
path_season5 = os.listdir(path_season4 + "\\")
# print(path_season5)
target_path_list4 = path6 + "\\" + str(j)
# print(target_path_list4)
target_path_list5 = os.listdir(target_path_list4 + "\\")
# print(target_path_list5)
for l in range(len(path_season5)):
inpath4 = path_season4 + "\\" + path_season5[l]
# print(inpath4)
target_path3 = target_path_list4 + "\\" + target_path_list5[l]
# print(target_path3)
rasters4 = glob.glob(os.path.join(inpath4, "*.tif"))
# print(rasters4)
for m in range(len(rasters4)):
inRaster = arcpy.Raster(rasters4[m])
result_std = arcpy.GetRasterProperties_management(inRaster, "STD")
std = result_std.getOutput(0)
result_mean = arcpy.GetRasterProperties_management(inRaster, "MEAN")
mean = result_mean.getOutput(0)
nameT4 = inpath4[32:36] + "_" + inpath4[37:] + "_" + "mod_day_UHI.tif"
# print(nameT4)
outname4 = os.path.join(target_path3, nameT4)
# print(outname4)
outCon =Con(inRaster < (float(mean)-float(std)),1,
Con((inRaster >= (float(mean)-float(std))) & (inRaster < (float(mean)-0.5*float(std))), 2,
Con((inRaster >= (float(mean)-0.5*float(std))) & (inRaster <= (float(mean)+0.5*float(std))), 3,
Con((inRaster > (float(mean)+0.5*float(std))) & (inRaster <= (float(mean)+float(std))), 4,
Con((inRaster > (float(mean)+float(std))), 5,)))))
outCon.save(outname4)
print path_season4 + " ---- UHI分级完成 ---- "