コード例 #1
0
    def test_carbon_full_undefined_nodata(self):
        """Carbon: full model run when input raster nodata is None."""
        from natcap.invest import carbon

        args = {
            'workspace_dir': self.workspace_dir,
            'do_valuation': True,
            'price_per_metric_ton_of_c': 43.0,
            'rate_change': 2.8,
            'lulc_cur_year': 2016,
            'lulc_fut_year': 2030,
            'discount_rate': -7.1,
            'n_workers': -1,
        }

        # Create LULC rasters and pools csv in workspace and add them to args.
        lulc_names = ['lulc_cur_path', 'lulc_fut_path', 'lulc_redd_path']
        for fill_val, lulc_name in enumerate(lulc_names, 1):
            args[lulc_name] = os.path.join(args['workspace_dir'],
                                           lulc_name + '.tif')
            make_simple_raster(args[lulc_name], fill_val, None)

        args['carbon_pools_path'] = os.path.join(args['workspace_dir'],
                                                 'pools.csv')
        make_pools_csv(args['carbon_pools_path'])

        carbon.execute(args)

        # Add assertions for npv for future and REDD scenarios.
        # The npv was calculated based on _calculate_npv in carbon.py.
        assert_raster_equal_value(
            os.path.join(args['workspace_dir'], 'npv_fut.tif'), -0.3422078)
        assert_raster_equal_value(
            os.path.join(args['workspace_dir'], 'npv_redd.tif'), -0.4602106)
コード例 #2
0
    def test_carbon_future(self):
        """Carbon: regression testing future scenario."""
        from natcap.invest import carbon
        args = {
            'workspace_dir': self.workspace_dir,
            'do_valuation': True,
            'price_per_metric_ton_of_c': 43.0,
            'rate_change': 2.8,
            'lulc_cur_year': 2016,
            'lulc_fut_year': 2030,
            'discount_rate': -7.1,
            'n_workers': -1,
        }

        lulc_names = ['lulc_cur_path', 'lulc_fut_path']
        for fill_val, lulc_name in enumerate(lulc_names, 1):
            args[lulc_name] = os.path.join(args['workspace_dir'],
                                           lulc_name + '.tif')
            make_simple_raster(args[lulc_name], fill_val, -1)

        args['carbon_pools_path'] = os.path.join(args['workspace_dir'],
                                                 'pools.csv')
        make_pools_csv(args['carbon_pools_path'])

        carbon.execute(args)
        # Add assertions for npv for the future scenario.
        # The npv was calculated based on _calculate_npv in carbon.py.
        assert_raster_equal_value(
            os.path.join(args['workspace_dir'], 'npv_fut.tif'), -0.3422078)
コード例 #3
0
    def test_carbon_missing_landcover_values(self):
        """Carbon: testing expected exception on missing LULC codes."""
        from natcap.invest import carbon
        args = {
            'workspace_dir': self.workspace_dir,
            'do_valuation': False,
            'n_workers': -1,
        }

        lulc_names = ['lulc_cur_path', 'lulc_fut_path']
        for fill_val, lulc_name in enumerate(lulc_names, 200):
            args[lulc_name] = os.path.join(args['workspace_dir'],
                                           lulc_name + '.tif')
            make_simple_raster(args[lulc_name], fill_val, -1)

        args['carbon_pools_path'] = os.path.join(args['workspace_dir'],
                                                 'pools.csv')
        make_pools_csv(args['carbon_pools_path'])

        # Value error should be raised with lulc code 200
        with self.assertRaises(ValueError) as cm:
            carbon.execute(args)

        self.assertTrue(
            "The missing values found in the LULC raster but not the table"
            " are: [200]" in str(cm.exception))
コード例 #4
0
    def test_carbon_zero_rates(self):
        """Carbon: test with 0 discount and rate change."""
        from natcap.invest import carbon

        args = {
            'workspace_dir': self.workspace_dir,
            'do_valuation': True,
            'price_per_metric_ton_of_c': 43.0,
            'rate_change': 0.0,
            'lulc_cur_year': 2016,
            'lulc_fut_year': 2030,
            'discount_rate': 0.0,
            'n_workers': -1,
        }

        # Create LULC rasters and pools csv in workspace and add them to args.
        lulc_names = ['lulc_cur_path', 'lulc_fut_path', 'lulc_redd_path']
        for fill_val, lulc_name in enumerate(lulc_names, 1):
            args[lulc_name] = os.path.join(args['workspace_dir'],
                                           lulc_name + '.tif')
            make_simple_raster(args[lulc_name], fill_val, -1)

        args['carbon_pools_path'] = os.path.join(args['workspace_dir'],
                                                 'pools.csv')
        make_pools_csv(args['carbon_pools_path'])

        carbon.execute(args)

        # Add assertions for npv for future and REDD scenarios.
        # The npv was calculated based on _calculate_npv in carbon.py.
        assert_raster_equal_value(
            os.path.join(args['workspace_dir'], 'npv_fut.tif'), -0.0178143)
        assert_raster_equal_value(
            os.path.join(args['workspace_dir'], 'npv_redd.tif'), -0.0239571)
