Skip to content

Commit f0c9053

Browse files
authored
Merge branch 'development' into dev_geospatial_templates_for_analysis
2 parents a4630af + 2aa7332 commit f0c9053

12 files changed

+594
-14
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pvdeg.utilities.display\_json
2+
=============================
3+
4+
.. currentmodule:: pvdeg.utilities
5+
6+
.. autofunction:: display_json
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pvdeg.utilities.read\_material
2+
==============================
3+
4+
.. currentmodule:: pvdeg.utilities
5+
6+
.. autofunction:: read_material

docs/source/_autosummary/pvdeg.utilities.rst

+24
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ pvdeg.utilities
2121

2222
pvdeg.utilities.compare_templates
2323
pvdeg.utilities.convert_tmy
24+
pvdeg.utilities.display_json
2425
pvdeg.utilities.fix_metadata
2526
pvdeg.utilities.geospatial_from_csv
2627
pvdeg.utilities.get_kinetics
@@ -32,8 +33,10 @@ pvdeg.utilities
3233
pvdeg.utilities.new_id
3334
pvdeg.utilities.nrel_kestrel_check
3435
pvdeg.utilities.quantile_df
36+
pvdeg.utilities.read_material
3537
pvdeg.utilities.remove_scenario_filetrees
3638
pvdeg.utilities.restore_gids
39+
pvdeg.utilities.search_json
3740
pvdeg.utilities.strip_normalize_tmy
3841
pvdeg.utilities.tilt_azimuth_scan
3942
pvdeg.utilities.ts_gid_df
@@ -67,6 +70,13 @@ pvdeg.utilities
6770
.. minigallery:: pvdeg.utilities.convert_tmy
6871
:add-heading:
6972

73+
.. autofunction:: display_json
74+
75+
.. _sphx_glr_backref_pvdeg.utilities.display_json:
76+
77+
.. minigallery:: pvdeg.utilities.display_json
78+
:add-heading:
79+
7080
.. autofunction:: fix_metadata
7181

7282
.. _sphx_glr_backref_pvdeg.utilities.fix_metadata:
@@ -144,6 +154,13 @@ pvdeg.utilities
144154
.. minigallery:: pvdeg.utilities.quantile_df
145155
:add-heading:
146156

157+
.. autofunction:: read_material
158+
159+
.. _sphx_glr_backref_pvdeg.utilities.read_material:
160+
161+
.. minigallery:: pvdeg.utilities.read_material
162+
:add-heading:
163+
147164
.. autofunction:: remove_scenario_filetrees
148165

149166
.. _sphx_glr_backref_pvdeg.utilities.remove_scenario_filetrees:
@@ -158,6 +175,13 @@ pvdeg.utilities
158175
.. minigallery:: pvdeg.utilities.restore_gids
159176
:add-heading:
160177

178+
.. autofunction:: search_json
179+
180+
.. _sphx_glr_backref_pvdeg.utilities.search_json:
181+
182+
.. minigallery:: pvdeg.utilities.search_json
183+
:add-heading:
184+
161185
.. autofunction:: strip_normalize_tmy
162186

163187
.. _sphx_glr_backref_pvdeg.utilities.strip_normalize_tmy:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pvdeg.utilities.search\_json
2+
============================
3+
4+
.. currentmodule:: pvdeg.utilities
5+
6+
.. autofunction:: search_json

docs/source/whatsnew/index.rst

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ What's New
44
==========
55
PVDegradationTools (pvdeg) change log:
66

7-
87
.. include:: releases/v0.4.3.rst
98
.. include:: releases/v0.4.2.rst
109
.. include:: releases/v0.4.1.rst

docs/source/whatsnew/releases/v0.4.2.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ Tests
1313
Contributors
1414
~~~~~~~~~~~~
1515
* Martin Springer (:ghuser:`martin-springer`)
16-
* Aidan Wesley (:ghuser:`AidanWesley``)
16+
* Aidan Wesley (:ghuser:`AidanWesley``)

docs/source/whatsnew/releases/v0.4.3.rst

+8-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ Enhancements
55
-------------
66
``pvdeg.geospatial.analysis`` implements autotemplating. No need to specify a template for common ``pvdeg`` functions during analysis. Manually creating and providing templates is still an option. Docstrings updated with examples.
77

