-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathskewTlogP.tsx
More file actions
235 lines (213 loc) · 6.39 KB
/
skewTlogP.tsx
File metadata and controls
235 lines (213 loc) · 6.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// Code modified from https://github.com/rsobash/d3-skewt/ (MIT license)
import * as d3 from "d3";
import { For, Show, createEffect, createSignal } from "solid-js";
import { createStore } from "solid-js/store";
import { AxisBottom, AxisLeft } from "./Axes";
import type { ChartData, SupportedScaleTypes } from "./ChartContainer";
import {
Chart,
ChartContainer,
highlight,
useChartContext,
} from "./ChartContainer";
import { Legend } from "./Legend";
export interface SoundingRecord {
p: number;
T: number;
Td: number;
}
const deg2rad = Math.PI / 180;
const tan = Math.tan(55 * deg2rad);
function getTempAtCursor(x: number, y: number, scaleY: SupportedScaleTypes) {
const basep = () => scaleY.domain()[0];
return x - (scaleY(basep()) - y) / tan;
}
export function getXPixelFromTemp(
T: number,
p: number,
scaleY: SupportedScaleTypes,
) {
const basep = () => scaleY.domain()[0];
return T + (scaleY(basep()) - scaleY(p)) / tan;
}
function SkewTGridLine(temperature: number) {
const [chart, updateChart] = useChartContext();
const x = (temp: number) => chart.scaleX(temp);
const y = (pres: number) => chart.scaleY(pres);
const basep = () => chart.scaleY.domain()[0];
const topPressure = () => chart.scaleY.domain()[1];
return (
<line
x1={x(temperature) + (y(basep()) - y(topPressure())) / tan}
x2={x(temperature)}
y1="0"
y2={chart.innerHeight}
clip-path="url(#clipper)"
stroke="#dfdfdf"
stroke-width="0.75px"
fill="none"
/>
);
}
function LogPGridLine(pressure: number) {
const [chart, updateChart] = useChartContext();
const x = (temp: number) => chart.scaleX(temp);
const y = (pres: number) => chart.scaleY(pres);
return (
<line
x1="0"
x2={chart.innerWidth}
y1={y(pressure)}
y2={y(pressure)}
stroke="#dfdfdf"
stroke-width="0.75px"
fill="none"
clip-path="url(#clipper)"
/>
);
}
/** Dry adiabats (lines of constant potential temperature): array of lines of [p, T] */
function DryAdiabat(d: [number, number][]) {
const [chart, updateChart] = useChartContext();
const x = (temp: number) => chart.scaleX(temp);
const y = (pres: number) => chart.scaleY(pres);
const basep = () => chart.scaleY.domain()[0];
const dryline = d3
.line()
.x(
(d) =>
x((273.15 + d[1]) / (1000 / d[0]) ** 0.286 - 273.15) +
(y(basep()) - y(d[0])) / tan,
)
.y((d) => y(d[0]));
return (
<path
d={dryline(d) || ""}
clip-path="url(#clipper)"
stroke="#dfdfdf"
stroke-width="0.75px"
fill="none"
/>
);
}
function Sounding(data: ChartData<SoundingRecord>) {
const [chart, updateChart] = useChartContext();
const [hovered, setHovered] = createSignal(false);
// Scales and axes. Note the inverted domain for the y-scale: bigger is up!
const x = (temp: number) => chart.scaleX(temp);
const y = (pres: number) => chart.scaleY(pres);
const basep = () => chart.scaleY.domain()[0];
const temperatureLine = d3
.line<SoundingRecord>()
.x((d) => x(d.T - 273.15) + (y(basep()) - y(d.p)) / tan)
.y((d) => y(d.p));
const dewpointLine = d3
.line<SoundingRecord>()
.x((d) => x(d.Td - 273.15) + (y(basep()) - y(d.p)) / tan)
.y((d) => y(d.p));
const titleT = () => `${data.label} T`;
const titleTd = () => `${data.label} Td`;
const stroke = () => (hovered() ? highlight(data.color) : data.color);
return (
<g
onMouseEnter={() => setHovered(true)}
onMouseLeave={() => setHovered(false)}
>
<path
d={temperatureLine(data.data) || ""}
clip-path="url(#clipper)"
stroke={stroke()}
stroke-dasharray={data.linestyle}
stroke-width={2}
fill="none"
class="cursor-pointer"
>
<title>{titleT()}</title>
</path>
<path
d={dewpointLine(data.data) || ""}
clip-path="url(#clipper)"
stroke={stroke()}
stroke-dasharray="5,5"
stroke-width={2}
fill="none"
class="cursor-pointer"
>
<title>{titleTd()}</title>
</path>
</g>
);
}
// Note: using temperatures in Kelvin as that's easiest to get from CLASS, but
// perhaps not the most interoperable with other sounding data sources.
export function SkewTPlot(props: {
data: () => ChartData<SoundingRecord>[];
id: string;
}) {
const pressureLines = [1000, 850, 700, 500, 300, 200, 100];
const temperatureLines = d3.range(-100, 45, 10);
const initialBasePressure = 1050;
const initialTopPressure = 100;
const pressureGrid = d3.range(
initialTopPressure,
initialBasePressure + 1,
10,
);
const temperatureGrid = d3.range(-30, 240, 20);
const dryAdiabats: [number, number][][] = temperatureGrid.map((temperature) =>
pressureGrid.map((pressure) => [pressure, temperature]),
);
const [toggles, setToggles] = createStore<Record<string, boolean>>({});
const dataForLegend = () =>
props.data().filter((d) => !d.label.includes("- fire plume"));
// Initialize all lines as visible
createEffect(() => {
for (const d of dataForLegend()) {
setToggles(d.label, true);
}
});
function toggleLine(label: string, value: boolean) {
setToggles(label, value);
}
function showSounding(i: number) {
const cd = props.data()[i];
if (!toggles || !cd) {
return true;
}
return toggles[cd.label.replace(" - fire plume", "")];
}
return (
<ChartContainer>
<Legend entries={dataForLegend} toggles={toggles} onChange={toggleLine} />
<Chart
id={props.id}
title="Thermodynamic diagram"
formatX={() => d3.format(".0d")}
formatY={() => d3.format(".0d")}
transformX={getTempAtCursor}
>
<AxisBottom
domain={() => [-45, 50]}
tickValues={temperatureLines}
label="Temperature [°C]"
/>
<AxisLeft
type="log"
domain={() => [initialBasePressure, initialTopPressure]}
tickValues={pressureLines}
label="Pressure [hPa]"
/>
<For each={temperatureLines}>{(t) => SkewTGridLine(t)}</For>
<For each={pressureLines}>{(p) => LogPGridLine(p)}</For>
<For each={dryAdiabats}>{(d) => <DryAdiabat {...d} />}</For>
<For each={props.data()}>
{(d, i) => (
<Show when={showSounding(i())}>
<Sounding {...d} />
</Show>
)}
</For>
</Chart>
</ChartContainer>
);
}