Skip to content

Commit

Permalink
Merge pull request #104 from ClimateImpactLab/assets
Browse files Browse the repository at this point in the history
Add visualization tools for gcp
  • Loading branch information
delgadom committed Dec 12, 2017
2 parents 62a275c + b4e1dbf commit b1c5125
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 0 deletions.
Binary file added impactlab_tools/assets/hierid_regions.dbf
Binary file not shown.
1 change: 1 addition & 0 deletions impactlab_tools/assets/hierid_regions.prj
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]]
Binary file added impactlab_tools/assets/hierid_regions.shp
Binary file not shown.
Binary file added impactlab_tools/assets/hierid_regions.shx
Binary file not shown.
63 changes: 63 additions & 0 deletions impactlab_tools/utils/visualize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import numpy as np

import matplotlib
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
from matplotlib.patches import Polygon
from matplotlib.collections import PatchCollection

import toolz


def get_shape():

return 'assets/hierid_regions'


@toolz.memoize
def prep_polygons(
shapepath=None,
projection='cyl',
**kwargs):

if shapepath is None:
shapepath = get_shape()

m = Basemap(projection=projection, llcrnrlat=-90, urcrnrlat=90,\
llcrnrlon=-180, urcrnrlon=180, resolution=None, **kwargs)
m.readshapefile(shapepath, 'shapes', drawbounds=False)

poly = []

for ii in range(len(m.shapes)):
poly.append(Polygon(m.shapes[ii], closed=False))

return m, poly

def plot_by_hierid(da, ax=None, clim=None, cmap='jet', **kwargs):

if ax is None:
ax = plt.subplot(111)

if clim is None:
clim = [da.min(), da.max()]

m, poly = prep_polygons()

hierids = np.array([m.shapes_info[i]['hierid'] for i in range(len(m.shapes))])
hierids = hierids[np.in1d(hierids, da.hierid)]

color = da.sel(hierid=hierids).values

c = PatchCollection(poly, array=color, cmap=cmap, **kwargs)
c.set_clim(clim) # set the range of colorbar here
ax.add_collection(c)
ax.set_xlim(-180, 180)
ax.set_ylim(-90,90)
ax.set_xticks(np.linspace(-180, 180, 7))
ax.set_yticks(np.linspace(-90, 90, 7))
ax.set_ylabel('Longitude')
ax.set_xlabel('Latitude')
plt.colorbar(c, ax=ax)

return ax
3 changes: 3 additions & 0 deletions requirements_conda.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ blaze==0.10.1
scipy==0.19.1
bottleneck==1.2.1
dask==0.15.4
basemap==1.0.7
matplotlib==2.1.0

0 comments on commit b1c5125

Please sign in to comment.