8+
Suite of utility functions to facilitate accessing material parameter json files.
9+
10+
* ``pvdeg.utilities.read_material`` creates a public api to replace the private ``pvdeg.untility._read_material`` function (to be deprecated soon)
11+
* ``pvdeg.utilities.search_json`` to search jsons and identify keys for desired materials.
12+
* ``pvdeg.utilities.display_json`` to view 2 level jsons in Jupyter Notebooks using HTML.
13+
814
Contributors
9-
~~~~~~~~~~~~
10-
* Tobin Ford (:ghuser:`tobin-ford`)
15+
-----------
16+
* Tobin Ford (:ghuser:`tobin-ford`)

pvdeg/temperature.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ def cell(
196196
Calculate the PV cell temperature using PVLIB
197197
Currently this only supports the SAPM temperature model.
198198
199-
Parameters:
199+
Parameters
200200
-----------
201201
weather_df : (pd.dataframe)
202202
Data Frame with minimum requirements of 'temp_air' and 'wind_speed'
@@ -304,7 +304,7 @@ def temperature(
304304
Calculate the PV cell or module temperature using PVLIB
305305
Current supports the following temperature models:
306306
307-
Parameters:
307+
Parameters
308308
-----------
309309
cell_or_mod : (str)
310310
choose to calculate the cell or module temperature. Use
@@ -347,12 +347,12 @@ def temperature(
347347
keyword argument dictionary used for the pvlib temperature model calculation.
348348
See https://pvlib-python.readthedocs.io/en/stable/reference/pv_modeling/temperature.html for more.
349349
350-
Return:
350+
Return
351351
-------
352352
temp_cell : pandas.DataFrame
353353
This is the temperature of the cell in a module at every time step.[°C]
354354
355-
References:
355+
References
356356
-----------
357357
R. Rabbani, M. Zeeshan, "Exploring the suitability of MERRA-2 reanalysis data for wind energy
358358
estimation, analysis of wind characteristics and energy potential assessment for selected

pvdeg/utilities.py

+145-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@
1313
from subprocess import run
1414
import cartopy.feature as cfeature
1515

16+
17+
# A mapping to simplify access to files stored in `pvdeg/data`
18+
pvdeg_datafiles = {
19+
"AApermeation": os.path.join(DATA_DIR, "AApermeation.json"),
20+
"H2Opermeation": os.path.join(DATA_DIR, "H2Opermeation.json"),
21+
"O2permeation": os.path.join(DATA_DIR, "O2permeation.json"),
22+
}
23+
24+
1625
def gid_downsampling(meta, n):
1726
"""
1827
Downsample the NSRDB GID grid by a factor of n
@@ -492,8 +501,7 @@ def convert_tmy(file_in, file_out="h5_from_tmy.h5"):
492501
)
493502

494503

495-
# previously: fname="materials.json"
496-
# add control over what parameters (O2, H2, AA)?
504+
### DEPRECATE ###
497505
def _read_material(name, fname="O2permeation.json"):
498506
"""
499507
read a material from materials.json and return the parameter dictionary
@@ -1337,4 +1345,138 @@ def merge_sparse(files: list[str])->xr.Dataset:
13371345
for var in ds.data_vars:
13381346
merged_ds[var].values[np.ix_(lat_inds, lon_inds)] = ds[var].values
13391347

1340-
return merged_ds
1348+
return merged_ds
1349+
1350+
def display_json(
1351+
pvdeg_file: str = None,
1352+
fp: str = None,
1353+
) -> None:
1354+
"""
1355+
Interactively view a 2 level JSON file in a JupyterNotebook
1356+
1357+
Parameters:
1358+
------------
1359+
pvdeg_file: str
1360+
keyword for material json file in `pvdeg/data`. Options:
1361+
>>> "AApermeation", "H2Opermeation", "O2permeation"
1362+
fp: str
1363+
file path to material parameters json with same schema as material parameters json files in `pvdeg/data`. `pvdeg_file` will override `fp` if both are provided.
1364+
"""
1365+
from IPython.display import display, HTML
1366+
1367+
if pvdeg_file:
1368+
try:
1369+
fp = pvdeg_datafiles[pvdeg_file]
1370+
except KeyError:
1371+
raise KeyError(f"{pvdeg_file} does not exist in pvdeg/data. Options are {pvdeg_datafiles.keys()}")
1372+
1373+
with open(fp, 'r') as file:
1374+
data = json.load(file)
1375+
1376+
def json_to_html(data):
1377+
json_str = json.dumps(data, indent=2)
1378+
for key in data.keys():
1379+
json_str = json_str.replace(f'"{key}":', f'<span style="color: plum;">"{key}":</span>')
1380+
1381+
indented_html = '<br>'.join([' ' * 4 + line for line in json_str.splitlines()])
1382+
return f'<pre style="color: white; background-color: black; padding: 10px; border-radius: 5px;">{indented_html}</pre>'
1383+
1384+
html = f'<h2 style="color: white;">JSON Output at fp: {fp}</h2><div>'
1385+
for key, value in data.items():
1386+
html += (
1387+
f'<div>'
1388+
f'<strong style="color: white;">{key}:</strong> '
1389+
f'<span onclick="this.nextElementSibling.style.display = this.nextElementSibling.style.display === \'none\' ? \'block\' : \'none\'" style="cursor: pointer; color: white;">&#9660;</span>'
1390+
f'<div style="display: none;">{json_to_html(value)}</div>'
1391+
f'</div>'
1392+
)
1393+
html += '</div>'
1394+
1395+
# Display the HTML
1396+
display(HTML(html))
1397+
1398+
1399+
def search_json(
1400+
pvdeg_file: str = None,
1401+
fp: str = None,
1402+
name_or_alias: str = None,
1403+
)-> str:
1404+
"""
1405+
Search through a 2 level JSON with arbitrary key names for subkeys with matching attributes of name or alias.
1406+
1407+
Parameters
1408+
------------
1409+
pvdeg_file: str
1410+
keyword for material json file in `pvdeg/data`. Options:
1411+
>>> "AApermeation", "H2Opermeation", "O2permeation"
1412+
fp: str
1413+
file path to material parameters json with same schema as material parameters json files in `pvdeg/data`. `pvdeg_file` will override `fp` if both are provided.
1414+
name_or_alias: str
1415+
searches for matching subkey value in either `name` or `alias` attributes. exits on the first matching instance.
1416+
1417+
Returns
1418+
---------
1419+
jsonkey: str
1420+
arbitrary key from json that owns the matching subattribute of `name` or `alias`.
1421+
"""
1422+
1423+
if pvdeg_file:
1424+
try:
1425+
fp = pvdeg_datafiles[pvdeg_file]
1426+
except KeyError:
1427+
raise KeyError(rf"{pvdeg_file} does not exist in pvdeg/data. Options are {pvdeg_datafiles.keys()}")
1428+
1429+
with open(fp, "r") as file:
1430+
data = json.load(file)
1431+
1432+
for key, subdict in data.items():
1433+
if "name" in subdict and "alias" in subdict:
1434+
if (subdict["name"] == name_or_alias or subdict["alias"] == name_or_alias):
1435+
return key
1436+
1437+
raise ValueError(rf"name_or_alias: {name_or_alias} not in JSON at {os.path(fp)}")
1438+
1439+
def read_material(
1440+
pvdeg_file: str = None,
1441+
fp: str = None,
1442+
key: str = None,
1443+
parameters: list[str] = None,
1444+
)-> dict:
1445+
"""
1446+
Read material parameters from a `pvdeg/data` file or JSON file path.
1447+
1448+
Parameters
1449+
-----------
1450+
pvdeg_file: str
1451+
keyword for material json file in `pvdeg/data`. Options:
1452+
>>> "AApermeation", "H2Opermeation", "O2permeation"
1453+
fp: str
1454+
file path to material parameters json with same schema as material parameters json files in `pvdeg/data`. `pvdeg_file` will override `fp` if both are provided.
1455+
key: str
1456+
key corresponding to specific material in the file. In the pvdeg files these have arbitrary names. Inspect the files or use `display_json` or `search_json` to identify the key for desired material.
1457+
parameters: list[str]
1458+
parameters to grab from the file at index key. If none, will grab all items at index key. the elements in parameters must match the keys in the json exactly or the output value for the specific key/parameter in the retunred dict will be `None`.
1459+
1460+
Returns
1461+
--------
1462+
material: dict
1463+
dictionary of material parameters from the seleted file at the index key.
1464+
"""
1465+
1466+
# these live in the `pvdeg/data` folder
1467+
if pvdeg_file:
1468+
try:
1469+
fp = pvdeg_datafiles[pvdeg_file]
1470+
except KeyError:
1471+
raise KeyError(f"{pvdeg_file} does not exist in pvdeg/data. Options are {pvdeg_datafiles.keys()}")
1472+
1473+
with open(fp, "r") as file:
1474+
data = json.load(file)
1475+
1476+
# take subdict from file
1477+
material_dict = data[key]
1478+
1479+
if parameters:
1480+
material_dict = {k: material_dict.get(k, None) for k in parameters}
1481+
1482+
return material_dict

pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ docs = [
6767
"nbsphinx",
6868
"sphinx_toggleprompt",
6969
"pydata_sphinx_theme",
70+
"mistune==3.0.0",
7071
]
7172
test = [
7273
"pytest",

0 commit comments

Comments
 (0)