Thursday, February 14, 2019

Solar Position Functions, Google Sheets Edition

Using the sun as a navigation aid is both an obvious idea and one fraught with difficulty. Making sense of the sun's position throughout the day involves understanding your latitude, longitude, timezone offset and a bunch of non-trivial (to me!) math. Fortunately, I've been able to untangle this math. The next step was to make leveraging these calclations a breeze. For this, I turned to Google Sheets.

I popped into Google Sheets, opened up the Script Editor and started translating scheme code to JavaScript. When I was done, I had the following custom Sheets functions implemented:

# Sun's Position
elevation(lat,lng,ts,tzOffset=null) - sun's position in degrees above the horizon
azimuth(lat,lng,ts,tzOffset=null) - heading of the sun's position in degrees 

# Notable times when the sun is 0° above the horizon

You can find the code that implements these functions here. The ts (timestamp) parameter passed in above uses the internal Google Sheets timestamp format. This allows the above code to gracefully integrate with other Sheets functions. The tzOffset parameter is optional; if unset it falls back to the user's current timezone preference. This auto behavior is handy for seamlessly dealing with daylight saving changes throughout the year.

Consider these examples:

These demonstrate printing a full day's worth of elevations and azimuths. Using these charts I can trivially derive my current direction by using the sun and my watch, or derive the time by using the sun and a compass.

Stay tuned for more ideas as to how these functions can be used.

No comments:

Post a Comment