Rasterio cookbook recipe number one

I got a nice message today from a developer who found rasterio to be the right
tool for scaling the values of a 15 GB LiDAR DEM. Here’s the code.

importnumpyasnpimportrasterio"""2014-02-13Bryan LumanUse it however you like at your own riskProblem:I have a huge DEM converted from LiDAR LAS points. I'd like to makeit slightly less massive for further processing.All my z values are between 682 and 869 feet stored as float32. IfI convert them to centimeters and they will fit well within a 16 bitunsigned integer.This saves a significant amount of storage space and greatly reducesany type of future analysis."""CM_IN_FOOT=30.48withrasterio.drivers():withrasterio.open('massive_dem_as_float')assrc:kwargs=src.metakwargs.update(driver='GTiff',dtype=rasterio.uint16,count=1,compress='lzw',nodata=0,bigtiff='YES'# Output will be larger than 4GB)windows=src.block_windows(1)withrasterio.open('less_massive_dem_with_as_int.tif','w',**kwargs)asdst:foridx,windowinwindows:src_data=src.read_band(1,window=window)# Source nodata value is a very small negative number# Converting in to zero for the output rasternp.putmask(src_data,src_data<0,0)dst_data=(src_data*CM_IN_FOOT).astype(rasterio.uint16)dst.write_band(1,dst_data,window=window)

This uses the windowed read and write and windows iterator features of
rasterio that I worked on with Asger Skovbo Petersen.

Do you have any more short rasterio code examples? I’d love to start collecting
these into a cookbook of sorts.