Ejemplo n.º 1
0
    def get_weather(
        self,
        coords: List[GeoPosition],
        begin: datetime,
        end: datetime,
        weather_factors: List[str] = None,
    ) -> xr.Dataset:
        """
            The function that gathers and processes the requested ERA5 Single Levels weather data from the repository
            and returns it as an Xarray Dataset.
        Args:
            coords:             A list of GeoPositions containing the locations the data is requested for.
            begin:              A datetime containing the start of the period to request data for.
            end:                A datetime containing the end of the period to request data for.
            weather_factors:    A list of weather factors to request data for (in string format)
        Returns:
            An Xarray Dataset containing the weather data for the requested period, locations and factors.
        """

        # Test and account for invalid datetime timeframes or input
        begin, end = validate_begin_and_end(
            begin,
            end,
            self.repository.first_day_of_repo,
            self.repository.last_day_of_repo,
        )

        # Validate the requested weather factors:
        validated_factors = self._validate_weather_factors(weather_factors)

        ds = self._fill_dataset_with_data(coords, begin, end, validated_factors)
        return ds
    def get_weather(
        self,
        coords: List[GeoPosition],
        begin: Optional[datetime],
        end: Optional[datetime],
        weather_factors: List[str] = None,
    ) -> xr.Dataset:
        """
            The function that gathers and processes the requested Harmonie Arome weather data from the KNMI site
            and returns it as an Xarray Dataset.
            (This model uses the KNMI Data Platform for data acquisition using a repository)
        Args:
            coords:             A list of GeoPositions containing the locations the data is requested for.
            begin:              A datetime containing the start of the period to request data for.
            end:                A datetime containing the end of the period to request data for.
            weather_factors:    A list of weather factors to request data for (in string format)
        Returns:
            An Xarray Dataset containing the weather data for the requested period, locations and factors.
        """
        # Test and account for invalid datetime timeframes or input
        begin, end = validate_begin_and_end(
            begin, end,
            datetime.utcnow() - relativedelta(years=1), datetime.utcnow())

        arome_repo = AromeRepository()
        ds = arome_repo.gather_period(begin, end, coords)
        print("BEGIN, END ==", begin, end)
        ds = ds.sel(prediction_moment=slice(
            begin, end))  # Slice of any overflowing time-range

        return ds
    def get_weather(
        self,
        coords: List[GeoPosition],
        begin: datetime,
        end: datetime,
        inseason=False,
        weather_factors: List[str] = None,
    ) -> xr.Dataset:
        """
            The function that gathers and processes the requested Daggegevens weather data from the KNMI site
            and returns it as an Xarray Dataset.
            (Though this model downloads from a specific download url, the question remains whether this source is also
            listed on the new KNMI Data Platform)
        Args:
            coords:             A list of GeoPositions containing the locations the data is requested for.
            begin:              A datetime containing the start of the period to request data for.
            end:                A datetime containing the end of the period to request data for.
            inseason:           A boolean representing the "inseason" parameter
            weather_factors:    A list of weather factors to request data for (in string format)
        Returns:
            An Xarray Dataset containing the weather data for the requested period, locations and factors.
        """
        # Test and account for invalid datetime timeframes or input
        begin, end = validate_begin_and_end(
            begin, end, None, datetime.utcnow() - relativedelta(days=1)
        )
        # Get a list of the relevant STNs and choose the closest STN for each coordinate
        station_id, stns, coords_stn_ind = find_closest_stn_list(
            stations_history, coords
        )

        # Download the weather data for the relevant STNs
        raw_data = self._download_weather(
            stations=stns,
            start=begin,
            end=end,
            inseason=inseason,
            weather_factors=weather_factors,
        )

        # Parse the raw data into a Dataset
        raw_ds = self._parse_raw_weather_data(raw_data)

        # Prepare and format the weather data for output
        ds = self._prepare_weather_data(coords, station_id, raw_ds)

        # The KNMI model isn't working properly yet, so we have to cut out any overflow time-wise..
        ds = ds.sel(time=slice(begin, end))
        return ds
    def get_weather(
        self,
        coords: List[GeoPosition],
        begin: datetime,
        end: datetime,
        weather_factors: List[str] = None,
        **_kwargs,
    ) -> xr.Dataset:
        """
            The function that gathers and processes the requested Daggegevens weather data from the KNMI site
            and returns it as an Xarray Dataset.
            (Though this model downloads from a specific download url, the question remains whether this source is also
            listed on the new KNMI Data Platform)
        Args:
            coords:             A list of GeoPositions containing the locations the data is requested for.
            begin:              A datetime containing the start of the period to request data for.
            end:                A datetime containing the end of the period to request data for.
            inseason:           A boolean representing the "inseason" parameter
            weather_factors:    A list of weather factors to request data for (in string format)
        Returns:
            An Xarray Dataset containing the weather data for the requested period, locations and factors.
        """

        # Test and account for invalid datetime timeframes or input
        begin, end = validate_begin_and_end(
            begin, end, datetime.utcnow(), datetime.utcnow() + relativedelta(days=15)
        )

        # get list of relevant STNs, choose closest STN
        coords_stn, stns, coords_stn_ind = find_closest_stn_list(
            stations_prediction, coords
        )

        # load default weather factors if unspecified
        if weather_factors is None:
            weather_factors = self.to_si.keys()

        # download the weather factors for all stations
        ds = self._download_weather(coords, coords_stn_ind, stns, weather_factors)

        ds = self._select_weather_from_given_period(ds, begin, end)
        ds = ds.dropna("time", "all")  # Dropping any times that only carry NaN values

        return ds
 def _select_weather_from_given_period(
     ds: xr.Dataset, begin: datetime, end: datetime
 ):
     """
         A function that filters the given Xarray Dataset to only the requested period.
     Args:
         ds:     An Xarray Dataset to be filtered
         begin:  A datetime containing the start of the period to filter.
         end:    A datetime containing the end of the period to filter.
     Returns:
         An Xarray Dataset containing all of the data from the original dataset that matches the given period.
     """
     begin, end = validate_begin_and_end(
         begin,
         end,
         datetime.today().replace(hour=0, minute=0, second=0),
         datetime.today().replace(hour=0, minute=0, second=0)
         + relativedelta(days=15),
     )
     ds = ds.sel(time=slice(begin, end))
     return ds
Ejemplo n.º 6
0
def test_validate_begin_and_end(starting_date, ending_date, result):
    # Testing assumes a repo starting date of 2019-03-03 and an ending date of 2020-08-08
    assert dh.validate_begin_and_end(starting_date, ending_date,
                                     datetime(2019, 3, 3),
                                     datetime(2020, 8, 8)) == result