Gianpaolo Coro 2014-02-26 00:02:47 +00:00
parent 57f150e546
commit eadc863c18
1 changed files with 73 additions and 31 deletions

View File

@ -1,6 +1,10 @@
package org.gcube.dataanalysis.geo.utils;
import java.util.ArrayList;
import java.util.List;
import org.gcube.contentmanagement.lexicalmatcher.utils.AnalysisLogger;
import org.gcube.dataanalysis.ecoengine.utils.Tuple;
import ucar.ma2.ArrayByte;
import ucar.ma2.ArrayDouble;
@ -8,46 +12,84 @@ import ucar.ma2.IndexIterator;
public class VectorTransformations {
public static ArrayDouble.D2 arrayByte2DArrayDouble(ArrayByte bytes){
int[] shapeD = bytes.getShape();
int yD=shapeD[0];
int xD=shapeD[1];
AnalysisLogger.getLogger().debug(xD+"X"+yD+"="+(xD*yD));
ArrayDouble.D2 doublea = new ArrayDouble.D2(yD,xD);
public static ArrayDouble.D2 arrayByte2DArrayDouble(ArrayByte bytes) {
int[] shapeD = bytes.getShape();
int yD = shapeD[0];
int xD = shapeD[1];
AnalysisLogger.getLogger().debug(xD + "X" + yD + "=" + (xD * yD));
ArrayDouble.D2 doublea = new ArrayDouble.D2(yD, xD);
IndexIterator iterator = bytes.getIndexIterator();
for (int x=0;x<xD;x++){
for (int y=0;y<yD;y++){
Byte bytex = (Byte)iterator.next();
doublea.set(y,x,bytex.doubleValue());
for (int x = 0; x < xD; x++) {
for (int y = 0; y < yD; y++) {
Byte bytex = (Byte) iterator.next();
doublea.set(y, x, bytex.doubleValue());
}
}
return doublea;
}
public static ArrayDouble.D3 arrayByte3DArrayDouble(ArrayByte bytes){
int[] shapeD = bytes.getShape();
int zD=shapeD[0];
int yD=shapeD[1];
int xD=shapeD[2];
AnalysisLogger.getLogger().debug(xD+"X"+yD+"X"+zD+"="+(xD*yD*zD));
ArrayDouble.D3 doublea = new ArrayDouble.D3(zD, yD,xD);
public static ArrayDouble.D3 arrayByte3DArrayDouble(ArrayByte bytes) {
int[] shapeD = bytes.getShape();
int zD = shapeD[0];
int yD = shapeD[1];
int xD = shapeD[2];
AnalysisLogger.getLogger().debug(xD + "X" + yD + "X" + zD + "=" + (xD * yD * zD));
ArrayDouble.D3 doublea = new ArrayDouble.D3(zD, yD, xD);
IndexIterator iterator = bytes.getIndexIterator();
for (int x=0;x<xD;x++){
for (int y=0;y<yD;y++){
for (int z=0;z<zD;z++){
Byte bytex = (Byte)iterator.next();
doublea.set(z,y,x,bytex.doubleValue());
for (int x = 0; x < xD; x++) {
for (int y = 0; y < yD; y++) {
for (int z = 0; z < zD; z++) {
Byte bytex = (Byte) iterator.next();
doublea.set(z, y, x, bytex.doubleValue());
}
}
}
return doublea;
}
/**
* Finds the best association between a grid of 3d points in a certain time instant and a set of 5 dimensional points. each tuple is expected to be formed by (x,y,z,t,value) in 5 dimensions and by (x,y,z) in 3 dimensions
**/
// suggestion: given a resolution R, give Math.sqrt(2)*R/2=0.7*R as tolerance
public static List<Double> assignPointsValuesToGrid(List<Tuple<Double>> grid3d, int gridTimeInstant, List<Tuple<Double>> coordinates5d, double tolerance) {
List<Double> valuesForGrid = new ArrayList<Double>();
int gridSize = grid3d.size();
for (int i = 0; i < gridSize; i++) {
valuesForGrid.add(Double.NaN);
}
for (Tuple<Double> coord5d : coordinates5d) {
double rx = coord5d.getElements().get(0);
double ry = coord5d.getElements().get(1);
double rz = coord5d.getElements().get(2);
double rt = coord5d.getElements().get(3);
double rvalue = coord5d.getElements().get(4);
int gridIdx = 0;
for (Tuple<Double> coord3d : grid3d) {
double x = coord3d.getElements().get(0);
double y = coord3d.getElements().get(1);
double z = coord3d.getElements().get(2);
double d = distance(x, y, z, gridTimeInstant, rx, ry, rz, rt);
if (d <= tolerance) {
// AnalysisLogger.getLogger().debug("Association: distance between grid:("+x+","+y+","+z+","+gridTimeInstant+") and point:("+rx+","+ry+","+rz+","+rt+") is "+d);
valuesForGrid.set(gridIdx, rvalue);
}
gridIdx++;
}
}
return valuesForGrid;
}
public static double distance(double x1, double y1, double z1, double t1, double x2, double y2, double z2, double t2) {
return Math.sqrt(((x1 - x2) * (x1 - x2)) + ((y1 - y2) * (y1 - y2)) + ((z1 - z2) * (z1 - z2)) + ((t1 - t2) * (t1 - t2)));
}
}