Skip to content

Instantly share code, notes, and snippets.

@zhuang-hao-ming
Created May 29, 2018 06:51
Show Gist options
  • Save zhuang-hao-ming/119574e8b20dd8ff9149b462835dd770 to your computer and use it in GitHub Desktop.
Save zhuang-hao-ming/119574e8b20dd8ff9149b462835dd770 to your computer and use it in GitHub Desktop.
convert envi bin to tiff
# -*- coding: utf-8 -*-
name_base = 'odiac2013a_1km_excl_intl_{}{}'
meta = '''ENVI
samples = 43200
lines = 21600
data type = 4
bands = 1
byte order = 0
map info = {Geographic Lat/Lon, 1, 1, -180, 90, 0.008333333333333, 0.008333333333333,WGS-84}
coordinate system string = {GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]]}
'''
import os
import subprocess
for year in range(84, 99+1):
for month in range(1, 12+1):
year_month_name_base = name_base.format(str(year).zfill(2), str(month).zfill(2))
gz_name = year_month_name_base + '.bin.gz'
# decompress
command_str = '7z.exe e {}'.format(gz_name)
print(command_str)
p = subprocess.Popen(command_str)
p.wait()
# add header
header_name = year_month_name_base + '.hdr'
print(header_name)
with open(header_name, 'w', encoding='utf-8') as f_handle:
f_handle.write(meta)
# bin to tif
envi_filename = year_month_name_base + '.bin'
tif_filename = './out/' + year_month_name_base + '.tif'
command_str = 'gdal_translate {in_filename} {out_filename}'.format(in_filename=envi_filename, out_filename=tif_filename)
print(command_str)
p = subprocess.Popen(command_str)
p.wait()
# compress tif
zip_name = './result/' + year_month_name_base + '.zip'
command_str = '7z.exe a {} {}'.format(zip_name, tif_filename)
print(command_str)
p = subprocess.Popen(command_str)
p.wait()
# os.remove(gz_name)
os.remove(envi_filename)
os.remove(header_name)
os.remove(tif_filename)
break
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment