forked from dafekt1ve/d3-skewt-and-indices
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsfunctions.js
192 lines (171 loc) · 6.19 KB
/
jsfunctions.js
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
function drawBackground() {
var svghodo = d3.select("div#hodobox svg g").append("g").attr("class", "hodobg");
var svg = d3.select("div#mainbox svg g").append("g").attr("class", "skewtbg");
var dryline = d3.svg.line()
.interpolate("linear")
.x(function(d,i) { return x( ( 273.15 + d ) / Math.pow( (1000/pp[i]), 0.286) -273.15) + (y(basep)-y(pp[i]))/tan;})
.y(function(d,i) { return y(pp[i])} );
var moistline = d3.svg.line()
.interpolate("linear")
.x(function(d,i) { return x(d) + (y(basep)-y(pp[i]))/tan;})
.y(function(d,i) { return y(pp[i])} );
var vp = 0;
e_array = [];
t_array = [];
for (t=-50; t<=50; t+=.01) {
vp=calcVaporPressure(t);
e_array.push(vp);
t_array.push(t);
}
var mixra1, mixra2;
var allmixr1000 = [];
var allmixr622 = [];
var Ttuple = [];
var allT = [];
var mixr_lims = [.4,.7,1,2,3,5,8,12,16,20];
mixr_lims.forEach(function (item) {
for (i=0; i<=e_array.length; i++) {
var mixraf1 = 0;
var mixraf2 = 0;
var Ttuple = [];
mixra1 = calcMixingRatio(e_array[i],1000);
if (mixra1 >= item) {
allmixr1000.push(mixra1);
Ttuple.push(t_array[i]);
break;
}
}
for (j=0; j<=e_array.length; j++) {
mixra2 = calcMixingRatio(e_array[j],622);
if (mixra2 >= item) {
allmixr622.push(mixra2);
Ttuple.push(t_array[j]);
break;
}
}
allT.push(Ttuple);
});
console.log(allT);
// Mixing Ratio Lines
var mrline = d3.svg.line()
.interpolate("linear")
.x(function(d,i) { return x(d) + (y(basep)-y(i%2 ? 622:1050))/tan})
.y(function(d,i) { return i % 2 ? y(622) : y(1050)} );
// Add clipping path
svg.append("clipPath")
.attr("id", "clipper")
.append("rect")
.attr("x", 0)
.attr("y", 0)
.attr("width", width)
.attr("height", height);
// Skewed temperature lines
svg.selectAll("gline")
.data(d3.range(-100,50,10))
.enter().append("line")
.attr("x1", function(d) { return x(d)-0.5 + (y(basep)-y(100))/tan; })
.attr("x2", function(d) { return x(d)-0.5; })
.attr("y1", 0)
.attr("y2", height)
.attr("class", function(d) { if (d == 0) { return "tempzero"; } else if (d == -20 || d == -10) { return "dendrite" }else { return "gridline"}})
.attr("clip-path", "url(#clipper)");
// Logarithmic pressure lines
svg.selectAll("gline2")
.data(plines)
.enter().append("line")
.attr("x1", 0)
.attr("x2", width)
.attr("y1", function(d) { return y(d); })
.attr("y2", function(d) { return y(d); })
.attr("class", "gridline");
// create array to plot dry adiabats
var pp = d3.range(topp,basep+1,10);
var dryad = d3.range(-40,240,20);
var all = [];
for (i=0; i<dryad.length; i++) {
var z = [];
for (j=0; j<pp.length; j++) { z.push(dryad[i]); }
all.push(z);
}
var allmoist = [];
var moistad = d3.range(-20,40,4);
for (i=0; i<moistad.length; i++) {
var a = [];
for (j=0; j<pp.length; j++) {
a.push( findTC( calcThetaE(basep, moistad[i], moistad[i]), pp[j]))
}
allmoist.push(a);
}
// Draw dry adiabats
svg.selectAll(".dryline")
.data(all)
.enter().append("path")
.attr("class", "gridline")
.attr("class", "dryline")
.attr("clip-path", "url(#clipper)")
.attr("d", dryline);
// Draw moist adiabats
svg.selectAll(".moistline")
.data(allmoist)
.enter().append("path")
.attr("class", "gridline")
.attr("class", "moistline")
.attr("clip-path", "url(#clipper)")
.attr("d", moistline);
// Draw mixing ratio lines
svg.selectAll(".mrline")
.data(allT)
.enter().append("path")
.attr("class", "gridline")
.attr("class", "mrline")
.attr("clip-path", "url(#clipper)")
.attr("d", mrline);
allT.forEach( function (item, i) {
xVal = x(item[1]) + (y(basep) - y(622))/tan;
yVal = y(622);
mixR622 = parseFloat(allmixr622[i]);
if (mixR622 < 1) {
roundedMixR = mixR622.toFixed(1);
xVal = xVal-11;
} else {
roundedMixR = mixR622.toFixed(0);
if (roundedMixR < 10) {
xVal = xVal-5;
} else {
xVal = xVal-8;
}
}
svg.append("text")
.attr("transform","translate( " + xVal + " ," + yVal + ")")
.text(roundedMixR)
.attr("opacity",".65")
.attr("fill","#48cf6a");
});
// Line along right edge of plot
svg.append("line")
.attr("x1", width-0.5)
.attr("x2", width-0.5)
.attr("y1", 0)
.attr("y2", height)
.attr("class", "gridline");
// draw hodograph background
svghodo.selectAll(".circles")
.data(d3.range(10,80,10))
.enter().append("circle")
.attr("cx", 0)
.attr("cy", 0)
.attr("r", function(d) { return r(d); })
.attr("class", "gridline");
svghodo.selectAll("hodolabels")
.data(d3.range(10,80,20)).enter().append("text")
.attr('x', 0)
.attr('y', function (d,i) { return r(d); })
.attr('dy', '0.4em')
.attr('class', 'hodolabels')
.attr('text-anchor', 'middle')
.text(function(d) { return d+'kts'; });
// Add axes
svg.append("g").attr("class", "x axis").attr("transform", "translate(0," + (height-0.5) + ")").call(xAxis);
svg.append("g").attr("class", "y axis").attr("transform", "translate(-0.5,0)").call(yAxis);
svg.append("g").attr("class", "y axis ticks").attr("transform", "translate(-0.5,0)").call(yAxis2);
}