Skip to content

Commit

Permalink
add date_difference
Browse files Browse the repository at this point in the history
  • Loading branch information
jdries committed Sep 25, 2023
1 parent 44ded5f commit fe78f49
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import org.slf4j.LoggerFactory
import spire.math.UShort
import spire.syntax.cfor.cfor

import java.time.{Duration, ZonedDateTime}
import java.util
import scala.Double.NaN
import scala.collection.JavaConversions.mapAsScalaMap
Expand Down Expand Up @@ -545,6 +546,28 @@ class OpenEOProcessScriptBuilder {
ifElseProcess(value, accept, reject)
}

private def dateDifferenceProcess(arguments: java.util.Map[String, Object]): OpenEOProcess = {
val date1 = arguments.get("date1")
val date2 = arguments.get("date2")


val dateDiffProcess = (context: Map[String, Any]) => {
def normalize(argument: Any): String = {
if( argument.isInstanceOf[String]) {
argument.asInstanceOf[String]
}else if(argument.isInstanceOf[util.Map[String,Any]] && argument.asInstanceOf[util.Map[String,Any]].containsKey("from_parameter")) {
val paramName = argument.asInstanceOf[util.Map[String,Any]].get("from_parameter").asInstanceOf[String]
context.getOrElse(paramName, throw new IllegalArgumentException(s"date_difference: Parameter $paramName not found in context: $context")).asInstanceOf[String]
}else{
throw new IllegalArgumentException(s"date_difference got unexpected argument: $argument")
}
}
val diff = Duration.between(ZonedDateTime.parse(normalize(date1)),ZonedDateTime.parse(normalize(date2))).toDays
createConstantTileFunction(diff)
}
dateDiffProcess
}


private def xyFunction(operator:(Tile,Tile) => Tile, xArgName:String = "x", yArgName:String = "y" ,convertBitCells: Boolean = true): OpenEOProcess = {
val x_function: OpenEOProcess = getProcessArg(xArgName)
Expand Down Expand Up @@ -602,7 +625,11 @@ class OpenEOProcessScriptBuilder {
val defaultName = defaultDataParameterName
inputFunction = (context:Map[String,Any]) => (tiles: Seq[Tile]) => {
if(context.contains(parameterName)) {
context.getOrElse(parameterName,tiles).asInstanceOf[Seq[Tile]]
if(context.get(parameterName).isInstanceOf[Seq[Tile]]) {
context.getOrElse(parameterName,tiles).asInstanceOf[Seq[Tile]]
}else{
null
}
}else if(parameterName == defaultName) {
tiles
}
Expand Down Expand Up @@ -708,6 +735,7 @@ class OpenEOProcessScriptBuilder {
val hasConditionExpression = arguments.get("condition") != null && !arguments.get("condition").isInstanceOf[Boolean]

val operation: OpenEOProcess = operator match {
case "date_difference" => dateDifferenceProcess(arguments)
case "if" => ifProcess(arguments)
// Comparison operators
case "gt" if hasXY => xyFunction(Greater.apply,convertBitCells = false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1657,6 +1657,42 @@ static OpenEOProcessScriptBuilder createArrayApply() {
return builder;
}

static OpenEOProcessScriptBuilder createArrayApplyDateDifference() {
OpenEOProcessScriptBuilder builder = new OpenEOProcessScriptBuilder();

Map<String, Object> arguments = new HashMap<>();
arguments.put("data", Collections.singletonMap("from_parameter", "x"));
arguments.put("process", "dummy");
builder.expressionStart("array_apply", arguments);

builder.argumentStart("data");
builder.fromParameter("data");
builder.argumentEnd();

builder.argumentStart("process");
{
// scope block, just for nice indentation
Map<String, Object> argumentsCos = new HashMap<>();
argumentsCos.put("date1", Collections.singletonMap("from_parameter", "label"));
argumentsCos.put("date2", "2022-01-01T00:00:00Z");
builder.expressionStart("date_difference", argumentsCos);

builder.argumentStart("date1");
builder.fromParameter("label");
builder.argumentEnd();
builder.argumentStart("date2");
//string constant is not yet transmitted
builder.argumentEnd();


builder.expressionEnd("date_difference", argumentsCos);
}
builder.argumentEnd();

builder.expressionEnd("array_apply", arguments);
return builder;
}

static OpenEOProcessScriptBuilder createRankComposite() {
OpenEOProcessScriptBuilder builder = new OpenEOProcessScriptBuilder();

Expand Down Expand Up @@ -1719,6 +1755,26 @@ public void testArrayApply() {
}
}

@DisplayName("Test array_apply with date difference process")
@Test
public void testArrayApplyDateDifference() {
for (OpenEOProcessScriptBuilder builder : Arrays.asList(createArrayApplyDateDifference())) {
Function1<Seq<Tile>, Seq<Tile>> transformation = builder.generateFunction(Collections.singletonMap("array_labels",JavaConversions.asScalaBuffer(Arrays.asList("2022-01-04T00:00:00Z","2022-01-05T00:00:00Z","2016-02-29T00:00:00Z","2019-06-30T00:00:00Z","2030-12-31T00:00:00Z"))));
Tile tile0 = FloatConstantNoDataArrayTile.fill(1, 4, 4);
Tile tile1 = FloatConstantNoDataArrayTile.fill(3, 4, 4);
Tile tile2 = FloatConstantNoDataArrayTile.fill(-1, 4, 4);
Tile tile3 = FloatConstantNoDataArrayTile.fill(1.9f, 4, 4);
Tile nodataTile = ByteConstantNoDataArrayTile.empty(4, 4);

Seq<Tile> result = transformation.apply(JavaConversions.asScalaBuffer(Arrays.asList(nodataTile, tile0, tile1, tile2, tile3)));

assertEquals(-3, result.apply(0).get(0, 0));
assertEquals(-4, result.apply(1).get(0, 0));
assertEquals(2133, result.apply(2).get(0, 0));

}
}

static OpenEOProcessScriptBuilder createMedian(Boolean ignoreNoData) {
OpenEOProcessScriptBuilder builder = new OpenEOProcessScriptBuilder();
Map<String, Object> arguments = ignoreNoData!=null? Collections.singletonMap("ignore_nodata",ignoreNoData.booleanValue()) : Collections.emptyMap();
Expand Down

0 comments on commit fe78f49

Please sign in to comment.