forked from JohanMerger/rak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsim.js
500 lines (395 loc) · 11.8 KB
/
sim.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
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
var periods = [12.42,12.0,12.66,23.93,25.82,24.07,6.21];
var periodsRad = [0.505892537,0.523598776,0.496302157,0.262565203,0.243345674,0.261038027,1.011785074];
var amplitudes = Create2DArray(1221,821);
var angles = Create2DArray(1221,821);
var colors = Create2DArray(1221,821);
var minOffset = -0.5509;
var uMeanOffset = -0.8947;
var vMeanOffset = -0.8782;
function initSim()
{
var vels = new Array(30);
for(var t=0;t<30;t++)
{
vels[t] = simulateCell(550,740, t);
}
InitPlaces();
}
function simulateArea(xStart,xStop,yStart,yStop, nComponents,date)
{
var t = HourFraction(date);
var hourOffset = date.getTimezoneOffset(); // Get offset in minutes
hourOffset = hourOffset / 60; //convert hour offsett to fractional hours and add offset to t
t = t + hourOffset;
var Year = date.getFullYear();
var Day = dayOfYear(date);
nComponents = 3;
Day = Day - 14;
t = t + 24*14;
//Round all coordinates to nearest integer
xStart = Math.round(xStart);
xStop = Math.round(xStop);
yStart = Math.round(yStart);
yStop = Math.round(yStop);
// Calculate the radian frequencies for the components
var wts = [periodsRad[0]*t , periodsRad[1]*t, periodsRad[2]*t ];
// Initialize variables to hold temporary results for components.
var alpha0 = 0;
var beta0 = 0;
var ic0 = 0;
var is0 = 0;
var argument0 = 0;
var alpha1 = 0;
var beta1 = 0;
var ic1 = 0;
var is1 = 0;
var argument1 = 0;
var alpha2 = 0;
var beta2 = 0;
var ic2 = 0;
var is2 = 0;
var argument2 = 0;
var u = 0;
var v = 0;
var areaMax = 0;
// Offset for minimum component and the mean (residual components). All values in js. files have offsett in order
// to avoid negative numbers. This saves storage and memory space
var astro = AstroArgAll(Day, Year, nComponents);
var xx = 0;
var yy = 0;
var span = (vizObj.avgSize-1)/2;
var colorIndex = 0;
for(var x=xStart+span;x<xStop-span;x+=vizObj.avgSize)
{
for(var y=yStart+span;y<yStop-span;y+=vizObj.avgSize)
{
if(map[y][x]!=-1)
{
var index = map[y][x];
//M2
argument0 = wts[0]- M2Pha[index]/100 + astro[0] ;
alpha0 = (M2Maj[index]/100)*Math.cos(argument0);
beta0 = ( (M2Min[index]/1000)+minOffset)*Math.sin(argument0);
ic0 = Math.cos(M2Inc[index]/100);
is0 = Math.sin(M2Inc[index]/100);
//S2
argument1 = wts[1]- S2Pha[index]/100 + astro[1] ;
alpha1 = (S2Maj[index]/100)*Math.cos(argument1);
beta1 = ( (S2Min[index]/1000)+minOffset)*Math.sin(argument1);
ic1 = Math.cos(S2Inc[index]/100);
is1 = Math.sin(S2Inc[index]/100);
//N2
argument2 = wts[2]- N2Pha[index]/100 + astro[2] ;
alpha2 = (N2Maj[index]/100)*Math.cos(argument2);
beta2 = ((N2Min[index]/1000)+minOffset)*Math.sin(argument2);
ic2 = Math.cos(N2Inc[index]/100);
is2 = Math.sin(N2Inc[index]/100);
u=(alpha0*ic0-beta0*is0) + (alpha1*ic1-beta1*is1) + (alpha2*ic2-beta2*is2) + (uMean[index]/1000)+uMeanOffset;
v=(alpha0*is0+beta0*ic0) + (alpha1*is1+beta1*ic1) + (alpha2*is2+beta2*ic2) + (vMean[index]/1000)+vMeanOffset;
/*
//M2
//argument0 = wts[0]- ( M2Pha[index]);
argument0 = wts[0]- M2Pha[index] +astro[0] ;
alpha0 = (M2Maj[index])*Math.cos(argument0);
beta0 = ( (M2Min[index]))*Math.sin(argument0);
ic0 = Math.cos(M2Inc[index]);
is0 = Math.sin(M2Inc[index]);
//S2
//argument1 = wts[1]- ( S2Pha[index] );
argument1 = wts[1]- S2Pha[index] + astro[1] ;
alpha1 = (S2Maj[index])*Math.cos(argument1);
beta1 = ( (S2Min[index]))*Math.sin(argument1);
ic1 = Math.cos(S2Inc[index]);
is1 = Math.sin(S2Inc[index]);
//N2
//argument2 = wts[2]- ( N2Pha[index] );
argument2 = wts[2]- N2Pha[index] + astro[2] ;
alpha2 = (N2Maj[index])*Math.cos(argument2);
beta2 = ((N2Min[index]))*Math.sin(argument2);
ic2 = Math.cos(N2Inc[index]);
is2 = Math.sin(N2Inc[index]);
u=(alpha0*ic0-beta0*is0) + (alpha1*ic1-beta1*is1) + (alpha2*ic2-beta2*is2) + (uMean[index]);
v=(alpha0*is0+beta0*ic0) + (alpha1*is1+beta1*ic1) + (alpha2*is2+beta2*ic2) + (vMean[index]);
*/
amplitudes[y][x] = Math.sqrt(Math.pow(u,2)+Math.pow(v,2));
angles[y][x] = Math.atan2(v,u);
colorIndex = amplitudes[y][x] / vizObj.maxAmplitude;
if(colorIndex>1)
{
colorIndex=1;
}
colorIndex = (gradientRGB.length-1) * colorIndex;
colorIndex = Math.round(colorIndex);
colors[y][x] = colorIndex;
if(vizObj.avgSize>1)
{
var amplitude = amplitudes[y][x];
var angle = angles[y][x];
for(var xx=x-span;xx<x+span+1;xx++ )
{
for(var yy=y-span;yy<y+span+1;yy++)
{
if(map[yy][xx]!=-1)
{
amplitudes[yy][xx] = amplitude;
angles[yy][xx] = angle;
colors[yy][xx] = colorIndex;
}
}
}
}
if(amplitudes[y][x] >areaMax)
{
areaMax = amplitudes[y][x];
}
}
}
}
vizObj.areaMax = areaMax;
}
function AstroArgAll(d, year, nComponents)
{
var output = new Array(nComponents);
for(var i=0;i<nComponents;i++)
{
output[i] = astroArg(d,year,i);
}
return output;
}
/*
function astroArg2(Y,M,D,S)
{
var J = 2452545.5 + (Y-2000) * 365.25 + 28 + 0 +D-1;
var U = (J-2452545.5 + S/86400)/36525;
var T = U + (TT-UTC)/86400/36252;
}
*/
function astroArg(d0, year0, harmonic)
{
var reference = new Date(1975,0,1);
var selected = new Date(year0, 1,d0);
var d = 365*(year0-1975) + Math.round(0.25*(year0-1973)) + d0;
var t = (27392.500528+1.0000000356*d)/36525;
var t2=t*t;
var h0=279.69668 + 36000.768925*t+ 0.000303*t2;
var s0= 270.434358 + 481267.88314137*t-0.001133*t2 + 0.0000019*t*t2
var p0= 334.329653 + 4069.0340329575*t-0.010325*t2 - 0.000012*t2 *t;
var output = 0;
//var test = (new Date(year0,1,27) - new Date(1975,0,1))/1000/60/60/24;
if(harmonic == 0)
{
output = (2*h0)-(2*s0);
}
else if(harmonic == 1)
{
output = 0;
}
else if(harmonic == 2)
{
output = (2*h0) - (3*s0) + p0;
}
else if(harmonic == 3)
{
output = 4*h0-4*s0;
}
else if(harmonic == 4)
{
output = h0 + (Math.PI/2);
}
else if(harmonic == 5)
{
output = h0-2*s0-(Math.PI/2);
}
else if(harmonic == 6)
{
output = h0-(Math.PI/2);
}
output = output % 360;
output = output * (6.283185307/360);
return output;
}
function currentDate()
{
return new Date();
}
function timeChange(direction)
{
if(initializing)
{
return;
}
var newDateObj = new Date();
if(direction==true)
{
newDateObj.setTime(vizObj.selectedTime.getTime() + (vizObj.timeIncrement * 60 * 1000));
}
else
{
newDateObj.setTime(vizObj.selectedTime.getTime() - (vizObj.timeIncrement * 60 * 1000));
}
vizObj.selectedTime = newDateObj;
$("#HourSlider").attr("value", currentDate().getHours()*60 + currentDate().getMinutes());
$( "#SelectedDateTime" ).text(dateToString(vizObj.selectedTime));
$( "#MapSelectedDate" ).text(dateToString(vizObj.selectedTime));
$( ".mapSelectedDate" ).text(dateToString(vizObj.selectedTime));
if(GPSState==false)
{
GPSDisplayUpdate(vizObj.lat,vizObj.lon);
}
}
function Point(x,y,z)
{
this.x = x;
this.y = y;
this.z = z;
}
function HourFraction(date)
{
var hour = date.getHours();
hour = hour + (date.getMinutes()/60);
return hour;
}
function GetArea(Index)
{
var Year = vizObj.selectedTime.getFullYear();
var Month = vizObj.selectedTime.getMonth();
var Day = vizObj.selectedTime.getDate();
var Hour = 0;
var Minutes = 0;
var DayOfYear = dayOfYear(vizObj.selectedTime);
//Define area
var areaSize = Places[Index].Size;
var start = [Places[Index].X,Places[Index].Y];
//Define how time resolution in minutes
var steps = 24;//vizObj.times.t.length;
var dt =60;// vizObj.timeIncrement;
var points = new Array(steps);
//Variable for holding amplitudes
var a = 0;
var aSteps = new Array(steps);
var angle = 0;
var angleSteps = new Array(steps);
for(var step=0;step<steps;step++)
{
var temp = new Date(Year, Month,Day,0,0,0);
temp.setMinutes(temp.getMinutes() + (dt*step) );
//var t = (dt*step)/60;
a = 0;
angle = 0;
simulateArea(start[0],start[0]+areaSize,start[1],start[1]+areaSize, 3, temp);
// Calculate average for selected time
for(var x=start[0];x<start[0]+areaSize;x++)
{
for(var y = start[1];y<start[1]+areaSize;y++)
{
a = a + amplitudes[y][x];
angle = angle + angles[y][x];
}
}
a = a / (areaSize*areaSize);
angle = angle / (areaSize*areaSize);
var p = new Point(temp,a.toFixed(3),rad2deg(angle) );
points[step] = p;
//aSteps[step] = a;
//angleSteps[step] = angle;
}
Places[Index].Points = points;
}
function Place(Name, X, Y, Size, Points)
{
this.Name = Name;
this.X = X;
this.Y = Y;
this.Size = Size;
this.Points = Points;
}
var Places = new Array();
function InitPlaces()
{
Places.push(new Place('Nólsoyarfjørður', 565,495,10));
Places.push(new Place('Mykinesfjørður', 154,368,10));
Places.push(new Place('Skopunarfjørður', 527,565,20));
Places.push(new Place('Vestmannasund', 325,336,5));
Places.push(new Place('Suðuroyarfjørður', 546,909,10));
/*
Places[0] = new Place('Suðuroyarfjørður', 546,909,10);
Places[0] = new Place('Vestanfyri', 205,881,1);
Places[1] = new Place('Mykinesfjørður', 154,368,10);
Places[2] = new Place('Skopunarfjørður', 527,565,20);
Places[3] = new Place('Eiðisflógvin', 340,125,5);
Places[4] = new Place('Vestmannasund', 325,336,5);
Places[5] = new Place('Fugloyarfjørður', 761,123,4);
Places[6] = new Place('Djúpini', 497,124,10);
//Places[7] = new Place('Norðurøkið', 527,565,20);
//Places[8] = new Place('Vesturøkið', 527,565,20);*/
OverviewUpdate();
}
function OverviewUpdate()
{
var avgSize = vizObj.avgSize;
vizObj.avgSize = 1;
for(i = 0; i<Places.length;i++)
{
GetArea(i);
}
var isActive = $('#Overview').css('display') != 'none';
if(!isActive)
{
$('#Overview').css('display','initial');
}
makeCharts(Places);
if(!isActive)
{
$('#Overview').css('display','none');
}
vizObj.avgSize = avgSize;
}
function simulateCell(x,y, t)
{
// Calculate the radian frequencies for the components
var wts = [periodsRad[0]*t , periodsRad[1]*t, periodsRad[2]*t ];
// Initialize variables to hold temporary results for components.
var alpha0 = 0;
var beta0 = 0;
var ic0 = 0;
var is0 = 0;
var argument0 = 0;
var alpha1 = 0;
var beta1 = 0;
var ic1 = 0;
var is1 = 0;
var argument1 = 0;
var alpha2 = 0;
var beta2 = 0;
var ic2 = 0;
var is2 = 0;
var argument2 = 0;
var u = 0;
var v = 0;
if(map[y][x]!=-1)
{
var index = map[y][x];
//M2
argument0 = wts[0]- ( M2Pha[index]);
alpha0 = (M2Maj[index])*Math.cos(argument0);
beta0 = ( (M2Min[index]))*Math.sin(argument0);
ic0 = Math.cos(M2Inc[index]);
is0 = Math.sin(M2Inc[index]);
//S2
argument1 = wts[1]- ( S2Pha[index] );
alpha1 = (S2Maj[index])*Math.cos(argument1);
beta1 = ( (S2Min[index]))*Math.sin(argument1);
ic1 = Math.cos(S2Inc[index]);
is1 = Math.sin(S2Inc[index]);
//N2
argument2 = wts[2]- ( N2Pha[index] );
alpha2 = (N2Maj[index])*Math.cos(argument2);
beta2 = ((N2Min[index]))*Math.sin(argument2);
ic2 = Math.cos(N2Inc[index]);
is2 = Math.sin(N2Inc[index]);
u=(alpha0*ic0-beta0*is0);// + (alpha1*ic1-beta1*is1) + (alpha2*ic2-beta2*is2) + (uMean[index]);
v=(alpha0*is0+beta0*ic0);// + (alpha1*is1+beta1*ic1) + (alpha2*is2+beta2*ic2) + (vMean[index]);
vel = Math.sqrt(Math.pow(u,2)+Math.pow(v,2));
return vel;
}
}