"""
   ulmo.util.raster
   ~~~~~~~~~~~~~~~~

   Collection of useful functions for raster manipulation
"""

import hashlib
from .misc import download_if_new
import os
from pyproj import Proj
import rasterio
import subprocess
import zipfile


def mosaic_and_clip(raster_tiles, xmin, ymin, xmax, ymax, output_path):
	print 'Mosaic and clip to bounding box extents'
	output_vrt = os.path.splitext(output_path)[0] + '.vrt'
	print subprocess.check_output(['gdalbuildvrt', '-overwrite', output_vrt] + raster_tiles)
	# check crs
	with rasterio.drivers():
		with rasterio.open(output_vrt) as src:
			p = Proj(src.crs)

	if not p.is_latlong():
		[xmax, xmin],[ymax, ymin] = p([xmax, xmin],[ymax,ymin])

	print subprocess.check_output(['gdalwarp', '-overwrite', '-te', repr(xmin), repr(ymin), repr(xmax), repr(ymax), output_vrt, output_path])
	print 'Output raster saved at %s', output_path


def download_tiles(path, tile_urls, tile_fmt, check_modified=False):
	raster_tiles = []
	for i, url in enumerate(tile_urls):
		filename = os.path.split(url)[-1]
		zip_path = os.path.join(path, 'zip', filename)
		print '... downloading tile %s of %s from %s' % (i+1, len(tile_urls), url)
		download_if_new(url, zip_path, check_modified=check_modified)
		print '... ... zipfile saved at %s' % zip_path
		tile_path = zip_path.replace('/zip', '')
		raster_tiles.append(extract_from_zip(zip_path, tile_path, tile_fmt))

	return raster_tiles


def extract_from_zip(zip_path, tile_path, tile_fmt):
    tile_path = os.path.splitext(tile_path)[0] + tile_fmt
    with zipfile.ZipFile(zip_path) as z:
        fname = [x for x in z.namelist() if tile_fmt in x][0]
        with open(tile_path, 'w') as f:
            f.write(z.read(fname))
            print '... ... %s format raster saved at %s' % (tile_fmt, tile_path)

    return tile_path


def generate_raster_uid(layer, xmin, ymin, xmax, ymax):
	return hashlib.md5(','.join([layer, repr(xmin), repr(ymin), repr(xmax), repr(ymax)])).hexdigest()
@@ -1039,19 +1039,97 @@
 )%5B-1%5D%0A%09%09
-zip
+print '... downloading tile %25s of %25s from %25s' %25 (i+1, len(tile_urls), url)%0A%09%09tile
 _path = 
@@ -1150,15 +1150,8 @@
 ath,
- 'zip',
  fil
@@ -1163,83 +1163,150 @@
 )%0A%09%09
-print '... downloading tile %25s of %25s from %25s' %25 (i+1, len(tile_urls), url
+if tile_fmt=='':%0A%09%09%09download_if_new(url, tile_path, check_modified=check_modified)%0A%09%09else:%0A%09%09%09zip_path = os.path.join(path, 'zip', filename
 )%0A
+%09
 %09%09do
@@ -1367,16 +1367,17 @@
 fied)%0A%09%09
+%09
 print '.
@@ -1415,16 +1415,17 @@
 ip_path%0A
+%09
 %09%09tile_p
@@ -1434,59 +1434,8 @@
 h = 
-zip_path.replace('/zip', '')%0A%09%09raster_tiles.append(
 extr
@@ -1477,18 +1477,50 @@
 ile_fmt)
-)
 %0A
+%0A%09%09raster_tiles.append(tile_path)
 %0A%09return

add jp2 (jpeg2000) format to raster format options. Allow direct download when files are not inside zip file.