Created
August 1, 2016 14:54
-
-
Save brookisme/d503d913cdc4c7828a0c43c88905dc9c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import sys | |
import subprocess | |
from glob import glob | |
import gdal | |
import gdalconst as gc | |
import os | |
from time import time | |
from datetime import timedelta | |
from alert_config import TIF_DIR, CSV_DIR | |
DATA_DIR=TIF_DIR | |
TARGET_DIR=CSV_DIR | |
TIF2CSV='./tif2csv.sh' | |
# | |
# PARAMS | |
# | |
params = sys.argv | |
params_len = len(params) | |
def _safe_params(i): | |
if params_len > i: | |
return params[i] | |
else: | |
return None | |
# | |
# FUNCTIONS | |
# | |
def _file_name(path,ext="tif"): | |
return os.path.basename(path).replace(".{}".format(ext),'') | |
def _tif2csv_subprocess_args(in_name,out_name=None,nbands=1): | |
if not out_name: out_name = in_name | |
return [TIF2CSV,DATA_DIR,TARGET_DIR,in_name,nbands,out_name] | |
def _nbands(path): | |
return str(gdal.Open("{}/{}.tif".format(DATA_DIR,path),gc.GA_ReadOnly).RasterCount) | |
def file_names(ext="tif",data_dir=DATA_DIR): | |
farray = glob("{}/*.{}".format(data_dir,ext)) | |
return map(_file_name,farray) | |
def tif2csv(in_name,out_name=None): | |
return subprocess.call(_tif2csv_subprocess_args(in_name,out_name,_nbands(in_name))) | |
def all2csv(): | |
start_time = time() | |
for file in file_names(): print tif2csv(file) | |
total_time = time()-start_time | |
print "TOTAL TIME [{}]: {}".format(total_time,timedelta(seconds=(total_time))) | |
if (__name__ == '__main__'): | |
action=_safe_params(1) | |
if action=='all': | |
DATA_DIR = _safe_params(2) or DATA_DIR | |
TARGET_DIR = _safe_params(3) or TARGET_DIR | |
all2csv() | |
elif action=='tif': | |
in_name = _safe_params(4) | |
if not in_name: | |
in_name = _safe_params(2) | |
out_name = _safe_params(3) | |
else: | |
DATA_DIR = _safe_params(2) or DATA_DIR | |
TARGET_DIR = _safe_params(3) or TARGET_DIR | |
out_name = _safe_params(5) | |
tif2csv(in_name,out_name) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment