-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Added convenience method for saving DataArray to netCDF file #990
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
Conversation
This is great, but it needs tests and documentation :). |
I'm also a little concerned about the design here, because it always saves/restores the DataArray with the name Instead, maybe we should only override the original name if it's Then we could restore by looking for a Dataset with single data variable regardless of the name, e.g.,
|
Thanks @shoyer - I wasn't actually aware that DataArrays can have a name before they're put into a Dataset (that's one of the things I love about contributing to OSS: you always learn things about the software that you never knew before!). I'll deal with the design issues ASAP. How would you suggest testing this? I wasn't sure where to put the tests, and what exactly to test? Presumably something about the equivalence between this and saving after manually converting to a dataset, but I wasn't really sure how best to go about it. |
I would add these tests to test_backends, probably a new test class. The
|
I think I've dealt with the comments/issues now, but let me know if more needs doing. |
|
||
Parameters | ||
---------- | ||
All parameters are passed directly to `xarray.open_dataset`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should mention the parameters again (at least path
). The fact that this uses open_dataset
internally is an implementation detail.
Thanks for the useful comments. I've added some bits to the Sphinx docs in the places that I think they should go - but let me know if I need to add any more. |
Also, it seems like there might be an issue with not closing the Dataset - but only on Windows (see the AppVeyor build failure at https://ci.appveyor.com/project/jhamman/xarray-injyf/build/1.0.268/job/kr8ss684ijxg55be). Any ideas how this could be sorted without having to implement |
The easiest fix would be to add a decorator to skip tests on Windows, like the existing decorators that we use: I'm not sure off hand how to detect if we're running on Windows but it can't be hard. To add a close method, I would copy these lines from Then, you just need to define |
Thanks - I've got that working with the The only failure on here now is the same as #991 - a conda error creating one of the environments. |
@@ -578,6 +578,19 @@ def where(self, cond, other=None, drop=False): | |||
|
|||
return outobj._where(outcond) | |||
|
|||
def close(self): | |||
"""Close any files linked to this dataset |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dataset -> object
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and here
I have a few final nits, but looking very nice! |
Great, thanks. I think I've done these changes (frustratingly I can't see a way to use |
chunks, lock, drop_variables) | ||
|
||
if len(dataset.data_vars) != 1: | ||
raise ValueError('Given file dataset contains more than one variable. ' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
variable -> data variable
I have a couple inline comments that I think you missed? |
Oops yes, sorry! I'm having a bit of an issue with changing the strings to constants (which, by the way, I completely agree is how it should be done). I've defined the constants in I am doing the import as:
But this gives me the following error:
It seems like my relative import is somehow mucking up another relative import inside the netCDF4 stuff. Do you have any ideas how I can solve this? Am I just doing my imports wrong? |
We have a bit of a circular imports mess :). For now, just import them at the top of the |
Ah ok - that works (not entirely sure why, but it works!) |
Added a simple function to DataArray that creates a dataset with one variable called 'data' and then saves it to a netCDF file. All parameters are passed through to to_netcdf(). Added an equivalent function called `open_dataarray` to be used to load from these files.
…ingle var dataset
Changed name that is used if da has no name Stored old name in attrs so it can be replaced Updated tests Updated docs
When loading a DataArray, the _file_obj attribute is set to the same object as the source Dataset's _file_obj. This can then be used to close the file from the DataArray. Tests have been updated to use this
4d76ee4
to
4920def
Compare
I realised that the reason that Travis and AppVeyor weren't running properly was because there were conflicts between this PR and master. I think I've managed to rebase and push properly - but let me know if I've done anything wrong. |
Thanks! |
Added a simple function to DataArray that creates a dataset with one variable
called 'data' and then saves it to a netCDF file. All parameters are passed through
to to_netcdf().
Added an equivalent function called
open_dataarray
to be used to load from these files.Fixes #915.