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

Some code cleanup #51

Merged
merged 8 commits into from
Sep 4, 2023
Merged

Some code cleanup #51

merged 8 commits into from
Sep 4, 2023

Conversation

navidcy
Copy link
Contributor

@navidcy navidcy commented Jun 22, 2023

This was mostly done in an attempt to understand what the functions are doing.

There was somewhere a definition of 1 degree in radians and then used to multiply all lat, lon values. I presume that's simply to convert degrees to radians. I changed to use np.deg2rad which is self-explanatory.

Also I relaxed the restriction that longitudes needed to be equidistant and that the dx = x[1] - x[0]. Now dx = np.diff(x); will this create issues?

I haven't tested anything. @ashjbarnes have a look and please test? Supposedly the code should not be different so perhaps we can compare a grid constructed on main and the same grid on this PR to ensure that nothing broke?

Comment on lines 336 to 337
`λ` (numpy.array): All longitude points on the supergrid.
`φ` (numpy.array): All latitude points on the supergrid.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't add Markdown to arguments docstrings like this, they are already recognised as variables from the context.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK -- didn't know!

Also what is "the super grid"? And if λ, φ are on the super grid then why we divide by 2 further down?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The supergrid is the set of all cell centres and corners, so it kind of looks like a double-resolution tracer grid. I expect they're divided by 2 to get the normal resolution grid.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh I see!
so is the λ, φ inputs of this function the "supergrid" but then the output is the normal grid? then dλ = np.diff(λ) * 2 (and not / 2), right?

@ashjbarnes can elaborate perhaps?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm yeah, division does sound strange in that case.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for such a late reply! I've totally abandoned this while I've been away. Was this resolved in the end? This code was all copied from the NWA people in a rush with the intention of rewriting later.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no reason to apologize!

But, what is NWA?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NWA is the North West Atlantic experiment that the NOAA people have done. This is where some of the code has been borrowed from, esp for the grid gen https://github.com/jsimkins2/nwa25

return angle


# Borrowed from grid tools (GFDL)
def quad_area(lat, lon):
"""Returns area of spherical quad (bounded by great arcs)."""
"""Returns area of spherical quadrilaterals (bounded by great arcs)."""
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Guess this should get a proper docstring?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes! I'm trying to understand first what it does.. there is some discussion in #39. :)

Copy link
Collaborator

@ashjbarnes ashjbarnes Aug 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok so on first pass this code fails when running the demo notebooks. Firstly there was a typo (y written instead of phi in line 347) but on fixing it I then get this error:

image

To test I cloned the demo notebook, checked that it still worked using the main branch then switched to the PR branch.

Basically looks like the new function modifies the size of what used to be x/y so the broadcast no longer works. I don't really have my head around what it's doing so didn't want to just fiddle with it to fix broadcast issue!

@codecov
Copy link

codecov bot commented Jun 22, 2023

Codecov Report

Patch coverage: 100.00% and project coverage change: -0.20% ⚠️

Comparison is base (6fd32fb) 15.61% compared to head (9cf83cd) 15.42%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #51      +/-   ##
==========================================
- Coverage   15.61%   15.42%   -0.20%     
==========================================
  Files           2        2              
  Lines         429      428       -1     
==========================================
- Hits           67       66       -1     
  Misses        362      362              
Files Changed Coverage Δ
regional_mom6/regional_mom6.py 14.89% <100.00%> (-0.21%) ⬇️

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@navidcy navidcy added documentation 📔 Improvements or additions to documentation cleanup 🧹 and removed documentation 📔 Improvements or additions to documentation labels Jun 22, 2023
dx = np.broadcast_to(
np.pi * R * np.cos(np.pi * y / 180) * 0.5 * res / 180,
(x.shape[0] - 1, y.shape[0]),
R * np.cos(np.deg2rad(y)) * np.deg2rad(np.diff(λ)) / 2,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This gained a np.diff? Does it change anything?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

was part of my questions to @ashjbarnes; I didn't understand why we weren't doing it previously.
the array should be the same but now the method also allows for non-uniform spacing in longitude... probably not necessary... I would definitely like to see a horizontal grid comparison using main and this PR before merging.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yeah, just making sure we have a comment at the right code location :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

before this was just np.diff(λ) -> res which was equal to x[1]-x[0]

@ashjbarnes ashjbarnes enabled auto-merge (squash) September 4, 2023 05:11
@ashjbarnes ashjbarnes merged commit 0160f4f into main Sep 4, 2023
6 checks passed
@ashjbarnes ashjbarnes deleted the ncc/clean-hgrid branch September 4, 2023 05:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants