From 1ad7cb35c66c95b2b9086e520410d45aaeadc4cf Mon Sep 17 00:00:00 2001 From: ben-chamberlain Date: Mon, 12 Dec 2022 13:56:59 +1300 Subject: [PATCH 1/3] Fix/UHS CSV (#309) * fix UHS CSV latlon; --- src/services/spectralAccel/spectralAccel.service.ts | 10 ++++++---- src/views/hazardCharts/HazardChartsSettings.tsx | 2 +- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/services/spectralAccel/spectralAccel.service.ts b/src/services/spectralAccel/spectralAccel.service.ts index 2e06c270..7702b500 100644 --- a/src/services/spectralAccel/spectralAccel.service.ts +++ b/src/services/spectralAccel/spectralAccel.service.ts @@ -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'; @@ -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[][] => { @@ -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() || '']; diff --git a/src/views/hazardCharts/HazardChartsSettings.tsx b/src/views/hazardCharts/HazardChartsSettings.tsx index fccbd452..c5a784f2 100644 --- a/src/views/hazardCharts/HazardChartsSettings.tsx +++ b/src/views/hazardCharts/HazardChartsSettings.tsx @@ -121,7 +121,7 @@ const HazardChartsSettings: React.FC = ({ data, spect {spectral ? ( - + From a370396ff63530cc2ad872ce67623008c95a6013 Mon Sep 17 00:00:00 2001 From: ben-chamberlain Date: Tue, 13 Dec 2022 11:30:53 +1300 Subject: [PATCH 2/3] sort hazard curve CSV SA; --- src/views/hazardCharts/hazardPage.service.ts | 51 +++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/src/views/hazardCharts/hazardPage.service.ts b/src/views/hazardCharts/hazardPage.service.ts index 22c5bb93..cfcd6ad4 100644 --- a/src/views/hazardCharts/hazardPage.service.ts +++ b/src/views/hazardCharts/hazardPage.service.ts @@ -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 | null; + readonly values: ReadonlyArray | null; + } | null; +} + export type HazardUncertaintyChartCurveGroup = Record; export type HazardUncertaintyChartData = Record; @@ -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]; From 9db53df76e4a43a3b6216a2a2cf0c72c3240de1c Mon Sep 17 00:00:00 2001 From: ben-chamberlain Date: Tue, 13 Dec 2022 15:37:42 +1300 Subject: [PATCH 3/3] v1.0.6 --- package.json | 2 +- src/CHANGELOG.md | 13 ++++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index f56c3eb7..8a6dd3d3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "kororaa", - "version": "1.0.5", + "version": "1.0.6", "private": true, "dependencies": { "@emotion/react": "^11.9.0", diff --git a/src/CHANGELOG.md b/src/CHANGELOG.md index e8594348..0b977e6d 100644 --- a/src/CHANGELOG.md +++ b/src/CHANGELOG.md @@ -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