1. ## 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. Originally Posted by xArt
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.

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. 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.

5. 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;
```
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.

6. 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. 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. Originally Posted by AndiPersti
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. 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.

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. Originally Posted by xArt
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. 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.