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

Pixel grid dimensions (40818x1) error fixed. #109

Merged
merged 6 commits into from
Dec 19, 2023
Merged
Changes from 2 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
64 changes: 56 additions & 8 deletions xee/ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import warnings

import affine
from itertools import cycle
dabhicusp marked this conversation as resolved.
Show resolved Hide resolved
import numpy as np
import pandas as pd
import pyproj
Expand Down Expand Up @@ -232,7 +233,7 @@ def __init__(
x_min_0, y_min_0, x_max_0, y_max_0 = _ee_bounds_to_bounds(
self.get_info['bounds']
)
# TODO(#40): Investigate data discrepancy (off-by-one) issue.

x_min, y_min = self.transform(x_min_0, y_min_0)
x_max, y_max = self.transform(x_max_0, y_max_0)
self.bounds = x_min, y_min, x_max, y_max
Expand Down Expand Up @@ -536,6 +537,44 @@ def _get_primary_coordinates(self) -> List[Any]:
]
return primary_coords

def _get_tile_from_EE(
dabhicusp marked this conversation as resolved.
Show resolved Hide resolved
self, tile_index: Tuple[Any, Union[str, int]]
) -> Tuple[slice, np.ndarray]:
"""Get a numpy array from EE for a specific bounding box (a 'tile')."""
tile_index, band_id = tile_index
bbox = self.project(
(tile_index[0], 0, tile_index[1], 1)
if band_id == 'longitude'
else (0, tile_index[0], 1, tile_index[1])
)
tile_idx = slice(tile_index[0], tile_index[1])
target_image = ee.Image.pixelLonLat()
return tile_idx, self.image_to_array(
target_image, grid=bbox, dtype=np.float32, bandIds=[band_id]
)

def process_coordinate_data(
dabhicusp marked this conversation as resolved.
Show resolved Hide resolved
self,
total_tile: int,
mahrsee1997 marked this conversation as resolved.
Show resolved Hide resolved
tile_size: int,
end_point: int,
coordinate_type: str,
) -> np.ndarray:
"""Process coordinate data using multithreading for longitude or latitude."""
data = [
(tile_size * i, min(tile_size * (i + 1), end_point))
for i in range(total_tile)
]
tiles = [None for _ in range(total_tile)]
dabhicusp marked this conversation as resolved.
Show resolved Hide resolved
with concurrent.futures.ThreadPoolExecutor() as pool:
for i, arr in pool.map(
self._get_tile_from_EE, list(zip(data, cycle([coordinate_type])))
):
tiles[i] = (
arr.tolist() if coordinate_type == 'longitude' else arr.tolist()[0]
)
return np.concatenate(tiles)

def get_variables(self) -> utils.Frozen[str, xarray.Variable]:
vars_ = [(name, self.open_store_variable(name)) for name in self._bands()]

Expand All @@ -551,15 +590,24 @@ def get_variables(self) -> utils.Frozen[str, xarray.Variable]:
f'ImageCollection due to: {e}.'
)

lnglat_img = ee.Image.pixelLonLat()
lon_grid = self.project((0, 0, v0.shape[1], 1))
lat_grid = self.project((0, 0, 1, v0.shape[2]))
lon = self.image_to_array(
lnglat_img, grid=lon_grid, dtype=np.float32, bandIds=['longitude']
if isinstance(self.chunks, dict):
# when the value of self.chunks = 'auto' or user-define.
self._apparent_chunks = self.chunks.copy()
dabhicusp marked this conversation as resolved.
Show resolved Hide resolved
else:
# when the value of self.chunks = -1
self._apparent_chunks = {k: 1 for k in self.PREFERRED_CHUNKS.keys()}
self._apparent_chunks['width'] = v0.shape[1]
self._apparent_chunks['height'] = v0.shape[2]

lon_total_tile = math.ceil(v0.shape[1] / self._apparent_chunks['width'])
lon = self.process_coordinate_data(
lon_total_tile, self._apparent_chunks['width'], v0.shape[1], 'longitude'
)
lat = self.image_to_array(
lnglat_img, grid=lat_grid, dtype=np.float32, bandIds=['latitude']
lat_total_tile = math.ceil(v0.shape[2] / self._apparent_chunks['height'])
lat = self.process_coordinate_data(
lat_total_tile, self._apparent_chunks['height'], v0.shape[2], 'latitude'
)

width_coord = np.squeeze(lon)
height_coord = np.squeeze(lat)

Expand Down
Loading