def test_nowcast_valid_end_time(self, mock_get): client = Client('apikey') lat = 52.446023244274045 lon = 4.819207798979252 timestep = 30 fields = [FIELD_TEMP] start_time = 'now' end_time = '2021-01-14T21:00:00.000Z' response = client.nowcast(lat=lat, lon=lon, fields=fields, timestep=timestep, start_time=start_time, end_time=end_time) self.assertFalse(response.has_error) expected_params = { 'lat': 52.446023244274045, 'lon': 4.819207798979252, 'timestep': 30, 'start_time': 'now', 'unit_system': 'si', 'fields': join_fields(fields), 'end_time': end_time } mock_get.assert_called_with( 'https://api.climacell.co/v3/weather/nowcast', params=expected_params, headers={'apikey': 'apikey'})
def test_hourly_valid_end_time(self, mock_get): client = Client('apikey') lat = 52.446023244274045 lon = 4.819207798979252 fields = [FIELD_TEMP, FIELD_DEW_POINT, FIELD_HUMIDITY] start_time = 'now' end_time = '2021-01-14T21:00:00.000Z' response = client.hourly(lat, lon, fields, start_time, end_time) self.assertFalse(response.has_error) expected_params = { 'lat': 52.446023244274045, 'lon': 4.819207798979252, 'start_time': 'now', 'unit_system': 'si', 'fields': join_fields([FIELD_TEMP, FIELD_DEW_POINT, FIELD_HUMIDITY]), 'end_time': '2021-01-14T21:00:00.000Z', } mock_get.assert_called_with( 'https://api.climacell.co/v3/weather/forecast/hourly', params=expected_params, headers={'apikey': 'apikey'})
def _forecast(self, endpoint, lat, lon, fields, start_time, end_time, units): """ Get a forecast response :param float lat: location latitude :param float lon: location longitude :param list[str] fields: requested data fields :param str start_time: ISO 8601 or 'now' :param str end_time: ISO 8601 or None :param str units: si or us :return: returns an hourly forecast response :rtype: Response """ params = { 'lat': lat, 'lon': lon, 'start_time': start_time, 'unit_system': units, 'fields': join_fields(fields), } if start_time != 'now' and not check_datetime_str(start_time): raise ValueError('Invalid start time provided') if end_time is not None: if not check_datetime_str(end_time): raise ValueError('Invalid end time provided') else: params['end_time'] = end_time response = self._do_request(endpoint, params) return Response(response, fields)
def nowcast(self, lat, lon, fields, timestep, start_time='now', end_time=None, units='si'): """ Get the nowcast forecast with a maximum of 360 minutes out :param float lat: :param float lon: :param list[str] fields: :param int timestep: :param str start_time: :param str end_time: :param str units: :return: returns a nowcast forecast response :rtype: Response """ params = { 'lat': lat, 'lon': lon, 'timestep': timestep, 'start_time': start_time, 'unit_system': units, 'fields': join_fields(fields), } if start_time != 'now' and not check_datetime_str(start_time): raise ValueError('Invalid start time provided') if end_time is not None: if not check_datetime_str(end_time): raise ValueError('Invalid end time provided') else: params['end_time'] = end_time endpoint = '/weather/nowcast' response = self._do_request(endpoint, params) return Response(response, fields)
def test_nowcast(self, mock_get): client = Client('apikey') lat = 52.446023244274045 lon = 4.819207798979252 timestep = 30 fields = [ FIELD_TEMP, FIELD_DEW_POINT, FIELD_HUMIDITY, FIELD_WIND_SPEED, FIELD_WIND_GUST, FIELD_WIND_DIRECTION, FIELD_SUNRISE, FIELD_SUNSET, ] response = client.nowcast(lat=lat, lon=lon, fields=fields, timestep=timestep) measurements = response.get_measurements() expected_params = { 'lat': 52.446023244274045, 'lon': 4.819207798979252, 'timestep': 30, 'start_time': 'now', 'unit_system': 'si', 'fields': join_fields(fields), } mock_get.assert_called_with( 'https://api.climacell.co/v3/weather/nowcast', params=expected_params, headers={'apikey': 'apikey'}) # 13 timesteps, 8 measurements per timestep self.assertEqual(13 * 8, len(measurements))
def test_hourly(self, mock_get): client = Client('apikey') lat = 52.446023244274045 lon = 4.819207798979252 fields = [FIELD_TEMP, FIELD_DEW_POINT, FIELD_HUMIDITY] response = client.hourly(lat=lat, lon=lon, fields=fields) measurements = response.get_measurements() expected_params = { 'lat': 52.446023244274045, 'lon': 4.819207798979252, 'start_time': 'now', 'unit_system': 'si', 'fields': join_fields([FIELD_TEMP, FIELD_DEW_POINT, FIELD_HUMIDITY]), } mock_get.assert_called_with( 'https://api.climacell.co/v3/weather/forecast/hourly', params=expected_params, headers={'apikey': 'apikey'}) self.assertEqual(6, len(measurements))