-
Notifications
You must be signed in to change notification settings - Fork 295
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
Support group_attrs
in cf_writer
#1914
base: main
Are you sure you want to change the base?
Conversation
Codecov Report
@@ Coverage Diff @@
## main #1914 +/- ##
=======================================
Coverage 93.78% 93.79%
=======================================
Files 282 282
Lines 42250 42260 +10
=======================================
+ Hits 39626 39636 +10
Misses 2624 2624
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report at Codecov.
|
Our new code checker isn't happy with the quality of the 'cf' writer. Any chance you could try cleaning some of this stuff up? https://github.com/pytroll/satpy/pull/1914/checks?check_run_id=5603372095 |
I have improved the test part. For the cf_writer, the high complexity seems OK? |
satpy/writers/cf_writer.py
Outdated
if flatten_attrs: | ||
group_attrs = flatten_dict(group_attrs) | ||
dataset.attrs = encode_attrs_nc(group_attrs) | ||
|
||
encoding, other_to_netcdf_kwargs = update_encoding(dataset, to_netcdf_kwargs, numeric_name_prefix) |
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.
If you put this chunk of code (everything inside the for loop besides the to_netcdf
and append
at the end) in a separate method, how ugly would it be?
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.
Thanks, that passes now.
def save_datasets(self, datasets, filename=None, groups=None, header_attrs=None, engine=None, epoch=EPOCH, | ||
flatten_attrs=False, exclude_attrs=None, include_lonlats=True, pretty=False, | ||
compression=None, include_orig_name=True, numeric_name_prefix='CHANNEL_', **to_netcdf_kwargs): | ||
def combine_info(self, group_name, group_datasets, epoch, group_attrs, flatten_attrs, exclude_attrs, |
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.
Sorry, this got uglier than I was expecting. I think any sub-methods should be prefixed with _
as they probably aren't public API for users. The main ugliness with this refactor is the number of arguments that needed to be passed. Looking at the arguments it looks like most of them go to the _collect_datasets
call. Maybe that could be moved back into the for loop? Then the time if/else in one method and the group attrs in another method. So the for loop would essentially look like:
for group_name, group_datasets ...:
... = self._collect_datasets(...)
dataset = xr.Dataset(datas)
self._add_time_bounds(...)
self._add_group_attrs(...)
encoding, other_to_netcdf_kwargs = update_encoding(...)
return dataset, encoding, other_to_netcdf_kwargs
@zxdawn What is the status of this PR? Any chance of looking into the remaining points? |
Sometimes when we save datasets to NetCDF as groups, it will be useful to add the group attributes. In this PR, I add the option by passing
group_attrs
. It works as same asheader_attrs
.Here's an example:
Output of
ncdump -h test_cf_group.nc
: