def _run_on_fuelbed(self, fb, season, location, burn_type): if 'consumption' not in fb: raise ValueError( "Missing consumption data required for computing emissions") if 'heat' not in fb: raise ValueError( "Missing heat data required for computing emissions") if 'pct' not in fb: raise ValueError( "Missing fuelbed 'ptc' required for computing emissions") if 'ecoregion' not in location: raise ValueError( "Missing ecoregion required for computing emissions") fuel_loadings_csv_filename = self.fuel_loadings_manager.generate_custom_csv( fb['fccs_id']) # unlike with consume consumption results, emissions results reflect # how you set area and output_units area = (fb['pct'] / 100.0) * location['area'] fc = FuelConsumptionForEmissions(fb["consumption"], fb['heat'], area, burn_type, fb['fccs_id'], season, location, fccs_file=fuel_loadings_csv_filename) e_fuel_loadings = self.fuel_loadings_manager.get_fuel_loadings( fb['fccs_id'], fc.FCCS) fb['emissions_fuel_loadings'] = e_fuel_loadings e = consume.Emissions(fuel_consumption_object=fc) e.output_units = 'tons' # Consume emissions prints out lines like # Converting units: tons_ac -> tons # which we want to capture and ifnore with capture_stdout() as stdout_buffer: r = e.results()['emissions'] fb['emissions'] = {f: {} for f in CONSUME_FIELDS} # r's key hierarchy is species > phase; we want phase > species for k in r: upper_k = 'PM2.5' if k == 'pm25' else k.upper() if k != 'stratum' and (not self.species or upper_k in self.species): for p in r[k]: fb['emissions'][p][upper_k] = r[k][p] if self.include_emissions_details: # Note: consume gives details per fuel category, not per # subcategory; to match what FEPS and Prichard/O'Neill calculators # produce, put all per-category details under'summary' # The details are under key 'stratum'. the key hierarchy is: # 'stratum' > species > fuel category > phase # we want phase > species: # 'summary' > fuel category > phase > species fb['emissions_details'] = { "summary": {} } for k in r.get('stratum', {}): upper_k = 'PM2.5' if k == 'pm25' else k.upper() if not self.species or upper_k in self.species: for c in r['stratum'][k]: fb['emissions_details']['summary'][c] = fb['emissions_details']['summary'].get(c, {}) for p in r['stratum'][k][c]: fb['emissions_details']['summary'][c][p] = fb['emissions_details']['summary'][c].get(p, {}) fb['emissions_details']['summary'][c][p][upper_k] = r['stratum'][k][c][p]
def test(self): with io.capture_stdout() as stdout_buffer: assert sys.stdout == stdout_buffer assert "" == stdout_buffer.read() print("sdf") stdout_buffer.seek(0) assert "sdf\n" == stdout_buffer.read() sys.stdout.write("322342") stdout_buffer.seek(0) assert "sdf\n322342" == stdout_buffer.read() assert sys.stdout == stdout_buffer assert sys.stdout != stdout_buffer assert sys.stdout == sys.__stdout__