Moon percentage Illumination

This is a discussion on Moon percentage Illumination within the C Programming forums, part of the General Programming Boards category; Hi Guys, I'm pulling my hair out here Looking for an algorithm to output percentage illumination of the moon already ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    69

    Moon percentage Illumination

    Hi Guys,
    I'm pulling my hair out here
    Looking for an algorithm to output percentage illumination of the moon already in C.
    There is already Java code out there for the public,
    but my lack of understanding of language specific syntax is biting me!

    I don't need decimal digits of precision or anything like that..
    nothing fancy, just an integer result would be fine.

    I HAVE done the work of porting original BASIC code for moon rise
    and set times from Sky & Telescope Magazine (from the '70s) to C,
    and have that C port working great...
    but that's because I was a BASIC programmer too
    I have also ported some simpler moon phase algorithms that only
    provide a basic phase: 0-7, but they can be a segment in error.

    I am happy to share the source of my moon times port with anyone
    who might want it.. it's just too big to paste in here.
    Cheers, Art.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by xArt View Post
    Hi Guys,
    I'm pulling my hair out here
    Well, God forbid further follicle fracturing!

    Looking for an algorithm to output percentage illumination of the moon already in C.
    There is already Java code out there for the public,
    but my lack of understanding of language specific syntax is biting me!

    I don't need decimal digits of precision or anything like that..
    nothing fancy, just an integer result would be fine.

    I HAVE done the work of porting original BASIC code for moon rise
    and set times from Sky & Telescope Magazine (from the '70s) to C,
    and have that C port working great...
    but that's because I was a BASIC programmer too
    I have also ported some simpler moon phase algorithms that only
    provide a basic phase: 0-7, but they can be a segment in error.

    I am happy to share the source of my moon times port with anyone
    who might want it.. it's just too big to paste in here.
    Cheers, Art.
    Surely there a math equation or expression, that gives this answer, waiting for you on Google.

    With that, you can forget the Java code you don't understand, and just write the C code that you want.

    That's always one way to solve the "can't find code for X", problems. Plan on coding up your own - get the basic info you need, or the algorithm - and then code up your C program.

    It's challenging, but also liberating, and gives you the freedom to take on a lot of problems like this.

    As a side note, we have a lot of university types hanging around (tried to ditch them, but failed), and most of them have studied Java. So posting up the part you don't understand, would also be a way to go forward with this.

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    69
    Thanks for the reply.
    I understand the math... no wait... I lie...
    I understand how to translate the math to C!
    It's somewhat to do with what functions look like in Java.

    I will come back and ask a question specific to porting from Java to C.

  4. #4
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,425
    i dont think it wil hurt to post one of the java functions, we can then show how you might translate it. But as Adak says - you just need the expression that defines the moon illumination - once you have that then you are away in any language.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  5. #5
    Registered User
    Join Date
    Jan 2013
    Posts
    69
    I found the C source that the Java was ported from, so no need to waste time porting it back
    File : moon.c - Ohloh Code Search
    Not having much luck though.

    I have included all of the child functions down to the function "phase" because I'm not interested in the others.

    It compiles without any complaints until it actually runs, and crashes at any of these lines at the bottom of the phase function:
    Code:
        if(pphase)   *pphase = MoonPhase;   
     if(mage)     *mage = synmonth * (fixangle(MoonAge) / 360.0);
        if(dist)     *dist = MoonDist;
        if(angdia)   *angdia = MoonAng;
        if(sudist)   *sudist = SunDist;
      if(suangdia) *suangdia = SunAng;
    
    Then it complains about bad access or some such error.
    If I omit these lines, I can call the function with dummy values and get some result in a float,
    but it's incorrect because I don't understand the arguments that it wants in the first place.

    For example:
    Code:
    static double phase(
    double pdate,            
    double *pphase,            
    double *mage,            
    double *dist,           
     double *angdia,            
    double *sudist,            
    double *suangdia){
    pphase isn't mentioned anywhere outside of the phase function, so I don't see what I'm supposed to do with it.
    same for pdate... I know what the date is, and have a program ready to drop it into and provide that information,
    but what my program knows are integers for year, month, day, hour, etc. Not pdate whatever that is.
    Since the example I linked to is an implementation in another program, I guess they don't care to explain.

    The comment at the top of the function says it's argument is the date... but it actually wants seven doubles.
    The reason I asked if anyone had one working is I actually tried this a few yrs ago, and was put off then.

    I don't think I'm going to find any short formula.. the decent ones are quite substantial.
    Last edited by xArt; 05-27-2013 at 02:49 AM.

  6. #6
    Registered User
    Join Date
    Jan 2013
    Posts
    69
    I think this one is closer to the original C implementation:
    ftp://ftp-archive.freebsd.org/pub/Fr...ool2_2X/moon.c

    Can someone tell me how I call the phase function if I know the date, time, everything else about the universe other than moon phase?

  7. #7
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Look at the next function ("MoonPhase") which includes a call to phase():
    Code:
    /* Calculate moon phase */
    mp = 360.0 * phase(jd, NULL, NULL, NULL, NULL, NULL, NULL);
    The only input to the function is the date. All the other parameters are so called "output parameters". If you provide them, then the function will store the corresponding value there. That's why there are all this if()-statements at the end to prevent dereferencing NULL pointers.
    The meaning of each of the parameters should be obvious from the naming of the local variables.

    Bye, Andreas

  8. #8
    Registered User
    Join Date
    Jan 2013
    Posts
    69
    Quote Originally Posted by AndiPersti View Post
    The only input to the function is the date. All the other parameters are so called "output parameters". If you provide them, then the function will store the corresponding value there. That's why there are all this if()-statements at the end to prevent dereferencing NULL pointers.
    The meaning of each of the parameters should be obvious from the naming of the local variables.
    Bye, Andreas
    Thanks, I did figure that part out, and just removed the pointers from the function and prototype.

    It doesn't work though
    Code:
    /* Convert to a true Julian date -- sorry for the name clashes! */
            jd = jtime(2013, 5, 27, 0, 0, 0);
            
    /* Calculate moon phase */
            mp = 360.0 * phase(jd);

    The output of the calls above is 229, which is 63.6%.
    It's very close to full moon at the moment... no matter what time zone or time of day, it should be around 98%.



  9. #9
    Registered User
    Join Date
    Jan 2013
    Posts
    69
    Looks like I've got it

    It looks like the value of interest is MoonPhase, which is one of the output values I previously discarded,
    so I modified the phase function to return that instead.
    Also, for some reason, the months begin from 0, not 1.

    If I make this adjustments, it agrees with the percentage illumination from this page:
    Fraction of the Moon Illuminated
    down to both decimal places that it provides.

    So I need to input the month of May as "4", but the days still begin from 1.
    Doesn't make much sense, but it does very much appear to be working and accurate.

    Thanks for the help.

  10. #10
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by xArt View Post
    Also, for some reason, the months begin from 0, not 1.

    So I need to input the month of May as "4", but the days still begin from 1.
    Doesn't make much sense, but it does very much appear to be working and accurate.
    jtime() obviously expects the date parts coming from the standard time structure (struct tm) where months start with 0.

    If you don't like that you can remove one line from jdate():
    Code:
    static long jdate(int y, int mon, int day) {
        long c, m;
        
        m = mon+1;   // assumes January = 0
    Bye, Andreas

  11. #11
    Registered User
    Join Date
    Jan 2013
    Posts
    69
    I'm not worried about handling it, but do appreciate you telling me what's going on.
    thanks
    That was a long day... most of it was Googling.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New moon rising
    By Salem in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 07-23-2007, 12:24 AM
  2. 3rd moon?
    By dbaryl in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 10-02-2002, 02:31 PM
  3. Moon shot in 4kb - could you?
    By Ken Fitlike in forum Tech Board
    Replies: 10
    Last Post: 09-04-2002, 02:17 PM
  4. Moon hoax?
    By mike_k in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-13-2002, 01:37 PM
  5. Moon Hoax???
    By DISGUISED in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 12-17-2001, 08:19 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21