Wanted- code to calculate Sunrise/Sunset in pb
Wanted- code to calculate Sunrise/Sunset in pb
I have code from another Basic but I can't get it to work in pb. If you have code that will work in pb I love to see it.
Any calendar makers out there?
Any calendar makers out there?
MacBook Pro-M1 (2021), Tahoe 26.2, PB 6.30b6
You'll need to edit this a bit to fit your application. I'm reading longitude, latitude and timezone from a preference file and results are output to text gadgets. I also adjust for DST in my application.
Code: Select all
Procedure GetDawnDusk() ;calculates sunrise/sunset
Protected RadiansPerDegree.d,latitude.d,longitude.d,tz.d,zone.d,lo.d,c.d,C2.d,SD.d,CD.d,sc.d,C3.d,yd,n,dawn,dusk
OpenPreferences("roZetta.prf"):PreferenceGroup("Global")
longitude=ReadPreferenceDouble("longitude",-84.56)
latitude=ReadPreferenceDouble("latitude",39.04)
tz=ReadPreferenceDouble("tz",-5.00)
ClosePreferences()
tz=tz*-1
RadiansPerDegree=0.017453293
yd=DayOfYear(Date())
If latitude=>0
If yd>=Val(DST2)/24 And yd<Val(DST4)/24
zone=tz-1.0
Else
zone=tz
EndIf
Else
If yd>=Val(DST4)/24 Or yd<Val(DST2)/24
zone=tz-1.0
Else
zone=tz
EndIf
EndIf
lo=4.8771+0.0172*(yd+0.5-longitude/360.0)
c=0.03342*Sin(lo+1.345)
C2=(1.0/RadiansPerDegree)*(ATan(Tan(lo + c))-ATan(0.9175*Tan(lo+c))-c)
SD=0.3978*Sin(lo+c)
CD=Sqr(1.0-SD*SD)
sc=(SD*Sin(latitude*RadiansPerDegree)+0.0145)/(Cos(latitude*RadiansPerDegree)*CD)
C3=(1.0/RadiansPerDegree)*ATan(sc/Sqr(1.0-sc*sc))
n=((6.0-zone-(longitude+C2+C3)/15.0)/24.0)*1440.0
dawn=(n/60)*100+(n%60)
n=((18.0-zone-(longitude+C2-C3)/15.0)/24.0)*1440.0
dusk=(n/60)*100+(n%60)
SetGadgetText(#TXT_DAWN_CFG, RSet(Str(dawn),4,"0"))
SetGadgetText(#TXT_DUSK_CFG, RSet(Str(dusk),4,"0"))
EndProcedureThanks for the reply!
If I use todays date (11/26/2008) and (from your code)
longitude=-84.56
latitude=39.04
tz (timezone?) =-5 (how do you calculate this?)
What should I get as dawn and dusk?
Right now I get 342 & 2734
(edited) the above code will work in 4.40x86b1
If I use todays date (11/26/2008) and (from your code)
longitude=-84.56
latitude=39.04
tz (timezone?) =-5 (how do you calculate this?)
What should I get as dawn and dusk?
Right now I get 342 & 2734
Code: Select all
; should give 734 / 1718 for 11/25/08
Procedure SunCalc() ;calculates sunrise/sunset
;Protected RadiansPerDegree.f,latitude.f,longitude.f,tz.l,zone.d,lo.d,c.d,C2.d,SD.d,CD.d,sc.d,C3.d,yd,n,dawn,dusk
longitude.d=-84.56 ;ReadPreferenceDouble("longitude",-84.56)
latitude.d=39.04 ; ReadPreferenceDouble("latitude",39.04)
tz.d=-5.00 ; ReadPreferenceDouble("tz",-5.00)
tz=tz*-1
RadiansPerDegree.d=0.017453293
yd=DayOfYear(Date(2008,11,25,0,0,0))
zone=tz
lo.d=4.8771+0.0172*(yd+0.5-longitude/360.0)
c.d=0.03342*Sin(lo+1.345)
C2.d=(1.0/RadiansPerDegree)*(ATan(Tan(lo + c))-ATan(0.9175*Tan(lo+c))-c)
SD.d=0.3978*Sin(lo+c)
CD.d=Sqr(1.0-SD*SD)
sc.d=(SD*Sin(latitude*RadiansPerDegree)+0.0145)/(Cos(latitude*RadiansPerDegree)*CD)
C3=(1.0/RadiansPerDegree)*ATan(sc/Sqr(1.0-sc*sc))
n=((6.0-zone-(longitude+C2+C3)/15.0)/24.0)*1440.0
dawn=(n/60)*100+(n%60)
n=((18.0-zone-(longitude+C2-C3)/15.0)/24.0)*1440.0
dusk=(n/60)*100+(n%60)
MessageRequester(Str(dawn),Str(dusk))
EndProcedure
SunCalc()
Last edited by WilliamL on Fri Aug 14, 2009 7:49 pm, edited 1 time in total.
MacBook Pro-M1 (2021), Tahoe 26.2, PB 6.30b6
Re: Wanted- code to calculate Sunrise/Sunset in pb
> I have code from another Basic
You should always post the other code because it makes it easy for us to
convert it for you.
You should always post the other code because it makes it easy for us to
convert it for you.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
"PureBasic won't be object oriented, period" - Fred.
Change... to...
Code: Select all
yd=DayOfYear(Date(yr,mnth,dy,0,0,0)) Code: Select all
yd=DayOfYear(Date(2008,11,25,0,0,0)) I'm still getting 342 and 2734. I've simplified some of the code and if you run it and get 734 and 1718 then maybe the Mac version of pb is the problem. At that point it is a bug report.
edit: Running in 4.30b1&4PPC. It crashes in x86 on line starting with 'C2='. I'm wondering if my version of pb knows how to do sin/cos/atan/tan?
Code: Select all
Procedure SunCalc() ;calculates sunrise/sunset
Protected RadiansPerDegree.d,latitude.d,longitude.d,tz.d,zone.d,lo.d,c.d,C2.d,SD.d,CD.d,sc.d,C3.d,yd,n,dawn,dusk
longitude=-84.56 ;ReadPreferenceDouble("longitude",-84.56)
latitude=39.04 ; ReadPreferenceDouble("latitude",39.04)
tz=-5 ; ReadPreferenceDouble("tz",-5.00)
tz=tz*-1
RadiansPerDegree=0.017453293
yd=DayOfYear(Date(2008,11,25,0,0,0))
zone=tz
lo=4.8771+0.0172*(yd+0.5-longitude/360.0)
c=0.03342*Sin(lo+1.345)
C2=(1.0/RadiansPerDegree)*(ATan(Tan(lo + c))-ATan(0.9175*Tan(lo+c))-c)
SD=0.3978*Sin(lo+c)
CD=Sqr(1.0-SD*SD)
sc=(SD*Sin(latitude*RadiansPerDegree)+0.0145)/(Cos(latitude*RadiansPerDegree)*CD)
C3=(1.0/RadiansPerDegree)*ATan(sc/Sqr(1.0-sc*sc))
n=((6.0-zone-(longitude+C2+C3)/15.0)/24.0)*1440.0
dawn=(n/60)*100+(n%60)
n=((18.0-zone-(longitude+C2-C3)/15.0)/24.0)*1440.0
dusk=(n/60)*100+(n%60)
MessageRequester(Str(dawn),Str(dusk))
EndProcedure
SunCalc()
MacBook Pro-M1 (2021), Tahoe 26.2, PB 6.30b6
Well, thanks for the code and the time you took to send it. I noticed that the results using sine and cos did not match the values I was getting in my old Basic and that might explain why my version didn't work either. I was hoping you could verify my last posting worked so I could use it as a bug report. I will try to get pb's attention and maybe they can help.
Hey, Fred, are you listening?
Hey, Fred, are you listening?
MacBook Pro-M1 (2021), Tahoe 26.2, PB 6.30b6
-
jamirokwai
- Addict

- Posts: 802
- Joined: Tue May 20, 2008 2:12 am
- Location: Cologne, Germany
- Contact:
He WilliamL,WilliamL wrote:Well, thanks for the code and the time you took to send it. I noticed that the results using sine and cos did not match the values I was getting in my old Basic and that might explain why my version didn't work either. I was hoping you could verify my last posting worked so I could use it as a bug report. I will try to get pb's attention and maybe they can help.
Hey, Fred, are you listening?
i tried your code on Mac. 4.20 and 4.30b4 PPC show 342/2734
with your example code from the above posting. 4.30b4 x86 dies with an
awful crash.
Regards,
JamiroKwai
JamiroKwai
Ahhh! Sorry, I thought your report of the crash superceded the request that I test your code.WilliamL wrote:I was hoping you could verify my last posting worked so I could use it as a bug report.
I pasted your code into my app and get the same results as my original code 735 & 1717.
I should add that my code also works under Linux (Debian, Ubuntu, Fedora) so it looks like a Mac PB issue.
Hmmm, this might be the reason it doesn't give the correct answer. (Install. txt - with the Mac installer)
It does crash in the Mac x86, without a doubt, but this is a beta.
Thanks all for the effort and I wish you all happy holidays.
(added) Thanks for the code dhouston. I'm looking forward to using it sometime in the future.
(edit) see the working code in my thread above
If I understand the third line correctly it would explain the small result differences in my calculations (which would become large enough to give poor results). So I've been beating my head against the wall over something that is not supported yet.Here are the current know bugs, limitations (which are under development):
- No 3D Engine
- No Systray library
- No 'double' and 'quad' native types
- some Flags and most of the Color options for Gadgets still need to be implemented
- threadsafe has not been tested with gui commands yet
- gtk1 & 2 subsytem are deprecated for now
Thanks all for the effort and I wish you all happy holidays.
(added) Thanks for the code dhouston. I'm looking forward to using it sometime in the future.
(edit) see the working code in my thread above
MacBook Pro-M1 (2021), Tahoe 26.2, PB 6.30b6

