Skip to content

Instantly share code, notes, and snippets.

@jkatagi
Last active May 24, 2017 10:57
Show Gist options
  • Save jkatagi/5b9ebee1ba027436696790e2e04f1862 to your computer and use it in GitHub Desktop.
Save jkatagi/5b9ebee1ba027436696790e2e04f1862 to your computer and use it in GitHub Desktop.
Convert DN to Reflection using GAIN BAND (I think it's more faster than using GRASS GIS and gdal_merge.py)
#!/usr/bin/env pytho3
# 2017/05/24 Jin Katagi
# convert DN to reflectance.
# usage) python3 convert_DN.py
import subprocess
import os
import re
from src import tif_tools
def get_scene_list(input_dir):
""" get tif list from input_dir (helper function)."""
scene_list = os.listdir(input_dir)
# read only *.tif
scene_list = [f for f in scene_list if re.match(r'.*tif$', f)]
return scene_list
def calc_GAIN(input_dir, scene_name, output_dir):
""" calc GAIN to DN bands and save as GeoTiff.
Inputs:
input_dir (str) : path to scene_name dir.
scene_naem (str) : name of the input img.
output_dir (str) : path to output dir.
"""
# read input image and calc GAIN.
print("read and calc GAIN:{}".format(scene_name))
band, dataset = tif_tools.tif2array(input_dir + "/" + scene_name)
savename=output_dir + "/" + scene_name
# save img
print("save:{}".format(scene_name))
tif_tools.array2raster(savename, dataset, band, dtype="Float32")
def main():
input_dir="/path/to/input_img/dir"
output_dir="./path/to/output_img/dir"
# make output dir.
subprocess.run(args=["mkdir", "-p", output_dir])
# get *.tif list.
scene_list = get_scene_list(input_dir)
for scene_name in scene_list:
calc_GAIN(input_dir, scene_name, output_dir)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment