def run(burn_type,
        csv_input,
        do_metric,
        msg_level,
        outfile,
        fuel_loadings=None,
        col_cfg=None,
        no_sera=False):
    # validate alternate loadings file if provide. Throws exception on invalid
    if fuel_loadings: validate_fuel_loadings(fuel_loadings)

    # obtain a FuelConsumption object
    consumer = consume.FuelConsumption(fccs_file=fuel_loadings, msg_level=msg_level) \
        if fuel_loadings else consume.FuelConsumption(msg_level=msg_level)

    # run the calculator and either pickle results for later output
    #  or output as specified
    consumer.burn_type = burn_type
    if consumer.load_scenario(csv_input, display=False):
        emissions = consume.Emissions(consumer)
        if no_sera:
            emissions.no_sera = True
        else:
            emissions.no_sera = False

        results = emissions.results()

        write_results(results, outfile, do_metric, col_cfg_file=col_cfg)
        if not pickle_output(col_cfg):
            print("\nSuccess!!! Results are in \"{}\"".format(outfile))
示例#2
0
def _run_fuelbed(fb, location, fuel_loadings_manager, season, burn_type,
                 msg_level):
    fuel_loadings_csv_filename = fuel_loadings_manager.generate_custom_csv(
        fb['fccs_id'])

    fc = consume.FuelConsumption(
        fccs_file=fuel_loadings_csv_filename)  #msg_level=msg_level)

    fb['fuel_loadings'] = fuel_loadings_manager.get_fuel_loadings(
        fb['fccs_id'], fc.FCCS)

    fc.burn_type = burn_type
    fc.fuelbed_fccs_ids = [fb['fccs_id']]
    fc.season = [season]

    # Note: if we end up running fc on all fuelbeds at once, use lists
    # for the rest
    # Note: consumption output is always returned in tons per acre
    #  (see comment, below) and is linearly related to area, so the
    #  consumption values are the same regardless of what we set
    #  fuelbed_area_acres to.  Released heat output, on the other hand,
    #  is not linearly related to area, so we need to set area
    area = (fb['pct'] / 100.0) * location['area']
    fc.fuelbed_area_acres = [area]
    fc.fuelbed_ecoregion = [location['ecoregion']]

    _apply_settings(fc, location, burn_type)
    _results = fc.results()
    if _results:
        # TODO: validate that _results['consumption'] and
        #   _results['heat'] are defined
        fb['consumption'] = _results['consumption']
        fb['consumption'].pop('debug', None)
        fb['heat'] = _results['heat release']

        # Multiply each consumption value by area if output_inits is 'tons_ac'
        # Note: regardless of what fc.output_units is set to, it gets
        #  reset to 'tons_ac' in the call to fc.results, and the output values
        #  are the same (presumably always in tons_ac)
        # Also multiply heat by area, though we're currently not sure if the
        # units are in fact BTU per acre
        if fc.output_units == 'tons_ac':
            datautils.multiply_nested_data(fb["consumption"], area)
            datautils.multiply_nested_data(fb["heat"], area)

    else:
        # TODO: somehow get error information from fc object; when
        #   you call fc.results() in an error situation, it writes to
        #   stdout or stderr (?), something like:
        #
        #     !!! Error settings problem, the following are required:
        #            fm_type
        #   it would be nice to access that error message here and
        #   include it in the exception message
        raise RuntimeError("Failed to calculate consumption for "
                           "fuelbed {}".format(fb['fccs_id']))
