def test_gsod_weather_cache_wide_date_range(): ws = GSODWeatherSource("722660", 2003, 2012) assert 3653 == len(ws.data) assert 4019 == len(ws.get_temperature_set().fetchall()) global ws_pk assert ws.weather_station_pk == ws_pk
def test_gsod_weather_cache(): ws = GSODWeatherSource("722660", 2012, 2012) assert 366 == len(ws.data) assert 366 == len(ws.get_temperature_set().fetchall()) global ws_pk ws_pk = ws.weather_station_pk
def test_gsod_weather_source(consumption_history_one_summer_electricity,gsod_weather_source): gsod_weather_source = GSODWeatherSource(*gsod_weather_source) consumptions = consumption_history_one_summer_electricity.get("electricity") avg_temps = gsod_weather_source.get_average_temperature(consumptions,"degF") assert abs(avg_temps[0] - 66.3833333333) < EPSILON assert abs(avg_temps[1] - 67.8032258065) < EPSILON assert abs(avg_temps[2] - 74.4451612903) < EPSILON hdds = gsod_weather_source.get_hdd(consumptions,"degF",65) assert abs(hdds[0] - 0.7) < EPSILON assert abs(hdds[1] - 20.4) < EPSILON assert abs(hdds[2] - 0.0) < EPSILON cdds = gsod_weather_source.get_cdd(consumptions,"degF",65) assert abs(cdds[0] - 42.2) < EPSILON assert abs(cdds[1] - 107.3) < EPSILON assert abs(cdds[2] - 292.8) < EPSILON
def get_weather_sources(station): weather_source = GSODWeatherSource(station, 2007, 2015) weather_normal_source = TMY3WeatherSource(station) if weather_source.data == {} or weather_normal_source.data == {}: message = "Insufficient weather data for station {}. Please choose " \ "a different weather station (by selecting a different " \ "zipcode).".format(station) return weather_source, weather_normal_source
def test_gsod_weather_source(periods, gsod_weather_source): gsod_weather_source = GSODWeatherSource(*gsod_weather_source) avg_temps = gsod_weather_source.average_temperature(periods,"degF") assert_allclose(avg_temps, [66.3833,67.803,74.445], rtol=RTOL,atol=ATOL) hdds = gsod_weather_source.hdd(periods,"degF",65) assert_allclose(hdds, [0.7,17.,0.0], rtol=RTOL,atol=ATOL) cdds = gsod_weather_source.cdd(periods,"degF",65) assert_allclose(cdds, [42.2,107.3,292.8], rtol=RTOL,atol=ATOL) hdds_per_day = gsod_weather_source.hdd(periods,"degF",65,per_day=True) assert_allclose(hdds_per_day, [0.023,0.658,0.0], rtol=RTOL,atol=ATOL) cdds_per_day = gsod_weather_source.cdd(periods,"degF",65,per_day=True) assert_allclose(cdds_per_day, [1.406,3.461,9.445], rtol=RTOL,atol=ATOL) json_data = gsod_weather_source.json() assert "station" in json_data assert type(json_data["records"][0]["datetime"]) == str
def test_gsod_weather_source(periods, gsod_weather_source): gsod_weather_source = GSODWeatherSource(*gsod_weather_source) avg_temps = gsod_weather_source.average_temperature(periods, "degF") assert_allclose(avg_temps, [66.3833, 67.803, 74.445], rtol=RTOL, atol=ATOL) hdds = gsod_weather_source.hdd(periods, "degF", 65) assert_allclose(hdds, [0.7, 20.4, 0.0], rtol=RTOL, atol=ATOL) cdds = gsod_weather_source.cdd(periods, "degF", 65) assert_allclose(cdds, [42.2, 107.3, 292.8], rtol=RTOL, atol=ATOL) hdds_per_day = gsod_weather_source.hdd(periods, "degF", 65, per_day=True) assert_allclose(hdds_per_day, [0.023, 0.658, 0.0], rtol=RTOL, atol=ATOL) cdds_per_day = gsod_weather_source.cdd(periods, "degF", 65, per_day=True) assert_allclose(cdds_per_day, [1.406, 3.461, 9.445], rtol=RTOL, atol=ATOL)
def test_gsod_weather_source(consumption_history_one_summer_electricity,gsod_weather_source): gsod_weather_source = GSODWeatherSource(*gsod_weather_source) consumptions = consumption_history_one_summer_electricity.get("electricity") avg_temps = gsod_weather_source.average_temperature(consumptions,"degF") assert_allclose(avg_temps, [66.3833,67.803,74.445], rtol=RTOL,atol=ATOL) hdds = gsod_weather_source.hdd(consumptions,"degF",65) assert_allclose(hdds, [0.7,20.4,0.0], rtol=RTOL,atol=ATOL) cdds = gsod_weather_source.cdd(consumptions,"degF",65) assert_allclose(cdds, [42.2,107.3,292.8], rtol=RTOL,atol=ATOL) hdds_per_day = gsod_weather_source.hdd(consumptions,"degF",65,per_day=True) assert_allclose(hdds_per_day, [0.023,0.658,0.0], rtol=RTOL,atol=ATOL) cdds_per_day = gsod_weather_source.cdd(consumptions,"degF",65,per_day=True) assert_allclose(cdds_per_day, [1.406,3.461,9.445], rtol=RTOL,atol=ATOL)
def __init__(self, location, consumption=[], baseline_period=None, reporting_period=None, other_periods=[], weather_source=None, weather_normal_source=None): self.location = location self.baseline_period = baseline_period self.reporting_period = reporting_period self.other_periods = other_periods if not type(consumption) == list: self.consumption = [consumption] else: self.consumption = consumption if not type(other_periods) == list: self.other_periods = [other_periods] else: self.other_periods = other_periods if location.station is None: message = ( "Unable to create project; no valid weather station was found." ) raise ValueError(message) if weather_source is None: weather_source = GSODWeatherSource(location.station) self.weather_source = weather_source if weather_normal_source is None: weather_normal_source = TMY3WeatherSource(location.station) self.weather_normal_source = weather_normal_source
def test_cache(): cache_dir = tempfile.mkdtemp() ws = GSODWeatherSource('722880', cache_directory=cache_dir) assert "GSOD" in ws.cache_filename assert ".json" in ws.cache_filename assert ws.tempC.shape == (0,) assert ws.tempC.shape == (0,) ws.add_year(2015) assert ws.tempC.shape == (365,) ws.add_year(2013) assert ws.tempC.shape == (365*3,) ws.save_to_cache() # new instance, loaded from full cache ws = GSODWeatherSource('722880', cache_directory=cache_dir) assert ws.tempC.shape == (365*3,) # corrupt the cache with open(ws.cache_filename, 'w') as f: f.seek(1000) f.write("0#2]]]],,,sd,f,\\sf\\f\s34") # shouldn't fail - should just clear the cache ws = GSODWeatherSource('722880', cache_directory=cache_dir) assert ws.tempC.shape == (0,) # new instance, loaded from empty cache ws = GSODWeatherSource('722880', cache_directory=cache_dir) assert ws.tempC.shape == (0,) # cache still empty ws.load_from_cache() assert ws.tempC.shape == (0,) # write an all-null cache file with open(ws.cache_filename, 'w') as f: f.write('[["20110101", null]]') # new instance, loaded from empty cache ws = GSODWeatherSource('722880', cache_directory=cache_dir) assert ws.tempC.shape == (1,)
def generate(self, location, period_elec, period_gas, baseline_period, reporting_period, noise_elec=None, noise_gas=None): """Returns a simple simulated project consisting of generated electricity and gas consumptions and estimated savings for each. Parameters ---------- location : eemeter.location.Location Location of project """ early_date = None late_date = None if not period_elec.closed or not period_gas.closed: message = "Periods of consumption must have start and end." raise ValueError(message) all_periods = [period_elec, period_gas, baseline_period, reporting_period] for period in all_periods: if early_date is None: early_date = period.start if late_date is None: late_date = period.end if period.start is not None and period.start < early_date: early_date = period.start if period.end is not None and late_date < period.end: late_date = period.end weather_source = GSODWeatherSource(location.station,early_date.year, late_date.year) weather_normal_source = TMY3WeatherSource(location.station) cd_elec, est_savings_elec, elec_bl_params, elec_rp_params = \ self._generate_fuel_consumptions( weather_source, weather_normal_source, period_elec, self.electricity_model, self.elec_param_dists, self.elec_param_delta_dists, noise_elec, baseline_period, reporting_period, "electricity", "kWh", self.temperature_unit_name) cd_gas, est_savings_gas, gas_bl_params, gas_rp_params = \ self._generate_fuel_consumptions( weather_source, weather_normal_source, period_gas, self.gas_model, self.gas_param_dists, self.gas_param_delta_dists, noise_gas, baseline_period, reporting_period, "natural_gas", "therm", self.temperature_unit_name) project = Project(location, [cd_elec, cd_gas], baseline_period, reporting_period) results = { "project": project, "electricity_estimated_savings": est_savings_elec, "natural_gas_estimated_savings": est_savings_gas, "electricity_pre_params": elec_bl_params, "natural_gas_pre_params": gas_bl_params, "electricity_post_params": elec_rp_params, "natural_gas_post_params": gas_rp_params, } return results
def test_bad_gsod_station(): with pytest.raises(ValueError): GSODWeatherSource("INVALID")
def mock_gsod_weather_source(): tmp_url = "sqlite:///{}/weather_cache.db".format(tempfile.mkdtemp()) ws = GSODWeatherSource("722880", tmp_url) ws.client = MockWeatherClient() return ws
def mock_gsod_weather_source(): tmp_dir = tempfile.mkdtemp() ws = GSODWeatherSource("722880", tmp_dir) ws.client = MockWeatherClient() return ws
def test_cache(): cache_dir = tempfile.mkdtemp() ws = GSODWeatherSource('722880', cache_directory=cache_dir) assert "GSOD" in ws.cache_filename assert ".json" in ws.cache_filename assert ws.tempC.shape == (0,) assert ws.tempC.shape == (0,) ws.add_year(2015) assert ws.tempC.shape == (365,) ws.add_year(2013) assert ws.tempC.shape == (365*3,) ws.save_to_cache() # new instance, loaded from full cache ws = GSODWeatherSource('722880', cache_directory=cache_dir) assert ws.tempC.shape == (365*3,) ws.clear_cache() # new instance, loaded from empty cache ws = GSODWeatherSource('722880', cache_directory=cache_dir) assert ws.tempC.shape == (0,) # cache still empty ws.load_from_cache() assert ws.tempC.shape == (0,)
def get_example_project(zipcode): # location location = Location(zipcode=zipcode) station = location.station weather_source = GSODWeatherSource(station,2011,2015) # model model_e = AverageDailyTemperatureSensitivityModel(cooling=True, heating=True) model_g = AverageDailyTemperatureSensitivityModel(cooling=False, heating=True) # model params params_e_b = { "cooling_slope": 1, "heating_slope": 1, "base_daily_consumption": 30, "cooling_balance_temperature": 73, "heating_balance_temperature": 68, } params_e_r = { "cooling_slope": .5, "heating_slope": .5, "base_daily_consumption": 15, "cooling_balance_temperature": 73, "heating_balance_temperature": 68, } params_g_b = { "heating_slope": .2, "base_daily_consumption": 2, "heating_balance_temperature": 68, } params_g_r = { "heating_slope": .1, "base_daily_consumption": 1, "heating_balance_temperature": 68, } #generators gen_e_b = MonthlyBillingConsumptionGenerator("electricity", "kWh", "degF", model_e, params_e_b) gen_e_r = MonthlyBillingConsumptionGenerator("electricity", "kWh", "degF", model_e, params_e_r) gen_g_b = MonthlyBillingConsumptionGenerator("natural_gas", "therm", "degF", model_g, params_g_b) gen_g_r = MonthlyBillingConsumptionGenerator("natural_gas", "therm", "degF", model_g, params_g_r) # time periods period = Period(datetime(2011,1,1,tzinfo=pytz.utc), datetime(2015,1,1,tzinfo=pytz.utc)) datetimes = generate_monthly_billing_datetimes(period, dist=randint(30,31)) # consumption data cd_e_b = gen_e_b.generate(weather_source, datetimes, daily_noise_dist=None) cd_e_r = gen_e_r.generate(weather_source, datetimes, daily_noise_dist=None) cd_g_b = gen_g_b.generate(weather_source, datetimes, daily_noise_dist=None) cd_g_r = gen_g_r.generate(weather_source, datetimes, daily_noise_dist=None) # periods periods = cd_e_b.periods() reporting_period = Period(datetime(2013,1,1,tzinfo=pytz.utc), datetime(2015,1,1,tzinfo=pytz.utc)) baseline_period = Period(datetime(2011,1,1,tzinfo=pytz.utc), datetime(2013,1,1,tzinfo=pytz.utc)) # records records_e = [] records_g = [] for e_b, e_r, g_b, g_r, p in zip(cd_e_b.data, cd_e_r.data, cd_g_b.data, cd_g_r.data, periods): e = e_r if p in reporting_period else e_b g = g_r if p in reporting_period else g_b record_e = {"start": p.start, "end": p.end, "value": e} record_g = {"start": p.start, "end": p.end, "value": g} records_e.append(record_e) records_g.append(record_g) # consumption_data cd_e = ConsumptionData(records_e, "electricity", "kWh", record_type="arbitrary") cd_g = ConsumptionData(records_g, "natural_gas", "therm", record_type="arbitrary") consumptions = [cd_e, cd_g] # project project = Project(location, consumptions, baseline_period, reporting_period) return project
def gsod_722880_2012_2014_weather_source(): ws = GSODWeatherSource("722880") ws.add_year_range(2012, 2014) return ws