arabeyes-org / ITL

Islamic Tools and Libraries
Other
81 stars 22 forks source link

Update the prayer times calculations #2

Open mohamed opened 10 years ago

mohamed commented 10 years ago

The methods used in prayertime are a bit old and do not reflect the prayer times reported by most modern prayer times tools. I am working on a "clean-room" implementation of prayertime which is based on the theory given in http://praytimes.org/calculation/

z3bi commented 10 years ago

I think the only calculation that differs from that document is Asr, and I have fixed that in my pull request https://github.com/arabeyes-org/ITL/pull/3 all of the others seem to be fine.

mohamed commented 10 years ago

I live in the Netherlands and I see that the times reported by ITL differ from the calendar used by the local mosques for all prayers by 2+ minutes. I think that the source of error is the method used to compute the Sun coordinates. The current astro.h/.c use the method from Jean Meeus book in 1998. That method uses huge tables. The US Naval Observatory (USNO) released an approximate algorithm for calculating the Sun coordinates (see http://aa.usno.navy.mil/faq/docs/SunApprox.php). I would propose that we replace astro.c/.h with simple and easy-to-maintain method to determine the Sun coordinates.

arabeyes commented 10 years ago

Go ahead and try it Mohamed. If the USNO algorithm implementation gives more accurate timings than the old method, then we'll use it in lieu of the old one.

mohamed commented 10 years ago

Hi arabeyes, Do you know which references almanac is used for the Sun coordinates? I am thinking of using NASA almanac program (Windows only) as a reference point.

arabeyes commented 10 years ago

No idea. I am not the original project owner and this isn't really my field of expertise. Is this program developed by NASA itself, or someone else did it based on NASA's almanac? If it is the latter, we should perhaps double-check it to make sure it contains no mistakes.

ghost commented 10 years ago

Salam, " I see that the times reported by ITL differ from the calendar used by the local mosques for all prayers by 2+ minutes" I think local mosques aren't a good bentch mark :) Here in the UK, when I asked some mosques on how they produce their timetables, all they do is adapt what's on islamicfinder.net. Not sure who are the people behind that website, and what lib/implementations they use.

mohamed commented 10 years ago

Wa alikom alsalam haqman,

I agree that local mosques are not a proper reference point. However, we can use the official calendars used in Islamic countries as the reference point.

abougouffa commented 10 years ago

Hi Mr. @mohamed, Check my library and my small program written in Python for the calculation of prayer times using the same (I think) method you mentioned, if this program works, I will re-write it in C https://github.com/a-bougouffa/islamic-lib

mohamed commented 10 years ago

Salam abdelhak, Thanks for the information. I have already finished the new implementation and I plan to push it soon to a new branch.

abougouffa commented 10 years ago

Hi, @mohamed After I saw the code of the current pray times implementation of ITL, I think that in your case it require more inputs to perform the exact results, because as you can see at the "Location" structure, it had three extras fields "temperature", "pressure" and "seaLevel", try to change the default values, if you get wrong results for your location, the new implementation will be the best one :+1:

acamarata commented 10 years ago

Was this implementation finished or is any work needed to help test?

acamarata commented 10 years ago

If the sun coordinates could be calculated would there be a need at all for the different calculation methods (ISNA, MWL, etc.) since those only differ in the sun angle for Fajr and Isha? If the angle could be dynamically calculated based on lat/lng to some accuracy than wouldn't that be better than these methods and provide the ability to make a "one formula" that would be "most accurate"?

If this can be implemented it is an exciting proposition and I would like to port to JavaScript to stop using praytimes.org inshaa' Allah

abougouffa commented 10 years ago

Assalam 3alaykom my brother @alisalaah, of course, the sun coordinates (sun declination) can be calculated, but for the lat/long depended zenith angle I don't know! I've a fully implemented prayer times library; I will port it from Python to C and itegrate it in ITL becouse @mohamed haven't do it! If you are interested to port the library from Python to JavaScript, you can find it here https://github.com/a-bougouffa/pyIslam

acamarata commented 10 years ago

I already have a Javascript library that works but with the 6 methods "trying" to get the times.. I would port this library if it had a sun angle algorithm that offered a one calculation for all locations and better accuracy. Otherwise I'll hold off inshaa' Allah

abougouffa commented 10 years ago

Yes of course, in my pyIslam library, all calculations depend on: longitude, latitude, timezone, fajr & ishaa method, asr madhab it calculate all needed algorithms depending on this variables and the date of course! To know how the library calculate prayer times you can see the sources at the prayertimes.py file and the "gregorianToJulianDay" function on baselib.py and for Omm al-Qura implementation, you need also to calculate the hijri date (the hijri.py file).

z3bi commented 10 years ago

I'm not sure we have determined if calculating things like the declination of the sun is better than the current table of values. I don't think we should make this change unless we can say for certain that calculating these values is more accurate. Honestly, I feel like the table is less error prone as the calculation will lose precision due to floating point math.

acamarata commented 10 years ago

Is there an endorsed global table/map of when to use which calculation for the most accurate times? The end result is just which angles to use for Fajr/Isha and so on but it seems to have direct relation to how far one is from the equator as we see UK/US and others using different values than say the middle east and so on. I am simply interested in trying to have the best, most accurate and consistent approach when doing prayer times anywhere - globally - inshaa' Allah. What do you see as the best solution to being able to get this with only inputting a users coords and having the software calc/formula handle the rest?

On Fri, Jul 11, 2014 at 11:30 PM, Ameir Al-Zoubi notifications@github.com wrote:

I'm not sure we have determined if calculating things like the declination of the sun is better than the current table of values. I don't think we should make this change unless we can say for certain that calculating these values is more accurate. Honestly, I feel like the table is less error prone as the calculation will lose precision due to floating point math.

— Reply to this email directly or view it on GitHub https://github.com/arabeyes-org/ITL/issues/2#issuecomment-48752467.

z3bi commented 10 years ago

@alisalaah I don't think there is any official table of which method to use for a particular country. As with any Fiqh issue there will always be multiple opinions. My suggestion is to start an open source listing of the most common method for different countries. This could potentially be a component of this project or perhaps a separate repo. We could then compile this list and verify each addition with a valid source, eventually we would be able to have a comprehensive list inshAllah.

acamarata commented 10 years ago

Is the only difference per each method the Fajr and Isha angles? Could we not collect a table of best angles per area and make a table of the known best angles at given coords and then grab closest data from that to do this automatically.

I haven't studied the different methods in detail but could we do something like best angles per area to make it location based? On Jul 15, 2014 9:46 PM, "Ameir Al-Zoubi" notifications@github.com wrote:

@alisalaah https://github.com/alisalaah I don't think there is any official table of which method to use for a particular country. As with any Fiqh issue there will always be multiple opinions. My suggestion is to start an open source listing of the most common method for different countries. This could potentially be a component of this project or perhaps a separate repo. We could then compile this list and verify each addition with a valid source, eventually we would be able to have a comprehensive list inshAllah.

— Reply to this email directly or view it on GitHub https://github.com/arabeyes-org/ITL/issues/2#issuecomment-49042171.

acamarata commented 10 years ago

http://moonsighting.com/how-we.html

On Tue, Jul 15, 2014 at 9:53 PM, Ali Camarata alisalaah@gmail.com wrote:

Is the only difference per each method the Fajr and Isha angles? Could we not collect a table of best angles per area and make a table of the known best angles at given coords and then grab closest data from that to do this automatically.

I haven't studied the different methods in detail but could we do something like best angles per area to make it location based? On Jul 15, 2014 9:46 PM, "Ameir Al-Zoubi" notifications@github.com wrote:

@alisalaah https://github.com/alisalaah I don't think there is any official table of which method to use for a particular country. As with any Fiqh issue there will always be multiple opinions. My suggestion is to start an open source listing of the most common method for different countries. This could potentially be a component of this project or perhaps a separate repo. We could then compile this list and verify each addition with a valid source, eventually we would be able to have a comprehensive list inshAllah.

— Reply to this email directly or view it on GitHub https://github.com/arabeyes-org/ITL/issues/2#issuecomment-49042171.

mohamed commented 9 years ago

Alsalam alikom all, I uploaded a new commit that implements a simpler calculation method which uses the approximate Sun coordinates method from the US Naval Observatory as discussed above (see: http://aa.usno.navy.mil/faq/docs/SunApprox.php). Please note that this work is still far from being complete and it is still WIP. Any efforts to improve/test the new method are highly welcome.

z3bi commented 9 years ago

Thank you for the work on this. I will try to help in creating some test cases so we can verify the new calculations against the existing ones.

z3bi commented 9 years ago

@mohamed I left a few comments on your pull request based on a quick glance, I will try to do a more thorough review soon inshAllah.

mohamed commented 9 years ago

Thanks a lot Ameir! I will look into them and update the code accordingly.

z3bi commented 9 years ago

@mohamed I've been using the new_method code with actual prayer time applications and I have a few comments/suggestions. The idea of "config" files are good for testing, but for practically using this as a library, it also needs a good programmatic interface. This will most likely be how most people interact with this code. Specifically, the way you set a date is not ideal. The previous code had a struct with year, month, day which was very easy to use. I recommend doing something similar.

It also doesn't make sense to use round DOWN or round UP. Turning 5:29:01 into 5:30 or 5:29:59 into 5:29 is a big difference. It would be safer to use normal rounding, that way at most the difference is 29 seconds.

For the dhuhr safety margin you are currently adding, it is based on the third definition of this page http://praytimes.org/wiki/A_note_on_Dhuhr but if you look at the first two fiqh definitions, a safety margin is not needed. Perhaps the dhuhr safety margin can be something set, a boolean to decide whether or not to include it. Based on my earlier comment about the programmatic interface I would suggest making the location struct contain location information only (latitude, longitude, altitude, timezone) and then having another struct with all the configuration info (method, madhab, extreme, etc) and then similar to the current library there should be a method to populate the config struct with the default values.

My last comment right now is that it does not build on Mac OS X as it errors out on the redefining of the "round" function.

mohamed commented 9 years ago

@ameiralzoubi I am glad that someone is taking a careful look to the code! :+1: I agree with your comments. So if you have time, please feel free to submit a patch to change the code. Otherwise, I will try to address them. However, I am quite busy during the week and I work on this only/mainly during the weekends.

z3bi commented 9 years ago

I will work on that inshAllah and add in the ability for custom angles.

acamarata commented 9 years ago

I implemented the USNO for a globally accurate single dynamic formula at http://praycalc.com which is also on my github (written in JavaScript for Meteor, based on the SunCalc (USNO formula) which does most the work).

mohamed commented 9 years ago

Alsalam alikom Ali, Thanks a lot for the link. Did you implement the extreme latitude methods? That's the only missing thing now from my USNO implementation. I didn't have time to go and study the equations and methods used in the extreme latitude calculations so an existing clean implementation is highly valuable.

acamarata commented 9 years ago

No, I haven't done that yet due to time but it should be easy. The current methods return times due to the inaccuracy of static angles and the USNO would return null because the twilights never actual happen, which we know.

According to fiqh you should go by the time of the nearest place that has these times in reality. All that needs to be done is, if Fajr is null, then loop calculating Fajr again but subtract .1 from the latitude until you get a result. Then display the result (maybe with a * to show it's derived this way). Same for Isha or Maghrib.

z3bi commented 8 years ago

If anyone on this thread is interested, I created a new prayer time library https://github.com/batoulapps/Adhan

All astronomical equations have been implemented from scratch directly from the book "Astronomical Algorithms", it has an official implementation in Swift and JavaScript with Python and Java in progress. It also features an extensive set of unit tests as well as worldwide location testing.x

ghost commented 8 years ago

How does it compare with libITL?

I need to remove C++ dependency from munadi.org and have one app for both web and native!

On 8 Apr 2016, at 04:01, زعبي notifications@github.com wrote:

If anyone on this thread is interested, I created a new prayer time library https://github.com/batoulapps/Adhan

All astronomical equations have been implemented from scratch directly from the book "Astronomical Algorithms", it has an official implementation in Swift and JavaScript with Python and Java in progress. It also features an extensive set of unit tests as well as worldwide location testing.x

— You are receiving this because you commented. Reply to this email directly or view it on GitHub

z3bi commented 8 years ago

@haqman the main differences is that in Adhan we re-implemented all the astronomical equations from scratch using a widely recognized book on astronomical calculations. Every equation is cited back to the book and has unit tests using values from the book. In addition we will be adding suggested methods for a location very soon. Right now we are collecting data to use for automated testing and to help determine the best method for a particular location.

acamarata commented 8 years ago

If using the NOAA, etc. algorithms there should be no need for "calculation methods" as they will return the correct values for every latitude and longitude with a single formula. For summer and winter at extreme north and such you will obviously get a "null" for Isha times and such and this should be dealt with by doing a loop and moving .1 (or whatever amount) closer to the equator until you get a non-null value, then return that with some identifier that this is derived not real (so people can indicate such on their sites).

On Mon, Apr 11, 2016 at 8:12 PM, زعبي notifications@github.com wrote:

@haqman https://github.com/haqman the main differences is that in Adhan we re-implemented all the astronomical equations from scratch using a widely recognized book on astronomical calculations. Every equation is cited back to the book and has unit tests using values from the book. In addition we will be adding suggested methods for a location very soon. Right now we are collecting data to use for automated testing and to help determine the best method for a particular location.

— You are receiving this because you were mentioned. Reply to this email directly or view it on GitHub https://github.com/arabeyes-org/ITL/issues/2#issuecomment-208333080

z3bi commented 8 years ago

@alisalaah the astronomical equations can give you precise values for sunrise, sunset, transit and whatever hour angle you ask it for. However, the challenge is still finding the visual appearance and disappearance of twilight (the basis for fajr and isha) which have a lot of variation based on light refraction. In addition, safeguards are put in place not just for null values, but to account for "hardship". An isha time of 11:45 PM and a fajr time of 1:30 AM are not reasonable, in these situations a comprise is used (such as using the more favorable time of 1/7 of the night).

mohamed commented 8 years ago

@z3bi Are you planning to have an implementation in C?

z3bi commented 8 years ago

@mohamed right now we don't have a plan to write a version in C but we would gladly accept a pull request if someone was interested in doing it. The large number of unit tests should make it easier to stay consistent when porting to a new language.