|
| 1 | +"""Test gen_epw with resampling functionality (GitHub issue #150).""" |
| 2 | + |
| 3 | +import pandas as pd |
| 4 | +import numpy as np |
| 5 | +import pytest |
| 6 | +import supy as sp |
| 7 | + |
| 8 | + |
| 9 | +class TestGenEpwResample: |
| 10 | + """Tests for gen_epw with frequency parameter and resample_output exposure.""" |
| 11 | + |
| 12 | + def test_resample_output_in_util(self): |
| 13 | + """Test that resample_output is accessible via supy.util.""" |
| 14 | + assert hasattr(sp.util, "resample_output") |
| 15 | + |
| 16 | + # Test it works with actual data |
| 17 | + df_state_init, df_forcing = sp.load_SampleData() |
| 18 | + df_output, _ = sp.run_supy(df_forcing.iloc[:48], df_state_init) |
| 19 | + |
| 20 | + df_hourly = sp.util.resample_output(df_output, freq="h") |
| 21 | + |
| 22 | + # Should have fewer rows after resampling |
| 23 | + assert len(df_hourly) < len(df_output) |
| 24 | + |
| 25 | + # Structure should be preserved |
| 26 | + assert isinstance(df_hourly.index, pd.MultiIndex) |
| 27 | + assert "grid" in df_hourly.index.names |
| 28 | + |
| 29 | + def test_resample_output_frequency_aliases(self): |
| 30 | + """Test that different frequency aliases work correctly.""" |
| 31 | + df_state_init, df_forcing = sp.load_SampleData() |
| 32 | + df_output, _ = sp.run_supy(df_forcing.iloc[:144], df_state_init) # 12 hours |
| 33 | + |
| 34 | + # Test various frequency aliases |
| 35 | + for freq in ["30min", "60min", "h", "1h"]: |
| 36 | + df_resampled = sp.util.resample_output(df_output, freq=freq) |
| 37 | + assert len(df_resampled) > 0 |
| 38 | + |
| 39 | + # Hourly should have fewer rows than 30-minute |
| 40 | + df_30min = sp.util.resample_output(df_output, freq="30min") |
| 41 | + df_hourly = sp.util.resample_output(df_output, freq="h") |
| 42 | + assert len(df_hourly) < len(df_30min) |
| 43 | + |
| 44 | + def test_resample_aggregation_methods(self): |
| 45 | + """Test that aggregation methods are applied correctly.""" |
| 46 | + df_state_init, df_forcing = sp.load_SampleData() |
| 47 | + df_output, _ = sp.run_supy(df_forcing.iloc[:288], df_state_init) # 1 day |
| 48 | + |
| 49 | + df_hourly = sp.util.resample_output(df_output, freq="h") |
| 50 | + |
| 51 | + # Get first grid |
| 52 | + grid = df_hourly.index.get_level_values("grid")[0] |
| 53 | + |
| 54 | + # Check that SUEWS variables exist |
| 55 | + assert "SUEWS" in df_hourly.columns.get_level_values("group").unique() |
| 56 | + |
| 57 | + # Variables used by gen_epw should be present |
| 58 | + suews_vars = df_hourly.loc[grid, "SUEWS"].columns.tolist() |
| 59 | + for var in ["T2", "RH2", "U10", "Kdown"]: |
| 60 | + assert var in suews_vars, f"Variable {var} not found in resampled output" |
| 61 | + |
| 62 | + |
| 63 | +class TestGenEpwMultiIndexInput: |
| 64 | + """Tests for gen_epw handling MultiIndex input directly.""" |
| 65 | + |
| 66 | + @pytest.fixture |
| 67 | + def sample_output(self): |
| 68 | + """Create sample SUEWS output for testing.""" |
| 69 | + df_state_init, df_forcing = sp.load_SampleData() |
| 70 | + # Use enough data for meaningful test but not too much |
| 71 | + df_output, _ = sp.run_supy(df_forcing.iloc[:288], df_state_init) # 1 day |
| 72 | + return df_output |
| 73 | + |
| 74 | + def test_gen_epw_accepts_multiindex(self, sample_output, tmp_path): |
| 75 | + """Test that gen_epw accepts MultiIndex input without freq.""" |
| 76 | + grid = sample_output.index.get_level_values("grid")[0] |
| 77 | + |
| 78 | + try: |
| 79 | + # This should work - providing extracted data (original behaviour) |
| 80 | + df_epw, meta, path = sp.util.gen_epw( |
| 81 | + sample_output.loc[grid, "SUEWS"], |
| 82 | + lat=51.5, |
| 83 | + lon=-0.1, |
| 84 | + path_epw=tmp_path / "test.epw", |
| 85 | + ) |
| 86 | + assert isinstance(df_epw, pd.DataFrame) |
| 87 | + except ImportError: |
| 88 | + pytest.skip("pvlib not installed") |
| 89 | + |
| 90 | + def test_gen_epw_with_grid_extraction(self, sample_output, tmp_path): |
| 91 | + """Test that gen_epw extracts grid automatically from MultiIndex.""" |
| 92 | + try: |
| 93 | + # This should auto-extract first grid |
| 94 | + df_epw, meta, path = sp.util.gen_epw( |
| 95 | + sample_output, # Full MultiIndex |
| 96 | + lat=51.5, |
| 97 | + lon=-0.1, |
| 98 | + path_epw=tmp_path / "test_auto.epw", |
| 99 | + ) |
| 100 | + assert isinstance(df_epw, pd.DataFrame) |
| 101 | + except ImportError: |
| 102 | + pytest.skip("pvlib not installed") |
| 103 | + |
| 104 | + def test_gen_epw_with_freq_param(self, sample_output, tmp_path): |
| 105 | + """Test that gen_epw accepts freq parameter for resampling.""" |
| 106 | + try: |
| 107 | + df_epw, meta, path = sp.util.gen_epw( |
| 108 | + sample_output, # Full MultiIndex |
| 109 | + lat=51.5, |
| 110 | + lon=-0.1, |
| 111 | + freq="h", # Resample to hourly |
| 112 | + path_epw=tmp_path / "test_freq.epw", |
| 113 | + ) |
| 114 | + assert isinstance(df_epw, pd.DataFrame) |
| 115 | + except ImportError: |
| 116 | + pytest.skip("pvlib not installed") |
| 117 | + |
| 118 | + def test_gen_epw_with_specific_grid(self, sample_output, tmp_path): |
| 119 | + """Test that gen_epw accepts specific grid parameter.""" |
| 120 | + grid = sample_output.index.get_level_values("grid")[0] |
| 121 | + |
| 122 | + try: |
| 123 | + df_epw, meta, path = sp.util.gen_epw( |
| 124 | + sample_output, |
| 125 | + lat=51.5, |
| 126 | + lon=-0.1, |
| 127 | + grid=grid, # Specify grid explicitly |
| 128 | + path_epw=tmp_path / "test_grid.epw", |
| 129 | + ) |
| 130 | + assert isinstance(df_epw, pd.DataFrame) |
| 131 | + except ImportError: |
| 132 | + pytest.skip("pvlib not installed") |
| 133 | + |
| 134 | + def test_gen_epw_freq_with_extracted_data(self, sample_output, tmp_path): |
| 135 | + """Test that freq works with pre-extracted single-grid data.""" |
| 136 | + grid = sample_output.index.get_level_values("grid")[0] |
| 137 | + df_single = sample_output.loc[grid, "SUEWS"] |
| 138 | + |
| 139 | + try: |
| 140 | + df_epw, meta, path = sp.util.gen_epw( |
| 141 | + df_single, |
| 142 | + lat=51.5, |
| 143 | + lon=-0.1, |
| 144 | + freq="h", # Should still work with simple resampling |
| 145 | + path_epw=tmp_path / "test_extracted_freq.epw", |
| 146 | + ) |
| 147 | + assert isinstance(df_epw, pd.DataFrame) |
| 148 | + except ImportError: |
| 149 | + pytest.skip("pvlib not installed") |
0 commit comments