"""Tutorial 12: Generate thermal infrared images using sun and shade leaf temperatures."""

import less


scene = less.Scene()
scene.size = 50.0

terrain = less.Terrain(property=less.Lambertian(reflectance=0.15))
terrain.set_property(less.ThermalProperty(
    emissivity=0.95,
    temperature_sunlit=312.0,
    temperature_shaded=303.0,
))
scene.terrain = terrain
scene.illumination = less.Illumination(source=less.Sun(zenith=30.0, azimuth=150.0), atmosphere=less.NoAtmosphere())

tree = less.Object("ash", mesh=less.examples.asset_path("FREX.obj"))
tree.set_property(
    "leaves", less.Prospect(cab=40, car=10, cw=0.012, cm=0.008, N=1.5))
tree.set_property("stem_branch", less.Lambertian(reflectance=0.08))
tree.set_property("leaves", less.ThermalProperty(
    emissivity=0.98,
    temperature_sunlit=305.0,
    temperature_shaded=300.0,
))
tree.set_property(
    "stem_branch", less.ThermalProperty(emissivity=0.97, temperature=300.0))

positions = less.place.random(scene, n=40, min_dist=4.0, seed=42)
n = len(positions)
scene.add(
    tree,
    positions=positions,
    scales=less.place.uniform_scale(n, 0.7, 1.3, seed=42),
    rotations=less.place.random_rotation(n, seed=42),
)
projection = less.Orthographic(image_size=512)
thermal = scene.simulate(less.ThermalImager(
    projection,
    bands=[10600],
    quality=128,
    mode="property",
))
thermal.save("12_thermal.png")
thermal.save("12_thermal.tif")

rgb = scene.simulate(less.OpticalImager(
    projection, bands=[650, 550, 450], quality=128))
rgb.save("12_rgb.png")

print(f"Thermal radiance range: {thermal.data.min():.2f} ~ {thermal.data.max():.2f}")
