def plot_org_and_moved(self,lon_range=[20.,130.],lat_range=[-40.,40.], land_mask=LandMask.read_from_netcdf('input/hycom_landmask.nc'), plot_mplstyle='plot_tools/plot.mplstyle'): original = self.read_from_shapefile() plt.style.use(plot_mplstyle) fig = plt.figure() ax = plt.gca(projection=ccrs.PlateCarree()) ax.set_extent([-180,180,-80,80],ccrs.PlateCarree()) ax.add_feature(cftr.COASTLINE,edgecolor='k',zorder=2) if land_mask is not None: # plot land mask cmap_lm = colors.ListedColormap(['#ffffff','#000000']) norm_lm = colors.BoundaryNorm([0,0.5,1],cmap_lm.N,clip=True) l_lon = np.logical_and(land_mask.lon>=lon_range[0],land_mask.lon<=lon_range[-1]) l_lat = np.logical_and(land_mask.lat>=lat_range[0],land_mask.lat<=lat_range[-1]) lon_edges,lat_edges = land_mask.get_edges_from_center_points(l_lon=l_lon,l_lat=l_lat) ax.pcolormesh(lon_edges,lat_edges,land_mask.mask[l_lat,:][:,l_lon], norm=norm_lm,cmap=cmap_lm,transform=ccrs.PlateCarree(),zorder=1) # plot source locations with nonzero waste input i_source = np.where(np.sum(self.waste,axis=1) != 0)[0] ax.scatter(original.lon[i_source],original.lat[i_source],marker='x', c='#DC3015',label='original source locations',transform=ccrs.PlateCarree(), zorder=3) ax.scatter(self.lon[i_source],self.lat[i_source],marker='o',c='#0C59A6', label='source locations in ocean',transform=ccrs.PlateCarree(),zorder=4) # legend patch_lm = Patch(facecolor='#000000',edgecolor='#000000',label='Hycom landmask') ax.legend() ax.set_title('River plastic source locations') plt.show()
def run_hycom_subset_monthly_release(input_dir, output_dir, output_name, time0, lon0, lat0, start_date, end_date, kh=10., interp_method='linear', indices=_get_io_indices_from_netcdf()): # get paths ncfiles = get_daily_ncfiles_in_time_range(input_dir, start_date, end_date) output_path = output_dir + output_name # create fieldset filenames = [input_dir + ncfile for ncfile in ncfiles] variables = {'U': 'u', 'V': 'v'} dimensions = {'lat': 'lat', 'lon': 'lon', 'time': 'time'} fset = FieldSet.from_netcdf(filenames, variables, dimensions, indices=indices) # add constant horizontal diffusivity (zero on land) lm = LandMask.read_from_netcdf() kh2D = kh * np.ones(lm.mask.shape) kh2D[lm.mask.astype('bool')] = 0.0 # diffusion zero on land kh2D_subset = kh2D[indices['lat'], :][:, indices['lon']] fset.add_field( Field('Kh_zonal', data=kh2D_subset, lon=fset.U.grid.lon, lat=fset.U.grid.lat, mesh='spherical', interp_method=interp_method)) fset.add_field( Field('Kh_meridional', data=kh2D_subset, lon=fset.U.grid.lon, lat=fset.U.grid.lat, mesh='spherical', interp_method=interp_method)) # montly release pset = ParticleSet(fieldset=fset, pclass=JITParticle, lon=lon0, lat=lat0, time=time0) # execute run_time = timedelta(days=(end_date - start_date).days) dt = timedelta(hours=1) output_interval = 24 kernel = pset.Kernel(AdvectionRK4) + pset.Kernel(DiffusionUniformKh) output_file = pset.ParticleFile(name=output_path, outputdt=dt * output_interval) pset.execute(kernel, runtime=run_time, dt=dt, output_file=output_file, verbose_progress=True, recovery={ErrorCode.ErrorOutOfBounds: delete_particle})
def get_kh_value_from_grid_size(lat0, input_path='input/hycom_landmask.nc') -> float: lm = LandMask.read_from_netcdf(input_path=input_path) mean_dx_degree = np.nanmean( [np.nanmean(np.diff(lm.lon)), np.nanmean(np.diff(lm.lat))]) dx = get_distance_between_points(113.0, lat0, 113.0 + mean_dx_degree, lat0) # m epsilon = 10**(-9) # m^2/s^3 kh = epsilon**(1 / 3) * dx**(4 / 3) return np.round(kh, 2)
def move_sources_to_ocean(self,land_mask=LandMask.read_from_netcdf('input/hycom_landmask.nc'), log_file='move_riversources_hycom_landmask.log'): lon = self.lon lat = self.lat lm_halo = land_mask.get_landmask_with_halo() log.info(log_file,'Increased land mask with 1 grid cell.') log.info(log_file,'Getting indices of points on land...') p_land = self._get_index_of_sources_on_land(lm_halo) log.info(log_file,f'Found {str(len(p_land))} sources on land.') for i,p in enumerate(p_land): log.info(log_file,f'Trying to move source {str(i+1)}/{str(len(p_land))} , p = {str(p)}:') lon[p],lat[p] = lm_halo.get_closest_ocean_point(self.lon[p],self.lat[p],log_file=log_file) return RiverSources(lon,lat,self.time,self.waste,self.waste_min,self.waste_max)
def get_release_time_lon_lat(): lon_org = 79.757667 lat_org = 7.071556 start_date = datetime(2008, 5, 22) # first explosion end_date = datetime(2008, 6, 2) # stern sunk n_particles = 1000 # per release release_interval_hours = 3 n_releases = ((end_date - start_date).days + 1) * int( 24 / release_interval_hours) # 3 hourly release lm = LandMask.read_from_netcdf() lm_halo = lm.get_landmask_with_halo() lon, lat = lm_halo.get_closest_ocean_point(lon_org, lat_org) time = np.array([ start_date + timedelta(hours=i * release_interval_hours) for i in range(n_releases) ]) time0 = np.repeat(time, n_particles) lon0 = np.repeat(lon, len(time0)) lat0 = np.repeat(lat, len(time0)) return time0, lon0, lat0
def plot(self,t=0,lon_range=None,lat_range=None, plot_mplstyle='plot_tools/plot.mplstyle', land_mask=LandMask.read_from_netcdf('input/hycom_landmask.nc')): plastic = np.round(self.waste[:,0]) plastic[plastic==0] = np.nan plastic_log = np.log10(plastic) month = datetime.strptime(str(t+1),'%m').strftime('%b') plt.style.use(plot_mplstyle) fig = plt.figure() ax = plt.gca(projection=ccrs.PlateCarree()) # map if not lon_range and not lat_range: lon_range = [-180,180] lat_range = [-80,80] ax.set_extent([lon_range[0],lon_range[1],lat_range[0],lat_range[1]],ccrs.PlateCarree()) if land_mask is not None: # plot land mask cmap_lm = colors.ListedColormap(['#ffffff','#000000']) norm_lm = colors.BoundaryNorm([0,0.5,1],cmap_lm.N,clip=True) l_lon = np.logical_and(land_mask.lon>=lon_range[0],land_mask.lon<=lon_range[-1]) l_lat = np.logical_and(land_mask.lat>=lat_range[0],land_mask.lat<=lat_range[-1]) lon_edges,lat_edges = land_mask.get_edges_from_center_points(l_lon=l_lon,l_lat=l_lat) ax.pcolormesh(lon_edges,lat_edges,land_mask.mask[l_lat,:][:,l_lon], norm=norm_lm,cmap=cmap_lm,transform=ccrs.PlateCarree(),zorder=1) else: ax.add_feature(cftr.COASTLINE,edgecolor='k',zorder=2) # set plot ranges and custom colormap ticks = np.array([1,10,10**2,10**3,10**4,10**5,10**6]) ticks_str = ['1','10','10$^2$','10$^3$','10$^4$','10$^5$','10$^6$'] levels = np.log10(ticks) cmap = colors.ListedColormap(['#63676b','#106bb8','#f1d435','#d50000','#640000','#470000']) norm = colors.BoundaryNorm(levels, cmap.N, clip=True) c = ax.scatter(self.lon,self.lat,c=plastic_log,cmap=cmap,norm=norm,s=30, transform=ccrs.PlateCarree(),zorder=3) # colorbar cbar = plt.colorbar(c,ticks=levels) cbar.ax.set_yticklabels(ticks_str) cbar.set_label('Plastic waste input [tonnes]') ax.set_title('River plastic input in '+month) plt.show()