Skip to content

Commit 6692fcc

Browse files
Merge branch 'alpha2-development' into documentation-start
2 parents 8ef43c1 + a46318b commit 6692fcc

1 file changed

Lines changed: 35 additions & 35 deletions

File tree

plasticparcels/scripts/create_release_maps.py

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -405,15 +405,14 @@ def create_global_concentrations_kaandorp_release_map(mask_land_filepath, mask_c
405405
We match this data to the model coastal and ocean cells for a better coverage release.
406406
We use only the 2020 data for all plastic class sizes, and for the ocean cells we only use the surface 0-5m depth.
407407
"""
408-
408+
409409
# Load in the data and select year 2020, all plastic sizes, and for ocean concentrations take the surface layer
410410
ds = xr.open_dataset(kaandorp_filepath)
411-
beach = ds['concentration_beach_mass_log10'].sel({'size_nominal':'all', 'time':2020})
412-
ocean = ds['concentration_mass_log10'].sel({'size_nominal':'all', 'time':2020, 'depth':'0 - <5'})
411+
beach = ds['concentration_beach_mass_log10'].sel({'size_nominal': 'all', 'time': 2020})
412+
ocean = ds['concentration_mass_log10'].sel({'size_nominal': 'all', 'time': 2020, 'depth': '0 - <5'})
413413

414414
# Load in coast mask and model coordinates
415415
data_mask_coast = xr.open_dataset(mask_coast_filepath)
416-
coords = xr.open_dataset(coords_filepath, decode_cf=False)
417416

418417
lats_coast = data_mask_coast['lat'].data[np.where(data_mask_coast['mask_coast'])]
419418
lons_coast = data_mask_coast['lon'].data[np.where(data_mask_coast['mask_coast'])]
@@ -422,12 +421,12 @@ def create_global_concentrations_kaandorp_release_map(mask_land_filepath, mask_c
422421
# Create a list of lons, lats, concentration values
423422
lon_beach = beach.lon_beach.values
424423
lat_beach = beach.lat_beach.values
425-
conc_beach = np.power(10,beach.values) # beach.values are in log10 form
424+
conc_beach = np.power(10, beach.values) # beach.values are in log10 form
426425

427426
# Load Natural Earth dataset for attaching country information to beach source
428427
shpfilename = shpreader.natural_earth(resolution='50m',
429-
category='cultural',
430-
name='admin_0_countries')
428+
category='cultural',
429+
name='admin_0_countries')
431430
reader = shpreader.Reader(shpfilename)
432431
countries = reader.records()
433432

@@ -442,52 +441,52 @@ def create_global_concentrations_kaandorp_release_map(mask_land_filepath, mask_c
442441
country_lons, country_lats = country_coords[:, 0], country_coords[:, 1]
443442

444443
country_df = pd.DataFrame({'Continent': np.repeat(continent, len(country_lons)),
445-
'Region': np.repeat(region_un, len(country_lons)),
446-
'Subregion': np.repeat(subregion, len(country_lons)),
447-
'Country': np.repeat(country_name, len(country_lons)),
448-
'Longitude': country_lons,
449-
'Latitude': country_lats})
444+
'Region': np.repeat(region_un, len(country_lons)),
445+
'Subregion': np.repeat(subregion, len(country_lons)),
446+
'Country': np.repeat(country_name, len(country_lons)),
447+
'Longitude': country_lons,
448+
'Latitude': country_lats})
450449
countries_list.append(country_df)
451450
coastal_df = pd.concat(countries_list)
452451

453452
# Create coastal concentrations dataset
454453
coast_concentration_list = []
455-
distance_threshhold=50.
454+
distance_threshhold = 50.
456455

457-
for i, (lon, lat) in enumerate(zip(lons_coast,lats_coast)):
456+
for i, (lon, lat) in enumerate(zip(lons_coast, lats_coast)):
458457
# Find the closest beach concentration
459458
distances = distance(np.repeat(lon, len(lon_beach)), np.repeat(lat, len(lat_beach)), lon_beach, lat_beach)
460459
closest_beach_id = np.argmin(distances)
461-
if distances[closest_beach_id] > distance_threshhold: # skip coastal grid cells not within a threshhold from a littered beach
460+
if distances[closest_beach_id] > distance_threshhold: # skip coastal grid cells not within a threshhold from a littered beach
462461
continue
463462
else:
464463
# Find the closest country point to the coastal cell to assign country information
465464
distances_country = distance(np.repeat(lon, len(coastal_df['Longitude'])),
466-
np.repeat(lat, len(coastal_df['Latitude'])),
467-
coastal_df['Longitude'],
468-
coastal_df['Latitude'])
465+
np.repeat(lat, len(coastal_df['Latitude'])),
466+
coastal_df['Longitude'],
467+
coastal_df['Latitude'])
469468
closest_country_id = np.argmin(distances_country)
470469

471470
coast_concentration_list.append({'Continent': coastal_df['Continent'].iloc[closest_country_id],
472-
'Region': coastal_df['Region'].iloc[closest_country_id],
473-
'Subregion': coastal_df['Subregion'].iloc[closest_country_id],
474-
'Country': coastal_df['Country'].iloc[closest_country_id],
475-
'Longitude': lon,
476-
'Latitude': lat,
477-
'Concentration': conc_beach[closest_beach_id],
478-
'ConcentrationType': 'Beach'})
471+
'Region': coastal_df['Region'].iloc[closest_country_id],
472+
'Subregion': coastal_df['Subregion'].iloc[closest_country_id],
473+
'Country': coastal_df['Country'].iloc[closest_country_id],
474+
'Longitude': lon,
475+
'Latitude': lat,
476+
'Concentration': conc_beach[closest_beach_id],
477+
'ConcentrationType': 'Beach'})
479478

480479
coast_concentration_df = pd.DataFrame.from_records(coast_concentration_list)
481480

482481
# Now tackle the surface ocean concentrations:
483-
conc_ocean = np.power(10,ocean.values) # Values are in log10 space
482+
conc_ocean = np.power(10, ocean.values) # Values are in log10 space
484483

485484
data_mask_land = xr.open_dataset(mask_land_filepath)
486485
lats_ocean = data_mask_land['lat'].data[np.where(~data_mask_land['mask_land'])]
487486
lons_ocean = data_mask_land['lon'].data[np.where(~data_mask_land['mask_land'])]
488487

489488
# Function to interpolate the ocean concentrations
490-
f_interp_conc_ocean = RegularGridInterpolator((ocean.lon, ocean.lat), conc_ocean.T, method='nearest', bounds_error=False, fill_value=None)
489+
f_interp_conc_ocean = RegularGridInterpolator((ocean.lon, ocean.lat), conc_ocean.T, method='nearest', bounds_error=False, fill_value=None)
491490

492491
# Interpolate the ocean concentrations to the model grid cells
493492
interp_conc_ocean = f_interp_conc_ocean((lons_ocean, lats_ocean))
@@ -497,20 +496,21 @@ def create_global_concentrations_kaandorp_release_map(mask_land_filepath, mask_c
497496
ocean_concentration_list = []
498497
for i in np.where(non_nan_id)[0]:
499498
ocean_concentration_list.append({'Continent': 'N/A',
500-
'Region': 'N/A',
501-
'Subregion': 'N/A',
502-
'Country': 'N/A',
503-
'Longitude': lons_ocean[i],
504-
'Latitude': lats_ocean[i],
505-
'Concentration': interp_conc_ocean[i],
506-
'ConcentrationType': 'Ocean'})
499+
'Region': 'N/A',
500+
'Subregion': 'N/A',
501+
'Country': 'N/A',
502+
'Longitude': lons_ocean[i],
503+
'Latitude': lats_ocean[i],
504+
'Concentration': interp_conc_ocean[i],
505+
'ConcentrationType': 'Ocean'})
507506
ocean_concentration_df = pd.DataFrame.from_records(ocean_concentration_list)
508507

509508
# Combine the two beach and ocean datasets
510509
concentration_df = pd.concat([ocean_concentration_df, coast_concentration_df])
511510

512511
return concentration_df
513512

513+
514514
output_data = '/Users/denes001/Research/Projects/PlasticParcels/PlasticParcels/data/release/generated_files/'
515515
mask_land_filepath = '/Users/denes001/Research/Projects/PlasticParcels/PlasticParcels/data/output_data/masks/mask_land_NEMO0083.nc'
516516
mask_coast_filepath = '/Users/denes001/Research/Projects/PlasticParcels/PlasticParcels/data/output_data/masks/mask_coast_NEMO0083.nc'
@@ -565,4 +565,4 @@ def create_global_concentrations_kaandorp_release_map(mask_land_filepath, mask_c
565565
concentration_dataset.to_csv(output_name)
566566
print("Concentration map file created:", output_name)
567567
else:
568-
print("Concentration map file already exists:", output_name)
568+
print("Concentration map file already exists:", output_name)

0 commit comments

Comments
 (0)