@nexgen-os/utilityrate5 0.0.0-stage → 0.5.0
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 +55 -2
- package/dist/index.d.ts +67 -0
- package/dist/index.js +271 -0
- package/package.json +32 -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,56 @@
|
|
|
1
|
-
#
|
|
1
|
+
# `@nexgen-os/utilityrate5` — SAM electricity rates ↔ plain rates
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Reads and writes the electricity rate of a case of the
|
|
4
|
+
[System Advisor Model](https://sam.nrel.gov) (SAM, NREL): the `ur_` inputs
|
|
5
|
+
of its `utilityrate5` compute module, as SAM writes a case to JSON and
|
|
6
|
+
PySAM's `export()` gives them, flat or under their group. MIT; no
|
|
7
|
+
dependency, no database, no network.
|
|
8
|
+
|
|
9
|
+
| Inputs | Read as | Notes |
|
|
10
|
+
|---|---|---|
|
|
11
|
+
| `ur_ec_tou_mat` | `energyCharges`: `period`, `tier`, `maxUsage`, `maxUsageUnit` (`kWh`, `kWh/kW`, `kWh daily`, `kWh/kW daily`), `buy`, `sell` | rates per kWh in the file's currency, which the file does not name |
|
|
12
|
+
| `ur_ec_sched_weekday`, `ur_ec_sched_weekend` | `weekday`, `weekend`: 12 months × 24 hours, the period of each hour | every period named is one `ur_ec_tou_mat` charges |
|
|
13
|
+
| `ur_sell_eq_buy` | `sellEqualsBuy` | |
|
|
14
|
+
| `ur_en_ts_buy_rate` and `ur_ts_buy_rate`, `ur_en_ts_sell_rate` and `ur_ts_sell_rate` | `timeSeriesBuy`, `timeSeriesSell` when turned on | equal steps over 8760 hours (hourly, quarter-hourly…) |
|
|
15
|
+
| every other `ur_` key: demand charges, fixed and minimum charges, metering option, a time series turned off | `other`, as read | written back as read |
|
|
16
|
+
|
|
17
|
+
`writeUtilityRate()` gives the flat `ur_` inputs back, the same keys in the
|
|
18
|
+
same values; `JSON.stringify` makes the file.
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
import { hourlyRates, rateFromHourly, readUtilityRate, writeUtilityRate } from '@nexgen-os/utilityrate5';
|
|
22
|
+
|
|
23
|
+
const rate = readUtilityRate(samCaseJson);
|
|
24
|
+
const { stepMinutes, buy, sell } = hourlyRates(rate); // a rate at each hour of SAM's year
|
|
25
|
+
const back = writeUtilityRate(rateFromHourly(buy, sell)); // the 12 × 24 schedules again
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## A rate at each hour of the year
|
|
29
|
+
|
|
30
|
+
SAM reads a rate over its own year: 365 days of 24 hours in local standard
|
|
31
|
+
time, from 1 January, the first day a Monday, Saturdays and Sundays on the
|
|
32
|
+
weekend schedule. `hourlyRates()` gives the buy and the sell rate of each
|
|
33
|
+
step of that year: the time-series rates where the rate has them, the
|
|
34
|
+
period of the hour's schedule otherwise, the buy rate when energy is sold
|
|
35
|
+
at the rate it is bought. Placing that year on a calendar and a time zone
|
|
36
|
+
is the reader's: the file holds neither.
|
|
37
|
+
|
|
38
|
+
`rateFromHourly(buy, sell?)` folds an hourly year back: when every hour of a
|
|
39
|
+
month, a time of day and a kind of day carries one buy and sell rate, it is
|
|
40
|
+
the 12 × 24 weekday and weekend schedules, one single-tier period for each
|
|
41
|
+
pair of rates in the order they first come; otherwise the same year as
|
|
42
|
+
time-series rates. Either way `hourlyRates()` gives the same year back.
|
|
43
|
+
|
|
44
|
+
What it never does: compute a bill, apply a tier, a demand charge or a
|
|
45
|
+
metering option, or name a currency. Writing a rate as the price series of
|
|
46
|
+
a contract is an importer's job.
|
|
47
|
+
|
|
48
|
+
## Refusals
|
|
49
|
+
|
|
50
|
+
`utilityrate5_unreadable` (not JSON, not an object, no energy charges and
|
|
51
|
+
no time-series buy rate, a row or a series that is not what SAM writes),
|
|
52
|
+
`utilityrate5_schedule_invalid` (a schedule that is not 12 × 24, or names a
|
|
53
|
+
period no charge has), `utilityrate5_tiered` (an hourly rate asked of a
|
|
54
|
+
period whose rate depends on the usage), `utilityrate5_series_length` (an
|
|
55
|
+
hourly year that is not 8760 hours, time-series buy and sell rates of
|
|
56
|
+
different steps).
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/** How the usage limit of a tier is counted (`ur_ec_tou_mat`, fourth column). */
|
|
2
|
+
export type UsageLimitUnit = 'kWh' | 'kWh/kW' | 'kWh daily' | 'kWh/kW daily';
|
|
3
|
+
/** One row of `ur_ec_tou_mat`: what a period charges and pays up to a usage. */
|
|
4
|
+
export interface EnergyCharge {
|
|
5
|
+
/** From 1, as the schedules name it. */
|
|
6
|
+
period: number;
|
|
7
|
+
/** From 1; the rate of tier n holds up to its `maxUsage`, then tier n + 1. */
|
|
8
|
+
tier: number;
|
|
9
|
+
/** In `maxUsageUnit`; SAM writes 1e38 for no limit. */
|
|
10
|
+
maxUsage: number;
|
|
11
|
+
maxUsageUnit: UsageLimitUnit;
|
|
12
|
+
/** Per kWh bought, in the file's currency. */
|
|
13
|
+
buy: number;
|
|
14
|
+
/** Per kWh sold, in the file's currency. */
|
|
15
|
+
sell: number;
|
|
16
|
+
}
|
|
17
|
+
/** Twelve months of 24 hours, each the period it falls in, from 1. */
|
|
18
|
+
export type MonthHourSchedule = number[][];
|
|
19
|
+
export interface UtilityRate {
|
|
20
|
+
energyCharges: EnergyCharge[];
|
|
21
|
+
weekday: MonthHourSchedule;
|
|
22
|
+
weekend: MonthHourSchedule;
|
|
23
|
+
/** `ur_sell_eq_buy`: energy sold at the rate it is bought. */
|
|
24
|
+
sellEqualsBuy: boolean;
|
|
25
|
+
/** `ur_ts_buy_rate` when `ur_en_ts_buy_rate` is on: per kWh, at equal steps over the year. */
|
|
26
|
+
timeSeriesBuy: number[] | null;
|
|
27
|
+
/** `ur_ts_sell_rate` when `ur_en_ts_sell_rate` is on. */
|
|
28
|
+
timeSeriesSell: number[] | null;
|
|
29
|
+
/** Every other `ur_` key as read, and a time series that was off. */
|
|
30
|
+
other: Record<string, unknown>;
|
|
31
|
+
}
|
|
32
|
+
/** The rates of a year, at equal steps from the first hour of SAM's year. */
|
|
33
|
+
export interface HourlyRates {
|
|
34
|
+
stepMinutes: number;
|
|
35
|
+
buy: number[];
|
|
36
|
+
sell: number[];
|
|
37
|
+
}
|
|
38
|
+
/** SAM's year: 365 days, January to December. */
|
|
39
|
+
export declare const DAYS_IN_MONTH: number[];
|
|
40
|
+
export declare const HOURS_IN_YEAR = 8760;
|
|
41
|
+
/**
|
|
42
|
+
* Reads the rate of a SAM case: its JSON text, or the object SAM or
|
|
43
|
+
* PySAM's `export()` gives. Refuses `utilityrate5_unreadable` and
|
|
44
|
+
* `utilityrate5_schedule_invalid`.
|
|
45
|
+
*/
|
|
46
|
+
export declare function readUtilityRate(input: unknown): UtilityRate;
|
|
47
|
+
/**
|
|
48
|
+
* Writes a rate as the flat `ur_` inputs of a SAM case, the keys it was
|
|
49
|
+
* read from in the same values; `JSON.stringify` gives the file.
|
|
50
|
+
*/
|
|
51
|
+
export declare function writeUtilityRate(rate: UtilityRate): Record<string, unknown>;
|
|
52
|
+
/**
|
|
53
|
+
* The buy and sell rate of each hour of SAM's year, per kWh in the file's
|
|
54
|
+
* currency: the time-series rates where the rate has them, the period of
|
|
55
|
+
* the hour's schedule otherwise. A period of several tiers has no rate of
|
|
56
|
+
* its own at an hour, its tiers depend on the usage: refused
|
|
57
|
+
* `utilityrate5_tiered`.
|
|
58
|
+
*/
|
|
59
|
+
export declare function hourlyRates(rate: UtilityRate): HourlyRates;
|
|
60
|
+
/**
|
|
61
|
+
* The rate an hourly year of buy and sell rates is: the 12 × 24 weekday and
|
|
62
|
+
* weekend schedules when every hour of a month, a time of day and a kind of
|
|
63
|
+
* day has one buy and sell rate, a period per pair in the order they first
|
|
64
|
+
* come; the same year as time-series rates otherwise. `sell` omitted is
|
|
65
|
+
* energy sold at the rate it is bought. Refuses `utilityrate5_series_length`.
|
|
66
|
+
*/
|
|
67
|
+
export declare function rateFromHourly(buy: number[], sell?: number[]): UtilityRate;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
// SAM utilityrate5 rates → plain rates, and back.
|
|
2
|
+
//
|
|
3
|
+
// The System Advisor Model (SAM, NREL) describes what a site pays and is
|
|
4
|
+
// paid for its electricity with the inputs of its `utilityrate5` compute
|
|
5
|
+
// module, the `ur_` keys: the energy charges by period and tier
|
|
6
|
+
// (`ur_ec_tou_mat`), the period of each hour of each month on weekdays and
|
|
7
|
+
// on weekends (`ur_ec_sched_weekday`, `ur_ec_sched_weekend`, 12 × 24), and
|
|
8
|
+
// buy and sell rates given hour by hour instead (`ur_ts_buy_rate`,
|
|
9
|
+
// `ur_ts_sell_rate`). They travel as the JSON SAM writes for a case and
|
|
10
|
+
// PySAM's `export()` gives, flat or under their group.
|
|
11
|
+
//
|
|
12
|
+
// The rates read here carry what the file says: rates per kWh in the file's
|
|
13
|
+
// currency, which the file does not name, periods and tiers numbered as SAM
|
|
14
|
+
// numbers them. Every other `ur_` key (demand charges, fixed and minimum
|
|
15
|
+
// charges, metering option) is carried as read, so that a rate written back
|
|
16
|
+
// gives the same keys and values.
|
|
17
|
+
//
|
|
18
|
+
// A rate is also a price at each hour of a year, the way SAM reads it: 365
|
|
19
|
+
// days of 24 hours in local standard time, the first a Monday, Saturday and
|
|
20
|
+
// Sunday on the weekend schedule (`hourlyRates`). An hourly year goes back to
|
|
21
|
+
// the 12 × 24 schedules when it is one (`rateFromHourly`), and is written as
|
|
22
|
+
// time-series rates otherwise.
|
|
23
|
+
//
|
|
24
|
+
// The library imports nothing, opens no connection and calls no network:
|
|
25
|
+
// writing a rate as a price series of the model is an importer's job.
|
|
26
|
+
//
|
|
27
|
+
// SPDX-License-Identifier: MIT
|
|
28
|
+
/** SAM's year: 365 days, January to December. */
|
|
29
|
+
export const DAYS_IN_MONTH = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
|
|
30
|
+
export const HOURS_IN_YEAR = 8760;
|
|
31
|
+
const UNITS = ['kWh', 'kWh/kW', 'kWh daily', 'kWh/kW daily'];
|
|
32
|
+
const TYPED = new Set([
|
|
33
|
+
'ur_ec_tou_mat',
|
|
34
|
+
'ur_ec_sched_weekday',
|
|
35
|
+
'ur_ec_sched_weekend',
|
|
36
|
+
'ur_sell_eq_buy',
|
|
37
|
+
'ur_en_ts_buy_rate',
|
|
38
|
+
'ur_ts_buy_rate',
|
|
39
|
+
'ur_en_ts_sell_rate',
|
|
40
|
+
'ur_ts_sell_rate',
|
|
41
|
+
]);
|
|
42
|
+
function isObject(value) {
|
|
43
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* The `ur_` keys of a SAM case: at the top of the object, or in the group
|
|
47
|
+
* PySAM's `export()` puts them under. Refuses `utilityrate5_unreadable`.
|
|
48
|
+
*/
|
|
49
|
+
function rateInputs(input) {
|
|
50
|
+
const parsed = typeof input === 'string' ? parseJson(input) : input;
|
|
51
|
+
if (!isObject(parsed))
|
|
52
|
+
throw new Error('utilityrate5_unreadable: a SAM case is a JSON object of inputs');
|
|
53
|
+
const found = {};
|
|
54
|
+
const take = (from) => {
|
|
55
|
+
for (const [key, value] of Object.entries(from))
|
|
56
|
+
if (key.startsWith('ur_'))
|
|
57
|
+
found[key] = value;
|
|
58
|
+
};
|
|
59
|
+
take(parsed);
|
|
60
|
+
for (const value of Object.values(parsed))
|
|
61
|
+
if (isObject(value))
|
|
62
|
+
take(value);
|
|
63
|
+
if (!('ur_ec_tou_mat' in found) && found.ur_en_ts_buy_rate !== 1) {
|
|
64
|
+
throw new Error('utilityrate5_unreadable: no ur_ec_tou_mat, and no time-series buy rate turned on');
|
|
65
|
+
}
|
|
66
|
+
return found;
|
|
67
|
+
}
|
|
68
|
+
function parseJson(text) {
|
|
69
|
+
try {
|
|
70
|
+
return JSON.parse(text.replace(/^/, ''));
|
|
71
|
+
}
|
|
72
|
+
catch {
|
|
73
|
+
throw new Error('utilityrate5_unreadable: not JSON');
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
function numbers(value, key) {
|
|
77
|
+
if (!Array.isArray(value) || value.some((v) => typeof v !== 'number' || !Number.isFinite(v))) {
|
|
78
|
+
throw new Error(`utilityrate5_unreadable: ${key} is not a list of numbers`);
|
|
79
|
+
}
|
|
80
|
+
return value;
|
|
81
|
+
}
|
|
82
|
+
function matrix(value, key) {
|
|
83
|
+
if (!Array.isArray(value))
|
|
84
|
+
throw new Error(`utilityrate5_unreadable: ${key} is not a matrix`);
|
|
85
|
+
return value.map((row) => numbers(row, key));
|
|
86
|
+
}
|
|
87
|
+
function flag(value) {
|
|
88
|
+
return value === 1 || value === true;
|
|
89
|
+
}
|
|
90
|
+
/** Checks a 12 × 24 schedule whose periods are those of the energy charges. */
|
|
91
|
+
function checkSchedule(schedule, key, periods) {
|
|
92
|
+
if (schedule.length !== 12 || schedule.some((row) => row.length !== 24)) {
|
|
93
|
+
throw new Error(`utilityrate5_schedule_invalid: ${key} is 12 months of 24 hours`);
|
|
94
|
+
}
|
|
95
|
+
for (const [m, row] of schedule.entries()) {
|
|
96
|
+
for (const [h, period] of row.entries()) {
|
|
97
|
+
if (!periods.has(period)) {
|
|
98
|
+
throw new Error(`utilityrate5_schedule_invalid: ${key} names period ${period} in month ${m + 1} at hour ${h}, which ur_ec_tou_mat does not charge`);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Reads the rate of a SAM case: its JSON text, or the object SAM or
|
|
105
|
+
* PySAM's `export()` gives. Refuses `utilityrate5_unreadable` and
|
|
106
|
+
* `utilityrate5_schedule_invalid`.
|
|
107
|
+
*/
|
|
108
|
+
export function readUtilityRate(input) {
|
|
109
|
+
const inputs = rateInputs(input);
|
|
110
|
+
const energyCharges = 'ur_ec_tou_mat' in inputs
|
|
111
|
+
? matrix(inputs.ur_ec_tou_mat, 'ur_ec_tou_mat').map((row) => {
|
|
112
|
+
if (row.length !== 6)
|
|
113
|
+
throw new Error('utilityrate5_unreadable: a row of ur_ec_tou_mat is period, tier, max usage, its unit, buy, sell');
|
|
114
|
+
const [period, tier, maxUsage, unit, buy, sell] = row;
|
|
115
|
+
const maxUsageUnit = UNITS[unit];
|
|
116
|
+
if (!maxUsageUnit)
|
|
117
|
+
throw new Error(`utilityrate5_unreadable: max usage unit ${unit} in ur_ec_tou_mat (0 to 3)`);
|
|
118
|
+
return { period, tier, maxUsage, maxUsageUnit, buy, sell };
|
|
119
|
+
})
|
|
120
|
+
: [];
|
|
121
|
+
const periods = new Set(energyCharges.map((c) => c.period));
|
|
122
|
+
const schedule = (key) => {
|
|
123
|
+
if (!(key in inputs)) {
|
|
124
|
+
if (energyCharges.length === 0)
|
|
125
|
+
return [];
|
|
126
|
+
throw new Error(`utilityrate5_schedule_invalid: no ${key}`);
|
|
127
|
+
}
|
|
128
|
+
const s = matrix(inputs[key], key);
|
|
129
|
+
checkSchedule(s, key, periods);
|
|
130
|
+
return s;
|
|
131
|
+
};
|
|
132
|
+
const other = {};
|
|
133
|
+
for (const [key, value] of Object.entries(inputs))
|
|
134
|
+
if (!TYPED.has(key))
|
|
135
|
+
other[key] = value;
|
|
136
|
+
const series = (on, values) => {
|
|
137
|
+
if (!flag(inputs[on])) {
|
|
138
|
+
if (on in inputs)
|
|
139
|
+
other[on] = inputs[on];
|
|
140
|
+
if (values in inputs)
|
|
141
|
+
other[values] = inputs[values];
|
|
142
|
+
return null;
|
|
143
|
+
}
|
|
144
|
+
const v = numbers(inputs[values], values);
|
|
145
|
+
if (v.length === 0 || (v.length % HOURS_IN_YEAR !== 0 && HOURS_IN_YEAR % v.length !== 0)) {
|
|
146
|
+
throw new Error(`utilityrate5_unreadable: ${values} holds ${v.length} values, not equal steps over ${HOURS_IN_YEAR} hours`);
|
|
147
|
+
}
|
|
148
|
+
return v;
|
|
149
|
+
};
|
|
150
|
+
return {
|
|
151
|
+
energyCharges,
|
|
152
|
+
weekday: schedule('ur_ec_sched_weekday'),
|
|
153
|
+
weekend: schedule('ur_ec_sched_weekend'),
|
|
154
|
+
sellEqualsBuy: flag(inputs.ur_sell_eq_buy),
|
|
155
|
+
timeSeriesBuy: series('ur_en_ts_buy_rate', 'ur_ts_buy_rate'),
|
|
156
|
+
timeSeriesSell: series('ur_en_ts_sell_rate', 'ur_ts_sell_rate'),
|
|
157
|
+
other,
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Writes a rate as the flat `ur_` inputs of a SAM case, the keys it was
|
|
162
|
+
* read from in the same values; `JSON.stringify` gives the file.
|
|
163
|
+
*/
|
|
164
|
+
export function writeUtilityRate(rate) {
|
|
165
|
+
const out = { ...rate.other };
|
|
166
|
+
if (rate.energyCharges.length > 0) {
|
|
167
|
+
out.ur_ec_tou_mat = rate.energyCharges.map((c) => [c.period, c.tier, c.maxUsage, UNITS.indexOf(c.maxUsageUnit), c.buy, c.sell]);
|
|
168
|
+
out.ur_ec_sched_weekday = rate.weekday.map((row) => [...row]);
|
|
169
|
+
out.ur_ec_sched_weekend = rate.weekend.map((row) => [...row]);
|
|
170
|
+
}
|
|
171
|
+
out.ur_sell_eq_buy = rate.sellEqualsBuy ? 1 : 0;
|
|
172
|
+
if (rate.timeSeriesBuy) {
|
|
173
|
+
out.ur_en_ts_buy_rate = 1;
|
|
174
|
+
out.ur_ts_buy_rate = [...rate.timeSeriesBuy];
|
|
175
|
+
}
|
|
176
|
+
if (rate.timeSeriesSell) {
|
|
177
|
+
out.ur_en_ts_sell_rate = 1;
|
|
178
|
+
out.ur_ts_sell_rate = [...rate.timeSeriesSell];
|
|
179
|
+
}
|
|
180
|
+
return out;
|
|
181
|
+
}
|
|
182
|
+
/** Month (0–11), hour of the day and weekend, for each hour of SAM's year. */
|
|
183
|
+
function* yearHours() {
|
|
184
|
+
let day = 0;
|
|
185
|
+
for (const [month, days] of DAYS_IN_MONTH.entries()) {
|
|
186
|
+
for (let d = 0; d < days; d++, day++) {
|
|
187
|
+
// The first day of SAM's year is a Monday: days 5 and 6 of each week are the weekend.
|
|
188
|
+
const weekend = day % 7 >= 5;
|
|
189
|
+
for (let hour = 0; hour < 24; hour++)
|
|
190
|
+
yield { month, hour, weekend };
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* The buy and sell rate of each hour of SAM's year, per kWh in the file's
|
|
196
|
+
* currency: the time-series rates where the rate has them, the period of
|
|
197
|
+
* the hour's schedule otherwise. A period of several tiers has no rate of
|
|
198
|
+
* its own at an hour, its tiers depend on the usage: refused
|
|
199
|
+
* `utilityrate5_tiered`.
|
|
200
|
+
*/
|
|
201
|
+
export function hourlyRates(rate) {
|
|
202
|
+
const lengths = [rate.timeSeriesBuy, rate.timeSeriesSell].filter((s) => s !== null).map((s) => s.length);
|
|
203
|
+
if (new Set(lengths).size > 1)
|
|
204
|
+
throw new Error('utilityrate5_series_length: the time-series buy and sell rates have different steps');
|
|
205
|
+
const steps = lengths[0] ?? HOURS_IN_YEAR;
|
|
206
|
+
const perHour = steps / HOURS_IN_YEAR;
|
|
207
|
+
const fromSchedule = (side) => {
|
|
208
|
+
const byPeriod = new Map();
|
|
209
|
+
for (const c of rate.energyCharges) {
|
|
210
|
+
if (rate.energyCharges.some((o) => o.period === c.period && o.tier !== c.tier)) {
|
|
211
|
+
throw new Error(`utilityrate5_tiered: period ${c.period} charges by tier of usage, not by hour`);
|
|
212
|
+
}
|
|
213
|
+
byPeriod.set(c.period, c[side]);
|
|
214
|
+
}
|
|
215
|
+
if (byPeriod.size === 0)
|
|
216
|
+
throw new Error(`utilityrate5_unreadable: no energy charge and no time-series ${side} rate`);
|
|
217
|
+
const out = [];
|
|
218
|
+
for (const { month, hour, weekend } of yearHours()) {
|
|
219
|
+
const period = (weekend ? rate.weekend : rate.weekday)[month][hour];
|
|
220
|
+
for (let k = 0; k < perHour; k++)
|
|
221
|
+
out.push(byPeriod.get(period));
|
|
222
|
+
}
|
|
223
|
+
return out;
|
|
224
|
+
};
|
|
225
|
+
const resample = (series) => {
|
|
226
|
+
if (series.length >= steps)
|
|
227
|
+
return [...series];
|
|
228
|
+
// A series coarser than the other side's is repeated over its finer steps.
|
|
229
|
+
const n = steps / series.length;
|
|
230
|
+
return series.flatMap((v) => Array.from({ length: n }, () => v));
|
|
231
|
+
};
|
|
232
|
+
const buy = rate.timeSeriesBuy ? resample(rate.timeSeriesBuy) : fromSchedule('buy');
|
|
233
|
+
const sell = rate.sellEqualsBuy ? [...buy] : rate.timeSeriesSell ? resample(rate.timeSeriesSell) : fromSchedule('sell');
|
|
234
|
+
return { stepMinutes: (60 * HOURS_IN_YEAR) / steps, buy, sell };
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* The rate an hourly year of buy and sell rates is: the 12 × 24 weekday and
|
|
238
|
+
* weekend schedules when every hour of a month, a time of day and a kind of
|
|
239
|
+
* day has one buy and sell rate, a period per pair in the order they first
|
|
240
|
+
* come; the same year as time-series rates otherwise. `sell` omitted is
|
|
241
|
+
* energy sold at the rate it is bought. Refuses `utilityrate5_series_length`.
|
|
242
|
+
*/
|
|
243
|
+
export function rateFromHourly(buy, sell) {
|
|
244
|
+
if (buy.length !== HOURS_IN_YEAR || (sell && sell.length !== HOURS_IN_YEAR)) {
|
|
245
|
+
throw new Error(`utilityrate5_series_length: an hourly year of SAM holds ${HOURS_IN_YEAR} rates`);
|
|
246
|
+
}
|
|
247
|
+
const sold = sell ?? buy;
|
|
248
|
+
const blank = () => Array.from({ length: 12 }, () => Array.from({ length: 24 }, () => 0));
|
|
249
|
+
const weekday = blank();
|
|
250
|
+
const weekend = blank();
|
|
251
|
+
const periods = new Map();
|
|
252
|
+
const energyCharges = [];
|
|
253
|
+
let i = 0;
|
|
254
|
+
for (const { month, hour, weekend: isWeekend } of yearHours()) {
|
|
255
|
+
const key = JSON.stringify([buy[i], sold[i]]);
|
|
256
|
+
let period = periods.get(key);
|
|
257
|
+
if (period === undefined) {
|
|
258
|
+
period = periods.size + 1;
|
|
259
|
+
periods.set(key, period);
|
|
260
|
+
energyCharges.push({ period, tier: 1, maxUsage: 1e38, maxUsageUnit: 'kWh', buy: buy[i], sell: sold[i] });
|
|
261
|
+
}
|
|
262
|
+
const schedule = isWeekend ? weekend : weekday;
|
|
263
|
+
const held = schedule[month][hour];
|
|
264
|
+
if (held !== 0 && held !== period) {
|
|
265
|
+
return { energyCharges: [], weekday: [], weekend: [], sellEqualsBuy: !sell, timeSeriesBuy: [...buy], timeSeriesSell: sell ? [...sell] : null, other: {} };
|
|
266
|
+
}
|
|
267
|
+
schedule[month][hour] = period;
|
|
268
|
+
i++;
|
|
269
|
+
}
|
|
270
|
+
return { energyCharges, weekday, weekend, sellEqualsBuy: !sell, timeSeriesBuy: null, timeSeriesSell: null, other: {} };
|
|
271
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,34 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nexgen-os/utilityrate5",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.5.0",
|
|
4
|
+
"description": "SAM utilityrate5 rates read and written: the energy charges, the 12 x 24 weekday and weekend schedules, the time-series rates, and the hourly rates of a year they give. No database, no dependency.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/nexgen-os/nexgen-os.git",
|
|
9
|
+
"directory": "packages/formats/utilityrate5"
|
|
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
|
+
}
|