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

netCDF Front End for Cloud Model Simulations #2176

Merged
merged 57 commits into from
Oct 7, 2020

Conversation

keltonhalbert
Copy link
Contributor

PR Summary

Added a front-end for reading CF compliant netCDF files from atmospheric cloud model simulations from the NCAR CM1 model. It uses xarray for disk io, but considering switching to the netCDF4 API due to extra/unnecessary dependencies required through xarray. This is in part to a dependency in order to import xarray within a try/except block.

Right now this will only work for netCDF files generated from CM1, as it depends on following the specific axis naming conventions. Ideally, this should be generalized such that any CF compliant netCDF can be read.

So far it seems like it handles the reading properly and plots - however, I am unsure of what tests should be conducted to make sure there aren't any issues hiding.

yt-image

@matthewturk matthewturk added triage Triage needed code frontends Things related to specific frontends new feature Something fun and new! and removed triage Triage needed labels Mar 6, 2019
@Xarthisius
Copy link
Member

@yt-fido test this please

@Xarthisius
Copy link
Member

@yt-fido test this please

Copy link
Member

@cphyc cphyc left a comment

Choose a reason for hiding this comment

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

Overall this looks good to me! Congrats ;)

I left some minor comments that should not delay much further the merge (of course provided the tests pass).

Comment on lines +144 to +149
self.domain_left_edge = np.array(
[xh.min(), yh.min(), zh.min()], dtype="float64"
)
self.domain_right_edge = np.array(
[xh.max(), yh.max(), zh.max()], dtype="float64"
)
Copy link
Member

Choose a reason for hiding this comment

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

Are xh, yh, zh the cell centers or the cell edges?

I would naively expect that if xh represents the cell edges, then the number of dimensions is ds.dimensions["xk"].size - 1, and if it represents the cell centers, then the left and right edge should be computed by adding -dx/2 and +dx/2 respectively.

I however see that you are using ds.dimensions and ds.variables, so there is definitely some subtlety that I am missing. In any case, it'd be worth commenting to say what the xh (as well as yh and zh represent).

Copy link
Contributor

Choose a reason for hiding this comment

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

we'll need @keltonhalbert to weigh in on xh, yh and zh (I think they are actually cell centers but I'm not positive?). I think adding a comment to the code would be good here -- maybe including a link to the cm1_lofs docs website?

But I think some confusion is coming from the fact that ds here is our netcdf dataset file handle, not a yt dataset. So ds.variables contains the netcdf objects with the actual data that need to be parsed and read while ds.dimensions is a netcdf attribute with dimensionality metadata. I think this will be clearer by switching ds to a different name (_handle as Matt suggested).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@chrishavlin @cphyc xh, yh, and zh are the cell center coordinates where scalars are defined.

Copy link
Contributor

Choose a reason for hiding this comment

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

added to the comments around this section noting the xh, yh and zh coordinates.


self.dimensionality = 3
self.domain_dimensions = np.array(dims, dtype="int64")
self.periodicity = (False, False, False)
Copy link
Member

Choose a reason for hiding this comment

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

Is it always the case? Or could you imagine cloud models produced with your code with periodic boundary conditions? If this is likely, add a # TODO comment to say that periodicity should be supported, or even better (if possible) raise a NotImplementedError if periodicity is detected.
If no cloud model uses periodicity, then would you mind adding a comment to document it? Something like

Suggested change
self.periodicity = (False, False, False)
# Cloud Model Simulations do not support periodicity, so we hard-code it here.
self.periodicity = (False, False, False)

yt/frontends/nc4_cm1/data_structures.py Outdated Show resolved Hide resolved

warn_netcdf(args[0])
try:
from netCDF4 import Dataset
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't it be our mock one from yt.utilities.on_demand_imports import _netCDF4 as netCDF4?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think it could be... this follows the exodus_ii is_valid() but I'm not sure if there was a reason for importing netcdf directly there either.

Copy link
Contributor

Choose a reason for hiding this comment

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

