Jump to content

Issue with windkit's to_rsffile function


Recommended Posts

Posted

Hi all,

I am trying to use PyWAsP to export weibull wind climates as rsf and wrg files but have been having trouble with the windkit to_rsffile and to_wrgfile functions. 
After loading in a tab file, contour map, roughness map, and turbine locations, I calculated the generalised wind climate and the predicted wind climate at each turbine. I then attempted to output the results to an rsf using the code below but I keep getting the following error.

site_effects = topo_map.get_site_effects(output_locs, nsecs=12)

pwc_rsf = pw.wasp.get_wasp_down(gwc, site_effects, genwc_interp="nearest")

wk.to_rsffile(pwc_rsf, 'test.rsf')
KeyError                                  Traceback (most recent call last)
File ~/mambaforge/envs/pywasp_env/lib/python3.8/site-packages/xarray/core/dataset.py:1340, in Dataset._construct_dataarray(self, name)
   1339 try:
-> 1340     variable = self._variables[name]
   1341 except KeyError:

KeyError: 'elevation'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
Cell In[104], line 1
----> 1 wk.to_rsffile(pwc_rsf, 'test.rsf')

File ~/mambaforge/envs/pywasp_env/lib/python3.8/site-packages/windkit/weibull_wind_climate.py:408, in to_rsffile(wwc, rsffile, wrg_header)
    398 def to_rsffile(wwc, rsffile, wrg_header=False):
    399     """Write weibull wind climate dataset to .rsf file.
    400 
    401     Parameters
   (...)
    406         Path to .rsf file
    407     """
--> 408     return _to_resource_file(wwc, rsffile, wrg_header=wrg_header)

File ~/mambaforge/envs/pywasp_env/lib/python3.8/site-packages/windkit/_validate.py:71, in create_validator.<locals>.validator_wrapper.<locals>.validate(*args, **kwargs)
     68 # Do bwc checks
     69 validator(obj)  # Raises ValueError if errors exist
---> 71 result = func(*args, **kwargs)
     72 return result

File ~/mambaforge/envs/pywasp_env/lib/python3.8/site-packages/windkit/weibull_wind_climate.py:363, in _to_resource_file(wwc, rsffile, wrg_header)
    360 wwc_cp["k"] = (wwc_cp["k"] * 100.0).astype(np.int16)
    361 wwc_cp["wdfreq"] = (wwc_cp["wdfreq"] * 1000.0).astype(np.int16)
--> 363 df = wwc_cp["elevation"].to_dataframe().reset_index()
    364 new_columns = ["point", "Name", "west_east", "south_north", "elevation", "height"]
    365 df = df.reindex(columns=new_columns)

File ~/mambaforge/envs/pywasp_env/lib/python3.8/site-packages/xarray/core/dataset.py:1431, in Dataset.__getitem__(self, key)
   1429     return self.isel(**key)
   1430 if utils.hashable(key):
-> 1431     return self._construct_dataarray(key)
   1432 if utils.iterable_of_hashable(key):
   1433     return self._copy_listed(key)

File ~/mambaforge/envs/pywasp_env/lib/python3.8/site-packages/xarray/core/dataset.py:1342, in Dataset._construct_dataarray(self, name)
   1340     variable = self._variables[name]
   1341 except KeyError:
-> 1342     _, name, variable = _get_virtual_variable(self._variables, name, self.dims)
   1344 needed_dims = set(variable.dims)
   1346 coords: dict[Hashable, Variable] = {}

File ~/mambaforge/envs/pywasp_env/lib/python3.8/site-packages/xarray/core/dataset.py:178, in _get_virtual_variable(variables, key, dim_sizes)
    176 split_key = key.split(".", 1)
    177 if len(split_key) != 2:
--> 178     raise KeyError(key)
    180 ref_name, var_name = split_key
    181 ref_var = variables[ref_name]

KeyError: 'elevation'

Does anyone know why I'm getting this error and/or how I could fix this issue? Am I missing some steps between downscaling to a wwc and outputting the rsf?

Any help is much appreciated, thank you 

Posted

Hi,

