def createTRENDMAP(self, varname): trend = self.calcTREND(self.VARS[varname]) output_fh = os.path.join(self.OutFldr, '{0}.zip'.format(varname)) self.factor = np.interp(self.Area, [50000, 625000000], [0.1, 1.0]) scale = self.scale.multiply(self.factor).getInfo() geegis.downloadImage(trend, output_fh, self.CountryShape, scale) TREND = becgis.OpenAsArray(os.path.join(output_fh[:-4], 'test.long-trend.tif')) MASK = becgis.OpenAsArray(os.path.join(output_fh[:-4], 'test.max.tif')) PVALUE = becgis.OpenAsArray(os.path.join(output_fh[:-4], 'test.p-value.tif')) assert np.shape(TREND) == np.shape(MASK), "resolution dont match" TREND[MASK == 0] = np.nan TREND[TREND == 0.0000000000] = np.nan # need to fix this properly. TREND[PVALUE > 0.1] = np.nan PVALUE[PVALUE <= 0.1] = np.nan PVALUE[MASK == 0] = np.nan PVALUE[~np.isnan(PVALUE)] = 1. AREA = becgis.MapPixelAreakm(os.path.join(output_fh[:-4], 'test.max.tif')) self.PixelArea = np.mean(AREA[MASK == 1]) * 100 # ha self.Columns[varname]['TREND'] = TREND self.Columns[varname]['PVALUE'] = PVALUE ds = gdal.Open(os.path.join(output_fh[:-4], 'test.long-trend.tif')) gt = ds.GetGeoTransform() n_cols = ds.RasterXSize n_rows = ds.RasterYSize ds = None self.extent_ll = (gt[0], gt[0] + (gt[1] * n_cols), gt[3] + (gt[5] * n_rows), gt[3])
def livestock_feed(output_folder, lu_fh, ndm_fhs, feed_dict, live_feed, cattle_fh, fraction_fhs, ndmdates): """ Calculate natural livestock feed production INPUTS ---------- lu_fh : str filehandle for land use map ndm_fhs: nd array array of filehandles of NDM maps ndm_dates: nd array array of dates for NDM maps feed_dict: dict dictionnary 'pasture class':[list of LULC] feed_pct: dict dictionnary 'pasture class':[percent available as feed] cattle_fh : str filehandle for cattle map """ Data_Path_Feed = "Feed" out_folder = os.path.join(output_folder, Data_Path_Feed) if not os.path.exists(out_folder): os.mkdir(out_folder) area_ha = becgis.MapPixelAreakm(lu_fh) * 100 LULC = RC.Open_tiff_array(lu_fh) # cattle = RC.Open_tiff_array(cattle_fh) geo_out, proj, size_X, size_Y = RC.Open_array_info(lu_fh) f_pct = np.zeros(LULC.shape) for lu_type in feed_dict.keys(): classes = feed_dict[lu_type] mask = np.logical_or.reduce([LULC == value for value in classes]) f_pct[mask] = live_feed[lu_type] feed_fhs_landscape = [] feed_fhs_incremental = [] for d in range(len(ndm_fhs)): ndm_fh = ndm_fhs[d] fraction_fh = fraction_fhs[d] date1 = ndmdates[d] year = '%d' %date1.year month = '%02d' %date1.month yield_fract = RC.Open_tiff_array(fraction_fh) out_fh_l = out_folder+'\\feed_prod_landscape_%s_%s.tif' %(year, month) out_fh_i = out_folder+'\\feed_prod_incremental_%s_%s.tif' %(year, month) # out_fh2 = out_folder+'\\Feed_prod_pH_%s_%s.tif' %(year, month) NDM = becgis.OpenAsArray(ndm_fh, nan_values=True) NDM_feed = NDM * f_pct NDM_feed_incremental = NDM_feed * yield_fract * area_ha/1e6 NDM_feed_landscape = (NDM_feed *(1-yield_fract)) * area_ha/1e6 DC.Save_as_tiff(out_fh_l, NDM_feed_landscape, geo_out) DC.Save_as_tiff(out_fh_i, NDM_feed_incremental, geo_out) # NDM_feed_perHead = NDM_feed / cattle # DC.Save_as_tiff(out_fh2, NDM_feed, geo_out) feed_fhs_landscape.append(out_fh_l) feed_fhs_incremental.append(out_fh_i) return feed_fhs_landscape, feed_fhs_incremental
def lu_type_sum(data_fh, lu_fh, lu_dict, convert=None): LULC = RC.Open_tiff_array(lu_fh) in_data = becgis.OpenAsArray(data_fh, nan_values=True) # in_data = RC.Open_tiff_array(data_fh) if convert == 'mm_to_km3': AREA = becgis.MapPixelAreakm(data_fh) in_data *= AREA / 1e6 out_data = {} for lu_class in lu_dict.keys(): mask = [LULC == value for value in lu_dict[lu_class]] mask = (np.sum(mask, axis=0)).astype(bool) out_data[lu_class] = np.nansum(in_data[mask]) return out_data
def fuel_wood(output_folder, lu_fh, ndm_fhs, fraction_fhs, ndmdates): """ Calculate natural livestock feed production INPUTS ---------- lu_fh : str filehandle for land use map ndm_fhs: nd array array of filehandles of NDM maps abv_grnd_biomass_ratio: dict dictionnary 'LULC':[above ground biomass] """ Data_Path_Fuel = "Fuel" out_folder = os.path.join(output_folder, Data_Path_Fuel) if not os.path.exists(out_folder): os.mkdir(out_folder) area_ha = becgis.MapPixelAreakm(lu_fh) * 100 LULC = RC.Open_tiff_array(lu_fh) geo_out, proj, size_X, size_Y = RC.Open_array_info(lu_fh) fuel_classes = [1, 8, 9, 10, 11, 12, 13] fuel_mask = np.zeros(LULC.shape) for fc in fuel_classes: fuel_mask[np.where(LULC == fc)] = 1 fuel_fhs_landscape = [] fuel_fhs_incremental = [] for d in range(len(ndm_fhs)): ndm_fh = ndm_fhs[d] fraction_fh = fraction_fhs[d] yield_fract = RC.Open_tiff_array(fraction_fh) date1 = ndmdates[d] year = '%d' %date1.year month = '%02d' %date1.month # year = ndm_fh[-14:-10] # month = ndm_fh[-9:-7] out_fh_l = out_folder+'\\fuel_prod_landscape_%s_%s.tif' %(year, month) out_fh_i = out_folder+'\\fuel_prod_incremental_%s_%s.tif' %(year, month) NDM = becgis.OpenAsArray(ndm_fh, nan_values=True) NDM_fuel_incremental = NDM * .05 * fuel_mask * yield_fract * area_ha/1e6 NDM_fuel_landscape = NDM * .05 * fuel_mask *(1-yield_fract) * area_ha/1e6 DC.Save_as_tiff(out_fh_i, NDM_fuel_incremental, geo_out) DC.Save_as_tiff(out_fh_l, NDM_fuel_landscape, geo_out) fuel_fhs_landscape.append(out_fh_l) fuel_fhs_incremental.append(out_fh_i) return fuel_fhs_landscape, fuel_fhs_incremental
def calc_sheet1(entries, lu_fh, sheet1_lucs, recycling_ratio, q_outflow, q_out_avg, output_folder, q_in_sw, q_in_gw = 0., q_in_desal = 0., q_out_sw = 0., q_out_gw = 0.): """ Calculate the required values to plot Water Accounting Plus Sheet 1. Parameters ---------- entries : dict Dictionary with several filehandles, also see examples below. lu_fh : str Filehandle pointing to the landuse map. sheet1_lucs : dict Dictionary sorting different landuse classes into categories. recycling_ratio : float Value indicating the recycling ratio. q_outflow : float The outflow of the basin. q_out_avg : float The longterm average outflow. output_folder : str Folder to store results. q_in_sw : float, optional Surfacewater inflow into the basin. Default is 0.0. q_in_gw : float, optional Groundwater inflow into the basin. Default is 0.0. q_in_desal : float, optional Desalinised water inflow into the basin. Default is 0.0. q_out_sw : float, optional Additional surfacewater outflow from basin. Default is 0.0. q_out_gw : float, optional Groundwater outflow from the basin. Default is 0.0. Returns ------- results : dict Dictionary containing necessary variables for Sheet 1. """ results = dict() LULC = becgis.OpenAsArray(lu_fh, nan_values = True) P = becgis.OpenAsArray(entries['P'], nan_values = True) ETgreen = becgis.OpenAsArray(entries['ETgreen'], nan_values = True) ETblue = becgis.OpenAsArray(entries['ETblue'], nan_values = True) pixel_area = becgis.MapPixelAreakm(lu_fh) gray_water_fraction = becgis.calc_basinmean(entries['WPL'], lu_fh) ewr_percentage = becgis.calc_basinmean(entries['EWR'], lu_fh) P[np.isnan(LULC)] = ETgreen[np.isnan(LULC)] = ETblue[np.isnan(LULC)] = np.nan P, ETgreen, ETblue = np.array([P, ETgreen, ETblue]) * 0.000001 * pixel_area ET = np.nansum([ETblue, ETgreen], axis = 0) results['et_advection'], results['p_advection'], results['p_recycled'], results['dS'] = calc_wb(P, ET, q_outflow, recycling_ratio, q_in_sw = q_in_sw, q_in_gw = q_in_gw, q_in_desal = q_in_desal, q_out_sw = q_out_sw, q_out_gw = q_out_gw) results['non_recoverable'] = gray_water_fraction * (q_outflow + q_out_sw) # Mekonnen and Hoekstra (2015), Global Gray Water Footprint and Water Pollution Levels Related to Anthropogenic Nitrogen Loads to Fresh Water results['reserved_outflow_demand'] = q_out_avg * ewr_percentage results['other'] = 0.0 landscape_et = calc_ETs(ETgreen, lu_fh, sheet1_lucs) incremental_et = calc_ETs(ETblue, lu_fh, sheet1_lucs) results['manmade'] = incremental_et['Managed'] results['natural'] = incremental_et['Modified'] + incremental_et['Protected'] + incremental_et['Utilized'] other_fractions = {'Modified': 0.00, 'Managed': 1.00, 'Protected':0.00, 'Utilized': 0.00} non_recoverable_fractions = {'Modified': 0.00, 'Managed': 1.00, 'Protected':0.00, 'Utilized': 0.00} results['uf_plu'], results['uf_ulu'], results['uf_mlu'], results['uf_mwu'] = calc_utilizedflow(incremental_et, results['other'], results['non_recoverable'], other_fractions, non_recoverable_fractions) net_inflow = results['p_recycled'] + results['p_advection'] + q_in_sw + q_in_gw + q_in_desal + results['dS'] consumed_water = np.nansum(landscape_et.values()) + np.nansum(incremental_et.values()) + results['other'] + results['non_recoverable'] non_consumed_water = net_inflow - consumed_water results['non_utilizable_outflow'] = min(non_consumed_water, max(0.0, calc_non_utilizable(P, ET, entries['Fractions']))) results['reserved_outflow_actual'] = min(non_consumed_water - results['non_utilizable_outflow'], results['reserved_outflow_demand']) results['utilizable_outflow'] = max(0.0, non_consumed_water - results['non_utilizable_outflow'] - results['reserved_outflow_actual']) results['landscape_et_mwu'] = landscape_et['Managed'] results['landscape_et_mlu'] = landscape_et['Modified'] results['landscape_et_ulu'] = landscape_et['Utilized'] results['landscape_et_plu'] = landscape_et['Protected'] results['q_outflow'] = q_outflow results['q_in_sw'] = q_in_sw results['q_in_gw'] = q_in_gw results['q_in_desal'] = q_in_desal results['q_out_sw'] = q_out_sw results['q_out_gw'] = q_out_gw return results