SunTime Component lazarus / 21-General ---------------------------------------------------------------------- Mi-Ki · 2022-02-02 10:02 Hello. I use in Delphi. I use Lazarus. I have on Raspberry. Will the component go to use in Lazarus? http://www.delphiarea.com/products/delphi-components/suntime/ Thank you. ---------------------------------------------------------------------- MarkMLl · 2022-02-02 10:23 Provided you work from the source it should be possible to convert it. However you will need to check the result carefully: I transcribed code from NOAA (derived from the standard Jean Meeus stuff) and found that it didn't quite match the times from an Android GPS app: there was a difference of about three minutes and the sign of the difference varied throughout the year in a way I never figured out. However three minutes is pretty good, and well within the margin of error caused by atmospheric refraction (i.e. dependant on air temperature etc.). MarkMLl ---------------------------------------------------------------------- wp · 2022-02-02 10:33 I ported it - see attachment; it was quite easy. The demo in the attachment runs without installing anything. Note that there is also the DelphiMoon component in Online-Package- Manager which, in spite of its name, does similar calculations (among others). Moreover, you might be interested in my own SunRise application: https://github.com/wp-xyz/sunrise. ---------------------------------------------------------------------- Thaddy · 2022-02-02 10:34 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$B-} // Complete Boolean Evaluation Off{$R-} // Range Checking Off{$Q-} // Overflow Checking Off{$O+} // Optimization On{$ifdef fpc}{$mode delphi}{$endif}  // That's basically all, in fact IS all....unit SunTime;  To convert the component takes a little bit more effort, but then again, not even necessary for the functionality. (posts crossed with wp) ---------------------------------------------------------------------- wp · 2022-02-02 11:11 I prefer mode objfpc because it warns in case of a naming conflict of function arguments. In this case, it's a few lines more. But you are right: using mode Delphi is the easiest and first step in converting a unit from Delphi to Lazarus. ---------------------------------------------------------------------- winni · 2022-02-02 11:38 Hi! Do it yourself: http://williams.best.vwh.net/sunrise_sunset_algorithm.htm Winni ---------------------------------------------------------------------- Thaddy · 2022-02-02 11:47 Link fails.... must be dawn... ---------------------------------------------------------------------- engkin · 2022-02-02 13:35 Reading the page linked by winni (using web.archive), looking at the code converted by wp and his own code, I don't see the elevation above sea level here which should affect the calculation by a few minutes. MarkMLl, do you use the elevation in your calculations? ---------------------------------------------------------------------- MarkMLl · 2022-02-02 14:04 Quote from: engkin on February 02, 2022, 02:35:31 pmMarkMLl, do you use the elevation in your calculations? It was basically a transcript of Meeus via NOAA, so it worked everything out from first principles without approximations except for using a nominal refractive index right at the end... I don't think it took site elevation into consideration, and it definitely didn't ask for the temperature and pressure. Results also varied slightly depending on the architecture, but allowing for the number of layers of transcendentals in there that's barely surprising. My recollection- although this was a few years ago- is that my times matched NIST's predictions provided that I ran on x86; however I don't claim to have checked that often enough to be 100% certain. And I think I'd prefer to bet on NOAA than on some closed-source Android app, no matter how widely-used and well-recommended it is. https://gml.noaa.gov/grad/solcalc/sunrise.html MarkMLl ---------------------------------------------------------------------- wp · 2022-02-02 14:08 Quote from: MarkMLl on February 02, 2022, 11:23:17 amHowever three minutes is pretty good, and well within the margin of error caused by atmospheric refraction (i.e. dependant on air temperature etc.). And given that people usually do not see the true horizon (looking to the east and west I see houses...) even this is a secondary source of error. ---------------------------------------------------------------------- MarkMLl · 2022-02-02 14:25 Quote from: wp on February 02, 2022, 03:08:53 pmQuote from: MarkMLl on February 02, 2022, 11:23:17 amHowever three minutes is pretty good, and well within the margin of error caused by atmospheric refraction (i.e. dependant on air temperature etc.). And given that people usually do not see the true horizon (looking to the east and west I see houses...) even this is a secondary source of error. I was at that time spending a lot of time on the top of the South Downs, with a good view to the West. And the amount of daylight and warmth left to us was really quite interesting. And I'm fairly confident in my results ** I've just been looking at my transcript and find that I've added this to the usual NOAA stuff. I don't know its ultimate provenance, i.e. whether it came from Meeus or from NOAA itself. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i FUNCTION CalcSunElevation(t, utcmins, latitude, longitude: DOUBLE): DOUBLE; //* ********************************************************************** ///* Name:    CalcSunElevation                                         *///* Type:    Function                                                 *///* Purpose: calculate the elevation (altitude) of the sun            *///* Arguments:                                                        *///*   t : number of Julian centuries since J2000.0                    *///*   utcmins: UTC time in minutes                                    *///*   latitude : latitude of observer in degrees                      *///*   longitude : longitude of observer in degrees                      *///* Return value:                                                     *///*   sun's elevation/altitude in degrees                               *///**************************** *******************************************/ (* This is not an original NOAA function, but is extracted from the calcSun()   *)(* function in http://www.srrb.noaa.gov/highlights/sunrise/azel.html so that    *)(* the current twilight state can be computed. MarkMLl.                       *) VAR     eqTime, solarDec, solarTimeFix, trueSolarTime, hourAngle, haRad,                csz, zenith, exoatmElevation, refractionCorrection,                te, solarZen: DOUBLE; BEGIN  eqTime:= calcEquationOfTime(t);  solarDec:= calcSunDeclination(t);  solarTimeFix:= eqTime - 4.0 * longitude (* + 60.0 * zone *) ;  trueSolarTime:= utcmins + solarTimeFix;       // in minutes  WHILE trueSolarTime > 1440 DO    trueSolarTime -= 1440;  hourAngle:= trueSolarTime / 4.0 - 180.0;  // Thanks to Louis Schwarzmayr for finding our error,  // and providing the following 2 lines to fix it:  IF hourAngle < -180 THEN    hourAngle += 360.0;  haRad:= degToRad(hourAngle);  csz:= Sin(degToRad(latitude)) *        Sin(degToRad(solarDec)) +        Cos(degToRad(latitude)) *        Cos(degToRad(solarDec)) * Cos(haRad);  IF csz > 1.0 THEN    csz:= 1.0  ELSE    IF csz < -1.0 THEN      csz:= -1.0;  zenith:= radToDeg(ArcCos(csz));   exoatmElevation:= 90.0 - zenith;  IF exoatmElevation > 85.0 THEN    refractionCorrection:= 0.0  ELSE BEGIN    te:= Tan(degToRad(exoatmElevation));    IF exoatmElevation > 5.0 THEN      refractionCorrection:= 58.1 / te - 0.07 / (te*te*te) +                                      0.000086 / (te*te*te*te*te)    ELSE    IF exoatmElevation > -0.575 THEN        refractionCorrection:= 1735.0 + exoatmElevation *                        (-518.2 + exoatmElevation * (103.4 +                        exoatmElevation * (-12.79 +                        exoatmElevation * 0.711) ) )      ELSE        refractionCorrection:= -20.774 / te;    refractionCorrection:= refractionCorrection / 3600.0  END;  solarZen:= zenith - refractionCorrection;  RESULT:= 90.0 - solarZenEND { CalcSunElevation } ;  MarkMLl ** Note to self: must remember to ask the Vatican Observatory if they ever check that the position of the Moon is where their calculation of Easter assumes. ---------------------------------------------------------------------- Roland57 · 2022-02-02 15:27 Quote from: wp on February 02, 2022, 11:33:22 amI ported it - see attachment; it was quite easy. The demo in the attachment runs without installing anything. Would someone know a Linux implementation for the function GetBiasMinutesAtDateTime? ---------------------------------------------------------------------- MarkMLl · 2022-02-02 15:36 No, but from the limited description I can see I speculate that it's related to the Equation of Time (which is the sum of two sines based on the date). MarkMLl ---------------------------------------------------------------------- engkin · 2022-02-02 15:54 MarkMLl, thanks for sharing your code. Quote from: wp on February 02, 2022, 03:08:53 pmQuote from: MarkMLl on February 02, 2022, 11:23:17 amHowever three minutes is pretty good, and well within the margin of error caused by atmospheric refraction (i.e. dependant on air temperature etc.). And given that people usually do not see the true horizon (looking to the east and west I see houses...) even this is a secondary source of error. It is more of an engineering problem. Has several applications, like tracking the sun during eclipse, or knowing the dimensions needed for an object, like amount of extrusion over windows to get a desired amount of shadow. Not to mention religious practices, like the beginning of Sabbath. ---------------------------------------------------------------------- winni · 2022-02-02 15:56 Hi! Okay, next try. Take the original and not the copy: https://edwilliams.org/sunrise_sunset_algorithm.htm Winni ---------------------------------------------------------------------- MarkMLl · 2022-02-02 18:04 I attach the Pascal unit I was using for Sunrise/Sunset and a unit for the Moon phase, plus some oddments that I collected at around the same time and saved as useful references. MarkMLl ---------------------------------------------------------------------- AlexTP · 2022-02-02 18:48 Maybe you can put these units to Github, please? I can do it too if you don't want to. ---------------------------------------------------------------------- MarkMLl · 2022-02-02 19:10 Quote from: Alextp on February 02, 2022, 07:48:19 pmMaybe you can put these units to Github, please? I can do it too if you don't want to. Good idea. Give me a day or so to tidy them up and put some sort of demo program with them... they were originally part of a weather station (Open2300 protocol) program that displayed useful things like the amount of time until Sunset (I can assure you that in my working environment, that /was/ useful), and also served as a testbed for testing various database stuff. I suppose that I could usefully throw in my inifile extensions which can parse lat/long... MarkMLl ---------------------------------------------------------------------- winni · 2022-02-02 23:16 Hi Maybe helpful in this context: A CSV file with all cities of the world with >= 50.000 inhabitants Structure: Name|Iso Country|Latitude|Longitude|Inhabitants In the attachment Winni ---------------------------------------------------------------------- MarkMLl · 2022-02-03 19:00 Now uploaded to https://github.com/MarkMLl/NOAA_AstroDemo The demo program is OKish, although I'm not 100% confident in how the hour angle (contributing to Sunrise/Sunset) and Solar elevation are being handled by NOAA. In any event I stand by my observation that the results are good to around ±3 minutes, but please note that the routines expect a source of UTC rather than local time. MarkMLl ---------------------------------------------------------------------- Mi-Ki · 2022-02-04 09:02 Thank you very much to all of you . ---------------------------------------------------------------------- MarkMLl · 2022-02-04 09:31 You're welcome, it's always nice to see somebody else who's interested in this sort of thing. MarkMLl