Public
Edited
May 8
Insert cell
Insert cell
netcdf = import("https://cdn.skypack.dev/netcdfjs@3.0.0?min").then((d) => d.NetCDFReader)
Insert cell
// Need to load the data from google file so it will run successfully
googleTracerdata_year1990 = FileAttachment("google://tracerData_year1990.nc")
Insert cell
// Need to load the data from google file so it will run successfully
fileBuffer = googleTracerdata_year1990.arrayBuffer()
Insert cell
{
const buffer = fileBuffer;
const view = new DataView(buffer);
// Check magic number
const magic = new TextDecoder().decode(new Uint8Array(buffer.slice(0, 3)));
if (magic === "CDF") {
// NetCDF-3 format
const versionByte = view.getUint8(3);
return {
version: versionByte === 1 ? "NetCDF-3 Classic" :
versionByte === 2 ? "NetCDF-3 64-bit Offset" :
`NetCDF-3 Unknown (version byte: ${versionByte})`,
magicNumber: magic,
versionByte: versionByte
};
}
else if (magic.charCodeAt(0) === 0x89 &&
new TextDecoder().decode(new Uint8Array(buffer.slice(1, 4))) === "HDF") {
return {
version: "NetCDF-4/HDF5",
magicNumber: "HDF5 format"
};
}
else {
return {
version: "Unknown format",
header: Array.from(new Uint8Array(buffer.slice(0, 8))).join(' ')
};
}
}
Insert cell
dataset = {
const [NetCDFReader, buffer] = await Promise.all([netcdf, fileBuffer]);
const reader = new NetCDFReader(buffer);
// Get full arrays (contains all time steps concatenated)
const lonCellFull = reader.getDataVariable('lonCell');
const latCellFull = reader.getDataVariable('latCell');
// Extract first month's data (first 594,836 values)
const nCells = 594836;
const lonCell = lonCellFull.slice(0, nCells);
const latCell = latCellFull.slice(0, nCells);
return {
lonCell: lonCell, // Now contains only first month's data
latCell: latCell, // Now contains only first month's data
tracers: {
dyeBering: reader.getDataVariable('dyeBering'),
dyeBarents: reader.getDataVariable('dyeBarents'),
dyeFram: reader.getDataVariable('dyeFram'),
dyeDavis: reader.getDataVariable('dyeDavis'),
dyeFuryHecla: reader.getDataVariable('dyeFuryHecla')
},
dimensions: reader.header.dimensions
};
}
Insert cell
land = {
const d3 = require("d3@7");
const topojson = await import("https://cdn.skypack.dev/topojson-client@3");
const yourTopoJSON = await FileAttachment("land-50m.json").json();
console.log("TopoJSON objects:", Object.keys(yourTopoJSON.objects));
const landFeature = yourTopoJSON.objects.land
? topojson.feature(yourTopoJSON, yourTopoJSON.objects.land)
: topojson.feature(yourTopoJSON, yourTopoJSON.objects.countries); // fallback
return {
d3,
topojson,
land: landFeature
};
}
Insert cell
processedData = {
const nCells = 594836;
const month = 0;
// Extract data with relaxed thresholds
const processedData = {
lonCell: dataset.lonCell.slice(0, nCells),
latCell: dataset.latCell.slice(0, nCells),
tracers: {
dyeBering: dataset.tracers.dyeBering.slice(11, nCells)
}
};

// Find data range - use actual max instead of percentile
const maxVal = d3.max(processedData.tracers.dyeBering);
processedData.tracers.dyeBering = processedData.tracers.dyeBering.map(v => v/maxVal);

console.log("Adjusted data stats:", {
min: d3.min(processedData.tracers.dyeBering),
max: d3.max(processedData.tracers.dyeBering),
validPoints: processedData.tracers.dyeBering.filter(v => v > 0).length
});

return processedData;
}
Insert cell
{
const width = 800;
const height = 800;
const svg = d3.create("svg")
.attr("width", width)
.attr("height", height)
.style("background", "#e6f3ff"); // Ocean blue background

// 1. Arctic projection with adjusted parameters
const projection = d3.geoStereographic()
.rotate([0, -90]) // North Pole is the venter here
.scale(900)
.translate([width / 2, height / 2]);

const path = d3.geoPath(projection);

// 2. DRAW LAND
svg.append("path")
.datum(land.land)
.attr("d", path)
.attr("fill", "#cdc0b0")
.attr("stroke", "#333");

// 3. CANVAS SETUP
const canvas = d3.create("canvas")
.attr("width", width)
.attr("height", height)
.style("position", "absolute")
.style("left", "0px")
.style("top", "0px");
svg.node().appendChild(canvas.node());
const ctx = canvas.node().getContext("2d");

// 4. Pre-process non-zero points for better performance
const nonZeroPoints = [];
for (let i = 0; i < processedData.tracers.dyeBering.length; i++) {
const val = processedData.tracers.dyeBering[i];
if (val > 0.01) {
nonZeroPoints.push({
lon: processedData.lonCell[i],
lat: processedData.latCell[i],
value: val
});
}
}

// 5. Draw only non-zero points with value-based coloring
ctx.clearRect(0, 0, width, height);
const colorScale = d3.interpolateViridis;
nonZeroPoints.forEach(point => {
const [x, y] = projection([point.lon, point.lat]);
if (!isNaN(x)) {
// Size based on value (1-3px)
const radius = 20;
ctx.fillStyle = colorScale(0.5);
ctx.beginPath();
ctx.arc(x, y, radius, 0, 2 * Math.PI);
ctx.fill();
}
});

// 6. Add informative title
svg.append("text")
.attr("x", 10)
.attr("y", 20)
.text(`Bering Strait Tracer (${nonZeroPoints.length} non-zero points)`)
.style("font-size", "16px")
.style("font-weight", "bold");

// 7. Add color legend
const legend = svg.append("g")
.attr("transform", `translate(20,${height - 40})`);
// Gradient for legend
const gradient = legend.append("defs").append("linearGradient")
.attr("id", "gradient")
.attr("x1", "0%").attr("y1", "0%")
.attr("x2", "100%").attr("y2", "0%");
gradient.selectAll("stop")
.data(d3.range(0, 1.01, 0.1))
.enter().append("stop")
.attr("offset", d => `${d * 100}%`)
.attr("stop-color", d => colorScale(d));
legend.append("rect")
.attr("width", 200)
.attr("height", 20)
.attr("fill", "url(#gradient)");
legend.append("text")
.attr("x", 100)
.attr("y", 35)
.text("Tracer Concentration")
.style("font-size", "12px")
.style("text-anchor", "middle");

return svg.node();
}
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more