#!/usr/bin/env python import glob import os import sys import stat chunked_dir = 'data/chunked-new' scaled_dir = 'data/chunked' def run(command): print command if os.system(command): raise Exception, 'problem' scales = [] for scale_dir in glob.glob('%s/scale_???' % (scaled_dir)): scales.append(int(scale_dir[-3:])) #for source_name in ['data/chunked-new/chunk_0_0.tif'] + glob.glob('%s/*.tif' % (chunked_dir)): for source_name in glob.glob('%s/*_0_0.tif' % (chunked_dir)) + glob.glob('%s/*.tif' % (chunked_dir)): base_name = os.path.basename(source_name)[:-4] print base_name source_stats = os.stat(source_name) source_modified = source_stats[stat.ST_MTIME] for scale in scales: scaled_name = '%s/scale_%03d/%s.tif' % (scaled_dir, scale, base_name) print scaled_name if os.path.exists(scaled_name): scaled_stats = os.stat(scaled_name) scaled_modified = scaled_stats[stat.ST_MTIME] # Has the source changed? if source_modified <= scaled_modified: continue temp_name = scaled_name + '.tmp' run('gdal_translate -b 1 -b 2 -b 3 -outsize %d%% %d%% "%s" "%s"' % ( scale, scale, source_name, temp_name, )) run('mv -f "%s" "%s"' % (temp_name, scaled_name))