oh, sorry, was conflating two issues. You just meant the import. Yes, should definitely be able to use the _netCDF4 import. But I've been confused about why the is_valid (here and in exodus_ii) did not use the NetCDF4FileHandler that is already imported. After more digging, the reason for that seems to be the need for the keepweakref argument, since NetCDF4FileHandler.open_ds uses hardcoded arguments for the call to the netcdf dataset. So I'm inclined to add **kwargs to NetCDF4FileHandler.open_ds so that we can simply pass along the keepweakref using the same NetCDF4FileHandler used in the rest of the front end.

Copy link
Contributor

Choose a reason for hiding this comment

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

latest commit fixes this: uses the NetCDF4FileHandler and no longer imports from netCDF4 directly.

if coordspassed != ncoords:
failed_vars.append(var)

if len(failed_vars) > 0:
Copy link
Member

Choose a reason for hiding this comment

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

It is a matter of taste, but I would rather show a warning than an error here. I'm fine if you think it should be an error.

yt/frontends/nc4_cm1/io.py Outdated Show resolved Hide resolved
_fields = ("thrhopert", "zvort")
cm1sim = "cm1_tornado_lofs/nc4_cm1_lofs_tornado_test.nc"


Copy link
Member

Choose a reason for hiding this comment

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

Should an answer test be added here?

Copy link
Contributor

Choose a reason for hiding this comment

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

test_cm1_mesh_fields is an answer test, ya?

Copy link
Member

@neutrinoceros neutrinoceros left a comment

Choose a reason for hiding this comment

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

Overall this looks nice, well done !
I have a few minor questions and remarks

  • this is committing an empty misc.py file that should either be removed or populated
  • I'm confused that the CM1Dataset class has a variables attribute that seems very close to what parameters is supposed to be for. Is there a reason to have both ?
  • I'm not reviewing the tests for now. We would need to coordinate this with the testing framework change.

I'll happily approve this once my questions and suggestions are answered :)

yt/frontends/api.py Outdated Show resolved Hide resolved
yt/frontends/nc4_cm1/api.py Outdated Show resolved Hide resolved
yt/frontends/nc4_cm1/data_structures.py Outdated Show resolved Hide resolved
yt/frontends/nc4_cm1/data_structures.py Outdated Show resolved Hide resolved
yt/frontends/nc4_cm1/data_structures.py Outdated Show resolved Hide resolved
yt/utilities/file_handler.py Outdated Show resolved Hide resolved
yt/frontends/nc4_cm1/io.py Show resolved Hide resolved
yt/frontends/nc4_cm1/io.py Outdated Show resolved Hide resolved
yt/frontends/nc4_cm1/io.py Outdated Show resolved Hide resolved
yt/frontends/nc4_cm1/fields.py Outdated Show resolved Hide resolved
@matthewturk
Copy link
Member

matthewturk commented Sep 30, 2020 via email

@matthewturk
Copy link
Member

matthewturk commented Sep 30, 2020 via email

@chrishavlin
Copy link
Contributor

just pushed some changes that incorporate most of the suggestions (but not all). will go through now and note the ones that still need addressing.

Copy link
Member

@neutrinoceros neutrinoceros left a comment

Choose a reason for hiding this comment

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

Great job @chrishavlin ! I like where this is going. Even though there are still some open questions that need addressing, I'll approve this to indicate the changes I requested were dealt with :-)

yt/utilities/on_demand_imports.py Outdated Show resolved Hide resolved
yt/frontends/nc4_cm1/data_structures.py Outdated Show resolved Hide resolved
chrishavlin and others added 2 commits October 5, 2020 10:34
@chrishavlin
Copy link
Contributor

added a note about the cut region bug in the existing issue #2720

@munkm
Copy link
Member

munkm commented Oct 7, 2020

Ok, awesome! I think this looks good to go, then, so I'll merge it and we can resolve the issue a bit down the road. 🙂

@munkm munkm merged commit 65af8de into yt-project:master Oct 7, 2020
@welcome
Copy link

welcome bot commented Oct 7, 2020

Hooray! Congratulations on your first merged pull request! We hope we keep seeing you around! 🎆

@munkm
Copy link
Member

munkm commented Oct 7, 2020

Congratulations on getting this frontend over the finish line @keltonhalbert and @chrishavlin !!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
code frontends Things related to specific frontends new feature Something fun and new!
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants