-
Notifications
You must be signed in to change notification settings - Fork 52
Scale offset from item asset #202
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
Changes from 6 commits
36e07d3
dae0481
1b93db6
1dcc7b3
5dbf54c
1179df4
559b03f
8bf5839
4f1d951
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,7 +32,6 @@ def _curthread(): | |
|
||
# /TODO | ||
|
||
|
||
# Default GDAL configuration options | ||
DEFAULT_GDAL_ENV = LayeredEnv( | ||
always=dict( | ||
|
@@ -64,7 +63,10 @@ def _curthread(): | |
# See `ThreadLocalRioDataset` for more. | ||
# https://github.com/pangeo-data/pangeo-example-notebooks/issues/21#issuecomment-432457955 | ||
# https://gdal.org/drivers/raster/vrt.html#multi-threading-issues | ||
|
||
MULTITHREADED_DRIVER_ALLOWLIST = {"GTiff"} | ||
DEFAULT_SCALE = 1 | ||
DEFAULT_OFFSET = 0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: I'd actually rather not have these be pulled out into variables, because they're not something that could ever change (DEFAULT_SCALE is never going to be 2, for instance), and the fact that they're 0 and 1 are somewhat significant to the code ( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds good, I had just done this because then we have the number 1 and 0 hard-coded in numerous places. You are right that they will not change. |
||
|
||
|
||
class ThreadsafeRioDataset(Protocol): | ||
|
@@ -280,7 +282,6 @@ class PickleState(TypedDict): | |
resampling: Resampling | ||
dtype: np.dtype | ||
fill_value: Union[int, float] | ||
rescale: bool | ||
gdal_env: Optional[LayeredEnv] | ||
errors_as_nodata: Tuple[Exception, ...] | ||
|
||
|
@@ -303,15 +304,15 @@ def __init__( | |
resampling: Resampling, | ||
dtype: np.dtype, | ||
fill_value: Union[int, float], | ||
rescale: bool, | ||
scale_offset: Tuple[float, float], | ||
gjoseph92 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
gdal_env: Optional[LayeredEnv] = None, | ||
errors_as_nodata: Tuple[Exception, ...] = (), | ||
) -> None: | ||
self.url = url | ||
self.spec = spec | ||
self.resampling = resampling | ||
self.dtype = dtype | ||
self.rescale = rescale | ||
self.scale_offset = scale_offset | ||
self.fill_value = fill_value | ||
self.gdal_env = gdal_env or DEFAULT_GDAL_ENV | ||
self.errors_as_nodata = errors_as_nodata | ||
|
@@ -399,14 +400,16 @@ def read(self, window: Window, **kwargs) -> np.ndarray: | |
|
||
raise RuntimeError(msg) from e | ||
|
||
if self.rescale: | ||
scale, offset = reader.scale_offset | ||
if scale != 1: | ||
result *= scale | ||
if offset != 0: | ||
result += offset | ||
if result.dtype != self.dtype: | ||
result = result.astype(self.dtype, copy=False) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am happy to remove and delegate to #208 as it is authored by you. What is your timeline for merging that? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was hoping to hear confirmation from @Berhinj that it solves the problem before merging, since I don't have a reproducer. However, I'm quite confident it'll work, so I'll just merge it now so we can get these both in. I think I'd slightly prefer that approach to what you have here, just because it maybe saves one copy (letting GDAL read the data into an array of the desired output dtype, vs copying into a new array). There are so many copies internally already though, I doubt it matters much. |
||
|
||
scale, offset = self.scale_offset | ||
|
||
if scale != DEFAULT_SCALE: | ||
result *= scale | ||
if offset != DEFAULT_OFFSET: | ||
result += offset | ||
|
||
result = result.astype(self.dtype, copy=False) | ||
result = np.ma.filled(result, fill_value=self.fill_value) | ||
return result | ||
|
||
|
@@ -435,7 +438,6 @@ def __getstate__( | |
"resampling": self.resampling, | ||
"dtype": self.dtype, | ||
"fill_value": self.fill_value, | ||
"rescale": self.rescale, | ||
gjoseph92 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"gdal_env": self.gdal_env, | ||
"errors_as_nodata": self.errors_as_nodata, | ||
} | ||
|
Uh oh!
There was an error while loading. Please reload this page.