-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #69 from noaa-ocs-modeling/bugfix/hfuntype
Hfun Collector Valid Input Types
- Loading branch information
Showing
5 changed files
with
274 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,7 @@ | ||
|
||
import os | ||
os.system(""" | ||
ls /tmp/test_dem.tif > /dev/null | ||
if [ $? -eq 0 ]; then exit; fi | ||
wget https://coast.noaa.gov/htdata/raster2/elevation/NCEI_ninth_Topobathy_2014_8483/northeast_sandy/ncei19_n40x75_w073x75_2015v1.tif -O /tmp/fullsize_dem.tif | ||
gdalwarp -tr 0.0005 0.0005 -r average -overwrite /tmp/fullsize_dem.tif /tmp/test_dem.tif | ||
""") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
import rasterio as rio | ||
import numpy as np | ||
from pyproj import CRS | ||
from jigsawpy import jigsaw_msh_t | ||
|
||
|
||
def create_rectangle_mesh(nx, ny, holes, x_extent=None, y_extent=None): | ||
""" | ||
Note: | ||
x = x-index | ||
y = y-index | ||
node-index(node-id) | ||
25(26) 29(30) | ||
5 *---*---*---*---* | ||
| \ | \ | \ | \ | | ||
4 *---*---*---*---* | ||
| \ | \ | \ | \ | | ||
3 *---*---*---*---* | ||
| \ | | \ | \ | | ||
2 *---*---*---*---* | ||
| \ | \ | \ | \ | | ||
1 *---*---*---*---* | ||
| \ | \ | \ | \ | | ||
0 *---*---*---*---* | ||
0(1) 4(5) | ||
0 1 2 3 4 | ||
""" | ||
|
||
if x_extent is None: | ||
x_range = range(nx) | ||
else: | ||
x_range = np.linspace(x_extent[0], x_extent[1], nx) | ||
|
||
if y_extent is None: | ||
y_range = range(ny) | ||
else: | ||
y_range = np.linspace(y_extent[0], y_extent[1], nx) | ||
|
||
X, Y = np.meshgrid(x_range, y_range) | ||
verts = np.array(list(zip(X.ravel(), Y.ravel()))) | ||
cells = [] | ||
for j in range(ny - 1): | ||
for i in range(nx - 1): | ||
if (i + 1) + ((nx-1) * j) in holes: | ||
continue | ||
cells.append([j * nx + i, j * nx + (i + 1), (j + 1) * nx + i]) | ||
cells.append([j * nx + (i + 1), (j + 1) * nx + (i + 1), (j + 1) * nx + i]) | ||
# NOTE: Everywhere is above 0 (auto: land) unless modified later | ||
vals = np.ones((len(verts), 1)) * 10 | ||
|
||
# TODO: Replace with "make_mesh" util function | ||
mesh_msht = jigsaw_msh_t() | ||
mesh_msht.ndims = +2 | ||
mesh_msht.mshID = 'euclidean-mesh' | ||
mesh_msht.tria3 = np.array( | ||
[(c, 0) for c in cells], dtype=jigsaw_msh_t.TRIA3_t | ||
) | ||
mesh_msht.vert2 = np.array( | ||
[(v, 0) for v in verts], dtype=jigsaw_msh_t.VERT2_t | ||
) | ||
mesh_msht.value = np.array( | ||
vals, dtype=jigsaw_msh_t.REALS_t | ||
) | ||
return mesh_msht | ||
|
||
|
||
# TODO: Move these helper functions to `utils` | ||
def raster_from_numpy( | ||
filename, | ||
data, | ||
mgrid, | ||
crs=CRS.from_epsg(4326) | ||
): | ||
x = mgrid[0][:, 0] | ||
y = mgrid[1][0, :] | ||
res_x = (x[-1] - x[0]) / data.shape[1] | ||
res_y = (y[-1] - y[0]) / data.shape[0] | ||
transform = rio.transform.Affine.translation( | ||
x[0], y[0] | ||
) * rio.transform.Affine.scale(res_x, res_y) | ||
if not isinstance(crs, CRS): | ||
crs = CRS.from_user_input(crs) | ||
|
||
with rio.open( | ||
filename, | ||
'w', | ||
driver='GTiff', | ||
height=data.shape[0], | ||
width=data.shape[1], | ||
count=1, | ||
dtype=data.dtype, | ||
crs=crs, | ||
transform=transform, | ||
) as dst: | ||
dst.write(data, 1) | ||
|
||
|
||
def msht_from_numpy( | ||
coordinates, | ||
triangles, | ||
crs=CRS.from_epsg(4326) | ||
): | ||
if not isinstance(crs, CRS): | ||
crs = CRS.from_user_input(crs) | ||
mesh = jigsaw_msh_t() | ||
mesh.mshID = 'euclidean-mesh' | ||
mesh.ndims = +2 | ||
mesh.crs = crs | ||
mesh.vert2 = np.array( | ||
[(coord, 0) for coord in coordinates], | ||
dtype=jigsaw_msh_t.VERT2_t | ||
) | ||
mesh.tria3 = np.array( | ||
[(index, 0) for index in triangles], | ||
dtype=jigsaw_msh_t.TRIA3_t | ||
) | ||
|
||
return mesh | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.