Skip to content

Commit

Permalink
Merge pull request #112 from NREL/geo_bug_fix
Browse files Browse the repository at this point in the history
Geo bug fix
  • Loading branch information
martin-springer authored Aug 19, 2024
2 parents a5a4a72 + 906c099 commit 77d8001
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 8 deletions.
6 changes: 4 additions & 2 deletions docs/source/whatsnew/releases/v0.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ Scenarios
* Added geospatial bounding box support. Clip unwanted data with rectangular bounding boxes. See: ``geospatial.apply_bounding_box``
* Added stochastic non-uniform density downselection function to preferentially select for mountains (higher point density with changes in elevation, lower density when flatter.) See: ``geospatial.elevation_stochastic_downselection``
* Updated non-uniform downselection with thresholding and non-linear normalization (support for logarithmic and exponential normalization) See: ``geospatial.identify_mountains_weights``
* Added Scenario and GeospatialScenario methods for quick plotting and downselection. See: ``GeospatialScenario.plot_coords``, ``GeospatialScenario.plot_meta_classification``, ``GeospatialScenario.plot_USA``, ``Scenario.extract``, ``Scenario.plot``, ``GeospatialScenario.classify_mountain_weights``, ``GeospatialScenario.classify_mountain_radii``, ``GeospatialScenario.downselect_elevation_stochastic``
* Added Scenario and GeospatialScenario methods for quick plotting and downselection. See: ``GeospatialScenario.plot_coords``, ``GeospatialScenario.plot_meta_classification``, ``GeospatialScenario.plot_USA``, ``Scenario.extract``, ``Scenario.plot``, ``GeospatialScenario.classify_mountain_weights``, ``GeospatialScenario.classify_mountain_radii``, ``GeospatialScenario.downselect_elevation_stochastic``.
* Added a convenience method ``GepspatialScenario.geospatial_data`` to quickly pull the geospatial weather and metadata from a scenario. Matches the API for ``pvdeg.weather.get``.

Geospatial Improvements

Expand All @@ -40,6 +41,7 @@ Bug Fixes
* Replaced deprecated numba `jit(nopython=True)` calls with `njit`
* Fix incorrect keyword arguments in `pvdeg.standards.T98_estimate`
* Fixed ``pvdeg.temperature.module`` and ``pvdeg.temperature.cell`` docstring return type. Correct return type ``pd.Series``
* Fixed broken Geospatial Analysis, when using chunked (dask) xarrays for weather data

Dependencies
------------
Expand All @@ -52,4 +54,4 @@ Dependencies
Contributors
~~~~~~~~~~~~
* Tobin Ford (:ghuser:`tobin-ford`)
* Mike Kempe (:ghuser:`MDKempe`)
* Mike Kempe (:ghuser:`MDKempe`)
8 changes: 4 additions & 4 deletions pvdeg/geospatial.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,17 +269,17 @@ def output_template(
dims = set([d for dim in shapes.values() for d in dim])
dims_size = dict(ds_gids.sizes) | add_dims

# if len(ds_gids.chunks) == 0:
# raise ValueError(f"argument ds_gids must contain chunks")

output_template = xr.Dataset(
data_vars={
var: (dim, da.empty([dims_size[d] for d in dim]), attrs.get(var))
for var, dim in shapes.items()
},
coords={dim: ds_gids[dim] for dim in dims},
attrs=global_attrs,
) # .chunk({dim: ds_gids.chunks[dim] for dim in dims})
) # moved chunks down from here

if ds_gids.chunks: # chunk to match input
output_template = output_template.chunk({dim: ds_gids.chunks[dim] for dim in dims})

return output_template

Expand Down
27 changes: 26 additions & 1 deletion pvdeg/scenario.py
Original file line number Diff line number Diff line change
Expand Up @@ -1206,7 +1206,9 @@ def addLocation(

geo_meta = geo_meta[geo_meta["county"].isin(county)]

geo_meta, geo_gids = pvdeg.utilities.gid_downsampling(
# we don't downsample weather data until this runs
# because on NSRDB we are storing weather OUT of MEMORY with dask
geo_meta, geo_gids = pvdeg.utilities.gid_downsampling(
geo_meta, downsample_factor
)

Expand Down Expand Up @@ -1525,6 +1527,29 @@ def coords_tonumpy(self) -> np.array:

return coords

def geospatial_data(self) -> tuple[xr.Dataset, pd.DataFrame]:
"""
Extract the geospatial weather dataset and metadata dataframe from the scenario object
Example Use:
>>> geo_weather, geo_meta = GeospatialScenario.geospatial_data()
This gets us the result we would use in the traditional pvdeg geospatial approach.
Parameters:
-----------
None
Returns:
--------
(weather_data, meta_data): (xr.Dataset, pd.DataFrame)
A tuple of weather data as an `xarray.Dataset` and the corresponding meta data as a dataframe.
"""
# downsample here, not done already happens at pipeline runtime
geo_weather_sub = self.weather_data.sel(gid=self.meta_data.index)
return geo_weather_sub, self.meta_data


def addJob(
self,
func: Callable = None,
Expand Down
2 changes: 1 addition & 1 deletion tests/test_geospatial.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
).compute()


def test_analysis_standoff():
def test_analysis_standoff_unchunked():
res_ds = pvdeg.geospatial.analysis(
weather_ds=GEO_WEATHER,
meta_df=GEO_META,
Expand Down
1 change: 1 addition & 0 deletions tests/test_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ def test_add_material():
json.dump(data, f, indent=4)


# this only works because we are not running on kestrel
def test_nrel_kestrel_check_bad():
with pytest.raises(ConnectionError):
pvdeg.utilities.nrel_kestrel_check()

0 comments on commit 77d8001

Please sign in to comment.