def test_get_object_from_config(): config = { 'modeling': { 'gmice': 'WGRW12' }, 'gmice_modules': { 'WGRW12': ['WGRW12', 'shakelib.gmice.wgrw12'] } } gmice = get_object_from_config('gmice', 'modeling', config) assert gmice.getName() == 'Worden et al. (2012)'
def contour_to_files(container, output_dir, logger, filter_size=DEFAULT_FILTER_SIZE): """ Generate contours of all IMT values. Args: container (ShakeMapOutputContainer): ShakeMapOutputContainer with ShakeMap output data. output_dir (str): Path to directory where output files will be written. logger (logging.Logger): Python logging Logger instance. Raises: LookupError: When configured file format is not supported """ # Right now geojson is all we support; if that changes, we'll have # to add a configuration or command-line option file_format = 'geojson' # open a file for writing driver, extension = FORMATS[file_format] sa_schema = { 'geometry': 'MultiLineString', 'properties': { 'value': 'float', 'units': 'str', 'color': 'str', 'weight': 'int' } } mmi_schema = { 'geometry': 'MultiLineString', 'properties': { 'value': 'float', 'units': 'str', 'color': 'str', 'weight': 'int' } } crs = { 'no_defs': True, 'ellps': 'WGS84', 'datum': 'WGS84', 'proj': 'longlat' } config = container.getConfig() gmice = get_object_from_config('gmice', 'modeling', config) gmice_imts = gmice.DEFINED_FOR_INTENSITY_MEASURE_TYPES gmice_pers = gmice.DEFINED_FOR_SA_PERIODS imtlist = container.getIMTs() for imtype in imtlist: component, imtype = imtype.split('/') fileimt = oq_to_file(imtype) oqimt = imt.from_string(imtype) if component == 'GREATER_OF_TWO_HORIZONTAL': fname = 'cont_%s.%s' % (fileimt, extension) else: fname = 'cont_%s_%s.%s' % (fileimt, component, extension) filename = os.path.join(output_dir, fname) if os.path.isfile(filename): fpath, fext = os.path.splitext(filename) flist = glob.glob(fpath + '.*') for fname in flist: os.remove(fname) if imtype == 'MMI' or not isinstance(oqimt, tuple(gmice_imts)) or \ (isinstance(oqimt, imt.SA) and oqimt.period not in gmice_pers): my_gmice = None else: my_gmice = gmice # fiona spews a warning here when driver is geojson # this warning appears to be un-catchable using # with warnings.catch_warnings() # or # logging.captureWarning() # or # even redirecting stderr/stdout to IO streams # not sure where the warning is coming from, # but there appears to be no way to stop it... with fiona.drivers(): if imtype == 'MMI': selected_schema = mmi_schema else: selected_schema = sa_schema vector_file = fiona.open(filename, 'w', driver=driver, schema=selected_schema, crs=crs) line_strings = contour(container, imtype, component, filter_size, my_gmice) for feature in line_strings: vector_file.write(feature) # Grab some metadata meta = container.getMetadata() event_info = meta['input']['event_information'] mdict = { 'eventid': event_info['event_id'], 'longitude': float(event_info['longitude']), 'latitude': float(event_info['latitude']) } logger.debug('Writing contour file %s' % filename) vector_file.close() # Get bounds tmp = fiona.open(filename) bounds = tmp.bounds tmp.close() # Read back in to add metadata/bounds data = json.load(open(filename)) data['metadata'] = mdict data['bbox'] = bounds with open(filename, 'w') as outfile: json.dump(data, outfile) ##################################### # Make an extra version of the MMI contour file # so that the current web rendering code can find it. # Delete this file once everyone has moved to new version # of ComCat code. if imtype == 'MMI': old_file = os.path.join(output_dir, 'cont_mi.json') shutil.copy(filename, old_file)
def draw_map(adict, override_scenario=False): """If adict['imtype'] is MMI, draw a map of intensity draped over topography, otherwise Draw IMT contour lines over hill-shaded topography. Args: adict (dictionary): A dictionary containing the following keys: 'imtype' (str): The intensity measure type 'topogrid' (Grid2d): A topography grid 'allcities' (Cities): A list of global cities, 'states_provinces' (Cartopy Feature): States/province boundaries. 'countries' (Cartopy Feature): Country boundaries. 'oceans' (Cartopy Feature): Oceans. 'lakes' (Cartopy Feature): Lakes. 'roads' (Shapely Feature): Roads. 'faults' (Shapely Feature): Fault traces 'datadir' (str): The path into which to deposit products 'operator' (str): The producer of this shakemap 'filter_size' (int): The size of the filter used before contouring 'info' (dictionary): The shakemap info structure 'component' (str): The intensity measure component being plotted 'imtdict' (dictionary): Dict containing the IMT grids 'rupdict' (dictionary): Dict containing the rupture data 'stationdict' (dictionary): Dict of station data 'config' (dictionary): The configuration data for this shakemap 'tdict' (dictionary): The text strings to be printed on the map in the user's choice of language. 'license_text' (str): License text to display at bottom of map 'license_logo' (str): Path to license logo image to display next to license text override_scenario (bool): Turn off scenario watermark. Returns: Tuple of (Matplotlib figure, Matplotlib figure): Objects containing the map generated by this function, and the intensity legend, respectively. If the imtype of this map is not 'MMI', the second element of the tuple will be None. """ imtype = adict['imtype'] imtdict = adict['imtdict'] # mmidict imtdata = np.nan_to_num(imtdict['mean'], nan=0.0) # mmidata gd = GeoDict(imtdict['mean_metadata']) imtgrid = Grid2D(imtdata, gd) # mmigrid gd = imtgrid.getGeoDict() # Retrieve the epicenter - this will get used on the map rupture = rupture_from_dict(adict['ruptdict']) origin = rupture.getOrigin() center_lat = origin.lat center_lon = origin.lon # load the cities data, limit to cities within shakemap bounds cities = adict['allcities'].limitByBounds((gd.xmin, gd.xmax, gd.ymin, gd.ymax)) # get the map boundaries and figure size bounds, figsize, aspect = _get_map_info(gd) # Note: dimensions are: [left, bottom, width, height] dim_left = 0.1 dim_bottom = 0.19 dim_width = 0.8 dim_height = dim_width/aspect if dim_height > 0.8: dim_height = 0.8 dim_width = 0.8 * aspect dim_left = (1.0 - dim_width) / 2 # Create the MercatorMap object, which holds a separate but identical # axes object used to determine collisions between city labels. mmap = MercatorMap( bounds, figsize, cities, padding=0.5, dimensions=[dim_left, dim_bottom, dim_width, dim_height]) fig = mmap.figure ax = mmap.axes # this needs to be done here so that city label collision # detection will work fig.canvas.draw() # get the geographic projection object geoproj = mmap.geoproj # get the mercator projection object proj = mmap.proj # get the proj4 string - used by Grid2D project() method projstr = proj.proj4_init # get the projected IMT and topo grids pimtgrid, ptopogrid = _get_projected_grids(imtgrid, adict['topogrid'], projstr) # get the projected geodict proj_gd = pimtgrid.getGeoDict() pimtdata = pimtgrid.getData() ptopo_data = ptopogrid.getData() mmimap = ColorPalette.fromPreset('mmi') if imtype == 'MMI': draped_hsv = _get_draped(pimtdata, ptopo_data, mmimap) else: # get the draped topo data topo_colormap = ColorPalette.fromPreset('shaketopo') draped_hsv = _get_shaded(ptopo_data, topo_colormap) # convert units if imtype == 'PGV': pimtdata = np.exp(pimtdata) else: pimtdata = np.exp(pimtdata) * 100 plt.sca(ax) ax.set_xlim(proj_gd.xmin, proj_gd.xmax) ax.set_ylim(proj_gd.ymin, proj_gd.ymax) img_extent = (proj_gd.xmin, proj_gd.xmax, proj_gd.ymin, proj_gd.ymax) plt.imshow(draped_hsv, origin='upper', extent=img_extent, zorder=IMG_ZORDER, interpolation='none') config = adict['config'] gmice = get_object_from_config('gmice', 'modeling', config) gmice_imts = gmice.DEFINED_FOR_INTENSITY_MEASURE_TYPES gmice_pers = gmice.DEFINED_FOR_SA_PERIODS oqimt = imt.from_string(imtype) if imtype != 'MMI' and (not isinstance(oqimt, tuple(gmice_imts)) or (isinstance(oqimt, imt.SA) and oqimt.period not in gmice_pers)): my_gmice = None else: my_gmice = gmice if imtype != 'MMI': # call the contour module in plotting to get the vertices of the # contour lines contour_objects = contour(imtdict, imtype, adict['filter_size'], my_gmice) # get a color palette for the levels we have # levels = [c['properties']['value'] for c in contour_objects] # cartopy shapely feature has some weird behaviors, so I had to go # rogue and draw contour lines/labels myself. # To choose which contours to label, we will keep track of the lengths # of contours, grouped by isovalue contour_lens = defaultdict(lambda: []) def arclen(path): """ Compute the arclength of *path*, which should be a list of pairs of numbers. """ x0, y0 = [np.array(c) for c in zip(*path)] x1, y1 = [np.roll(c, -1) for c in (x0, y0)] # offset by 1 # don't include first-last vertices as an edge: x0, y0, x1, y1 = [c[:-1] for c in (x0, y0, x1, y1)] return np.sum(np.sqrt((x0 - x1)**2 + (y0 - y1)**2)) # draw dashed contours first, the ones over land will be overridden by # solid contours for contour_object in contour_objects: props = contour_object['properties'] multi_lines = sShape(contour_object['geometry']) pmulti_lines = proj.project_geometry(multi_lines, src_crs=geoproj) for multi_line in pmulti_lines: pmulti_line = mapping(multi_line)['coordinates'] x, y = zip(*pmulti_line) contour_lens[props['value']].append(arclen(pmulti_line)) # color = imt_cmap.getDataColor(props['value']) ax.plot(x, y, color=props['color'], linestyle='dashed', zorder=DASHED_CONTOUR_ZORDER) white_box = dict( boxstyle="round", ec=(0, 0, 0), fc=(1., 1, 1), color='k' ) # draw solid contours next - the ones over water will be covered by # ocean polygon for contour_object in contour_objects: props = contour_object['properties'] multi_lines = sShape(contour_object['geometry']) pmulti_lines = proj.project_geometry(multi_lines, src_crs=geoproj) # only label long contours (relative to others with the same # isovalue) min_len = np.array(contour_lens[props['value']]).mean() for multi_line in pmulti_lines: pmulti_line = mapping(multi_line)['coordinates'] x, y = zip(*pmulti_line) # color = imt_cmap.getDataColor(props['value']) ax.plot(x, y, color=props['color'], linestyle='solid', zorder=CONTOUR_ZORDER) if arclen(pmulti_line) >= min_len: # try to label each segment with black text in a white box xc = x[int(len(x)/3)] yc = y[int(len(y)/3)] if _label_close_to_edge( xc, yc, proj_gd.xmin, proj_gd.xmax, proj_gd.ymin, proj_gd.ymax): continue # TODO: figure out if box is going to go outside the map, # if so choose a different point on the line. # For small values, use scientific notation with 1 sig fig # to avoid multiple contours labelled 0.0: value = props['value'] fmt = '%.1g' if abs(value) < 0.1 else '%.1f' ax.text(xc, yc, fmt % value, size=8, ha="center", va="center", bbox=white_box, zorder=AXES_ZORDER-1) # make the border thicker lw = 2.0 ax.outline_patch.set_zorder(BORDER_ZORDER) ax.outline_patch.set_linewidth(lw) ax.outline_patch.set_joinstyle('round') ax.outline_patch.set_capstyle('round') # Coastlines will get drawn when we draw the ocean edges # ax.coastlines(resolution="10m", zorder=COAST_ZORDER, linewidth=3) if adict['states_provinces']: ax.add_feature(adict['states_provinces'], edgecolor='0.5', zorder=COAST_ZORDER) if adict['countries']: ax.add_feature(adict['countries'], edgecolor='black', zorder=BORDER_ZORDER) if adict['oceans']: ax.add_feature(adict['oceans'], edgecolor='black', zorder=OCEAN_ZORDER) if adict['lakes']: ax.add_feature(adict['lakes'], edgecolor='black', zorder=OCEAN_ZORDER) if adict['faults'] is not None: ax.add_feature(adict['faults'], edgecolor='firebrick', zorder=ROAD_ZORDER) if adict['roads'] is not None: ax.add_feature(adict['roads'], edgecolor='dimgray', zorder=ROAD_ZORDER) # draw graticules, ticks, tick labels _draw_graticules(ax, *bounds) # is this event a scenario? info = adict['info'] etype = info['input']['event_information']['event_type'] is_scenario = etype == 'SCENARIO' if is_scenario and not override_scenario: plt.text( center_lon, center_lat, adict['tdict']['title_parts']['scenario'], fontsize=72, zorder=SCENARIO_ZORDER, transform=geoproj, alpha=WATERMARK_ALPHA, color=WATERMARK_COLOR, horizontalalignment='center', verticalalignment='center', rotation=45, path_effects=[ path_effects.Stroke(linewidth=1, foreground='black')] ) # Draw the map scale in the unoccupied lower corner. corner = 'll' draw_scale(ax, corner, pady=0.05, padx=0.05, zorder=SCALE_ZORDER) # draw cities mmap.drawCities(shadow=True, zorder=CITIES_ZORDER, draw_dots=True) # Draw the epicenter as a black star plt.sca(ax) plt.plot(center_lon, center_lat, 'k*', markersize=16, zorder=EPICENTER_ZORDER, transform=geoproj) # draw the rupture polygon(s) in black, if not point rupture point_source = True if not isinstance(rupture, PointRupture): point_source = False json_dict = rupture._geojson for feature in json_dict['features']: for coords in feature['geometry']['coordinates']: for pcoords in coords: poly2d = sLineString([xy[0:2] for xy in pcoords]) ppoly = proj.project_geometry(poly2d) mppoly = mapping(ppoly)['coordinates'] for spoly in mppoly: x, y = zip(*spoly) ax.plot(x, y, 'k', lw=1, zorder=FAULT_ZORDER) # draw the station data on the map stations = adict['stationdict'] _draw_stations(ax, stations, imtype, mmimap, geoproj) _draw_title(imtype, adict) process_time = info['processing']['shakemap_versions']['process_time'] map_version = int(info['processing']['shakemap_versions']['map_version']) if imtype == 'MMI': _draw_mmi_legend(fig, mmimap, gmice, process_time, map_version, point_source, adict['tdict']) # make a separate MMI legend fig2 = plt.figure(figsize=figsize) _draw_mmi_legend(fig2, mmimap, gmice, process_time, map_version, point_source, adict['tdict']) else: _draw_imt_legend(fig, mmimap, imtype, gmice, process_time, map_version, point_source, adict['tdict']) plt.draw() fig2 = None _draw_license(fig, adict) return (fig, fig2)
def get_extent(rupture=None, config=None): """ Method to compute map extent from rupture. There are numerous methods for getting the extent: - It can be specified directly in the config file, - it can be hard coded for specific magnitude ranges in the config file, or - it can be based on the MultiGMPE for the event. All methods except for the first requires a rupture object. If no config is provided then a rupture is required and the extent is based on a generic set of active/stable. Args: rupture (Rupture): A ShakeMap Rupture instance. config (ConfigObj): ShakeMap config object. Returns: tuple: lonmin, lonmax, latmin, latmax rounded to the nearest arc-minute.. """ # ------------------------------------------------------------------------- # Check to see what parameters are specified in the extent config # ------------------------------------------------------------------------- spans = {} bounds = [] if config is not None: if 'extent' in config: if 'magnitude_spans' in config['extent']: if len(config['extent']['magnitude_spans']): if isinstance(config['extent']['magnitude_spans'], dict): spans = config['extent']['magnitude_spans'] if 'bounds' in config['extent']: if 'extent' in config['extent']['bounds']: if config['extent']['bounds']['extent'][0] != -999.0: bounds = config['extent']['bounds']['extent'] # ------------------------------------------------------------------------- # Simplest option: extent was specified in the config, use that and exit. # ------------------------------------------------------------------------- if len(bounds): xmin, ymin, xmax, ymax = bounds return (xmin, xmax, ymin, ymax) if not rupture or not isinstance(rupture, Rupture): raise TypeError('get_extent() requires a rupture object if the extent ' 'is not specified in the config object.') # Find the central point origin = rupture.getOrigin() if isinstance(rupture, (QuadRupture, EdgeRupture)): # For an extended rupture, it is the midpoint between the extent of the # verticies lats = rupture.lats lons = rupture.lons # Remove nans lons = lons[~np.isnan(lons)] lats = lats[~np.isnan(lats)] clat = 0.5 * (np.nanmax(lats) + np.nanmin(lats)) clon = 0.5 * (np.nanmax(lons) + np.nanmin(lons)) else: # For a point source, it is just the epicenter clat = origin.lat clon = origin.lon mag = origin.mag # ------------------------------------------------------------------------- # Second simplest option: spans are hardcoded based on magnitude # ------------------------------------------------------------------------- if len(spans): xmin = None xmax = None ymin = None ymax = None for spankey, span in spans.items(): if mag > span[0] and mag <= span[1]: ymin = clat - span[2] / 2 ymax = clat + span[2] / 2 xmin = clon - span[3] / 2 xmax = clon + span[3] / 2 break if xmin is not None: return (xmin, xmax, ymin, ymax) # ------------------------------------------------------------------------- # Use MultiGMPE to get spans # ------------------------------------------------------------------------- if config is not None: gmpe = MultiGMPE.from_config(config) gmice = get_object_from_config('gmice', 'modeling', config) else: # Put in some default values for conf config = { 'extent': { 'mmi': { 'threshold': 4.5, 'mindist': 100, 'maxdist': 1000 } } } # Generic GMPEs choices based only on active vs stable # as defaults... stable = is_stable(origin.lon, origin.lat) if not stable: ASK14 = AbrahamsonEtAl2014() CB14 = CampbellBozorgnia2014() CY14 = ChiouYoungs2014() gmpes = [ASK14, CB14, CY14] site_gmpes = None weights = [1 / 3.0, 1 / 3.0, 1 / 3.0] gmice = WGRW12() else: Fea96 = FrankelEtAl1996MwNSHMP2008() Tea97 = ToroEtAl1997MwNSHMP2008() Sea02 = SilvaEtAl2002MwNSHMP2008() C03 = Campbell2003MwNSHMP2008() TP05 = TavakoliPezeshk2005MwNSHMP2008() AB06p = AtkinsonBoore2006Modified2011() Pea11 = PezeshkEtAl2011() Atk08p = Atkinson2008prime() Sea01 = SomervilleEtAl2001NSHMP2008() gmpes = [ Fea96, Tea97, Sea02, C03, TP05, AB06p, Pea11, Atk08p, Sea01 ] site_gmpes = [AB06p] weights = [0.16, 0.0, 0.0, 0.17, 0.17, 0.3, 0.2, 0.0, 0.0] gmice = AK07() gmpe = MultiGMPE.from_list(gmpes, weights, default_gmpes_for_site=site_gmpes) min_mmi = config['extent']['mmi']['threshold'] default_imt = imt.SA(1.0) sd_types = [const.StdDev.TOTAL] # Distance context dx = DistancesContext() # This imposes minimum/ maximum distances of: # 80 and 800 km; could make this configurable d_min = config['extent']['mmi']['mindist'] d_max = config['extent']['mmi']['maxdist'] dx.rjb = np.logspace(np.log10(d_min), np.log10(d_max), 2000) # Details don't matter for this; assuming vertical surface rupturing fault # with epicenter at the surface. dx.rrup = dx.rjb dx.rhypo = dx.rjb dx.repi = dx.rjb dx.rx = np.zeros_like(dx.rjb) dx.ry0 = np.zeros_like(dx.rjb) dx.rvolc = np.zeros_like(dx.rjb) # Sites context sx = SitesContext() # Set to soft soil conditions sx.vs30 = np.full_like(dx.rjb, 180) sx = MultiGMPE.set_sites_depth_parameters(sx, gmpe) sx.vs30measured = np.full_like(sx.vs30, False, dtype=bool) sx = Sites._addDepthParameters(sx) sx.backarc = np.full_like(sx.vs30, False, dtype=bool) # Rupture context rx = RuptureContext() rx.mag = origin.mag rx.rake = 0.0 # From WC94... rx.width = 10**(-0.76 + 0.27 * rx.mag) rx.dip = 90.0 rx.ztor = origin.depth rx.hypo_depth = origin.depth gmpe_imt_mean, _ = gmpe.get_mean_and_stddevs(sx, rx, dx, default_imt, sd_types) # Convert to MMI gmpe_to_mmi, _ = gmice.getMIfromGM(gmpe_imt_mean, default_imt) # Minimum distance that exceeds threshold MMI? dists_exceed_mmi = dx.rjb[gmpe_to_mmi > min_mmi] if len(dists_exceed_mmi): mindist_km = np.max(dists_exceed_mmi) else: mindist_km = d_min # Get a projection proj = OrthographicProjection(clon - 4, clon + 4, clat + 4, clat - 4) if isinstance(rupture, (QuadRupture, EdgeRupture)): ruptx, rupty = proj(lons, lats) else: ruptx, rupty = proj(clon, clat) xmin = np.nanmin(ruptx) - mindist_km ymin = np.nanmin(rupty) - mindist_km xmax = np.nanmax(ruptx) + mindist_km ymax = np.nanmax(rupty) + mindist_km # Put a limit on range of aspect ratio dx = xmax - xmin dy = ymax - ymin ar = dy / dx if ar > 1.2: # Inflate x dx_target = dy / 1.2 ddx = dx_target - dx xmax = xmax + ddx / 2 xmin = xmin - ddx / 2 if ar < 0.83: # Inflate y dy_target = dx * 0.83 ddy = dy_target - dy ymax = ymax + ddy / 2 ymin = ymin - ddy / 2 lonmin, latmin = proj(np.array([xmin]), np.array([ymin]), reverse=True) lonmax, latmax = proj(np.array([xmax]), np.array([ymax]), reverse=True) # # Round coordinates to the nearest minute -- that should make the # output grid register with common grid resolutions (60c, 30c, # 15c, 7.5c) # logging.debug("Extent: %f, %f, %f, %f" % (lonmin, lonmax, latmin, latmax)) return _round_coord(lonmin[0]), _round_coord(lonmax[0]), \ _round_coord(latmin[0]), _round_coord(latmax[0])
def _get_extent_from_multigmpe(rupture, config=None): """ Use MultiGMPE to determine extent """ (clon, clat) = _rupture_center(rupture) origin = rupture.getOrigin() if config is not None: gmpe = MultiGMPE.from_config(config) gmice = get_object_from_config('gmice', 'modeling', config) if imt.SA in gmice.DEFINED_FOR_INTENSITY_MEASURE_TYPES: default_imt = imt.SA(1.0) elif imt.PGV in gmice.DEFINED_FOR_INTENSITY_MEASURE_TYPES: default_imt = imt.PGV() else: default_imt = imt.PGA() else: # Put in some default values for conf config = { 'extent': { 'mmi': { 'threshold': 4.5, 'mindist': 100, 'maxdist': 1000 } } } # Generic GMPEs choices based only on active vs stable # as defaults... stable = is_stable(origin.lon, origin.lat) if not stable: ASK14 = AbrahamsonEtAl2014() CB14 = CampbellBozorgnia2014() CY14 = ChiouYoungs2014() gmpes = [ASK14, CB14, CY14] site_gmpes = None weights = [1/3.0, 1/3.0, 1/3.0] gmice = WGRW12() else: Fea96 = FrankelEtAl1996MwNSHMP2008() Tea97 = ToroEtAl1997MwNSHMP2008() Sea02 = SilvaEtAl2002MwNSHMP2008() C03 = Campbell2003MwNSHMP2008() TP05 = TavakoliPezeshk2005MwNSHMP2008() AB06p = AtkinsonBoore2006Modified2011() Pea11 = PezeshkEtAl2011() Atk08p = Atkinson2008prime() Sea01 = SomervilleEtAl2001NSHMP2008() gmpes = [Fea96, Tea97, Sea02, C03, TP05, AB06p, Pea11, Atk08p, Sea01] site_gmpes = [AB06p] weights = [0.16, 0.0, 0.0, 0.17, 0.17, 0.3, 0.2, 0.0, 0.0] gmice = AK07() gmpe = MultiGMPE.from_list( gmpes, weights, default_gmpes_for_site=site_gmpes) default_imt = imt.SA(1.0) min_mmi = config['extent']['mmi']['threshold'] sd_types = [const.StdDev.TOTAL] # Distance context dx = DistancesContext() # This imposes minimum/ maximum distances of: # 80 and 800 km; could make this configurable d_min = config['extent']['mmi']['mindist'] d_max = config['extent']['mmi']['maxdist'] dx.rjb = np.logspace(np.log10(d_min), np.log10(d_max), 2000) # Details don't matter for this; assuming vertical surface rupturing fault # with epicenter at the surface. dx.rrup = dx.rjb dx.rhypo = dx.rjb dx.repi = dx.rjb dx.rx = np.zeros_like(dx.rjb) dx.ry0 = np.zeros_like(dx.rjb) dx.rvolc = np.zeros_like(dx.rjb) # Sites context sx = SitesContext() # Set to soft soil conditions sx.vs30 = np.full_like(dx.rjb, 180) sx = MultiGMPE.set_sites_depth_parameters(sx, gmpe) sx.vs30measured = np.full_like(sx.vs30, False, dtype=bool) sx = Sites._addDepthParameters(sx) sx.backarc = np.full_like(sx.vs30, False, dtype=bool) # Rupture context rx = RuptureContext() rx.mag = origin.mag rx.rake = 0.0 # From WC94... rx.width = 10**(-0.76 + 0.27*rx.mag) rx.dip = 90.0 rx.ztor = origin.depth rx.hypo_depth = origin.depth gmpe_imt_mean, _ = gmpe.get_mean_and_stddevs( sx, rx, dx, default_imt, sd_types) # Convert to MMI gmpe_to_mmi, _ = gmice.getMIfromGM(gmpe_imt_mean, default_imt) # Minimum distance that exceeds threshold MMI? dists_exceed_mmi = dx.rjb[gmpe_to_mmi > min_mmi] if len(dists_exceed_mmi): mindist_km = np.max(dists_exceed_mmi) else: mindist_km = d_min # Get a projection proj = OrthographicProjection(clon - 4, clon + 4, clat + 4, clat - 4) if isinstance(rupture, (QuadRupture, EdgeRupture)): ruptx, rupty = proj( rupture.lons[~np.isnan(rupture.lons)], rupture.lats[~np.isnan(rupture.lats)] ) else: ruptx, rupty = proj(clon, clat) xmin = np.nanmin(ruptx) - mindist_km ymin = np.nanmin(rupty) - mindist_km xmax = np.nanmax(ruptx) + mindist_km ymax = np.nanmax(rupty) + mindist_km # Put a limit on range of aspect ratio dx = xmax - xmin dy = ymax - ymin ar = dy / dx if ar > 1.2: # Inflate x dx_target = dy / 1.2 ddx = dx_target - dx xmax = xmax + ddx / 2 xmin = xmin - ddx / 2 if ar < 0.83: # Inflate y dy_target = dx * 0.83 ddy = dy_target - dy ymax = ymax + ddy / 2 ymin = ymin - ddy / 2 lonmin, latmin = proj(np.array([xmin]), np.array([ymin]), reverse=True) lonmax, latmax = proj(np.array([xmax]), np.array([ymax]), reverse=True) # # Round coordinates to the nearest minute -- that should make the # output grid register with common grid resolutions (60c, 30c, # 15c, 7.5c) # logging.debug("Extent: %f, %f, %f, %f" % (lonmin, lonmax, latmin, latmax)) return _round_coord(lonmin[0]), _round_coord(lonmax[0]), \ _round_coord(latmin[0]), _round_coord(latmax[0])
def create_polygons(container, datadir, logger, max_workers, method='pcontour'): """ Generates a set of closed polygons (with or without holes) using the specified method (either pcontour or skimage), and uses fiona to convert the resulting GeoJSON objects into ESRI-style shape files which are then zipped into an archive along with .prj, .lyr, and metadata .xml files. A warning will be emitted if .lyr, or .xml files cannot be found for the ground motion parameter in question. Args: container (ShakeMapOutputContainer): An open ShakeMap output container object. datadir (str): The products directory for the event in question. logger (logger): This module's logger object. method (str): Contouring implementation to use (either 'pcontour' or 'skimage') Returns: (nothing): Nothing. """ # gmice info for shakelib.plotting.contour config = container.getConfig() gmice = get_object_from_config('gmice', 'modeling', config) gmice_imts = gmice.DEFINED_FOR_INTENSITY_MEASURE_TYPES gmice_pers = gmice.DEFINED_FOR_SA_PERIODS component = list(container.getComponents())[0] imts = container.getIMTs(component) if method == 'pcontour': schema = { 'properties': OrderedDict([('AREA', 'float:13.3'), ('PERIMETER', 'float:14.3'), ('PGAPOL_', 'int:12'), ('PGAPOL_ID', 'int:12'), ('GRID_CODE', 'int:12'), ('PARAMVALUE', 'float:14.4')]), 'geometry': 'Polygon' } elif method == 'skimage': schema = { 'properties': OrderedDict([('value', 'float:2.1'), ('units', 'str'), ('color', 'str'), ('weight', 'float:13.3')]), 'geometry': 'MultiLineString' } else: raise ValueError('Unknown contouring method {}'.format(method)) smdata = os.path.join(get_data_path(), 'gis') # Make a directory for the files to live in prior to being zipped alist = [] with tempfile.TemporaryDirectory(dir=datadir) as tdir: for imt in imts: gdict = container.getIMTGrids(imt, component) fgrid = gdict['mean'] if imt == 'MMI': fname = 'mi' elif imt == 'PGV': fname = 'pgv' else: fname = oq_to_file(imt) if method == 'pcontour': my_gmice = None if imt == 'MMI': contour_levels = np.arange(0.1, 10.2, 0.2) elif imt == 'PGV': fgrid = np.exp(fgrid) cont_max = np.ceil(np.max(fgrid)) + 2.0 contour_levels = np.arange(1.0, cont_max, 2.0) if contour_levels.size == 0: contour_levels = np.array([1.0]) else: fgrid = np.exp(fgrid) cont_max = (np.ceil(100 * np.max(fgrid)) + 2.0) / 100.0 contour_levels = np.arange(0.01, cont_max, 0.02) if contour_levels.size == 0: contour_levels = np.array([0.01]) else: # skimage method chooses its own levels contour_levels = None # but wants gmice info oqimt = OQIMT.from_string(imt) if imt == 'MMI' or not isinstance(oqimt, tuple(gmice_imts)) or \ (isinstance(oqimt, OQIMT.SA) and oqimt.period not in gmice_pers): my_gmice = None else: my_gmice = gmice a = { 'fgrid': fgrid, 'dx': gdict['mean_metadata']['dx'], 'dy': gdict['mean_metadata']['dy'], 'xmin': gdict['mean_metadata']['xmin'], 'ymax': gdict['mean_metadata']['ymax'], 'contour_levels': contour_levels, 'tdir': tdir, 'fname': fname, 'schema': schema, 'imt': imt, 'gmice': my_gmice, 'gdict': gdict } alist.append(a) copyfile(os.path.join(smdata, 'WGS1984.prj'), os.path.join(tdir, fname + '.prj')) lyrfile = os.path.join(smdata, fname + '.lyr') if not os.path.isfile(lyrfile): logger.warning("No " + fname + ".lyr file in " + smdata) else: copyfile(lyrfile, os.path.join(tdir, fname + '.lyr')) xmlfile = os.path.join(smdata, fname + '.shp.xml') if not os.path.isfile(xmlfile): logger.warning("No " + fname + ".shp.xml file in " + smdata) else: copyfile(xmlfile, os.path.join(tdir, fname + '.shp.xml')) worker = partial(make_shape_files, method=method) if max_workers > 0: with cf.ProcessPoolExecutor(max_workers=max_workers) as ex: results = ex.map(worker, alist) list(results) else: for adict in alist: worker(adict) zfilename = os.path.join(datadir, 'shape.zip') zfile = zipfile.ZipFile(zfilename, mode='w', compression=zipfile.ZIP_DEFLATED) filelist = [] for (dirpath, dirnames, filenames) in os.walk(tdir): filelist.extend(filenames) break for sfile in filelist: zfile.write(os.path.join(tdir, sfile), sfile) zfile.close()