Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A more appropriate way to define moon_radius constants #7465

Merged
merged 3 commits into from
Mar 21, 2024

Conversation

ahmedhosssam
Copy link
Contributor

Hi,
In sunpy/coordinates/sun.py in eclipse_amount(), I think that we could encapsulate the constants in a class K and initialize the value of k with K.IAU as a default, and then check if moon_radius is neither IAU nor minimum to throw an error.
I think the code is more readable now.

Please let me know if there are any improvements needed.

@ahmedhosssam ahmedhosssam requested a review from a team as a code owner March 1, 2024 17:26
@ahmedhosssam ahmedhosssam changed the title More appropriate way to define moon_radius constants A More appropriate way to define moon_radius constants Mar 1, 2024
@ahmedhosssam ahmedhosssam changed the title A More appropriate way to define moon_radius constants A more appropriate way to define moon_radius constants Mar 1, 2024
@nabobalis
Copy link
Contributor

nabobalis commented Mar 1, 2024

Can you explain why the class is better?

I guess I am left a bit confused as how it improves the code.

@ahmedhosssam
Copy link
Contributor Author

I think it's more maintainable, as it makes it clear that these values are related and belong to a certain context.
Also it makes it easier to use the same value somewhere else in the code (if we want to), especially because it's a floating-point number. So it's better to encapsulate it.

@ayshih
Copy link
Member

ayshih commented Mar 1, 2024

If you want to go this route, it should be a dictionary rather than a class. For example:

radius_options = {
    'IAU': 0.2725076,
    'minimum': 0.272281
}
try:
    R_moon = radius_options[moon_radius] * R_earth
except KeyError:
    raise ValueError("The supported values for `moon_radius` are: " + ", ".join(radius_options.keys()))

@nabobalis
Copy link
Contributor

nabobalis commented Mar 1, 2024

Albert, are these values not defined in astropy? And if not, should we upstream them there?

Or even as sunpy constants?

@ayshih
Copy link
Member

ayshih commented Mar 1, 2024

Hence the TODO that I wrote: # TODO: Find somewhere more appropriate to define these constants

The constants are not defined in astropy nor does it make sense to add them there. It's not clear it makes sense to add them to our constants. It's academic until some other code wants to use these constants.

@nabobalis
Copy link
Contributor

Hence the TODO that I wrote: # TODO: Find somewhere more appropriate to define these constants

The constants are not defined in astropy nor does it make sense to add them there. It's not clear it makes sense to add them to our constants. It's academic until some other code wants to use these constants.

If we are going through the effort to tidy up how we present these numbers in the function, I do not see why we should go with the slight extra effort to define them formally as constants.

@ayshih
Copy link
Member

ayshih commented Mar 1, 2024

It's not code effort, but rather design effort. These are constants that make sense only for solar eclipses, hence way too specialized to put into astropy. Our existing constants are specific to the Sun – we have sunpy.sun.constants, not sunpy.constants – so we'd need to create a completely new place for Moon constants to go.

@nabobalis
Copy link
Contributor

nabobalis commented Mar 1, 2024

Sounds good to me, let's do it.

Could it be sunpy.constants.moon and sunpy.constants.sun?

@wtbarnes
Copy link
Member

wtbarnes commented Mar 7, 2024

I think we should take a step back and ask ourselves if there is anywhere, in our existing codebase, besides this function, where these values would be useful. If the answer is no, I don't see the point in trying to expose them in some more formal way at this point.

@nabobalis
Copy link
Contributor

In that case, the dict that Albert posted should be the method we use instead of a class.

@nabobalis nabobalis added coordinates Affects the coordinates submodule Minor Change PR only needs one approval to merge labels Mar 20, 2024
sunpy/coordinates/sun.py Outdated Show resolved Hide resolved
@nabobalis nabobalis added the Merge When CI Passes Hit that merge button when it's all green! label Mar 20, 2024
@nabobalis nabobalis merged commit 51c3a28 into sunpy:main Mar 21, 2024
25 of 27 checks passed
@nabobalis nabobalis added the No Backport A PR that isn't to be backported to any release branch. (To be used as a flag to other maintainers) label Mar 21, 2024
@nabobalis
Copy link
Contributor

Thanks for the PR @ahmedhosssam

@ahmedhosssam ahmedhosssam deleted the moon_coordinates branch May 22, 2024 09:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
coordinates Affects the coordinates submodule Merge When CI Passes Hit that merge button when it's all green! Minor Change PR only needs one approval to merge No Backport A PR that isn't to be backported to any release branch. (To be used as a flag to other maintainers) No Changelog Entry Needed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants