Gianpaolo Coro 2013-05-03 09:28:15 +00:00
parent 7e8f3a6e5c
commit 88ab16c6bd
1 changed files with 141 additions and 13 deletions

View File

@ -13,11 +13,17 @@ import javax.xml.xpath.XPathFactory;
import org.gcube.contentmanagement.graphtools.utils.HttpRequest;
import org.gcube.contentmanagement.lexicalmatcher.utils.AnalysisLogger;
import org.gcube.dataanalysis.ecoengine.utils.Tuple;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import ucar.ma2.Array;
import ucar.ma2.ArrayFloat;
import ucar.ma2.Index;
import ucar.ma2.Index2D;
import ucar.ma2.Index3D;
import ucar.ma2.Range;
import ucar.ma2.StructureData;
import ucar.ma2.StructureMembers.Member;
import ucar.nc2.constants.FeatureType;
@ -80,6 +86,132 @@ public class ThreddsDataExplorer {
}
}
public static List<Double> retrieveDataFromNetCDF(String openDapLink, String layer, int time, List<Tuple<Double>> triplets, double xL, double xR, double yL, double yR) {
try {
List<Double> values = new ArrayList<Double>();
if (isGridDataset(openDapLink)) {
AnalysisLogger.getLogger().debug("Managing Grid File");
return manageGridDataset(layer, openDapLink, time, triplets, xL, xR, yL, yR);
}
/*
* else if (isPointDataset(openDapLink)) { AnalysisLogger.getLogger().debug("Managing Points File"); }
*/
else
AnalysisLogger.getLogger().debug("Warning: the NETCDF file is of an unknown type");
return values;
} catch (Exception e) {
AnalysisLogger.getLogger().debug("ERROR: " + e.getMessage());
AnalysisLogger.getLogger().debug(e);
e.printStackTrace();
return null;
}
}
// A GridDatatype is like a specialized Variable that explicitly handles X,Y,Z,T dimensions
public static List<Double> manageGridDataset(String layer, String filename, int time, List<Tuple<Double>> triplets, double xL, double xR, double yL, double yR) throws Exception {
List<Double> values = new ArrayList<Double>();
GridDataset gds = ucar.nc2.dt.grid.GridDataset.open(filename);
List<GridDatatype> gridTypes = gds.getGrids();
for (GridDatatype gdt : gridTypes) {
AnalysisLogger.getLogger().debug("Inside File - layer name: " + gdt.getFullName());
if (layer.equalsIgnoreCase(gdt.getFullName())) {
AnalysisLogger.getLogger().debug("Found layer " + layer + " inside file");
GridDatatype grid = gds.findGridDatatype(gdt.getName());
CoordinateAxis zAxis = gdt.getCoordinateSystem().getVerticalAxis();
CoordinateAxis xAxis = gdt.getCoordinateSystem().getXHorizAxis();
CoordinateAxis yAxis = gdt.getCoordinateSystem().getYHorizAxis();
double resolutionZ = Math.abs((double) (zAxis.getMaxValue() - zAxis.getMinValue()) / (double) zAxis.getShape()[0]);
double resolutionX = Math.abs((double) (xAxis.getMaxValue() - xAxis.getMinValue()) / (double) xAxis.getShape()[0]);
double resolutionY = Math.abs((double) (yAxis.getMaxValue() - yAxis.getMinValue()) / (double) yAxis.getShape()[0]);
int tsize = triplets.size();
long t01 = System.currentTimeMillis();
LatLonRect llr = null;
GridDatatype gdtsub = grid.makeSubset(new Range(time, time), null, llr, 1, 1, 1);
Array data = gdtsub.readVolumeData(time); // note order is t, z, y, x
int[] shapeD = data.getShape();
int zD = 0;
int xD = 0;
int yD = 0;
if (shapeD.length>2)
{
zD=shapeD[0];
yD=shapeD[1];
xD=shapeD[2];
}
AnalysisLogger.getLogger().debug("Shape: Z:"+zD+" X:"+ xD+" Y:"+yD);
AnalysisLogger.getLogger().debug("Layer Information Retrieval ELAPSED Time: " + (System.currentTimeMillis() - t01));
int rank = data.getRank();
AnalysisLogger.getLogger().debug("Rank of the layer: " + rank);
ArrayFloat.D3 data3 = null;
ArrayFloat.D2 data2 = null;
if (data.getRank() == 3)
data3 = (ArrayFloat.D3) data;
else
data2 = (ArrayFloat.D2) data;
AnalysisLogger.getLogger().debug("Z dimension: "+zD+" Zmin:"+ zAxis.getMinValue()+" Zmax:"+zAxis.getMaxValue());
double xmin = xAxis.getMinValue();
double xmax = xAxis.getMaxValue();
if (((xmax==360) && (xmin==0)) || ((xmax==359.5) && (xmin==0.5))){
xmax = 180;
xmin=-180;
}
AnalysisLogger.getLogger().debug("X dimension: "+xD+" Xmin:"+ xmax+" Xmax:"+xmin);
for (int i = 0; i < tsize; i++) {
int zint = 0;
int xint = 0;
int yint = 0;
Tuple<Double> triplet = triplets.get(i);
double x = triplet.getElements().get(0);
double y = triplet.getElements().get(1);
if (x == 180)
x = -180;
if (y == 90)
y = -90;
double z = 0;
if (triplet.getElements().size() > 1)
z = triplet.getElements().get(2);
if (resolutionZ > 0) {
if ((zAxis.getMinValue() <= z) && (zAxis.getMaxValue() >= z))
zint = Math.abs((int) Math.round((z - zAxis.getMinValue()) / resolutionZ));
}
// AnalysisLogger.getLogger().debug("Z Index: "+zint);
/*
GridCoordSystem gcs = grid.getCoordinateSystem();
int[] xy = gcs.findXYindexFromLatLon(x, y, null);
Array datas=grid.readDataSlice(time, zint, xy[1], xy[0]);
*/
if ((xmin <= x) && (xmax >= x))
xint = (int) Math.round((x - xmin) / resolutionX);
if ((yAxis.getMinValue() <= y) && (yAxis.getMaxValue() >= y))
yint = (int) Math.round((y - yAxis.getMinValue()) / resolutionY);
Double val = Double.NaN;
if (xint > xD-1)
xint = xD-1;
if (yint > yD-1)
yint = yD-1;
if (zint>zD-1)
zint = zD-1;
if (data3 != null){
// System.out.println(val+" - "+zint+" "+yint+" "+xint +" --- "+xy[1]+" "+xy[0]);
// System.out.println(val+" - "+zint+" "+yint+" "+xint+" "+x+" "+y);
val = Double.valueOf(data3.get(zint, yint, xint));
}else if (data2 != null)
val = Double.valueOf(data2.get(yint, xint));
values.add(val);
}
break;
}
}
return values;
}
// A GridDatatype is like a specialized Variable that explicitly handles X,Y,Z,T dimensions
public static LinkedHashMap<String, Double> manageGridDataset(String layer, String filename, double x, double y, double z) throws Exception {
LinkedHashMap<String, Double> valuesMap = new LinkedHashMap<String, Double>();
@ -105,12 +237,14 @@ public class ThreddsDataExplorer {
CoordinateAxis zAxis = gdt.getCoordinateSystem().getVerticalAxis();
double resolutionZ = Math.abs((double) (zAxis.getMaxValue() - zAxis.getMinValue()) / (double) zAxis.getShape()[0]);
int zint = 0;
if (resolutionZ>0)
zint = (int) Math.round(z / resolutionZ);
if (resolutionZ > 0) {
if ((zAxis.getMinValue() <= z) && (zAxis.getMaxValue() >= z))
zint = Math.abs((int) Math.round((z - zAxis.getMinValue()) / resolutionZ));
}
AnalysisLogger.getLogger().debug("Z index to take: " + zint);
int[] xy = gcs.findXYindexFromLatLon(y, x, null);
int[] xy = gcs.findXYindexFromLatLon(x, y, null);
for (int j = 0; j < timeSteps; j++) {
try {
Array data = grid.readDataSlice(j, zint, xy[1], xy[0]); // note order is t, z, y, x
@ -260,20 +394,14 @@ public class ThreddsDataExplorer {
public static double adjX(double x) {
/*
if (x < -180)
x = -180;
if (x > 180)
x = 180;
* if (x < -180) x = -180; if (x > 180) x = 180;
*/
return x;
}
public static double adjY(double y) {
/*
if (y < -90)
y = -90;
if (y > 90)
y = 90;
* if (y < -90) y = -90; if (y > 90) y = 90;
*/
return y;
}