-
Notifications
You must be signed in to change notification settings - Fork 0
/
Test_earth.js
6354 lines (5768 loc) · 211 KB
/
Test_earth.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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
var d,
m,
y,
t,
slot = "00",
forecast = "000",
current_d,
current_m,
current_slot = "00",
current_y,
current_t;
var currentdate = new Date();
console.log("[Local Date]", currentdate);
var utc = currentdate.getTime() + currentdate.getTimezoneOffset() * 60000;
var datetime = new Date(utc + 3600000 * "-4.0");
console.log("[Sync date]", datetime);
current_d = datetime.getDate().toString();
current_m = (datetime.getMonth() + 1).toString();
current_y = datetime.getFullYear().toString();
current_t = datetime.getHours().toString();
current_slot = "00";
if (current_t >= 0 && current_t <= 5) {
current_slot = "00";
} else if (current_t >= 6 && current_t <= 11) {
current_slot = "06";
} else if (current_t >= 12 && current_t <= 17) {
current_slot = "12";
} else {
current_slot = "18";
}
if (sessionStorage.getItem("m") === null) {
d = current_d;
m = current_m;
t = current_t;
slot = current_slot;
} else {
d = sessionStorage.getItem("d");
m = sessionStorage.getItem("m");
slot = sessionStorage.getItem("slot");
}
console.log("[Latest date]", m + " " + d + " " + slot);
console.log("[Current date]", current_m + " " + current_d + " " + current_slot + " " + current_t);
// product.js start
/**
* products - defines the behavior of weather data grids, including grid construction, interpolation, and color scales.
*
* Copyright (c) 2014 Cameron Beccario
* The MIT License - http://opensource.org/licenses/MIT
*
* https://github.com/cambecc/earth
*/
var products = (function () {
"use strict";
// var WEATHER_PATH = "./data/weather";
var WEATHER_PATH = "../../../Backend/Media";
var OSCAR_PATH = "./data/oscar";
var height;
var period;
var index = 0;
var catalogs = {
// The OSCAR catalog is an array of file names, sorted and prefixed with yyyyMMdd. Last item is the
// most recent. For example: [ 20140101-abc.json, 20140106-abc.json, 20140112-abc.json, ... ]
oscar: µ.loadJson([OSCAR_PATH, "catalog.json"].join("/")),
};
function buildProduct(overrides) {
return _.extend(
{
description: "",
paths: [],
date: null,
navigate: function (step) {
return gfsStep(this.date, step);
},
load: function (cancel) {
var me = this;
return when.map(this.paths, µ.loadJson).then(function (files) {
return cancel.requested
? null
: _.extend(me, buildGrid(me.builder.apply(me, files)));
});
},
},
overrides
);
}
/**
* @param attr
* @param {String} type
* @param {String?} surface
* @param {String?} level
* @returns {String}
*/
function gfsDate(attr) {
if (attr.date === "current") {
// Construct the date from the current time, rounding down to the nearest three-hour block.
var now = new Date(Date.now()),
hour = Math.floor(now.getUTCHours() / 6);
return new Date(
Date.UTC(
now.getUTCFullYear(),
now.getUTCMonth(),
now.getUTCDate(),
hour
)
);
}
var parts = attr.date.split("/");
return new Date(
Date.UTC(+parts[0], parts[1] - 1, +parts[2], +attr.hour.substr(0, 2))
);
}
/**
* Returns a date for the chronologically next or previous GFS data layer. How far forward or backward in time
* to jump is determined by the step. Steps of ±1 move in 3-hour jumps, and steps of ±10 move in 24-hour jumps.
*/
function gfsStep(date, step) {
var offset = (step > 1 ? 8 : step < -1 ? -8 : step) * 6,
adjusted = new Date(date);
adjusted.setHours(adjusted.getHours() + offset);
return adjusted;
}
function netcdfHeader(time, lat, lon, center) {
return {
lo1: lon.sequence.start,
la1: lat.sequence.start,
dx: lon.sequence.delta,
dy: -lat.sequence.delta,
nx: lon.sequence.size,
ny: lat.sequence.size,
refTime: time.data[0],
forecastTime: 0,
centerName: center,
};
}
function describeSurface(attr) {
return attr.surface === "surface" ? "Surface" : µ.capitalize(attr.level);
}
function describeSurfaceJa(attr) {
return attr.surface === "surface" ? "地上" : µ.capitalize(attr.level);
}
/**
* Returns a function f(langCode) that, given table:
* {foo: {en: "A", ja: "あ"}, bar: {en: "I", ja: "い"}}
* will return the following when called with "en":
* {foo: "A", bar: "I"}
* or when called with "ja":
* {foo: "あ", bar: "い"}
*/
function localize(table) {
return function (langCode) {
var result = {};
_.each(table, function (value, key) {
result[key] = value[langCode] || value.en || value;
});
return result;
};
}
function gfs1p0degPath(type, year, month, day, slot) {
console.log("gfs called");
if(type === "currents"){
console.log("[Currents]");
return `./current/current.json`;
}
let Slot_map = { "00": 1, "06": 2, 12: 3, 18: 4 };
let days_diff = day - current_d;
let hours_to_be_added =
days_diff * 24 + (Slot_map[slot] - Slot_map[current_slot]) * 6;
if (parseInt(hours_to_be_added) < 100) {
hours_to_be_added = "0" + hours_to_be_added;
}
if (parseInt(hours_to_be_added) < 10) {
hours_to_be_added = "0" + hours_to_be_added;
}
var data_day, data_slot;
if (
days_diff < 0 ||
(days_diff === 0 && Slot_map[slot] - Slot_map[current_slot] < 0)
) {
hours_to_be_added = "000";
data_day = day;
data_slot = slot;
} else {
data_slot = current_slot;
data_day = current_d;
}
console.log(
"./" +
type +
"_" +
month +
"_" +
data_day +
"_" +
data_slot +
"_" +
hours_to_be_added +
"_" +
type +
".json"
);
return `./${type}/${month}_${data_day}_${data_slot}_${hours_to_be_added}_${type}.json`;
}
var FACTORIES = {
wind: {
matches: _.matches({
param: "wind",
surface: "surface",
level: "level",
overlayType: "off",
}),
create: function (attr) {
console.log(attr);
return buildProduct({
field: "vector",
type: "wind",
description: localize({
name: { en: "Wind", ja: "風速" },
qualifier: {
en: " @ " + describeSurface(attr),
ja: " @ " + describeSurfaceJa(attr),
},
}),
// paths: ["./data/weather/current/dummy.json"],
// paths: ["./data/weather/current/3_14_12_000_wind.json"],
// paths: [gfs1p0degPath("wind", y, m, d, slot)],
paths: [gfs1p0degPath("wind", y, m, d, slot)],
// paths: ["F:/Globe/Backend/Media/wind/3_16_12_000_wind.json"],
// paths: ["./Media/wind/3_14_12_073_wind.json"],
// paths: ["./3_14_12_000_wind.json"],
date: gfsDate(attr),
builder: function (file) {
var uData = file[2].data,
vData = file[3].data;
return {
header: file[0].header,
interpolate: bilinearInterpolateVector,
data: function (i) {
return [uData[i], vData[i]];
},
};
},
units: [
{
label: "km/h",
conversion: function (x) {
return x * 3.6;
},
precision: 0,
},
{
label: "m/s",
conversion: function (x) {
return x;
},
precision: 1,
},
{
label: "kn",
conversion: function (x) {
return x * 1.943844;
},
precision: 0,
},
{
label: "mph",
conversion: function (x) {
return x * 2.236936;
},
precision: 0,
},
],
scale: {
bounds: [0, 100],
gradient: function (v, a) {
return µ.extendedSinebowColor(Math.min(v, 100) / 100, a);
},
},
particles: { velocityScale: 1 / 60000, maxIntensity: 17 },
});
},
},
waves: {
matches: _.matches({
param: "ocean",
surface: "surface",
level: "waves",
}),
create: function (attr) {
return when(catalogs.oscar).then(function (catalog) {
return buildProduct({
field: "vector",
type: "currents",
description: localize({
name: { en: "Peak Wave Period", ja: "海流" },
qualifier: { en: " ", ja: " @ 地上" },
}),
// paths: ["./data/weather/wave/wavedummy.json"],
paths: [gfs1p0degPath("wave", y, m, d, slot)],
date: oscarDate(catalog, attr),
navigate: function (step) {
return oscarStep(catalog, this.date, step);
},
builder: function (file) {
var uData = file[0].data,
vData = file[1].data;
height = file[2].data;
period = file[3].data;
var maxi = 0;
for (var i = 0; i < period.length; i++) {
if (!isNaN(period[i])) {
maxi = Math.max(period[i], maxi);
}
}
return {
header: file[0].header,
interpolate: bilinearInterpolateVector,
data: function (i) {
var u = uData[i],
v = vData[i];
return µ.isValue(u) && µ.isValue(v)
? [u, v, height[i], period[i]]
: null;
},
};
},
units: [
{
label: "m/s",
conversion: function (x) {
return x;
},
precision: 2,
},
{
label: "km/h",
conversion: function (x) {
return x * 3.6;
},
precision: 1,
},
{
label: "kn",
conversion: function (x) {
return x * 1.943844;
},
precision: 1,
},
{
label: "mph",
conversion: function (x) {
return x * 2.236936;
},
precision: 1,
},
],
scale: {
bounds: [0, 20],
gradient: µ.segmentedColorScale([
[0, [10, 25, 68]],
[1, [10, 25, 206]],
[3, [17, 146, 166]],
[5, [62, 251, 94]],
[7, [237, 234, 101]],
[12, [255, 219, 15]],
[14, [255, 79, 15]],
[20, [255, 20, 15]],
]),
},
particles: { velocityScale: 1 / 440000, maxIntensity: 0.7 },
});
});
},
},
/*
"temp": {
matches: _.matches({param: "wind", overlayType: "temp"}),
create: function(attr) {
return buildProduct({
field: "scalar",
type: "temp",
description: localize({
name: {en: "Temp", ja: "気温"},
qualifier: {en: " @ " + describeSurface(attr), ja: " @ " + describeSurfaceJa(attr)}
}),
paths: [gfs1p0degPath(attr, "temp", attr.surface, attr.level)],
date: gfsDate(attr),
builder: function(file) {
var record = file[0], data = record.data;
return {
header: record.header,
interpolate: bilinearInterpolateScalar,
data: function(i) {
return data[i];
}
}
},
units: [
{label: "°C", conversion: function(x) { return x - 273.15; }, precision: 1},
{label: "°F", conversion: function(x) { return x * 9/5 - 459.67; }, precision: 1},
{label: "K", conversion: function(x) { return x; }, precision: 1}
],
scale: {
bounds: [193, 328],
gradient: µ.segmentedColorScale([
[193, [37, 4, 42]],
[206, [41, 10, 130]],
[219, [81, 40, 40]],
[233.15, [192, 37, 149]], // -40 C/F
[255.372, [70, 215, 215]], // 0 F
[273.15, [21, 84, 187]], // 0 C
[275.15, [24, 132, 14]], // just above 0 C
[291, [247, 251, 59]],
[298, [235, 167, 21]],
[311, [230, 71, 39]],
[328, [88, 27, 67]]
])
}
});
}
},
"relative_humidity": {
matches: _.matches({param: "wind", overlayType: "relative_humidity"}),
create: function(attr) {
return buildProduct({
field: "scalar",
type: "relative_humidity",
description: localize({
name: {en: "Relative Humidity", ja: "相対湿度"},
qualifier: {en: " @ " + describeSurface(attr), ja: " @ " + describeSurfaceJa(attr)}
}),
paths: [gfs1p0degPath(attr, "relative_humidity", attr.surface, attr.level)],
date: gfsDate(attr),
builder: function(file) {
var vars = file.variables;
var rh = vars.Relative_humidity_isobaric || vars.Relative_humidity_height_above_ground;
var data = rh.data;
return {
header: netcdfHeader(vars.time, vars.lat, vars.lon, file.Originating_or_generating_Center),
interpolate: bilinearInterpolateScalar,
data: function(i) {
return data[i];
}
};
},
units: [
{label: "%", conversion: function(x) { return x; }, precision: 0}
],
scale: {
bounds: [0, 100],
gradient: function(v, a) {
return µ.sinebowColor(Math.min(v, 100) / 100, a);
}
}
});
}
},
"air_density": {
matches: _.matches({param: "wind", overlayType: "air_density"}),
create: function(attr) {
return buildProduct({
field: "scalar",
type: "air_density",
description: localize({
name: {en: "Air Density", ja: "空気密度"},
qualifier: {en: " @ " + describeSurface(attr), ja: " @ " + describeSurfaceJa(attr)}
}),
paths: [gfs1p0degPath(attr, "air_density", attr.surface, attr.level)],
date: gfsDate(attr),
builder: function(file) {
var vars = file.variables;
var air_density = vars.air_density, data = air_density.data;
return {
header: netcdfHeader(vars.time, vars.lat, vars.lon, file.Originating_or_generating_Center),
interpolate: bilinearInterpolateScalar,
data: function(i) {
return data[i];
}
};
},
units: [
{label: "kg/m³", conversion: function(x) { return x; }, precision: 2}
],
scale: {
bounds: [0, 1.5],
gradient: function(v, a) {
return µ.sinebowColor(Math.min(v, 1.5) / 1.5, a);
}
}
});
}
},
"wind_power_density": {
matches: _.matches({param: "wind", overlayType: "wind_power_density"}),
create: function(attr) {
var windProduct = FACTORIES.wind.create(attr);
var airdensProduct = FACTORIES.air_density.create(attr);
return buildProduct({
field: "scalar",
type: "wind_power_density",
description: localize({
name: {en: "Wind Power Density", ja: "風力エネルギー密度"},
qualifier: {en: " @ " + describeSurface(attr), ja: " @ " + describeSurfaceJa(attr)}
}),
paths: [windProduct.paths[0], airdensProduct.paths[0]],
date: gfsDate(attr),
builder: function(windFile, airdensFile) {
var windBuilder = windProduct.builder(windFile);
var airdensBuilder = airdensProduct.builder(airdensFile);
var windData = windBuilder.data, windInterpolate = windBuilder.interpolate;
var airdensData = airdensBuilder.data, airdensInterpolate = airdensBuilder.interpolate;
return {
header: _.clone(airdensBuilder.header),
interpolate: function(x, y, g00, g10, g01, g11) {
var m = windInterpolate(x, y, g00[0], g10[0], g01[0], g11[0])[2];
var ρ = airdensInterpolate(x, y, g00[1], g10[1], g01[1], g11[1]);
return 0.5 * ρ * m * m * m;
},
data: function(i) {
return [windData(i), airdensData(i)];
}
};
},
units: [
{label: "kW/m²", conversion: function(x) { return x / 1000; }, precision: 1},
{label: "W/m²", conversion: function(x) { return x; }, precision: 0}
],
scale: {
bounds: [0, 80000],
gradient: µ.segmentedColorScale([
[0, [15, 4, 96]],
[250, [30, 8, 180]],
[1000, [121, 102, 2]],
[2000, [118, 161, 66]],
[4000, [50, 102, 219]],
[8000, [19, 131, 193]],
[16000, [59, 204, 227]],
[64000, [241, 1, 45]],
[80000, [243, 0, 241]]
])
}
});
}
},
"total_cloud_water": {
matches: _.matches({param: "wind", overlayType: "total_cloud_water"}),
create: function(attr) {
return buildProduct({
field: "scalar",
type: "total_cloud_water",
description: localize({
name: {en: "Total Cloud Water", ja: "雲水量"},
qualifier: ""
}),
paths: [gfs1p0degPath(attr, "total_cloud_water")],
date: gfsDate(attr),
builder: function(file) {
var record = file[0], data = record.data;
return {
header: record.header,
interpolate: bilinearInterpolateScalar,
data: function(i) {
return data[i];
}
}
},
units: [
{label: "kg/m²", conversion: function(x) { return x; }, precision: 3}
],
scale: {
bounds: [0, 1],
gradient: µ.segmentedColorScale([
[0.0, [5, 5, 89]],
[0.2, [170, 170, 230]],
[1.0, [255, 255, 255]]
])
}
});
}
},
"total_precipitable_water": {
matches: _.matches({param: "wind", overlayType: "total_precipitable_water"}),
create: function(attr) {
return buildProduct({
field: "scalar",
type: "total_precipitable_water",
description: localize({
name: {en: "Total Precipitable Water", ja: "可降水量"},
qualifier: ""
}),
paths: [gfs1p0degPath(attr, "total_precipitable_water")],
date: gfsDate(attr),
builder: function(file) {
var record = file[0], data = record.data;
return {
header: record.header,
interpolate: bilinearInterpolateScalar,
data: function(i) {
return data[i];
}
}
},
units: [
{label: "kg/m²", conversion: function(x) { return x; }, precision: 3}
],
scale: {
bounds: [0, 70],
gradient:
µ.segmentedColorScale([
[0, [230, 165, 30]],
[10, [120, 100, 95]],
[20, [40, 44, 92]],
[30, [21, 13, 193]],
[40, [75, 63, 235]],
[60, [25, 255, 255]],
[70, [150, 255, 255]]
])
}
});
}
},
"mean_sea_level_pressure": {
matches: _.matches({param: "wind", overlayType: "mean_sea_level_pressure"}),
create: function(attr) {
return buildProduct({
field: "scalar",
type: "mean_sea_level_pressure",
description: localize({
name: {en: "Mean Sea Level Pressure", ja: "海面更正気圧"},
qualifier: ""
}),
paths: [gfs1p0degPath(attr, "mean_sea_level_pressure")],
date: gfsDate(attr),
builder: function(file) {
var record = file[0], data = record.data;
return {
header: record.header,
interpolate: bilinearInterpolateScalar,
data: function(i) {
return data[i];
}
}
},
units: [
{label: "hPa", conversion: function(x) { return x / 100; }, precision: 0},
{label: "mmHg", conversion: function(x) { return x / 133.322387415; }, precision: 0},
{label: "inHg", conversion: function(x) { return x / 3386.389; }, precision: 1}
],
scale: {
bounds: [92000, 105000],
gradient: µ.segmentedColorScale([
[92000, [40, 0, 0]],
[95000, [187, 60, 31]],
[96500, [137, 32, 30]],
[98000, [16, 1, 43]],
[100500, [36, 1, 93]],
[101300, [241, 254, 18]],
[103000, [228, 246, 223]],
[105000, [255, 255, 255]]
])
}
});
}
},
*/
currents: {
matches: _.matches({
param: "ocean",
surface: "surface",
level: "currents",
}),
create: function (attr) {
return when(catalogs.oscar).then(function (catalog) {
return buildProduct({
field: "vector",
type: "currents",
description: localize({
name: { en: "Ocean Currents", ja: "海流" },
qualifier: { en: " @ Surface", ja: " @ 地上" },
}),
paths: ["./current/current.json"],
// paths: [gfs1p0degPath("currents", y, m, d, slot)],
date: oscarDate(catalog, attr),
navigate: function (step) {
return oscarStep(catalog, this.date, step);
},
builder: function (file) {
var uData = file[0].data,
vData = file[1].data;
return {
header: file[0].header,
interpolate: bilinearInterpolateVector,
data: function (i) {
var u = uData[i],
v = vData[i];
return µ.isValue(u) && µ.isValue(v) ? [u, v] : null;
},
};
},
units: [
{
label: "m/s",
conversion: function (x) {
return x;
},
precision: 2,
},
{
label: "km/h",
conversion: function (x) {
return x * 3.6;
},
precision: 1,
},
{
label: "kn",
conversion: function (x) {
return x * 1.943844;
},
precision: 1,
},
{
label: "mph",
conversion: function (x) {
return x * 2.236936;
},
precision: 1,
},
],
scale: {
bounds: [0, 1.5],
gradient: µ.segmentedColorScale([
[0, [10, 25, 68]],
[0.15, [10, 25, 250]],
[0.4, [24, 255, 93]],
[0.65, [255, 233, 102]],
[1.0, [255, 233, 15]],
[1.5, [255, 15, 15]],
]),
},
particles: { velocityScale: 1 / 4400, maxIntensity: 0.7 },
});
});
},
},
off: {
matches: _.matches({ overlayType: "off" }),
create: function () {
return null;
},
},
};
/**
* Returns the file name for the most recent OSCAR data layer to the specified date. If offset is non-zero,
* the file name that many entries from the most recent is returned.
*
* The result is undefined if there is no entry for the specified date and offset can be found.
*
* UNDONE: the catalog object itself should encapsulate this logic. GFS can also be a "virtual" catalog, and
* provide a mechanism for eliminating the need for /data/weather/current/* files.
*
* @param {Array} catalog array of file names, sorted and prefixed with yyyyMMdd. Last item is most recent.
* @param {String} date string with format yyyy/MM/dd or "current"
* @param {Number?} offset
* @returns {String} file name
*/
function lookupOscar(catalog, date, offset) {
offset = +offset || 0;
if (date === "current") {
return catalog[catalog.length - 1 + offset];
}
var prefix = µ.ymdRedelimit(date, "/", ""),
i = _.sortedIndex(catalog, prefix);
i = (catalog[i] || "").indexOf(prefix) === 0 ? i : i - 1;
return catalog[i + offset];
}
function oscar0p33Path(catalog, attr) {
var file = lookupOscar(catalog, attr.date);
return file ? [OSCAR_PATH, file].join("/") : null;
}
function oscarpath(catalognew, attr) {
var file = lookupOscar(catalognew, attr.date);
return file ? [OSCAR_PATH, file].join("/") : null;
}
function oscarDate(catalog, attr) {
var file = lookupOscar(catalog, attr.date);
var parts = file ? µ.ymdRedelimit(file, "", "/").split("/") : null;
return parts
? new Date(Date.UTC(+parts[0], parts[1] - 1, +parts[2], 0))
: null;
}
/**
* @returns {Date} the chronologically next or previous OSCAR data layer. How far forward or backward in
* time to jump is determined by the step and the catalog of available layers. A step of ±1 moves to the
* next/previous entry in the catalog (about 5 days), and a step of ±10 moves to the entry six positions away
* (about 30 days).
*/
function oscarStep(catalog, date, step) {
var file = lookupOscar(
catalog,
µ.dateToUTCymd(date, "/"),
step > 1 ? 6 : step < -1 ? -6 : step
);
var parts = file ? µ.ymdRedelimit(file, "", "/").split("/") : null;
return parts
? new Date(Date.UTC(+parts[0], parts[1] - 1, +parts[2], 0))
: null;
}
function dataSource(header) {
// noinspection FallthroughInSwitchStatementJS
switch (header.center || header.centerName) {
case -3:
return "OSCAR / Earth & Space Research";
case 7:
case "US National Weather Service, National Centres for Environmental Prediction (NCEP)":
return "GFS / NCEP / US National Weather Service";
default:
return header.centerName;
}
}
function bilinearInterpolateScalar(x, y, g00, g10, g01, g11) {
var rx = 1 - x;
var ry = 1 - y;
return g00 * rx * ry + g10 * x * ry + g01 * rx * y + g11 * x * y;
}
function bilinearInterpolateVector(x, y, g00, g10, g01, g11) {
// index++
var rx = 1 - x;
var ry = 1 - y;
var a = rx * ry,
b = x * ry,
c = rx * y,
d = x * y;
var u = g00[0] * a + g10[0] * b + g01[0] * c + g11[0] * d;
var v = g00[1] * a + g10[1] * b + g01[1] * c + g11[1] * d;
if (height && period) {
return [u, v, Math.sqrt(u * u + v * v), g01[2], g01[3]];
} else {
return [u, v, Math.sqrt(u * u + v * v)];
}
}
/**
* Builds an interpolator for the specified data in the form of JSON-ified GRIB files. Example:
*
* [
* {
* "header": {
* "refTime": "2013-11-30T18:00:00.000Z",
* "parameterCategory": 2,
* "parameterNumber": 2,
* "surface1Type": 100,
* "surface1Value": 100000.0,
* "forecastTime": 6,
* "scanMode": 0,
* "nx": 360,
* "ny": 181,
* "lo1": 0,
* "la1": 90,
* "lo2": 359,
* "la2": -90,
* "dx": 1,
* "dy": 1
* },
* "data": [3.42, 3.31, 3.19, 3.08, 2.96, 2.84, 2.72, 2.6, 2.47, ...]
* }
* ]
*
*/
function buildGrid(builder) {
// var builder = createBuilder(data);
var header = builder.header;
var λ0 = header.lo1,
φ0 = header.la1; // the grid's origin (e.g., 0.0E, 90.0N)
var Δλ = header.dx,
Δφ = header.dy; // distance between grid points (e.g., 2.5 deg lon, 2.5 deg lat)
var ni = header.nx,
nj = header.ny; // number of grid points W-E and N-S (e.g., 144 x 73)
var date = new Date(header.refTime);
date.setHours(date.getHours() + header.forecastTime);
// Scan mode 0 assumed. Longitude increases from λ0, and latitude decreases from φ0.
// http://www.nco.ncep.noaa.gov/pmb/docs/grib2/grib2_table3-4.shtml
var grid = [],
p = 0;
var isContinuous = Math.floor(ni * Δλ) >= 360;
for (var j = 0; j < nj; j++) {
var row = [];
for (var i = 0; i < ni; i++, p++) {
row[i] = builder.data(p);
}
if (isContinuous) {
// For wrapped grids, duplicate first column as last column to simplify interpolation logic
row.push(row[0]);
}
grid[j] = row;
}
function interpolate(λ, φ) {
var i = µ.floorMod(λ - λ0, 360) / Δλ; // calculate longitude index in wrapped range [0, 360)
var j = (φ0 - φ) / Δφ;
// calculate latitude index in direction +90 to -90
// 1 2 After converting λ and φ to fractional grid indexes i and j, we find the
// fi i ci four points "G" that enclose point (i, j). These points are at the four
// | =1.4 | corners specified by the floor and ceiling of i and j. For example, given
// ---G--|---G--- fj 8 i = 1.4 and j = 8.3, the four surrounding grid points are (1, 8), (2, 8),
// j ___|_ . | (1, 9) and (2, 9).
// =8.3 | |
// ---G------G--- cj 9 Note that for wrapped grids, the first column is duplicated as the last
// | | column, so the index ci can be used without taking a modulo.
var fi = Math.floor(i),
ci = fi + 1;
var fj = Math.floor(j),
cj = fj + 1;
var row;
if ((row = grid[fj])) {
var g00 = row[fi];
var g10 = row[ci];
if (µ.isValue(g00) && µ.isValue(g10) && (row = grid[cj])) {
var g01 = row[fi];
var g11 = row[ci];
if (µ.isValue(g01) && µ.isValue(g11)) {
// All four points found, so interpolate the value.
return builder.interpolate(i - fi, j - fj, g00, g10, g01, g11);
}
}
}
// console.log("cannot interpolate: " + λ + "," + φ + ": " + fi + " " + ci + " " + fj + " " + cj);
return null;
}
return {
source: dataSource(header),
date: date,
interpolate: interpolate,
forEachPoint: function (cb) {
for (var j = 0; j < nj; j++) {
var row = grid[j] || [];
for (var i = 0; i < ni; i++) {
cb(µ.floorMod(180 + λ0 + i * Δλ, 360) - 180, φ0 - j * Δφ, row[i]);
}
}
},
};
}
function productsFor(attributes) {
var attr = _.clone(attributes),