Skip to content

Commit

Permalink
Merge pull request #282 from ASFHyP3/s1-crop-fix
Browse files Browse the repository at this point in the history
Ensure all encoding attributes are carried over when cropping
  • Loading branch information
jhkennedy authored Jul 3, 2024
2 parents 035115f + e050c21 commit 95a884d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 20 deletions.
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

0 comments on commit 95a884d

Please sign in to comment.