File Exchange

## Solar Position Calculator

version 1.0.0.0 (57.6 KB) by
Basic analytical calculation of solar zenith and azimuth without atmospheric refraction.

Updated 26 Jul 2016

%SOLARPOSITION Calculate solar position using most basic algorithm
% This is the most basic algorithm. It is documented in Seinfeld &
% Pandis, Duffie & Beckman and Wikipedia.
%
% [ANGLES,PROJECTION] = SOLARPOSITION(DATE,TIME,LATITUDE,LONGITUDE,TIME_ZONE)
% returns ZENITH & AZIMUTH for all DATE & TIME pairs at LATITUDE, LONGITUDE.
% ANGLES = [ZENITH,AZIMUTH] and PROJECTION = [PHI_X, PHI_Y]
% PHI_X is projection on x-z plane & PHI_Y is projection on y-z plane.
% DATETIME can be string, vector [YEAR, MONTH, DAY, HOURS, MINUTES, SECONDS],
% cellstring or matrix N x [YEAR, MONTH, DAY, HOURS, MINUTES, SECONDS] for N
% times.
% LATITUDE [degrees] and LONGITUDE [degrees] are the coordinates of the site.
% TIME_ZONE [hours] of the site.
% ROTATION [degrees] clockwise rotation of system relative to north.
% DST [logical] flag for daylight savings time, typ. from March to November
% in the northern hemisphere.
%
% References:
% http://en.wikipedia.org/wiki/Solar_azimuth_angle
% http://en.wikipedia.org/wiki/Solar_elevation_angle
%
% Mark A. Mikofski
%

### Cite As

Mark Mikofski (2021). Solar Position Calculator (https://www.mathworks.com/matlabcentral/fileexchange/58405-solar-position-calculator), MATLAB Central File Exchange. Retrieved .

Jaco Luus

Im sure it works well if you can get it working, but the fact that there are no clear instructions on how to use this, makes it quite annoying

Jan Antoni Mikulski

Mark Mikofski

Imane, sorry for typos:
* "linters" --> "lines"

Also if you prefer MATLAB, please try pvlib-MATLAB:
https://pvpmc.sandia.gov/applications/pv_lib-toolbox/

Mark Mikofski

Hi Imane,

You also need to have "datefig" from the File Exchange:

Or you can remove linters from:
ax = datefig.ployy(...
to the end, which will also remove the plot

Or you can reply it with the built in MATLAB plotyy:
[ax, h1, h2] = plotyy (...
https://www.mathworks.com/help/matlab/ref/plotyy.html

If you're okay with using Python, I would recommend pvlib instead:
There's already a solar position example in the gallery
Or on stack overflow with the [pvlib] tag:
http://stackoverflow.com/questions/tagged/pvlib

If you are a student, pvlib is participating in the Google Summer of Code with NumFOCUS:
https://github.com/pvlib/pvlib-python/wiki/GSoC-2020-Project

I hope this helps, good luck!

Sincerely,
Mark

Mark Mikofski

Hi Imane,

I also realized that the example uses the NREL SOLPOS code which you would need to compile into a mex file. The MathWorks did not allow me to post this file on the File Exchange because of NREL's disclaimers, but you can get the mex file from my Gist here: https://gist.github.com/mikofski/2301c8e1fb0c2db20d33b2ffd05033c5#file-solpos_mex-c

https://www.nrel.gov/grid/solar-resource/solpos.html

Then you can compile the NREL SOLPOS file using MATLAB built-in "mex" function:
mex solpos_mex.c

Again, if you are open to using Python, the NREL SOLPOS algorithm is also available here: https://sunpower.github.io/SolarUtils/
you can install it in any Python environment using this command: pip install SolarUtils

Also NREL makes SOLPOS available as an online calculator here: https://midcdmz.nrel.gov/solpos/solpos.html

The example only uses SOLPOS to compare the analytic calculations to more accurate SOLPOS model, so if you want, you can comment out the lines that call solpos() or use it's output.

If you have more questions, you can repost here, or on the pvlib google group, or you can try StackOverflow.

Take care,
Mark

imane ourraoui

I tried to run the program you posted but it doesn't work

Maha

Not easy to use at first instance, I was looping over single lines of lat / lon / date / time and had strange behaviour giving me 6 numbers. But then I figured it out (by looping over twin lines) and it's giving great results compare to the NREL/SPA ! Thanks for it Mark :)

Mark Mikofski

Hi QINGYANG LIU and others, The ROTATION parameter does not affect the ANGLES output which contains the (unrefracted) zenith and azimuth of the sun in a right-hand coordinate system with X to the east, Y to the north, and Z straight up. The ROTATION parameter represents an alternate coordinate system, call it XYZ-prime, that has X-prime and Y-prime rotated clockwise from X and Y. This affects the PROJECTION output, which are the angles on the ZX-prime plane and ZY-prime planes measured from Z. An example would be a solar PV power plant that was oriented 175 degrees from north (or southeasterly). The PROJECTION angles phi_x and phi_y are useful for calculating the angle of incidence of DNI on the PV panels or the position of a single axis tracker. Typically the X-prime axis would be aligned with a row of PV panels, and the Y-prime axis is perpendicular to them. Hope this helps! Thanks for using this. Also check out this cool MATLAB implementation by Meysam Mahooti of the NREL SPA algorithm: https://www.mathworks.com/matlabcentral/fileexchange/59903-nrel-s-solar-position-algorithm-spa

Srinivas Kolluru

Sreelakshmi J

Mark Mikofski

Hi Steven (and others), I found the source code for the NREL solpos mex file. I pasted a gist of it online (https://gist.github.com/mikofski/2301c8e1fb0c2db20d33b2ffd05033c5) which you can append to the end of the NREL solpos.c source code which you can download for free (http://rredc.nrel.gov/solar/codesandalgorithms/solpos/solpos.c) you will also need the header file (http://rredc.nrel.gov/solar/codesandalgorithms/solpos/solpos00.h) and to make the mex function work you'll need the appropriate compiler for your platform. (optionally you can comment or remove the void function S_decode from the original NREL solpos.c source code on lines 933 to 998 but I don't think it's necessary). The MATLAB mex file is based on NREL stest00.c source code (http://rredc.nrel.gov/solar/codesandalgorithms/solpos/stest00.c)

Steven Hong

I'm trying to run your example, but where is the solpos function? Is it from NERL website? But they only have c code, which matlab doesn't recognize.

Mark Mikofski

There's also a Gist of the Solar Position Calculator on GitHub here: https://gist.github.com/mikofski/7551290#file-solarposition-m

##### MATLAB Release Compatibility
Created with R2013b
Compatible with any release
##### Platform Compatibility
Windows macOS Linux