Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sunrise time calculation doesn't match Google's result

I am not sure if it really is related to Android, but since I am going to use it for an Android app, I am tagging Android too.

I am trying to build some logic to calculate sunrise time if lat-long are given.

I've tried following, but everything gives a slightly different time of the Sunrise of the location I provide it with (of Bangalore, India), and when I search for google with the keyword Bangalore Sunrise Google gives a different result in the card which looks more accurate:

  • Tried porting this C code to Java : http://souptonuts.sourceforge.net/code/sunrise.c.html
  • Tried using this formula from WikiPedia: http://en.wikipedia.org/wiki/Sunrise_equation
  • Tried using this library : https://github.com/mikereedell/sunrisesunsetlib-java
  • Tried using this Web Service : http://www.earthtools.org/webservices.htm#sun

All give different results, and still Google's looks to be more acccurate.

Does anybody here know of an API or a better calculation for this? Or the algorithm/webservice that Google is using for their cards?

Any help is much appreciated.

like image 827
Aman Alam Avatar asked Apr 09 '13 11:04

Aman Alam


3 Answers

Indian standard time is UTC+05:30, that's probably why you are seeing a 30 min difference.

The algorithm can calculate sunrise time in UTC, but it won't be able to convert UTC to local time, which requires complicated timezone knowledge. The algorithm probably does something very simplistic, like

localTime = utcTime + 1_hour * longitude / 15_degree

which is 5 hours for city of Banglore.

You should take the UTC time, then convert it to local time by using Java date APIs.

like image 124
ZhongYu Avatar answered Oct 04 '22 02:10

ZhongYu


I haven't studied all the links you gave, but most likely the differences stem from different assumptions & definitions. In particular, the following factors tend to vary from algorithm to algorithm:

  • Atmospheric refraction included or not (i.e. "true" or "apparent" elevation)
  • "Top" or "center" of sun breaking horizon

There are certainly other factors as well, and the problem becomes increasingly ill-defined as latitude gets farther from the equator. But these factors alone can account for minutes of difference.

like image 38
Drew Hall Avatar answered Oct 04 '22 02:10

Drew Hall


A quick search lead me to this api

http://www.earthtools.org/webservices.htm#sun

like image 41
Fernando Gallego Avatar answered Oct 04 '22 04:10

Fernando Gallego