use of com.google.firestore.admin.v1.Index in project imageio-ext by geosolutions-it.
the class NetCDFCFExperiment 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.firestore.admin.v1.Index in project imageio-ext by geosolutions-it.
the class NetCDFCFExperiment method setTimeVariableAttributes.
private float setTimeVariableAttributes(Variable time_0, NetcdfFileWriteable ncFileOut) throws IOException {
Array time_0_Data = time_0.read();
Index time_0_Index = time_0_Data.getIndex();
final String name = time_0.getName();
final float referenceTime = time_0_Data.getFloat(time_0_Index.set(0));
float fTime = referenceTime;
Attribute offset = time_0.findAttribute("add_offset");
if (offset != null)
fTime += offset.getNumericValue().floatValue();
GregorianCalendar calendar = fromJulian(fTime);
final String year = Integer.toString(calendar.get(Calendar.YEAR));
final String month = Integer.toString((calendar.get(Calendar.MONTH) + 1));
final String day = Integer.toString(calendar.get(Calendar.DAY_OF_MONTH));
String hour = Integer.toString(calendar.get(Calendar.HOUR));
if (hour.equalsIgnoreCase("0"))
hour += "0";
String minute = Integer.toString(calendar.get(Calendar.MINUTE));
if (minute.equalsIgnoreCase("0"))
minute += "0";
String second = Integer.toString(calendar.get(Calendar.SECOND));
if (second.equalsIgnoreCase("0"))
second += "0";
final String millisecond = Integer.toString(calendar.get(Calendar.MILLISECOND));
final StringBuffer sbTime = new StringBuffer(year).append("-").append(month).append("-").append(day).append(" ").append(hour).append(":").append(minute).append(":").append(second).append(".").append(millisecond);
ncFileOut.addVariableAttribute(name, "units", "days since " + sbTime.toString());
ncFileOut.addVariableAttribute(name, "long_name", "time");
return referenceTime;
}
use of com.google.firestore.admin.v1.Index in project imageio-ext by geosolutions-it.
the class NetCDFConverterUtilities method isFillValueOutsideValidRange.
public static boolean isFillValueOutsideValidRange(Attribute validRange, Attribute fillValue, DataType dataType) {
Array range = validRange.getValues();
Index index = range.getIndex();
if (dataType == DataType.FLOAT) {
final float min = range.getFloat(index.set(0));
final float max = range.getFloat(index.set(1));
final float fill = fillValue.getNumericValue().floatValue();
if (fill == min || fill == max)
return false;
else
return true;
} else if (dataType == DataType.DOUBLE) {
final double min = range.getDouble(index.set(0));
final double max = range.getDouble(index.set(1));
final double fill = fillValue.getNumericValue().doubleValue();
if (fill == min || fill == max)
return false;
else
return true;
} else if (dataType == DataType.BYTE) {
final byte min = range.getByte(index.set(0));
final byte max = range.getByte(index.set(1));
final byte fill = fillValue.getNumericValue().byteValue();
if (fill == min || fill == max)
return false;
else
return true;
} else if (dataType == DataType.SHORT) {
final short min = range.getShort(index.set(0));
final short max = range.getShort(index.set(1));
final short fill = fillValue.getNumericValue().shortValue();
if (fill == min || fill == max)
return false;
else
return true;
} else if (dataType == DataType.INT) {
final int min = range.getInt(index.set(0));
final int max = range.getInt(index.set(1));
final int fill = fillValue.getNumericValue().intValue();
if (fill == min || fill == max)
return false;
else
return true;
}
throw new IllegalArgumentException("Actually unsupported Datatype");
}
use of com.google.firestore.admin.v1.Index in project imageio-ext by geosolutions-it.
the class NetCDFConverterUtilities method setData1D.
public static void setData1D(Array originalData, Array destinationData, final DataType navLatDataType, final int maxIndex, final boolean flipData) {
Index originalDataIndex = originalData.getIndex();
Index destinationDataIndex = flipData ? destinationData.getIndex() : originalDataIndex;
if (navLatDataType == DataType.FLOAT) {
for (int pos = 0; pos < maxIndex; pos++) {
float f = originalData.getFloat(originalDataIndex.set(pos));
// Flipping latitudes
if (flipData) {
destinationDataIndex.set(maxIndex - pos - 1);
}
destinationData.setFloat(destinationDataIndex, f);
}
} else if (navLatDataType == DataType.DOUBLE) {
for (int pos = 0; pos < maxIndex; pos++) {
double d = originalData.getDouble(originalDataIndex.set(pos));
// Flipping latitudes
if (flipData) {
destinationDataIndex.set(maxIndex - pos - 1);
}
destinationData.setDouble(destinationDataIndex, d);
}
} else if (navLatDataType == DataType.BYTE) {
for (int pos = 0; pos < maxIndex; pos++) {
byte b = originalData.getByte(originalDataIndex.set(pos));
// Flipping latitudes
if (flipData) {
destinationDataIndex.set(maxIndex - pos - 1);
}
destinationData.setByte(destinationDataIndex, b);
}
} else if (navLatDataType == DataType.SHORT) {
for (int pos = 0; pos < maxIndex; pos++) {
short s = originalData.getShort(originalDataIndex.set(pos));
// Flipping latitudes
if (flipData) {
destinationDataIndex.set(maxIndex - pos - 1);
}
destinationData.setShort(destinationDataIndex, s);
}
} else if (navLatDataType == DataType.INT) {
for (int pos = 0; pos < maxIndex; pos++) {
int i = originalData.getInt(originalDataIndex.set(pos));
// Flipping latitudes
if (flipData) {
destinationDataIndex.set(maxIndex - pos - 1);
}
destinationData.setInt(destinationDataIndex, i);
}
}
}
use of com.google.firestore.admin.v1.Index in project imageio-ext by geosolutions-it.
the class SWANConverter method run.
private void run(String fileNameIn, String fileNameOut) {
try {
final File fileIn = new File(fileNameIn);
final NetcdfFile ncFileIn = NetcdfFile.open(fileNameIn);
final File fileOut = new File(fileNameOut);
// keep original name
final File outputFile = File.createTempFile(fileIn.getName(), ".tmp");
final NetcdfFileWriteable ncFileOut = NetcdfFileWriteable.createNew(outputFile.getAbsolutePath());
boolean hasZeta = false;
// input dimensions
final Dimension timeDim0 = ncFileIn.findDimension("time");
final int nTimes = timeDim0.getLength();
final Dimension latDim0 = ncFileIn.findDimension(NetCDFUtilities.LATITUDE);
final int nLat = latDim0.getLength();
final Dimension lonDim0 = ncFileIn.findDimension(NetCDFUtilities.LONGITUDE);
final int nLon = lonDim0.getLength();
// input VARIABLES
final Variable timeOriginalVar = ncFileIn.findVariable("time");
final Array timeOriginalData = timeOriginalVar.read();
final Index timeOriginalIndex = timeOriginalData.getIndex();
final DataType timeDataType = timeOriginalVar.getDataType();
final Variable lonOriginalVar = ncFileIn.findVariable(NetCDFUtilities.LONGITUDE);
final DataType lonDataType = lonOriginalVar.getDataType();
final Variable latOriginalVar = ncFileIn.findVariable(NetCDFUtilities.LATITUDE);
final DataType latDataType = latOriginalVar.getDataType();
final Array latOriginalData = latOriginalVar.read();
final Array lonOriginalData = lonOriginalVar.read();
// //
//
// Depth related vars
//
// //
Array levelOriginalData = null;
int nZeta = 0;
Array zeta1Data = null;
Dimension zDim = null;
DataType zetaDataType = null;
// Depth
final Variable levelOriginalVar = ncFileIn.findVariable("z");
if (levelOriginalVar != null) {
nZeta = levelOriginalVar.getDimension(0).getLength();
levelOriginalData = levelOriginalVar.read();
zetaDataType = levelOriginalVar.getDataType();
hasZeta = true;
}
Dimension timeDim = ncFileOut.addDimension("time", nTimes);
Dimension latDim = ncFileOut.addDimension(NetCDFUtilities.LAT, nLat);
Dimension lonDim = ncFileOut.addDimension(NetCDFUtilities.LON, nLon);
if (hasZeta)
zDim = ncFileOut.addDimension(NetCDFUtilities.HEIGHT, nZeta);
NetCDFConverterUtilities.copyGlobalAttributes(ncFileOut, ncFileIn.getGlobalAttributes());
// Dimensions
Variable timeVar = ncFileOut.addVariable("time", timeDataType, new Dimension[] { timeDim });
NetCDFConverterUtilities.setVariableAttributes(timeOriginalVar, ncFileOut, new String[] { "long_name" });
ncFileOut.addVariableAttribute("time", "long_name", "time");
ncFileOut.addVariable(NetCDFUtilities.LAT, latDataType, new Dimension[] { latDim });
NetCDFConverterUtilities.setVariableAttributes(latOriginalVar, ncFileOut, NetCDFUtilities.LAT);
ncFileOut.addVariable(NetCDFUtilities.LON, lonDataType, new Dimension[] { lonDim });
NetCDFConverterUtilities.setVariableAttributes(lonOriginalVar, ncFileOut, NetCDFUtilities.LON);
if (hasZeta) {
ncFileOut.addVariable(NetCDFUtilities.HEIGHT, zetaDataType, new Dimension[] { zDim });
NetCDFConverterUtilities.setVariableAttributes(levelOriginalVar, ncFileOut, NetCDFUtilities.HEIGHT, new String[] { "long_name" });
ncFileOut.addVariableAttribute(NetCDFUtilities.HEIGHT, "positive", "up");
ncFileOut.addVariableAttribute(NetCDFUtilities.HEIGHT, "long_name", NetCDFUtilities.HEIGHT);
}
// lat Variable
Array lat1Data = NetCDFConverterUtilities.getArray(nLat, latDataType);
NetCDFConverterUtilities.setData1D(latOriginalData, lat1Data, latDataType, nLat, true);
// lon Variable
Array lon1Data = NetCDFConverterUtilities.getArray(nLon, lonDataType);
NetCDFConverterUtilities.setData1D(lonOriginalData, lon1Data, lonDataType, nLon, false);
if (hasZeta) {
// depth level Variable
zeta1Data = NetCDFConverterUtilities.getArray(nZeta, zetaDataType);
NetCDFConverterUtilities.setData1D(levelOriginalData, zeta1Data, zetaDataType, nZeta, false);
}
// {} Variables
final ArrayList<String> variables = new ArrayList<String>(5);
int numVars = 0;
List<Variable> findVariables = ncFileIn.getVariables();
for (Variable var : findVariables) {
if (var != null) {
String varName = var.getName();
if (varName.equalsIgnoreCase(NetCDFUtilities.LATITUDE) || varName.equalsIgnoreCase(NetCDFUtilities.LONGITUDE) || varName.equalsIgnoreCase(NetCDFUtilities.TIME) || varName.equalsIgnoreCase(NetCDFUtilities.ZETA))
continue;
variables.add(varName);
List<Dimension> dims = var.getDimensions();
boolean hasLocalZeta = false;
for (Dimension dim : dims) {
if (dim.getName().equalsIgnoreCase(NetCDFUtilities.ZETA)) {
hasLocalZeta = true;
break;
}
}
if (hasZeta && hasLocalZeta)
ncFileOut.addVariable(varName, var.getDataType(), new Dimension[] { timeDim, zDim, latDim, lonDim });
else
ncFileOut.addVariable(varName, var.getDataType(), new Dimension[] { timeDim, latDim, lonDim });
NetCDFConverterUtilities.setVariableAttributes(var, ncFileOut, new String[] { "missing_value" });
numVars++;
}
}
// writing bin data ...
ncFileOut.create();
ArrayFloat timeData = new ArrayFloat(new int[] { timeDim.getLength() });
Index timeIndex = timeData.getIndex();
for (int t = 0; t < timeDim.getLength(); t++) {
timeData.setFloat(timeIndex.set(t), timeOriginalData.getFloat(timeOriginalIndex.set(t)));
}
ncFileOut.write("time", timeData);
timeVar = ncFileOut.findVariable("time");
timeDim.addCoordinateVariable(timeVar);
ncFileOut.write(NetCDFUtilities.LAT, lat1Data);
ncFileOut.write(NetCDFUtilities.LON, lon1Data);
if (hasZeta) {
Variable heightVar = ncFileOut.findVariable(NetCDFUtilities.HEIGHT);
zDim.addCoordinateVariable(heightVar);
ncFileOut.write(NetCDFUtilities.HEIGHT, zeta1Data);
}
for (int i = 0; i < numVars; i++) {
String varName = (String) variables.get(i);
Variable var = ncFileIn.findVariable(varName);
boolean hasLocalZeta = NetCDFConverterUtilities.hasThisDimension(var, NetCDFUtilities.ZETA);
Array originalVarArray = var.read();
DataType varDataType = var.getDataType();
Array destArray = null;
int[] dimensions = null;
if (hasZeta && hasLocalZeta) {
dimensions = new int[] { timeDim.getLength(), zDim.getLength(), latDim.getLength(), lonDim.getLength() };
} else {
dimensions = new int[] { timeDim.getLength(), latDim.getLength(), lonDim.getLength() };
}
destArray = NetCDFConverterUtilities.getArray(dimensions, varDataType);
final boolean setZeta = hasZeta && hasLocalZeta;
final int[] loopLengths;
if (setZeta)
loopLengths = new int[] { nTimes, nZeta, nLat, nLon };
else
loopLengths = new int[] { nTimes, nLat, nLon };
NetCDFConverterUtilities.writeData(ncFileOut, varName, var, originalVarArray, destArray, false, false, loopLengths, true);
}
ncFileOut.close();
outputFile.renameTo(fileOut);
} catch (Exception e) {
// something bad happened
if (NetCDFConverterUtilities.LOGGER.isLoggable(Level.INFO))
NetCDFConverterUtilities.LOGGER.log(Level.INFO, e.getLocalizedMessage(), e);
JAI.getDefaultInstance().getTileCache().flush();
}
}
Aggregations