def io_beaching_animations(): ps = [0.05, 0.5, 0.95] dx = 8 start_time = datetime(1995, 1, 1, 12, 0) end_time = datetime(2005, 1, 1, 12, 0) for p in ps: output_name = f'beaching_8km_p{str(p).replace(".","")}' input_path_nh = get_beached_particles_path('io_nh', dx, p) input_path_sh = get_beached_particles_path('io_sh', dx, p) particles_nh = BeachingParticles.read_from_netcdf( input_path_nh, time_start=start_time, time_end=end_time) particles_sh = BeachingParticles.read_from_netcdf( input_path_sh, time_start=start_time, time_end=end_time) time = particles_nh.time lon = np.concatenate((particles_nh.lon, particles_sh.lon), axis=0) lat = np.concatenate((particles_nh.lat, particles_sh.lat), axis=0) beached = np.concatenate((particles_nh.beached, particles_sh.beached), axis=0) create_animation_beached(time, lon, lat, beached, p, output_name=output_name, plot_legend=True) del (particles_nh, particles_sh, time, lon, lat, beached)
def get_from_particles(particles: BeachingParticles): log.info(None, 'Creating connectivity matrix from particles...') cgrid = get_cgrid_with_halo_and_extended_islands() lon_start, lat_start = particles.get_initial_particle_lon_lat() lon_end, lat_end = particles.get_final_particle_lon_lat() _, start_countries = cgrid.get_country_code_and_name( lon_start, lat_start) _, end_countries = cgrid.get_country_code_and_name(lon_end, lat_end) sorted_start_countries = sort_most_common_countries(start_countries) sorted_end_countries = sort_most_common_countries(end_countries) # create blame matrix blame_matrix = np.zeros( (len(sorted_end_countries), len(sorted_start_countries))) for p in range(len(particles.pid)): if end_countries[p] == '': continue if start_countries[p] == '': warnings.warn( f'Start position of particle not in a country: lon = {lon_start[p]}, lat = {lat_start[p]}' ) continue i = sorted_end_countries.index(end_countries[p]) j = sorted_start_countries.index(start_countries[p]) blame_matrix[i, j] += 1 # row normalize blame matrix [percentage] blame_matrix_normalized = np.copy(blame_matrix) for i in range(blame_matrix_normalized.shape[0]): blame_matrix_normalized[i, :] = blame_matrix_normalized[ i, :] / sum(blame_matrix_normalized[i, :]) * 100 return ConnectivityMatrix(sorted_start_countries, sorted_end_countries, blame_matrix_normalized)
def christmas_island_animation(): particles = BeachingParticles.read_from_netcdf( get_dir('christmas_island_input')) lon_range = [100, 115] lat_range = [-12, -5] output_name = 'plastic_waste_christmas_island' logo_extent = (lon_range[1] - 2, lon_range[1] - 0.1, lat_range[0] + 0.2, lat_range[0] + 2) create_animation_beached(particles.time, particles.lon, particles.lat, None, None, output_name=output_name, lon_range=lon_range, lat_range=lat_range, text='Christmas Island', text_lon=105.6, text_lat=-11., dpi=300, fps=2, title_style='month', add_uwa_logo=True, logo_extent=logo_extent)
def process_specific_file_particles_in_lon_lat_range(input_path, output_path, lon_range=None, lat_range=None, t_interval=1): if type(input_path) == list: particles = BeachingParticles.read_from_parcels_netcdf( input_path[0], t_interval=t_interval) for i in range(len(input_path) - 1): particles.add_particles_from_parcels_netcdf(input_path[i + 1]) elif type(input_path) == str: particles = BeachingParticles.read_from_parcels_netcdf( input_path, t_interval=t_interval) if lon_range is not None and lat_range is not None: particles_in_range = particles.get_particles_from_initial_lon_lat_range( lon_range, lat_range) _write_to_netcdf(particles_in_range, output_path) else: _write_to_netcdf(particles, output_path)
def process_particles(input_description='io_river_sources', basin_name='io_nh', years=np.arange(1995, 2016, 1), output_description=None): if years is not None: input_path = _get_parcels_particles_path(input_description, year=years[0]) particles = BeachingParticles.read_from_parcels_netcdf(input_path) for year in years[1:]: input_path = _get_parcels_particles_path(input_description, year=year) particles.add_particles_from_parcels_netcdf(input_path) else: input_path = _get_parcels_particles_path(input_description) particles = BeachingParticles.read_from_parcels_netcdf(input_path) log.info(None, f'Extracting particles from initial basin only: {basin_name}') basin_particles = particles.get_particles_from_initial_basin(basin_name) # write to netcdf output_path = get_particles_path(basin_name, extra_description=output_description) _write_to_netcdf(basin_particles, output_path) return basin_particles
def postprocess_beached(dx, p, basin_name='io_nh', extra_description=None): log.info( None, f'--- Postprocessing results for {basin_name} beaching particles at: dx = {dx}, p = {p} ---' ) input_path_particles = get_beached_particles_path( basin_name, dx, p, extra_description=extra_description) input_path_density = get_beached_density_path( basin_name, dx, p, extra_description=extra_description) # output paths output_io_dev = get_output_path(basin_name, dx, p, 'dev', 'nc', extra_description=extra_description) output_particles_per_country = get_output_path( basin_name, dx, p, 'ppcountry', 'csv', extra_description=extra_description) output_connectivity_matrix = get_output_path( basin_name, dx, p, 'connectivity', 'csv', extra_description=extra_description) # load data particles = BeachingParticles.read_from_netcdf(input_path_particles) density = Density.read_from_netcdf(input_path_density, t_index=-1) # particles per country ppcountry = ParticlesPerCountry.get_from_density(density) ppcountry.write_to_csv(output_particles_per_country) # connectivity matrix cmatrix = ConnectivityMatrix.get_from_particles(particles) cmatrix.write_to_csv(output_connectivity_matrix) # IO particle development io_dev = IoParticleDevelopmentTimeseries.create_from_particles(particles) io_dev.write_to_netcdf(output_io_dev)
def xpresspearl_animation(): particles = BeachingParticles.read_from_netcdf( get_dir('xpresspearl_output') + 'particles_2008-2009.nc') output_name = 'xpresspearl' lon_range = [40, 120] lat_range = [-20, 40] _, lon0, lat0 = get_xpresspearl_release_time_lon_lat() create_animation_beached(particles.time, particles.lon, particles.lat, None, None, lon_range=lon_range, lat_range=lat_range, output_name=output_name, point=True, point_lon=lon0, point_lat=lat0, dpi=300, fps=5, title_style='time_passed_simple')
def cocos_keeling_islands_animation(): particles = BeachingParticles.read_from_netcdf(get_dir('iot_input_2008')) lon_range = [90., 128.] lat_range = [-20., 6.] output_name = 'plastic_waste_cki_2008' create_animation_beached(particles.time, particles.lon, particles.lat, None, None, output_name=output_name, lon_range=lon_range, lat_range=lat_range, text='Cocos Keeling\nIslands', text_lon=97., text_lat=-13., point=True, point_lon=96.86, point_lat=-12.14, dpi=150, fps=4, title_style='month')
def figure3_release_arrival_histograms( input_path=get_dir('iot_input'), output_path=None, plot_style='plot_tools/plot.mplstyle', river_names=['Serayu', 'Progo', 'Tanduy', 'Wulan', 'Bogowonto'], river_lons=[109.1125, 110.2125, 108.7958333, 108.1458333, 110.0291667], river_lats=[ -7.679166667, -7.979166667, -7.670833333, -7.779166667, -7.895833333 ], river_styles=['-', '-.', ':', '--', '-'], river_colors=['k', 'k', 'k', 'k', '#bfbfbf']): particles = BeachingParticles.read_from_netcdf(input_path) cki_n_release, _, cki_n_entry = get_n_particles_per_month_release_arrival( particles, 'cki') ci_n_release, _, ci_n_entry = get_n_particles_per_month_release_arrival( particles, 'christmas') all_sources = RiverSources.read_from_shapefile() river_waste = [] for i in range(len(river_names)): i_river = np.where( np.logical_and(all_sources.lon == river_lons[i], all_sources.lat == river_lats[i])) river_waste.append(np.squeeze(all_sources.waste[i_river])) plt.style.use(plot_style) fig = plt.figure(figsize=(5, 4)) plt.rcParams['font.size'] = 5 plt.rcParams['axes.labelsize'] = 5 ax1 = plt.subplot(2, 2, (1, 2)) for i in range(len(river_names)): ax1.plot(all_sources.time, river_waste[i], label=river_names[i], color=river_colors[i], linestyle=river_styles[i]) ax1.set_xticks(all_sources.time) ax1.set_xticklabels([ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]) ax1.set_xlim(1, 12) ax1.set_yticks(np.arange(0, 3000, 500)) ax1.set_ylim(0, 3000) ax1.set_ylabel('[# particles]') ax1.legend(loc='upper right', bbox_to_anchor=(1.18, 1.0)) anchored_text1 = AnchoredText( f'(a) Seasonal input of plastic waste from {int(len(river_names))} main polluting rivers', loc='upper left', borderpad=0.0) ax1.add_artist(anchored_text1) ax2 = plt.subplot(2, 2, 3) _histogram_release_arrival(ax2, cki_n_release, cki_n_entry) anchored_text2 = AnchoredText( f'(b) Seasonality of particles reaching Cocos Keeling Islands', loc='upper left', borderpad=0.0) ax2.add_artist(anchored_text2) # legend legend_elements = [ Patch(facecolor='w', edgecolor='k', hatch='//////', label='Release'), Patch(facecolor='w', edgecolor='k', label='Arrival') ] ax2.legend(handles=legend_elements, loc='upper left', bbox_to_anchor=(0.0, 0.91)) ax3 = plt.subplot(2, 2, 4) _histogram_release_arrival(ax3, ci_n_release, ci_n_entry) ax3.yaxis.set_label_position('right') ax3.yaxis.tick_right() anchored_text3 = AnchoredText( f'(c) Seasonality of particles reaching Christmas Island', loc='upper left', borderpad=0.0) ax3.add_artist(anchored_text3) if output_path: plt.savefig(output_path, bbox_inches='tight', dpi=300) plt.show()
def figure2_main_sources(input_path=get_dir('iot_input'), river_names_cki=[], river_names_ci=[], ylim_cki=[0, 45], ylim_ci=[0, 45], output_path=None, plot_style='plot_tools/plot.mplstyle'): particles = BeachingParticles.read_from_netcdf(input_path) box_lon_cki, box_lat_cki = get_cki_box_lon_lat_range() box_lon_ci, box_lat_ci = get_christmas_box_lon_lat_range() l_box_cki = get_l_particles_in_box(particles, 'cki') l_box_ci = get_l_particles_in_box(particles, 'christmas') lon_main_cki, lat_main_cki, waste_main_cki = get_main_sources_lon_lat_n_particles( particles, 'cki') lon_main_ci, lat_main_ci, waste_main_ci = get_main_sources_lon_lat_n_particles( particles, 'christmas') (main_colors_cki, main_sizes_cki, main_edgewidths_cki ) = _get_marker_colors_sizes_edgewidths_for_main_sources(waste_main_cki) (main_colors_ci, main_sizes_ci, main_edgewidths_ci ) = _get_marker_colors_sizes_edgewidths_for_main_sources(waste_main_ci) x_cki, waste_big_cki, colors_big_cki = _get_sorted_river_contribution_info( lon_main_cki, lat_main_cki, waste_main_cki, main_colors_cki, river_names_cki, 'CKI') x_ci, waste_big_ci, colors_big_ci = _get_sorted_river_contribution_info( lon_main_ci, lat_main_ci, waste_main_ci, main_colors_ci, river_names_ci, 'CI') plt.style.use(plot_style) fig = plt.figure(figsize=(6, 4)) plt.rcParams['font.size'] = 5 plt.rcParams['axes.labelsize'] = 5 plt.subplots_adjust(wspace=0.0) plt.subplots_adjust(hspace=0.35) land_color = '#cfcfcf' in_box_color = '#2e4999' # (a) main sources CKI ax1 = plt.subplot(2, 3, (1, 2), projection=ccrs.PlateCarree()) mplot1 = _iot_basic_map(ax1) plt.rcParams['font.size'] = 5 mplot1.ax.set_xticklabels([]) mplot1.tracks(particles.lon[l_box_cki, :], particles.lat[l_box_cki, :], color=in_box_color, linewidth=0.2) mplot1.box(box_lon_cki, box_lat_cki, linewidth=0.8, color='w') mplot1.box(box_lon_cki, box_lat_cki, linewidth=0.5) ax1.add_feature(cftr.LAND, facecolor=land_color, edgecolor='k', zorder=5) mplot1.points(lon_main_cki, lat_main_cki, marker='o', facecolor=main_colors_cki, markersize=np.array(main_sizes_cki) * 5, edgewidth=main_edgewidths_cki) mplot1.add_subtitle( f'(a) Source locations and tracks of particles reaching\n Cocos Keeling Islands (CKI)' ) ax1.set_anchor('W') # (c) main sources CI ax2 = plt.subplot(2, 3, (4, 5), projection=ccrs.PlateCarree()) mplot2 = _iot_basic_map(ax2) plt.rcParams['font.size'] = 5 mplot2.tracks(particles.lon[l_box_ci, :], particles.lat[l_box_ci, :], color=in_box_color, linewidth=0.2) mplot2.box(box_lon_ci, box_lat_ci, linewidth=0.8, color='w') mplot2.box(box_lon_ci, box_lat_ci, linewidth=0.5) ax2.add_feature(cftr.LAND, facecolor=land_color, edgecolor='k', zorder=5) mplot2.points(lon_main_ci, lat_main_ci, marker='o', facecolor=main_colors_ci, markersize=np.array(main_sizes_ci) * 5, edgewidth=main_edgewidths_ci) mplot2.add_subtitle( f'(c) Source locations and tracks of particles reaching\n Christmas Island (CI)' ) # sources legend legend_entries = _get_legend_entries_for_main_sources() ax2.set_anchor('W') ax2.legend(handles=legend_entries, title='[# particles]', loc='upper right', bbox_to_anchor=(1.3, 1.0)) # (b) river contributions CKI ax3 = plt.subplot(2, 3, 3) ax3.bar(x_cki, waste_big_cki, color=colors_big_cki, zorder=5) ax3.set_ylabel('[% particles arriving]') ax3.set_ylim(ylim_cki) yticks = np.arange(0, ylim_cki[1], 5) yticks[0] = ylim_cki[0] ax3.set_yticks(yticks) xticks = np.arange(0, len(river_names_cki)) ax3.set_xticks(xticks) ax3.set_xticklabels(river_names_cki, rotation='vertical') ax3.grid(False, axis='x') anchored_text1 = AnchoredText( f'(b) Contributions of rivers to particles\n reaching the CKI', loc='upper left', borderpad=0.0) ax3.add_artist(anchored_text1) ax3.set_anchor('W') # (d) river contributions CI ax4 = plt.subplot(2, 3, 6) ax4.bar(x_ci, waste_big_ci, color=colors_big_ci, zorder=5) ax4.set_ylim(ylim_ci) yticks2 = np.arange(0, ylim_ci[1], 5) yticks2[0] = ylim_ci[0] ax4.set_yticks(yticks2) ax4.set_ylabel('[% particles arriving]') xticks = np.arange(0, len(river_names_ci)) ax4.set_xticks(xticks) ax4.set_xticklabels(river_names_ci, rotation='vertical') ax4.grid(False, axis='x') anchored_text2 = AnchoredText( f'(d) Contributions of rivers to particles\n reaching CI', loc='upper left', borderpad=0.0) ax4.add_artist(anchored_text2) ax4.set_anchor('W') if output_path: plt.savefig(output_path, bbox_inches='tight', dpi=300) plt.show()
def process_specific_file_density(input_path, output_path, dx_grid=0.5): particles = BeachingParticles.read_from_netcdf(input_path) grid = get_global_grid(dx=dx_grid) density = Density.create_from_particles(grid, particles) _write_density_to_netcdf(density, output_path)
def get_particle_release_locations_box(particles: BeachingParticles, iot_island): l_box = get_l_particles_in_box(particles, iot_island) lon0, lat0 = particles.get_initial_particle_lon_lat() return (lon0[l_box], lat0[l_box])
def get_particle_release_time_box(particles: BeachingParticles, iot_island): l_box = get_l_particles_in_box(particles, iot_island) _, t_release = particles._get_release_time_and_particle_indices() time_release = particles.time[t_release[l_box]] return time_release