From 63c2afe5c8f9178c8078cc24f8e85a3c82d17d41 Mon Sep 17 00:00:00 2001 From: PatchesMaps Date: Tue, 6 Aug 2024 09:28:37 -0400 Subject: [PATCH] fix: await processLayer in extractConfigFromWMTS.js --- tasks/build-options/extractConfigFromWMTS.js | 4 +- tasks/build-options/processTemporalLayer.js | 170 ++++++++++--------- 2 files changed, 93 insertions(+), 81 deletions(-) diff --git a/tasks/build-options/extractConfigFromWMTS.js b/tasks/build-options/extractConfigFromWMTS.js index 2d7bdcf99c..d4525a77fd 100644 --- a/tasks/build-options/extractConfigFromWMTS.js +++ b/tasks/build-options/extractConfigFromWMTS.js @@ -139,7 +139,7 @@ async function processEntry (entry) { for (const gcLayer of gcContents.Layer) { try { layerCount += 1 - processLayer(gcLayer, wvLayers, entry) + await processLayer(gcLayer, wvLayers, entry) } catch (error) { if (error instanceof SkipException) { warningCount += 1 @@ -184,7 +184,7 @@ async function processLayer (gcLayer, wvLayers, entry) { const dimension = gcLayer.Dimension if (dimension['ows:Identifier']._text === 'Time') { try { - wvLayer = await processTemporalLayer(wvLayer, dimension.Value) + wvLayer = await processTemporalLayer(wvLayer, dimension.Value, entry.source) } catch (e) { console.error(e) console.error(`${prog}: ERROR: [${ident}] Error processing time values.`) diff --git a/tasks/build-options/processTemporalLayer.js b/tasks/build-options/processTemporalLayer.js index 3e434a552b..de8860a9f4 100644 --- a/tasks/build-options/processTemporalLayer.js +++ b/tasks/build-options/processTemporalLayer.js @@ -1,101 +1,113 @@ const moment = require('moment') const xml2js = require('xml2js') +const projDict = { + 'GIBS:geographic': 'epsg4326', + 'GIBS:arctic': 'epsg3413', + 'GIBS:antarctic': 'epsg3031' +} + function toList (val) { return val instanceof Array ? val : [val] } -async function processTemporalLayer (wvLayer, value) { +async function processTemporalLayer (wvLayer, value, source = 'GIBS:geographic') { const dateFormat = 'YYYY-MM-DD' const dateTimeFormat = 'YYYY-MM-DD HH:mm:ss' try { let ranges = toList(value) - const describeDomainsUrl = `https://gibs.earthdata.nasa.gov/wmts/epsg4326/best/1.0.0/${wvLayer.id}/default/250m/all/all.xml` - const describeDomainsResponse = await fetch(describeDomainsUrl).catch((e) => { - console.error(`Error fetching DescribeDomains for ${describeDomainsUrl}: ${e}`) - }) - if (describeDomainsResponse?.ok) { - const describeDomainsText = await describeDomainsResponse?.text?.() || '' - const parser = new xml2js.Parser() - const describeDomainsJson = await parser.parseStringPromise(describeDomainsText) - const domain = describeDomainsJson?.Domains?.DimensionDomain?.[0]?.Domain?.[0] || '' - const domains = domain.split(',').map((d) => { - return { - _text: d + const describeDomainsUrl = `https://gibs.earthdata.nasa.gov/wmts/${projDict[source]}/best/1.0.0/${wvLayer.id}/default/250m/all/all.xml` + try { + const describeDomainsResponse = await fetch(describeDomainsUrl) + if (describeDomainsResponse?.ok) { + const describeDomainsText = await describeDomainsResponse?.text?.() || '' + const parser = new xml2js.Parser() + const describeDomainsJson = await parser.parseStringPromise(describeDomainsText) + const domain = describeDomainsJson?.Domains?.DimensionDomain?.[0]?.Domain?.[0] || '' + const domains = domain.split(',') + if (domains?.length) { + const formattedDomains = domains.map((d) => { + return { + _text: d + } + }) + ranges = toList(formattedDomains) } - }) - ranges = toList(domains) - } - if (ranges && ranges[0] && ranges[0]._text && ranges[0]._text.includes('T')) { - wvLayer.period = 'subdaily' - } else { - if (ranges && ranges[0] && ranges[0]._text && ranges[0]._text.endsWith('Y')) { - wvLayer.period = 'yearly' - } else if (ranges && ranges[0] && ranges[0]._text && ranges[0]._text.endsWith('M')) { - wvLayer.period = 'monthly' - } else { - wvLayer.period = 'daily' } - } - let startDate = moment.min() - let endDate = moment.max() - const dateRangeStart = [] - const dateRangeEnd = [] - const rangeInterval = [] - for (const range of ranges) { - const [start, end, interval] = range._text.split('/') - if ( - wvLayer.period === 'daily' || - wvLayer.period === 'monthly' || - wvLayer.period === 'yearly' - ) { - startDate = moment.min(startDate, moment(start, dateFormat)) - endDate = moment.max(endDate, moment(end, dateFormat)) - if (start) { - startDate = moment(start, dateFormat).format('YYYY-MM-DDTHH:mm:ss[Z]') - dateRangeStart.push(startDate) - } - if (end) { - endDate = moment(end, dateFormat).format('YYYY-MM-DDTHH:mm:ss[Z]') - } - if (interval !== 'P1D') { - endDate = moment.utc(endDate).add(moment.duration(interval)).format('YYYY-MM-DDTHH:mm:ss[Z]') - // For monthly products subtract 1 day - if (wvLayer.period === 'monthly') { - endDate = moment.utc(endDate).subtract(1, 'day').format('YYYY-MM-DDTHH:mm:ss[Z]') - } - } - const regex = /\d+/g - const match = regex.exec(interval) - rangeInterval.push(match) - if (endDate.endsWith('T00:00:00Z')) { - endDate = endDate.replace('T00:00:00Z', 'T23:59:59Z') - } - dateRangeEnd.push(endDate) + } catch (error) { + console.error(`Error fetching ${describeDomainsUrl}: ${error}`) + } finally { + if (ranges && ranges[0] && ranges[0]._text && ranges[0]._text.includes('T')) { + wvLayer.period = 'subdaily' } else { - // Subdaily Layers - startDate = moment(start, dateTimeFormat).format('YYYY-MM-DDTHH:mm:ss[Z]') - endDate = moment(end, dateTimeFormat).format('YYYY-MM-DDTHH:mm:ss[Z]') - - if (start) { - dateRangeStart.push(startDate) + if (ranges && ranges[0] && ranges[0]._text && ranges[0]._text.endsWith('Y')) { + wvLayer.period = 'yearly' + } else if (ranges && ranges[0] && ranges[0]._text && ranges[0]._text.endsWith('M')) { + wvLayer.period = 'monthly' + } else { + wvLayer.period = 'daily' } - if (end) { + } + let startDate = moment.min() + let endDate = moment.max() + const dateRangeStart = [] + const dateRangeEnd = [] + const rangeInterval = [] + for (const range of ranges) { + const [start, end, interval] = range._text.split('/') + if ( + wvLayer.period === 'daily' || + wvLayer.period === 'monthly' || + wvLayer.period === 'yearly' + ) { + startDate = moment.min(startDate, moment(start, dateFormat)) + endDate = moment.max(endDate, moment(end, dateFormat)) + if (start) { + startDate = moment(start, dateFormat).format('YYYY-MM-DDTHH:mm:ss[Z]') + dateRangeStart.push(startDate) + } + if (end) { + endDate = moment(end, dateFormat).format('YYYY-MM-DDTHH:mm:ss[Z]') + } + if (interval !== 'P1D') { + endDate = moment.utc(endDate).add(moment.duration(interval)).format('YYYY-MM-DDTHH:mm:ss[Z]') + // For monthly products subtract 1 day + if (wvLayer.period === 'monthly') { + endDate = moment.utc(endDate).subtract(1, 'day').format('YYYY-MM-DDTHH:mm:ss[Z]') + } + } + const regex = /\d+/g + const match = regex.exec(interval) + rangeInterval.push(match) + if (endDate.endsWith('T00:00:00Z')) { + endDate = endDate.replace('T00:00:00Z', 'T23:59:59Z') + } dateRangeEnd.push(endDate) - } + } else { + // Subdaily Layers + startDate = moment(start, dateTimeFormat).format('YYYY-MM-DDTHH:mm:ss[Z]') + endDate = moment(end, dateTimeFormat).format('YYYY-MM-DDTHH:mm:ss[Z]') - rangeInterval.push(interval.match(/\d+/)[0]) - } + if (start) { + dateRangeStart.push(startDate) + } + if (end) { + dateRangeEnd.push(endDate) + } - wvLayer.startDate = dateRangeStart[0] - wvLayer.endDate = dateRangeEnd[dateRangeEnd.length - 1] + rangeInterval.push(interval.match(/\d+/)[0]) + } - if (dateRangeStart.length && dateRangeEnd.length) { - wvLayer.dateRanges = dateRangeStart.map((s, i) => ({ - startDate: s, - endDate: dateRangeEnd[i], - dateInterval: rangeInterval[i] - })) + wvLayer.startDate = dateRangeStart[0] + wvLayer.endDate = dateRangeEnd[dateRangeEnd.length - 1] + + if (dateRangeStart.length && dateRangeEnd.length) { + wvLayer.dateRanges = dateRangeStart.map((s, i) => ({ + startDate: s, + endDate: dateRangeEnd[i], + dateInterval: rangeInterval[i] + })) + } } } } catch (e) {