Sorry for this error. The issue is that by default elevation is not included in the result from `get_wasp_down`, but is required for creating the RSF.

The easiest fix would be to set the argument `return_site_factors` to `True` in the call to `get_wasp_down`, this will include all of the site effects in the `pwc_rsf` variable. Alternatively, you could just copy the elevation like `pwc_rsf["elevation"] = site_effects["elevation"]`

Posted

Thanks for getting back to me so quickly. 

I realised that part of the issue was that the label for elevation is by default 'site_elev' not 'elevation' but using that fact and your solutions worked to solve the elevation error so thank you! 
Unfortunately, I then ran into a similar issue with the variables A_combined, k_combined, and power_density, all of which the function requires but the wwc was missing. In solving these errors, I've found that the following code seems to work best to include all of these variables in the weibull wind climate with the correct labels:

# Using the downscale function automatically calculates power_density and site_elev
pwc_rsf = pw.wasp.downscale(gwc, topo_map, output_locs, conf, genwc_interp="nearest")

# Relabelling site_elev as elevation
pwc_rsf['elevation'] = pwc_rsf['site_elev']
pwc_rsf = pwc_rsf.drop_vars(["site_elev"])

# Adding A_combined and k_combined
pwc_rsf["A_combined"], pwc_rsf["k_combined"] = wk.weibull_wind_climate.weibull_combined(pwc_rsf)

wk.to_rsffile(pwc_rsf, 'test.rsf')

However, I'm now encountering this error when I run the to_rsffile function:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[37], line 1
----> 1 wk.to_rsffile(pwc_rsf, 'test.rsf')

File ~/mambaforge/envs/pywasp_env/lib/python3.8/site-packages/windkit/weibull_wind_climate.py:408, in to_rsffile(wwc, rsffile, wrg_header)
    398 def to_rsffile(wwc, rsffile, wrg_header=False):
    399     """Write weibull wind climate dataset to .rsf file.
    400 
    401     Parameters
   (...)
    406         Path to .rsf file
    407     """
--> 408     return _to_resource_file(wwc, rsffile, wrg_header=wrg_header)

File ~/mambaforge/envs/pywasp_env/lib/python3.8/site-packages/windkit/_validate.py:71, in create_validator.<locals>.validator_wrapper.<locals>.validate(*args, **kwargs)
     68 # Do bwc checks
     69 validator(obj)  # Raises ValueError if errors exist
---> 71 result = func(*args, **kwargs)
     72 return result

File ~/mambaforge/envs/pywasp_env/lib/python3.8/site-packages/windkit/weibull_wind_climate.py:370, in _to_resource_file(wwc, rsffile, wrg_header)
    368 df = df.merge(wwc_cp["k_combined"].to_dataframe().reset_index())
    369 df = df.merge(wwc_cp["power_density"].to_dataframe().reset_index())
--> 370 df.loc[:, "nsec"] = wwc_cp.dims["sector"]
    372 freq_sec = list(wwc_cp["wdfreq"].groupby("sector"))
    373 A_sec = list(wwc_cp["A"].groupby("sector"))

File ~/mambaforge/envs/pywasp_env/lib/python3.8/site-packages/pandas/core/indexing.py:818, in _LocationIndexer.__setitem__(self, key, value)
    815 self._has_valid_setitem_indexer(key)
    817 iloc = self if self.name == "iloc" else self.obj.iloc
--> 818 iloc._setitem_with_indexer(indexer, value, self.name)

File ~/mambaforge/envs/pywasp_env/lib/python3.8/site-packages/pandas/core/indexing.py:1718, in _iLocIndexer._setitem_with_indexer(self, indexer, value, name)
   1716 if not len(self.obj):
   1717     if not is_list_like_indexer(value):
-> 1718         raise ValueError(
   1719             "cannot set a frame with no "
   1720             "defined index and a scalar"
   1721         )
   1722     self.obj[key] = value
   1723     return

ValueError: cannot set a frame with no defined index and a scalar

Do you know how I could fix this problem?

  • 2 weeks later...
Posted

Sorry for the long delay. We have fixed the issue in an updated release of Windkit 0.6.2. You should be able to install it from both pip and mamba now.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...