Skip to content

Commit

Permalink
Merge branch 'main' into issue/wind-mo
Browse files Browse the repository at this point in the history
  • Loading branch information
peterdudfield authored Nov 20, 2024
2 parents f2492c8 + 4cbcc37 commit a870594
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .bumpversion.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 1.1.5
current_version = 1.1.6
commit = True
tag = True
message = Bump version: {current_version} → {new_version} [ci skip]
Expand Down
2 changes: 1 addition & 1 deletion india_forecast_app/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
"""India Forecast App"""
__version__ = "1.1.5"
__version__ = "1.1.6"
8 changes: 2 additions & 6 deletions india_forecast_app/models/pvnet/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,9 @@ def predict(self, site_id: str, timestamp: dt.datetime):
# Run batch through model
device_batch = copy_batch_to_device(batch_to_tensor(batch), DEVICE)
preds = self.model(device_batch).detach().cpu().numpy()

# filter out night time
if self.asset_type == SiteAssetType.pv:
if self.asset_type == SiteAssetType.pv.name:
preds = set_night_time_zeros(batch, preds)

# Store predictions
Expand Down Expand Up @@ -174,17 +175,12 @@ def predict(self, site_id: str, timestamp: dt.datetime):
0.0,
]
log.debug(f"Previous values are {values_df['forecast_power_kw']}")
zero_values = values_df["forecast_power_kw"] == 0
for idx in range(8):
values_df["forecast_power_kw"][idx] -= (
values_df["forecast_power_kw"][idx] - final_gen_points
) * smooth_values[final_gen_index + idx]
log.debug(f"New values are {values_df['forecast_power_kw']}")

if self.asset_type == "solar":
# make sure previous zero values are still zero
values_df["forecast_power_kw"][zero_values] = 0

if self.asset_type == "wind":
# Smooth with a 1 hour rolling window
# Only smooth the wind else we introduce too much of a lag in the solar
Expand Down
20 changes: 13 additions & 7 deletions india_forecast_app/models/pvnet/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def populate_data_config_sources(input_path, output_path):
def process_and_cache_nwp(source_nwp_path: str, dest_nwp_path: str):
"""Reads zarr file, renames t variable to t2m and saves zarr to new destination"""

log.info(f'Processing and caching NWP data for {source_nwp_path}')
log.info(f"Processing and caching NWP data for {source_nwp_path}")

# Load dataset from source
ds = xr.open_zarr(source_nwp_path)
Expand All @@ -108,7 +108,7 @@ def process_and_cache_nwp(source_nwp_path: str, dest_nwp_path: str):

is_gfs = "gfs" in source_nwp_path.lower()

if not is_gfs: # this is for ECMWF NWP
if not is_gfs: # this is for ECMWF NWP
# Rename t variable to t2m
variables = list(ds.variable.values)
new_variables = []
Expand All @@ -122,12 +122,12 @@ def process_and_cache_nwp(source_nwp_path: str, dest_nwp_path: str):
else:
new_variables.append(var)
ds.__setitem__("variable", new_variables)

# Hack to resolve some NWP data format differences between providers
elif is_gfs:
data_var = ds[list(ds.data_vars.keys())[0]]
# # Use .to_dataset() to split the data variable based on 'variable' dim
ds = data_var.to_dataset(dim='variable')
ds = data_var.to_dataset(dim="variable")
ds = ds.rename({"t2m": "t"})
# Save destination path
ds.to_zarr(dest_nwp_path, mode="a")
Expand All @@ -139,18 +139,23 @@ def download_satellite_data(satellite_source_file_path: str) -> None:
# download satellite data
fs = fsspec.open(satellite_source_file_path).fs
if fs.exists(satellite_source_file_path):
log.info(f"Downloading satellite data from {satellite_source_file_path} "
f"to sat_15_min.zarr.zip")
log.info(
f"Downloading satellite data from {satellite_source_file_path} "
f"to sat_15_min.zarr.zip"
)
fs.get(satellite_source_file_path, "sat_15_min.zarr.zip")
log.info(f"Unzipping sat_15_min.zarr.zip to {satellite_path}")
os.system(f"unzip -qq sat_15_min.zarr.zip -d {satellite_path}")
else:
log.error(f"Could not find satellite data at {satellite_source_file_path}")


def set_night_time_zeros(batch, preds, sun_elevation_limit=0.0):
"""
Set all predictions to zero for night time values
"""

log.debug("Setting night time values to zero")
# get sun elevation values and if less 0, set to 0
if BatchKey.wind_solar_elevation in batch.keys():
key = BatchKey.wind_solar_elevation
Expand All @@ -170,7 +175,8 @@ def set_night_time_zeros(batch, preds, sun_elevation_limit=0.0):
sun_elevation = sun_elevation.detach().cpu().numpy()

# expand dimension from (1,197) to (1,197,7), 7 is due to the number plevels
sun_elevation = np.repeat(sun_elevation[:, :, np.newaxis], 7, axis=2)
n_plevels = preds.shape[2]
sun_elevation = np.repeat(sun_elevation[:, :, np.newaxis], n_plevels, axis=2)
# only take future time steps
sun_elevation = sun_elevation[:, batch[t0_key] + 1 :, :]
preds[sun_elevation < sun_elevation_limit] = 0
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "india_forecast_app"
version = "1.1.5"
version = "1.1.6"
description = "Runs wind and PV forecasts for India and saves to database"
authors = ["Chris Briggs <[email protected]>"]
readme = "README.md"
Expand Down

0 comments on commit a870594

Please sign in to comment.