mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-10-26 19:01:39 -04:00
* Add a Solar calculator with offline NASA POWER data Estimates a solar array's typical-year production from a location and the panels; everything else has defaults under an optional "Fine-tune" section. The model works from 12 monthly averages: Klein's average day per month, the Erbs monthly diffuse fraction, Collares-Pereira & Rabl / Liu & Jordan hourly split, HDKR transposition for fixed, single-axis and dual-axis arrays, then PVWatts' ASHRAE incidence loss, SAPM cell temperature, DC power, system losses and optional inverter. "Best for the year" and "Best for winter" sweep the tilt. Sunlight, diffuse, albedo and temperature come from NASA POWER's 2001-2020 climatology on a 1 degree grid, packed into app/public/data (3.1 MB, ~1 MB gzipped) by scripts/build_solar_climatology.py and fetched only when the calculator opens. Users can replace it with their own monthly values. Every setting lives in the URL, with defaults left out, so the Share button shares the whole configuration. Against PVWatts v8 for a 1 kW fixed array: Phoenix +0.6%, Seattle +8.8%, Anchorage +8.0% annually; these are recorded as tests. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Open the Solar calculator from the map, pins and current location The map's right-click (or long-press) menu gains "Calculate Solar Performance", and each saved pin a sun button; both open the calculator at that location. A button beside Latitude fills in the browser's current location. Locations are rounded to 4 decimals, and longitudes from a map panned around the world are wrapped back to -180..180. The map removed lat/lon/z from the URL when it closed, even after navigating to another page, which stripped the calculator's location from its URL (and from links shared with the Share button). It now only cleans up while the URL is still a map page. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Add off-grid sizing to the Solar calculator A new "Off-grid sizing" tab sizes a battery system for the darkest month from a list of devices (watts x hours per day): - Battery bank for a number of days without sun, for LiFePO4 or lead-acid, at 12, 24 or 48 V. Outdoor lead-acid is enlarged for the cold; outdoor lithium warns against charging below 0 C. - Panels needed, from the worst month's DC production per kW at the chosen angle, with a button to apply the count. A PWM controller's loss (battery voltage over the panel's Vmp) raises the count. - Charge controller current, and the string voltage on the coldest morning against the controller's limit, since cold panels exceed their rated Voc. The coldest morning defaults to 25 C below the coldest monthly average. All inputs live in the URL like the rest of the calculator. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Bump wrolpi-help for the Solar calculator help page Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Credit NASA POWER as it asks, and say how the data was changed - The calculator gains a "Data attribution" link under the location with NASA POWER's requested acknowledgment, the data reference (Climatology API v2.10.0, downloaded 2026/10/04), the CC BY 4.0 license, and how WROLPi modified the data (rounded to one byte per value, temperature averaged into 1 degree cells, interpolated between cells), which CC BY 4.0 requires. - The build script writes the same text to solar-climatology.LICENSE.txt beside the data, with the API version taken from NASA's responses and the date the tiles were downloaded. A test keeps the page and file in step. - Bumps wrolpi-help for the updated attribution in the help page. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Solar: fix PWM sizing, Voc sign, unit switching and loading on review - PWM usable power now uses the string's voltage (Vmp x panels in series), delivers nothing when the charging voltage is at or above the string's Voc, and falls off between Vmp and Voc. The "too little voltage" warning covers PWM as well as MPPT. Previously two panels in series counted double, and a panel that could not charge a 48 V bank counted as fully usable. - A Voc temperature coefficient entered without its minus sign is treated as negative, in the math, the URL and the input, so the cold-morning check cannot be made too generous. - Switching Fahrenheit on or off converts the entered temperatures instead of reinterpreting them. - While the sunlight data downloads, the off-grid tab shows a loader rather than asking for a location, and holds the cold-morning verdict until the site's temperatures are known or one has been entered. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Solar: move the data attribution to the bottom of the page The full NASA POWER acknowledgment, license and changes now sit in a small "Data attribution" section at the end of the calculator, always visible, instead of behind a link under the location. The source line under the location points to it. Bumps wrolpi-help to match. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Solar: drop the data source line under the location The attribution at the bottom of the page is enough. The line under the location now only appears to report the data's state: loading, failed to load, or replaced by the user's own values. Bumps wrolpi-help for the refreshed screenshot. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
256 lines
13 KiB
JavaScript
256 lines
13 KiB
JavaScript
import React from "react";
|
|
import {fireEvent, screen, within} from "@testing-library/react";
|
|
import {renderWithProviders} from "../../test-utils";
|
|
import {missingInputs, SolarCalculator, sunlightFor, systemFromParams} from "./SolarCalculator";
|
|
import {CalculatorsPage} from "../Calculators";
|
|
import {DEFAULT_LOSSES, estimateProduction, MODULE_TYPES} from "./solar";
|
|
import {decodeClimatology, forgetClimatology, lookupClimatology} from "./solarData";
|
|
import {mockClimatologyFetch, SUNNY_GHI, uniformClimatology} from "./solarData.fixtures";
|
|
import {roundDigits} from "../Common";
|
|
|
|
const BASE = '/more/calculators?calc=solar';
|
|
const DENVER = `${BASE}&lat=40&lon=-105`;
|
|
|
|
const params = query => {
|
|
const searchParams = new URLSearchParams(query);
|
|
return key => searchParams.get(key);
|
|
};
|
|
|
|
const currentParams = () => new URLSearchParams(window.location.search);
|
|
|
|
// The estimate appears once the data file has loaded and decoded, which can be slow on a busy machine.
|
|
const findEstimate = (unit = 'DC') => screen.findByText(`kWh ${unit} per year`, {}, {timeout: 5000});
|
|
|
|
// The annual estimate the page should show for a location, from the same uniform test data.
|
|
const expectedAnnual = (overrides = {}) => {
|
|
const grid = decodeClimatology(uniformClimatology());
|
|
const sunlight = lookupClimatology(grid, 40, -105);
|
|
return estimateProduction({latitude: 40, kwp: 0.4, tilt: 40, azimuth: 180, ...sunlight, ...overrides}).annual;
|
|
};
|
|
|
|
beforeEach(() => {
|
|
forgetClimatology();
|
|
mockClimatologyFetch();
|
|
});
|
|
|
|
describe('systemFromParams', () => {
|
|
test('defaults when the URL is empty', () => {
|
|
const s = systemFromParams(params(''));
|
|
expect(s.latitude).toBeNull();
|
|
expect(s.kwp).toBeCloseTo(0.4, 6);
|
|
expect(s.tracking).toBe('fixed');
|
|
expect(s.mounting).toBe('open');
|
|
expect(s.gamma).toBe(MODULE_TYPES.standard.gamma);
|
|
expect(s.losses).toEqual(DEFAULT_LOSSES);
|
|
expect(s.inverterOn).toBe(false);
|
|
expect(s.ownData).toBe(false);
|
|
expect(s.albedo).toBeNull();
|
|
});
|
|
|
|
test('tilt and azimuth default from the latitude', () => {
|
|
expect(systemFromParams(params('lat=40.4')).tilt).toBe(40);
|
|
expect(systemFromParams(params('lat=40.4')).azimuth).toBe(180);
|
|
expect(systemFromParams(params('lat=-33.9')).tilt).toBe(34);
|
|
expect(systemFromParams(params('lat=-33.9')).azimuth).toBe(0);
|
|
});
|
|
|
|
test('invalid values fall back instead of crashing', () => {
|
|
const s = systemFromParams(params('lat=999&track=bogus&mount=x&tilt=abc&losses=1,2,abc&alb=7&pn=-3'));
|
|
expect(s.latitude).toBeNull();
|
|
expect(s.tracking).toBe('fixed');
|
|
expect(s.mounting).toBe('open');
|
|
expect(s.tilt).toBe(0);
|
|
expect(s.losses.slice(0, 3)).toEqual([1, 2, DEFAULT_LOSSES[2]]);
|
|
expect(s.albedo).toBe(1);
|
|
expect(s.kwp).toBe(0);
|
|
});
|
|
|
|
test('own Fahrenheit temperatures are converted to Celsius', () => {
|
|
const s = systemFromParams(params('ghi=5&tu=f&temp=32,212'));
|
|
expect(s.ownData).toBe(true);
|
|
expect(s.temperature[0]).toBeCloseTo(0, 6);
|
|
expect(s.temperature[1]).toBeCloseTo(100, 6);
|
|
expect(s.temperatureInput[0]).toBe(32);
|
|
});
|
|
});
|
|
|
|
describe('sunlight and missing inputs', () => {
|
|
const grid = decodeClimatology(uniformClimatology());
|
|
|
|
test('the NASA data is used unless the user gave their own', () => {
|
|
const nasa = sunlightFor(systemFromParams(params('lat=40&lon=-105')), grid);
|
|
expect(nasa.ghi[5]).toBeCloseTo(SUNNY_GHI[5], 1);
|
|
expect(nasa.diffuse[5]).toBeCloseTo(SUNNY_GHI[5] * 0.3, 1);
|
|
|
|
const own = sunlightFor(systemFromParams(params(`lat=40&lon=-105&ghi=${Array(12).fill(1).join(',')}`)), grid);
|
|
expect(own.ghi).toEqual(Array(12).fill(1));
|
|
expect(own.diffuse).toBeNull();
|
|
});
|
|
|
|
test('a location is enough with the NASA data', () => {
|
|
const s = systemFromParams(params('lat=40&lon=-105'));
|
|
expect(missingInputs(s, sunlightFor(s, grid))).toEqual([]);
|
|
const noLon = systemFromParams(params('lat=40'));
|
|
expect(missingInputs(noLon, sunlightFor(noLon, grid))).toEqual(['a longitude']);
|
|
});
|
|
|
|
test('own data needs all 12 months but no longitude', () => {
|
|
const s = systemFromParams(params('lat=40&ghi=1,2,3'));
|
|
expect(missingInputs(s, sunlightFor(s, grid))).toEqual(['sunlight for all 12 months']);
|
|
});
|
|
});
|
|
|
|
describe('SolarCalculator', () => {
|
|
test('is linked from the Engineering group of the calculators page', () => {
|
|
renderWithProviders(<CalculatorsPage/>, {route: '/more/calculators'});
|
|
const engineering = screen.getByRole('heading', {name: 'Engineering'}).closest('.wrolpi-header').parentElement;
|
|
const link = within(engineering).getByRole('link', {name: /Solar/});
|
|
expect(link).toHaveAttribute('href', '/more/calculators?calc=solar');
|
|
});
|
|
|
|
test('asks for a location', async () => {
|
|
renderWithProviders(<SolarCalculator/>, {route: BASE});
|
|
expect(await screen.findByText('Enter a latitude, a longitude to see an estimate.', {}, {timeout: 5000}))
|
|
.toBeInTheDocument();
|
|
});
|
|
|
|
test('a location alone gives an estimate from the built-in data', async () => {
|
|
renderWithProviders(<SolarCalculator/>, {route: DENVER});
|
|
expect(await findEstimate()).toBeInTheDocument();
|
|
expect(screen.getByText(`${roundDigits(expectedAnnual(), 0)}`)).toBeInTheDocument();
|
|
// The attribution is only at the bottom of the page.
|
|
expect(screen.queryByText(/Sunlight and temperature/)).not.toBeInTheDocument();
|
|
expect(global.fetch).toHaveBeenCalledWith(expect.stringMatching(/\/data\/solar-climatology\.bin$/));
|
|
});
|
|
|
|
test('the data attribution is at the bottom of the page', async () => {
|
|
renderWithProviders(<SolarCalculator/>, {route: DENVER});
|
|
await findEstimate();
|
|
const heading = screen.getByRole('heading', {name: 'Data attribution'});
|
|
const section = screen.getByRole('region', {name: 'Data attribution'});
|
|
expect(within(section).getByText(/Prediction Of Worldwide Energy Resources/)).toBeInTheDocument();
|
|
expect(within(section).getByText(/WROLPi modified the data/)).toBeInTheDocument();
|
|
expect(within(section).getByText(/CC BY 4\.0/)).toBeInTheDocument();
|
|
// After everything else, including the last Fine-tune section.
|
|
const lastControl = screen.getByText('Use my own sunlight data: off');
|
|
expect(lastControl.compareDocumentPosition(heading) & Node.DOCUMENT_POSITION_FOLLOWING).toBeTruthy();
|
|
});
|
|
|
|
test('typing writes the URL and leaves defaults out', async () => {
|
|
renderWithProviders(<SolarCalculator/>, {route: BASE});
|
|
fireEvent.change(screen.getByLabelText('Latitude'), {target: {value: '40°0\'54"N'}});
|
|
expect(currentParams().get('lat')).toBe('40°0\'54"N');
|
|
expect(currentParams().get('calc')).toBe('solar');
|
|
|
|
fireEvent.change(screen.getByLabelText('Panels'), {target: {value: '4'}});
|
|
expect(currentParams().get('pn')).toBe('4');
|
|
fireEvent.change(screen.getByLabelText('Panels'), {target: {value: '1'}});
|
|
expect(currentParams().has('pn')).toBe(false);
|
|
});
|
|
|
|
describe('use my current location', () => {
|
|
const withGeolocation = getCurrentPosition => {
|
|
Object.defineProperty(window, 'isSecureContext', {value: true, configurable: true});
|
|
Object.defineProperty(navigator, 'geolocation', {value: {getCurrentPosition}, configurable: true});
|
|
};
|
|
|
|
afterEach(() => {
|
|
delete window.isSecureContext;
|
|
delete navigator.geolocation;
|
|
});
|
|
|
|
test('fills in the location, rounded to 4 decimals', async () => {
|
|
withGeolocation(success => success({coords: {latitude: 39.7392358, longitude: -104.990251}}));
|
|
renderWithProviders(<SolarCalculator/>, {route: BASE});
|
|
fireEvent.click(screen.getByRole('button', {name: 'Use my current location'}));
|
|
expect(currentParams().get('lat')).toBe('39.7392');
|
|
expect(currentParams().get('lon')).toBe('-104.9903');
|
|
expect(screen.getByLabelText('Latitude')).toHaveValue('39.7392');
|
|
expect(await findEstimate()).toBeInTheDocument();
|
|
});
|
|
|
|
test('explains a denied permission', () => {
|
|
withGeolocation((success, failure) => failure({code: 1, message: 'User denied Geolocation'}));
|
|
renderWithProviders(<SolarCalculator/>, {route: BASE});
|
|
fireEvent.click(screen.getByRole('button', {name: 'Use my current location'}));
|
|
expect(screen.getByText('Could not get your location')).toBeInTheDocument();
|
|
expect(screen.getByText(/Location permission was denied/)).toBeInTheDocument();
|
|
expect(currentParams().has('lat')).toBe(false);
|
|
});
|
|
|
|
test('is disabled without HTTPS', () => {
|
|
Object.defineProperty(window, 'isSecureContext', {value: false, configurable: true});
|
|
renderWithProviders(<SolarCalculator/>, {route: BASE});
|
|
expect(screen.getByRole('button', {name: 'Use my current location'})).toBeDisabled();
|
|
});
|
|
});
|
|
|
|
test('an invalid latitude is flagged', () => {
|
|
renderWithProviders(<SolarCalculator/>, {route: `${BASE}&lat=95`});
|
|
expect(screen.getByText('Latitude must be between -90 and 90')).toBeInTheDocument();
|
|
});
|
|
|
|
test('the inverter switches the results to AC', async () => {
|
|
renderWithProviders(<SolarCalculator/>, {route: DENVER});
|
|
await findEstimate();
|
|
fireEvent.click(screen.getByText('Inverter: off (DC output)'));
|
|
fireEvent.click(screen.getByLabelText('Include an inverter (AC output)'));
|
|
expect(currentParams().get('inv')).toBe('1');
|
|
expect(screen.getByText('kWh AC per year')).toBeInTheDocument();
|
|
});
|
|
|
|
test('best-for-the-year replaces a poor tilt', async () => {
|
|
renderWithProviders(<SolarCalculator/>, {route: `${DENVER}&tilt=10`});
|
|
await findEstimate();
|
|
expect(screen.getByText('Panel angle: 10° tilt, facing S')).toBeInTheDocument();
|
|
fireEvent.click(screen.getByText('Panel angle: 10° tilt, facing S'));
|
|
fireEvent.click(await screen.findByRole('button', {name: 'Best for the year'}));
|
|
// A result of 40 equals the default (the latitude), so it is left out of the URL.
|
|
const tilt = Number(currentParams().get('tilt') ?? 40);
|
|
expect(tilt).toBeGreaterThanOrEqual(30);
|
|
expect(tilt).toBeLessThanOrEqual(44);
|
|
});
|
|
|
|
test('own sunlight data starts from the built-in values and replaces them', async () => {
|
|
renderWithProviders(<SolarCalculator/>, {route: DENVER});
|
|
await findEstimate();
|
|
fireEvent.click(screen.getByText('Use my own sunlight data: off'));
|
|
fireEvent.click(screen.getByLabelText('Use my own sunlight data'));
|
|
|
|
const ghi = currentParams().get('ghi').split(',').map(Number);
|
|
ghi.forEach((v, i) => expect(v).toBeCloseTo(SUNNY_GHI[i], 1));
|
|
expect(screen.getByText('Using your own sunlight data.')).toBeInTheDocument();
|
|
expect(screen.getByLabelText('Jun sunlight')).toHaveValue(`${ghi[5]}`);
|
|
|
|
// Halving June's sunlight lowers the estimate.
|
|
fireEvent.change(screen.getByLabelText('Jun sunlight'), {target: {value: `${ghi[5] / 2}`}});
|
|
expect(currentParams().get('ghi').split(',')[5]).toBe(`${ghi[5] / 2}`);
|
|
|
|
fireEvent.click(screen.getByLabelText('Use my own sunlight data'));
|
|
expect(currentParams().has('ghi')).toBe(false);
|
|
expect(currentParams().has('temp')).toBe(false);
|
|
});
|
|
|
|
test('switching to Fahrenheit converts your temperatures instead of relabeling them', async () => {
|
|
const temps = '0,10,,,,,,,,,,';
|
|
renderWithProviders(<SolarCalculator/>,
|
|
{route: `${DENVER}&ghi=${SUNNY_GHI.join(',')}&temp=${temps}`});
|
|
await findEstimate();
|
|
fireEvent.click(screen.getByText('Use my own sunlight data: on'));
|
|
fireEvent.click(screen.getByLabelText('Fahrenheit'));
|
|
expect(currentParams().get('tu')).toBe('f');
|
|
expect(currentParams().get('temp')).toBe('32,50,,,,,,,,,,');
|
|
expect(screen.getByLabelText('Jan temperature')).toHaveValue('32');
|
|
fireEvent.click(screen.getByLabelText('Fahrenheit'));
|
|
expect(currentParams().has('tu')).toBe(false);
|
|
expect(currentParams().get('temp')).toBe(temps);
|
|
});
|
|
|
|
test('a failed download explains how to continue', async () => {
|
|
forgetClimatology();
|
|
global.fetch = jest.fn(() => Promise.resolve({ok: false, status: 404}));
|
|
renderWithProviders(<SolarCalculator/>, {route: DENVER});
|
|
expect(await screen.findByText('Sunlight data could not be loaded', {}, {timeout: 5000})).toBeInTheDocument();
|
|
expect(screen.getByText('Enter a location with sunlight data to see an estimate.')).toBeInTheDocument();
|
|
});
|
|
});
|