aboutsummaryrefslogtreecommitdiff
path: root/src/components/PredictedSpaceWeather.svelte
blob: 2282a407618d66418f88a1c00e8eff276f52c058 (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
<script lang="ts">
    import PredictedSpaceWeatherThing from './PredictedSpaceWeatherThing.svelte';

    import { onMount } from 'svelte';
    import { earth_weather, space_weather } from '../stores';

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

    let predictions;

    space_weather.subscribe(assembleWeatherData);
    earth_weather.subscribe(assembleWeatherData);

    async function assembleWeatherData() {
        if ($space_weather.updating || $earth_weather.updating) {
            predictions = null;
            return;
        }

        // First just reorganize the space_weather data
        let forecast = $space_weather.usnoaa_data_raw.noaa_planetary_k_index_forecast.filter(x => x.observed !== "observed");
        predictions = forecast.map(
            pred => ({
                "time": pred.time,
                "kp": pred.kp,
                "observed": pred.observed,
                "temp": null,
                "clouds": null,
                "hasNOMETData": $earth_weather.available
            })
        );

        // Add earth weather data if it is available
        if ($earth_weather.available) {
            predictions.forEach((pred, i) => {
                let closestDate = new Date(0,0,0);
                let temp;
                let clouds;

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

                predictions[i] = {
                    ...predictions[i], "temp": temp, "clouds": clouds
                }
            });
        }

    }

</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;
        box-shadow: 0px -6px 7px 0px black;
    }

    @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>