Skip to content

Commit

Permalink
Return error if bands don't exist #31
Browse files Browse the repository at this point in the history
  • Loading branch information
m-mohr committed Jan 23, 2024
1 parent b241eff commit 128c4f5
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 107 deletions.
13 changes: 2 additions & 11 deletions src/processes/aggregate_temporal_frequency.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,19 @@
import GeeProcess from '../processgraph/process.js';
import Commons from '../processgraph/commons.js';
import Errors from '../utils/errors.js';

export default class aggregate_temporal_frequency extends GeeProcess {

reduce(node, imageCollection) {
const callback = node.getCallback('reducer');
if (callback.getNodeCount() !== 1) {
throw new Errors.ProcessArgumentInvalid({
process: this.id,
argument: 'reducer',
reason: "No complex reducer supported at the moment"
});
throw node.invalidArgument('reducer', 'No complex reducer supported at the moment');
}
else {
// This is a simple reducer with just one node
const childNode = callback.getResultNode();
const process = callback.getProcess(childNode);
if (typeof process.geeReducer !== 'function') {
throw new Errors.ProcessArgumentInvalid({
process: this.id,
argument: 'reducer',
reason: 'The specified reducer is invalid.'
});
throw node.invalidArgument('reducer', 'The specified reducer is invalid.');
}
node.debug("Bypassing node " + childNode.id + "; Executing as native GEE reducer instead.");
const reducerFunc = process.geeReducer(node);
Expand Down
6 changes: 1 addition & 5 deletions src/processes/array_element.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,7 @@ export default class array_element extends GeeProcess {
else {
// ToDo processes: only bands is currently supported
if (dimension.type !== "bands") {
throw new Errors.ProcessArgumentInvalid({
process: this.id,
argument: 'dimension',
reason: 'Only dimension "bands" is currently supported.'
});
throw node.invalidArgument('dimension', 'Only dimension "bands" is currently supported.');
}
else {
index = labels.indexOf(label);
Expand Down
6 changes: 1 addition & 5 deletions src/processes/filter_bands.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,10 @@ import Commons from '../processgraph/commons.js';

export default class filter_bands extends GeeProcess {

process(data, bands, node) {
return Commons.filterBands(data, bands, node);
}

executeSync(node) {
const dc = node.getDataCube('data');
const bands = node.getArgument('bands');
return this.process(dc, bands, node);
return Commons.filterBands(dc, bands, node);
}

}
3 changes: 1 addition & 2 deletions src/processes/first.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import GeeProcess from '../processgraph/process.js';
import Errors from '../utils/errors.js';

export default class first extends GeeProcess {

Expand All @@ -21,7 +20,7 @@ export default class first extends GeeProcess {
return data.first();
}
else {
throw new Errors.ProcessArgumentInvalid();
throw node.invalidArgument('data', 'Unsupported datatype');
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/processes/last.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import GeeProcess from '../processgraph/process.js';
import Errors from '../utils/errors.js';

export default class last extends GeeProcess {

Expand All @@ -23,7 +22,7 @@ export default class last extends GeeProcess {
return data.toList(data.size()).get(-1);
}
else {
throw new Errors.ProcessArgumentInvalid();
throw node.invalidArgument('data', 'Unsupported datatype');
}
}

Expand Down
13 changes: 2 additions & 11 deletions src/processes/rename_labels.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import GeeProcess from '../processgraph/process.js';
import Errors from '../utils/errors.js';

export default class rename_labels extends GeeProcess {

Expand All @@ -17,21 +16,13 @@ export default class rename_labels extends GeeProcess {
const source = node.getArgument("source");

if (!dc.hasDimension(dimensionName)) {
throw new Errors.ProcessArgumentInvalid({
process: this.id,
argument: 'dimension',
reason: 'Dimension "' + dimensionName + '" does not exist.'
});
throw node.invalidArgument('dimension', `Dimension '${dimensionName}' does not exist.`);
}

// ToDo processes: only bands is currently supported
const dimension = dc.getDimension(dimensionName);
if (dimension.type !== "bands") {
throw new Errors.ProcessArgumentInvalid({
process: this.id,
argument: 'dimension',
reason: 'Only dimension "bands" is currently supported.'
});
throw node.invalidArgument('dimension', `Only dimension "bands" is currently supported.`);
}
// ToDo processes: Number values for the labels arguments causes problems
dc.renameLabels(dimension, target, source);
Expand Down
91 changes: 25 additions & 66 deletions src/processgraph/commons.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@ export default class Commons {
const dimensionName = node.getArgument(dimensionArgName);
const dimension = dc.getDimension(dimensionName);
if (!allowedDimensionTypes.includes(dimension.type)) {
throw new Errors.ProcessArgumentInvalid({
process: process_id,
argument: dimensionArgName,
reason: 'Reducing dimension types other than ' + allowedDimensionTypes.join(' or ') + ' is currently not supported.'
});
throw node.invalidArgument(dimensionArgName, `Reducing dimension types other than ${allowedDimensionTypes.join(' or ')} is currently not supported.`);
}

const callback = node.getCallback(reducerArgName);
Expand All @@ -25,11 +21,7 @@ export default class Commons {
const childNode = callback.getResultNode();
const process = callback.getProcess(childNode);
if (typeof process.geeReducer !== 'function') {
throw new Errors.ProcessArgumentInvalid({
process: process_id,
argument: reducerArgName,
reason: 'The specified ' + reducerArgName + ' is invalid.'
});
throw node.invalidArgument(reducerArgName, 'The specified reducer is invalid.');
}
node.debug("Bypassing node " + childNode.id + "; Executing as native GEE reducer instead.");
dc = Commons.reduceSimple(dc, process.geeReducer(node));
Expand Down Expand Up @@ -108,18 +100,10 @@ export default class Commons {
const arg1 = node.getArgument(arg1Name);
const arg2 = node.getArgument(arg2Name);
if (typeof arg1 === 'undefined') {
throw new Errors.ProcessArgumentInvalid({
process: node.process_id,
argument: arg1Name,
reason: "Argument is undefined."
});
throw node.argumentInvalid(arg1Name, "Argument is undefined.");
}
if (typeof arg2 === 'undefined') {
throw new Errors.ProcessArgumentInvalid({
process: node.process_id,
argument: arg2Name,
reason: "Argument is undefined."
});
throw node.argumentInvalid(arg2Name, "Argument is undefined.");
}

return this._reduceBinary(node, imgReducer, jsReducer, arg1, arg2, arg1Name + "/" + arg2Name);
Expand All @@ -128,11 +112,7 @@ export default class Commons {
static reduceInCallback(node, imgReducer, jsReducer, dataArg = "data") {
const list = node.getArgument(dataArg);
if (!Array.isArray(list) || list.length <= 1) {
throw new Errors.ProcessArgumentInvalid({
process: node.process_id,
argument: dataArg,
reason: "Not enough elements."
});
throw node.invalidArgument(dataArg, "Argument must be an array with at least two elements.");
}

let result;
Expand Down Expand Up @@ -178,11 +158,7 @@ export default class Commons {
result = dataCubeB.imageCollection(ic => ic.map(imgB => imgReducer(imgA, imgB)));
}
else {
throw new Errors.ProcessArgumentInvalid({
process: node.process_id,
argument: dataArg,
reason: "Reducing number with unknown type not supported"
});
throw node.invalidArgument(dataArg, "Reducing number with unknown type not supported");
}
}
else if (dataCubeA.isImageCollection()) {
Expand All @@ -202,11 +178,7 @@ export default class Commons {
});
}
else {
throw new Errors.ProcessArgumentInvalid({
process: node.process_id,
argument: dataArg,
reason: "Reducing image collection with unknown type not supported"
});
throw node.invalidArgument(dataArg, "Reducing image collection with unknown type not supported");
}
}
else if (dataCubeA.isImage()) {
Expand All @@ -218,19 +190,11 @@ export default class Commons {
result = dataCubeB.imageCollection(ic => ic.map(imgB => imgReducer(dataCubeA.image(), imgB)));
}
else {
throw new Errors.ProcessArgumentInvalid({
process: node.process_id,
argument: dataArg,
reason: "Reducing image with unknown type not supported"
});
throw node.invalidArgument(dataArg, "Reducing image with unknown type not supported");
}
}
else {
throw new Errors.ProcessArgumentInvalid({
process: node.process_id,
argument: dataArg,
reason: "Reducing an unknown type is not supported"
});
throw node.invalidArgument(dataArg, "Reducing unknown type not supported");
}
return result;
}
Expand Down Expand Up @@ -269,31 +233,30 @@ export default class Commons {
dc.setSpatialExtent(bbox);
return Commons.restrictToSpatialExtent(node, dc);
} catch (e) {
throw new Errors.ProcessArgumentInvalid({
process: process_id,
argument: paramName,
reason: e.message
});
throw node.invalidArgument(paramName, e.message);
}
}

static filterBands(dc, bands, node) {
static filterBands(dc, bands, node, parameterName = 'bands') {
const dc_bands = dc.getBands();
const col_id = dc.getCollectionId();
const col_meta = node.getContext().getCollection(col_id);
const eo_bands = Array.isArray(col_meta.summaries["eo:bands"]) ? col_meta.summaries["eo:bands"] : [];

const band_list = [];
for(const b of bands) {
if (dc_bands.indexOf(b) > -1) {
band_list.push(b)
for(const name of bands) {
if (dc_bands.indexOf(name) > -1) {
band_list.push(name);
continue;
}
else {
for (const eob of col_meta.summaries["eo:bands"]){
if (b === eob["common_name"]) {
band_list.push(eob["name"]);
break;
}
}

const match = eo_bands.find(eob => name === eob.common_name && typeof eob.name === 'string');
if (match) {
band_list.push(match.name);
continue;
}

throw node.invalidArgument(parameterName, `Band with name or common name '${name}' not found in data cube.`);
}
dc.imageCollection(ic => ic.select(band_list));
dc.dimBands().setValues(band_list);
Expand All @@ -308,11 +271,7 @@ export default class Commons {
dc.imageCollection(ic => ic.map(img => img.clip(geom)));
return dc;
} catch (e) {
throw new Errors.ProcessArgumentInvalid({
process: process_id,
argument: paramName,
reason: e.message
});
throw node.invalidArgument(paramName, e.message);
}
}

Expand Down
1 change: 1 addition & 0 deletions src/processgraph/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ export default class ProcessingContext {
throw new Errors.ProcessArgumentInvalid({
argument: "options",
process: "save_result",
namespace: "backend",
reason: "The output band definitions are not properly given."
});
}
Expand Down
15 changes: 10 additions & 5 deletions src/processgraph/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,18 @@ export default class GeeProcessGraphNode extends ProcessGraphNode {
getCallback(name) {
const callback = this.getArgument(name);
if (!(callback instanceof ProcessGraph)) {
throw new Errors.ProcessArgumentInvalid({
process: this.process_id,
argument: 'process',
reason: 'No process specified.'
});
throw this.invalidArgument('process', 'No process specified.');
}
return callback;
}

invalidArgument(argument, reason) {
return new Errors.ProcessArgumentInvalid({
process: this.process_id,
namespace: this.namespace,
argument,
reason
});
}

}
1 change: 1 addition & 0 deletions src/processgraph/process.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { BaseProcess } from '@openeo/js-processgraphs';
import Utils from '../utils/utils.js';
import Errors from '../utils/errors.js';

Check failure on line 3 in src/processgraph/process.js

View workflow job for this annotation

GitHub Actions / deploy (lts/*)

'Errors' is defined but never used

export default class GeeProcess extends BaseProcess {

Expand Down

0 comments on commit 128c4f5

Please sign in to comment.