def create_png(event_dir, lsmodels=None, lqmodels=None, mercator=True, lsmask=0.002, lqmask=0.005, legends=False, eventsource='', eventsourcecode=''): """ Creates transparent PNG file for website. Args: event_dir (srt): Directory containing ground failure results. lsmodels (list): List of dictionaries of model summary info compiled by the hazdev function. If not specified, code will search for the hdf5 files for the preferred model and will create this dictionary and will apply default colorbars and bins. lqmodels (list): Same as above for liquefaction. mercator (bool): Project raster to web mercator lsmask (float): Mask all landslide cells with probabilities below this threshold lqmask (float): Mask all liquefaction cells with probabilities below this threshold legends (bool): if True, will produce png files of legends for each preferred model Returns: .png map overlays and .json files specifying their mapped extents """ filenames = [] files = os.listdir(event_dir) if lsmodels is None: # Read in the "preferred" model for landslides ls_mod_file = [f for f in files if 'jessee_2017.hdf5' in f] if len(ls_mod_file) == 1: ls_file = os.path.join(event_dir, ls_mod_file[0]) ls_mod = loadlayers(ls_file) filesnippet = 'jessee_2017' out = make_rgba(ls_mod['model']['grid'], mask=lsmask, mercator=mercator, levels=DFBINS, colorlist=DFCOLORS) rgba_img, ls_extent, lmin, lmax, cmap = out filen = os.path.join(event_dir, '%s_extent.json' % filesnippet) filenames.append(filen) with open(filen, 'w') as f: json.dump(ls_extent, f) filen = os.path.join(event_dir, '%s.png' % filesnippet) plt.imsave(filen, rgba_img, vmin=lmin, vmax=lmax, cmap=cmap) else: raise OSError("Preferred landslide model result (%s) not found." % ls_mod_file) else: for lsm in lsmodels: # if lsm['preferred']: filesnippet = lsm['id'] fsh = '%s.hdf5' % filesnippet ls_mod_file = [f for f in files if fsh in f] if len(ls_mod_file) == 1: ls_file = os.path.join(event_dir, ls_mod_file[0]) ls_mod = loadlayers(ls_file) else: raise OSError( "Specified landslide model result (%s) not found." % fsh) out = make_rgba(ls_mod['model']['grid'], mask=lsmask, mercator=mercator, levels=DFBINS, colorlist=DFCOLORS) rgba_img, ls_extent, lmin, lmax, cmap = out filen = os.path.join(event_dir, '%s_extent.json' % filesnippet) filenames.append(filen) with open(filen, 'w') as f: json.dump(ls_extent, f) filen = os.path.join(event_dir, '%s.png' % filesnippet) plt.imsave(filen, rgba_img, vmin=lmin, vmax=lmax, cmap=cmap) if lqmodels is None: # read in preferred model for liquefaction if none specified lq_mod_file = [f2 for f2 in files if 'zhu_2017_general.hdf5' in f2] if len(lq_mod_file) == 1: lq_file = os.path.join(event_dir, lq_mod_file[0]) lq_mod = loadlayers(lq_file) filesnippet = 'zhu_2017_general' tempbins = DFBINS tempbins[0] = 0.005 out = make_rgba(lq_mod['model']['grid'], mask=lqmask, mercator=mercator, levels=tempbins, colorlist=DFCOLORS) rgba_img, lq_extent, lmin, lmax, cmap = out filen = os.path.join(event_dir, '%s_extent.json' % filesnippet) filenames.append(filen) with open(filen, 'w') as f: json.dump(lq_extent, f) filen = os.path.join(event_dir, '%s.png' % filesnippet) plt.imsave(filen, rgba_img, vmin=lmin, vmax=lmax, cmap=cmap) filenames.append(filen) else: raise OSError( "Preferred liquefaction model result (%s) not found." % lq_mod_file) else: for lqm in lqmodels: #if lqm['preferred']: filesnippet = lqm['id'] tempbins = DFBINS tempbins[0] = 0.005 fsh = '%s.hdf5' % filesnippet lq_mod_file = [f2 for f2 in files if fsh in f2] if len(lq_mod_file) == 1: lq_file = os.path.join(event_dir, lq_mod_file[0]) lq_mod = loadlayers(lq_file) else: raise OSError( "Specified liquefaction model result (%s) not found." % fsh) out = make_rgba(lq_mod['model']['grid'], mask=lqmask, mercator=mercator, levels=tempbins, colorlist=DFCOLORS) rgba_img, lq_extent, lmin, lmax, cmap = out filen = os.path.join(event_dir, '%s_extent.json' % filesnippet) filenames.append(filen) with open(filen, 'w') as f: json.dump(lq_extent, f) filen = os.path.join(event_dir, '%s.png' % filesnippet) plt.imsave(filen, rgba_img, vmin=lmin, vmax=lmax, cmap=cmap) filenames.append(filen) if legends: lsname, lqname = make_legends(lqmin=lqmask, lsmin=lsmask, outfolder=event_dir) filenames.append(lsname) filenames.append(lqname) return filenames
def create_info(event_dir, lsmodels=None, lqmodels=None, eventsource='', eventsourcecode='', point=True): """Create info.json for ground failure product. Args: event_dir (srt): Directory containing ground failure results. lsmodels (list): List of dictionaries of model summary info compiled by the hazdev function. If not specified, code will search for the hdf5 files for the preferred model and will create this dictionary and will apply default colorbars and bins. lqmodels (list): Same as above for liquefaction. point (bool): if True, event is a point source and warning should be displayed Returns: creates info.json for this event """ filenames = [] # Find the shakemap grid.xml file with open(os.path.join(event_dir, 'shakefile.txt'), 'r') as f: shakefile = f.read() files = os.listdir(event_dir) if lsmodels is None and lqmodels is None: # Read in the "preferred" model for landslides and liquefaction ls_mod_file = [f2 for f2 in files if 'jessee_2017.hdf5' in f2] if len(ls_mod_file) == 1: ls_file = os.path.join(event_dir, ls_mod_file[0]) ls_mod = loadlayers(ls_file) # get extents lsext = get_zoomextent(ls_mod['model']['grid']) else: raise OSError("Preferred landslide model result not found.") lq_mod_file = [f2 for f2 in files if 'zhu_2017_general.hdf5' in f2] if len(lq_mod_file) == 1: lq_file = os.path.join(event_dir, lq_mod_file[0]) lq_mod = loadlayers(lq_file) # get extents lqext = get_zoomextent(lq_mod['model']['grid']) else: raise OSError("Preferred liquefaction model result not found.") # Read in extents ls_extent_file = [ f2 for f2 in files if 'jessee_2017_extent.json' in f2 ] if len(ls_extent_file) == 1: ls_file = os.path.join(event_dir, ls_extent_file[0]) with open(ls_file) as f: jessee_extent = json.load(f) else: raise OSError("Landslide extent not found.") lq_extent_file = [ f2 for f2 in files if 'zhu_2017_general_extent.json' in f2 ] if len(lq_extent_file) == 1: lq_file = os.path.join(event_dir, lq_extent_file[0]) with open(lq_file) as f: zhu_extent = json.load(f) else: raise OSError("Liquefaction extent not found.") # Read in default paths to get location of the population grid default_file = os.path.join(os.path.expanduser('~'), '.gfail_defaults') defaults = ConfigObj(default_file) pop_file = defaults['popfile'] # Landslide alert statistics ls_stats = computeStats(ls_mod['model']['grid'], probthresh=None, shakefile=shakefile, shakethresh=10.0, shakethreshtype='pga', statprobthresh=None, pop_file=pop_file) # Liquefaction alert statistics lq_stats = computeStats(lq_mod['model']['grid'], probthresh=None, shakefile=shakefile, shakethresh=10.0, shakethreshtype='pga', statprobthresh=None, pop_file=pop_file) # Get alert levels ls_haz_level = ls_stats['hagg_0.10g'] lq_haz_level = lq_stats['hagg_0.10g'] ls_pop_level = ls_stats['exp_pop_0.10g'] lq_pop_level = lq_stats['exp_pop_0.10g'] # If hazard alert level is less than 0.1, zero it out # (due to rounding to 2 sig digits later, this can give # overly precise results, e.g., 0.000012 if we don't clip, # but this doesn't happen with pop alerts because they are # integers) if ls_haz_level < 0.1: ls_haz_level = 0.0 if lq_haz_level < 0.1: lq_haz_level = 0.0 # Convert levels into categories alert_info = get_alert(ls_haz_level, lq_haz_level, ls_pop_level, lq_pop_level) # Unpack info (I think we are now assuming that the statements will be # constructed on the website and so we don't need them here) ls_haz_alert, ls_pop_alert, lq_haz_alert, lq_pop_alert, \ ls_alert, lq_alert = alert_info if lsmodels is None: lsmodels = [{ 'id': 'nowicki_jessee_2017', 'title': 'Nowicki Jessee and others (2017)', 'overlay': 'jessee_2017.png', 'extent': jessee_extent, 'units': "Proportion of area affected", 'preferred': True, 'alert': ls_alert, 'hazard_alert': { 'color': ls_haz_alert, 'value': set_num_precision(ls_haz_level, 2, 'float'), 'parameter': 'Aggregate Hazard', 'units': 'km^2' }, 'population_alert': { 'color': ls_pop_alert, 'value': set_num_precision(ls_pop_level, 2, 'int'), 'parameter': 'Population exposure', 'units': 'people' }, 'probability': { 'max': float("%.2f" % ls_stats['Max']), 'std': float("%.2f" % ls_stats['Std']), 'hagg0.1g': float("%.2f" % ls_stats['hagg_0.10g']), 'popexp0.1g': float("%.2f" % ls_stats['exp_pop_0.10g']) } }] if lqmodels is None: lqmodels = [{ 'id': 'zhu_2017', 'title': 'Zhu and others (2017)', 'overlay': 'zhu_2017.png', 'extent': zhu_extent, 'units': "Proportion of area affected", 'preferred': True, 'alert': lq_alert, 'hazard_alert': { 'color': lq_haz_alert, 'value': set_num_precision(lq_haz_level, 2, 'float'), 'parameter': 'Aggregate Hazard', 'units': 'km^2' }, 'population_alert': { 'color': lq_pop_alert, 'value': set_num_precision(lq_pop_level, 2, 'int'), 'parameter': 'Population exposure', 'units': 'people' }, 'probability': { 'max': float("%.2f" % lq_stats['Max']), 'std': float("%.2f" % lq_stats['Std']), 'hagg0.1g': float("%.2f" % ls_stats['hagg_0.10g']), 'popexp0.1g': float("%.2f" % ls_stats['exp_pop_0.10g']) } }] else: # Get all info from dictionaries of preferred events, add in extent # and filename for lsm in lsmodels: # Add extent and filename for preferred model if lsm['preferred']: filesnippet = lsm['id'] # Read in extents flnm = '%s_extent.json' % filesnippet ls_extent_file = [f for f in files if flnm in f] if len(ls_extent_file) == 1: ls_file = os.path.join(event_dir, ls_extent_file[0]) with open(ls_file) as f: ls_extent = json.load(f) else: raise OSError("Landslide extent not found.") lsm['extent'] = ls_extent # lsm['filename'] = flnm lsext = lsm['zoomext'] # Get zoom extent ls_alert = lsm['alert'] rmkeys = ['bin_edges', 'bin_colors', 'zoomext'] else: # Remove any alert keys rmkeys = [ 'bin_edges', 'bin_colors', 'zoomext', 'population_alert', 'alert', 'hazard_alert' ] for key in rmkeys: if key in lsm: lsm.pop(key) for lqm in lqmodels: if lqm['preferred']: filesnippet = lqm['id'] # Read in extents flnm = '%s_extent.json' % filesnippet lq_extent_file = [f2 for f2 in files if flnm in f2] if len(lq_extent_file) == 1: lq_file = os.path.join(event_dir, lq_extent_file[0]) with open(lq_file) as f: lq_extent = json.load(f) else: raise OSError("Liquefaction extent not found.") lqm['extent'] = lq_extent # lqm['filename'] = flnm lqext = lqm['zoomext'] # Get zoom extent lq_alert = lqm['alert'] rmkeys = ['bin_edges', 'bin_colors', 'zoomext'] else: # Remove any alert keys rmkeys = [ 'bin_edges', 'bin_colors', 'zoomext', 'population_alert', 'alert', 'hazard_alert' ] for key in rmkeys: if key in lqm: lqm.pop(key) # Try to get event info shake_grid = ShakeGrid.load(shakefile, adjust='res') event_dict = shake_grid.getEventDict() sm_dict = shake_grid.getShakeDict() base_url = 'https://earthquake.usgs.gov/earthquakes/eventpage/' # Is this a point source? # point = is_grid_point_source(shake_grid) # Temporarily hard code this until we can get a better solution via # new grid.xml attributes. #point = True net = eventsource code = eventsourcecode time = event_dict['event_timestamp'].strftime('%Y-%m-%dT%H:%M:%SZ') event_url = '%s%s%s#executive' % (base_url, net, code) # Get extents that work for both unless one is green and the other isn't if lq_alert == 'green' and ls_alert != 'green' and ls_alert is not None: xmin = lsext['xmin'] xmax = lsext['xmax'] ymin = lsext['ymin'] ymax = lsext['ymax'] elif lq_alert != 'green' and ls_alert == 'green' and lq_alert is not None: xmin = lqext['xmin'] xmax = lqext['xmax'] ymin = lqext['ymin'] ymax = lqext['ymax'] else: xmin = np.min((lqext['xmin'], lsext['xmin'])) xmax = np.max((lqext['xmax'], lsext['xmax'])) ymin = np.min((lqext['ymin'], lsext['ymin'])) ymax = np.max((lqext['ymax'], lsext['ymax'])) # Should we display the warning about point source? rupture_warning = False if point and event_dict['magnitude'] > 6.5: rupture_warning = True # Create info.json for website rendering and metadata purposes info_dict = { 'Summary': { 'code': code, 'net': net, 'magnitude': event_dict['magnitude'], 'depth': event_dict['depth'], 'time': time, 'lat': event_dict['lat'], 'lon': event_dict['lon'], 'event_url': event_url, 'shakemap_version': sm_dict['shakemap_version'], 'rupture_warning': rupture_warning, 'point_source': point, 'zoom_extent': [xmin, xmax, ymin, ymax] }, 'Landslides': lsmodels, 'Liquefaction': lqmodels } info_file = os.path.join(event_dir, 'info.json') with open(info_file, 'w') as f: json.dump(info_dict, f) # allow_nan=False) filenames.append(info_file) return filenames
def create_png(event_dir, lsmodels=None, lqmodels=None, mercator=True, lsmask=0.002, lqmask=0.005, legends=False): """ Creates transparent PNG file for website. Args: event_dir (srt): Directory containing ground failure results. lsmodels (list): List of dictionaries of model summary info compiled by the hazdev function. If not specified, code will search for the hdf5 files for the preferred model and will create this dictionary and will apply default colorbars and bins. lqmodels (list): Same as above for liquefaction. mercator (bool): Project raster to web mercator lsmask (float): Mask all landslide cells with probabilities below this threshold lqmask (float): Mask all liquefaction cells with probabilities below this threshold legends (bool): if True, will produce png files of legends for each preferred model Returns: .png map overlays and .json files specifying their mapped extents """ filenames = [] files = os.listdir(event_dir) if lsmodels is None: # Read in the "preferred" model for landslides ls_mod_file = [f for f in files if 'jessee_2017.hdf5' in f] if len(ls_mod_file) == 1: ls_file = os.path.join(event_dir, ls_mod_file[0]) ls_mod = loadlayers(ls_file) levels = DFBINS colors1 = DFCOLORS filesnippet = 'jessee_2017' ls_grid = ls_mod['model']['grid'] ls_data = ls_grid.getData() if lsmask: ls_data[ls_data < lsmask] = float('nan') ls_geodict = ls_grid.getGeoDict() ls_extent = [ ls_geodict.xmin - 0.5*ls_geodict.dx, ls_geodict.xmax + 0.5*ls_geodict.dx, ls_geodict.ymin - 0.5*ls_geodict.dy, ls_geodict.ymax + 0.5*ls_geodict.dy, ] filen = os.path.join(event_dir, '%s_extent.json' % filesnippet) filenames.append(filen) with open(filen, 'w') as f: json.dump(ls_extent, f) lmin = levels[0] lmax = levels[-1] ls_data2 = np.clip(ls_data, lmin, lmax) cmap = mpl.colors.ListedColormap(colors1) norm = mpl.colors.BoundaryNorm(levels, cmap.N) ls_data2 = np.ma.array(ls_data2, mask=np.isnan(ls_data)) #scalarMap = cm.ScalarMappable(norm=norm, cmap=cmap) #rgba_img = scalarMap.to_rgba_array(ls_data2, alpha=alpha) rgba_img = cmap(norm(ls_data2)) if mercator: rgba_img = mercator_transform( rgba_img, (ls_extent[2], ls_extent[3]), origin='upper') filen = os.path.join(event_dir, '%s.png' % filesnippet) plt.imsave(filen, rgba_img, vmin=lmin, vmax=lmax, cmap=cmap ) else: raise OSError( "Preferred landslide model result (%s) not found." % ls_mod_file) else: for lsm in lsmodels: #if lsm['preferred']: levels = lsm['bin_edges'] colors1 = lsm['bin_colors'] filesnippet = lsm['id'] fsh = '%s.hdf5' % filesnippet ls_mod_file = [f for f in files if fsh in f] if len(ls_mod_file) == 1: ls_file = os.path.join(event_dir, ls_mod_file[0]) ls_mod = loadlayers(ls_file) else: raise OSError( "Specified landslide model result (%s) not found." % fsh) ls_grid = ls_mod['model']['grid'] ls_data = ls_grid.getData() if lsmask: ls_data[ls_data < lsmask] = float('nan') ls_geodict = ls_grid.getGeoDict() ls_extent = [ ls_geodict.xmin - 0.5*ls_geodict.dx, ls_geodict.xmax + 0.5*ls_geodict.dx, ls_geodict.ymin - 0.5*ls_geodict.dy, ls_geodict.ymax + 0.5*ls_geodict.dy, ] filen = os.path.join(event_dir, '%s_extent.json' % filesnippet) filenames.append(filen) with open(filen, 'w') as f: json.dump(ls_extent, f) lmin = levels[0] lmax = levels[-1] ls_data2 = np.clip(ls_data, lmin, lmax) cmap = mpl.colors.ListedColormap(colors1) norm = mpl.colors.BoundaryNorm(levels, cmap.N) ls_data2 = np.ma.array(ls_data2, mask=np.isnan(ls_data)) #scalarMap = cm.ScalarMappable(norm=norm, cmap=cmap) #rgba_img = scalarMap.to_rgba_array(ls_data2, alpha=alpha) rgba_img = cmap(norm(ls_data2)) if mercator: rgba_img = mercator_transform( rgba_img, (ls_extent[2], ls_extent[3]), origin='upper') filen = os.path.join(event_dir, '%s.png' % filesnippet) plt.imsave(filen, rgba_img, vmin=lmin, vmax=lmax, cmap=cmap ) if lqmodels is None: # read in preferred model for liquefaction if none specified lq_mod_file = [f2 for f2 in files if 'zhu_2017_general.hdf5' in f2] if len(lq_mod_file) == 1: lq_file = os.path.join(event_dir, lq_mod_file[0]) lq_mod = loadlayers(lq_file) levels = DFBINS colors1 = DFCOLORS filesnippet = 'zhu_2017_general' lq_grid = lq_mod['model']['grid'] lq_data = lq_grid.getData() if lqmask: lq_data[lq_data < lqmask] = float('nan') lq_geodict = lq_grid.getGeoDict() lq_extent = [ lq_geodict.xmin - 0.5*lq_geodict.dx, lq_geodict.xmax + 0.5*lq_geodict.dx, lq_geodict.ymin - 0.5*lq_geodict.dy, lq_geodict.ymax + 0.5*lq_geodict.dy, ] filen = os.path.join(event_dir, '%s_extent.json' % filesnippet) filenames.append(filen) with open(filen, 'w') as f: json.dump(lq_extent, f) lmin = levels[0] lmax = levels[-1] lq_data2 = np.clip(lq_data, lmin, lmax) cmap = mpl.colors.ListedColormap(colors1) norm = mpl.colors.BoundaryNorm(levels, cmap.N) lq_data2 = np.ma.array(lq_data2, mask=np.isnan(lq_data)) rgba_img = cmap(norm(lq_data2)) if mercator: rgba_img = mercator_transform( rgba_img, (lq_extent[2], lq_extent[3]), origin='upper') filen = os.path.join(event_dir, '%s.png' % filesnippet) plt.imsave(filen, rgba_img, vmin=lmin, vmax=lmax, cmap=cmap ) filenames.append(filen) else: raise OSError( "Preferred liquefaction model result (%s) not found." % lq_mod_file) else: for lqm in lqmodels: if lqm['preferred']: levels = lqm['bin_edges'] colors1 = lqm['bin_colors'] filesnippet = lqm['id'] fsh = '%s.hdf5' % filesnippet lq_mod_file = [f2 for f2 in files if fsh in f2] if len(lq_mod_file) == 1: lq_file = os.path.join(event_dir, lq_mod_file[0]) lq_mod = loadlayers(lq_file) else: raise OSError( "Specified liquefaction model result (%s) not found." % fsh) lq_grid = lq_mod['model']['grid'] lq_data = lq_grid.getData() if lqmask: lq_data[lq_data < lqmask] = float('nan') lq_geodict = lq_grid.getGeoDict() lq_extent = [ lq_geodict.xmin - 0.5*lq_geodict.dx, lq_geodict.xmax + 0.5*lq_geodict.dx, lq_geodict.ymin - 0.5*lq_geodict.dy, lq_geodict.ymax + 0.5*lq_geodict.dy, ] filen = os.path.join(event_dir, '%s_extent.json' % filesnippet) filenames.append(filen) with open(filen, 'w') as f: json.dump(lq_extent, f) lmin = levels[0] lmax = levels[-1] lq_data2 = np.clip(lq_data, lmin, lmax) cmap = mpl.colors.ListedColormap(colors1) norm = mpl.colors.BoundaryNorm(levels, cmap.N) lq_data2 = np.ma.array(lq_data2, mask=np.isnan(lq_data)) rgba_img = cmap(norm(lq_data2)) if mercator: rgba_img = mercator_transform( rgba_img, (lq_extent[2], lq_extent[3]), origin='upper') filen = os.path.join(event_dir, '%s.png' % filesnippet) plt.imsave(filen, rgba_img, vmin=lmin, vmax=lmax, cmap=cmap ) filenames.append(filen) if legends: lsname, lqname = make_legend(lqmin=lqmask, lsmin=lsmask, outfolder=event_dir) filenames.append(lsname) filenames.append(lqname) return filenames