Update src/seismic_hazard_forecasting.py
This commit is contained in:
		@@ -48,7 +48,7 @@ def main(catalog_file, mc_file, pdf_file, m_file, m_select, mag_label, mc, m_max
 | 
				
			|||||||
    import logging
 | 
					    import logging
 | 
				
			||||||
    from base_logger import getDefaultLogger
 | 
					    from base_logger import getDefaultLogger
 | 
				
			||||||
    from timeit import default_timer as timer
 | 
					    from timeit import default_timer as timer
 | 
				
			||||||
    from math import ceil, floor
 | 
					    from math import ceil, floor, isnan
 | 
				
			||||||
    import numpy as np
 | 
					    import numpy as np
 | 
				
			||||||
    import scipy
 | 
					    import scipy
 | 
				
			||||||
    import obspy
 | 
					    import obspy
 | 
				
			||||||
@@ -439,8 +439,11 @@ verbose: {verbose}")
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        PGA = np.zeros(shape=(nx * ny))
 | 
					        PGA = np.zeros(shape=(nx * ny))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        # use dask parallel computing
 | 
					 | 
				
			||||||
        start = timer()
 | 
					        start = timer()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        use_pp = False
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if use_pp:         # use dask parallel computing
 | 
				
			||||||
            pbar = ProgressBar()
 | 
					            pbar = ProgressBar()
 | 
				
			||||||
            pbar.register()
 | 
					            pbar.register()
 | 
				
			||||||
            # iter = range(0,len(distances))
 | 
					            # iter = range(0,len(distances))
 | 
				
			||||||
@@ -451,10 +454,24 @@ verbose: {verbose}")
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
                imls = [dask.delayed(compute_IMT_exceedance)(rx_lat[i], rx_lon[i], distances[i].flatten(), fr, p, lambdas,
 | 
					                imls = [dask.delayed(compute_IMT_exceedance)(rx_lat[i], rx_lon[i], distances[i].flatten(), fr, p, lambdas,
 | 
				
			||||||
                                                            forecast_len, lambdas_perc, m_range, m_pdf, m_cdf, model,
 | 
					                                                            forecast_len, lambdas_perc, m_range, m_pdf, m_cdf, model,
 | 
				
			||||||
                                                         log_level=logging.DEBUG, imt=imt, IMT_min=0.0, IMT_max=2.0,
 | 
					                                                            log_level=logging.DEBUG, imt=imt, IMT_min=0.0, IMT_max=2.0, rx_label=i,
 | 
				
			||||||
                                                         rx_label=i) for i in iter]
 | 
					                                                            rtol=0.1, use_cython=False) for i in iter]
 | 
				
			||||||
                iml = dask.compute(*imls)
 | 
					                iml = dask.compute(*imls)
 | 
				
			||||||
                iml_grid_raw.append(list(iml))
 | 
					                iml_grid_raw.append(list(iml))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        else:
 | 
				
			||||||
 | 
					            iml_grid_raw = []
 | 
				
			||||||
 | 
					            iter = indices
 | 
				
			||||||
 | 
					            for imt in products:
 | 
				
			||||||
 | 
					                iml = []
 | 
				
			||||||
 | 
					                for i in iter:
 | 
				
			||||||
 | 
					                    iml_i = compute_IMT_exceedance(rx_lat[i], rx_lon[i], distances[i].flatten(), fr, p, lambdas, forecast_len, 
 | 
				
			||||||
 | 
					                                                    lambdas_perc, m_range, m_pdf, m_cdf, model, imt=imt, IMT_min = 0.0,
 | 
				
			||||||
 | 
					                                                    IMT_max = 2.0, rx_label = i, rtol = 0.1, use_cython=False)
 | 
				
			||||||
 | 
					                    iml.append(iml_i)
 | 
				
			||||||
 | 
					                    logger.info(f"Estimated {imt} at rx {i} is {iml_i}")
 | 
				
			||||||
 | 
					                iml_grid_raw.append(iml)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        end = timer()
 | 
					        end = timer()
 | 
				
			||||||
        logger.info(f"Ground motion exceedance computation time: {round(end - start, 1)} seconds")
 | 
					        logger.info(f"Ground motion exceedance computation time: {round(end - start, 1)} seconds")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -486,6 +503,12 @@ verbose: {verbose}")
 | 
				
			|||||||
                                 mode='reflect', anti_aliasing=False)
 | 
					                                 mode='reflect', anti_aliasing=False)
 | 
				
			||||||
            iml_grid_hd[iml_grid_hd == 0.0] = np.nan  # change zeroes back to nan
 | 
					            iml_grid_hd[iml_grid_hd == 0.0] = np.nan  # change zeroes back to nan
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            # trim edges so the grid is not so blocky
 | 
				
			||||||
 | 
					            vmin_hd = min(x for x in iml_grid_hd.flatten() if not isnan(x))
 | 
				
			||||||
 | 
					            vmax_hd = max(x for x in iml_grid_hd.flatten() if not isnan(x))
 | 
				
			||||||
 | 
					            trim_thresh = vmin
 | 
				
			||||||
 | 
					            iml_grid_hd[iml_grid_hd < trim_thresh] = np.nan
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            # generate image overlay
 | 
					            # generate image overlay
 | 
				
			||||||
            north, south = lat.max(), lat.min()  # Latitude range
 | 
					            north, south = lat.max(), lat.min()  # Latitude range
 | 
				
			||||||
            east, west = lon.max(), lon.min()  # Longitude range
 | 
					            east, west = lon.max(), lon.min()  # Longitude range
 | 
				
			||||||
@@ -494,8 +517,10 @@ verbose: {verbose}")
 | 
				
			|||||||
            map_center = [np.mean([north, south]), np.mean([east, west])]
 | 
					            map_center = [np.mean([north, south]), np.mean([east, west])]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            # Create an image from the grid
 | 
					            # Create an image from the grid
 | 
				
			||||||
 | 
					            cmap_name = 'viridis'
 | 
				
			||||||
 | 
					            cmap = plt.get_cmap(cmap_name)
 | 
				
			||||||
            fig, ax = plt.subplots(figsize=(6, 6))
 | 
					            fig, ax = plt.subplots(figsize=(6, 6))
 | 
				
			||||||
            ax.imshow(iml_grid_hd, origin='lower', cmap='viridis')
 | 
					            ax.imshow(iml_grid_hd, origin='lower', cmap=cmap, vmin=vmin, vmax=vmax)
 | 
				
			||||||
            ax.axis('off')
 | 
					            ax.axis('off')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            # Save the figure
 | 
					            # Save the figure
 | 
				
			||||||
@@ -513,7 +538,6 @@ verbose: {verbose}")
 | 
				
			|||||||
            logger.info(f"Saved geographic bounds to SVG metadata (data-map-bounds): {overlay_filename} → {map_bounds}")
 | 
					            logger.info(f"Saved geographic bounds to SVG metadata (data-map-bounds): {overlay_filename} → {map_bounds}")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            # Make the color bar
 | 
					            # Make the color bar
 | 
				
			||||||
            cmap_name = 'viridis'
 | 
					 | 
				
			||||||
            width = 50
 | 
					            width = 50
 | 
				
			||||||
            height = 500
 | 
					            height = 500
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -523,14 +547,14 @@ verbose: {verbose}")
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
            fig, ax = plt.subplots(figsize=((width + 40) / 100.0, (height + 20) / 100.0),
 | 
					            fig, ax = plt.subplots(figsize=((width + 40) / 100.0, (height + 20) / 100.0),
 | 
				
			||||||
                                   dpi=100)  # Increase fig size for labels
 | 
					                                   dpi=100)  # Increase fig size for labels
 | 
				
			||||||
            ax.imshow(gradient, aspect='auto', cmap=plt.get_cmap(cmap_name),
 | 
					            ax.imshow(gradient, aspect='auto', cmap=cmap.reversed(),
 | 
				
			||||||
                      extent=[0, 1, vmin, vmax])  # Note: extent order is different for vertical
 | 
					                      extent=[0, 1, vmin, vmax_hd])  # Note: extent order is different for vertical
 | 
				
			||||||
            ax.set_xticks([])  # Remove x-ticks for vertical colorbar
 | 
					            ax.set_xticks([])  # Remove x-ticks for vertical colorbar
 | 
				
			||||||
            num_ticks = 11  # Show more ticks
 | 
					            num_ticks = 11  # Show more ticks
 | 
				
			||||||
            tick_positions = np.linspace(vmin, vmax, num_ticks)
 | 
					            tick_positions = np.linspace(vmin, vmax_hd, num_ticks)
 | 
				
			||||||
            ax.set_yticks(tick_positions)
 | 
					            ax.set_yticks(tick_positions)
 | 
				
			||||||
            ax.set_yticklabels([f"{tick:.2f}" for tick in tick_positions])  # format tick labels
 | 
					            ax.set_yticklabels([f"{tick:.2f}" for tick in tick_positions])  # format tick labels
 | 
				
			||||||
            ax.set_title(imt, pad=15)
 | 
					            ax.set_title(products[j], pad=15)
 | 
				
			||||||
            fig.subplots_adjust(left=0.25, right=0.75, bottom=0.05, top=0.95)  # Adjust Layout
 | 
					            fig.subplots_adjust(left=0.25, right=0.75, bottom=0.05, top=0.95)  # Adjust Layout
 | 
				
			||||||
            fig.savefig("colorbar_" + str(j) + ".svg", bbox_inches='tight')
 | 
					            fig.savefig("colorbar_" + str(j) + ".svg", bbox_inches='tight')
 | 
				
			||||||
            plt.close(fig)
 | 
					            plt.close(fig)
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user