Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add spectral factor gallery example #2114

Merged
merged 40 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from 37 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
c9b860c
Create spectral_factor.py
RDaxini Jul 1, 2024
d1156be
Update spectral_factor.py
RDaxini Jul 1, 2024
8fefcf2
Update spectral_factor.py
RDaxini Jul 1, 2024
c566795
Update spectral_factor.py
RDaxini Jul 1, 2024
e8b63ec
Update spectral_factor.py
RDaxini Jul 1, 2024
12c5d6c
Update spectral_factor.py
RDaxini Jul 3, 2024
fedba93
Update spectral_factor.py
RDaxini Jul 3, 2024
eeea56b
Update docs/examples/spectrum/spectral_factor.py
RDaxini Jul 3, 2024
bca1f93
Update docs/examples/spectrum/spectral_factor.py
RDaxini Jul 3, 2024
4c7ca2d
Update docs/examples/spectrum/spectral_factor.py
RDaxini Jul 3, 2024
134fc30
Update docs/examples/spectrum/spectral_factor.py
RDaxini Jul 3, 2024
96c6144
Update spectral_factor.py
RDaxini Jul 3, 2024
dba0ce8
Update docs/examples/spectrum/spectral_factor.py
RDaxini Jul 3, 2024
2f1fa7a
Update docs/examples/spectrum/spectral_factor.py
RDaxini Jul 3, 2024
c3f9b05
Update spectral_factor.py
RDaxini Jul 3, 2024
9837013
Update spectral_factor.py
RDaxini Jul 3, 2024
4105785
Merge branch 'main' into scf_example
RDaxini Jul 3, 2024
21043c9
Update spectral_factor.py
RDaxini Jul 4, 2024
4254c85
Update docs/examples/spectrum/spectral_factor.py
RDaxini Jul 4, 2024
18ceb37
Update docs/examples/spectrum/spectral_factor.py
RDaxini Jul 4, 2024
1bf00b1
Update spectral_factor.py
RDaxini Jul 4, 2024
02891ea
Merge branch 'scf_example' of https://github.com/RDaxini/pvlib-python…
RDaxini Jul 4, 2024
8c859a7
Update spectral_factor.py
RDaxini Jul 4, 2024
d935a7c
Update spectral_factor.py
RDaxini Jul 4, 2024
37bdee0
Update spectral_factor.py
RDaxini Jul 4, 2024
665f494
Update spectral_factor.py
RDaxini Jul 4, 2024
ea48684
Update v0.11.1.rst
RDaxini Jul 4, 2024
3b0a79b
Update spectral_factor.py
RDaxini Jul 4, 2024
d4c03cb
Update spectral_factor.py
RDaxini Jul 4, 2024
0af9b2d
Update spectral_factor.py
RDaxini Jul 4, 2024
e1398d3
Apply suggestions from code review
RDaxini Jul 5, 2024
69d9567
Update spectral_factor.py
RDaxini Jul 6, 2024
68edc03
Update docs/examples/spectrum/spectral_factor.py
RDaxini Jul 8, 2024
094a744
expand ama/amr initialism
RDaxini Jul 8, 2024
fa2e353
Update spectral_factor.py
RDaxini Jul 8, 2024
782e076
Update spectral_factor.py
RDaxini Jul 8, 2024
19d6ddb
Update spectral_factor.py
RDaxini Jul 8, 2024
2db4db0
Update docs/examples/spectrum/spectral_factor.py
RDaxini Jul 8, 2024
63d52cb
Update spectral_factor.py
RDaxini Jul 8, 2024
ecd9cf9
Merge remote-tracking branch 'upstream/main' into scf_example
RDaxini Jul 9, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 149 additions & 0 deletions docs/examples/spectrum/spectral_factor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
"""
Spectral Mismatch Estimation
============================

Comparison of methods to estimate the spectral mismatch factor
from atmospheric variable inputs.
"""

# %%
# Introduction
# ------------
# This example demonstrates how to use different ``spectrum.spectral_factor_*``
# models in pvlib-python to calculate the spectral mismatch factor, :math:`M`.
# While :math:`M` for a photovoltaic (PV) module can be calculated exactly
# using spectral irradiance and module spectral response data, these data are
# often not available. pvlib-python provides several functions to estimate the
# spectral mismatch factor, :math:`M`, using proxies of the prevailing spectral
# irradiance conditions, such as air mass and clearsky index, which are easily
# derived from common ground-based measurements such as broadband irradiance.
# More information on a range of spectral factor models, as well as the
# variables upon which they are based, can be found in [1]_.
#
# To demonstrate this functionality, first we need to import some data.
# This example uses a Typical Meteorological Year 3
# (TMY3) file for the location of Greensboro, North Carolina, from the
# pvlib-python data directory. This TMY3 file is constructed using the median
# month from each year between 1980 and 2003, from which we extract the first
# week of August 2001 to analyse.

# %%
import pathlib
from matplotlib import pyplot as plt
import pandas as pd
import pvlib
from pvlib import location

DATA_DIR = pathlib.Path(pvlib.__file__).parent / 'data'
meteo, metadata = pvlib.iotools.read_tmy3(DATA_DIR / '723170TYA.CSV',
coerce_year=2001, map_variables=True)
meteo = meteo.loc['2001-08-01':'2001-08-07']

