-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask.ts
153 lines (133 loc) · 5.9 KB
/
task.ts
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
import moment from 'moment';
import { Static, TSchema, Type } from '@sinclair/typebox';
import { Feature, Polygon } from 'geojson';
import ETL, { Event, SchemaType, handler as internal, local, InputFeatureCollection, InputFeature } from '@tak-ps/etl';
export default class Task extends ETL {
static name = 'etl-caic';
async schema(type: SchemaType = SchemaType.Input): Promise<TSchema> {
if (type === SchemaType.Input) {
return Type.Object({
'DEBUG': Type.Boolean({ description: 'Print results in logs', default: false, })
})
} else {
return Type.Object({
forecaster: Type.String(),
issueDateTime: Type.String({ format: 'date-time' }),
expiryDateTime: Type.String(),
isTranslated: Type.Boolean(),
rating: Type.String(),
ratingAbove: Type.String(),
ratingNear: Type.String(),
ratingBelow: Type.String()
});
}
}
async control(): Promise<void> {
//const layer = await this.layer();
const dateTime = moment().toISOString();
const res = await fetch(`https://avalanche.state.co.us/api-proxy/avid?_api_proxy_uri=%2Fproducts%2Fall%2Farea%3FproductType%3Davalancheforecast%26datetime%3D${encodeURIComponent(dateTime)}%26includeExpired%3Dfalse`, {
method: 'GET'
});
if (!res.ok) throw new Error('Error fetching Forecast Geometries');
const featMap = new Map<string, Feature>();
(await res.json()).features.map((feat: Feature) => {
featMap.set(String(feat.id), feat);
});
const res2 = await fetch(`https://avalanche.state.co.us/api-proxy/avid?_api_proxy_uri=%2Fproducts%2Fall%3Fdatetime%3D${encodeURIComponent(dateTime)}%26includeExpired%3Dfalse`, {
method: 'GET'
});
if (!res2.ok) throw new Error('Error fetching Forecast');
const products = await res2.json();
const fc: Static<typeof InputFeatureCollection> = {
type: 'FeatureCollection',
features: []
};
const forecasts: Array<{
id: string;
title: string;
publicName: string;
type: string;
polygons: Array<string>;
areaId: string;
forecaster: string;
issueDateTime: string;
expiryDateTime: string;
isTranslated: boolean;
weatherSummary: unknown;
avalancheSummary: {
days: Array<{
date: string;
content: string;
}>
}
dangerRatings: {
days: Array<{
alp: string;
tln: string;
btl: string;
}>
}
}> = products.filter((f: any) => { return f.type === 'avalancheforecast' });
let severity = [ 'extreme', 'high', 'considerable', 'moderate', 'low', 'noRating' ];
const fills: Record<string, string> = {
extreme: '#221e1f',
high: '#ee1d23',
considerable: '#f8931d',
moderate: '#fef102',
low: '#4db748',
noRating: '#ffffff'
};
for (const f of forecasts) {
if (!f.avalancheSummary.days.length) continue;
const featGeometry = featMap.get(f.areaId);
if (!featGeometry) continue;
let severityIndex = severity.indexOf('noRating');
if (severity.indexOf(f.dangerRatings.days[0].btl) < severityIndex) severityIndex = severity.indexOf(f.dangerRatings.days[0].btl);
if (severity.indexOf(f.dangerRatings.days[0].tln) < severityIndex) severityIndex = severity.indexOf(f.dangerRatings.days[0].tln);
if (severity.indexOf(f.dangerRatings.days[0].alp) < severityIndex) severityIndex = severity.indexOf(f.dangerRatings.days[0].alp);
const feature: Static<typeof InputFeature> = {
id: `caic-${f.areaId}`,
type: 'Feature',
properties: {
callsign: severity[severityIndex],
fill: fills[severity[severityIndex]],
'fill-opacity': 0.5,
stroke: fills[severity[severityIndex]],
'stroke-opacity': 0.75,
remarks: f.avalancheSummary.days.length ? f.avalancheSummary.days[0].content : 'No Remarks',
metadata: {
forecaster: f.forecaster,
issueDateTime: f.issueDateTime,
expiryDateTime: f.expiryDateTime,
isTranslated: f.isTranslated,
ratingAbove: f.dangerRatings.days[0].alp,
ratingNear: f.dangerRatings.days[0].tln,
ratingBelow: f.dangerRatings.days[0].btl,
}
},
geometry: featGeometry.geometry as Polygon
};
if (feature.geometry.type.startsWith('Multi')) {
feature.geometry.coordinates.forEach((coords: any, idx: number) => {
fc.features.push({
id: feature.id + '-' + idx,
type: 'Feature',
properties: feature.properties,
geometry: {
// @ts-expect-error -- Cast to ENUM
type: feature.geometry.type.replace('Multi', ''),
coordinates: coords
}
});
});
} else {
fc.features.push(feature)
}
}
await this.submit(fc);
}
}
await local(new Task(import.meta.url), import.meta.url);
export async function handler(event: Event = {}) {
return await internal(new Task(import.meta.url), event);
}