use of com.beanit.openiec61850.Array in project imageio-ext by geosolutions-it.
the class NCOMConverter 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 hasDepth = false;
// input dimensions
final Dimension timeDim0 = ncFileIn.findDimension("time");
final int nTimes = timeDim0.getLength();
final Dimension latDim0 = ncFileIn.findDimension("lat");
final int nLat = latDim0.getLength();
final Dimension lonDim0 = ncFileIn.findDimension("lon");
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("lon");
final DataType lonDataType = lonOriginalVar.getDataType();
final Variable latOriginalVar = ncFileIn.findVariable("lat");
final DataType latDataType = latOriginalVar.getDataType();
final Array latOriginalData = latOriginalVar.read();
final Array lonOriginalData = lonOriginalVar.read();
// //
//
// Depth related vars
//
// //
Array depthOriginalData = null;
int nDepths = 0;
Array depth1Data = null;
Dimension depthDim = null;
DataType depthDataType = null;
// Depth
final Variable depthOriginalVar = ncFileIn.findVariable("depth");
if (depthOriginalVar != null) {
nDepths = depthOriginalVar.getDimension(0).getLength();
depthOriginalData = depthOriginalVar.read();
depthDataType = depthOriginalVar.getDataType();
hasDepth = true;
}
Dimension timeDim = ncFileOut.addDimension("time", nTimes);
Dimension latDim = ncFileOut.addDimension(NetCDFUtilities.LAT, nLat);
Dimension lonDim = ncFileOut.addDimension(NetCDFUtilities.LON, nLon);
if (hasDepth)
depthDim = ncFileOut.addDimension(NetCDFUtilities.DEPTH, nDepths);
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);
ncFileOut.addVariable(NetCDFUtilities.LON, lonDataType, new Dimension[] { lonDim });
NetCDFConverterUtilities.setVariableAttributes(lonOriginalVar, ncFileOut);
if (hasDepth) {
ncFileOut.addVariable(NetCDFUtilities.DEPTH, depthDataType, new Dimension[] { depthDim });
NetCDFConverterUtilities.setVariableAttributes(depthOriginalVar, ncFileOut);
}
// 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 (hasDepth) {
// depth level Variable
depth1Data = NetCDFConverterUtilities.getArray(nDepths, depthDataType);
NetCDFConverterUtilities.setData1D(depthOriginalData, depth1Data, depthDataType, nDepths, false);
}
// {} Variables
final ArrayList<String> variables = new ArrayList<String>(5);
final HashMap<String, String> updatingValidRange = new HashMap<String, String>(5);
int numVars = 0;
for (int i = 0; i < NUMVARS; i++) {
String varName = (String) VARIABLES.get(i);
Variable var = ncFileIn.findVariable(varName);
if (var != null) {
variables.add(varName);
List<Dimension> dims = var.getDimensions();
boolean hasLocalDepth = false;
for (Dimension dim : dims) {
if (dim.getName().equalsIgnoreCase("depth")) {
hasLocalDepth = true;
break;
}
}
if (hasDepth && hasLocalDepth)
ncFileOut.addVariable(varName, var.getDataType(), new Dimension[] { timeDim, depthDim, latDim, lonDim });
else
ncFileOut.addVariable(varName, var.getDataType(), new Dimension[] { timeDim, latDim, lonDim });
Attribute validRange = var.findAttribute(NetCDFUtilities.DatasetAttribs.VALID_MAX);
if (validRange != null)
validRange = var.findAttribute(NetCDFUtilities.DatasetAttribs.VALID_MIN);
if (validRange == null)
validRange = var.findAttribute(NetCDFUtilities.DatasetAttribs.VALID_RANGE);
if (validRange == null) {
updatingValidRange.put(varName, "");
ArrayShort range = new ArrayShort(new int[] { 2 });
Index index = range.getIndex();
range.setShort(index.set(0), Short.MIN_VALUE);
range.setShort(index.set(1), Short.MAX_VALUE);
ncFileOut.addVariableAttribute(varName, NetCDFUtilities.DatasetAttribs.VALID_RANGE, range);
}
NetCDFConverterUtilities.setVariableAttributes(var, ncFileOut);
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 (hasDepth) {
Variable depthVar = ncFileOut.findVariable("depth");
depthDim.addCoordinateVariable(depthVar);
ncFileOut.write(NetCDFUtilities.DEPTH, depth1Data);
}
for (int i = 0; i < numVars; i++) {
String varName = (String) variables.get(i);
Variable var = ncFileIn.findVariable(varName);
boolean hasLocalDepth = NetCDFConverterUtilities.hasThisDimension(var, "depth");
Array originalVarArray = var.read();
DataType varDataType = var.getDataType();
Array destArray = null;
int[] dimensions = null;
if (hasDepth && hasLocalDepth) {
dimensions = new int[] { timeDim.getLength(), depthDim.getLength(), latDim.getLength(), lonDim.getLength() };
} else {
dimensions = new int[] { timeDim.getLength(), latDim.getLength(), lonDim.getLength() };
}
destArray = NetCDFConverterUtilities.getArray(dimensions, varDataType);
boolean findNewRange = updatingValidRange.containsKey(varName);
final boolean setDepth = hasDepth && hasLocalDepth;
final int[] loopLengths;
if (setDepth)
loopLengths = new int[] { nTimes, nDepths, nLat, nLon };
else
loopLengths = new int[] { nTimes, nLat, nLon };
NetCDFConverterUtilities.writeData(ncFileOut, varName, var, originalVarArray, destArray, findNewRange, 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();
}
}
use of com.beanit.openiec61850.Array in project imageio-ext by geosolutions-it.
the class NetCDFCFExperiment method run.
private void run(String fileNameIn, String fileNameOut) {
try {
NetcdfFile ncFileIn = NetcdfFile.open(fileNameIn);
ncFileIn.writeCDL(System.out, true);
NetcdfFileWriteable ncFileOut = NetcdfFileWriteable.createNew(fileNameOut);
// input dimensions
Dimension timeDim0 = ncFileIn.findDimension("time");
Dimension latDim0 = ncFileIn.findDimension("lat");
Dimension lonDim0 = ncFileIn.findDimension("lon");
// input variables
Variable time_0 = ncFileIn.findVariable("time");
Array time_0_Data = time_0.read();
Index time_0_Index = time_0_Data.getIndex();
Variable grid3 = ncFileIn.findVariable("grid3");
final int nLat = grid3.getDimension(0).getLength();
final int nLon = grid3.getDimension(1).getLength();
final String units = ((Attribute) grid3.findAttribute(UNITS)).getStringValue();
final String[] unit = units.split(",");
Array lat_0_Data = grid3.read("0:" + (nLat - 1) + ":1, 0:0:1, 1:1:1").reduce();
Index lat_0_Index = lat_0_Data.getIndex();
Array lon_0_Data = grid3.read("0:0:1, 0:" + (nLon - 1) + ":1, 0:0:1").reduce();
Index lon_0_Index = lon_0_Data.getIndex();
// Depth
Variable z_0 = ncFileIn.findVariable("zout");
final int nLevels = z_0.getDimension(0).getLength();
Array z_0_Data = z_0.read("0:" + (nLevels - 1) + ":1, 2:2:1");
// Index z_0_Index = z_0_Data.getIndex();
Dimension timeDim = ncFileOut.addDimension("time", timeDim0.getLength());
Dimension latDim = ncFileOut.addDimension(NetCDFUtilities.LAT, latDim0.getLength());
Dimension lonDim = ncFileOut.addDimension(NetCDFUtilities.LON, lonDim0.getLength());
Dimension depthDim = ncFileOut.addDimension(NetCDFUtilities.DEPTH, nLevels);
// writing file
this.computeMatrixExtremes(lat_0_Data, lon_0_Data, lonDim0.getLength(), latDim0.getLength(), lat_0_Index, lon_0_Index);
copyGlobalAttributes(ncFileOut, ncFileIn.getGlobalAttributes());
// //
//
// Time requires a special Management
//
// //
// time Variable
ncFileOut.addVariable("time", DataType.FLOAT, new Dimension[] { timeDim });
// ncFileOut.addVariableAttribute("Time", "long_name", "Time");
final float referenceTime = setTimeVariableAttributes(time_0, ncFileOut);
// lat Variable
ArrayFloat lat_1_Data = new ArrayFloat(new int[] { latDim0.getLength() });
Index lat_1_Index = lat_1_Data.getIndex();
ncFileOut.addVariable(NetCDFUtilities.LAT, DataType.FLOAT, new Dimension[] { latDim });
ncFileOut.addVariableAttribute(NetCDFUtilities.LAT, "long_name", NetCDFUtilities.LATITUDE);
ncFileOut.addVariableAttribute(NetCDFUtilities.LAT, UNITS, unit[1].trim());
for (int yPos = 0; yPos < latDim0.getLength(); yPos++) {
lat_1_Data.setFloat(lat_1_Index.set(yPos), new Float(this.ymax - (new Float(yPos).floatValue() * this.periodY)).floatValue());
// new Float(
// this.ymin
// + (new Float(yPos)
// .floatValue() * this.periodY))
// .floatValue());
}
// lon Variable
ArrayFloat lon_1_Data = new ArrayFloat(new int[] { lonDim0.getLength() });
Index lon_1_Index = lon_1_Data.getIndex();
ncFileOut.addVariable(NetCDFUtilities.LON, DataType.FLOAT, new Dimension[] { lonDim });
ncFileOut.addVariableAttribute(NetCDFUtilities.LON, "long_name", NetCDFUtilities.LONGITUDE);
ncFileOut.addVariableAttribute(NetCDFUtilities.LON, UNITS, unit[0].trim());
for (int xPos = 0; xPos < lonDim0.getLength(); xPos++) {
lon_1_Data.setFloat(lon_1_Index.set(xPos), new Float(this.xmin + (new Float(xPos).floatValue() * this.periodX)).floatValue());
}
// depth level Variable
ArrayInt depthlevelDim_1_Data = new ArrayInt(new int[] { depthDim.getLength() });
Index depthlevelDim_1_Index = depthlevelDim_1_Data.getIndex();
ncFileOut.addVariable(NetCDFUtilities.DEPTH, DataType.FLOAT, new Dimension[] { depthDim });
ncFileOut.addVariableAttribute(NetCDFUtilities.DEPTH, "long_name", NetCDFUtilities.DEPTH);
ncFileOut.addVariableAttribute(NetCDFUtilities.DEPTH, UNITS, unit[2].trim());
ncFileOut.addVariableAttribute(NetCDFUtilities.DEPTH, "positive", "up");
for (int wPos = 0; wPos < depthDim.getLength(); wPos++) {
depthlevelDim_1_Data.setFloat(depthlevelDim_1_Index.set(wPos), z_0_Data.getFloat(depthlevelDim_1_Index));
}
// {} Variables
for (int i = 0; i < NUMVARS; i++) {
String varName = variables.get(i);
Variable var = ncFileIn.findVariable(varName);
ncFileOut.addVariable(varName, var.getDataType(), new Dimension[] { timeDim, depthDim, latDim, lonDim });
setVariableAttributes(var, ncFileOut, new String[] { "positions" });
}
// 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++) {
float julianTime = time_0_Data.getFloat(time_0_Index.set(t)) - referenceTime;
timeData.setFloat(timeIndex.set(t), julianTime);
}
ncFileOut.write("time", timeData);
Variable timeVar = ncFileOut.findVariable("time");
timeDim.addCoordinateVariable(timeVar);
ncFileOut.write(NetCDFUtilities.LAT, lat_1_Data);
ncFileOut.write(NetCDFUtilities.LON, lon_1_Data);
Variable depthVar = ncFileOut.findVariable("depth");
depthDim.addCoordinateVariable(depthVar);
ncFileOut.write(NetCDFUtilities.DEPTH, depthlevelDim_1_Data);
// TODO: AutoApply MASK?
ArrayFloat maskMatrix = new ArrayFloat.D2(latDim.getLength(), lonDim.getLength());
Index maskIma = maskMatrix.getIndex();
if (APPLY_MASK) {
Variable mask = ncFileIn.findVariable("mask");
Array maskData = mask.read();
Index maskIndex = maskData.getIndex();
ArrayFloat tempData = new ArrayFloat(new int[] { latDim0.getLength(), lonDim0.getLength() });
Index tempIndex = tempData.getIndex();
for (int yPos = 0; yPos < latDim0.getLength(); yPos++) {
for (int xPos = 0; xPos < lonDim0.getLength(); xPos++) {
tempData.setFloat(tempIndex.set(yPos, xPos), maskData.getFloat(maskIndex.set(yPos, xPos)));
}
}
WritableRaster outData = this.Resampler(lat_0_Data, lon_0_Data, lonDim0.getLength(), latDim0.getLength(), 2, tempData, -1);
for (int j = 0; j < latDim0.getLength(); j++) {
for (int k = 0; k < lonDim0.getLength(); k++) {
float sample = outData.getSampleFloat(k, j, 0);
maskMatrix.setFloat(maskIma.set(j, k), sample);
}
}
}
for (int i = 0; i < NUMVARS; i++) {
String varName = variables.get(i);
Variable var = ncFileIn.findVariable(varName);
Array originalVarData = var.read();
Index varIndex = originalVarData.getIndex();
Attribute fv = var.findAttribute("_FillValue");
float fillValue = Float.NaN;
if (fv != null) {
fillValue = (fv.getNumericValue()).floatValue();
}
ArrayFloat T_tmp_Data = new ArrayFloat(new int[] { latDim0.getLength(), lonDim0.getLength() });
Index T_tmp_Index = T_tmp_Data.getIndex();
ArrayFloat Tmatrix = new ArrayFloat.D4(timeDim.getLength(), depthDim.getLength(), latDim.getLength(), lonDim.getLength());
Index Tima = Tmatrix.getIndex();
for (int tPos = 0; tPos < timeDim0.getLength(); tPos++) {
for (int levelPos = 0; levelPos < depthDim.getLength(); levelPos++) {
for (int yPos = 0; yPos < latDim0.getLength(); yPos++) {
for (int xPos = 0; xPos < lonDim0.getLength(); xPos++) {
T_tmp_Data.setFloat(T_tmp_Index.set(yPos, xPos), originalVarData.getFloat(varIndex.set(tPos, yPos, xPos, levelPos)));
}
}
WritableRaster outData = this.Resampler(lat_0_Data, lon_0_Data, lonDim0.getLength(), latDim0.getLength(), 2, T_tmp_Data, fillValue);
for (int j = 0; j < latDim0.getLength(); j++) {
for (int k = 0; k < lonDim0.getLength(); k++) {
float sample = outData.getSampleFloat(k, j, 0);
if (APPLY_MASK) {
float maskValue = maskMatrix.getFloat(maskIma.set(j, k));
if (maskValue == 0)
sample = fillValue;
}
Tmatrix.setFloat(Tima.set(tPos, levelPos, j, k), sample);
}
}
}
}
ncFileOut.write(varName, Tmatrix);
}
ncFileOut.close();
} catch (Exception e) {
e.printStackTrace(System.out);
}
}
use of com.beanit.openiec61850.Array 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.beanit.openiec61850.Array 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.beanit.openiec61850.Array in project imageio-ext by geosolutions-it.
the class HDF4APSImageReader method initializeProfile.
/**
* Initialize main properties for this <code>HDF4APSImageReader</code>
*/
protected void initializeProfile() throws IOException {
final NetcdfDataset dataset = reader.getDataset();
if (dataset == null) {
throw new IOException("Unable to initialize profile due to a null dataset");
}
final List<Variable> variables = dataset.getVariables();
final List<Attribute> attributes = dataset.getGlobalAttributes();
final int numVars = variables.size();
reader.setNumGlobalAttributes(attributes.size());
// //
//
// Getting projection dataset name
//
// //
final String navAttrib = NetCDFUtilities.getGlobalAttributeAsString(dataset, HDF4APSProperties.PFA_NA_MAPPROJECTION);
if (navAttrib != null && navAttrib.length() > 0) {
projectionDatasetName = navAttrib;
}
final String prodAttrib = NetCDFUtilities.getGlobalAttributeAsString(dataset, HDF4APSProperties.PRODLIST);
int numImages = 0;
if (prodAttrib != null && prodAttrib.length() > 0) {
String[] products = prodAttrib.split(",");
productList = HDF4APSProperties.refineProductList(products);
numImages = productList.length;
} else {
numImages = numVars;
}
setNumImages(numImages);
reader.setNumImages(numImages);
final Map<Range, APSDatasetWrapper> indexMap = new HashMap<Range, APSDatasetWrapper>(numImages);
Variable varProjection;
// //
//
// Setting spatial domain
//
// //
// getting map dataset
varProjection = dataset.findVariable(projectionDatasetName);
if (varProjection != null && varProjection.getName().equalsIgnoreCase(projectionDatasetName)) {
// TODO: All projection share the same dataset
// structure?
Array data = varProjection.read();
final int datatype = NetCDFUtilities.getRawDataType(varProjection);
if (projectionMap == null) {
projectionMap = buildProjectionAttributesMap(data, datatype);
// Force UoM of MapBoundary product as the last element in
// the map
}
}
try {
// Scanning all the datasets
for (Variable var : variables) {
final String name = var.getName();
for (int j = 0; j < numImages; j++) {
// Checking if the actual dataset is a product.
if (name.equals(productList[j])) {
// Updating the subDatasetsMap map
indexMap.put(new Range(j, j + 1), new APSDatasetWrapper(var));
break;
}
}
}
} catch (InvalidRangeException e) {
throw new IllegalArgumentException("Error occurred during NetCDF file parsing", e);
}
reader.setIndexMap(indexMap);
}
Aggregations