cosinekitty / astronomy

Astronomy Engine: multi-language calculation of Sun, Moon, and planet positions. Predicts lunar phases, eclipses, transits, oppositions, conjunctions, equinoxes, solstices, rise/set times, and other events. Provides vector and angular coordinate transforms among equatorial, ecliptic, horizontal, and galactic orientations.
MIT License
496 stars 63 forks source link

add astro_time_t and test TerrestrialTime #317

Closed DannyRavi closed 1 year ago

DannyRavi commented 1 year ago

Finally, after some delay, I converted a part of the program from C code to Go, and especially I tested the "TerrestrialTime" part in both languages and the result was the same. I also converted "astro_time_t" to Go equivalent. I am waiting for the implementation of the next step if the completed parts are approved. Thanks.

cosinekitty commented 1 year ago

Hi @DannyRavi! This is a good start, and I'm glad to see you are still working on it. I will need to do a few things over the weekend to help make more progress. I will update the code generator to process this Go code, so that it can be put in the correct place (generate/template/) and the generated code will go into /source/golang/. We will also need to separate the test code so that the astronomy.go does not have a main function. I need to learn more about Go, but I'm pretty sure there is a standard way of adding unit tests so that they don't interfere with other code using the library. Thank you for your work on this! I will let you know in a day or two when I have done the steps I mentioned above.

cosinekitty commented 1 year ago

Hi again Danny! As you can see, I merged your changes into the golang branch. I also made some more changes:

When you make future changes, again you should be editing the source code in the generate/template directory. Then you will need to run the full test suite to generate the target code and run all the tests. You will need to change into the generate directory and execute ./run (Linux or Mac) or run.bat (Windows). It will automate the process of running the code generator and unit tests for all languages (C, C#, JavaScript, Python, Kotlin, and Go).

Unfortunately, this script takes a while to finish, and you will need to install dependencies for all the supported programming languages as mentioned here.

I hope this helps!

cosinekitty commented 1 year ago

I finally fixed some problems with the way Git and Windows interacted to cause line-ending problems with Go source code. Also, I still want to make a change in how functions report and detect errors.

The C code that you based your changes on is probably not the most natural way to handle this in Go. In the C code, I made an unusual decision to include a status field in structs, and to return the structs by value. This is because a C function can only return a single type, and I wanted to avoid having to pass a pointer to an error code, or have to call some separate function to detect errors everywhere.

However, in Go, functions can return tuple types. This allows a more idiomatic way of reporting and handling errors in Go. I think we should follow that pattern so that experienced Go programmers easily recognize and understand how to use Astronomy Engine.

So I will go back and refactor the types to separate error reporting from return data. I will do that later today.

cosinekitty commented 1 year ago

Oh, one more thing: because C does not have namespaces or modules, I have to prefix a lot of things with "Astronomy" or "astro" to avoid name collisions. We don't need to do that in Go because everything is contained inside the module "astronomy". So I will make a quick pass of renaming things today also. After that, we can continue that pattern of naming.

cosinekitty commented 1 year ago

I think we are both learning Go at the same time. One thing I discovered is that names beginning with upper case are for public/exported symbols. Names beginning with lower case are for private/internal symbols. There is a lot of good information in the Go Style Guide. I think we should both read this document and try to follow it as much as possible.

I also think the C# version of Astronomy Engine will be a better reference for how to name things in Go than the C version. Both are more modern languages with concepts like interfaces, namespaces, etc. There is also more of a similar culture of using PascalCase and camelCase naming in both languages.

In commit 9756c1ebe07f59cc509dde665ee0314015b12161 I renamed things to follow Go's preferred style, and the requirement of public/private specification by having an upper/lower case first character in the name.

DannyRavi commented 1 year ago

Hi @cosinekitty. In the past, I tried to translate most of the program from C code to Golang, but the code was very complicated and I could not translate the program by dividing and conquering method. Finally, I was able to translate a part of the program with the help of one of the examples, but I did not understand the schematic of the program. I am also familiar with the many concepts of Golang language, I am also familiar with the topics of pubilc and private names, but because I did not fully understand the structure of the program, I left these items to refactoring in the near future.

I think that method I use for translation is not suitable. If I translate the C code into Golang, it will make the Golang syntax look like C. It's kind of overfitting. Please specify the important parts of the program that do not depend on each other so that I can take the next steps better. Finally, I need more time and effort to get a good result.

cosinekitty commented 1 year ago

OK, that makes sense. Yes, it is very difficult to come into a project this big and know where to start. I appreciate your willingness and effort! I'm thinking more about using the C# code as the reference than the C code. Let me think more about what the next steps should be. I may have to make some kind of diagram or document of dependencies, to figure out what is the best sequence. This will take a little bit of time, but I will reply back here.

DannyRavi commented 1 year ago

I am waiting for the next step to be determined. Now, out of curiosity, I ran the C program on the Arduino, and there were limited resources on the Arduino Uno, but esp8266(node mcu) runs well with a few changes, for example, on ASTRONOMY_ENGINE_WHOLE_SECOND, and although incorrect information is output from the serial port. It seems that microsecond time unit can be activated with some changes and using SysTick or micros() or hardware timer.