Repository navigation
Conversation
oiffrig
left a comment
There was a problem hiding this comment.
The implementation looks good to me.
Regarding the design, there are a few things I'm not completely comfortable with, but I'm not sure there is a good solution in the current state (since it's the first time we've been trying to use the Target interface to write something other than GRIB):
- This is not directly compatible with the currently accepted use of
Target.writewith a GRIB message - When writing more general tabular data, this will for now be the only suitable target
- The
formatattribute mixes different concerns, namely the storage format (netcdf, xarray) and the location (file, memory) - The
typeattribute matches the data format passed towrite, while other targets use it to specify the location
I don't want this to be a blocker though, so I don't mind merging for now (maybe making these caveats explicit in docstrings / comments, and/or adding safety checks), but I think it would be nice to design an output interface that allows non-GRIB data. Shall we have a think about that and then discuss?
|
Hi @oiffrig, thanks for the review and your thoughts on the design! I've pushed another commit to improve the separation of concerns and to bring the implementation a bit closer to the existing targets: the transactional Dataset/DataArray collection functionality is extracted into a new I've split the test accordingly, but they don't cover some of the new methods yet. For tabular data, this will be the only suitable target for now, as you say. I think the |
|
@oiffrig, this PR is ready for review again. I've updated the description at the top to match the current state and my last comment explains the refactoring. |
| # while the lock is aquired to synchronise the committed | ||
| # state of the builder with the actual file content | ||
| self._builder.undo_commit(stage=True) | ||
| raise |
There was a problem hiding this comment.
What is the purpose of calling undo_commit, if you raise afterwards?
There was a problem hiding this comment.
In case the calling context handles the exception and wants to retry the flush.
I seemed like a good idea to keep the file contents and committed builder state always in sync, but thinking about it now, I'm not sure anymore that it solves any specific problem. I'll have another look.
There was a problem hiding this comment.
Ok, thanks, I understand now. No need to spend too much time on it, happy to leave it as is.
jinmannwong
left a comment
There was a problem hiding this comment.
Just one small question, but otherwise looks good to me
Description
Adds a way to write xarray data to NetCDF files.
DatasetBuilder(pproc.common.xr_helpers): a transactional collector forxarray.DataArray/Datasetobjects.NetCDFTarget(pproc.config.targets): a target that writes xarray data as NetCDF to a file.I intend to use this target for several reduction-type products where the results aren't fields, in particular Hovmöller diagrams and indices for MJO, SSW and weather regimes. The plan is to also provide a CoverageJSONTarget based on the
DatasetBuilderin the future.DatasetBuilderstage(ds)DataArrayor aDatasetto the staging areacommit()reset_staged()undo_commit(stage=False)squash_commits()to_dataset()enable_parallel(manager)combine_first: values from a later commit take precedence, and earlier commits only supply values and attributes where later ones have none. NaN marks missing values. A later NaN does not replace an earlier value, and an earlier NaN is filled by a later value.NetCDFTargetThis mixes format and target but I think it it's too different from the existing file target to merge the two. I'm open to something like
netcdf-fileas an alternative name.The target supports parallel writes and checkpointing/recovery although with some caveats. The whole NetCDF file is rewritten on flush. Race conditions arise in parallel processing when values are set repeatedly over the same coordinates, though I don't expect this to matter in actual workflows.
Tests
The test suite covers the stage/commit transactional behaviour and the NetCDF writing/recovery.
Contributor Declaration
By opening this pull request, I affirm the following: