Search in sources :

Example 1 with AbstractSensorDataset

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;
}
Also used : BinProfileSubSensor(org.n52.sos.netcdf.data.subsensor.BinProfileSubSensor) EncodingException(org.n52.svalbard.encode.exception.EncodingException) Index(ucar.ma2.Index) ProfileSubSensor(org.n52.sos.netcdf.data.subsensor.ProfileSubSensor) SubSensor(org.n52.sos.netcdf.data.subsensor.SubSensor) BinProfileSubSensor(org.n52.sos.netcdf.data.subsensor.BinProfileSubSensor) ArrayDouble(ucar.ma2.ArrayDouble) ProfileSubSensor(org.n52.sos.netcdf.data.subsensor.ProfileSubSensor) BinProfileSubSensor(org.n52.sos.netcdf.data.subsensor.BinProfileSubSensor)

Example 2 with AbstractSensorDataset

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;
}
Also used : Array(ucar.ma2.Array) StaticLocationDataset(org.n52.sos.netcdf.data.dataset.StaticLocationDataset) EncodingException(org.n52.svalbard.encode.exception.EncodingException) Index(ucar.ma2.Index)

Example 3 with AbstractSensorDataset

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());
    }
}
Also used : EncodingException(org.n52.svalbard.encode.exception.EncodingException) BinaryAttachmentResponse(org.n52.shetland.ogc.sos.response.BinaryAttachmentResponse) AbstractSensorDataset(org.n52.sos.netcdf.data.dataset.AbstractSensorDataset) IOException(java.io.IOException) File(java.io.File) DateTime(org.joda.time.DateTime) NetCDFObservation(org.n52.sos.netcdf.om.NetCDFObservation)

Example 4 with AbstractSensorDataset

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;
}
Also used : EncodingException(org.n52.svalbard.encode.exception.EncodingException) BinaryAttachmentResponse(org.n52.shetland.ogc.sos.response.BinaryAttachmentResponse) AbstractSensorDataset(org.n52.sos.netcdf.data.dataset.AbstractSensorDataset) File(java.io.File) DateTime(org.joda.time.DateTime) NetCDFObservation(org.n52.sos.netcdf.om.NetCDFObservation)

Example 5 with AbstractSensorDataset

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();
}
Also used : Time(org.n52.shetland.ogc.gml.time.Time) DateTime(org.joda.time.DateTime) DateTime(org.joda.time.DateTime)

Aggregations

DateTime (org.joda.time.DateTime)8 EncodingException (org.n52.svalbard.encode.exception.EncodingException)8 File (java.io.File)4 Time (org.n52.shetland.ogc.gml.time.Time)4 BinaryAttachmentResponse (org.n52.shetland.ogc.sos.response.BinaryAttachmentResponse)4 AbstractSensorDataset (org.n52.sos.netcdf.data.dataset.AbstractSensorDataset)4 StaticLocationDataset (org.n52.sos.netcdf.data.dataset.StaticLocationDataset)4 NetCDFObservation (org.n52.sos.netcdf.om.NetCDFObservation)4 Index (ucar.ma2.Index)4 Array (ucar.ma2.Array)3 Attribute (ucar.nc2.Attribute)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 IOException (java.io.IOException)2 BinProfileSubSensor (org.n52.sos.netcdf.data.subsensor.BinProfileSubSensor)2 ProfileSubSensor (org.n52.sos.netcdf.data.subsensor.ProfileSubSensor)2 SubSensor (org.n52.sos.netcdf.data.subsensor.SubSensor)2 ArrayDouble (ucar.ma2.ArrayDouble)2 Map (java.util.Map)1 OmObservableProperty (org.n52.shetland.ogc.om.OmObservableProperty)1 Value (org.n52.shetland.ogc.om.values.Value)1