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

Add HU, HT, KMU to get grid #54

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

bradyrx
Copy link
Contributor

@bradyrx bradyrx commented May 29, 2020

This PR derives and adds HU, HT, and KMU as default output for the get_grid function. It follows @matt-long's equations in #14, per the POP reference manual (3.2 and 3.3).

Closes #14.

pop_tools/grid.py Outdated Show resolved Hide resolved
Comment on lines +150 to +151
KMT_reidx = KMT - 1
KMT_reidx[KMT_reidx == -1] = 0
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This deals with the issues @klindsay28 mentioned about IndexErrors. Since the index from KMT is not zero-based.

Question: Would one anticipate the HT/HU output for land to be np.nan or 0? Currently it's 0.



@pytest.mark.parametrize('grid', pop_tools.grid_defs.keys())
def test_HT_HU_KMU_in_grid(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.

Probably unnecessary, but I like to add comprehensive testing for any PR. I know this is at odds with the fact that other variables aren't checked. Let me know if we should retain this.

@bradyrx
Copy link
Contributor Author

bradyrx commented May 29, 2020

Note that with using numba for the KMU calculation, there is little change in speed for retrieving the high resolution grids.

Here is a plot of KMT - KMU from get_grid:

Screen Shot 2020-05-29 at 12 17 51 PM

This looks like what I'd expect. The large differences are organized around smaller coastal shelves where the cell-center and cell-edge might have drastically different column sizes.

@bradyrx bradyrx mentioned this pull request May 29, 2020
2 tasks
KMU = np.zeros_like(KMT)
for i in prange(KMT.shape[0]):
for j in prange(KMT.shape[1]):
KMU[i, j] = min(KMT[i, j], KMT[i - 1, j], KMT[i, j - 1], KMT[i - 1, j - 1])
Copy link
Contributor

Choose a reason for hiding this comment

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

You may want to use np.min() here. Python's builtin min() function doesn't appear on the list of array operations that can be parallelized by numba

See: https://numba.pydata.org/numba-doc/latest/user/parallel.html#supported-operations

Copy link
Contributor

Choose a reason for hiding this comment

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

Thinking more about this, np.min() is probably not needed since you are operating on scalars in min(KMT[i, j], KMT[i - 1, j], KMT[i, j - 1], KMT[i - 1, j - 1])

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So keep it as is? My thinking is the loops are parallelized with prange and numba such that min is just comparing 4 scalars, but in parallel at the (i, j) level.

@jit(nopython=True, parallel=True)
def _generate_KMU(KMT):
"""Computes KMU from KMT."""
KMU = np.zeros_like(KMT)
Copy link
Contributor

Choose a reason for hiding this comment

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

How about initializing KMU outside of the function so as to avoid having to initialize it for every function call? This change would involve passing KMU as an input, and updating it inside the function. We could then change the function signature to

def _generate_KMU(KMT, KMU)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This will only be called once upon calling get_grid() so I figure it's fine to initialize in there. I went ahead and switched it though. I'm returning the modified KMU since that's generally how I write this kind of thing.

But per your pass-by-reference model, do I need to return it at all? Or will it automatically be modified? I'm still blown away by this. I thought modifications inside functions were just to the private variable, not the global variable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add HT, HU,... to get_grid
2 participants