Showcase · examples/urban_heat_island.lms

Urban heat island field

Meteorology / Heatmaps: 24×24 km nocturnal urban heat island anomaly field Δ T(x, y) (with image) paired with West–East and South–North thermal dome transects and river cooling dip

Loading the WebAssembly host…

What runs

The page fetches examples/urban_heat_island.bin, 7,733 bytes of lmcode bytecode (sha256 429d4595e798…), and hands it to the same lmplot.wasm every example uses. The bytecode calls the host's Plot functions (plot.cmd, plot.data, plot.num, plot.png …); gnuplot draws into an in-memory file system and the page shows the files the program wrote.

In SVG mode plot.png("x.png", w, h) writes x.svg with gnuplot's svg terminal; PNG mode uses lmplot's own rasterizer and fonts, the same bytes as the native binary.

The program was compiled ahead of time from the source on the right with lmscript; the lmscript lines around the _lmcode_ block (data generation, ${...} values, run_lmcode) ran at compile time.

# Urban Heat Island (UHI) thermal anomaly field across a 24x24 km metropolitan
# basin (downtown core, industrial corridor, river cooling channel, and park),
# paired with West-East and South-North cross-section transects.
# Writes images/urban_heat_island.png (900x560).
mkdir("images");
mkdir("build");
p = _lmcode_ cli="lmplot" install="github:lmctl-ai/lmplot@v0.3.1";

fn exp_neg(u: float) -> float {
  return pow(2.718281828459045, 0.0 - u);
}

# Build a 61x61 grid over [-12, +12] km (0.4 km cells):
let grid_rows = "";
let we_rows = "";
let sn_rows = "";
for (iy in range(0, 61)) {
  let y = -12.0 + 24.0 * float(iy) / 60.0;
  for (ix in range(0, 61)) {
    let x = -12.0 + 24.0 * float(ix) / 60.0;
    let r2_core = (x * x + 1.2 * y * y) / 18.0;
    let r2_ind = ((x - 4.5) * (x - 4.5) + (y + 2.5) * (y + 2.5)) / 10.0;
    let r2_sub = (x * x + y * y) / 65.0;
    let d_river = (y - 0.35 * x + 2.0);
    let r2_river = (d_river * d_river) / 1.4;
    let r2_park = ((x + 4.0) * (x + 4.0) + (y - 3.5) * (y - 3.5)) / 5.5;
    let urban = 4.4 * exp_neg(r2_core) + 2.5 * exp_neg(r2_ind) + 1.4 * exp_neg(r2_sub);
    # The river cools its banks and damps the urban excess it flows through.
    let river = exp_neg(r2_river);
    let dt = urban * (1.0 - 0.55 * river) - 1.2 * river - 1.65 * exp_neg(r2_park);
    grid_rows = grid_rows + str(x) + " " + str(y) + " " + str(dt) + "\n";
    if (iy == 30) {
      we_rows = we_rows + str(x) + " " + str(dt) + "\n";
    }
    if (ix == 30) {
      sn_rows = sn_rows + str(y) + " " + str(dt) + "\n";
    }
  }
  grid_rows = grid_rows + "\n";
}
plot.data("UHI", grid_rows);
plot.data("WE", we_rows);
plot.data("SN", sn_rows);

plot.cmd("set terminal unknown");
plot.cmd("stats $UHI using 3 nooutput");
let dt_max = plot.num("STATS_max");
let dt_min = plot.num("STATS_min");
let dt_mean = plot.num("STATS_mean");
plot.echo("Peak downtown anomaly: +" + str(dt_max) + " C");
plot.echo("Park/river minimum:    " + str(dt_min) + " C");
plot.echo("Basin mean anomaly:    +" + str(dt_mean) + " C");
plot.cmd("stats $WE using 2 nooutput");
let we_min = plot.num("STATS_min");
let we_min_x = plot.num("STATS_index_min");

plot.png("images/urban_heat_island.png", 900, 560);
plot.cmd("set encoding utf8");
plot.cmd("set multiplot");

# Left panel: 2D Urban Heat Island map
plot.script("set lmargin at screen 0.075\nset rmargin at screen 0.475\nset tmargin at screen 0.86\nset bmargin at screen 0.14");
plot.cmd("set colorbox user origin screen 0.49, 0.14 size screen 0.022, 0.72");
plot.cmd("set title 'Nocturnal Urban Heat Island ΔT(x, y) [°C]' font ',11.5'");
plot.cmd("set xlabel 'East–West Distance x (km)'");
plot.cmd("set ylabel 'South–North Distance y (km)'");
plot.cmd("set xrange [-12.2:12.2]");
plot.cmd("set yrange [-12.2:12.2]");
plot.cmd("set cbrange [-1.0:5.8]");
plot.cmd("set cblabel 'ΔT above rural baseline (°C)' offset 0.8,0");
plot.cmd("set palette defined (0 '#1e3a8a', 0.22 '#38bdf8', 0.42 '#fef08a', 0.70 '#f97316', 1 '#991b1b')");
plot.cmd("unset key");
plot.cmd("set arrow 1 from -11.6, 0 to 11.6, 0 nohead dt 2 lw 1.3 lc rgb '#ffffff' front");
plot.cmd("set arrow 2 from 0, -11.6 to 0, 11.6 nohead dt 3 lw 1.3 lc rgb '#ffffff' front");
plot.cmd("set label 1 sprintf('CBD core\n+%.1f°C', " + str(dt_max) + ") at 0.6, 2.6 left front tc rgb '#ffffff' font ',9'");
plot.cmd("set label 2 'Park' at -4.0, 5.6 center front tc rgb '#0f172a' font ',8.5'");
plot.cmd("set label 3 'Industrial' at 4.5, -2.8 center front tc rgb '#ffffff' font ',8.5'");
plot.cmd("set label 5 'River' at -9.5, -6.2 center front rotate by 19 tc rgb '#ffffff' font ',8.5'");
plot.cmd("plot $UHI using 1:2:3 with image");

# Right panel: West-East and South-North cross-section transects
plot.cmd("unset arrow 1");
plot.cmd("unset arrow 2");
plot.cmd("unset label 1");
plot.cmd("unset label 2");
plot.cmd("unset label 3");
plot.cmd("unset label 5");
plot.script("set lmargin at screen 0.645\nset rmargin at screen 0.98\nset tmargin at screen 0.86\nset bmargin at screen 0.14");
plot.cmd("set title 'Transects Through the CBD' font ',11.5'");
plot.cmd("set xlabel 'Transect Coordinate (km from CBD)'");
plot.cmd("set ylabel 'Temperature Anomaly ΔT (°C)'");
plot.cmd("set xrange [-12:12]");
plot.cmd("set yrange [-2:8]");
plot.cmd("set grid xtics ytics lc rgb '#e2e8f0' lw 1");
plot.cmd("set key top right width -4 samplen 1.6 spacing 1.1 opaque box lc rgb '#cbd5e1' font ',8.5'");
plot.cmd("set style fill transparent solid 0.20 noborder");

let stat_txt = plot.str("sprintf('peak  +%.1f °C\nmean  +%.2f °C\nriver (W–E) %.1f °C\n  at x = %.1f km\ncoolest  %.1f °C', " + str(dt_max) + ", " + str(dt_mean) + ", " + str(we_min) + ", " + str(-12.0 + 0.4 * we_min_x) + ", " + str(dt_min) + ")");
plot.cmd("set label 4 '" + stat_txt + "' at -11.4, 7.6 left front boxed noenhanced font ',8'");

plot.cmd("plot $WE using 1:2 with filledcurves y1=0 lc rgb '#ef4444' notitle, $WE using 1:2 with lines lw 2.5 lc rgb '#dc2626' title 'W–E, y = 0', $SN using 1:2 with lines lw 2.2 dt 2 lc rgb '#2563eb' title 'S–N, x = 0', " + str(dt_mean) + " with lines dt 3 lw 1.3 lc rgb '#475569' title 'basin mean'");

plot.cmd("unset multiplot");
plot.close();
end _lmcode_
r = run_lmcode(p);
assert(r.ok, json_stringify(r));
println("wrote images/urban_heat_island.png");