Open live version Open live version

Plot the Yearly Path of the Sun

Make a webpage that shows the yearly path of the Sun at the visitor’s location.

code

X SunPlot[] := Block[{points, xMin, xMax, yMin, yMax, skyTopColor = Hue[0.6, 0.7, 0.5], skyBottomColor = Hue[0.6, 0.5, 0.9], groundColor = Hue[0, 0.5, 0.2], sunColor = Hue[0.15, 0.3, 0.9]}, points = Map[QuantityMagnitude, SunPosition[DateRange[ DateObject[{2014, 1, 1, 9, 0}], DateObject[{2014, 12, 31, 9, 0}], 10]]["Values"], {2}]; {{xMin, xMax}, {yMin, yMax}} = ({Min[#], Max[#]} + 10 {-1, 1}) & /@ Transpose[points]; yMin = -5; Graphics[{ {Blue, Polygon[{{xMin, yMin}, {xMin, yMax}, {xMax, yMax}, {xMax, yMin}}, VertexColors -> {skyBottomColor, skyTopColor, skyTopColor, skyBottomColor}]}, {groundColor, Rectangle[{xMin, yMin}, {xMax, 0}]}, {sunColor, Disk[#, .5] & /@ points} }, Frame -> True, ImageSize -> 500, PlotLabel -> "Yearly path of the Sun at 9:00 in " <> CommonName[First[GeoIdentify["City", $GeoLocation]]]] ]

X CloudDeploy[ APIFunction[{}, Pane[SunPlot[], ImageMargins -> 100] &, "PNG"], Permissions -> "Public"]

how it works

The Sun’s analemma, or yearly path in the sky, is a set of snapshots of the position of the Sun every day at the same time of day. This expression returns the {azimuth, altitude} coordinates of the Sun’s position in 10-day increments throughout one year:

In[1]:= X positions = SunPosition[ DateRange[DateObject[{2014, 1, 1, 9, 0}], DateObject[{2014, 12, 31, 9, 0}], 10]]["Values"]

Out[1]=

Extract numeric values by mapping QuantityMagnitude at level 2 of that list:

In[2]:= X points = Map[QuantityMagnitude, positions, {2}]

Out[2]=

From those points, make a graphic showing the Sun’s path throughout the year:

In[3]:= X Graphics[Point[points], Frame -> True]

Out[3]=

Format the graphic to make it more attractive and more suggestive of a view of the sky above the horizon. First find the minima and maxima of the azimuth and altitudes of the Sun’s path, and add 10° in each direction to widen the view slightly. Replace yMin by -5° so that the view extends 5° below the horizon:

In[4]:= X {{xMin, xMax}, {yMin, yMax}} = ({Min[#], Max[#]} + 10 {-1, 1}) & /@ Transpose[points]; yMin = -5;

The Sun subtends about 0.5° in the sky. Give the Sun images the appropriate radii using Disk rather than Point, and specify the PlotRange necessary to give a wider view:

In[5]:= X Graphics[Disk[#, 0.5] & /@ points, Frame -> True, PlotRange -> {{xMin, xMax}, {yMin, yMax}}]

Out[5]=

Add graphic elements to render the ground and horizon, and the sky shaded from light blue at the bottom to dark blue at the top. Add a plot label that specifies the time and location of the image. To put a color setter inline in the code, type DynamicSetting[ColorSetter[]], select that expression, and press Shift-Return to evaluate it in place. Click the resulting color setter and pick a color:

In[6]:= X SunPlot[] := Block[{points, xMin, xMax, yMin, yMax, skyTopColor = \!\(\* TagBox[ ColorSetterBox[Hue[0.6, 0.7, 0.5], "Swatch"], Setting]\), skyBottomColor = \!\(\* TagBox[ ColorSetterBox[Hue[0.6, 0.5, 0.9], "Swatch"], Setting]\), groundColor = \!\(\* TagBox[ ColorSetterBox[Hue[0, 0.5, 0.2], "Swatch"], Setting]\), sunColor = \!\(\* TagBox[ ColorSetterBox[Hue[0.15, 0.3, 0.9], "Swatch"], Setting]\)}, points = Map[QuantityMagnitude, SunPosition[DateRange[ DateObject[{2014, 1, 1, 9, 0}], DateObject[{2014, 12, 31, 9, 0}], 10]]["Values"], {2}]; {{xMin, xMax}, {yMin, yMax}} = ({Min[#], Max[#]} + 10 {-1, 1}) & /@ Transpose[points]; yMin = -5; Graphics[{ {Blue, Polygon[{{xMin, yMin}, {xMin, yMax}, {xMax, yMax}, {xMax, yMin}}, VertexColors -> {skyBottomColor, skyTopColor, skyTopColor, skyBottomColor}]}, {groundColor, Rectangle[{xMin, yMin}, {xMax, 0}]}, {sunColor, Disk[#, .5] & /@ points} }, Frame -> True, ImageSize -> 500, PlotLabel -> "Yearly path of the Sun at 9:00 in " <> CommonName[First[GeoIdentify["City", $GeoLocation]]]] ]

In[7]:= X SunPlot[]

Out[7]=

To make the image available to everyone, deploy it to the Wolfram Cloud, adding margins around the image using Pane.

In[8]:= X obj = CloudDeploy[ APIFunction[{}, Pane[SunPlot[], ImageMargins -> 100] &, "PNG"], Permissions -> "Public"]

The image each visitor sees when visiting the deployed image is customized to their location.