Skip to content

Commit d244fa1

Browse files
authored
netcdf example (#1351)
1 parent e523424 commit d244fa1

File tree

8 files changed

+161
-0
lines changed

8 files changed

+161
-0
lines changed

examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
### Charts
3535

36+
* [`netcdf`](https://observablehq.observablehq.cloud/framework-example-netcdf/) - Parsing NetCDF with `netcdfjs`, then visualizing with Observable Plot
3637
* [`vega-dark`](https://observablehq.observablehq.cloud/framework-example-vega-dark/) - Responsive dark mode in Vega-Lite
3738
* [`vega-responsive`](https://observablehq.observablehq.cloud/framework-example-vega-responsive/) - Responsive width in Vega-Lite using ResizeObserver
3839

examples/netcdf/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.DS_Store
2+
/dist/
3+
node_modules/
4+
yarn-error.log

examples/netcdf/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[Framework examples →](../)
2+
3+
# Framework + NetCDF
4+
5+
View live: <https://observablehq.observablehq.cloud/framework-example-netcdf/>
6+
7+
This Observable Framework example demonstrates how to use the [`netcdfjs` library](https://github.com/cheminfo/netcdfjs) to read a NetCDF file, and to display it as a raster plot using Observable Plot. The code is in [`src/index.md`](./src/index.md). The example data is global marine winds from NOAA’s Pacific Marine Environmental Laboratory.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
export default {
2+
root: "src",
3+
4+
// Shared Observable example configuration; feel free to remove this.
5+
title: "Observable Framework",
6+
pager: false,
7+
toc: false,
8+
sidebar: false,
9+
head:
10+
process.env.CI &&
11+
`<script type="module" async src="https://events.observablehq.com/client.js?pageLoad"></script>
12+
<script async src="https://www.googletagmanager.com/gtag/js?id=G-9B88TP6PKQ"></script>
13+
<script>window.dataLayer=window.dataLayer||[];\nfunction gtag(){dataLayer.push(arguments);}\ngtag('js',new Date());\ngtag('config','G-9B88TP6PKQ');</script>`,
14+
header: `<style>
15+
16+
#observablehq-header a[href] {
17+
color: inherit;
18+
}
19+
20+
#observablehq-header a[target="_blank"] {
21+
display: flex;
22+
align-items: center;
23+
gap: 0.25rem;
24+
text-decoration: none;
25+
}
26+
27+
#observablehq-header a[target="_blank"]:hover span {
28+
text-decoration: underline;
29+
}
30+
31+
#observablehq-header a[target="_blank"]::after {
32+
content: "\\2197";
33+
}
34+
35+
#observablehq-header a[target="_blank"]:not(:hover, :focus)::after {
36+
color: var(--theme-foreground-muted);
37+
}
38+
39+
@container not (min-width: 640px) {
40+
.hide-if-small {
41+
display: none;
42+
}
43+
}
44+
45+
</style>
46+
<div style="display: flex; align-items: center; gap: 0.5rem; height: 2.2rem; margin: -1.5rem -2rem 2rem -2rem; padding: 0.5rem 2rem; border-bottom: solid 1px var(--theme-foreground-faintest); font: 500 16px var(--sans-serif);">
47+
<a href="https://observablehq.com/" target="_self" rel="" style="display: flex; align-items: center;">
48+
<svg width="22" height="22" viewBox="0 0 21.92930030822754 22.68549919128418" fill="currentColor">
49+
<path d="M10.9646 18.9046C9.95224 18.9046 9.07507 18.6853 8.33313 18.2467C7.59386 17.8098 7.0028 17.1909 6.62722 16.4604C6.22789 15.7003 5.93558 14.8965 5.75735 14.0684C5.56825 13.1704 5.47613 12.2574 5.48232 11.3427C5.48232 10.6185 5.52984 9.92616 5.62578 9.26408C5.7208 8.60284 5.89715 7.93067 6.15391 7.24843C6.41066 6.56618 6.74143 5.97468 7.14438 5.47308C7.56389 4.9592 8.1063 4.54092 8.72969 4.25059C9.38391 3.93719 10.1277 3.78091 10.9646 3.78091C11.977 3.78091 12.8542 4.00021 13.5962 4.43879C14.3354 4.87564 14.9265 5.49454 15.3021 6.22506C15.6986 6.97704 15.9883 7.7744 16.1719 8.61712C16.3547 9.459 16.447 10.3681 16.447 11.3427C16.447 12.067 16.3995 12.7593 16.3035 13.4214C16.2013 14.1088 16.0206 14.7844 15.7644 15.437C15.4994 16.1193 15.1705 16.7108 14.7739 17.2124C14.3774 17.714 13.8529 18.1215 13.1996 18.4349C12.5463 18.7483 11.8016 18.9046 10.9646 18.9046ZM12.8999 13.3447C13.4242 12.8211 13.7159 12.0966 13.7058 11.3427C13.7058 10.5639 13.4436 9.89654 12.92 9.34074C12.3955 8.78495 11.7441 8.50705 10.9646 8.50705C10.1852 8.50705 9.53376 8.78495 9.00928 9.34074C8.49569 9.87018 8.21207 10.5928 8.22348 11.3427C8.22348 12.1216 8.48572 12.7889 9.00928 13.3447C9.53376 13.9005 10.1852 14.1784 10.9646 14.1784C11.7441 14.1784 12.3891 13.9005 12.8999 13.3447ZM10.9646 22.6855C17.0199 22.6855 21.9293 17.6068 21.9293 11.3427C21.9293 5.07871 17.0199 0 10.9646 0C4.90942 0 0 5.07871 0 11.3427C0 17.6068 4.90942 22.6855 10.9646 22.6855Z"></path>
50+
</svg>
51+
</a>
52+
<div style="display: flex; flex-grow: 1; justify-content: space-between; align-items: baseline;">
53+
<a href="https://observablehq.com/framework/" target="_self" rel="">
54+
<span class="hide-if-small">Observable</span> Framework
55+
</a>
56+
<span style="display: flex; align-items: baseline; gap: 0.5rem; font-size: 14px;">
57+
<a target="_blank" href="https://github.com/observablehq/framework/tree/main/examples/netcdf"><span>View source</span></a>
58+
</span>
59+
</div>
60+
</div>`
61+
};

examples/netcdf/package.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"type": "module",
3+
"private": true,
4+
"scripts": {
5+
"clean": "rimraf src/.observablehq/cache",
6+
"build": "rimraf dist && observable build",
7+
"dev": "observable preview",
8+
"deploy": "observable deploy",
9+
"observable": "observable"
10+
},
11+
"dependencies": {
12+
"@observablehq/framework": "^1.7.1"
13+
},
14+
"devDependencies": {
15+
"rimraf": "^5.0.5"
16+
},
17+
"engines": {
18+
"node": ">=18"
19+
}
20+
}

examples/netcdf/src/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/.observablehq/cache/

examples/netcdf/src/index.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# NetCDF
2+
3+
This example demonstrates how to read a NetCDF file using [`netcdfjs`](https://github.com/cheminfo/netcdfjs) and visualize it with Observable Plot’s [raster mark](https://observablehq.com/plot/marks/raster). The data is from [NOAA’s Pacific Marine Environmental Laboratory](https://ferret.pmel.noaa.gov/Ferret/documentation/users-guide/introduction/SAMPLE-DATA-SETS), by way of [Patrick Brockmann](https://github.com/PBrockmann/D3_netcdfjs), and represents global marine winds for the period 1982–1990.
4+
5+
To start, let’s import `NetCDFReader` from `netcdfjs`:
6+
7+
```js echo
8+
import {NetCDFReader} from "npm:netcdfjs";
9+
```
10+
11+
Then let’s declare a `FileAttachment` for the NetCDF file and load it as an `ArrayBuffer` (since it’s binary). `FileAttachment.arrayBuffer` returns a `Promise`, which we chain with the `NetCDFReader` constructor.
12+
13+
```js echo
14+
const winds = FileAttachment("navy_winds_2.nc").arrayBuffer().then((data) => new NetCDFReader(data));
15+
```
16+
17+
Now `winds` is a promise to a `NetCDFReader`. (Promises are implicitly awaited across code blocks, so we don’t need to explicitly `await` below.)
18+
19+
We can inspect the metadata of the NetCDF file via the `header` property. The `header.dimensions` tells us the grid resolution, while the `header.variables` tells us what values are stored in the grid. `UWND` is zonal wind, while `VWND` is meridional wind; that’s latitudinal and longitudinal, respectively.
20+
21+
```js echo
22+
winds.header
23+
```
24+
25+
Now let’s visualize the zonal wind using a raster plot. The values are represented as a one-dimensional array of numbers, which we can use as the raster mark’s data; but we also need to specify the `width` and `height` of the grid. Since wind values can be both positive and negative, we can use the `rdbu` diverging color scheme.
26+
27+
```js echo
28+
Plot.plot({
29+
color: {
30+
label: "UWND",
31+
legend: true,
32+
scheme: "rdbu"
33+
},
34+
marks: [
35+
Plot.raster(winds.getDataVariable("UWND"), {
36+
width: winds.header.dimensions[0].size,
37+
height: winds.header.dimensions[1].size
38+
})
39+
]
40+
})
41+
```
42+
43+
We can use [Plot’s projection system](https://observablehq.com/plot/features/projections) to apply a suitable global projection. Below we use the Equal Earth projection, an equal-area projection preserving the relative size of areas. The `x1`, `y1`, `x2`, and `y2` options specify the bounds of the grid in geographic coordinates, while the `clip` option clips the data to the globe.
44+
45+
```js echo
46+
Plot.plot({
47+
projection: "equal-earth",
48+
color: {
49+
label: "UWND",
50+
legend: true,
51+
scheme: "rdbu"
52+
},
53+
marks: [
54+
Plot.raster(winds.getDataVariable("UWND"), {
55+
width: winds.header.dimensions[0].size,
56+
height: winds.header.dimensions[1].size,
57+
x1: -180,
58+
y1: -90,
59+
x2: 180,
60+
y2: 90,
61+
interpolate: "barycentric",
62+
clip: "sphere"
63+
}),
64+
Plot.graticule({stroke: "black"})
65+
]
66+
})
67+
```

examples/netcdf/src/navy_winds_2.nc

512 KB
Binary file not shown.

0 commit comments

Comments
 (0)