def formatCityList(self,citylist,ncities): if len(citylist) == 0: return 'No cities were exposed.' citylist = self.getCityTable(citylist) ncities = min(ncities,len(citylist)) #citylist2 = self.sortCities(citylist,method) cwidth = 0 for city in citylist: if len(city['name']) > cwidth: cwidth = len(city['name']) fmt1 = '%-4s %-' + str(cwidth) + 's %-10s\n' fmt2 = '%-' + str(cwidth) + 's %-10s\n' if 'mmi' in citylist[0].keys(): citytext = fmt1 % ('MMI','City','Population') else: citytext = fmt2 % ('City','Population') for i in range(0,ncities): city = citylist[i] name = city['name'] pop = commify(city['pop']) if 'mmi' in city.keys() and city['mmi'] >= 1: mmi = decToRoman(round(city['mmi'])) citytext = citytext + fmt1 % (mmi,name,pop) else: citytext = citytext + fmt2 % (name,pop) return citytext
def printExposure(exposure): for expbin in exposure: tpl = (expbin['mintime'],expbin['maxtime'],commify(expbin['exposure'])) print '\t%2i - %2i seconds: %s' % tpl
def makeDualMap( lqgrid, lsgrid, topogrid, slopegrid, eventdict, outfolder, isScenario=False, roadslist=[], colors={}, cityfile=None ): # create the figure and axes instances. fig = plt.figure() ax = fig.add_axes([0.1, 0.1, 0.8, 0.8]) # setup of basemap ('lcc' = lambert conformal conic). # use major and minor sphere radii from WGS84 ellipsoid. xmin, xmax, ymin, ymax = topogrid.getRange() clat = ymin + (ymax - ymin) / 2.0 clon = xmin + (xmax - xmin) / 2.0 m = Basemap( llcrnrlon=xmin, llcrnrlat=ymin, urcrnrlon=xmax, urcrnrlat=ymax, rsphere=(6378137.00, 6356752.3142), resolution="h", area_thresh=1000.0, projection="lcc", lat_1=clat, lon_0=clon, ax=ax, ) clear_color = [0, 0, 0, 0.0] # topoextent = getGridExtent(topogrid,m) # rgb,topopalette = getTopoRGB(topogrid) # color_tuple = rgb.transpose((1,0,2)).reshape((rgb.shape[0]*rgb.shape[1],rgb.shape[2]))/255.0 hillsh = hillshade(topogrid, 315, 50) # divide by 2 to mute colors topogrid2 = GMTGrid() topogrid2.loadFromGrid(topogrid) topogrid2.interpolateToGrid(lqgrid.geodict) # define what cells are below sea level iwater = np.where(topogrid2.griddata == SEA_LEVEL) lons = np.arange(xmin, xmax, topogrid.getGeoDict()["xdim"]) lons = lons[: topogrid.getGeoDict()["ncols"]] # make sure right length lats = np.arange(ymax, ymin, -topogrid.getGeoDict()["ydim"]) # backwards so it plots right lats = lats[: topogrid.getGeoDict()["nrows"]] llons, llats = np.meshgrid(lons, lats) # make meshgrid x, y = m(llons, llats) # get projection coordinates im = m.pcolormesh(x, y, hillsh, cmap="Greys", lw=0, vmin=0.0, vmax=550.0, rasterized=True) # im = m.imshow(rgb,cmap=topopalette,extent=topoextent) # figure out the aspect ratio of the axes print "Map width: %s" % commify(int(m.xmax - m.xmin)) print "Map height: %s" % commify(int(m.ymax - m.ymin)) aspect = (m.xmax - m.xmin) / (m.ymax - m.ymin) slope = -0.33 intercept = 0.463 leftx = slope * aspect + intercept print "Left edge of left colorbar is at: %.2f" % leftx slope = 20.833 intercept = -0.833 titlex = slope * aspect + intercept print "Title X of right colorbar is at: %.2f" % titlex # this business apparently has to happen after something has been # rendered on the map, which I guess makes sense. # draw the map ticks on outside of all edges fig.canvas.draw() # have to do this for tick stuff to show xticks, xlabels, yticks, ylabels = getMapTicks(m, xmin, xmax, ymin, ymax) plt.sca(ax) plt.tick_params(axis="both", direction="in", right="on", colors="white") plt.xticks(xticks, xlabels, size=8) plt.yticks(yticks, ylabels, size=8) for tick in ax.axes.yaxis.get_major_ticks(): tick.set_pad(-38) tick.label2.set_horizontalalignment("right") for tick in ax.axes.xaxis.get_major_ticks(): tick.set_pad(-15) tick.label2.set_verticalalignment("top") [i.set_color("white") for i in plt.gca().get_xticklabels()] [i.set_color("white") for i in plt.gca().get_yticklabels()] # render liquefaction lqdat = lqgrid.getData().copy() * 100.0 palettelq = cm.autumn_r i = np.where(lqdat < 2.0) lqdat[i] = 0 lqdat[iwater] = 0 lqdatm = np.ma.masked_equal(lqdat, 0) palettelq.set_bad(clear_color, alpha=0.0) xmin, xmax, ymin, ymax = lqgrid.getRange() lons = np.arange(xmin, xmax, lqgrid.getGeoDict()["xdim"]) lons = lons[ : lqgrid.getGeoDict()["ncols"] ] # make sure it's the right length, sometimes it is one too long, sometimes it isn't because of GMTGrid issue lats = np.arange(ymax, ymin, -lqgrid.getGeoDict()["ydim"]) # backwards so it plots right side up lats = lats[ : lqgrid.getGeoDict()["nrows"] ] # make sure it's the right length, sometimes it is one too long, sometimes it isn't because of GMTGrid issue # make meshgrid llons, llats = np.meshgrid(lons, lats) x, y = m(llons, llats) # get projection coordinates lqprobhandle = m.pcolormesh(x, y, lqdatm, lw=0, cmap=palettelq, vmin=2.0, vmax=20.0, alpha=ALPHA, rasterized=True) # extent = getGridExtent(lqgrid,m) # lqprobhandle = m.imshow(lqdatm,cmap=palettelq,vmin=2.0,vmax=20.0,alpha=ALPHA,origin='upper',extent=extent) norm = mpl.colors.Normalize(vmin=2.0, vmax=20.0) cbarlq = m.colorbar(mappable=lqprobhandle, norm=norm, cmap=palettelq) cbarlq.solids.set_rasterized(True) # cbarlq.solids.set_edgecolor("face") # plt.draw() cbarlq.set_ticks([2.0, 11.0, 19.0]) cbarlq.set_ticklabels(["Low", "Medium", "High"]) # vertically oriented colorbar # render landslide topogrid2 = GMTGrid() topogrid2.loadFromGrid(topogrid) topogrid2.interpolateToGrid(lsgrid.geodict) lsdat = lsgrid.getData().copy() * 100.0 clear_color = [0, 0, 0, 0.0] palettels = cm.cool i = np.where(lsdat < 2.0) lsdat[i] = 0 lsdat[iwater] = 0 lsdatm = np.ma.masked_equal(lsdat, 0) palettels.set_bad(clear_color, alpha=0.0) xmin, xmax, ymin, ymax = lsgrid.getRange() lons = np.arange(xmin, xmax, lsgrid.getGeoDict()["xdim"]) lons = lons[ : lsgrid.getGeoDict()["ncols"] ] # make sure it's the right length, sometimes it is one too long, sometimes it isn't because of GMTGrid issue lats = np.arange(ymax, ymin, -lsgrid.getGeoDict()["ydim"]) # backwards so it plots right side up # make meshgrid lats = lats[: lsgrid.getGeoDict()["nrows"]] llons, llats = np.meshgrid(lons, lats) x, y = m(llons, llats) # get projection coordinates lsprobhandle = m.pcolormesh(x, y, lsdatm, lw=0, cmap=palettels, vmin=2.0, vmax=20.0, alpha=ALPHA, rasterized=True) # extent = getGridExtent(lsgrid,m) # lsprobhandle = plt.imshow(lsdatm,cmap=palettels,vmin=2.0,vmax=20.0,alpha=ALPHA,origin='upper',extent=extent) # draw landslide colorbar on the left side axleft = fig.add_axes([leftx, 0.1, 0.033, 0.8]) norm = mpl.colors.Normalize(vmin=2.0, vmax=20.0) cbarls = mpl.colorbar.ColorbarBase(axleft, cmap=palettels, norm=norm, orientation="vertical") cbarls.solids.set_rasterized(True) cbarls.ax.yaxis.set_ticks_position("left") cbarls.set_ticks([2.0, 11.0, 19.0]) cbarls.set_ticklabels(["Low", "Medium", "High"]) # vertically oriented colorbar # draw roads on the map, if they were provided to us roadcolor = ROADCOLOR if colors.has_key("roadcolor"): roadcolor = "#" + colors["roadcolor"] for road in roadslist: xy = list(road["geometry"]["coordinates"]) roadx, roady = zip(*xy) mapx, mapy = m(roadx, roady) m.plot(mapx, mapy, roadcolor, lw=1.0) # add city names to map with population >50,000 (add option later) if cityfile is not None: dmin = 0.04 * (m.ymax - m.ymin) xyplotted = [] cities = PagerCity(cityfile) # Find cities within bounding box boundcity = cities.findCitiesByRectangle(bounds=(xmin, xmax, ymin, ymax)) # Just keep 7 biggest cities thresh = sorted([cit["pop"] for cit in boundcity])[-7] plotcity = [cit for cit in boundcity if cit["pop"] >= thresh] # For cities that are more than one xth of the xwidth apart, keep only the larger one pass # do later # Plot cities for ( cit ) in ( plotcity ): # should sort so it plots them in order of population so larger cities are preferentially plotted - do later xi, yi = m(cit["lon"], cit["lat"]) dist = [np.sqrt((xi - x0) ** 2 + (yi - y0) ** 2) for x0, y0 in xyplotted] if not dist or np.min(dist) > dmin: m.scatter(cit["lon"], cit["lat"], c="k", latlon=True, marker=".") ax.text(xi, yi, cit["name"], ha="right", va="top", fontsize=8) xyplotted.append((xi, yi)) # draw titles cbartitle_ls = "Landslide\nProbability" plt.text(-1.0, 1.03, cbartitle_ls, multialignment="left", axes=ax) cbartitle_ls = "Liquefaction\nProbability" plt.text(titlex, 1.03, cbartitle_ls, multialignment="left", axes=ax) axwidth = 20 # where can I get this from? # draw a map boundary, fill in oceans with water # water_color = [.47,.60,.81] # too similar to a blue shade in ls colorbar water_color = "#B8EEFF" m.drawmapboundary(fill_color=water_color) if isScenario: title = eventdict["loc"] else: timestr = eventdict["time"].strftime("%b %d %Y") title = "M%.1f %s v%i\n %s" % (eventdict["mag"], timestr, eventdict["version"], eventdict["loc"]) # draw the title on the plot ax.set_title(title) # draw star at epicenter plt.sca(ax) elat, elon = eventdict["epicenter"] ex, ey = m(elon, elat) plt.plot(ex, ey, "*", markeredgecolor="k", mfc="None", mew=1.5, ms=24) # fill in the lakes and rivers m.fillcontinents(color=clear_color, lake_color=water_color) m.drawrivers(color=water_color) # draw country boundaries countrycolor = COUNTRYCOLOR if colors.has_key("countrycolor"): countrycolor = "#" + colors["countrycolor"] m.drawcountries(color=countrycolor, linewidth=1.0) # add map scale m.drawmapscale( (xmax + xmin) / 2, (ymin + (ymax - ymin) / 150), clon, clat, np.round((((xmax - xmin) * 110) / 5) / 10.0) * 10, barstyle="fancy", ) # draw coastlines m.drawcoastlines(color="#476C91", linewidth=0.5) # draw scenario watermark, if scenario if isScenario: plt.sca(ax) cx, cy = m(clon, clat) plt.text(cx, cy, "SCENARIO", rotation=45, alpha=0.10, size=72, ha="center", va="center", color="red") # plt.title(ptitle,axes=ax) outfile = os.path.join(outfolder, "%s.pdf" % eventdict["eventid"]) pngfile = os.path.join(outfolder, "%s.png" % eventdict["eventid"]) plt.savefig(outfile) plt.savefig(pngfile) print "Saving map output to %s" % outfile print "Saving map output to %s" % pngfile