Skip to content

Commit

Permalink
Merge pull request #311 from GNS-Science/deploy-test
Browse files Browse the repository at this point in the history
Fix/UHS CSV (#309)
  • Loading branch information
benjamineac authored Dec 13, 2022
2 parents bc576e4 + 9db53df commit 2ce3a47
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "kororaa",
"version": "1.0.5",
"version": "1.0.6",
"private": true,
"dependencies": {
"@emotion/react": "^11.9.0",
Expand Down
13 changes: 12 additions & 1 deletion src/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).

## [v1.0.5](https://github.com/GNS-Science/kororaa/compare/v1.0.4...v1.0.5)
## [v1.0.6](https://github.com/GNS-Science/kororaa/compare/v1.0.5...v1.0.6)

### Merged

- Fix/310 Sort SA CSV [`#312`](https://github.com/GNS-Science/kororaa/pull/312)
- Fix/UHS CSV [`#309`](https://github.com/GNS-Science/kororaa/pull/309)

### Commits

- sort hazard curve CSV SA; [`a370396`](https://github.com/GNS-Science/kororaa/commit/a370396ff63530cc2ad872ce67623008c95a6013)

## [v1.0.5](https://github.com/GNS-Science/kororaa/compare/v1.0.4...v1.0.5) - 2022-12-07

### Merged

Expand Down
10 changes: 6 additions & 4 deletions src/services/spectralAccel/spectralAccel.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as mathjs from 'mathjs';

import { hazardPageOptions } from '../../views/hazardCharts/constants/hazardPageOptions';
import { HazardChartsPlotsViewQuery$data } from '../../views/hazardCharts/__generated__/HazardChartsPlotsViewQuery.graphql';
import { getLatLonFromLocationKey, roundLatLon } from '../latLon/latLon.service';
import { getLatLonFromLocationName, roundLatLon } from '../latLon/latLon.service';
import { getColor } from '../../utils/colorUtils';
import { SA_LOG_PGA_SUBSTITUTE, HAZARD_IMTS, MEAN, LOWER1, LOWER2, UPPER1, UPPER2, HAZARD_MODEL_VERSION } from '../../utils/environmentVariables';

Expand Down Expand Up @@ -131,8 +131,10 @@ export const sortSACurveGroups = (curveGroups: UncertaintyChartData): Uncertaint

export const tryParseLatLon = (loc: string): string[] => {
if (loc.split(',').length === 1) {
return getLatLonFromLocationKey(loc).split(',');
} else return loc.split(',');
return getLatLonFromLocationName(loc)
.split(',')
.map((l) => l.trim());
} else return loc.split(',').map((l) => l.trim());
};

export const getSpectralCSVData = (curves: UncertaintyChartData, poe: number | undefined): string[][] => {
Expand All @@ -142,7 +144,7 @@ export const getSpectralCSVData = (curves: UncertaintyChartData, poe: number | u
Object.fromEntries(
Object.entries(curves).map((curve) => {
const vs30 = curve[0].split(' ')[0].replace('m/s', '');
const location = curve[0].split(' ').length === 3 ? curve[0].split(' ')[1] + curve[0].split(' ')[2] : curve[0].split(' ')[1];
const location = curve[0].split(' ').length === 3 ? `${curve[0].split(' ')[1]} ${curve[0].split(' ')[2]}` : curve[0].split(' ')[1];
const latLon = tryParseLatLon(location);
Object.entries(curve[1])?.forEach((value) => {
const curveCSVData = [latLon[0], latLon[1], vs30, (poe && poe * 100)?.toString() || ''];
Expand Down
2 changes: 1 addition & 1 deletion src/views/hazardCharts/HazardChartsSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ const HazardChartsSettings: React.FC<HazardChartsSettingsProps> = ({ data, spect
</MenuItem>
<MenuItem>
{spectral ? (
<StyledCSVLink data={saCSVData} filename="spectral-acceleration-curves.csv">
<StyledCSVLink data={saCSVData} filename="UHS-curves.csv">
<StyledIconButton>
<TextSnippetIcon />
</StyledIconButton>
Expand Down
51 changes: 50 additions & 1 deletion src/views/hazardCharts/hazardPage.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ export interface HazardUncertaintyChart {
data: UncertaintyDatum[];
}

export interface HazardCurve {
readonly hazard_model: string | null;
readonly imt: string | null;
readonly loc: string | null;
readonly agg: string | null;
readonly vs30: number | null;
readonly curve: {
readonly levels: ReadonlyArray<number | null> | null;
readonly values: ReadonlyArray<number | null> | null;
} | null;
}

export type HazardUncertaintyChartCurveGroup = Record<string, HazardUncertaintyChart>;

export type HazardUncertaintyChartData = Record<string, HazardUncertaintyChartCurveGroup>;
Expand Down Expand Up @@ -74,8 +86,45 @@ export const getFilteredCurveGroups = (curveGroups: HazardUncertaintyChartData,
};

export const getHazardCSVData = (data: HazardChartsPlotsViewQuery$data): string[][] => {
const regExp = /\(([^)]+)\)/g;
const hazardCurves: HazardCurve[] = [];
//create mutable copy of hazard curves
data.hazard_curves?.curves && data.hazard_curves?.curves.forEach((val) => hazardCurves.push(Object.assign({}, val)));
//separate hazard curves by location
const hazardCurvesByLoc: HazardCurve[][] = [];
hazardCurves.forEach((curve) => {
const latLon = curve?.loc;
if (latLon) {
const index = hazardCurvesByLoc.findIndex((curve) => curve[0]?.loc === latLon);
if (index === -1) {
hazardCurvesByLoc.push([curve]);
} else {
hazardCurvesByLoc[index].push(curve);
}
}
});
//sort hazard curves by imt, parsing imts number value to sort the 10.0 properly
const sortedHazardCurves: HazardCurve[] = hazardCurvesByLoc
.map((a) =>
a.sort((a, b) => {
if (a?.imt && b?.imt) {
const aMatch = a?.imt.match(regExp);
const bMatch = b?.imt.match(regExp);
if (aMatch && bMatch) {
const aMatchNumber = parseFloat(aMatch[0].replace(/[()]/g, ''));
const bMatchNumber = parseFloat(bMatch[0].replace(/[()]/g, ''));
if (aMatchNumber && bMatchNumber) {
return aMatchNumber - bMatchNumber;
}
}
}
return 0;
}),
)
.flat();

const datetimeAndVersion = [`date-time: ${new Date().toLocaleString('en-GB', { timeZone: 'UTC' })}, (UTC)`, `NSHM model version: ${HAZARD_MODEL_VERSION}`];
const CSVData = data.hazard_curves?.curves?.map((curve) => {
const CSVData = sortedHazardCurves.map((curve) => {
const latLonArray = curve?.loc?.split('~');
if (latLonArray && curve?.curve?.values && curve?.vs30) {
const curveCSVData = [latLonArray[0], latLonArray[1], curve?.vs30.toString(), curve?.imt, curve?.agg];
Expand Down

0 comments on commit 2ce3a47

Please sign in to comment.