# %%
# Spectral Factor Functions
# -------------------------
# This example demonstrates the application of three pvlib-python
# spectral factor functions:
#
# - :py:func:`~pvlib.spectrum.spectral_factor_sapm`, which requires only
# the absolute airmass, :math:`AM_a`
# - :py:func:`~pvlib.spectrum.spectral_factor_pvspec`, which requires
# :math:`AM_a` and the clearsky index, :math:`k_c`
# - :py:func:`~pvlib.spectrum.spectral_factor_firstsolar`, which requires
# :math:`AM_a` and the atmospheric precipitable water content, :math:`W`

# %%
# Calculation of inputs
# ---------------------
# Let's calculate the absolute air mass, which is required for all three
# models. We use the Kasten and Young [2]_ model, which requires the apparent
# sun zenith. Note: TMY3 files timestamps indicate the end of the hour, so we
# shift the indices back 30-minutes to calculate solar position at the centre
# of the interval.

# Create a location object
lat, lon = metadata['latitude'], metadata['longitude']
alt = altitude = metadata['altitude']
tz = 'Etc/GMT+5'
loc = location.Location(lat, lon, tz=tz, name='Greensboro, NC')

# Calculate solar position parameters
solpos = loc.get_solarposition(
meteo.index.shift(freq="-30min"),
pressure=meteo.pressure*100, # convert from millibar to Pa
temperature=meteo.temp_air)
solpos.index = meteo.index # reset index to end of the hour

airmass_relative = pvlib.atmosphere.get_relative_airmass(
solpos.apparent_zenith).dropna()
airmass_absolute = pvlib.atmosphere.get_absolute_airmass(airmass_relative)
RDaxini marked this conversation as resolved.
Show resolved Hide resolved
# %%
# Now we calculate the clearsky index, :math:`k_c`, which is the ratio of GHI
# to clearsky GHI.

cs = loc.get_clearsky(meteo.index.shift(freq="-30min"))
cs.index = meteo.index # reset index to end of the hour
kc = pvlib.irradiance.clearsky_index(meteo.ghi, cs.ghi)
# %%
# :math:`W` is provided in the TMY3 file but in other cases can be calculated
RDaxini marked this conversation as resolved.
Show resolved Hide resolved
# from temperature and relative humidity
# (see :py:func:`pvlib.atmosphere.gueymard94_pw`).
RDaxini marked this conversation as resolved.
Show resolved Hide resolved

w = meteo.precipitable_water

# %%
# Calculation of Spectral Mismatch
# --------------------------------
# Let's calculate the spectral mismatch factor using the three spectral factor
# functions. First, we need to import some model coefficients for the SAPM
# spectral factor function, which, unlike the other two functions, lacks
# built-in coefficients.

# Import some for a mc-Si module from the SAPM module database.
module = pvlib.pvsystem.retrieve_sam('SandiaMod')['LG_LG290N1C_G3__2013_']
#
# Calculate M using the three models for an mc-Si PV module.
RDaxini marked this conversation as resolved.
Show resolved Hide resolved
m_sapm = pvlib.spectrum.spectral_factor_sapm(airmass_absolute, module)
m_pvspec = pvlib.spectrum.spectral_factor_pvspec(airmass_absolute, kc,
'multisi')
m_fs = pvlib.spectrum.spectral_factor_firstsolar(w, airmass_absolute,
'multisi')

df_results = pd.concat([m_sapm, m_pvspec, m_fs], axis=1)
RDaxini marked this conversation as resolved.
Show resolved Hide resolved
df_results.columns = ['SAPM', 'PVSPEC', 'FS']
# %%
# Comparison Plots
# ----------------
# We can plot the results to visualise variability between the models. Note
# that this is not an exact comparison since the exact same PV modules has
# not been modelled in each case, only the same module technology.

# Plot M
fig1, (ax1, ax2) = plt.subplots(2, 1)
df_results.plot(ax=ax1, legend=False)
ax1.set_xlabel('Day')
ax1.set_ylabel('Spectral mismatch (-)')
ax1.set_ylim(0.85, 1.15)
RDaxini marked this conversation as resolved.
Show resolved Hide resolved
ax1.legend(loc='upper center', frameon=False, ncols=3,
bbox_to_anchor=(0.5, 1.3))

# We can also zoom in one one day, for example August 2nd.
df_results.loc['2001-08-02'].plot(ax=ax2, legend=False)
ax2.set_xlabel('Time')
ax2.set_ylabel('Spectral mismatch (-)')
ax2.set_ylim(0.85, 1.15)
RDaxini marked this conversation as resolved.
Show resolved Hide resolved

plt.tight_layout()

plt.show()

# %%
# References
# ----------
# .. [1] Daxini, R., and Wu, Y., 2023. "Review of methods to account
# for the solar spectral influence on photovoltaic device performance."
# Energy 286
# :doi:`10.1016/j.energy.2023.129461`
# .. [2] Kasten, F. and Young, A.T., 1989. Revised optical air mass tables
# and approximation formula. Applied Optics, 28(22), pp.4735-4738.
# :doi:`10.1364/AO.28.004735`
4 changes: 4 additions & 0 deletions docs/sphinx/source/whatsnew/v0.11.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ Testing

Documentation
~~~~~~~~~~~~~
* Added gallery example demonstrating the application of
several spectral mismatch factor models.
(:issue:`2107`, :pull:`2114`)


Requirements
Expand All @@ -34,3 +37,4 @@ Requirements
Contributors
~~~~~~~~~~~~
* Echedey Luis (:ghuser:`echedey-ls`)
* Rajiv Daxini (:ghuser:`RDaxini`)
Loading