@nexgen-os/open-meteo 0.0.0-stage → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +10 -2
- package/dist/index.d.ts +9 -0
- package/dist/index.js +146 -0
- package/package.json +35 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Nexgen OS contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,11 @@
|
|
|
1
|
-
#
|
|
1
|
+
# `@nexgen-os/open-meteo` — weather forecast and ERA5 history
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Hourly forecast of temperature and irradiance and history from the ERA5 archive (irradiance on a plane, horizontal components `ghi`, `dni`, `dhi`, temperature, wind speed), worldwide, as a `WeatherProvider` of `@nexgen-os/core`. Source `open_meteo`. MIT; the data is CC BY 4.0, and the free API is for non-commercial use, a paid plan otherwise.
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
import { openMeteo } from '@nexgen-os/open-meteo';
|
|
7
|
+
|
|
8
|
+
const weather = openMeteo();
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
What it returns lands through `storeSeries()` of `@nexgen-os/core`, each series with its source. Decision [0011](../../../docs/decisions/0011-external-data.md).
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Fetch } from '@nexgen-os/core';
|
|
2
|
+
import { type WeatherProvider } from '@nexgen-os/core';
|
|
3
|
+
export declare const SOURCE_CODE = "open_meteo";
|
|
4
|
+
export declare function openMeteo(options?: {
|
|
5
|
+
baseUrl?: string;
|
|
6
|
+
archiveUrl?: string;
|
|
7
|
+
fetch?: Fetch;
|
|
8
|
+
now?: () => Date;
|
|
9
|
+
}): WeatherProvider;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
// Open-Meteo — https://open-meteo.com, weather forecasts and archives,
|
|
2
|
+
// worldwide, CC BY 4.0, no key for non-commercial use. The open
|
|
3
|
+
// WeatherProvider that ships with the core.
|
|
4
|
+
//
|
|
5
|
+
// Subtleties of the API are kept straight here so that nobody downstream
|
|
6
|
+
// has to know them: times come back without an offset (UTC is asked for and
|
|
7
|
+
// marked); the radiation of an hour is the average of the hour *before* the
|
|
8
|
+
// stamp, so it is stored as an interval starting one hour earlier; wind
|
|
9
|
+
// speeds come in km/h unless metres per second are asked for; the azimuth of
|
|
10
|
+
// a plane is counted from south, west positive, where the core counts from
|
|
11
|
+
// true north, clockwise.
|
|
12
|
+
import { azimuthFromSouth } from '@nexgen-os/core';
|
|
13
|
+
export const SOURCE_CODE = 'open_meteo';
|
|
14
|
+
/** Open-Meteo hourly forecast variables, to the core's quantities. Units already match (°C, W/m²). */
|
|
15
|
+
const VARIABLES = {
|
|
16
|
+
temperature: { variable: 'temperature_2m', value_kind: 'instant', precedingHour: false },
|
|
17
|
+
irradiance: { variable: 'shortwave_radiation', value_kind: 'interval', precedingHour: true },
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Hourly archive variables (ERA5 reanalysis), to the core's quantities, in
|
|
21
|
+
* the core's units once `wind_speed_unit=ms` is asked. `poa_irradiance` is
|
|
22
|
+
* Open-Meteo's `global_tilted_irradiance`: the transposition to the plane is
|
|
23
|
+
* done by Open-Meteo for the tilt and azimuth given. `ghi`, `dni` and `dhi`
|
|
24
|
+
* are the horizontal components an engine transposes itself.
|
|
25
|
+
*/
|
|
26
|
+
const HISTORY_VARIABLES = {
|
|
27
|
+
poa_irradiance: { variable: 'global_tilted_irradiance', value_kind: 'interval', precedingHour: true },
|
|
28
|
+
irradiance: { variable: 'shortwave_radiation', value_kind: 'interval', precedingHour: true },
|
|
29
|
+
ghi: { variable: 'shortwave_radiation', value_kind: 'interval', precedingHour: true },
|
|
30
|
+
dni: { variable: 'direct_normal_irradiance', value_kind: 'interval', precedingHour: true },
|
|
31
|
+
dhi: { variable: 'diffuse_radiation', value_kind: 'interval', precedingHour: true },
|
|
32
|
+
temperature: { variable: 'temperature_2m', value_kind: 'instant', precedingHour: false },
|
|
33
|
+
wind_speed: { variable: 'wind_speed_10m', value_kind: 'instant', precedingHour: false },
|
|
34
|
+
};
|
|
35
|
+
const HOUR_MS = 3_600_000;
|
|
36
|
+
const DAY_MS = 24 * HOUR_MS;
|
|
37
|
+
export function openMeteo(options = {}) {
|
|
38
|
+
const base = (options.baseUrl ?? 'https://api.open-meteo.com').replace(/\/+$/, '');
|
|
39
|
+
const archive = (options.archiveUrl ?? 'https://archive-api.open-meteo.com').replace(/\/+$/, '');
|
|
40
|
+
const doFetch = options.fetch ?? globalThis.fetch;
|
|
41
|
+
const now = options.now ?? (() => new Date());
|
|
42
|
+
const getHourly = async (url) => {
|
|
43
|
+
const response = await doFetch(url, { headers: { Accept: 'application/json' } });
|
|
44
|
+
if (!response.ok)
|
|
45
|
+
throw new Error(`source_unavailable: ${response.status} from Open-Meteo`);
|
|
46
|
+
return (await response.json());
|
|
47
|
+
};
|
|
48
|
+
return {
|
|
49
|
+
source_code: SOURCE_CODE,
|
|
50
|
+
async forecast(where, opts = {}) {
|
|
51
|
+
const quantities = (opts.quantities ?? Object.keys(VARIABLES)).filter((q) => q in VARIABLES);
|
|
52
|
+
if (quantities.length === 0)
|
|
53
|
+
throw new Error('quantity_unknown: Open-Meteo gives temperature and irradiance');
|
|
54
|
+
const params = new URLSearchParams({
|
|
55
|
+
latitude: String(where.lat),
|
|
56
|
+
longitude: String(where.lon),
|
|
57
|
+
hourly: quantities.map((q) => VARIABLES[q].variable).join(','),
|
|
58
|
+
forecast_days: String(Math.max(1, Math.ceil((opts.hours ?? 48) / 24))),
|
|
59
|
+
timezone: 'UTC',
|
|
60
|
+
});
|
|
61
|
+
const body = await getHourly(`${base}/v1/forecast?${params}`);
|
|
62
|
+
const issued_at = now().toISOString();
|
|
63
|
+
const times = (body.hourly?.time ?? []);
|
|
64
|
+
const limit = opts.hours ?? times.length;
|
|
65
|
+
const series = quantities.map((quantity_code) => {
|
|
66
|
+
const spec = VARIABLES[quantity_code];
|
|
67
|
+
const column = (body.hourly?.[spec.variable] ?? []);
|
|
68
|
+
const values = times.slice(0, limit).flatMap((t, i) => {
|
|
69
|
+
const value = column[i];
|
|
70
|
+
if (value === null || value === undefined)
|
|
71
|
+
return [];
|
|
72
|
+
const stamp = new Date(`${t}Z`);
|
|
73
|
+
if (spec.precedingHour)
|
|
74
|
+
stamp.setUTCHours(stamp.getUTCHours() - 1);
|
|
75
|
+
return [{ at: stamp.toISOString(), value }];
|
|
76
|
+
});
|
|
77
|
+
return { quantity_code, value_kind: spec.value_kind, step: '1 hour', values };
|
|
78
|
+
});
|
|
79
|
+
return { issued_at, series };
|
|
80
|
+
},
|
|
81
|
+
/**
|
|
82
|
+
* The ERA5 archive, hourly, for the days `from` to `to` included. Every
|
|
83
|
+
* hour of those days is covered: the mean of the last hour of `to` is
|
|
84
|
+
* stamped at midnight the day after, so one more day is asked and the
|
|
85
|
+
* values outside the period are left out. A null (an hour the archive
|
|
86
|
+
* does not have yet) is left out, never stored as zero. Without `model`,
|
|
87
|
+
* Open-Meteo blends its reanalyses (`best_match`: ERA5 with ERA5-Land
|
|
88
|
+
* and others where they exist); `model: 'era5'` asks ERA5 alone, on its
|
|
89
|
+
* 0.25° grid.
|
|
90
|
+
*/
|
|
91
|
+
async history(where, from, to, opts = {}) {
|
|
92
|
+
const start = dayStart(from);
|
|
93
|
+
const end = dayStart(to) + DAY_MS;
|
|
94
|
+
if (end <= start)
|
|
95
|
+
throw new Error(`period_empty: ${from} to ${to}`);
|
|
96
|
+
const oriented = opts.tilt_deg !== undefined && opts.azimuth_deg !== undefined;
|
|
97
|
+
const quantities = opts.quantities ?? [oriented ? 'poa_irradiance' : 'irradiance', 'temperature', 'wind_speed'];
|
|
98
|
+
const unknown = quantities.filter((q) => !(q in HISTORY_VARIABLES));
|
|
99
|
+
if (unknown.length > 0)
|
|
100
|
+
throw new Error(`quantity_unknown: ${unknown.join(', ')}; the Open-Meteo archive gives ${Object.keys(HISTORY_VARIABLES).join(', ')}`);
|
|
101
|
+
if (quantities.includes('poa_irradiance') && !oriented)
|
|
102
|
+
throw new Error('orientation_required: poa_irradiance needs tilt_deg and azimuth_deg');
|
|
103
|
+
const params = new URLSearchParams({
|
|
104
|
+
latitude: String(where.lat),
|
|
105
|
+
longitude: String(where.lon),
|
|
106
|
+
start_date: from,
|
|
107
|
+
end_date: new Date(end).toISOString().slice(0, 10),
|
|
108
|
+
hourly: quantities.map((q) => HISTORY_VARIABLES[q].variable).join(','),
|
|
109
|
+
wind_speed_unit: 'ms',
|
|
110
|
+
timezone: 'GMT',
|
|
111
|
+
timeformat: 'unixtime',
|
|
112
|
+
});
|
|
113
|
+
if (opts.model)
|
|
114
|
+
params.set('models', opts.model);
|
|
115
|
+
if (oriented) {
|
|
116
|
+
params.set('tilt', String(opts.tilt_deg));
|
|
117
|
+
params.set('azimuth', String(azimuthFromSouth(opts.azimuth_deg)));
|
|
118
|
+
}
|
|
119
|
+
const body = await getHourly(`${archive}/v1/archive?${params}`);
|
|
120
|
+
if (!body.hourly)
|
|
121
|
+
throw new Error(`source_unavailable: Open-Meteo archive answered without hourly values${body.reason ? ` (${body.reason})` : ''}`);
|
|
122
|
+
const stamps = body.hourly.time;
|
|
123
|
+
return quantities.map((quantity_code) => {
|
|
124
|
+
const spec = HISTORY_VARIABLES[quantity_code];
|
|
125
|
+
const column = (body.hourly?.[spec.variable] ?? []);
|
|
126
|
+
const values = stamps.flatMap((unix, i) => {
|
|
127
|
+
const value = column[i];
|
|
128
|
+
const at = unix * 1000 - (spec.precedingHour ? HOUR_MS : 0);
|
|
129
|
+
if (value === null || value === undefined || at < start || at >= end)
|
|
130
|
+
return [];
|
|
131
|
+
return [{ at: new Date(at).toISOString(), value }];
|
|
132
|
+
});
|
|
133
|
+
return { quantity_code, value_kind: spec.value_kind, step: '1 hour', values };
|
|
134
|
+
});
|
|
135
|
+
},
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
/** `YYYY-MM-DD` → milliseconds at 00:00 UTC that day. */
|
|
139
|
+
function dayStart(day) {
|
|
140
|
+
if (!/^\d{4}-\d{2}-\d{2}$/.test(day))
|
|
141
|
+
throw new Error(`date_unreadable: ${day} (YYYY-MM-DD expected)`);
|
|
142
|
+
const t = Date.parse(`${day}T00:00:00Z`);
|
|
143
|
+
if (Number.isNaN(t))
|
|
144
|
+
throw new Error(`date_unreadable: ${day}`);
|
|
145
|
+
return t;
|
|
146
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,37 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nexgen-os/open-meteo",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.4.1",
|
|
4
|
+
"description": "Open-Meteo weather forecast and ERA5 history as a Nexgen OS weather connector.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/nexgen-os/nexgen-os.git",
|
|
9
|
+
"directory": "packages/connectors/open-meteo"
|
|
10
|
+
},
|
|
11
|
+
"publishConfig": {
|
|
12
|
+
"access": "public"
|
|
13
|
+
},
|
|
14
|
+
"type": "module",
|
|
15
|
+
"engines": {
|
|
16
|
+
"node": ">=20"
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"dist",
|
|
20
|
+
"README.md",
|
|
21
|
+
"LICENSE"
|
|
22
|
+
],
|
|
23
|
+
"main": "./dist/index.js",
|
|
24
|
+
"types": "./dist/index.d.ts",
|
|
25
|
+
"exports": {
|
|
26
|
+
".": {
|
|
27
|
+
"types": "./dist/index.d.ts",
|
|
28
|
+
"import": "./dist/index.js"
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
"scripts": {
|
|
32
|
+
"build": "tsc -p tsconfig.build.json"
|
|
33
|
+
},
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"@nexgen-os/core": "^0.4.0"
|
|
36
|
+
}
|
|
37
|
+
}
|