python call HEG batch processing tool MODIS data

The following code mainly used python to call NASA official language processing tools MODIS HEG projected coordinate conversion and re-sampling batch processing
main reference

  1. HEG user manual: https: //newsroom.gsfc.nasa.gov/sdptoolkit/HEG/HEG215/EED2-TP-030_Rev01_HEG_UsersGuide_2.15.pdf
  2. HEG batch Help: https: //newsroom.gsfc.nasa.gov/sdptoolkit/HEG/HEG_Batch_job_Help.htm

The main considerations are as follows:

  1. First, according to the official guidelines HEG installation tools, installation steps can refer to my article on the blog: https: //www.cnblogs.com/yhpan/p/11298595.html
  2. HEG User's Manual batch generate batch files based on parameters, you can generate a file in HEG tool, used to do it ourselves instead with
  3. Which tool specific call, how to write parameter file, be sure to carefully read the user's manual, all things on it. Commonly used is resample.exe and swtif.exe, if it can not determine a first handle its own data with the GUI HEG, save a prm file, and then based on the parameters in the file, a user's manual for shining a find, on it.
  4. Generating parameter files must be noted that, as set newline '\ n-', FO = Open (prmfilename, 'W', NEWLINE = '\ n-'), or by default because the windows system as newline '\ r \ n ', the program can not run successfully

Here is the source code sharing

# -*- coding: utf-8 -*-
"""
Created on Sun Feb 16 11:27:19 2020
调用HEG相关工具批处理MODIS数据,主要完成投影坐标转换与重采样
@author: pan
"""
import os

# 设置HEG相关环境变量
os.environ['MRTDATADIR']='D:/MyApps/HEG/HEG_Win/data'
os.environ['PGSHOME']='D:/MyApps/HEG/HEG_Win/TOOLKIT_MTD'
os.environ['MRTBINDIR']='D:/MyApps/HEG/HEG_Win/bin'

# 设置HEG的bin路径
hegpath = 'D:/MyApps/HEG/HEG_Win/bin'
# 指定处理模块的可执行程序文件路径,此处采用resample.exe,可以根据具体的处理问题设置
hegdo = os.path.join(hegpath, 'resample.exe')
hegdo = hegdo.replace('\\', '/') # 全路径以“/”连接

# 指定输入数据的路径
inpath = r'C:\Users\pan\Desktop\Py_ex\data\hdf'
inpath = inpath.replace('\\', '/')
# 指定输出数据的路径
outpath = r'C:\Users\pan\Desktop\Py_ex\data\hdf\out'
outpath = outpath.replace('\\', '/')
# os.chdir(inpath) #改变当前工作目录到输入数据目录

# 获取当前文件夹下的所有hdf文件
allfiles = os.listdir(inpath)
allhdffiles = []
for eachfile in allfiles:
    if os.path.splitext(eachfile)[1] =='.hdf':
        allhdffiles.append(eachfile)
print('--'*20)
print('文件数量为:', len(allhdffiles),',所有hdf文件如下')
print('  '+'\n  '.join(allhdffiles))
print('--'*20)

# prm文件设置模块,需要首先在HEG工具中生成一个参考的prm文件,示例如下
# 设置prm文件存储路径
prmpath = r"C:\Users\pan\Desktop\Py_ex\data\hdf\prm"
prmpath = prmpath.replace('\\', '/')
for eachhdf in allhdffiles:
    prm=['NUM_RUNS = 1\n',
      'BEGIN\n',
      'INPUT_FILENAME = ' + inpath+'/'+eachhdf+'\n',
      'OBJECT_NAME = MODIS_Grid_8Day_1km_LST|\n',
      'FIELD_NAME = LST_Day_1km\n',
      'BAND_NUMBER = 1\n','SPATIAL_SUBSET_UL_CORNER = ( 90.0 -180.0 )\n',
      'SPATIAL_SUBSET_LR_CORNER = ( -90.0 180 )\n',
      'RESAMPLING_TYPE = BI\n',
      'OUTPUT_PROJECTION_TYPE = ALBERS\n',
      'ELLIPSOID_CODE = WGS84\n',
      'OUTPUT_PROJECTION_PARAMETERS = ( 0.0 0.0 25.0 47.0 105.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0  )\n',
      'OUTPUT_PIXEL_SIZE = 500.0\n',
      'OUTPUT_FILENAME = ' + outpath+'/'+eachhdf+'_out.tif\n',
      'OUTPUT_TYPE = GEO\n',
      'END\n']
    prmfilename=prmpath +'/'+ eachhdf+'.prm'
    prmfilename=prmfilename.replace('\\', '/')
    #这里一定要注意,设定换行符为‘\n’,否则由于在windows系统下默认换行符为‘\r\n’,则无法运行成功
    fo=open(prmfilename,'w',newline='\n')
    fo.writelines(prm)
    fo.close()

for eachhdf in allhdffiles:
    prmfilepath=prmpath +'\\'+ eachhdf + '.prm'
    prmfilepath=prmfilepath.replace('\\', '/')
    try:
        resamplefiles = '{0} -P {1}'.format(hegdo, prmfilepath)
        os.system(resamplefiles)        
        print(eachhdf + ' has finished')
    except:
        # 提示错误信息
        print(eachhdf + 'was wrong')

Guess you like

Origin www.cnblogs.com/yhpan/p/12324465.html