[Gambas-user] searching sunset/sunrise function

Ron ron at ...1740...
Thu May 22 14:02:09 CEST 2008


Benoit Minisini schreef:
> On jeudi 22 mai 2008, Ron wrote:
>   
>>> If you have the snippets, I'm interested in converting to gambas
>>>
>>>
>>> ---------- Original Message -----------
>>> From: Ron <ron at ...1740...>
>>> To: mailing list for gambas users <gambas-user at lists.sourceforge.net>
>>> Sent: Mon, 19 May 2008 12:01:33 +0200
>>> Subject: [Gambas-user] searching sunset/sunrise function
>>>
>>>       
>>>> Hi,
>>>>
>>>> Does anybody know if  a sunset/sunrise calculator functions/snippet in
>>>> gambas exists by any chance?
>>>>
>>>> I have found some vb and vbscript snippets, but porting them is not
>>>> easy, or they give wrong results.
>>>>
>>>> Regards,
>>>> Ron.
>>>>         
>> Nando,
>>
>> I have attached the module made so far.
>>
>> CalcSunTimes() is returning fair results, as far as I an see, but
>> haven't checked it much, you're welcome to make it better. (see link
>> above routine)
>> CalcMoonPhase() is the beginning of converting code (from link in
>> module), but the phase it returns is false, puzzle as to why, maybe
>> usage of wrong datatype, or round()s? would be nice to get this fixed.
>>
>> Alas I'm no Gambas nor Math/Astronomy wizard.
>>
>> Regards,
>> Ron.
>>     
>
> Math.Floor() in Javascript must be replaced by Int(), not Round(). Maybe it is 
> the source of the problem.
>
> Regards,
>   

Ok, got it working ok now.
The Int()'s did magic, and I forgot ( ) around a calculation somewhere.
The new MoonPhase routine is attached.

Thanks,
Ron.


-------------- next part --------------
'http://home.att.net/~srschmitt/script_moon_phase.html

PUBLIC FUNCTION CalcMoonPhase()
  DIM yy, mm, k1, k2, k3, jd AS Integer
  DIM ip, dp, np, rp AS Float
  DIM AG AS Float 'moon's age
  DIM DI AS Float 'moon's distance in earth radii
  DIM LA AS Float 'moon's ecliptic latitude
  DIM LO AS Float 'moon's ecliptic longitude
  DIM Phase, Zodiac AS String
  DIM Y, D, M AS Integer
  
  y = Year(Now)
  m = Month(Now)
  d = Day(Now)
  
  'calculate the Julian Date at 12h UT
  YY = Y - Int((12 - M) / 10)
  MM = M + 9
  IF (MM >= 12) THEN MM = MM - 12

  K1 = Int(365.25 * (YY + 4712))
  K2 = Int(30.6 * MM + 0.5)
  K3 = Int(Int((YY / 100) + 49) * 0.75) - 38
  
  JD = K1 + K2 + D + 59 'for dates in Julian calendar
  IF (JD > 2299160) THEN JD = JD - K3 'for Gregorian calendar

  'calculate moon's age in days
  IP = normalize((JD - 2451550.1) / 29.530588853)
  AG = IP * 29.53
  
  IF (AG < 1.84566) THEN 
    Phase = "NEW"
  ELSE IF (AG < 5.53699) THEN 
    Phase = "Evening crescent"
  ELSE IF (AG < 9.22831) THEN 
    Phase = "First quarter"
  ELSE IF (AG < 12.91963) THEN 
    Phase = "Waxing gibbous"
  ELSE IF (AG < 16.61096) THEN 
    Phase = "FULL"
  ELSE IF (AG < 20.30228) THEN 
    Phase = "Waning gibbous"
  ELSE IF (AG < 23.99361) THEN 
    Phase = "Last quarter"
  ELSE IF (AG < 27.68493) THEN 
    Phase = "Morning crescent"
  ELSE 
    Phase = "NEW"
  ENDIF 

  IP = IP * 2 * Pi 'Convert phase to radians

  'calculate moon's distance
  DP = 2 * Pi * normalize((JD - 2451562.2) / 27.55454988)
  DI = 60.4 - 3.3 * Cos(DP) - 0.6 * Cos(2 * IP - DP) - 0.5 * Cos(2 * IP)

  'calculate moon's ecliptic latitude
  NP = 2 * Pi * normalize((JD - 2451565.2) / 27.212220817)
  LA = 5.1 * Sin(NP)

  'calculate moon's ecliptic longitude
  RP = normalize((JD - 2451555.8) / 27.321582241)
  LO = 360 * RP + 6.3 * Sin(DP) + 1.3 * Sin(2 * IP - DP) + 0.7 * Sin(2 * IP)

  IF (LO < 33.18) THEN 
    Zodiac = "Pisces"
  ELSE IF (LO < 51.16) THEN 
    Zodiac = "Aries"
  ELSE IF (LO < 93.44) THEN 
    Zodiac = "Taurus"
  ELSE IF (LO < 119.48) THEN 
    Zodiac = "Gemini"
  ELSE IF (LO < 135.30) THEN 
    Zodiac = "Cancer"
  ELSE IF (LO < 173.34) THEN 
    Zodiac = "Leo"
  ELSE IF (LO < 224.17) THEN 
    Zodiac = "Virgo"
  ELSE IF (LO < 242.57) THEN 
    Zodiac = "Libra"
  ELSE IF (LO < 271.26) THEN 
    Zodiac = "Scorpio"
  ELSE IF (LO < 302.49) THEN 
    Zodiac = "Sagittarius"
  ELSE IF (LO < 311.72) THEN 
    Zodiac = "Capricorn"
  ELSE IF (LO < 348.58) THEN 
    Zodiac = "Aquarius"
  ELSE 
    Zodiac = "Pisces"
  END IF
  
  'so longitude is not greater than 360!
  IF (LO > 360) THEN LO = LO - 360
  
  PRINT "Moon phase is " & Phase & "."
  PRINT "Moon is in " & Zodiac & "."
  PRINT "Age from new " & Round(ag, -2) & " days."
  PRINT "Distance " & Round(di, -2) & " Earth radii."
  PRINT "Ecliptic latitude: " & Round(la, -2) & " degrees."
  PRINT "Ecliptic longitude: " & Round(lo, -2) & " degrees."
  IF isleapyear(y) THEN PRINT "This is a leap year."
END

FUNCTION normalize(v AS Float) AS Float
    v = v - Round(v)
    IF (v < 0) THEN v = v + 1
    RETURN v
END

PRIVATE FUNCTION isleapyear(y AS Integer) AS Boolean
  DIM x, w, z AS Integer

  x = Int(y - 4 * Int(y / 4))
  w = Int(y - 100 * Int(y / 100))
  z = Int(y - 400 * Int(y / 400))
  
  IF (x = 0) THEN 'possible leap Year
      IF ((w = 0) & (NOT z = 0))
        RETURN FALSE 'not a leap Year
      ELSE
        RETURN TRUE 'is leap Year
      END IF
  END IF
  RETURN FALSE
END


More information about the User mailing list