Skip to content

Commit

Permalink
FIX: Fixing rasterion get_transform to use more general machinery. …
Browse files Browse the repository at this point in the history
…Also updating unit tests to catch an error in case of descening 'lat' coordinates.
  • Loading branch information
mpu-creare committed Apr 20, 2020
1 parent 89ad856 commit 0ca7e5c
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 43 deletions.
39 changes: 21 additions & 18 deletions podpac/core/cache/test/test_cache_stores.py
Expand Up @@ -327,24 +327,27 @@ def teardown_method(self):
shutil.rmtree(self.test_cache_dir, ignore_errors=True)

def test_cache_dir(self):
# absolute path
podpac.settings["DISK_CACHE_DIR"] = self.test_cache_dir
expected = self.test_cache_dir
store = DiskCacheStore()
store.put(NODE1, 10, "mykey1")
assert store.find(NODE1, "mykey1").startswith(expected)
store.clear()

# relative path
podpac.settings["DISK_CACHE_DIR"] = "_testcache_"
expected = os.path.join(
os.environ.get("XDG_CACHE_HOME", os.path.join(os.path.expanduser("~"), ".config", "podpac")), "_testcache_"
)
store = DiskCacheStore()
store.clear()
store.put(NODE1, 10, "mykey1")
assert store.find(NODE1, "mykey1").startswith(expected)
store.clear()
with podpac.settings:

# absolute path
podpac.settings["DISK_CACHE_DIR"] = self.test_cache_dir
expected = self.test_cache_dir
store = DiskCacheStore()
store.put(NODE1, 10, "mykey1")
assert store.find(NODE1, "mykey1").startswith(expected)
store.clear()

# relative path
podpac.settings["DISK_CACHE_DIR"] = "_testcache_"
expected = os.path.join(
os.environ.get("XDG_CACHE_HOME", os.path.join(os.path.expanduser("~"), ".config", "podpac")),
"_testcache_",
)
store = DiskCacheStore()
store.clear()
store.put(NODE1, 10, "mykey1")
assert store.find(NODE1, "mykey1").startswith(expected)
store.clear()

def test_size(self):
store = self.Store()
Expand Down
9 changes: 6 additions & 3 deletions podpac/core/coordinates/coordinates1d.py
Expand Up @@ -201,9 +201,12 @@ def get_area_bounds(self, boundary):
---------
boundary : float, timedelta, array, None
Boundary offsets in this dimension.
* For a centered uniform boundary, use a single positive float or timedelta offset
* For a uniform boundary (segment or polygon), use an array of float or timedelta offsets
* For a fully specified boundary, use an array of boundary arrays, one per coordinate
* For a centered uniform boundary (same for every coordinate), use a single positive float or timedelta
offset. This represents the "total segment length" / 2.
* For a uniform boundary (segment or polygon same for every coordinate), use an array of float or
timedelta offsets
* For a fully specified boundary, use an array of boundary arrays (2-D array, N_coords x boundary spec),
one per coordinate. The boundary_spec can be a single number, two numbers, or an array of numbers.
* For point coordinates, use None.
Returns
Expand Down
25 changes: 4 additions & 21 deletions podpac/core/interpolation/interpolators.py
Expand Up @@ -269,27 +269,13 @@ def interpolate(self, udims, source_coordinates, source_data, eval_coordinates,
self.interpolate, keep_dims, udims, source_coordinates, source_data, eval_coordinates, output_data
)

def get_rasterio_transform(c):
west = c["lon"].bounds[0] - c["lon"].step / 2.0
east = c["lon"].bounds[1] + c["lon"].step / 2.0
south = c["lat"].bounds[0] - c["lat"].step / 2.0
north = c["lat"].bounds[1] + c["lat"].step / 2.0
cols = c["lon"].size
rows = c["lat"].size
return transform.from_bounds(west, south, east, north, cols, rows)

with rasterio.Env():
# src_transform = transform.Affine.from_gdal(*source_coordinates.geotransform)
src_transform = get_rasterio_transform(source_coordinates)
src_transform = transform.Affine.from_gdal(*source_coordinates.geotransform)
src_crs = {"init": source_coordinates.crs}
# Need to make sure array is c-contiguous
if source_coordinates["lat"].is_descending:
source = np.ascontiguousarray(source_data.data)
else:
source = np.ascontiguousarray(source_data.data[::-1, :])
source = np.ascontiguousarray(source_data.data)

# dst_transform = transform.Affine.from_gdal(*eval_coordinates.geotransform)
dst_transform = get_rasterio_transform(eval_coordinates)
dst_transform = transform.Affine.from_gdal(*eval_coordinates.geotransform)
dst_crs = {"init": eval_coordinates.crs}
# Need to make sure array is c-contiguous
if not output_data.data.flags["C_CONTIGUOUS"]:
Expand All @@ -308,10 +294,7 @@ def get_rasterio_transform(c):
dst_nodata=np.nan,
resampling=getattr(Resampling, self.method),
)
if eval_coordinates["lat"].is_descending:
output_data.data[:] = destination
else:
output_data.data[:] = destination[::-1, :]
output_data.data[:] = destination

return output_data

Expand Down
2 changes: 1 addition & 1 deletion podpac/core/interpolation/test/test_interpolators.py
Expand Up @@ -248,7 +248,7 @@ def test_interpolate_rasterio_descending(self):
"""should handle descending"""

source = np.random.rand(5, 5)
coords_src = Coordinates([clinspace(0, 10, 5), clinspace(0, 10, 5)], dims=["lat", "lon"])
coords_src = Coordinates([clinspace(10, 0, 5), clinspace(0, 10, 5)], dims=["lat", "lon"])
coords_dst = Coordinates([clinspace(2, 12, 5), clinspace(2, 12, 5)], dims=["lat", "lon"])

node = MockArrayDataSource(
Expand Down

0 comments on commit 0ca7e5c

Please sign in to comment.