コード例 #5
0
ファイル: test_carbon.py プロジェクト: hkotaro1215/invest
 def test_carbon_future_no_val(self):
     """Carbon: regression testing future scenario with no valuation."""
     from natcap.invest import carbon
     args = {
         u'carbon_pools_path': os.path.join(
             SAMPLE_DATA, 'carbon/carbon_pools_samp.csv'),
         u'lulc_cur_path': os.path.join(
             SAMPLE_DATA, 'Base_Data/Terrestrial/lulc_samp_cur'),
         u'lulc_fut_path': os.path.join(
             SAMPLE_DATA, 'Base_Data/Terrestrial/lulc_samp_fut'),
         u'workspace_dir': self.workspace_dir,
         u'do_valuation': True,
         u'price_per_metric_ton_of_c': 43.0,
         u'rate_change': 2.8,
         u'lulc_cur_year': 2016,
         u'lulc_fut_year': 2030,
         u'discount_rate': -7.1,
     }
     carbon.execute(args)
     CarbonTests._test_same_files(
         os.path.join(REGRESSION_DATA, 'file_list_fut_only.txt'),
         args['workspace_dir'])
     natcap.invest.pygeoprocessing_0_3_3.testing.assert_rasters_equal(
         os.path.join(REGRESSION_DATA, 'delta_cur_fut.tif'),
         os.path.join(self.workspace_dir, 'delta_cur_fut.tif'), 1e-6)
コード例 #6
0
ファイル: test_carbon.py プロジェクト: hkotaro1215/invest
 def test_carbon_missing_landcover_values(self):
     """Carbon: testing expected exception on incomplete  table."""
     from natcap.invest import carbon
     args = {
         u'carbon_pools_path': os.path.join(
             REGRESSION_DATA, 'carbon_pools_missing_coverage.csv'),
         u'lulc_cur_path': os.path.join(
             SAMPLE_DATA, 'Base_Data/Terrestrial/lulc_samp_cur'),
         u'lulc_fut_path': os.path.join(
             SAMPLE_DATA, 'Base_Data/Terrestrial/lulc_samp_fut'),
         u'workspace_dir': self.workspace_dir,
         u'do_valuation': False,
     }
     with self.assertRaises(ValueError):
         carbon.execute(args)
コード例 #7
0
    def test_carbon_zero_rates(self):
        """Carbon: test with 0 discount and rate change."""
        from natcap.invest import carbon

        args = {
            'workspace_dir': self.workspace_dir,
            'do_valuation': True,
            'price_per_metric_ton_of_c': 43.0,
            'rate_change': 0.0,
            'lulc_cur_year': 2016,
            'lulc_fut_year': 2030,
            'discount_rate': 0.0,
            'n_workers': -1,
        }

        # Create LULC rasters and pools csv in workspace and add them to args.
        lulc_names = ['lulc_cur_path', 'lulc_fut_path', 'lulc_redd_path']
        for fill_val, lulc_name in enumerate(lulc_names, 1):
            args[lulc_name] = os.path.join(args['workspace_dir'],
                                           lulc_name + '.tif')
            make_simple_raster(args[lulc_name], fill_val, -1)

        args['carbon_pools_path'] = os.path.join(args['workspace_dir'],
                                                 'pools.csv')
        make_pools_csv(args['carbon_pools_path'])

        carbon.execute(args)

        # Add assertions for npv for future and REDD scenarios.
        # carbon change from cur to fut: 
        # -58 Mg/ha * .0001 ha/pixel * 43 $/Mg = -0.2494 $/pixel
        assert_raster_equal_value(
            os.path.join(args['workspace_dir'], 'npv_fut.tif'), -0.2494)
        # carbon change from cur to redd: 
        # -78 Mg/ha * .0001 ha/pixel * 43 $/Mg = -0.3354 $/pixel
        assert_raster_equal_value(
            os.path.join(args['workspace_dir'], 'npv_redd.tif'), -0.3354)
args['calc_sequestration'] = False
args['carbon_pools_path'] = BioTable
args['do_redd'] = False
args['do_valuation'] = False
args['lulc_cur_path'] = LULC

# Seasonal Water Yield
args['workspace_dir'] = os.path.join(
    os.path.split(os.getcwd())[0], 'RESULTS', '02-Seasonal-Water-Yield')
swy.execute(args)

# Anual Water Yield
args['workspace_dir'] = os.path.join(
    os.path.split(os.getcwd())[0], 'RESULTS', '01-Anual-Water-Yield')
awy.execute(args)

# Sediment Delivery Ratio
args['workspace_dir'] = os.path.join(
    os.path.split(os.getcwd())[0], 'RESULTS', '03-Sediment-Delivery-Ratio')
sdr.execute(args)

# Nutrient Delivery Ratio
args['workspace_dir'] = os.path.join(
    os.path.split(os.getcwd())[0], 'RESULTS', '04-Nutrient-Delivery-Ratio')
ndr.execute(args)

# Carbons
args['workspace_dir'] = os.path.join(
    os.path.split(os.getcwd())[0], 'RESULTS', '05-Carbons')
carbon.execute(args)