mirror of
https://github.com/ANL-CEEESA/UnitCommitment.jl.git
synced 2025-12-06 00:08:52 -06:00
web: profiled units: Allow CSV upload
This commit is contained in:
@@ -16,58 +16,46 @@ import FileUploadElement from "../../Common/Buttons/FileUploadElement";
|
|||||||
import { useRef } from "react";
|
import { useRef } from "react";
|
||||||
import { ValidationError } from "../../../core/Validation/validate";
|
import { ValidationError } from "../../../core/Validation/validate";
|
||||||
import DataTable, {
|
import DataTable, {
|
||||||
addNameColumn,
|
|
||||||
addTimeseriesColumn,
|
|
||||||
ColumnSpec,
|
ColumnSpec,
|
||||||
generateCsv,
|
generateCsv,
|
||||||
generateTableColumns,
|
generateTableColumns,
|
||||||
generateTableData,
|
generateTableData,
|
||||||
|
parseCsv,
|
||||||
} from "../../Common/Forms/DataTable";
|
} from "../../Common/Forms/DataTable";
|
||||||
|
|
||||||
import { UnitCommitmentScenario } from "../../../core/fixtures";
|
import { UnitCommitmentScenario } from "../../../core/fixtures";
|
||||||
import { ColumnDefinition } from "tabulator-tables";
|
import { ColumnDefinition } from "tabulator-tables";
|
||||||
import { parseBusesCsv } from "./BusesCsv";
|
import {
|
||||||
|
changeBusData,
|
||||||
|
createBus,
|
||||||
|
deleteBus,
|
||||||
|
renameBus,
|
||||||
|
} from "../../../core/Operations/busOperations";
|
||||||
|
|
||||||
|
export const BusesColumnSpec: ColumnSpec[] = [
|
||||||
|
{
|
||||||
|
title: "Name",
|
||||||
|
type: "string",
|
||||||
|
width: 150,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
title: "Load (MW)",
|
||||||
|
type: "number[]",
|
||||||
|
width: 60,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
export const generateBusesData = (
|
export const generateBusesData = (
|
||||||
scenario: UnitCommitmentScenario,
|
scenario: UnitCommitmentScenario,
|
||||||
): [any[], ColumnDefinition[]] => {
|
): [any[], ColumnDefinition[]] => {
|
||||||
const colSpecs: ColumnSpec[] = [
|
const columns = generateTableColumns(scenario, BusesColumnSpec);
|
||||||
{
|
const data = generateTableData(scenario.Buses, BusesColumnSpec, scenario);
|
||||||
title: "Name",
|
|
||||||
type: "string",
|
|
||||||
width: 150,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
title: "Load (MW)",
|
|
||||||
type: "number[]",
|
|
||||||
width: 60,
|
|
||||||
},
|
|
||||||
];
|
|
||||||
const columns = generateTableColumns(scenario, colSpecs);
|
|
||||||
const data = generateTableData(scenario.Buses, colSpecs, scenario);
|
|
||||||
return [data, columns];
|
return [data, columns];
|
||||||
};
|
};
|
||||||
|
|
||||||
export const generateBusesColumns = (
|
|
||||||
scenario: UnitCommitmentScenario,
|
|
||||||
): ColumnDefinition[] => {
|
|
||||||
const columns: ColumnDefinition[] = [];
|
|
||||||
addNameColumn(columns);
|
|
||||||
addTimeseriesColumn(scenario, "Load (MW)", columns);
|
|
||||||
return columns;
|
|
||||||
};
|
|
||||||
|
|
||||||
interface BusesProps {
|
interface BusesProps {
|
||||||
scenario: UnitCommitmentScenario;
|
scenario: UnitCommitmentScenario;
|
||||||
onBusCreated: () => void;
|
|
||||||
onBusDataChanged: (
|
|
||||||
bus: string,
|
|
||||||
field: string,
|
|
||||||
newValue: string,
|
|
||||||
) => ValidationError | null;
|
|
||||||
onBusDeleted: (bus: string) => ValidationError | null;
|
|
||||||
onBusRenamed: (oldName: string, newName: string) => ValidationError | null;
|
|
||||||
onDataChanged: (scenario: UnitCommitmentScenario) => void;
|
onDataChanged: (scenario: UnitCommitmentScenario) => void;
|
||||||
|
onError: (msg: string) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
function BusesComponent(props: BusesProps) {
|
function BusesComponent(props: BusesProps) {
|
||||||
@@ -81,19 +69,70 @@ function BusesComponent(props: BusesProps) {
|
|||||||
|
|
||||||
const onUpload = () => {
|
const onUpload = () => {
|
||||||
fileUploadElem.current!.showFilePicker((csvContents: any) => {
|
fileUploadElem.current!.showFilePicker((csvContents: any) => {
|
||||||
const newScenario = parseBusesCsv(props.scenario, csvContents);
|
const [newBuses, err] = parseCsv(
|
||||||
|
csvContents,
|
||||||
|
BusesColumnSpec,
|
||||||
|
props.scenario,
|
||||||
|
);
|
||||||
|
if (err) {
|
||||||
|
props.onError(err.message);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const newScenario = {
|
||||||
|
...props.scenario,
|
||||||
|
Buses: newBuses,
|
||||||
|
};
|
||||||
props.onDataChanged(newScenario);
|
props.onDataChanged(newScenario);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const onAdd = () => {
|
||||||
|
const newScenario = createBus(props.scenario);
|
||||||
|
props.onDataChanged(newScenario);
|
||||||
|
};
|
||||||
|
|
||||||
|
const onDataChanged = (
|
||||||
|
bus: string,
|
||||||
|
field: string,
|
||||||
|
newValue: string,
|
||||||
|
): ValidationError | null => {
|
||||||
|
const [newScenario, err] = changeBusData(
|
||||||
|
bus,
|
||||||
|
field,
|
||||||
|
newValue,
|
||||||
|
props.scenario,
|
||||||
|
);
|
||||||
|
if (err) {
|
||||||
|
props.onError(err.message);
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
props.onDataChanged(newScenario);
|
||||||
|
return null;
|
||||||
|
};
|
||||||
|
|
||||||
|
const onDelete = (bus: string): ValidationError | null => {
|
||||||
|
const newScenario = deleteBus(bus, props.scenario);
|
||||||
|
props.onDataChanged(newScenario);
|
||||||
|
return null;
|
||||||
|
};
|
||||||
|
|
||||||
|
const onRename = (
|
||||||
|
oldName: string,
|
||||||
|
newName: string,
|
||||||
|
): ValidationError | null => {
|
||||||
|
const [newScenario, err] = renameBus(oldName, newName, props.scenario);
|
||||||
|
if (err) {
|
||||||
|
props.onError(err.message);
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
props.onDataChanged(newScenario);
|
||||||
|
return null;
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<SectionHeader title="Buses">
|
<SectionHeader title="Buses">
|
||||||
<SectionButton
|
<SectionButton icon={faPlus} tooltip="Add" onClick={onAdd} />
|
||||||
icon={faPlus}
|
|
||||||
tooltip="Add"
|
|
||||||
onClick={props.onBusCreated}
|
|
||||||
/>
|
|
||||||
<SectionButton
|
<SectionButton
|
||||||
icon={faDownload}
|
icon={faDownload}
|
||||||
tooltip="Download"
|
tooltip="Download"
|
||||||
@@ -102,9 +141,9 @@ function BusesComponent(props: BusesProps) {
|
|||||||
<SectionButton icon={faUpload} tooltip="Upload" onClick={onUpload} />
|
<SectionButton icon={faUpload} tooltip="Upload" onClick={onUpload} />
|
||||||
</SectionHeader>
|
</SectionHeader>
|
||||||
<DataTable
|
<DataTable
|
||||||
onRowDeleted={props.onBusDeleted}
|
onRowDeleted={onDelete}
|
||||||
onRowRenamed={props.onBusRenamed}
|
onRowRenamed={onRename}
|
||||||
onDataChanged={props.onBusDataChanged}
|
onDataChanged={onDataChanged}
|
||||||
generateData={() => generateBusesData(props.scenario)}
|
generateData={() => generateBusesData(props.scenario)}
|
||||||
/>
|
/>
|
||||||
<FileUploadElement ref={fileUploadElem} accept=".csv" />
|
<FileUploadElement ref={fileUploadElem} accept=".csv" />
|
||||||
|
|||||||
@@ -1,64 +0,0 @@
|
|||||||
/*
|
|
||||||
* UnitCommitment.jl: Optimization Package for Security-Constrained Unit Commitment
|
|
||||||
* Copyright (C) 2020-2025, UChicago Argonne, LLC. All rights reserved.
|
|
||||||
* Released under the modified BSD license. See COPYING.md for more details.
|
|
||||||
*/
|
|
||||||
|
|
||||||
import { Buses, UnitCommitmentScenario } from "../../../core/fixtures";
|
|
||||||
import Papa from "papaparse";
|
|
||||||
import { generateBusesColumns } from "./Buses";
|
|
||||||
|
|
||||||
export const parseBusesCsv = (
|
|
||||||
scenario: UnitCommitmentScenario,
|
|
||||||
csvData: string,
|
|
||||||
): UnitCommitmentScenario => {
|
|
||||||
const results = Papa.parse(csvData, {
|
|
||||||
header: true,
|
|
||||||
skipEmptyLines: true,
|
|
||||||
transformHeader: (header) => header.trim(),
|
|
||||||
transform: (value) => value.trim(),
|
|
||||||
});
|
|
||||||
|
|
||||||
// Check for parsing errors
|
|
||||||
if (results.errors.length > 0) {
|
|
||||||
throw Error(`Invalid CSV: Parsing error: ${results.errors}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check CSV headers
|
|
||||||
const expectedFields = generateBusesColumns(scenario).map(
|
|
||||||
(col) => col.field,
|
|
||||||
)!;
|
|
||||||
const actualFields = results.meta.fields!;
|
|
||||||
for (let i = 0; i < expectedFields.length; i++) {
|
|
||||||
if (expectedFields[i] !== actualFields[i]) {
|
|
||||||
throw Error(`Invalid CSV: Header mismatch at column ${i + 1}"`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Parse each row
|
|
||||||
const T = getNumTimesteps(scenario);
|
|
||||||
const buses: Buses = {};
|
|
||||||
for (let i = 0; i < results.data.length; i++) {
|
|
||||||
const row = results.data[i] as { [key: string]: any };
|
|
||||||
const busName = row["Name"] as string;
|
|
||||||
const busLoad: number[] = Array(T);
|
|
||||||
for (let j = 0; j < T; j++) {
|
|
||||||
busLoad[j] = parseFloat(row[`Load ${j}`]);
|
|
||||||
}
|
|
||||||
buses[busName] = {
|
|
||||||
"Load (MW)": busLoad,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
return {
|
|
||||||
...scenario,
|
|
||||||
Buses: buses,
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
function getNumTimesteps(scenario: UnitCommitmentScenario) {
|
|
||||||
return (
|
|
||||||
(scenario.Parameters["Time horizon (h)"] *
|
|
||||||
scenario.Parameters["Time step (min)"]) /
|
|
||||||
60
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@@ -17,19 +17,8 @@ import "tabulator-tables/dist/css/tabulator.min.css";
|
|||||||
import "../Common/Forms/Tables.css";
|
import "../Common/Forms/Tables.css";
|
||||||
import { useState } from "react";
|
import { useState } from "react";
|
||||||
import Footer from "./Footer";
|
import Footer from "./Footer";
|
||||||
import { validate, ValidationError } from "../../core/Validation/validate";
|
import { validate } from "../../core/Validation/validate";
|
||||||
import { offerDownload } from "../Common/io";
|
import { offerDownload } from "../Common/io";
|
||||||
import {
|
|
||||||
changeBusData,
|
|
||||||
createBus,
|
|
||||||
deleteBus,
|
|
||||||
renameBus,
|
|
||||||
} from "../../core/Operations/busOperations";
|
|
||||||
import {
|
|
||||||
changeParameter,
|
|
||||||
changeTimeHorizon,
|
|
||||||
changeTimeStep,
|
|
||||||
} from "../../core/Operations/parameterOperations";
|
|
||||||
import { preprocess } from "../../core/Operations/preprocessing";
|
import { preprocess } from "../../core/Operations/preprocessing";
|
||||||
import Toast from "../Common/Forms/Toast";
|
import Toast from "../Common/Forms/Toast";
|
||||||
import ProfiledUnitsComponent from "./ProfiledUnits/ProfiledUnits";
|
import ProfiledUnitsComponent from "./ProfiledUnits/ProfiledUnits";
|
||||||
@@ -59,50 +48,10 @@ const CaseBuilder = () => {
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
const onBusCreated = () => {
|
|
||||||
const newScenario = createBus(scenario);
|
|
||||||
setAndSaveScenario(newScenario);
|
|
||||||
};
|
|
||||||
|
|
||||||
const onBusDataChanged = (
|
|
||||||
bus: string,
|
|
||||||
field: string,
|
|
||||||
newValue: string,
|
|
||||||
): ValidationError | null => {
|
|
||||||
const [newScenario, err] = changeBusData(bus, field, newValue, scenario);
|
|
||||||
if (err) {
|
|
||||||
setToastMessage(err.message);
|
|
||||||
return err;
|
|
||||||
}
|
|
||||||
setAndSaveScenario(newScenario);
|
|
||||||
return null;
|
|
||||||
};
|
|
||||||
|
|
||||||
const onBusDeleted = (bus: string): ValidationError | null => {
|
|
||||||
const newScenario = deleteBus(bus, scenario);
|
|
||||||
setAndSaveScenario(newScenario);
|
|
||||||
return null;
|
|
||||||
};
|
|
||||||
|
|
||||||
const onBusRenamed = (
|
|
||||||
oldName: string,
|
|
||||||
newName: string,
|
|
||||||
): ValidationError | null => {
|
|
||||||
const [newScenario, err] = renameBus(oldName, newName, scenario);
|
|
||||||
if (err) {
|
|
||||||
setToastMessage(err.message);
|
|
||||||
return err;
|
|
||||||
}
|
|
||||||
setAndSaveScenario(newScenario);
|
|
||||||
return null;
|
|
||||||
};
|
|
||||||
|
|
||||||
const onDataChanged = (newScenario: UnitCommitmentScenario) => {
|
const onDataChanged = (newScenario: UnitCommitmentScenario) => {
|
||||||
setAndSaveScenario(newScenario);
|
setAndSaveScenario(newScenario);
|
||||||
};
|
};
|
||||||
|
|
||||||
const onProfiledUnitCreated = () => {};
|
|
||||||
|
|
||||||
const onLoad = (scenario: UnitCommitmentScenario) => {
|
const onLoad = (scenario: UnitCommitmentScenario) => {
|
||||||
const preprocessed = preprocess(
|
const preprocessed = preprocess(
|
||||||
scenario,
|
scenario,
|
||||||
@@ -119,42 +68,24 @@ const CaseBuilder = () => {
|
|||||||
setToastMessage("Data loaded successfully");
|
setToastMessage("Data loaded successfully");
|
||||||
};
|
};
|
||||||
|
|
||||||
const onParameterChanged = (key: string, value: string) => {
|
|
||||||
let newScenario, err;
|
|
||||||
if (key === "Time horizon (h)") {
|
|
||||||
[newScenario, err] = changeTimeHorizon(scenario, value);
|
|
||||||
} else if (key === "Time step (min)") {
|
|
||||||
[newScenario, err] = changeTimeStep(scenario, value);
|
|
||||||
} else {
|
|
||||||
[newScenario, err] = changeParameter(scenario, key, value);
|
|
||||||
}
|
|
||||||
if (err) {
|
|
||||||
setToastMessage(err.message);
|
|
||||||
return err;
|
|
||||||
}
|
|
||||||
setAndSaveScenario(newScenario);
|
|
||||||
return null;
|
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<Header onClear={onClear} onSave={onSave} onLoad={onLoad} />
|
<Header onClear={onClear} onSave={onSave} onLoad={onLoad} />
|
||||||
<div className="content">
|
<div className="content">
|
||||||
<Parameters
|
<Parameters
|
||||||
onParameterChanged={onParameterChanged}
|
|
||||||
scenario={scenario}
|
scenario={scenario}
|
||||||
|
onDataChanged={onDataChanged}
|
||||||
|
onError={setToastMessage}
|
||||||
/>
|
/>
|
||||||
<Buses
|
<Buses
|
||||||
scenario={scenario}
|
scenario={scenario}
|
||||||
onBusCreated={onBusCreated}
|
|
||||||
onBusDataChanged={onBusDataChanged}
|
|
||||||
onBusRenamed={onBusRenamed}
|
|
||||||
onBusDeleted={onBusDeleted}
|
|
||||||
onDataChanged={onDataChanged}
|
onDataChanged={onDataChanged}
|
||||||
|
onError={setToastMessage}
|
||||||
/>
|
/>
|
||||||
<ProfiledUnitsComponent
|
<ProfiledUnitsComponent
|
||||||
scenario={scenario}
|
scenario={scenario}
|
||||||
onProfiledUnitCreated={onProfiledUnitCreated}
|
onDataChanged={onDataChanged}
|
||||||
|
onError={setToastMessage}
|
||||||
/>
|
/>
|
||||||
<Toast message={toastMessage} />
|
<Toast message={toastMessage} />
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -8,14 +8,36 @@ import SectionHeader from "../../Common/SectionHeader/SectionHeader";
|
|||||||
import Form from "../../Common/Forms/Form";
|
import Form from "../../Common/Forms/Form";
|
||||||
import TextInputRow from "../../Common/Forms/TextInputRow";
|
import TextInputRow from "../../Common/Forms/TextInputRow";
|
||||||
import { UnitCommitmentScenario } from "../../../core/fixtures";
|
import { UnitCommitmentScenario } from "../../../core/fixtures";
|
||||||
import { ValidationError } from "../../../core/Validation/validate";
|
import {
|
||||||
|
changeParameter,
|
||||||
|
changeTimeHorizon,
|
||||||
|
changeTimeStep,
|
||||||
|
} from "../../../core/Operations/parameterOperations";
|
||||||
|
|
||||||
interface ParametersProps {
|
interface ParametersProps {
|
||||||
scenario: UnitCommitmentScenario;
|
scenario: UnitCommitmentScenario;
|
||||||
onParameterChanged: (key: string, value: string) => ValidationError | null;
|
onError: (msg: string) => void;
|
||||||
|
onDataChanged: (scenario: UnitCommitmentScenario) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
function Parameters(props: ParametersProps) {
|
function Parameters(props: ParametersProps) {
|
||||||
|
const onDataChanged = (key: string, value: string) => {
|
||||||
|
let newScenario, err;
|
||||||
|
if (key === "Time horizon (h)") {
|
||||||
|
[newScenario, err] = changeTimeHorizon(props.scenario, value);
|
||||||
|
} else if (key === "Time step (min)") {
|
||||||
|
[newScenario, err] = changeTimeStep(props.scenario, value);
|
||||||
|
} else {
|
||||||
|
[newScenario, err] = changeParameter(props.scenario, key, value);
|
||||||
|
}
|
||||||
|
if (err) {
|
||||||
|
props.onError(err.message);
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
props.onDataChanged(newScenario);
|
||||||
|
return null;
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<SectionHeader title="Parameters"></SectionHeader>
|
<SectionHeader title="Parameters"></SectionHeader>
|
||||||
@@ -25,23 +47,21 @@ function Parameters(props: ParametersProps) {
|
|||||||
unit="h"
|
unit="h"
|
||||||
tooltip="Length of the planning horizon (in hours)."
|
tooltip="Length of the planning horizon (in hours)."
|
||||||
initialValue={`${props.scenario.Parameters["Time horizon (h)"]}`}
|
initialValue={`${props.scenario.Parameters["Time horizon (h)"]}`}
|
||||||
onChange={(v) => props.onParameterChanged("Time horizon (h)", v)}
|
onChange={(v) => onDataChanged("Time horizon (h)", v)}
|
||||||
/>
|
/>
|
||||||
<TextInputRow
|
<TextInputRow
|
||||||
label="Time step"
|
label="Time step"
|
||||||
unit="min"
|
unit="min"
|
||||||
tooltip="Length of each time step (in minutes). Must be a divisor of 60 (e.g. 60, 30, 20, 15, etc)."
|
tooltip="Length of each time step (in minutes). Must be a divisor of 60 (e.g. 60, 30, 20, 15, etc)."
|
||||||
initialValue={`${props.scenario.Parameters["Time step (min)"]}`}
|
initialValue={`${props.scenario.Parameters["Time step (min)"]}`}
|
||||||
onChange={(v) => props.onParameterChanged("Time step (min)", v)}
|
onChange={(v) => onDataChanged("Time step (min)", v)}
|
||||||
/>
|
/>
|
||||||
<TextInputRow
|
<TextInputRow
|
||||||
label="Power balance penalty"
|
label="Power balance penalty"
|
||||||
unit="$/MW"
|
unit="$/MW"
|
||||||
tooltip="Penalty for system-wide shortage or surplus in production (in /MW). This is charged per time step. For example, if there is a shortage of 1 MW for three time steps, three times this amount will be charged."
|
tooltip="Penalty for system-wide shortage or surplus in production (in /MW). This is charged per time step. For example, if there is a shortage of 1 MW for three time steps, three times this amount will be charged."
|
||||||
initialValue={`${props.scenario.Parameters["Power balance penalty ($/MW)"]}`}
|
initialValue={`${props.scenario.Parameters["Power balance penalty ($/MW)"]}`}
|
||||||
onChange={(v) =>
|
onChange={(v) => onDataChanged("Power balance penalty ($/MW)", v)}
|
||||||
props.onParameterChanged("Power balance penalty ($/MW)", v)
|
|
||||||
}
|
|
||||||
/>
|
/>
|
||||||
</Form>
|
</Form>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -16,66 +16,94 @@ import DataTable, {
|
|||||||
generateCsv,
|
generateCsv,
|
||||||
generateTableColumns,
|
generateTableColumns,
|
||||||
generateTableData,
|
generateTableData,
|
||||||
|
parseCsv,
|
||||||
} from "../../Common/Forms/DataTable";
|
} from "../../Common/Forms/DataTable";
|
||||||
import { UnitCommitmentScenario } from "../../../core/fixtures";
|
import { UnitCommitmentScenario } from "../../../core/fixtures";
|
||||||
import { ColumnDefinition } from "tabulator-tables";
|
import { ColumnDefinition } from "tabulator-tables";
|
||||||
import { offerDownload } from "../../Common/io";
|
import { offerDownload } from "../../Common/io";
|
||||||
|
import FileUploadElement from "../../Common/Buttons/FileUploadElement";
|
||||||
|
import { useRef } from "react";
|
||||||
|
|
||||||
interface ProfiledUnitsProps {
|
interface ProfiledUnitsProps {
|
||||||
scenario: UnitCommitmentScenario;
|
scenario: UnitCommitmentScenario;
|
||||||
onProfiledUnitCreated: () => void;
|
onDataChanged: (scenario: UnitCommitmentScenario) => void;
|
||||||
|
onError: (msg: string) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const ProfiledUnitsColumnSpec: ColumnSpec[] = [
|
||||||
|
{
|
||||||
|
title: "Name",
|
||||||
|
type: "string",
|
||||||
|
width: 150,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
title: "Bus",
|
||||||
|
type: "string",
|
||||||
|
width: 150,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
title: "Cost ($/MW)",
|
||||||
|
type: "number",
|
||||||
|
width: 100,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
title: "Maximum power (MW)",
|
||||||
|
type: "number[]",
|
||||||
|
width: 60,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
title: "Minimum power (MW)",
|
||||||
|
type: "number[]",
|
||||||
|
width: 60,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
const generateProfiledUnitsData = (
|
const generateProfiledUnitsData = (
|
||||||
scenario: UnitCommitmentScenario,
|
scenario: UnitCommitmentScenario,
|
||||||
): [any[], ColumnDefinition[]] => {
|
): [any[], ColumnDefinition[]] => {
|
||||||
const colSpecs: ColumnSpec[] = [
|
const columns = generateTableColumns(scenario, ProfiledUnitsColumnSpec);
|
||||||
{
|
const data = generateTableData(
|
||||||
title: "Name",
|
scenario.Generators,
|
||||||
type: "string",
|
ProfiledUnitsColumnSpec,
|
||||||
width: 150,
|
scenario,
|
||||||
},
|
);
|
||||||
{
|
|
||||||
title: "Bus",
|
|
||||||
type: "string",
|
|
||||||
width: 150,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
title: "Cost ($/MW)",
|
|
||||||
type: "number",
|
|
||||||
width: 100,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
title: "Maximum power (MW)",
|
|
||||||
type: "number[]",
|
|
||||||
width: 60,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
title: "Minimum power (MW)",
|
|
||||||
type: "number[]",
|
|
||||||
width: 60,
|
|
||||||
},
|
|
||||||
];
|
|
||||||
const columns = generateTableColumns(scenario, colSpecs);
|
|
||||||
const data = generateTableData(scenario.Generators, colSpecs, scenario);
|
|
||||||
return [data, columns];
|
return [data, columns];
|
||||||
};
|
};
|
||||||
|
|
||||||
const ProfiledUnitsComponent = (props: ProfiledUnitsProps) => {
|
const ProfiledUnitsComponent = (props: ProfiledUnitsProps) => {
|
||||||
|
const fileUploadElem = useRef<FileUploadElement>(null);
|
||||||
|
|
||||||
const onDownload = () => {
|
const onDownload = () => {
|
||||||
const [data, columns] = generateProfiledUnitsData(props.scenario);
|
const [data, columns] = generateProfiledUnitsData(props.scenario);
|
||||||
const csvContents = generateCsv(data, columns);
|
const csvContents = generateCsv(data, columns);
|
||||||
offerDownload(csvContents, "text/csv", "profiled_units.csv");
|
offerDownload(csvContents, "text/csv", "profiled_units.csv");
|
||||||
};
|
};
|
||||||
const onUpload = () => {};
|
|
||||||
|
const onUpload = () => {
|
||||||
|
fileUploadElem.current!.showFilePicker((csvContents: any) => {
|
||||||
|
const [newGenerators, err] = parseCsv(
|
||||||
|
csvContents,
|
||||||
|
ProfiledUnitsColumnSpec,
|
||||||
|
props.scenario,
|
||||||
|
);
|
||||||
|
if (err) {
|
||||||
|
props.onError(err.message);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const newScenario = {
|
||||||
|
...props.scenario,
|
||||||
|
Generators: newGenerators,
|
||||||
|
};
|
||||||
|
props.onDataChanged(newScenario);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const onAdd = () => {};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<SectionHeader title="Profiled Units">
|
<SectionHeader title="Profiled Units">
|
||||||
<SectionButton
|
<SectionButton icon={faPlus} tooltip="Add" onClick={onAdd} />
|
||||||
icon={faPlus}
|
|
||||||
tooltip="Add"
|
|
||||||
onClick={props.onProfiledUnitCreated}
|
|
||||||
/>
|
|
||||||
<SectionButton
|
<SectionButton
|
||||||
icon={faDownload}
|
icon={faDownload}
|
||||||
tooltip="Download"
|
tooltip="Download"
|
||||||
@@ -95,6 +123,7 @@ const ProfiledUnitsComponent = (props: ProfiledUnitsProps) => {
|
|||||||
}}
|
}}
|
||||||
generateData={() => generateProfiledUnitsData(props.scenario)}
|
generateData={() => generateProfiledUnitsData(props.scenario)}
|
||||||
/>
|
/>
|
||||||
|
<FileUploadElement ref={fileUploadElem} accept=".csv" />
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -5,9 +5,11 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import assert from "node:assert";
|
import assert from "node:assert";
|
||||||
import { parseBusesCsv } from "../../CaseBuilder/Buses/BusesCsv";
|
import {
|
||||||
import { generateBusesData } from "../../CaseBuilder/Buses/Buses";
|
BusesColumnSpec,
|
||||||
import { generateCsv } from "./DataTable";
|
generateBusesData,
|
||||||
|
} from "../../CaseBuilder/Buses/Buses";
|
||||||
|
import { generateCsv, parseCsv } from "./DataTable";
|
||||||
import { TEST_DATA_1 } from "../../../core/fixtures.test";
|
import { TEST_DATA_1 } from "../../../core/fixtures.test";
|
||||||
|
|
||||||
test("generate CSV", () => {
|
test("generate CSV", () => {
|
||||||
@@ -21,38 +23,19 @@ test("generate CSV", () => {
|
|||||||
assert.strictEqual(actualCsv, expectedCsv);
|
assert.strictEqual(actualCsv, expectedCsv);
|
||||||
});
|
});
|
||||||
|
|
||||||
test("parse valid CSV", () => {
|
test("parse CSV", () => {
|
||||||
// const csvContents =
|
|
||||||
// "Name,Load 0,Load 1,Load 2,Load 3,Load 4\n" +
|
|
||||||
// "b1,0,1,2,3,4\n" +
|
|
||||||
// "b3,27.3729,26.29698,25.58005,25.15675,25.4268";
|
|
||||||
// const newScenario = parseBusesCsv(FixturesTest, csvContents);
|
|
||||||
// assert.deepEqual(newScenario.Buses, {
|
|
||||||
// b1: {
|
|
||||||
// "Load (MW)": [0, 1, 2, 3, 4],
|
|
||||||
// },
|
|
||||||
// b3: {
|
|
||||||
// "Load (MW)": [27.3729, 26.29698, 25.58005, 25.15675, 25.4268],
|
|
||||||
// },
|
|
||||||
// });
|
|
||||||
});
|
|
||||||
|
|
||||||
test("parse invalid CSV (wrong headers)", () => {
|
|
||||||
const csvContents =
|
const csvContents =
|
||||||
"Name,Load 5,Load 7,Load 23,Load 3,Load 4\n" +
|
"Name,Load (MW) 00:00,Load (MW) 01:00,Load (MW) 02:00,Load (MW) 03:00,Load (MW) 04:00\n" +
|
||||||
"b1,0,1,2,3,4\n" +
|
"b1,0,1,2,3,4\n" +
|
||||||
"b3,27.3729,26.29698,25.58005,25.15675,25.4268";
|
"b3,27.3729,26.29698,25.58005,25.15675,25.4268";
|
||||||
expect(() => {
|
const [newBuses, err] = parseCsv(csvContents, BusesColumnSpec, TEST_DATA_1);
|
||||||
parseBusesCsv(TEST_DATA_1, csvContents);
|
assert(err === null);
|
||||||
}).toThrow(Error);
|
assert.deepEqual(newBuses, {
|
||||||
});
|
b1: {
|
||||||
|
"Load (MW)": [0, 1, 2, 3, 4],
|
||||||
test("parse invalid CSV (wrong data length)", () => {
|
},
|
||||||
const csvContents =
|
b3: {
|
||||||
"Name,Load 0,Load 1,Load 2,Load 3,Load 4\n" +
|
"Load (MW)": [27.3729, 26.29698, 25.58005, 25.15675, 25.4268],
|
||||||
"b1,0,1,2,3\n" +
|
},
|
||||||
"b3,27.3729,26.29698,25.58005,25.15675,25.4268";
|
});
|
||||||
expect(() => {
|
|
||||||
parseBusesCsv(TEST_DATA_1, csvContents);
|
|
||||||
}).toThrow(Error);
|
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import {
|
|||||||
} from "tabulator-tables";
|
} from "tabulator-tables";
|
||||||
import { ValidationError } from "../../../core/Validation/validate";
|
import { ValidationError } from "../../../core/Validation/validate";
|
||||||
import { UnitCommitmentScenario } from "../../../core/fixtures";
|
import { UnitCommitmentScenario } from "../../../core/fixtures";
|
||||||
|
import Papa from "papaparse";
|
||||||
|
|
||||||
export interface ColumnSpec {
|
export interface ColumnSpec {
|
||||||
title: string;
|
title: string;
|
||||||
@@ -126,17 +127,85 @@ export const generateCsv = (data: any[], columns: ColumnDefinition[]) => {
|
|||||||
return `${csvHeader}\n${csvBody}`;
|
return `${csvHeader}\n${csvBody}`;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const floatFormatter = (cell: CellComponent) => {
|
export const parseCsv = (
|
||||||
return parseFloat(cell.getValue()).toFixed(1);
|
csvContents: string,
|
||||||
|
colSpecs: ColumnSpec[],
|
||||||
|
scenario: UnitCommitmentScenario,
|
||||||
|
): [any, ValidationError | null] => {
|
||||||
|
// Parse contents
|
||||||
|
const csv = Papa.parse(csvContents, {
|
||||||
|
header: true,
|
||||||
|
skipEmptyLines: true,
|
||||||
|
transformHeader: (header) => header.trim(),
|
||||||
|
transform: (value) => value.trim(),
|
||||||
|
});
|
||||||
|
|
||||||
|
// Check for parsing errors
|
||||||
|
if (csv.errors.length > 0) {
|
||||||
|
console.error(csv.errors);
|
||||||
|
return [null, { message: "Could not parse CSV file" }];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check CSV headers
|
||||||
|
const columns = generateTableColumns(scenario, colSpecs);
|
||||||
|
const expectedHeader: string[] = [];
|
||||||
|
columns.forEach((column) => {
|
||||||
|
if (column.columns) {
|
||||||
|
column.columns.forEach((subcolumn) => {
|
||||||
|
expectedHeader.push(subcolumn.field!);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
expectedHeader.push(column.field!);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
const actualHeader = csv.meta.fields!;
|
||||||
|
for (let i = 0; i < expectedHeader.length; i++) {
|
||||||
|
if (expectedHeader[i] !== actualHeader[i]) {
|
||||||
|
return [
|
||||||
|
null,
|
||||||
|
{
|
||||||
|
message: `Invalid CSV: Header mismatch at column ${i + 1}.
|
||||||
|
Expected "${expectedHeader[i]}", found "${actualHeader[i]}"`,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse each row
|
||||||
|
const timeslots = generateTimeslots(scenario);
|
||||||
|
const data: { [key: string]: any } = {};
|
||||||
|
for (let i = 0; i < csv.data.length; i++) {
|
||||||
|
const row = csv.data[i] as { [key: string]: any };
|
||||||
|
const name = row["Name"] as string;
|
||||||
|
data[name] = {};
|
||||||
|
|
||||||
|
for (const spec of colSpecs) {
|
||||||
|
if (spec.title === "Name") continue;
|
||||||
|
switch (spec.type) {
|
||||||
|
case "string":
|
||||||
|
case "number":
|
||||||
|
data[name][spec.title] = row[spec.title];
|
||||||
|
break;
|
||||||
|
case "number[]":
|
||||||
|
data[name][spec.title] = Array(timeslots.length);
|
||||||
|
|
||||||
|
for (let i = 0; i < timeslots.length; i++) {
|
||||||
|
data[name][spec.title][i] = parseFloat(
|
||||||
|
row[`${spec.title} ${timeslots[i]}`],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
console.error(`Unknown type: ${spec.type}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return [data, null];
|
||||||
};
|
};
|
||||||
|
|
||||||
export const addNameColumn = (columns: ColumnDefinition[]) => {
|
export const floatFormatter = (cell: CellComponent) => {
|
||||||
columns.push({
|
return parseFloat(cell.getValue()).toFixed(1);
|
||||||
...columnsCommonAttrs,
|
|
||||||
title: "Name",
|
|
||||||
field: "Name",
|
|
||||||
minWidth: 150,
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export const generateTimeslots = (scenario: UnitCommitmentScenario) => {
|
export const generateTimeslots = (scenario: UnitCommitmentScenario) => {
|
||||||
@@ -156,29 +225,6 @@ export const generateTimeslots = (scenario: UnitCommitmentScenario) => {
|
|||||||
return timeslots;
|
return timeslots;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const addTimeseriesColumn = (
|
|
||||||
scenario: UnitCommitmentScenario,
|
|
||||||
title: string,
|
|
||||||
columns: ColumnDefinition[],
|
|
||||||
minWidth: number = 65,
|
|
||||||
) => {
|
|
||||||
const timeSlots = generateTimeslots(scenario);
|
|
||||||
const subColumns: ColumnDefinition[] = [];
|
|
||||||
timeSlots.forEach((t) => {
|
|
||||||
subColumns.push({
|
|
||||||
...columnsCommonAttrs,
|
|
||||||
title: `${t}`,
|
|
||||||
field: `${title} ${t}`,
|
|
||||||
minWidth: minWidth,
|
|
||||||
formatter: floatFormatter,
|
|
||||||
});
|
|
||||||
});
|
|
||||||
columns.push({
|
|
||||||
title: title,
|
|
||||||
columns: subColumns,
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
export const columnsCommonAttrs: ColumnDefinition = {
|
export const columnsCommonAttrs: ColumnDefinition = {
|
||||||
headerHozAlign: "left",
|
headerHozAlign: "left",
|
||||||
hozAlign: "left",
|
hozAlign: "left",
|
||||||
|
|||||||
Reference in New Issue
Block a user