Esempio n. 1
0
def make_annual_loadings(modifications, years=settings.NgwRasters.keys()):

	# First make the loadings for just the years we have precalculated (1945, 1960, etc)

	print("Building Annual Loadings")
	loadings = {}
	for year in years:
		print(year)
		base_loading_matrix = compatibility.raster_to_numpy_array(settings.NgwRasters[year])
		if year >= settings.ChangeYear:  # if this year is after our reductions are supposed to be made
			weight_matrix = make_weight_raster(settings.LandUseRasters[year], modifications=modifications)
			loadings[year] = weight_matrix * base_loading_matrix
		else:  # otherwise, use the straight Ngw values - no changes have been made since they're in the past
			loadings[year] = base_loading_matrix

	# Now that we have the values for the base years, we want to interpolate between them to make ndarrays for each year

	print("Interpolating between years")
	sorted_years = sorted(years)
	all_years_data = None
	for i, year in enumerate(sorted_years):
		print(year)
		if year == sorted_years[-1]:  # if it's the last year, we have special behavior
			break

		next_data_year = sorted_years[i+1]
		interval_size = next_data_year - year
		all_years_in_range = create_ranges_nd(loadings[year], loadings[next_data_year], interval_size)
		if all_years_data is None:
			all_years_data = all_years_in_range
		else:
			all_years_data = numpy.concatenate([all_years_data, all_years_in_range], -1)  # stack them on the last axis now

	return all_years_data
Esempio n. 2
0
def make_weight_raster(land_use, modifications):
	"""
		Given a land use raster and a set of weights, applies the weights to each land use type
		then sets everything else to 1 so that the raster can be used as a multiplier later
	:param land_use: path to a land use raster on disk
	:param modifications: an iterable of npsat_manager.models.Modification objects
	:return:
	"""
	land_use_array = compatibility.raster_to_numpy_array(land_use)
	land_use_array += 10000  # offset everything by 10000 so we can identify everything that's still a default later
	for modification in modifications:
		land_use_array[land_use_array == modification.crop.caml_code] = modification.reduction

	land_use_array[land_use_array >= 10000] = 1

	return land_use_array