%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
% 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.
% Mark A. Mikofski
% Copyright (c) 2013
Mark Mikofski (2020). Solar Position Calculator (https://www.mathworks.com/matlabcentral/fileexchange/58405-solar-position-calculator), MATLAB Central File Exchange. Retrieved .
Imane, sorry for typos:
* "linters" --> "lines"
* "reply" --> "replace"
Also if you prefer MATLAB, please try pvlib-MATLAB:
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 (...
For more information see:
If you're okay with using Python, I would recommend pvlib instead:
There's already a solar position example in the gallery
You can ask questions on the Google group forum:
Or on stack overflow with the [pvlib] tag:
If you are a student, pvlib is participating in the Google Summer of Code with NumFOCUS:
There's more info here:
I hope this helps, good luck!
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
But as the directions at the top of the file say, you would also need the solpos source and headers which you can download for free from NREL here:
Then you can compile the NREL SOLPOS file using MATLAB built-in "mex" function:
for more information see: https://www.mathworks.com/help/matlab/ref/mex.html
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.
I tried to run the program you posted but it doesn't work
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 :)
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
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)
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.
There's also a Gist of the Solar Position Calculator on GitHub here: https://gist.github.com/mikofski/7551290#file-solarposition-m