我是靠谱客的博主 精明外套,这篇文章主要介绍利用Python和GDAL把MODIS的HDF格式表观反射率产品MOD09A1转换为TIFF格式并重投影到与LANDSAT8-OLI一致,现在分享给大家,希望可以做个参考。

解释

1.MOD09A1是表观反射率产品,是HDF格式并且投影和常用的Landsat数据不一致。为了统一反演时使用,想要将该产品转换为TIFF格式并重投影。特别是要批量的进行转换和重投影。

2.采用Python和GDAL进行转换,其中采用的包为:

  • gdal
  • numpy
  • os

其中,os自带,numpy极为常用,gdal需要自己装

  1. 将除了8,12,13的波段合为一个多波段的数据,其中1-7为反射率数据,波段范围参看MOD09A1文档中的说明。8,9,10波段为太阳天顶角、观测天顶角和相对方位角,可用于反演。如需使用8、12、13三个质量控制波段,可根据代码自行修改,注意这三个波段的数据类型与其他波段不一致,需要特别关注。

3.用的时候首先创建一个文件夹,把下载的MOD09A1产品都放进去,然后创建一个输出文件夹。将这两个文件夹名字在代码中一上来制定输入输出的地方进行替换,运行即可。输出为多波段文件和重投影过后的多波段文件。

4.代码粗糙,本着能用就行的思想,结构也非常简单,不能做到通用化。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from osgeo import gdal import numpy as np import os # gdal打开hdf数据集 # MOD09A1表观反射率数据 input_file_dir = "time_series" output_file_dir = "time_series_tif" height = width = 2400 N_layers = 13 # ============================================= file_list = os.listdir(input_file_dir) if os.path.exists(input_file_dir) == False: Exception("input file dir does not exist.") if os.path.exists(output_file_dir) == False: os.mkdir(output_file_dir) print("make dir: ", output_file_dir, ".") for file in file_list: if file.endswith('hdf'): path = os.path.join(input_file_dir, file) datasets = gdal.Open(path) # 获取hdf中的子数据集 SubDatasets = datasets.GetSubDatasets() Metadata = datasets.GetMetadata() o_data = np.zeros((height, width, N_layers)) for n_layer in range(N_layers): # 获取要转换的子数据集 if n_layer == 7 or n_layer == 11 or n_layer == 12: # 三个质量控制波段,且数据类型和反射率、角度波段不一致,不用 continue data_name = datasets.GetSubDatasets()[n_layer][0] raster_data = gdal.Open(data_name) o_data[:, :, n_layer] = raster_data.ReadAsArray() basename = '.'.join(file.split(".")[0:5]) TifName = os.path.join(output_file_dir, (basename + "_2tif.tif")) datatype = raster_data.GetRasterBand(1).DataType file_driver = gdal.GetDriverByName('GTiff') o_dataset = file_driver.Create(TifName, width, height, N_layers-3, datatype) o_dataset.SetGeoTransform(raster_data.GetGeoTransform()) o_dataset.SetProjection(raster_data.GetProjection()) for band in range(N_layers-3): o_dataset.GetRasterBand(band + 1).WriteArray(o_data[:, :, band]) o_dataset.FlushCache() # 保存为tif proj_TifName = os.path.join(output_file_dir, (basename + "_2tif_proj.tif")) print("saving:", TifName) print("saving:", proj_TifName) geoData = gdal.Warp(proj_TifName, o_dataset, dstSRS = 'EPSG:4326', format = 'GTiff', resampleAlg = gdal.GRA_Bilinear) else: continue

最后

以上就是精明外套最近收集整理的关于利用Python和GDAL把MODIS的HDF格式表观反射率产品MOD09A1转换为TIFF格式并重投影到与LANDSAT8-OLI一致的全部内容,更多相关利用Python和GDAL把MODIS内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(95)

评论列表共有 0 条评论

立即
投稿
返回
顶部