def main(): from parameters import states_nhd from paths import nhd_raster_path, soil_raster_path, weather_raster_path, cdl_path, combo_path years = [2010] # range(2010, 2016) arcpy.CheckOutExtension("Spatial") arcpy.env.overwriteOutput = True cell_size = 30 # Create output directory if not os.path.exists(os.path.dirname(combo_path)): os.makedirs(os.path.dirname(combo_path)) # Initialize weather raster nhd_regions = ['07'] # Iterate through year/region combinations for region in nhd_regions: nhd_raster = nhd_raster_path.format(vpus_nhd[region], region) for year in years: cdl_raster = cdl_path.format(region, year) master_array = np.zeros((0, 5)) for state in states_nhd[region]: print("Performing overlay for {}, {}, {}".format( year, region, state)) ssurgo_raster = soil_raster_path.format(state.lower()) table = overlay_rasters(nhd_raster, weather_raster_path, cdl_raster, ssurgo_raster) master_array = np.concatenate((master_array, table)) out_array = condense_array(master_array, cell_size) save_table(out_array, combo_path.format(region, year))
def main(): years = range(2015, 2020) # range(2010, 2016) overwrite_raster = True overwrite_combos = False regions = ['07'] + list(nhd_regions) for region in regions: nhd_raster = nhd_raster_path.format(vpus_nhd[region], region) arcpy.env.snapRaster = nhd_raster arcpy.env.mask = nhd_raster for year in years: print(region, year) cdl_raster = cdl_path.format(year) combined_raster = combined_raster_path.format(region, year) + "test" combinations_table = combo_path.format(region, year) if overwrite_raster or not os.path.exists(combined_raster): report("Performing raster overlay for Region {}, {}...".format(region, year)) try: overlay_rasters(combined_raster, cdl_raster, nhd_raster) report(f"Combined raster saved to {combined_raster}") except Exception as e: raise e if overwrite_combos or not os.path.exists(combinations_table): report("Building combinations table for Region {}, {}...".format(region, year)) try: combos = generate_combos(combined_raster, year) combos.to_csv(combinations_table, index=None) except Exception as e: raise e
def combinations_new(region, year): """ Not yet implemented pending new combination files """ for state in states_nhd[region]: combo_file = combo_path.format(region, state, year) combo_table = pd.DataFrame(dtype=np.int32, **np.load(combo_file)) matrix = combo_table if matrix is None else pd.concat([matrix, combo_table], axis=0) # Column names get written as binary by spatial_overlay matrix.columns = [f.decode('ascii') for f in matrix.columns.values] # Combine duplicate rows and convert counts to areas matrix = matrix.groupby(['gridcode', 'weather_grid', 'cdl', 'mukey']).sum().reset_index() matrix['area'] *= cell_size ** 2 return matrix
def combinations(region, years, nrows=None): """ Read a table of soil/land cover/weather/watershed combinations for generating scenario files. :param region: NHD Plus Hydroregion (str) :param years: Years to fetch data for (iter, int) :param nrows: Number of rows to read (int, optional) :return: Combinations table (df) """ all_combos = None for year in years: header = ['gridcode', 'cdl', 'weather_grid', 'mukey', 'area'] combo_file = combo_path.format(region, year) combos = pd.read_csv(combo_file, dtype=np.uint32, nrows=nrows)[header] combos['year'] = np.int16(year) all_combos = combos if all_combos is None else pd.concat( [all_combos, combos], axis=0) all_combos['region'] = str(region).zfill(2) return all_combos
def combinations(region, year, table): """ Write a combinations table to a csv file """ out_path = combo_path.format(region, year) # Create output directory create_dir(out_path) table.to_csv(out_path, index=None)
def combinations(region, year): combos = pd.DataFrame(**np.load(combo_path.format(region, year)), dtype=np.int32) return combos