示例#3
0
def _run_fuelbed(fb, location, fuel_loadings_manager, season, burn_type,
                 msg_level):
    fuel_loadings_csv_filename = fuel_loadings_manager.generate_custom_csv(
        fb['fccs_id'])

    fc = consume.FuelConsumption(
        fccs_file=fuel_loadings_csv_filename)  #msg_level=msg_level)

    fb['fuel_loadings'] = fuel_loadings_manager.get_fuel_loadings(
        fb['fccs_id'], fc.FCCS)

    fc.burn_type = burn_type
    fc.fuelbed_fccs_ids = [fb['fccs_id']]
    fc.season = [season]

    # Note: if we end up running fc on all fuelbeds at once, use lists
    # for the rest
    # Note: consume expects area, but disregards it when computing
    #  consumption values - it produces tons per unit area (acre?), not
    #  total tons; so, to avoid confution, just set it to 1 and
    #  then correct the consumption values by multiplying by area, below
    area = (fb['pct'] / 100.0) * location['area']
    fc.fuelbed_area_acres = [1]  # see see note,above
    fc.fuelbed_ecoregion = [location['ecoregion']]

    _apply_settings(fc, location, burn_type)
    _results = fc.results()
    if _results:
        # TODO: validate that _results['consumption'] and
        #   _results['heat'] are defined
        fb['consumption'] = _results['consumption']
        fb['consumption'].pop('debug', None)
        fb['heat'] = _results['heat release']

        # multiply each consumption and heat value by area if
        # output_inits is 'tons_ac',
        # TODO: multiple by area even if user sets output_units to 'tons',
        #   because consume doesn't seem to be multiplying by area for us
        #   even when 'tons' is specified
        if fc.output_units == 'tons_ac':
            datautils.multiply_nested_data(fb["consumption"], area)
            datautils.multiply_nested_data(fb["heat"], area)

    else:
        # TODO: somehow get error information from fc object; when
        #   you call fc.results() in an error situation, it writes to
        #   stdout or stderr (?), something like:
        #
        #     !!! Error settings problem, the following are required:
        #            fm_type
        #   it would be nice to access that error message here and
        #   include it in the exception message
        raise RuntimeError("Failed to calculate consumption for "
                           "fuelbed {}".format(fb['fccs_id']))
    def setUpClass(self):
        '''
        Expected values come from a spreadsheet in the Consume docs repo.
        Run a script there to extract and format the expected values into .csv file.
        Current, copy the files to this repo. Consider using CI server in the future.
        '''
        self._south_exp = pd.read_csv(helper.imp(SOUTHERN_EXPECTED_FILE))
        self._west_exp = pd.read_csv(helper.imp(WESTERN_EXPECTED_FILE))
        
        # unfortunately, the basal acc. and sq. midden calculations use proportional duff consumption
        self._duff_proportional_consumption = None
        
        loadings_file = helper.get_test_loadingsfile()
        self.fc = consume.FuelConsumption(fccs_file=loadings_file)
        self.fc.burn_type = 'natural'
        input_file = helper.get_test_inputfile()
        # debug: my_print(' - Loading input from: {}'.format(input_file))
        self.fc.load_scenario(load_file=input_file)
        self._loadings = self.fc._get_loadings_for_specified_files(self.fc._settings.get('fuelbeds'))

        # Setup ecoregion masks for equations that vary by ecoregion
        self._ecodict = {"maskb": {"boreal": 1, "western": 0, "southern": 0},
                   "masks": {"boreal": 0, "western": 0, "southern": 1},
                   "maskw": {"boreal": 0, "western": 1, "southern": 0}}

        my_print('--- ecoregion settings')
        my_print('\t'.join(self.fc._settings.get('ecoregion')))

        ecoregion = self.fc._settings.get('ecoregion')
        self._ecob_mask = [self._ecodict["maskb"][e] for e in ecoregion]
        self._ecos_mask = [self._ecodict["masks"][e] for e in ecoregion]
        self._ecow_mask = [self._ecodict["maskw"][e] for e in ecoregion]
        self._ecoregion_masks = {
            'boreal': self._ecob_mask,
            'southern': self._ecos_mask,
            'western': self._ecow_mask}
 def setUp(self):
     infile = helper.get_test_loadingsfile()
     self._consumer = consume.FuelConsumption()
     '''