def map_haz(fig, plt, haz_map_file, sitelon, sitelat, **kwargs): ''' kwargs: shpfile: path to area source - is a list of files resolution: c (crude), l (low), i (intermediate), h (high), f (full) mbuffer: map buffer in degrees ''' from openquake.nrmllib.hazard.parsers import GMFScenarioParser from mpl_toolkits.basemap import Basemap from numpy import arange, array, log10, mean, mgrid, percentile from matplotlib.mlab import griddata from matplotlib import colors, colorbar, cm from os import path from mapping_tools import drawshapepoly, labelpolygon import shapefile # set kwargs drawshape = False res = 'c' mbuff = -0.3 keys = ['shapefile', 'resolution', 'mbuffer'] for key in keys: if key in kwargs: if key == 'shapefile': shpfile = kwargs[key] drawshape = True if key == 'resolution': res = kwargs[key] if key == 'mbuffer': mbuff = kwargs[key] gmfsp = GMFScenarioParser(haz_map_file).parse() metadata, values = parse_nrml_hazard_map(haz_map_file) hazvals = [] latlist = [] lonlist = [] for val in values: lonlist.append(val[0]) latlist.append(val[1]) hazvals.append(val[2]) # get map bounds llcrnrlat = min(latlist) - mbuff / 2. urcrnrlat = max(latlist) + mbuff / 2. llcrnrlon = min(lonlist) - mbuff urcrnrlon = max(lonlist) + mbuff lon_0 = mean([llcrnrlon, urcrnrlon]) lat_1 = percentile([llcrnrlat, urcrnrlat], 25) lat_2 = percentile([llcrnrlat, urcrnrlat], 75) m = Basemap(llcrnrlon=llcrnrlon,llcrnrlat=llcrnrlat, \ urcrnrlon=urcrnrlon,urcrnrlat=urcrnrlat, projection='lcc',lat_1=lat_1,lat_2=lat_2,lon_0=lon_0, resolution=res,area_thresh=1000.) m.drawcoastlines(linewidth=0.5, color='k') m.drawcountries() # draw parallels and meridians. m.drawparallels(arange(-90., 90., 1.), labels=[1, 0, 0, 0], fontsize=10, dashes=[2, 2], color='0.5', linewidth=0.5) m.drawmeridians(arange(0., 360., 1.), labels=[0, 0, 0, 1], fontsize=10, dashes=[2, 2], color='0.5', linewidth=0.5) # make regular grid N = 150j extent = (min(lonlist), max(lonlist), min(latlist), max(latlist)) xs, ys = mgrid[extent[0]:extent[1]:N, extent[2]:extent[3]:N] resampled = griddata(array(lonlist), array(latlist), log10(array(hazvals)), xs, ys) ############################################################################# # transform grid to basemap # get 1D lats and lons for map transform lons = ogrid[extent[0]:extent[1]:N] lats = ogrid[extent[2]:extent[3]:N] # transform to map projection if max(lonlist) - min(lonlist) < 1: transspace = 500 elif max(lonlist) - min(lonlist) < 5: transspace = 1000 elif max(lonlist) - min(lonlist) < 10: transspace = 2000 else: transspace = 5000 nx = int((m.xmax - m.xmin) / transspace) + 1 ny = int((m.ymax - m.ymin) / transspace) + 1 transhaz = m.transform_scalar(resampled.T, lons, lats, nx, ny) m.imshow(transhaz, cmap='Spectral_r', extent=extent, vmin=-2, vmax=log10(2.), zorder=0) # plot site xx, yy = m(sitelon, sitelat) plt.plot(xx, yy, '*', ms=20, mec='k', mew=2.0, mfc="None") # superimpose area source shapefile if drawshape == True: for shp in shpfile: sf = shapefile.Reader(shp) drawshapepoly(m, plt, sf, col='k', lw=1.5, polyline=True) labelpolygon(m, plt, sf, 'CODE', fsize=14) # set colourbar # set cb for final fig plt.gcf().subplots_adjust(bottom=0.1) cax = fig.add_axes([0.6, 0.05, 0.25, 0.02]) # setup colorbar axes. norm = colors.Normalize(vmin=-2, vmax=log10(2.)) cb = colorbar.ColorbarBase(cax, cmap=cm.Spectral_r, norm=norm, orientation='horizontal') # set cb labels #linticks = array([0.01, 0.03, 0.1, 0.3 ]) logticks = arange(-2, log10(2.), 0.25) cb.set_ticks(logticks) labels = [str('%0.2f' % 10**x) for x in logticks] cb.set_ticklabels(labels) # get map probabiltiy from filename mprob = path.split(haz_map_file)[-1].split('_')[-1].split('-')[0] probstr = str(int(float(mprob) * 100)) # set colourbar title if metadata['statistics'] == 'mean': titlestr = ''.join((metadata['imt'], ' ', metadata['sa_period'], ' s ', \ probstr,'% in ',metadata['investigation_time'][0:-2], \ ' Year Mean Hazard (g)')) #cb.set_ticklabels(labels) cb.set_label(titlestr, fontsize=12) return plt
''' ########################################################################################## # parse mag zones ########################################################################################## import shapefile from shapely.geometry import Point, Polygon from mapping_tools import get_field_data, drawshapepoly, labelpolygon shpfile = 'shapefile/australia_ml_regions.shp' sf = shapefile.Reader(shpfile) shapes = sf.shapes() drawshapepoly(m, plt, sf, fillcolor='none', edgecolor='r', lw=2.5, zorder=0) labelpolygon(m, plt, sf, 'ML_REGION', fsize=24, addOutline=True) ########################################################################################## # add ficticious earthquakes ########################################################################################## import matplotlib.patheffects as path_effects data = loadtxt('ficticious_eq_locs.csv', delimiter=',') x, y = m(data[:, 0], data[:, 1]) plt.plot(x, y, '*', mfc='k', mec='k', mew=0, markersize=16,
x, y = m(lons[idx], lats[idx]) m.plot(x, y, 'o', ms=2, mfc='0.4', mec='0.4', zorder=1) idx = where((mags >= 5.6) & (year >= 1880))[0] x, y = m(lons[idx], lats[idx]) m.plot(x, y, 'o', ms=7, mfc='maroon', mec='w', mew=1, zorder=2) ########################################################################################## # add shapefiles ########################################################################################## shpfile = '../../Catalogue/Completeness_NSHA18/Leonard_17_mcomp_d.shp' sf = shapefile.Reader(shpfile) drawshapepoly(m, plt, sf, col='r', lw=2) # label shapes labelpolygon(m, plt, sf, 'NAME', fweight='normal', fsize=16, addOutline=True) # add subplot letter x, y = m(llcrnrlon + 1, urcrnrlat - 1) plt.text(x, y, '(a)', size=22, ha='left', va='top', weight='normal') ########################################################################################## # add 2nd axis ########################################################################################## ax = fig.add_subplot(122) plt.tick_params(labelsize=8) m = Basemap(projection='merc',\ llcrnrlon=llcrnrlon,llcrnrlat=llcrnrlat, \ urcrnrlon=urcrnrlon,urcrnrlat=urcrnrlat,\
cindex = [] # loop thru b values for b, n0 in zip(src_bval, src_n0): if not isnan(n0): idx = interp(b, [b_min, b_max], [0, ncolours-1]) cindex.append(int(round(idx))) # get cmap cmap = plt.get_cmap('YlOrRd', ncolours) # plt source zone boundary drawshapepoly(m2, plt, sf, cindex=cindex, cmap=cmap, ncolours=ncolours, fillshape=True) labelpolygon(m2, plt, sf, 'CODE', value='CBGZ', col='k', fweight='normal', fsize=14, addOutline=False) x,y = m2(130., -35.35) plt.text(x, y, 'EBGZ', va='center', ha='center', fontsize=14) xlim = ax.get_xlim() xtxt = xlim[1] * 0.02 ylim = ax.get_ylim() ytxt = ylim[1] * 0.02 plt.text(xtxt, ytxt, 'a)', fontsize=17, va='bottom', ha='left') # label polygons #labelpolygon(m2, plt, sf, 'CODE') ############################################################################### # make map2 ###############################################################################