CLEARSKY
Overview
The CLEARSKY
function calculates the clear sky estimates of global horizontal irradiance (GHI), direct normal irradiance (DNI), and diffuse horizontal irradiance (DHI) for a given location and time using the PVLib library. This is useful for photovoltaic system modeling, solar resource assessment, and energy yield estimation. The calculation is based on the selected clear sky model (e.g., ‘ineichen’, ‘haurwitz’, or ‘simplified_solis’) and requires the latitude, longitude, and a list of timestamps. For more details, see the pvlib Location.get_clearsky documentation .
This example function is provided as-is without any representation of accuracy.
Usage
To use the function in Excel:
=CLEARSKY(latitude, longitude, time, [altitude], [tz], [model])
latitude
(float, required): Latitude of the location in decimal degrees (positive for north, negative for south).longitude
(float, required): Longitude of the location in decimal degrees (positive for east, negative for west).time
(str, required): Timestamp as a string in ISO 8601 format (e.g., “2025-06-20T12:00:00”).altitude
(float, optional, default=0.0): Altitude of the location in meters.tz
(str, optional, default=“UTC”): Timezone string (e.g., “UTC”, “America/Denver”).model
(str, optional, default=“ineichen”): Clear sky model to use. Must be one of “ineichen”, “haurwitz”, or “simplified_solis”.
The function returns a single row 2D list with columns: ['ghi', 'dni', 'dhi']
, or a 2D list of error messages if the input is invalid.
Examples
Example 1: Noon Irradiance in Denver
In Excel:
=CLEARSKY(39.7392, -104.9903, "2025-06-20T12:00:00")
Expected output:
ghi | dni | dhi |
---|---|---|
1002.6 | 888.2 | 234.1 |
Example 2: Custom Altitude and Timezone
In Excel:
=CLEARSKY(39.7392, -104.9903, "2025-06-20T12:00:00", 1609.3, "America/Denver")
Expected output:
ghi | dni | dhi |
---|---|---|
1050.7 | 930.4 | 250.3 |
Example 3: Using Haurwitz Model
In Excel:
=CLEARSKY(39.7392, -104.9903, "2025-06-20T12:00:00", 0, "UTC", "haurwitz")
Expected output:
ghi | dni | dhi |
---|---|---|
950.2 |
Example 4: Custom Location and Altitude
In Excel:
=CLEARSKY(34.0522, -118.2437, "2025-06-20T12:00:00", 89.0, "America/Los_Angeles")
Expected output:
ghi | dni | dhi |
---|---|---|
980.0 | 870.0 | 210.0 |
This means, for example, the clear sky GHI at noon in Denver is approximately 1002.6 W/m^2 using the default model.
Python Code
import micropip
await micropip.install('pvlib')
import tzdata
import pandas as pd
from pvlib.location import Location
def clearsky(latitude, longitude, time, altitude=0.0, tz="UTC", model="ineichen"):
"""
Calculate clear sky GHI, DNI, and DHI for a location and time using PVLib.
Args:
latitude: Latitude in decimal degrees.
longitude: Longitude in decimal degrees.
time: Timestamp string in ISO 8601 format.
altitude: Altitude in meters (default: 0.0).
tz: Timezone string (default: "UTC").
model: Clear sky model (default: "ineichen").
Returns:
2D list of [ghi, dni, dhi] for the timestamp, or 2D list of error messages if input is invalid.
This example function is provided as-is without any representation of accuracy.
"""
# Validate latitude and longitude
try:
lat = float(latitude)
lon = float(longitude)
except Exception:
return [["Invalid input: latitude and longitude must be numbers."]]
# Validate time
if not isinstance(time, str) or not time:
return [["Invalid input: time must be a string in ISO 8601 format."]]
try:
# Per pvlib convention: interpret input as local time in the specified tz
times_index = pd.DatetimeIndex([time])
if times_index.tz is None:
times_index = times_index.tz_localize(tz)
else:
times_index = times_index.tz_convert(tz)
except Exception:
return [["Invalid input: time must be a valid ISO 8601 string and tz must be a valid timezone."]]
# Validate altitude
try:
alt = float(altitude)
except Exception:
alt = 0.0
# Validate model
if model not in ["ineichen", "haurwitz", "simplified_solis"]:
return [["Invalid input: model must be 'ineichen', 'haurwitz', or 'simplified_solis'."]]
try:
loc = Location(lat, lon, tz=tz, altitude=alt)
cs = loc.get_clearsky(times_index, model=model)
# Handle models with different outputs
if model == "haurwitz":
# Only 'ghi' is available; fill dni/dhi with None
result = [[cs['ghi'].round(1).iloc[0], None, None]]
else:
result = cs[['ghi', 'dni', 'dhi']].round(1).values.tolist()
return result
except Exception as e:
return [[f"pvlib error: {e}"]]
Live Notebook
Edit this function in a live notebook .