aboutsummaryrefslogtreecommitdiff
path: root/src/components/PredictedSpaceWeather.svelte
blob: 27fd02dadbc6face179807bb960868b40819ca89 (plain) (blame)
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
<script lang="ts">
    import PredictedSpaceWeatherThing from './PredictedSpaceWeatherThing.svelte';
    import { onMount } from 'svelte';

    const monthNames = ["January", "February", "March", "April", "May", "June",
      "July", "August", "September", "October", "November", "December"
    ];

    let longitude;
    let latitude;
    let locationSupported = false;
    let dataLoading = true;

    let predictions;

    async function getWeather(longitude, latitude) {
        let yr_data;
        if (locationSupported) {
            yr_data = await fetch(`https://api.met.no/weatherapi/locationforecast/2.0/compact?lat=${latitude}&lon=${longitude}`).then(res => res.json());
        }
        let kp_data = await fetch("https://services.swpc.noaa.gov/products/noaa-planetary-k-index-forecast.json").then(res => res.json());
        kp_data.shift();

        let updatedPredictions = [];
        kp_data.forEach((pred, i) => {
            if (pred[2] != "observed") {
                let temp;
                let clouds;
                let cDate = new Date(pred[0]);
                let closestDate = new Date(0,0,0);

                if (locationSupported) {
                    yr_data["properties"]["timeseries"].forEach((pred, i) => {
                        let predDate = new Date(pred["time"]);
                        if (Math.abs(predDate.getTime() - cDate.getTime()) < Math.abs(closestDate.getTime() - cDate.getTime())) {
                            closestDate = predDate;
                            temp = (pred["data"]["instant"]["details"]["air_temperature"]);
                            clouds = pred["data"]["instant"]["details"]["cloud_area_fraction"];
                        }
                    });
                }

                updatedPredictions.push({
                    "time": pred[0],
                    "kp": pred[1],
                    "temp": temp,
                    "clouds": clouds,
                    "hasNOMETData": locationSupported
                });
            }
        });
        predictions = updatedPredictions;
    }

    function getLocation() {
        if (navigator.geolocation) {
            dataLoading = true
            locationSupported = true
            navigator.geolocation.getCurrentPosition(setLocation, locationError)
        } else {
            locationSupported = false
            noLocation()
        }
    }

    function setLocation(position) {
        longitude = position.coords.longitude
        latitude = position.coords.latitude
        getWeather(longitude, latitude)
    }

    function locationError(err) {
        noLocation()
    }

    function noLocation() {
        longitude = 28.283333
        latitude = -15.416667
        getWeather(0, 0);
        toggleLoading()
    }
    function toggleLoading() {
        dataLoading = !dataLoading
    }

    onMount(getLocation);
</script>

<style>
    .predicted-weather {
        border-top-left-radius: 2rem;
        border-top-right-radius: 2rem;
        --bg-opacity: 1;
        background-color: #f7fafc;
        background-color: rgba(247, 250, 252, var(--bg-opacity));
        padding: 1.5rem;
        --text-opacity: 1;
        color: #1a202c;
        color: rgba(26, 32, 44, var(--text-opacity));
        height: 100%;
        overflow: hidden;
        align-self: stretch;
    }

    @media (min-width: 640px), (min-height: 720px) {
        .predicted-weather {
            padding: 2rem;
            padding-top: 1.5rem;
        }
    }

    @media (min-width: 640px) {
        .predicted-weather {
            border-bottom-right-radius: 1rem;
            border-bottom-left-radius: 1rem;
        }
    }

    .predicted-weather h2 {
        text-transform: uppercase;
        font-size: 0.875rem;
        letter-spacing: 0.1em;
        font-weight: 700;
        margin-top: 0.25rem;
        margin-bottom: 0.5rem;
    }

    .prediction-table {
        height: 100%;
        overflow-y: scroll;
        padding-bottom: 1rem;
    }

    .prediction-table::-webkit-scrollbar {
        display: none;
    }

    .no-data {
        height: 100%;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        text-align: center;
        color: grey;

    }

    .no-data {
        margin-top: 1rem;
    }
</style>

<div class="predicted-weather">
    {#if predictions}
    	<div className="flex flex-row justify-between items-top">
    		<h2>Predicted</h2>
    	</div>
        <div class="prediction-table">
            {#each predictions as prediction, i}
                <PredictedSpaceWeatherThing {prediction}/>
            {/each}
        </div>
    {:else}
        <div class="no-data">
            <i class="fas fa-7x fa-exclamation-triangle"></i>
            <p>No prediction data</p>
        </div>
    {/if}
</div>