1- import type { ClassOutput , OutputVariableKey } from "@classmodel/class/output" ;
1+ import type { OutputVariableKey } from "@classmodel/class/output" ;
2+ import type { TimeSeries0D } from "@classmodel/class/runner" ;
23import { BlobReader , BlobWriter , ZipWriter } from "@zip.js/zip.js" ;
34import { toPartial } from "./encode" ;
45import type { ExperimentConfig } from "./experiment_config" ;
@@ -11,12 +12,21 @@ export function toConfigBlob(experiment: ExperimentConfig) {
1112 } ) ;
1213}
1314
14- function outputToCsv ( output : ClassOutput ) {
15+ export function outputToCsv ( output : TimeSeries0D ) {
1516 const headers = Object . keys ( output ) as OutputVariableKey [ ] ;
16- const lines = [ headers . join ( "," ) ] ;
17- for ( let i = 0 ; i < output [ headers [ 0 ] ] . length ; i ++ ) {
18- lines . push ( headers . map ( ( h ) => output [ h ] [ i ] ) . join ( "," ) ) ;
17+ const lines : string [ ] = [ ] ;
18+
19+ // CSV header
20+ lines . push ( headers . join ( "," ) ) ;
21+
22+ // Determine number of rows from the first variable
23+ const nRows = output [ headers [ 0 ] ] ?. length ?? 0 ;
24+
25+ for ( let i = 0 ; i < nRows ; i ++ ) {
26+ const row = headers . map ( ( h ) => output [ h ] [ i ] ) ;
27+ lines . push ( row . join ( "," ) ) ;
1928 }
29+
2030 return lines . join ( "\n" ) ;
2131}
2232
@@ -32,9 +42,12 @@ export async function createArchive(experiment: Experiment) {
3242 await zipWriter . add ( "config.json" , new BlobReader ( configBlob ) ) ;
3343
3444 if ( experiment . output . reference ) {
35- const csvBlob = new Blob ( [ outputToCsv ( experiment . output . reference ) ] , {
36- type : "text/csv" ,
37- } ) ;
45+ const csvBlob = new Blob (
46+ [ outputToCsv ( experiment . output . reference . timeseries ) ] ,
47+ {
48+ type : "text/csv" ,
49+ } ,
50+ ) ;
3851 await zipWriter . add (
3952 `${ experiment . config . reference . name } .csv` ,
4053 new BlobReader ( csvBlob ) ,
@@ -45,7 +58,7 @@ export async function createArchive(experiment: Experiment) {
4558 const permConfig = experiment . config . permutations [ index ] ;
4659 const permutationOutput = experiment . output . permutations [ index ] ;
4760 if ( permutationOutput ) {
48- const csvBlob = new Blob ( [ outputToCsv ( permutationOutput ) ] , {
61+ const csvBlob = new Blob ( [ outputToCsv ( permutationOutput . timeseries ) ] , {
4962 type : "text/csv" ,
5063 } ) ;
5164 await zipWriter . add ( `${ permConfig . name } .csv` , new BlobReader ( csvBlob ) ) ;
0 commit comments