Sun's elevation and azimuth calculation


In anticipation of Parrot releasing the application note soon here is another snippet of code that may be useful. It uses the Astral python library to work out the sun’s elevation and azimuth at the time a Sequoia image is captured.

import datetime
import pytz
from pytz import timezone
import exiftool
from astral import Astral

longitude_tag = 'Composite:GPSLongitude'
latitude_tag = 'Composite:GPSLatitude'
gps_date_tag = 'EXIF:GPSDateStamp'
gps_time_tag = 'EXIF:GPSTimeStamp'

def solar_info(gps_date, gps_time, timezone_name, lon, lat):
    Returns sun elevation and sun azimuth for a given position at a given date and time
        gps_date: Date as 'YYYY:MM:DD'
        gps_time: Time in UTC as 'HH:MM:SS.sss'
        timezone_name: Name of timezone, e.g. Africa/Johnannesburg
        lon: Longitude
        lat: Latitude

        (sun elevation, sun azimuth)
    gps_date_split = gps_date.split(':')
    year = int(gps_date_split[0])
    month = int(gps_date_split[1])
    day = int(gps_date_split[2])
    gps_time_split = gps_time.split(':')
    hour = int(gps_time_split[0])
    minutes = int(gps_time_split[1])
    dt = datetime.datetime(year, month, day, hour, minutes, 0)
    utc_dt = pytz.timezone('UTC').localize(dt)
    sat_timezone = timezone(timezone_name)
    sat_dt = utc_dt.astimezone(sat_timezone)

    astral = Astral()
    sun_elevation = astral.solar_elevation(sat_dt, lat, lon)
    sun_azimuth = astral.solar_azimuth(sat_dt, lat, lon)

    return (sun_elevation, sun_azimuth)

def solar_info_for_file(filename, timezone_name):
    tags = [ longitude_tag, latitude_tag, gps_date_tag, gps_time_tag ]
    with exiftool.ExifTool() as et:
        metadata = et.get_tags(tags, filename)
        gps_date = metadata[gps_date_tag]
        gps_time = metadata[gps_time_tag]
        lon = metadata[longitude_tag]
        lat = metadata[latitude_tag]
        return solar_info(gps_date, gps_time, timezone_name, lon, lat)

sun_elevation, sun_azimuth = solar_info_for_file('003\\IMG_170228_085706_0719_RED.TIF', 'Africa/Johannesburg')
print('Sun elevation: {0:.1f}  Sun azimuth: {1:.1f}'.format(sun_elevation, sun_azimuth))

And sample output for the sample image file used above taken on Feb 28 2017 at 8:57am UTC.

Sun elevation: 51.8  Sun azimuth: 53.2

Details of Irradiance List tag for Sunshine sensor in exif data of Sequoia

@seanmcleod, thanks a lot.


Also available in Grass GIS with r.sunhours