All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.n52.sos.netcdf.NetCDFUtil Maven / Gradle / Ivy

There is a newer version: 6.0.1
Show newest version
/*
 * Copyright (C) 2012-2021 52°North Initiative for Geospatial Open Source
 * Software GmbH
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published
 * by the Free Software Foundation.
 *
 * If the program is linked with libraries which are licensed under one of
 * the following licenses, the combination of the program with the linked
 * library is not considered a "derivative work" of the program:
 *
 *     - Apache License, version 2.0
 *     - Apache Software License, version 1.0
 *     - GNU Lesser General Public License, version 3
 *     - Mozilla Public License, versions 1.0, 1.1 and 2.0
 *     - Common Development and Distribution License (CDDL), version 1.0
 *
 * Therefore the distribution of the program linked with libraries licensed
 * under the aforementioned licenses, is permitted by the copyright holders
 * if the distribution is compliant with both the GNU General Public
 * License version 2 and the aforementioned licenses.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 * Public License for more details.
 */
package org.n52.sos.netcdf;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

import org.locationtech.jts.geom.Envelope;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.LineString;
import org.locationtech.jts.geom.Point;
import org.n52.shetland.ogc.gml.AbstractFeature;
import org.n52.shetland.ogc.gml.time.Time;
import org.n52.shetland.ogc.gml.time.TimePeriod;
import org.n52.shetland.ogc.om.AbstractPhenomenon;
import org.n52.shetland.ogc.om.NamedValue;
import org.n52.shetland.ogc.om.ObservationStream;
import org.n52.shetland.ogc.om.ObservationValue;
import org.n52.shetland.ogc.om.OmCompositePhenomenon;
import org.n52.shetland.ogc.om.OmConstants;
import org.n52.shetland.ogc.om.OmObservableProperty;
import org.n52.shetland.ogc.om.OmObservation;
import org.n52.shetland.ogc.om.OmObservationConstellation;
import org.n52.shetland.ogc.om.SingleObservationValue;
import org.n52.shetland.ogc.om.StreamingValue;
import org.n52.shetland.ogc.om.features.samplingFeatures.AbstractSamplingFeature;
import org.n52.shetland.ogc.om.values.GeometryValue;
import org.n52.shetland.ogc.om.values.QuantityValue;
import org.n52.shetland.ogc.om.values.Value;
import org.n52.shetland.ogc.ows.exception.OwsExceptionReport;
import org.n52.sos.netcdf.data.dataset.IdentifierDatasetSensor;
import org.n52.sos.netcdf.data.dataset.TimeSeriesProfileSensorDataset;
import org.n52.sos.netcdf.data.dataset.TimeSeriesSensorDataset;
import org.n52.sos.netcdf.data.dataset.TrajectoryProfileSensorDataset;
import org.n52.sos.netcdf.data.dataset.TrajectorySensorDataset;
import org.n52.sos.netcdf.data.subsensor.BinProfileSubSensor;
import org.n52.sos.netcdf.data.subsensor.PointProfileSubSensor;
import org.n52.sos.netcdf.data.subsensor.SubSensor;
import org.n52.sos.netcdf.feature.FeatureUtil;
import org.n52.sos.netcdf.om.NetCDFObservation;
import org.n52.sos.util.GeometryHandler;
import org.n52.svalbard.encode.exception.EncodingException;

import com.google.common.collect.HashMultimap;
import com.google.common.collect.Maps;
import com.google.common.collect.SetMultimap;

import ucar.nc2.constants.CF;

/**
 * Utility class for netCDF encoding.
 *
 * @author Shane StClair
 * @author Carsten Hollmann
 * @since 4.4.0
 *
 */
public interface NetCDFUtil {

    GeometryHandler getGeometryHandler();

    NetcdfHelper getNetcdfHelper();

    /**
     * Organizes OmObservation collection into a list of NetCDFObservation
     * blocks, each of which contain a single feature type
     *
     * @param omObservations
     *            The collection of observations to transform
     * @return List<NetCDFObservation> ready for encoding
     * @throws EncodingException
     *             if an error occurs
     */
    default List createNetCDFSosObservations(ObservationStream omObservations)
            throws EncodingException, OwsExceptionReport {
        // the main map of observation value strings by asset, time, phenomenon,
        // and subsensor (height, profile bin, etc)
        Map>>>> obsValuesMap = new HashMap<>();

        SetMultimap sensorPhens = HashMultimap.create();
        Map sensorProcedure = Maps.newHashMap();
        // Map stationPeriodMap = new
        // HashMap();

        // maps to keep track of unique dimension values by sensor (these may or
        // may not vary, determining the feature type)
        SetMultimap sensorLngs = HashMultimap.create();
        SetMultimap sensorLats = HashMultimap.create();
        SetMultimap sensorHeights = HashMultimap.create();

        while (omObservations.hasNext()) {
            OmObservation sosObs = omObservations.next();
            if (sosObs.getValue() instanceof StreamingValue) {
                StreamingValue streaming = (StreamingValue) sosObs.getValue();
                while (streaming.hasNext()) {
                    processObservation(streaming.next(), sensorPhens, sensorProcedure, sensorLngs, sensorLats,
                            sensorHeights, obsValuesMap);
                }
            } else {
                processObservation(sosObs, sensorPhens, sensorProcedure, sensorLngs, sensorLats, sensorHeights,
                        obsValuesMap);
            }
        }

        // now we know about each station's dimensions, sort into CF feature
        // types

        // sampling time periods
        // TimePeriod pointSamplingTimePeriod = new TimePeriod();
        TimePeriod timeSeriesSamplingTimePeriod = new TimePeriod();
        // TimePeriod profileSamplingTimePeriod = new TimePeriod();
        TimePeriod timeSeriesProfileSamplingTimePeriod = new TimePeriod();
        TimePeriod trajectorySamplingTimePeriod = new TimePeriod();
        TimePeriod trajectoryProfileSamplingTimePeriod = new TimePeriod();

        // station datasets
        // Map pointSensorDatasets =
        // new HashMap();
        Map timeSeriesSensorDatasets = new HashMap<>();
        // Map profileSensorDatasets =
        // new HashMap();
        Map timeSeriesProfileSensorDatasets = new HashMap<>();
        Map trajectorySensorDatasets = new HashMap<>();
        Map trajectoryProfileSensorDatasets = new HashMap<>();

        // phenomena
        // Set pointPhenomena = new
        // HashSet();
        Set timeSeriesPhenomena = new HashSet<>();
        // Set profilePhenomena = new
        // HashSet();
        Set timeSeriesProfilePhenomena = new HashSet<>();
        Set trajectoryPhenomena = new HashSet<>();
        Set trajectoryProfilePhenomena = new HashSet<>();

        // envelopes
        // Envelope pointEnvelope = new Envelope();
        Envelope timeSeriesEnvelope = new Envelope();
        // Envelope profileEnvelope = new Envelope();
        Envelope timeSeriesProfileEnvelope = new Envelope();
        Envelope trajectoryEnvelope = new Envelope();
        Envelope trajectoryProfileEnvelope = new Envelope();

        for (Map.Entry>>>> obsValuesEntry : obsValuesMap
                .entrySet()) {
            IdentifierDatasetSensor datasetSensor = new IdentifierDatasetSensor(obsValuesEntry.getKey());
            String sensor = datasetSensor.getSensorIdentifier();
            Set




© 2015 - 2024 Weber Informatics LLC | Privacy Policy