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

Ensure all encoding attributes are carried over when cropping #282

Merged
merged 2 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ and uses [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [0.18.0]
### Added
* The Sentinel-1 correction workflow will now calculate and write the M11/M12 conversion matrices to a netCDF file.

### Fixed
* `hyp3_autorift.crop` will now preserve the `add_offset` and `scale_factor` encoding attributes for all variables, and in particular, for the M11/M12 conversion matrices.

### Removed
* Support for Python 3.8 has been dropped.

Expand Down
27 changes: 7 additions & 20 deletions src/hyp3_autorift/crop.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,7 @@
import pyproj
import xarray as xr


ENCODING_TEMPLATE = {
'interp_mask': {'_FillValue': 0.0, 'dtype': 'ubyte', "zlib": True, "complevel": 2, "shuffle": True},
'chip_size_height': {'_FillValue': 0.0, 'dtype': 'ushort', "zlib": True, "complevel": 2, "shuffle": True},
'chip_size_width': {'_FillValue': 0.0, 'dtype': 'ushort', "zlib": True, "complevel": 2, "shuffle": True},
'M11': {'_FillValue': -32767, 'dtype': 'short', "zlib": True, "complevel": 2, "shuffle": True},
'M12': {'_FillValue': -32767, 'dtype': 'short', "zlib": True, "complevel": 2, "shuffle": True},
'v': {'_FillValue': -32767.0, 'dtype': 'short', "zlib": True, "complevel": 2, "shuffle": True},
'vx': {'_FillValue': -32767.0, 'dtype': 'short', "zlib": True, "complevel": 2, "shuffle": True},
'vy': {'_FillValue': -32767.0, 'dtype': 'short', "zlib": True, "complevel": 2, "shuffle": True},
'v_error': {'_FillValue': -32767.0, 'dtype': 'short', "zlib": True, "complevel": 2, "shuffle": True},
'va': {'_FillValue': -32767.0, 'dtype': 'short', "zlib": True, "complevel": 2, "shuffle": True},
'vr': {'_FillValue': -32767.0, 'dtype': 'short', "zlib": True, "complevel": 2, "shuffle": True},
'x': {'_FillValue': None},
'y': {'_FillValue': None}
}
ENCODING_ATTRS = ['_FillValue', 'dtype', "zlib", "complevel", "shuffle", 'add_offset', 'scale_factor']


def crop_netcdf_product(netcdf_file: Path) -> Path:
Expand Down Expand Up @@ -114,10 +99,12 @@ def crop_netcdf_product(netcdf_file: Path) -> Path:
chunk_lines = np.min([np.ceil(8192 / dims['y']) * 128, dims['y']])
two_dim_chunks_settings = (chunk_lines, dims['x'])

encoding = ENCODING_TEMPLATE.copy()
if not netcdf_file.name.startswith('S1'):
for radar_variable in ['M11', 'M12', 'va', 'vr']:
del encoding[radar_variable]
encoding = {}
for variable in ds.data_vars.keys():
if variable in ['img_pair_info', 'mapping']:
continue
attributes = {attr: ds[variable].encoding[attr] for attr in ENCODING_ATTRS if attr in ds[variable].encoding}
encoding[variable] = attributes

for _, attributes in encoding.items():
if attributes['_FillValue'] is not None:
Expand Down