Skip to content

Commit

Permalink
Add basic spatial coordinate check
Browse files Browse the repository at this point in the history
  • Loading branch information
katamartin committed Jul 1, 2024
1 parent ab1850a commit 75e31fa
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 15 deletions.
23 changes: 13 additions & 10 deletions components/data/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,21 +91,24 @@ const useStore = create((set, get) => ({
})
_registerLoading(name)

const { centerPoint: variableCenterPoint, selectors } =
await dataset.initializeVariable(name)

set({ selectors })
let cp
try {
const { centerPoint: variableCenterPoint, selectors } =
await dataset.initializeVariable(name)
set({ selectors })
cp = centerPoint ?? variableCenterPoint
} catch (e) {
set({ error: e.message })
_unregisterLoading(name)
return
}

await dataset.updateSelection(
centerPoint ?? variableCenterPoint,
zoom,
selectors
)
await dataset.updateSelection(cp, zoom, get().selectors)
const clim = await dataset.getClim()
set({ clim: urlClim ?? clim })
_unregisterLoading(name)

get().resetMapProps(centerPoint ?? variableCenterPoint, zoom)
get().resetMapProps(cp, zoom)
},
resetMapProps: async (centerPoint, zoom) => {
const { dataset, selectors } = get()
Expand Down
39 changes: 34 additions & 5 deletions components/utils/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ export const getArrays = async (
export const getVariableLevelInfo = async (
name,
{ level, arrays, headers },
{ cfAxes, metadata }
{ cfAxes, metadata, pyramid }
) => {
const dataArray = arrays[name]
const prefix = level ? `${level}/` : ''
Expand Down Expand Up @@ -262,11 +262,40 @@ export const getVariableLevelInfo = async (
}
}, {})

const centerPoint = [
axes.X.array.data[Math.round((axes.X.array.data.length - 1) / 2)],
axes.Y.array.data[Math.round((axes.Y.array.data.length - 1) / 2)],
]

if (!pyramid) {
const unhandledCoords = [
[
axes.X.array.data[0],
axes.X.array.data[axes.X.array.data.length - 1],
].some((el) => Math.abs(el) > 360)
? cfAxes[name].X
: null,
[
axes.Y.array.data[0],
axes.Y.array.data[axes.Y.array.data.length - 1],
].some((el) => Math.abs(el) > 180)
? cfAxes[name].Y
: null,
].filter(Boolean)

if (unhandledCoords.length > 0) {
throw new Error(
`Cannot handle coordinate${
unhandledCoords.length > 1 ? 's' : ''
}: ${unhandledCoords.join(
', '
)}. Spatial coordinates must refer to latitude and longitude.`
)
}
}

return {
centerPoint: [
axes.X.array.data[Math.round((axes.X.array.data.length - 1) / 2)],
axes.Y.array.data[Math.round((axes.Y.array.data.length - 1) / 2)],
],
centerPoint,
northPole:
gridMapping &&
gridMapping.hasOwnProperty('grid_north_pole_longitude') &&
Expand Down

0 comments on commit 75e31fa

Please sign in to comment.