Skip to content

Commit

Permalink
Merge pull request #53 from carbonplan/katamartin/equally-spaced-vali…
Browse files Browse the repository at this point in the history
…dation

Add validation check that proxied datasets have equally spaced spatial coordinates
  • Loading branch information
katamartin authored Jan 17, 2025
2 parents 78c3ad7 + 912bed5 commit 42d3f20
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
17 changes: 16 additions & 1 deletion components/utils/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
// LZ4,
// Zstd,
} from 'numcodecs'

import { deviation } from 'd3-array'
import { PROJECTIONS, ASPECTS } from '../constants'

const COMPRESSORS = {
Expand Down Expand Up @@ -270,6 +270,21 @@ export const getVariableLevelInfo = async (
const axes = ['X', 'Y'].reduce((accum, key, i) => {
const index = dimensions.indexOf(cfAxes[name][key])
const array = coordinates[i]
if (!pyramid) {
const stepValues = array.data.reduce((a, d, i) => {
if (i === 0) return a
const delta = d - array.data[i - 1]
a.add(delta)
return a
}, new Set())
const stepDeviation = deviation(stepValues)
if (stepDeviation && stepDeviation > 1e-3) {
throw new Error(
`Cannot handle coordinate: ${cfAxes[name][key]}. Spatial coordinates must be equally spaced.`
)
}
}

const step = Math.abs(Number(array.data[0]) - Number(array.data[1]))
const reversed = array.data[0] > array.data[array.data.length - 1]

Expand Down
36 changes: 32 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"@mdx-js/react": "^2.1.5",
"@next/mdx": "^12.3.1",
"@theme-ui/color": "^0.15.3",
"d3-array": "^3.1.0",
"d3-format": "^3.1.0",
"d3-geo": "^3.1.0",
"d3-scale": "^3.1.0",
Expand Down

0 comments on commit 42d3f20

Please sign in to comment.