Skip to content

Commit

Permalink
Consolidate variable checks with cfAxes checks
Browse files Browse the repository at this point in the history
  • Loading branch information
katamartin committed Apr 26, 2024
1 parent c206859 commit 2a78b17
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
1 change: 1 addition & 0 deletions components/data/dataset.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Dataset {
this.chunks = {}
const { variables, levels } = await getVariables(
this.metadata,
this.cfAxes,
this.pyramid
)
this.variables = variables
Expand Down
8 changes: 0 additions & 8 deletions components/data/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,6 @@ const useStore = create((set, get) => ({
// default to first variable
const initialVariable = dataset.variables[0]

// if (Object.keys(cfAxes[initialVariable] ?? {}).length < 2) {
// set({
// error: 'Unable to parse coordinates. Please use CF conventions.',
// })
// _unregisterLoading('metadata')
// return
// }

get().setVariable(initialVariable, { clim })
_unregisterLoading('metadata')
},
Expand Down
24 changes: 20 additions & 4 deletions components/utils/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export const toKeyArray = (chunkKey, { chunk_separator }) => {
return chunkKey.split(chunk_separator).map(Number)
}

export const getVariables = async (metadata, pyramid) => {
export const getVariables = async (metadata, cfAxes, pyramid) => {
if (!metadata.metadata) {
throw new Error(metadata?.message || 'Unable to parse metadata')
}
Expand All @@ -106,20 +106,19 @@ export const getVariables = async (metadata, pyramid) => {
.map((k) => k.match(pyramid ? /0\/\w+(?=\/\.zarray)/ : /\w+(?=\/\.zarray)/))
.filter(Boolean)
.map((a) => a[0].replace('0/', ''))
.filter((d) => !['lat', 'lon'].includes(d))
.filter((d) => metadata.metadata[`${prefix}${d}/.zarray`].shape.length >= 2)

if (multiDimensionalVariables.length === 0) {
throw new Error('Please provide a dataset with at least 2D data arrays.')
}

const variables = multiDimensionalVariables.filter((d) =>
const variablesWithCoords = multiDimensionalVariables.filter((d) =>
metadata.metadata[`${prefix}${d}/.zattrs`]['_ARRAY_DIMENSIONS'].every(
(dim) => metadata.metadata[`${prefix}${dim}/.zarray`]
)
)

if (variables.length === 0) {
if (variablesWithCoords.length === 0) {
const missingCoordinates = multiDimensionalVariables.reduce((a, d) => {
metadata.metadata[`${prefix}${d}/.zattrs`]['_ARRAY_DIMENSIONS'].forEach(
(dim) => {
Expand All @@ -137,6 +136,23 @@ export const getVariables = async (metadata, pyramid) => {
)
}

const variables = variablesWithCoords.filter((d) => cfAxes[d])

if (variables.length === 0) {
throw new Error(
`No viewable variables found. Unable to infer spatial dimensions for ${
variablesWithCoords.size > 1 ? 'variables' : 'variable'
}: ${Array.from(variablesWithCoords)
.map(
(v) =>
`${v} (${metadata.metadata[`${prefix}${v}/.zattrs`][
'_ARRAY_DIMENSIONS'
].join(', ')})`
)
.join(', ')}.`
)
}

const levels = Object.keys(metadata.metadata)
.map((k) => k.match(new RegExp(`[0-9]+(?=\/${variables[0]}\/.zarray)`)))
.filter(Boolean)
Expand Down

0 comments on commit 2a78b17

Please sign in to comment.