use of com.google.datastore.admin.v1.Index in project imageio-ext by geosolutions-it.
the class NetCDFCF_CLewis_Converter method Resampler.
private WritableRaster Resampler(final Array latData, final Array lonData, final int imageWidth, final int imageHeight, final int polyDegree, final Array data, final float fillValue) {
final Index latIndex = latData.getIndex();
final Index lonIndex = lonData.getIndex();
final int numCoeffs = (polyDegree + 1) * (polyDegree + 2) / 2;
final int XOFFSET = 0;
final int YOFFSET = 1;
final int stepX = 2;
final int stepY = 2;
int numNeededPoints = 0;
for (int xi = 0; xi < imageWidth; xi += stepX) {
for (int yi = 0; yi < imageHeight; yi += stepY) {
numNeededPoints++;
}
}
computeMatrixExtremes(latData, lonData, imageWidth, imageHeight, latIndex, lonIndex);
float[] destCoords = new float[2 * numNeededPoints];
float[] srcCoords = new float[2 * numNeededPoints];
/*
* Copy source and destination coordinates into float arrays. The
* destination coordinates are scaled in order to gets values similar to
* source coordinates (values will be identical if all "real world"
* coordinates are grid indices multiplied by a constant).
*/
int offset = 0;
for (int yi = 0; yi < imageHeight; yi += stepY) {
for (int xi = 0; xi < imageWidth; xi += stepX) {
srcCoords[offset] = xi;
srcCoords[offset + 1] = yi;
destCoords[offset] = (float) ((lonData.getFloat(lonIndex.set(xi)) - this.xmin) / this.periodX);
destCoords[offset + 1] = (float) ((this.ymax - latData.getFloat(latIndex.set(yi))) / this.periodY);
// destCoords[offset + 1] = ((latData.getFloat(latIndex.set(yi)) - this.ymin) / this.periodY);
offset += 2;
}
}
GMatrix A = new GMatrix(numNeededPoints, numCoeffs);
for (int coord = 0; coord < numNeededPoints; coord++) {
int var = 0;
for (int i = 0; i <= polyDegree; i++) {
for (int j = 0; j <= i; j++) {
double value = Math.pow(destCoords[2 * coord + XOFFSET], (double) (i - j)) * Math.pow(destCoords[2 * coord + YOFFSET], (double) j);
A.setElement(coord, var++, value);
}
}
}
GMatrix AtAi = new GMatrix(numCoeffs, numCoeffs);
GMatrix Ap = new GMatrix(numCoeffs, numNeededPoints);
AtAi.mulTransposeLeft(A, A);
AtAi.invert();
Ap.mulTransposeRight(AtAi, A);
GMatrix xVector = new GMatrix(numNeededPoints, 1);
GMatrix yVector = new GMatrix(numNeededPoints, 1);
for (int idx = 0; idx < numNeededPoints; idx++) {
xVector.setElement(idx, 0, srcCoords[2 * idx + XOFFSET]);
yVector.setElement(idx, 0, srcCoords[2 * idx + YOFFSET]);
}
GMatrix xCoeffsG = new GMatrix(numCoeffs, 1);
GMatrix yCoeffsG = new GMatrix(numCoeffs, 1);
xCoeffsG.mul(Ap, xVector);
yCoeffsG.mul(Ap, yVector);
float[] xCoeffs = new float[numCoeffs];
float[] yCoeffs = new float[numCoeffs];
for (int ii = 0; ii < numCoeffs; ii++) {
xCoeffs[ii] = new Double(xCoeffsG.getElement(ii, 0)).floatValue();
yCoeffs[ii] = new Double(yCoeffsG.getElement(ii, 0)).floatValue();
}
WritableRaster outDataCube;
WritableRandomIter iteratorDataCube;
SampleModel outSampleModel = RasterFactory.createBandedSampleModel(// data type
DataBuffer.TYPE_FLOAT, // width
imageWidth, // height
imageHeight, // num bands
1);
outDataCube = Raster.createWritableRaster(outSampleModel, null);
iteratorDataCube = RandomIterFactory.createWritable(outDataCube, null);
// Transfering data in the WritableRaster structure
Index indexInputVar = data.getIndex();
for (int jj = 0; jj < outDataCube.getNumBands(); jj++) {
for (int kk = 0; kk < outDataCube.getWidth(); kk++) {
for (int ll = 0; ll < outDataCube.getHeight(); ll++) {
iteratorDataCube.setSample(kk, ll, jj, data.getFloat(indexInputVar.set(ll, kk)));
}
}
}
WritableRaster target = RasterFactory.createWritableRaster(outSampleModel, null);
for (int bi = 0; bi < outDataCube.getNumBands(); bi++) {
for (int yi = 0; yi < imageHeight; yi++) {
for (int xi = 0; xi < imageWidth; xi++) {
float[] dstCoords = new float[2];
GMatrix regressionVec = new GMatrix(numCoeffs, 1);
int var = 0;
for (int i = 0; i <= polyDegree; i++) {
for (int j = 0; j <= i; j++) {
double value = Math.pow(xi, (double) (i - j)) * Math.pow(yi, (double) j);
regressionVec.setElement(var++, 0, value);
}
}
GMatrix xG = new GMatrix(1, 1);
GMatrix yG = new GMatrix(1, 1);
xG.mulTransposeLeft(regressionVec, xCoeffsG);
yG.mulTransposeLeft(regressionVec, yCoeffsG);
int X = (int) Math.round(xG.getElement(0, 0));
int Y = (int) Math.round(yG.getElement(0, 0));
if (X >= 0 && Y >= 0 && X < imageWidth && Y < imageHeight) {
target.setSample(xi, yi, bi, outDataCube.getSampleFloat(X, Y, bi));
} else {
// TODO: Change with fillvalue
// target.setSample(xi, yi, bi, Float.NaN);
target.setSample(xi, yi, bi, fillValue);
}
}
}
}
return target;
}
use of com.google.datastore.admin.v1.Index in project imageio-ext by geosolutions-it.
the class NetCDFConverterUtilities method getRangeArray.
public static Array getRangeArray(DataType varDataType) {
int[] dim = new int[] { 2 };
if (varDataType == DataType.FLOAT) {
Array array = new ArrayFloat(dim);
Index index = array.getIndex();
array.setFloat(index.set(0), Float.MIN_VALUE);
array.setFloat(index.set(1), Float.MAX_VALUE);
return array;
} else if (varDataType == DataType.DOUBLE) {
Array array = new ArrayDouble(dim);
Index index = array.getIndex();
array.setDouble(index.set(0), Double.MIN_VALUE);
array.setDouble(index.set(1), Double.MAX_VALUE);
return array;
} else if (varDataType == DataType.BYTE) {
Array array = new ArrayByte(dim);
Index index = array.getIndex();
array.setByte(index.set(0), Byte.MIN_VALUE);
array.setByte(index.set(1), Byte.MAX_VALUE);
return array;
} else if (varDataType == DataType.SHORT) {
Array array = new ArrayShort(dim);
Index index = array.getIndex();
array.setShort(index.set(0), Short.MIN_VALUE);
array.setShort(index.set(1), Short.MAX_VALUE);
return array;
} else if (varDataType == DataType.INT) {
Array array = new ArrayInt(dim);
Index index = array.getIndex();
array.setInt(index.set(0), Integer.MIN_VALUE);
array.setInt(index.set(1), Integer.MAX_VALUE);
return array;
}
throw new IllegalArgumentException("Actually unsupported Datatype");
}
use of com.google.datastore.admin.v1.Index in project imageio-ext by geosolutions-it.
the class NetCDFConverterUtilities method writeData.
public static void writeData(NetcdfFileWriteable ncFileOut, final String varName, Variable var, final Array originalVarData, final Array destArray, final boolean findNewRange, final boolean updateFillValue, final int[] loopLengths, final boolean flipY) throws IOException, InvalidRangeException {
final int nestedLoops = loopLengths.length;
final boolean setDepth = nestedLoops > 3;
// timeDim
final int timePositions = loopLengths[0];
final int depthPositions;
final int latPositions;
final int lonPositions;
if (setDepth) {
depthPositions = loopLengths[1];
latPositions = loopLengths[2];
lonPositions = loopLengths[3];
} else {
depthPositions = -1;
latPositions = loopLengths[1];
lonPositions = loopLengths[2];
}
final DataType varDataType = var.getDataType();
Attribute fv = null;
if (updateFillValue)
fv = var.findAttribute(NetCDFUtilities.DatasetAttribs.MISSING_VALUE);
else
fv = var.findAttribute(NetCDFUtilities.DatasetAttribs.FILL_VALUE);
Index varIndex = originalVarData.getIndex();
Index destIndex = destArray.getIndex();
// //
if (varDataType == DataType.FLOAT) {
float min = Float.MAX_VALUE;
float max = Float.MIN_VALUE;
float fillValue = Float.MAX_VALUE;
if (fv != null) {
fillValue = (fv.getNumericValue()).floatValue();
}
if (setDepth) {
for (int tPos = 0; tPos < timePositions; tPos++) {
for (int levelPos = 0; levelPos < depthPositions; levelPos++) {
for (int yPos = 0; yPos < latPositions; yPos++) {
for (int xPos = 0; xPos < lonPositions; xPos++) {
float sVal = originalVarData.getFloat(varIndex.set(tPos, levelPos, yPos, xPos));
if (findNewRange) {
if (sVal >= max && sVal != fillValue)
max = sVal;
if (sVal <= min && sVal != fillValue)
min = sVal;
}
int newYpos = yPos;
// Flipping y
if (flipY) {
newYpos = latPositions - yPos - 1;
}
destArray.setFloat(destIndex.set(tPos, levelPos, newYpos, xPos), sVal);
}
}
}
}
} else {
for (int tPos = 0; tPos < timePositions; tPos++) {
for (int yPos = 0; yPos < latPositions; yPos++) {
for (int xPos = 0; xPos < lonPositions; xPos++) {
float sVal = originalVarData.getFloat(varIndex.set(tPos, yPos, xPos));
if (findNewRange) {
if (sVal >= max && sVal != fillValue)
max = sVal;
if (sVal <= min && sVal != fillValue)
min = sVal;
}
// Flipping y
int newYpos = yPos;
// Flipping y
if (flipY) {
newYpos = latPositions - yPos - 1;
}
destArray.setFloat(destIndex.set(tPos, newYpos, xPos), sVal);
}
}
}
}
ncFileOut.write(varName, destArray);
if (findNewRange) {
Array range = NetCDFConverterUtilities.getRangeArray(varDataType);
Index index = range.getIndex();
range.setFloat(index.set(0), min);
range.setFloat(index.set(1), max);
ncFileOut.updateAttribute(ncFileOut.findVariable(varName), new Attribute(NetCDFUtilities.DatasetAttribs.VALID_RANGE, range));
}
if (updateFillValue) {
ncFileOut.updateAttribute(ncFileOut.findVariable(varName), new Attribute(NetCDFUtilities.DatasetAttribs.FILL_VALUE, new Float(fillValue)));
}
// //
//
// DOUBLE
//
// //
} else if (varDataType == DataType.DOUBLE) {
double min = Double.MAX_VALUE;
double max = Double.MIN_VALUE;
double fillValue = Double.MAX_VALUE;
if (fv != null) {
fillValue = (fv.getNumericValue()).doubleValue();
}
if (setDepth) {
for (int tPos = 0; tPos < timePositions; tPos++) {
for (int levelPos = 0; levelPos < depthPositions; levelPos++) {
for (int yPos = 0; yPos < latPositions; yPos++) {
for (int xPos = 0; xPos < lonPositions; xPos++) {
double sVal = originalVarData.getDouble(varIndex.set(tPos, levelPos, yPos, xPos));
if (findNewRange) {
if (sVal >= max && sVal != fillValue)
max = sVal;
if (sVal <= min && sVal != fillValue)
min = sVal;
}
int newYpos = yPos;
// Flipping y
if (flipY) {
newYpos = latPositions - yPos - 1;
}
destArray.setDouble(destIndex.set(tPos, levelPos, newYpos, xPos), sVal);
}
}
}
}
} else {
for (int tPos = 0; tPos < timePositions; tPos++) {
for (int yPos = 0; yPos < latPositions; yPos++) {
for (int xPos = 0; xPos < lonPositions; xPos++) {
double sVal = originalVarData.getDouble(varIndex.set(tPos, yPos, xPos));
if (findNewRange) {
if (sVal >= max && sVal != fillValue)
max = sVal;
if (sVal <= min && sVal != fillValue)
min = sVal;
}
// Flipping y
int newYpos = yPos;
// Flipping y
if (flipY) {
newYpos = latPositions - yPos - 1;
}
destArray.setDouble(destIndex.set(tPos, newYpos, xPos), sVal);
}
}
}
}
ncFileOut.write(varName, destArray);
if (findNewRange) {
Array range = NetCDFConverterUtilities.getRangeArray(varDataType);
Index index = range.getIndex();
range.setDouble(index.set(0), min);
range.setDouble(index.set(1), max);
ncFileOut.updateAttribute(ncFileOut.findVariable(varName), new Attribute(NetCDFUtilities.DatasetAttribs.VALID_RANGE, range));
}
if (updateFillValue) {
ncFileOut.updateAttribute(ncFileOut.findVariable(varName), new Attribute(NetCDFUtilities.DatasetAttribs.FILL_VALUE, new Double(fillValue)));
}
// //
//
// BYTE
//
// //
} else if (varDataType == DataType.BYTE) {
byte min = Byte.MAX_VALUE;
byte max = Byte.MIN_VALUE;
byte fillValue = Byte.MAX_VALUE;
if (fv != null) {
fillValue = (fv.getNumericValue()).byteValue();
}
if (setDepth) {
for (int tPos = 0; tPos < timePositions; tPos++) {
for (int levelPos = 0; levelPos < depthPositions; levelPos++) {
for (int yPos = 0; yPos < latPositions; yPos++) {
for (int xPos = 0; xPos < lonPositions; xPos++) {
byte sVal = originalVarData.getByte(varIndex.set(tPos, levelPos, yPos, xPos));
if (findNewRange) {
if (sVal >= max && sVal != fillValue)
max = sVal;
if (sVal <= min && sVal != fillValue)
min = sVal;
}
int newYpos = yPos;
// Flipping y
if (flipY) {
newYpos = latPositions - yPos - 1;
}
destArray.setByte(destIndex.set(tPos, levelPos, newYpos, xPos), sVal);
}
}
}
}
} else {
for (int tPos = 0; tPos < timePositions; tPos++) {
for (int yPos = 0; yPos < latPositions; yPos++) {
for (int xPos = 0; xPos < lonPositions; xPos++) {
byte sVal = originalVarData.getByte(varIndex.set(tPos, yPos, xPos));
if (findNewRange) {
if (sVal >= max && sVal != fillValue)
max = sVal;
if (sVal <= min && sVal != fillValue)
min = sVal;
}
// Flipping y
int newYpos = yPos;
// Flipping y
if (flipY) {
newYpos = latPositions - yPos - 1;
}
destArray.setByte(destIndex.set(tPos, newYpos, xPos), sVal);
}
}
}
}
ncFileOut.write(varName, destArray);
if (findNewRange) {
Array range = NetCDFConverterUtilities.getRangeArray(varDataType);
Index index = range.getIndex();
range.setByte(index.set(0), min);
range.setByte(index.set(1), max);
ncFileOut.updateAttribute(ncFileOut.findVariable(varName), new Attribute(NetCDFUtilities.DatasetAttribs.VALID_RANGE, range));
}
if (updateFillValue) {
ncFileOut.updateAttribute(ncFileOut.findVariable(varName), new Attribute(NetCDFUtilities.DatasetAttribs.FILL_VALUE, new Byte(fillValue)));
}
// //
//
// SHORT
//
// //
} else if (varDataType == DataType.SHORT) {
short min = Short.MAX_VALUE;
short max = Short.MIN_VALUE;
short fillValue = Short.MAX_VALUE;
if (fv != null) {
fillValue = (fv.getNumericValue()).shortValue();
}
if (setDepth) {
for (int tPos = 0; tPos < timePositions; tPos++) {
for (int levelPos = 0; levelPos < depthPositions; levelPos++) {
for (int yPos = 0; yPos < latPositions; yPos++) {
for (int xPos = 0; xPos < lonPositions; xPos++) {
short sVal = originalVarData.getShort(varIndex.set(tPos, levelPos, yPos, xPos));
if (findNewRange) {
if (sVal >= max && sVal != fillValue)
max = sVal;
if (sVal <= min && sVal != fillValue)
min = sVal;
}
int newYpos = yPos;
// Flipping y
if (flipY) {
newYpos = latPositions - yPos - 1;
}
destArray.setShort(destIndex.set(tPos, levelPos, newYpos, xPos), sVal);
}
}
}
}
} else {
for (int tPos = 0; tPos < timePositions; tPos++) {
for (int yPos = 0; yPos < latPositions; yPos++) {
for (int xPos = 0; xPos < lonPositions; xPos++) {
short sVal = originalVarData.getShort(varIndex.set(tPos, yPos, xPos));
if (findNewRange) {
if (sVal >= max && sVal != fillValue)
max = sVal;
if (sVal <= min && sVal != fillValue)
min = sVal;
}
// Flipping y
int newYpos = yPos;
// Flipping y
if (flipY) {
newYpos = latPositions - yPos - 1;
}
destArray.setShort(destIndex.set(tPos, newYpos, xPos), sVal);
}
}
}
}
ncFileOut.write(varName, destArray);
if (findNewRange) {
Array range = NetCDFConverterUtilities.getRangeArray(varDataType);
Index index = range.getIndex();
range.setShort(index.set(0), min);
range.setShort(index.set(1), max);
ncFileOut.updateAttribute(ncFileOut.findVariable(varName), new Attribute(NetCDFUtilities.DatasetAttribs.VALID_RANGE, range));
}
if (updateFillValue) {
ncFileOut.updateAttribute(ncFileOut.findVariable(varName), new Attribute(NetCDFUtilities.DatasetAttribs.FILL_VALUE, new Short(fillValue)));
}
} else // //
if (varDataType == DataType.INT) {
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
int fillValue = Integer.MAX_VALUE;
if (fv != null) {
fillValue = (fv.getNumericValue()).intValue();
}
if (setDepth) {
for (int tPos = 0; tPos < timePositions; tPos++) {
for (int levelPos = 0; levelPos < depthPositions; levelPos++) {
for (int yPos = 0; yPos < latPositions; yPos++) {
for (int xPos = 0; xPos < lonPositions; xPos++) {
int sVal = originalVarData.getInt(varIndex.set(tPos, levelPos, yPos, xPos));
if (findNewRange) {
if (sVal >= max && sVal != fillValue)
max = sVal;
if (sVal <= min && sVal != fillValue)
min = sVal;
}
int newYpos = yPos;
// Flipping y
if (flipY) {
newYpos = latPositions - yPos - 1;
}
destArray.setInt(destIndex.set(tPos, levelPos, newYpos, xPos), sVal);
}
}
}
}
} else {
for (int tPos = 0; tPos < timePositions; tPos++) {
for (int yPos = 0; yPos < latPositions; yPos++) {
for (int xPos = 0; xPos < lonPositions; xPos++) {
int sVal = originalVarData.getInt(varIndex.set(tPos, yPos, xPos));
if (findNewRange) {
if (sVal >= max && sVal != fillValue)
max = sVal;
if (sVal <= min && sVal != fillValue)
min = sVal;
}
// Flipping y
int newYpos = yPos;
// Flipping y
if (flipY) {
newYpos = latPositions - yPos - 1;
}
destArray.setInt(destIndex.set(tPos, newYpos, xPos), sVal);
}
}
}
}
ncFileOut.write(varName, destArray);
if (findNewRange) {
Array range = NetCDFConverterUtilities.getRangeArray(varDataType);
Index index = range.getIndex();
range.setInt(index.set(0), min);
range.setInt(index.set(1), max);
ncFileOut.updateAttribute(ncFileOut.findVariable(varName), new Attribute(NetCDFUtilities.DatasetAttribs.VALID_RANGE, range));
}
if (updateFillValue) {
ncFileOut.updateAttribute(ncFileOut.findVariable(varName), new Attribute(NetCDFUtilities.DatasetAttribs.FILL_VALUE, new Integer(fillValue)));
}
} else
throw new IllegalArgumentException("Unsupported DataType");
}
use of com.google.datastore.admin.v1.Index in project OpenTripPlanner by opentripplanner.
the class GenericEdgeUpdater method getClosestPropertyValue.
/**
* Returns closest a value of selected property for given point.
* <p>
* Returned array represent value for selected property time
*
* @param samplePoint point
* @param propertyName propertyName
* @return result the closest value for selected property for given point
*/
private <E> List<E> getClosestPropertyValue(Point2D samplePoint, String propertyName) {
double lon = samplePoint.getX();
double lat = samplePoint.getY();
int lonIndex = getClosestIndex(dataFile.getLongitudeArray(), lon);
int latIndex = getClosestIndex(dataFile.getLatitudeArray(), lat);
int timeSize = (int) dataFile.getTimeArray().getSize();
List result = new ArrayList<>();
int height = 0;
for (int timeIndex = 0; timeIndex < timeSize; timeIndex++) {
Array dataArray = genericVariablesData.get(propertyName);
Index selectIndex = dataArray.getIndex();
if (selectIndex.getRank() == 3) {
selectIndex.set(timeIndex, latIndex, lonIndex);
} else if (selectIndex.getRank() == 4) {
selectIndex.set(timeIndex, height, latIndex, lonIndex);
} else {
throw new IllegalArgumentException(String.format("Invalid data array shape for %s", propertyName));
}
Class dataArrayType = dataArray.getDataType().getPrimitiveClassType();
if (dataArrayType.equals(Integer.TYPE)) {
result.add(timeIndex, ((ArrayInt) dataArray).get(selectIndex));
} else if (dataArrayType.equals(Double.TYPE)) {
result.add(timeIndex, ((ArrayDouble) dataArray).get(selectIndex));
} else if (dataArrayType.equals(Float.TYPE)) {
result.add(timeIndex, ((ArrayFloat) dataArray).get(selectIndex));
} else if (dataArrayType.equals(Long.TYPE)) {
result.add(timeIndex, ((ArrayLong) dataArray).get(selectIndex));
} else {
throw new IllegalArgumentException(String.format("Unsupported format %s of %s variable", dataArrayType, propertyName));
}
}
return result;
}
use of com.google.datastore.admin.v1.Index in project java-datastore by googleapis.
the class DatastoreAdminClientTest method getIndexTest.
@Test
public void getIndexTest() throws Exception {
Index expectedResponse = Index.newBuilder().setProjectId("projectId-894832108").setIndexId("indexId1943291277").setKind("kind3292052").addAllProperties(new ArrayList<Index.IndexedProperty>()).build();
mockDatastoreAdmin.addResponse(expectedResponse);
GetIndexRequest request = GetIndexRequest.newBuilder().setProjectId("projectId-894832108").setIndexId("indexId1943291277").build();
Index actualResponse = client.getIndex(request);
Assert.assertEquals(expectedResponse, actualResponse);
List<AbstractMessage> actualRequests = mockDatastoreAdmin.getRequests();
Assert.assertEquals(1, actualRequests.size());
GetIndexRequest actualRequest = ((GetIndexRequest) actualRequests.get(0));
Assert.assertEquals(request.getProjectId(), actualRequest.getProjectId());
Assert.assertEquals(request.getIndexId(), actualRequest.getIndexId());
Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Aggregations