Skip to content

Commit

Permalink
WIP: Don't worry about lat-lon-alt dependent case when transforming.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmilloy committed Apr 21, 2020
1 parent 4bb8a3b commit d057adf
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 46 deletions.
65 changes: 20 additions & 45 deletions podpac/core/coordinates/coordinates.py
Original file line number Diff line number Diff line change
Expand Up @@ -1355,27 +1355,25 @@ def transform(self, crs):
if tc:
cs[self.dims.index("lat")] = tc[0]
cs[self.dims.index("lon")] = tc[1]
if "alt" in self.dims:
cs[self.dims.index("alt")] = tc[2]
return Coordinates(cs, crs=crs, validate_crs=False)

# otherwise convert lat-lon to dependent coordinates
ilat = self.dims.index("lat")
ilon = self.dims.index("lon")
if ilat == ilon - 1:
c1, c2 = self["lat"], self["lon"]
elif ilon == ilat - 1:
c1, c2 = self["lon"], self["lat"]

c = DependentCoordinates(
np.meshgrid(c1.coordinates, c2.coordinates, indexing="ij"), dims=[c1.name, c2.name]
)
else:
ilat = self.dims.index("lat")
ilon = self.dims.index("lon")
if ilat == ilon - 1:
c1, c2 = self["lat"], self["lon"]
elif ilon == ilat - 1:
c1, c2 = self["lon"], self["lat"]

c = DependentCoordinates(
np.meshgrid(c1.coordinates, c2.coordinates, indexing="ij"), dims=[c1.name, c2.name]
)

# replace 'lat' and 'lon' entries with single 'lat,lon' entry
i = min(ilat, ilon)
cs.pop(i)
cs.pop(i)
cs.insert(i, c)
# replace 'lat' and 'lon' entries with single 'lat,lon' entry
i = min(ilat, ilon)
cs.pop(i)
cs.pop(i)
cs.insert(i, c)

# transform
ts = []
Expand All @@ -1392,12 +1390,8 @@ def _simplified_transform(self, crs, transformer):
""" Transform coordinates to simple Coordinates1d (instead of DependentCoordinates) if possible """

# check if we can simplify the coordinates by transforming a downsampled grid
if "alt" in self.dims:
dims = ["lat", "lon", "alt"]
else:
dims = ["lat", "lon"]
sample = [np.linspace(self[dim].coordinates[0], self[dim].coordinates[-1], 5) for dim in dims]
temp_coords = DependentCoordinates(np.meshgrid(*sample, indexing="ij"), dims=dims)
sample = [np.linspace(self[dim].coordinates[0], self[dim].coordinates[-1], 5) for dim in ["lat", "lon"]]
temp_coords = DependentCoordinates(np.meshgrid(*sample, indexing="ij"), dims=["lat", "lon"])
t = temp_coords._transform(transformer)

# if we get DependentCoordinates from the transform, they are not independent
Expand Down Expand Up @@ -1428,28 +1422,9 @@ def _simplified_transform(self, crs, transformer):
[self["lon"].coordinates, np.full_like(self["lon"].coordinates, self["lat"].coordinates.mean())],
name="lon_lat",
)
temp_coords._transform(transformer)["lon"].simplify()

# no alt coordinates, return now
if len(t) < 3:
return (t_lat, t_lon)

# alt
if isinstance(t[2], UniformCoordinates1d):
t_alt = clinspace(t[2].coordinates[0], t[2].coordinates[-1], self["alt"].size, name="alt")
else:
# compute the non-uniform coordinates (and simplify to uniform if they are *now* uniform)
temp_coords = StackedCoordinates(
[
self["alt"].coordinates,
np.full_like(self["alt"].coordinates, self["lat"].coordinates.mean()),
np.full_like(self["alt"].coordinates, self["lat"].coordinates.mean()),
],
name="alt_lon_lat",
)
t_alt = temp_coords._transform(transformer)["alt"].simplify()
t_lon = temp_coords._transform(transformer)["lon"].simplify()

return (t_lat, t_lon, t_alt)
return t_lat, t_lon

# ------------------------------------------------------------------------------------------------------------------
# Operators/Magic Methods
Expand Down
2 changes: 1 addition & 1 deletion podpac/core/coordinates/dependent_coordinates.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ def simplify(self):
i = self.dims.index(dim)
slc = slc_start.copy()
slc[i] = slice(None)
if not np.allclose(coords[i][tuple(slc)], coords[i], atol=1e-7):
if dim in ["lat", "lon"] and not np.allclose(coords[i][tuple(slc)], coords[i], atol=1e-7):
return self
coords[i] = ArrayCoordinates1d(coords[i][tuple(slc)].squeeze(), name=dim).simplify()

Expand Down

0 comments on commit d057adf

Please sign in to comment.