use of org.n52.sos.netcdf.data.dataset.AbstractSensorDataset in project SOS by 52North.
the class AbstractNetcdfEncoder method populateHeightDepthArray.
private Double populateHeightDepthArray(AbstractSensorDataset sensorDataset, Array heightDephtArray, Variable v) throws EncodingException {
Index index = heightDephtArray.getIndex();
int indexCounter = 0;
Double consistentBinHeight = null;
for (SubSensor subSensor : sensorDataset.getSubSensors()) {
if (subSensor instanceof ProfileSubSensor) {
index.setDim(0, indexCounter++);
heightDephtArray.setDouble(index, checkValue(v, ((ProfileSubSensor) subSensor).getHeight()));
// check for consistent bin size
if (subSensor instanceof BinProfileSubSensor) {
double binHeight = checkValue(v, ((BinProfileSubSensor) subSensor).getBinHeight());
if (consistentBinHeight == null) {
consistentBinHeight = binHeight;
} else if (consistentBinHeight != getNetcdfHelper().getFillValue() && consistentBinHeight != binHeight) {
// mark bin height as inconsistent
consistentBinHeight = getNetcdfHelper().getFillValue();
}
}
} else {
throw new EncodingException("Non-profile subsensors not supported.");
}
}
return consistentBinHeight;
}
use of org.n52.sos.netcdf.data.dataset.AbstractSensorDataset in project SOS by 52North.
the class AbstractNetcdfEncoder method getLatitudeArray.
protected Array getLatitudeArray(AbstractSensorDataset sensorDataset) throws EncodingException {
if (sensorDataset instanceof StaticLocationDataset) {
StaticLocationDataset locationDataset = (StaticLocationDataset) sensorDataset;
if (locationDataset.getLat() != null) {
Array array = getArray();
initArrayWithFillValue(array, getNetcdfHelper().getFillValue());
Index index = array.getIndex();
index.set(0);
array.setDouble(index, locationDataset.getLat());
}
} else {
// TODO support varying lat
throw new EncodingException("Varying lat are not yet supported.");
}
return null;
}
use of org.n52.sos.netcdf.data.dataset.AbstractSensorDataset in project SOS by 52North.
the class NetcdfEncoder method encodeNetCDFObsToNetcdf.
protected BinaryAttachmentResponse encodeNetCDFObsToNetcdf(List<NetCDFObservation> netCDFObsList, Version version) throws EncodingException {
if (CollectionHelper.isEmptyOrNull(netCDFObsList)) {
throw new EncodingException("No feature types to encode");
} else if (netCDFObsList.size() > 1) {
throwTooManyFeatureTypesOrSensorsException(netCDFObsList, netCDFObsList.size(), null);
}
NetCDFObservation netCDFObservation = netCDFObsList.get(0);
if (CollectionHelper.isEmpty(netCDFObservation.getSensorDatasets())) {
throw new EncodingException("No sensors to encode");
} else if (netCDFObservation.getSensorDatasets().size() > 1) {
throwTooManyFeatureTypesOrSensorsException(netCDFObsList, null, netCDFObservation.getSensorDatasets().size());
}
AbstractSensorDataset sensorDataset = netCDFObservation.getSensorDatasets().get(0);
File tempDir = Files.createTempDir();
String filename = getFilename(sensorDataset);
File netcdfFile = new File(tempDir, filename);
try {
encodeSensorDataToNetcdf(netcdfFile, sensorDataset, version);
return new BinaryAttachmentResponse(Files.toByteArray(netcdfFile), getContentType(), String.format(filename, makeDateSafe(new DateTime(DateTimeZone.UTC))));
} catch (IOException e) {
throw new EncodingException("Couldn't create netCDF file", e);
} finally {
LOGGER.debug("Temporary file deleted: {}", tempDir.delete());
}
}
use of org.n52.sos.netcdf.data.dataset.AbstractSensorDataset in project SOS by 52North.
the class OceanSITESEncoder method encodeNetCDFObsToNetcdf.
@Override
protected BinaryAttachmentResponse encodeNetCDFObsToNetcdf(List<NetCDFObservation> netCDFObsList, Version version) throws EncodingException, IOException {
if (CollectionHelper.isEmptyOrNull(netCDFObsList)) {
throw new EncodingException("No feature types to encode");
} else if (netCDFObsList.size() > 1) {
throwTooManyFeatureTypesOrSensorsException(netCDFObsList, netCDFObsList.size(), null);
}
NetCDFObservation netCDFObservation = netCDFObsList.get(0);
if (CollectionHelper.isEmpty(netCDFObservation.getSensorDatasets())) {
throw new EncodingException("No sensors to encode");
} else if (netCDFObservation.getSensorDatasets().size() > 1) {
throwTooManyFeatureTypesOrSensorsException(netCDFObsList, null, netCDFObservation.getSensorDatasets().size());
}
AbstractSensorDataset sensorDataset = netCDFObservation.getSensorDatasets().get(0);
File tempDir = Files.createTempDir();
String filename = getFilename(sensorDataset);
File netcdfFile = new File(tempDir, filename);
encodeSensorDataToNetcdf(netcdfFile, sensorDataset, version);
BinaryAttachmentResponse response = null;
try {
response = new BinaryAttachmentResponse(Files.toByteArray(netcdfFile), getContentType(), String.format(filename, makeDateSafe(new DateTime(DateTimeZone.UTC))));
} finally {
LOGGER.debug("Temporary file deleted: {}", tempDir.delete());
}
return response;
}
use of org.n52.sos.netcdf.data.dataset.AbstractSensorDataset in project SOS by 52North.
the class AbstractNetcdfEncoder method getFilename.
protected String getFilename(AbstractSensorDataset sensorDataset) throws EncodingException {
List<Time> times = Lists.newArrayList(sensorDataset.getTimes());
Collections.sort(times);
DateTime firstTime = getDateTime(times.get(0));
DateTime lastTime = getDateTime(times.get(times.size() - 1));
StringBuilder pathBuffer = new StringBuilder();
pathBuffer.append(sensorDataset.getSensorIdentifier().replaceAll("http://", "").replaceAll("/", "_"));
pathBuffer.append("_").append(sensorDataset.getFeatureType().name().toLowerCase(Locale.ROOT));
pathBuffer.append("_").append(makeDateSafe(firstTime));
// if (!(sensorDataset instanceof IStaticTimeDataset)) {
pathBuffer.append("_").append(makeDateSafe(lastTime));
// }
pathBuffer.append("_").append(Long.toString(java.lang.System.nanoTime())).append(".nc");
return pathBuffer.toString();
}
Aggregations