forked from Esri/wind-js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
292 lines (261 loc) · 9.11 KB
/
index.html
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no">
<title>Sea Surface Animation</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/github-fork-ribbon-css/0.2.0/gh-fork-ribbon.min.css" />
<!--[if lt IE 9]>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/github-fork-ribbon-css/0.2.0/gh-fork-ribbon.ie.min.css" />
<![endif]-->
<link rel="stylesheet" href="https://js.arcgis.com/3.17/esri/css/esri.css">
<style>
html,body {
width:100%;
height:100%;
margin: 0;
padding: 0px 0 0 0;
}
#mapCanvas {
padding:0;
}
#credit {
position: absolute;
bottom: 10px;
left: 10px;
color: #fff;
font-size: 14px;
}
#credit a {
color: #08c;
}
</style>
<script>
var dojoConfig = {
paths: {
plugins: location.pathname + "./plugins"
}
};
</script>
<script src="windy.js"></script>
<script src="netcdfjs.js"></script>
<script src="https://js.arcgis.com/3.17compact/"></script>
<script>
if (!Date.prototype.toISOString2) {
(function() {
function pad(number) {
if (number < 10) {
return '0' + number;
}
return number;
}
Date.prototype.toISOString2 = function() {
return this.getUTCFullYear() +
'-' + pad(this.getUTCMonth() + 1) +
'-' + pad(this.getUTCDate()) +
'T' + pad(this.getUTCHours()) +
':' + pad(this.getUTCMinutes()) +
':' + pad(this.getUTCSeconds()) +
'Z';
};
}());
}
function display_time(time){
document.getElementById("display_time").setAttribute("title",(new Date(time)).toISOString2());
}
var windlayers = [];
function flatten(a){
return [].concat.apply([], a);
}
function nc2gfs(atime, alat, alon, xvel, yvel){
var results = Array();
var stride = alat.length * alon.length;
for(var itime=0; itime < atime.length; itime++){
var time = atime[itime];
var date = new Date();
date.setTime(time*1000);
var u = {
header: {
"parameterCategory": 2,
"parameterNumber": 2,
"forecastTime": 0,
"refTime" : date.toISOString2(),
"nx": alon.length,
"ny": alat.length,
"la2": alat[0],
"lo1": alon[0],
"la1": alat[alat.length-1],
"lo2": alon[alon.length-1],
},
"data": Array(),
};
var v = {
"header": {
"parameterCategory": 2,
"parameterNumber": 3,
"forecastTime": 0,
"refTime" : date.toISOString2(),
"nx": alon.length,
"ny": alat.length,
"la2": alat[0],
"lo1": alon[0],
"la1": alat[alat.length-1],
"lo2": alon[alon.length-1],
},
"data": Array()
};
for(var ilat=0; ilat < alat.length; ilat++){
var slice = itime * (alat.length*alon.length)+ilat*alon.length;
v.data.unshift(yvel.slice(slice, slice+alon.length));
u.data.unshift(xvel.slice(slice, slice+alon.length));
}
v.data = flatten(v.data);
u.data = flatten(u.data);
results.push(u);
results.push(v);
}
for(var i=0;i<results.length;i++){
var header = results[i].header;
header.dx = Math.abs((header.lo1 - header.lo2)/(header.nx-1));
header.dy = Math.abs((header.la1 - header.la2)/(header.ny-1));
}
return results;
}
var fetchNCGrid = function(url,nullx,nully,cb){
var xhr = new XMLHttpRequest();
xhr.open("GET", url, true);
xhr.responseType = "arraybuffer";
xhr.addEventListener("load", function (fn) {
if (xhr.status === 200) {
var reader = new netcdfjs(xhr.response);
var vars = Array();
for(var i=0;i< reader.variables.length;i++){
vars.push(reader.getDataVariable(reader.variables[i].name));
}
for(var i=0; i<vars[3].length; i++){
if(vars[3][i] == nullx){
vars[3][i] = null;
}
}
for(var i=0; i<vars[4].length; i++){
if(vars[4][i] == nully){
vars[4][i] = null;
}
}
var grid = nc2gfs(vars[0],vars[1],vars[2],vars[3],vars[4]);
fn.success(grid);
}
}.bind(null,cb),false);
xhr.send();
}
var ncRequest = function(url,nullx,nully){
//TODO: use params
this.then = function(success,err){
this.success = success;
this.err = err;
}.bind(this);
this.success = function(what){
console.log("no sucess");
}.bind(this);
this.err = function(e){
console.log(e);
}
setTimeout(fetchNCGrid.bind(null,url,nullx,nully,this),0);
return this;
};
</script>
<script>
var create_map = function(){
var map, rasterLayer;
var canvasSupport;
require([
"esri/map", "esri/layers/ArcGISTiledMapServiceLayer",
"esri/domUtils", "esri/request",
"dojo/parser", "dojo/number", "dojo/json", "dojo/dom",
"dijit/registry", "plugins/RasterLayer","esri/layers/WebTiledLayer",
"esri/config",
"dojo/domReady!"
], function(
Map, ArcGISTiledMapServiceLayer,
domUtils, esriRequest,
parser, number, JSON, dom,
registry, RasterLayer, WebTiledLayer, esriConfig
){
parser.parse();
// does the browser support canvas?
canvasSupport = supports_canvas();
map = new Map("mapCanvas", {
center: [-9.32, 53.19],
zoom: 11,
basemap: "dark-gray"
});
map.on("load", mapLoaded);
function mapLoaded() {
// Add raster layer
if ( canvasSupport ) {
rasterLayer = new RasterLayer(null, {
opacity: 0.55
});
map.addLayer(rasterLayer);
map.on("extent-change", redraw);
map.on("resize", function(){});
map.on("zoom-start", redraw);
map.on("pan-start", redraw);
var layerRequest = ncRequest("connemara.nc",1.0E37,1.0E37);
layerRequest.then(
function(gfs) {
var windy = new Windy({ canvas: rasterLayer._element, data: gfs });
windy.onTimeChange(display_time);
windlayers.push(windy);
redraw();
}, function(error) {
console.log("Error: ", error.message);
});
} else {
dom.byId("mapCanvas").innerHTML = "This browser doesn't support canvas. Visit <a target='_blank' href='http://www.caniuse.com/#search=canvas'>caniuse.com</a> for supported browsers";
}
}
// does the browser support canvas?
function supports_canvas() {
return !!document.createElement("canvas").getContext;
}
function redraw(){
rasterLayer._element.width = map.width;
rasterLayer._element.height = map.height;
windlayers.forEach(function(windy){
windy.stop();
});
var extent = map.geographicExtent;
windlayers.forEach(function(layer){
setTimeout(function(windy){
windy.start(
[[0,0],[map.width, map.height]],
map.width,
map.height,
[[extent.xmin, extent.ymin],[extent.xmax, extent.ymax]]
);
}.bind(null,layer),500);
});
}
});
};
var phantom_wait_for_netcdfjs = function(callback){
if(typeof netcdfjs === 'undefined' && typeof window.netcdfjs === 'undefined'){
console.log("netcdfjs not loaded yet...");
setTimeout(phantom_wait_for_netcdfjs.bind(null,callback),1000);
}else{
callback();
}
}
document.addEventListener("DOMContentLoaded", function(event) {
phantom_wait_for_netcdfjs(create_map);
});
</script>
</head>
<body class="">
<div id="mapCanvas" style="height:100%;">
</div>
<div class="github-fork-ribbon" href="https://github.com/fullergalway/water-js" title="Fork Me on Github!" id="display_time">Fork Me on Github!</div>
<div id="credit">Implemented by <a href="http://www.marine.ie">Irish Marine Institute</a>, based on <a href="https://github.com/Esri/wind-js">wind-js</a>, as inspired by <a href="https://github.com/cambecc/earth">Earth</a>, Marine Institute data via <a href="https://coastwatch.pfeg.noaa.gov/erddap/">Erddap</a>, open source on <a href="https://github.com/fullergalway/water-js">Github</a></div>
</body>
</html>