Beispiel #1
0
    def add_timeseries_instance(self, identifier, freq, initial_metadata,
                                **kwargs):
        """
            Define an instance of a timeseries.

            A timeseries instance is a combination of a timeseries, frequency and attributes.

            :param identifier: Identifier of the timeseries.
            :type identifier: string
            :param freq: Data frequency (e.g. 'D' for day, as supported by pandas.)
            :type freq: string
            :param initial_metadata: Store some metadata about this series.
                Potentially freeform header from a source file about to be loaded.
            :type initial_metadata: string
            :param \*\*kwargs: Any additional attributes to attach to the timeseries instance.
            :type \*\*kwargs: kwargs
        """
        session = Session()

        timeseries = self.__get_record_by_id(identifier, session)

        attributes = self.__parse_attribute_kwargs(session=session, **kwargs)

        query = session.query(TimeseriesInstance). \
                filter_by(timeseries=timeseries, freq=freq, **attributes)
        try:
            record = query.one()
            session.rollback()
            raise DuplicateError('TimeseriesInstance for ({:}) already exists.'. \
                    format(identifier, **kwargs))
        except NoResultFound as e:
            # No result is good, we can now create a ts instance.
            pass

        with session.no_autoflush:
            tsi = TimeseriesInstance(initial_metadata=initial_metadata)
            tsi.measurand = attributes['measurand']
            tsi.source = attributes['source']
            tsi.freq = freq
            tsi.uuid = uuid.uuid4().hex
            timeseries.ts_instances.append(tsi)

            session.add(tsi)
            try:
                session.commit()
            except IntegrityError:
                raise DuplicateError(
                    "Timeseries instance already exists: '{0}', '{1}'".format(
                        identifier, freq))
Beispiel #2
0
    def add_measurand(self, measurand_short_id, measurand_long_id,
                      description):
        """
            Create a measurand entry.

            Measurand being a measurable timeseries type. e.g. Streamflow, Temperature, Rainfall, etc.

            :param measurand_short_id: Short identifier of the measurand.
            :type measurand_short_id: string
            :param measurand_long_id: Long identifier of the measurand.
            :type measurand_long_id: string
            :param description: Description of the measurand.
            :type description: string
        """
        short_id = measurand_short_id.strip()
        long_id = measurand_long_id.strip()
        session = Session()
        measurand = Measurand(short_id=short_id,
                              long_id=long_id,
                              description=description)
        session.add(measurand)
        try:
            session.commit()
        except IntegrityError:
            raise DuplicateError(
                "Already exists: '{0}'".format(measurand_short_id))
Beispiel #3
0
    def add_timeseries(self, identifier):
        """
            Create a timeseries entry to be identified by the supplied ID.

            :param identifier: Identifier of the timeseries.
            :type identifier: string
        """
        the_id = identifier.strip()
        session = Session()
        ts = Timeseries(primary_id=identifier)
        session.add(ts)
        try:
            session.commit()
        except IntegrityError:
            raise DuplicateError("Already exists: '{0}'".format(identifier))
Beispiel #4
0
    def add_source(self, source, description):
        """
            Define a source.

            Source being the origin of the data. For example the source used in
            the examples/hrs example is BOM_HRS. Indicated the origin of the
            data was the Bureau of Metorology Hydrologic Reference Stations
            project.

            :param source: Identifier of the source.
            :type source: string
            :param description: Description of the source.
            :type description: string
        """
        short_id = source.strip()
        session = Session()
        source = Source(short_id=short_id, description=description)
        session.add(source)
        try:
            session.commit()
        except IntegrityError:
            raise DuplicateError("Already exists: '{0}'".format(source))