Timber bucking demo

This notebook demonstrates timber objects, tapers, pricelists, the Nasberg bucking algorithm and how solution cubes store many results in xarray.

[1]:
from pyforestry.base.pricelist import create_pricelist_from_data
from pyforestry.base.pricelist.data.mellanskog_2013 import Mellanskog_2013_price_data
from pyforestry.base.pricelist.solutioncube import SolutionCube
from pyforestry.base.timber_bucking.nasberg_1985 import BuckingConfig, Nasberg_1985_BranchBound
from pyforestry.sweden.taper import EdgrenNylinder1949
from pyforestry.sweden.timber.swe_timber import SweTimber

Create timber object

[2]:
timber = SweTimber(species='pinus sylvestris', diameter_cm=18, height_m=25)
timber
[2]:
<pyforestry.sweden.timber.swe_timber.SweTimber at 0x7f4f47bea600>

Load pricelist

[3]:
pricelist = create_pricelist_from_data(Mellanskog_2013_price_data, species_to_load='pinus sylvestris')
pricelist.Timber.keys()
[3]:
dict_keys(['pinus sylvestris'])

Run bucking optimizer

[4]:
optimizer = Nasberg_1985_BranchBound(timber, pricelist, EdgrenNylinder1949)
result = optimizer.calculate_tree_value(min_diam_dead_wood=99, config=BuckingConfig(save_sections=True))
result
[4]:
BuckingResult(species_group='pinus sylvestris', total_value=np.float32(111.70413), top_proportion=0.006394778317476707, dead_wood_proportion=0.0, high_stump_volume_proportion=0.0, high_stump_value_proportion=0.0, last_cut_relative_height=np.float64(0.894), volume_per_quality=[0.0, 0.1061747827409899, 0.10089169267225448, 0.1242397819277461, 0.0, 0.0, 0.0], timber_price_by_quality=[0.0, np.float32(374.95026), np.float32(395.25204), np.float32(131.40779), 0.0, 0.0, 0.0], vol_fub_5cm=0.3312953029462704, vol_sk_ub=0.333427497880165, DBH_cm=np.float64(18.000000000000004), height_m=25, stump_height_m=0.25, diameter_stump_cm=np.float64(20.503363467016907), taperDiams_cm=[20.50336265563965, 19.994277954101562, 19.61321449279785, 19.30855369567871, 19.054716110229492, 18.83714485168457, 18.646760940551758, 18.47751808166504, 18.325185775756836, 18.186691284179688, 18.059728622436523, 17.942522048950195, 17.833681106567383, 17.732088088989258, 17.6368408203125, 17.547189712524414, 17.46251678466797, 17.382295608520508, 17.306081771850586, 17.233495712280273, 17.16420555114746, 17.097925186157227, 17.034404754638672, 16.97342300415039, 16.914785385131836, 16.858318328857422, 16.803865432739258, 16.751291275024414, 16.70046615600586, 16.651281356811523, 16.6036319732666, 16.557424545288086, 16.512577056884766, 16.46900749206543, 16.426651000976562, 16.38543701171875, 16.345306396484375, 16.306203842163086, 16.26807975769043, 16.230884552001953, 16.1945743560791, 16.159109115600586, 16.124448776245117, 16.090559005737305, 16.057403564453125, 16.02495574951172, 15.993182182312012, 15.962057113647461, 15.931553840637207, 15.90164852142334, 15.872318267822266, 15.843542098999023, 15.81529712677002, 15.787566184997559, 15.760330200195312, 15.727702140808105, 15.703722953796387, 15.679622650146484, 15.655400276184082, 15.631054878234863, 15.606584548950195, 15.581988334655762, 15.557265281677246, 15.532414436340332, 15.50743293762207, 15.482320785522461, 15.457075119018555, 15.431696891784668, 15.406182289123535, 15.380531311035156, 15.354741096496582, 15.328811645507812, 15.302741050720215, 15.276528358459473, 15.25016975402832, 15.223666191101074, 15.197013854980469, 15.170212745666504, 15.143260955810547, 15.116156578063965, 15.088896751403809, 15.061481475830078, 15.033907890319824, 15.006174087524414, 14.978279113769531, 14.950220108032227, 14.921995162963867, 14.893603324890137, 14.865041732788086, 14.836308479309082, 14.807401657104492, 14.778319358825684, 14.749058723449707, 14.719618797302246, 14.689995765686035, 14.660187721252441, 14.630193710327148, 14.60000991821289, 14.569634437561035, 14.539064407348633, 14.508298873901367, 14.477333068847656, 14.446166038513184, 14.414793968200684, 14.38321590423584, 14.35142707824707, 14.319425582885742, 14.287208557128906, 14.254773139953613, 14.222116470336914, 14.18923568725586, 14.156126976013184, 14.122786521911621, 14.089213371276855, 14.055401802062988, 14.021349906921387, 13.987054824829102, 13.952510833740234, 13.917716026306152, 13.882665634155273, 13.847357749938965, 13.811785697937012, 13.775948524475098, 13.739839553833008, 13.70345687866211, 13.666794776916504, 13.629850387573242, 13.59261703491211, 13.555092811584473, 13.517271041870117, 13.479147911071777, 13.440718650817871, 13.401978492736816, 13.362921714782715, 13.323542594909668, 13.28383731842041, 13.243800163269043, 13.203424453735352, 13.162704467773438, 13.121635437011719, 13.08021068572998, 13.038424491882324, 12.996269226074219, 12.953739166259766, 12.91082763671875, 12.867527961730957, 12.823832511901855, 12.77973461151123, 12.722068786621094, 12.649435997009277, 12.576272964477539, 12.502571105957031, 12.428321838378906, 12.353517532348633, 12.27815055847168, 12.202211380004883, 12.125691413879395, 12.048582077026367, 11.970873832702637, 11.892557144165039, 11.813623428344727, 11.734062194824219, 11.653862953186035, 11.573016166687012, 11.491511344909668, 11.40933609008789, 11.326481819152832, 11.242935180664062, 11.158685684204102, 11.07371997833252, 10.988027572631836, 10.901595115661621, 10.814409255981445, 10.726457595825195, 10.637725830078125, 10.548199653625488, 10.457866668701172, 10.366710662841797, 10.2747163772583, 10.181868553161621, 10.088150978088379, 9.993548393249512, 9.898042678833008, 9.801615715026855, 9.704252243041992, 9.605931282043457, 9.506634712219238, 9.406343460083008, 9.305037498474121, 9.2026948928833, 9.099294662475586, 8.994815826416016, 8.889233589172363, 8.782526969909668, 8.67466926574707, 8.56563663482666, 8.455403327941895, 8.343942642211914, 8.231225967407227, 8.117225646972656, 8.001911163330078, 7.885253429412842, 7.767219543457031, 7.6477766036987305, 7.526891231536865, 7.40452766418457, 7.280649185180664, 7.155218601226807, 7.028195858001709, 6.899539947509766, 6.769208908081055, 6.637157917022705, 6.503341197967529, 6.367711067199707, 6.230217933654785, 6.090808868408203, 5.9494309425354, 5.806026935577393, 5.660538196563721, 5.512903213500977, 5.363057613372803, 5.210934162139893], taperHeights_m=[0.25, 0.35, 0.45, 0.55, 0.65, 0.75, 0.8500000000000001, 0.9500000000000001, 1.05, 1.15, 1.25, 1.35, 1.4500000000000002, 1.55, 1.6500000000000001, 1.75, 1.85, 1.9500000000000002, 2.05, 2.1500000000000004, 2.25, 2.35, 2.45, 2.5500000000000003, 2.6500000000000004, 2.75, 2.85, 2.95, 3.0500000000000003, 3.1500000000000004, 3.25, 3.35, 3.45, 3.5500000000000003, 3.6500000000000004, 3.75, 3.85, 3.95, 4.050000000000001, 4.15, 4.25, 4.3500000000000005, 4.45, 4.55, 4.65, 4.75, 4.8500000000000005, 4.95, 5.050000000000001, 5.15, 5.25, 5.3500000000000005, 5.45, 5.550000000000001, 5.65, 5.75, 5.8500000000000005, 5.95, 6.050000000000001, 6.15, 6.25, 6.3500000000000005, 6.45, 6.550000000000001, 6.65, 6.75, 6.8500000000000005, 6.95, 7.050000000000001, 7.15, 7.25, 7.3500000000000005, 7.45, 7.550000000000001, 7.65, 7.75, 7.8500000000000005, 7.95, 8.05, 8.15, 8.25, 8.35, 8.450000000000001, 8.55, 8.65, 8.75, 8.85, 8.950000000000001, 9.05, 9.15, 9.25, 9.35, 9.450000000000001, 9.55, 9.65, 9.75, 9.850000000000001, 9.950000000000001, 10.05, 10.15, 10.25, 10.350000000000001, 10.450000000000001, 10.55, 10.65, 10.75, 10.850000000000001, 10.950000000000001, 11.05, 11.15, 11.25, 11.350000000000001, 11.450000000000001, 11.55, 11.65, 11.75, 11.850000000000001, 11.950000000000001, 12.05, 12.15, 12.25, 12.350000000000001, 12.450000000000001, 12.55, 12.65, 12.75, 12.850000000000001, 12.950000000000001, 13.05, 13.15, 13.25, 13.350000000000001, 13.450000000000001, 13.55, 13.65, 13.75, 13.850000000000001, 13.950000000000001, 14.05, 14.15, 14.25, 14.350000000000001, 14.450000000000001, 14.55, 14.65, 14.75, 14.850000000000001, 14.950000000000001, 15.05, 15.15, 15.25, 15.350000000000001, 15.450000000000001, 15.55, 15.65, 15.75, 15.850000000000001, 15.950000000000001, 16.05, 16.15, 16.25, 16.35, 16.45, 16.55, 16.650000000000002, 16.75, 16.85, 16.95, 17.05, 17.150000000000002, 17.25, 17.35, 17.45, 17.55, 17.650000000000002, 17.75, 17.85, 17.95, 18.05, 18.150000000000002, 18.25, 18.35, 18.45, 18.55, 18.650000000000002, 18.75, 18.85, 18.95, 19.05, 19.150000000000002, 19.25, 19.35, 19.450000000000003, 19.55, 19.650000000000002, 19.75, 19.85, 19.950000000000003, 20.05, 20.150000000000002, 20.25, 20.35, 20.450000000000003, 20.55, 20.650000000000002, 20.75, 20.85, 20.950000000000003, 21.05, 21.150000000000002, 21.25, 21.35, 21.450000000000003, 21.55, 21.650000000000002, 21.75, 21.85, 21.950000000000003, 22.05, 22.150000000000002, 22.25, 22.35], sections=[CrossCutSection(start_point=np.int16(100), end_point=221, volume=0.1242397819277461, top_diameter=np.float32(5.210934), value=np.float32(32.01622), species_group='pinus sylvestris', timber_proportion=1.0, pulp_proportion=0.0, cull_proportion=0.0, fuelwood_proportion=0.0, quality=<QualityType.TopLog: 3>), CrossCutSection(start_point=np.int16(45), end_point=np.int16(100), volume=0.10089169267225448, top_diameter=np.float32(14.508299), value=np.float32(39.877647), species_group='pinus sylvestris', timber_proportion=1.0, pulp_proportion=0.0, cull_proportion=0.0, fuelwood_proportion=0.0, quality=<QualityType.MiddleLog: 2>), CrossCutSection(start_point=np.int16(0), end_point=np.int16(45), volume=0.1061747827409899, top_diameter=np.float32(16.024956), value=np.float32(39.81026), species_group='pinus sylvestris', timber_proportion=1.0, pulp_proportion=0.0, cull_proportion=0.0, fuelwood_proportion=0.0, quality=<QualityType.ButtLog: 1>)])
[5]:
result.plot()
../_images/notebooks_timber_bucking_demo_9_0.png

Generate a small solution cube

[6]:
cube = SolutionCube.generate(
    pricelist_data=Mellanskog_2013_price_data,
    taper_model=EdgrenNylinder1949,
    species_list=['picea abies'],
    dbh_range=(20,22),
    height_range=(15,15.2),
    dbh_step=2,
    height_step=0.2,
    workers=1
)
cube.dataset
Generating Solution Cube using 1 parallel processes...
Pricelist hash: 4058af6a860784c4a6ce94f561a4f484a0401624c2f3cc8405c3074570164174
Total trees to process: 4
Generating Solution Cube: 100%|██████████| 4/4 [00:00<00:00, 626.81it/s]

Finished parallel computation in 1.23 seconds.
Successfully created xarray Dataset.

[6]:
<xarray.Dataset> Size: 88B
Dimensions:            (species: 1, height: 2, dbh: 2)
Coordinates:
  * species            (species) object 8B 'picea abies'
  * height             (height) float64 16B 15.0 15.2
  * dbh                (dbh) int64 16B 20 22
Data variables:
    total_value        (species, height, dbh) float32 16B 55.12 69.41 ... 70.02
    solution_sections  (species, height, dbh) object 32B '[{"start_point": 80...
Attributes:
    pricelist_hash:     4058af6a860784c4a6ce94f561a4f484a0401624c2f3cc8405c30...
    taper_model:        EdgrenNylinder1949
    creation_date_utc:  2025-07-19T11:07:21.979503+00:00
    dbh_range:          20-22 cm
    height_range:       15-15.2 m
[7]:
cube.lookup(species='picea abies', dbh=20.0, height=15.0)
[7]:
(55.121742248535156,
 [{'start_point': 80,
   'end_point': 115,
   'volume': 0.016191082232439986,
   'top_diameter': 5.185598373413086,
   'value': 4.290634632110596,
   'species_group': 'picea abies',
   'timber_proportion': 1.0,
   'pulp_proportion': 0.0,
   'cull_proportion': 0.0,
   'fuelwood_proportion': 0.0,
   'quality': 3},
  {'start_point': 52,
   'end_point': 80,
   'volume': 0.032022008100272975,
   'top_diameter': 9.956061363220215,
   'value': 8.485830307006836,
   'species_group': 'picea abies',
   'timber_proportion': 1.0,
   'pulp_proportion': 0.0,
   'cull_proportion': 0.0,
   'fuelwood_proportion': 0.0,
   'quality': 2},
  {'start_point': 0,
   'end_point': 52,
   'volume': 0.1362180790952966,
   'top_diameter': 14.06132698059082,
   'value': 42.345279693603516,
   'species_group': 'picea abies',
   'timber_proportion': 1.0,
   'pulp_proportion': 0.0,
   'cull_proportion': 0.0,
   'fuelwood_proportion': 0.0,
   'quality': 1}])