components/data/data.component.ts
Table of MFD data from service response.
selector | app-data |
imports |
NshmpLibNgDataTablePanelComponent
LowerCasePipe
|
templateUrl | ./data.component.html |
styleUrl | ./data.component.scss |
Properties |
Methods |
|
constructor(service: AppService)
|
||||||
Defined in components/data/data.component.ts:36
|
||||||
Parameters :
|
mfdFilename |
mfdFilename()
|
Defined in components/data/data.component.ts:46
|
Return the filename to save the data.
Returns :
string
|
Private toTableData | ||||||||||||
toTableData(serviceResponse: SourceLogicTreesResponse, form: ControlForm)
|
||||||||||||
Defined in components/data/data.component.ts:72
|
||||||||||||
Convert service response to table data.
Parameters :
Returns :
TableData[]
|
mfdTableData |
Default value : computed(() => {
const serviceResponse = this.service.serviceResponse();
if (serviceResponse) {
return this.toTableData(
serviceResponse,
this.service.formGroup.getRawValue(),
);
} else {
return [];
}
})
|
Defined in components/data/data.component.ts:25
|
MFD table data |
import {LowerCasePipe} from '@angular/common';
import {Component, computed} from '@angular/core';
import {hazardUtils} from '@ghsc/nshmp-lib-ng/hazard';
import {
NshmpLibNgDataTablePanelComponent,
TableData,
} from '@ghsc/nshmp-lib-ng/nshmp';
import {SourceLogicTreesResponse} from '@ghsc/nshmp-utils-ts/libs/nshmp-haz/www/source-logic-trees-service';
import {Sequences} from '@ghsc/nshmp-utils-ts/libs/nshmp-lib/data';
import {ControlForm} from '../../models/control-form.model';
import {AppService} from '../../services/app.service';
/**
* Table of MFD data from service response.
*/
@Component({
imports: [NshmpLibNgDataTablePanelComponent, LowerCasePipe],
selector: 'app-data',
styleUrl: './data.component.scss',
templateUrl: './data.component.html',
})
export class DataComponent {
/** MFD table data */
mfdTableData = computed(() => {
const serviceResponse = this.service.serviceResponse();
if (serviceResponse) {
return this.toTableData(
serviceResponse,
this.service.formGroup.getRawValue(),
);
} else {
return [];
}
});
constructor(private service: AppService) {}
/**
* Return the filename to save the data.
*
* @param usage The source logic tree usage
* @param form Control panel form field values
*/
mfdFilename(): string {
const values = this.service.formGroup.getRawValue();
if (values.source === null || this.service.usage() === null) {
return 'mfd.csv';
}
const sourceTree = this.service
.findTreeInfo(
this.service.usage().response.trees,
values.source,
values.sourceTree,
)
?.name?.replace(/ /g, '_');
return `mfd-${values.source.tectonicSettings}-${values.source.sourceType}-${
sourceTree ?? values.sourceTree
}.csv`;
}
/**
* Convert service response to table data.
*
* @param branches The source branches
* @param form Control panel form field values
*/
private toTableData(
serviceResponse: SourceLogicTreesResponse,
form: ControlForm,
): TableData[] {
const branches = serviceResponse.response.branches;
const tree = serviceResponse.response;
if (branches === null || branches === undefined) {
return [];
}
const totalMfd = form.cumulative
? Sequences.toCumulative(tree.totalMfd)
: tree.totalMfd;
const tableData: TableData[] = [
{
label: 'Total MFD',
td: totalMfd.xs,
th: 'Magnitude',
},
{
td: totalMfd.ys.map(y => y.toExponential(4)),
th: 'Rate',
},
];
branches.forEach(branch => {
this.service.filterMfds(branch.mfds, form.mfdType).forEach(mfdInfo => {
const label = `${branch.path} - ${mfdInfo.id}`;
let xySequence = hazardUtils.cleanXySequence(mfdInfo.mfd.data);
if (form.cumulative) {
xySequence = Sequences.toCumulative(xySequence);
}
tableData.push({
label,
td: xySequence.xs,
th: 'Magnitude',
});
tableData.push({
td: xySequence.ys.map(y => y.toExponential(4)),
th: 'Rate',
});
tableData.push({
td: [branch.weight],
th: 'Branch Weight',
});
tableData.push({
td: [mfdInfo.weight],
th: 'MFD Weight',
});
Object.entries(mfdInfo.mfd.props).forEach(
([key, value]: [string, string]) => {
tableData.push({
td: [value],
th: key,
});
},
);
});
});
return tableData;
}
}
<nshmp-lib-ng-data-table-panel
[table]="mfdTableData()"
[filename]="mfdFilename() | lowercase"
buttonText="Export MFD as CSV"
title="MFD Data"
/>