<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:gml="http://www.opengis.net/gml/3.2"
  xmlns:swe="http://www.opengis.net/swe/2.0" xmlns:wml2="http://www.opengis.net/waterml/2.0"
  xmlns:om="http://www.opengis.net/om/2.0" targetNamespace="http://www.opengis.net/waterml/2.0"
  elementFormDefault="qualified" attributeFormDefault="unqualified" version="2.0.2">
  <annotation>
    <documentation>This schema describes the timeseries result structures for OGC WaterML2.0. The XML implements the interleaved coverage timeseries
      represetation described in the OGC WaterML2.0 conceptual model. This implementation only covers timeseries of measurements and categorical values
      (string with codespaces). Subsequent versions of schema may support more complex results such as multi-result (compound), vectors etc. 
      
      OGC WaterML 2.0 is an OGC Standard.
      Copyright (c) 2012,2014 Open Geospatial Consortium.
      To obtain additional rights of use, visit http://www.opengeospatial.org/legal/ .
    </documentation>
  </annotation>
  <import namespace="http://www.opengis.net/gml/3.2"
    schemaLocation="http://schemas.opengis.net/gml/3.2.1/gml.xsd"/>
  <import namespace="http://www.opengis.net/swe/2.0"
    schemaLocation="http://schemas.opengis.net/sweCommon/2.0/swe.xsd"/>
  <import namespace="http://www.opengis.net/om/2.0"
    schemaLocation="http://schemas.opengis.net/om/2.0/observation.xsd"/>
  <include schemaLocation="waterml2.xsd"/>

  <!-- ====================================================================== -->
  <!-- ===== Time-value pairs and metadata ======= -->
  <!-- ====================================================================== -->

  <!-- ===== Base level time-value pair metadata ======= -->
  <element name="TVPMetadata" type="wml2:TVPMetadataType"/>
  <complexType name="TVPMetadataType">
    <sequence>
      <element maxOccurs="1" minOccurs="0" name="quality" type="gml:ReferenceType">
        <annotation>
          <documentation>Defines the quality of the point (or the default if it is the defaultTimeValuePair) using an OGC WaterML2.0 quality code. The
            qualifier element should be used for other assertions (e.g. using internal codes). </documentation>
        </annotation>
      </element>
      <element maxOccurs="1" minOccurs="0" name="nilReason" type="gml:ReferenceType">
        <annotation>
          <documentation>Specifies the reason for a null point (xsi:nil='true'). Must be present if the value is null. </documentation>
        </annotation>
      </element>
      <element maxOccurs="1" minOccurs="0" name="comment" type="string">
        <annotation>
          <documentation>Free text comment associated with the data point.</documentation>
        </annotation>
      </element>
      <element maxOccurs="unbounded" minOccurs="0" name="relatedObservation"
        type="om:ObservationContextPropertyType">
        <annotation>
          <documentation>Defines a reference to an observation that is related to this point. E.g. derived from a sample.</documentation>
        </annotation>
      </element>
      <element maxOccurs="unbounded" minOccurs="0" name="qualifier" type="swe:QualityPropertyType">
        <annotation>
          <documentation>A general qualifier for the point to define influencing conditions, or other qualifying information. A broader, more
            flexible location for qualifiers.</documentation>
        </annotation>
      </element>
      <element maxOccurs="1" minOccurs="0" name="processing" type="gml:ReferenceType">
        <annotation>
          <documentation>Defines what processing has occured on the data point using a reference to a code item.</documentation>
        </annotation>
      </element>
      <element maxOccurs="1" minOccurs="0" name="source" type="gml:ReferenceType">
        <annotation>
          <documentation>Defines the source of the value using a reference to an appropriate resource. This is used where the timeseries may be aggregated from multiple sources.his can be an internal link to om:OM_Observation/om:metadata/wml2:ObservationMetadata/gmd:identificationInfo/gmd:MD_DataIdentification</documentation>
        </annotation>
      </element>
    </sequence>
  </complexType>

  <!-- ===== Measurement specific time-value pair metadata ======= -->
  <element name="TVPMeasurementMetadata" substitutionGroup="wml2:TVPMetadata"
    type="wml2:TVPMeasurementMetadataType"/>
  <complexType name="TVPMeasurementMetadataType">
    <complexContent>
      <extension base="wml2:TVPMetadataType">
        <sequence>
          <element name="uom" maxOccurs="1" minOccurs="0" type="swe:UnitReference">
            <annotation>
              <documentation>Defines the default unit of measure across the series. The uom code is specified using the UCUM code. </documentation>
            </annotation>
          </element>
          <element maxOccurs="1" minOccurs="0" name="interpolationType" type="gml:ReferenceType">
            <annotation>
              <documentation>The interpolation type of the time series point. This describes the nature of the value to the time with which it is
                associated. E.g. average in the preceeding interval. The URL codes are available at http://www.opengis.net/def/waterml/2.0/interpolationType/</documentation>
            </annotation>
          </element>
          <element maxOccurs="1" minOccurs="0" name="censoredReason" type="gml:ReferenceType">
            <annotation>
              <documentation>Specifies the reason the value has been censored. e.g. left/right censoring, </documentation>
            </annotation>
          </element>
          <element maxOccurs="1" minOccurs="0" name="accuracy" type="swe:QuantityPropertyType">
            <annotation>
              <documentation>Used to specify the accuracy of a single value. </documentation>
            </annotation>
          </element>
          <element maxOccurs="1" minOccurs="0" name="aggregationDuration" type="duration">
            <annotation>
              <documentation>Describes the temporal aggregation that has occured to the value. </documentation>
            </annotation>
          </element>
        </sequence>
      </extension>
    </complexContent>
  </complexType>

  <complexType name="TVPMetadataPropertyType">
    <sequence>
      <element ref="wml2:TVPMetadata"/>
    </sequence>
    <attributeGroup ref="gml:OwnershipAttributeGroup"/>
  </complexType>

  <complexType name="TVPMeasurementMetadataPropertyType">
    <sequence>
      <element ref="wml2:TVPMeasurementMetadata"/>
    </sequence>
    <attributeGroup ref="gml:OwnershipAttributeGroup"/>
  </complexType>

  <complexType name="TVPDefaultMetadataPropertyType">
    <sequence>
      <element ref="wml2:DefaultTVPMetadata"/>
    </sequence>
    <attributeGroup ref="gml:OwnershipAttributeGroup"/>
  </complexType>

  <!-- ====================================================================== -->
  <!-- ===== Timeseries and timeseries metadata ======= -->
  <!-- ====================================================================== -->
  <element name="Timeseries" substitutionGroup="gml:AbstractFeature" type="wml2:TimeseriesType">
    <annotation>
      <documentation>Describes a time series as a collection of time-value pairs (implements TimeseriesTVP model type). A time series is a also a realisation of a discrete time coverage as
        defined by ISO19123 - Coverages.</documentation>
    </annotation>
  </element>
  <complexType name="TimeseriesType" abstract="true">
    <complexContent>
      <extension base="gml:AbstractFeatureType">
        <sequence>
          <element maxOccurs="1" minOccurs="0" name="metadata"
            type="wml2:TimeseriesMetadataPropertyType"/>
          <element maxOccurs="unbounded" minOccurs="0" name="defaultPointMetadata"
            type="wml2:TVPDefaultMetadataPropertyType"/>
        </sequence>
      </extension>
    </complexContent>
  </complexType>

  <!-- The point element is directly restricted using XML schema types. The metadata
    (wml2:Timeseries/wml2:metadata) is restricted using schematron. This is done to maintain element ordering
    for the metadata element, but schema is used for the point tp provide better integration 
    with XML tools (and it won't affect element ordering. -->
  <element name="MeasurementTimeseries" substitutionGroup="wml2:Timeseries"
    type="wml2:MeasurementTimeseriesType">
    <annotation>
      <documentation>Describes a time series as a collection of time-value pairs where the value-type is a measurement
        and associated metadata is specific to measurement types. </documentation>
    </annotation>
  </element>
  <complexType name="MeasurementTimeseriesType">
    <complexContent>
      <extension base="wml2:TimeseriesType">
        <sequence>
          <element maxOccurs="unbounded" minOccurs="0" name="point">
            <complexType>
              <sequence>
                <element ref="wml2:MeasurementTVP"/>
              </sequence>
            </complexType>
          </element>
        </sequence>
      </extension>
    </complexContent>
  </complexType>

  <element name="CategoricalTimeseries" substitutionGroup="wml2:Timeseries"
    type="wml2:CategoricalTimeseriesType">
    <annotation>
      <documentation>Describes a time series as a collection of time-value pairs where the value-type is a category
        and associated metadata is specific to categorical types. </documentation>
    </annotation>
  </element>
  <complexType name="CategoricalTimeseriesType">
    <complexContent>
      <extension base="wml2:TimeseriesType">
        <sequence minOccurs="0">
          <element maxOccurs="unbounded" minOccurs="0" name="point">
            <complexType>
              <sequence>
                <element ref="wml2:CategoricalTVP"/>
              </sequence>
            </complexType>
          </element>
        </sequence>
      </extension>
    </complexContent>
  </complexType>

  <complexType name="TimeseriesPropertyType">
    <sequence minOccurs="0">
      <element ref="wml2:Timeseries"/>
    </sequence>
    <attributeGroup ref="gml:AssociationAttributeGroup"/>
    <attributeGroup ref="gml:OwnershipAttributeGroup"/>
  </complexType>

  <!-- ===== Base level timeseries metadata ======= -->
  <element name="TimeseriesMetadata" type="wml2:TimeseriesMetadataType"/>
  <complexType name="TimeseriesMetadataType">
    <sequence>
      <element name="temporalExtent" type="gml:TimePeriodPropertyType">
        <annotation>
          <documentation>Defines the start and end times of the represented time series.</documentation>
        </annotation>
      </element>
      <element maxOccurs="1" minOccurs="0" name="baseTime" type="gml:TimePositionType">
        <annotation>
          <documentation>Defines the start time for isochronous (equidistant) time series. Each point occurs at regular steps (defined by the spacing
            property). If baseTime is specified you must supply a spacing element. </documentation>
        </annotation>
      </element>
      <element maxOccurs="1" minOccurs="0" name="spacing" type="duration">
        <annotation>
          <documentation>The spacing between points for isochronous or equidistant (equally space) points. This is used with the baseTime element to allow calculation of time instants.</documentation>
        </annotation>
      </element>
      <element maxOccurs="unbounded" minOccurs="0" name="commentBlock"
        type="wml2:CommentBlockPropertyType">
        <annotation>
          <documentation>A comment that applies over a period of time for the time series.</documentation>
        </annotation>
      </element>
      <element maxOccurs="unbounded" minOccurs="0" name="parameter" type="om:NamedValuePropertyType">
        <annotation>
          <documentation>A named-value extension point, allowing extra metadata to be associated with a timeseries. </documentation>
        </annotation>
      </element>
    </sequence>
  </complexType>

  <complexType name="TimeseriesMetadataPropertyType">
    <sequence>
      <element ref="wml2:TimeseriesMetadata"/>
    </sequence>
    <attributeGroup ref="gml:OwnershipAttributeGroup"/>
  </complexType>

  <!-- ===== Measurement specific timeseries metadata ======= -->
  <element name="MeasurementTimeseriesMetadata" substitutionGroup="wml2:TimeseriesMetadata"
    type="wml2:MeasurementTimeseriesMetadataType"/>
  <complexType name="MeasurementTimeseriesMetadataType">
    <complexContent>
      <extension base="wml2:TimeseriesMetadataType">
        <sequence>
          <element maxOccurs="1" minOccurs="0" name="startAnchorPoint" type="gml:TimePositionType">
            <annotation>
              <documentation>The anchor point for the beginning of the series.</documentation>
            </annotation>
          </element>
          <element maxOccurs="1" minOccurs="0" name="endAnchorPoint" type="gml:TimePositionType">
            <annotation>
              <documentation>The anchor point for the end of the series.</documentation>
            </annotation>
          </element>
          <element maxOccurs="1" minOccurs="0" name="cumulative" type="boolean">
            <annotation>
              <documentation>Sets the time series to be a cumulative series.</documentation>
            </annotation>
          </element>
          <element maxOccurs="1" minOccurs="0" name="accumulationAnchorTime"
            type="gml:TimePositionType">
            <annotation>
              <documentation>Defines the time at which accumulation begins.</documentation>
            </annotation>
          </element>
          <element maxOccurs="1" minOccurs="0" name="accumulationIntervalLength" type="duration">
            <annotation>
              <documentation>Defines the period over which accumulation occurs.</documentation>
            </annotation>
          </element>
          <element maxOccurs="1" minOccurs="0" name="maxGapPeriod" type="duration">
            <annotation>
              <documentation>Indicates the maxiumum period allowed between two series before interpolation is not possible. 
              </documentation>
            </annotation>
          </element>
        </sequence>
      </extension>
    </complexContent>
  </complexType>

  <complexType name="MeasurementTSMetadataPropertyType">
    <sequence>
      <element ref="wml2:MeasurementTimeseriesMetadata"/>
    </sequence>
    <attributeGroup ref="gml:OwnershipAttributeGroup"/>
  </complexType>


  <!-- ===== Default point metadata ======= -->
  <!-- Measurment uses the same TVP types;  -->
  <!-- Categorical uses extended version of base TVP metadata, adding codeSpace -->

  <element name="DefaultTVPMetadata" type="wml2:TVPMetadataType"/>
  <element name="DefaultTVPMeasurementMetadata" substitutionGroup="wml2:DefaultTVPMetadata"
    type="wml2:TVPMeasurementMetadataType"/>
  <element name="DefaultTVPCategoricalMetadata" substitutionGroup="wml2:DefaultTVPMetadata"
    type="wml2:DefaultCategoricalTVPMetadataType"/>

  <complexType name="DefaultCategoricalTVPMetadataType">
    <complexContent>
      <extension base="wml2:TVPMetadataType">
        <sequence>
          <element name="codeSpace" maxOccurs="1" minOccurs="0" type="anyURI">
            <annotation>
              <documentation>Defines the default codeSpace for the category timeseries</documentation>
            </annotation>
          </element>
        </sequence>
      </extension>
    </complexContent>
  </complexType>

  <complexType name="DefaultTVPMetadataPropertyType">
    <sequence minOccurs="0">
      <element ref="wml2:DefaultTVPMetadata"/>
    </sequence>
    <attributeGroup ref="gml:AssociationAttributeGroup"/>
    <attributeGroup ref="gml:OwnershipAttributeGroup"/>
  </complexType>

  <!-- ===== Time value pair base type =======                      -->
  <!-- The value type is not restricted here but by the sub types   -->
  <!-- for the required series types: e..g measures.                -->
  <!-- This type is the head of the substitution group, the subtypes -->
  <!-- define the content.                                          -->
  <element name="TimeValuePair" type="wml2:TimeValuePairType"/>
  <complexType name="TimeValuePairType" abstract="true">
    <sequence>
      <element maxOccurs="1" minOccurs="0" name="time" type="gml:TimePositionType">
        <annotation>
          <documentation>The time instant for the data point.</documentation>
        </annotation>
      </element>
    </sequence>
  </complexType>

  <element name="MeasurementTVP" type="wml2:MeasureTVPType" substitutionGroup="wml2:TimeValuePair"/>
  <complexType name="MeasureTVPType">
    <complexContent>
      <extension base="wml2:TimeValuePairType">
        <sequence>
          <element maxOccurs="1" minOccurs="0" name="value" nillable="true" type="wml2:MeasureType">
            <annotation>
              <documentation>The value of a measurement timeseries is a measure</documentation>
            </annotation>
          </element>
          <element maxOccurs="1" minOccurs="0" name="metadata"
            type="wml2:TVPMeasurementMetadataPropertyType"/>
        </sequence>
      </extension>
    </complexContent>
  </complexType>

  <element name="CategoricalTVP" type="wml2:CategoricalTVPType"
    substitutionGroup="wml2:TimeValuePair"/>
  <complexType name="CategoricalTVPType">
    <complexContent>
      <extension base="wml2:TimeValuePairType">
        <sequence>
          <element maxOccurs="1" minOccurs="0" name="value" nillable="true"
            type="swe:CategoryPropertyType">
            <annotation>
              <documentation>The value type is a category</documentation>
            </annotation>
          </element>
          <element maxOccurs="1" minOccurs="0" name="metadata" type="wml2:TVPMetadataPropertyType"/>
        </sequence>
      </extension>
    </complexContent>
  </complexType>

  <!-- ====== Comment blocks (temporally bounded comments) ======= -->
  <element name="CommentBlock" substitutionGroup="gml:AbstractObject" type="wml2:CommentBlockType"/>
  <complexType name="CommentBlockType">
    <sequence>
      <element name="applicablePeriod" type="gml:TimePeriodPropertyType">
        <annotation>
          <documentation>The period over which the comment applies.</documentation>
        </annotation>
      </element>
      <element name="comment" type="string">
        <annotation>
          <documentation>The comment that applies for the period.</documentation>
        </annotation>
      </element>
    </sequence>
  </complexType>
  <complexType name="CommentBlockPropertyType">
    <sequence>
      <element ref="wml2:CommentBlock"/>
    </sequence>
    <attributeGroup ref="gml:OwnershipAttributeGroup"/>
  </complexType>

  <!-- ======== WML2 definition of measure type ========= -->
  <!--  gml:MeasureType his not used as it has as a mandatory attribute uom which means  -->
  <!--  defaulting for uom may not be used. This type also adds the censorReason for censored values. -->
  <element name="value" type="wml2:MeasureType"/>
  <complexType name="MeasureType">
    <annotation>
      <documentation>gml:MeasureType supports recording an amount encoded as a value of XML Schema double, together with a units of measure indicated
        by an attribute uom, short for "units Of measure". The value of the uom attribute identifies a reference system for the amount, usually a
        ratio or interval scale. OGC WaterML2.0 alters this definition to make uom optional as this value is defaulted for a series.</documentation>
    </annotation>
    <simpleContent>
      <extension base="double">
        <attribute name="uom" type="swe:UomSymbol"/>
        <!-- Nil reason and censored reason are captured in point/metadata -->
        <!-- attribute name="nilReason" type="gml:NilReasonType"/-->
        <!--attribute name="censorReason" type="gml:NilReasonType"/-->
      </extension>
    </simpleContent>
  </complexType>

</schema>
