Page 1 of 1

Wanted- code to calculate Sunrise/Sunset in pb

Posted: Wed Nov 26, 2008 11:07 pm
by WilliamL
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?

Posted: Wed Nov 26, 2008 11:29 pm
by dhouston
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"))
EndProcedure

Posted: Thu Nov 27, 2008 12:03 am
by WilliamL
Thanks 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

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()
(edited) the above code will work in 4.40x86b1

Re: Wanted- code to calculate Sunrise/Sunset in pb

Posted: Thu Nov 27, 2008 1:05 am
by 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. :)

Posted: Thu Nov 27, 2008 2:05 am
by dhouston
You should get 0734 and 1718. Skip the DST offset - you should get the right answers then.

tz is the offset from Greenwich (Universal) time. Greenwich is at 0.00 longitude.

BTW, the algorithm came from Sky & Telescope magazine. Originally, it was in GWBasic.

Posted: Thu Nov 27, 2008 2:39 am
by dhouston
Change...

Code: Select all

    yd=DayOfYear(Date(yr,mnth,dy,0,0,0)) 
to...

Code: Select all

    yd=DayOfYear(Date(2008,11,25,0,0,0)) 

Posted: Thu Nov 27, 2008 3:56 am
by WilliamL
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.

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()

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?

Posted: Thu Nov 27, 2008 4:35 am
by dhouston
I don't have a Mac so I can't help there.

I've used the basic algorithm under VisualBasic, PB and a couple of embedded versions of Basic and it has always been accurate to within a minute. My guess is there's a problem with the Mac version of PB.

Posted: Thu Nov 27, 2008 4:42 am
by WilliamL
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?

Posted: Thu Nov 27, 2008 8:40 am
by dige
Seems, that the sun here have the same special sunrise/sunset times like in cincinnati ;-) .. have got: 139 / 1006

Posted: Thu Nov 27, 2008 9:07 am
by jamirokwai
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?
He WilliamL,

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.

Posted: Thu Nov 27, 2008 2:27 pm
by dhouston
WilliamL wrote:I was hoping you could verify my last posting worked so I could use it as a bug report.
Ahhh! Sorry, I thought your report of the crash superceded the request that I test your code.

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.

Posted: Thu Nov 27, 2008 5:28 pm
by WilliamL
Hmmm, this might be the reason it doesn't give the correct answer. (Install. txt - with the Mac installer)
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
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. :oops: 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