Skip to content

Time series data from Point locations does not retain station information #197

@Intergalactyc

Description

@Intergalactyc

Sorry, I accidentally closed this (was issue 196) when trying to reply to it. Unsure how I managed to do that haha.

The issue

When obtaining data given a Point location the stations used to obtain that data are not retained in the data object.

Minimal example

from meteostat import Point, Daily # Same for Monthly or Hourly as for Daily (any TimeSeries-based object)
from datetime import datetime

loc = Point(41.91, -91.65)
data = Daily(loc, datetime(2016,1,1), datetime(2017,1,1))
print(data.stations)

Printed: Index(['XXXXX'], dtype='object')
Expected: Index(['72545', 'KIOW0'], dtype='string', name='id')

Indeed, loc.stations stores this expected stations index after using loc to construct data as above, but expected behavior is that these stations remain associated with the data as well.

Source code review

In timeseries.py, stations for a TimeSeries are set in _init_time_series with

        if isinstance(loc, pd.DataFrame):
            self._stations = loc.index
        elif isinstance(loc, Point):
            stations = loc.get_stations("daily", start, end, model)
            self._stations = stations.index
        else:
            if not isinstance(loc, list):
                loc = [loc]
            self._stations = pd.Index(loc)

However, if the location is a Point, this follows in the method:

        # Interpolate data spatially if requested
        # location is a geographical point
        if isinstance(loc, Point):
            self._resolve_point(loc.method, stations, loc.alt, loc.adapt_temp)

In meteodata.py, the method MeteoData._resolve_point which is called in the lines above concludes with

        # Set station index
        self._stations = pd.Index(["XXXXX"])

which overwrites the preexisting stations. This is then never updated. Because of this, the station(s) used to create the time series are lost. Commenting out this line of meteodata.py does revert behavior to expectation.

(I'm happy to work on a PR for this but I didn't want to cause any issues by removing what I see as the offending lines from MeteoData._resolve_point without first checking. Is there a good reason for setting the station to "XXXXX" in the first place?)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions