Skip to content

Commit 699e72d

Browse files
committed
Merging functions from seabed_tools to geography
- Deleting old functions in seabed_tools that are unused nowadays (getPlotBounds, getCoast, getDepthFromBathymetryMesh) - - convertLatLong2Meters - old function that has now been updated in geography - - processASC - basically the old version of getMapBathymetry - also in geography - Merging writeBathymetryFile functions between seabed_tools and geography - processGeoTiff now moved to geography since it deals with processing geographic-related information
1 parent c4a8536 commit 699e72d

File tree

2 files changed

+108
-424
lines changed

2 files changed

+108
-424
lines changed

famodel/geography.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,14 +337,21 @@ def writeBathymetryFile(moorpy_bathymetry_filename, bathXs, bathYs, bath_depths,
337337
'''Write a MoorDyn/MoorPy-style bathymetry text file based on provided
338338
x and y grid line values and a 2D array of depth values.'''
339339

340+
# open the file
340341
f = open(os.path.join(os.getcwd(), moorpy_bathymetry_filename), 'w')
342+
341343
f.write('--- MoorPy Bathymetry Input File ---\n')
342344
f.write(f'nGridX {len(bathXs)}\n')
343345
f.write(f'nGridY {len(bathYs)}\n')
344346
f.write(f' ')
347+
348+
# x-coordinates
345349
for ix in range(len(bathXs)):
346350
f.write(f'{bathXs[ix]:.2f} ')
347351
f.write('\n')
352+
#f.write(" ".join(map(str, grid_x)) + "\n") # old version from seabed_tools
353+
354+
# y-coordintes
348355
for iy in range(len(bathYs)):
349356
f.write(f'{bathYs[iy]:.2f} ')
350357
for id in range(len(bath_depths[iy])):
@@ -353,6 +360,11 @@ def writeBathymetryFile(moorpy_bathymetry_filename, bathXs, bathYs, bath_depths,
353360
else:
354361
f.write(f'{bath_depths[iy,id]:8.3f} ')
355362
f.write('\n')
363+
#for i, y in enumerate(grid_y): # alternative writing version
364+
#row = [y] + list(grid_depth[i, :])
365+
#f.write(" ".join(map(str, row)) + "\n")
366+
367+
# close the file
356368
f.close()
357369

358370

@@ -407,6 +419,94 @@ def getLeaseAndBathymetryInfo(lease_name, bathymetry_file, bath_ncols=100, bath_
407419

408420

409421

422+
423+
def processGeotiff(filename, lat, lon, outfilename="processGeotiff.txt", **kwargs):
424+
'''Process a geotiff file containing bathymetry (or other info)
425+
and convert into a rectangular bathymetry grid in units of m relative to
426+
the project reference point.
427+
428+
Parameters
429+
----------
430+
filename : string
431+
Path and name of geotiff file to load.
432+
lat : float
433+
lattitude of reference point to use for array y grid
434+
long : float
435+
lattitude of reference point to use for array x grid
436+
outfilename : string, optional
437+
If provided, writes a MoorDyn/MoorPy style bathymetry file
438+
kwargs : dict
439+
Optional extra arguments that will be relayed to convertBathymetry2Meters,
440+
can be used to specify desired x and y grid coordinates.
441+
442+
Returns
443+
-------
444+
Xs : array
445+
x values of grid points [m]
446+
Ys : array
447+
y values of grid points [m]
448+
depths : 2D array
449+
water depth grid (positive down) [m]
450+
'''
451+
452+
import rasterio
453+
import rasterio.plot
454+
455+
tiff = rasterio.open(filename) # load the geotiff file
456+
457+
#rasterio.plot.show(tiff) # plot it to see that it works
458+
459+
# note: a CRS is stored with the geotiff, accessible with tiff.crs
460+
461+
# Get lattitude and longitude grid values
462+
#_, longs = rasterio.transform.xy(tiff.transform, range(tiff.height),0)
463+
#lats, _ = rasterio.transform.xy(tiff.transform, 0, range(tiff.width-1,-1,-1))
464+
height, width = tiff.shape
465+
cols, rows = np.meshgrid(np.arange(width), np.arange(height))
466+
longs_mesh, lats_mesh = rasterio.transform.xy(tiff.transform, rows, cols)
467+
longs = np.array(longs_mesh)[0,:]
468+
lats = np.flip(np.array(lats_mesh)[:,0])
469+
# lats data provided from top left corner, i.e., latitudes are descending. It seems that the following interpolation functions (getDepthFromBathymetry)
470+
# can only work if latitudes start small and increase, meaning that the first latitude entry has to be the bottom left corner
471+
472+
# Depth values in numpy array form
473+
depths = -tiff.read(1)
474+
depths = np.flipud(depths)
475+
# because the interpolation functions require the latitude array to go from small to big (i.e., the bottom left corner), we need to flip the depth matrix to align
476+
# it will all get sorted out later to what it should be geographically when plotting in MoorPy
477+
478+
479+
# extract the coordinate reference systems needed (pyproj CRS objects)
480+
latlong_crs = tiff.crs
481+
#target_crs = getTargetCRS(lon, lat) # should be UTM 10N for Humboldt/California coast
482+
target_crs = getCustomCRS(lon, lat) # get custom CRS centered around the lat/long point you want
483+
484+
# get the centroid/reference location in lat/long coordinates
485+
centroid = (lon, lat)
486+
487+
# get the centroid/reference location in target_crs coordinates
488+
#centroid_utm = (lon, lat)
489+
_, _, centroid_utm = convertLatLong2Meters([centroid[0]], [centroid[1]], centroid, latlong_crs, target_crs, return_centroid=True)
490+
491+
# set the number of rows and columns to use in the MoorPy bathymetry file
492+
ncols = 100
493+
nrows = 100
494+
495+
# convert bathymetry to meters
496+
bath_xs, bath_ys, bath_depths = convertBathymetry2Meters(longs, lats, depths,
497+
centroid, centroid_utm,
498+
latlong_crs, target_crs,
499+
ncols, nrows, **kwargs)
500+
# export to MoorPy-readable file
501+
writeBathymetryFile(outfilename, bath_xs, bath_ys, bath_depths)
502+
503+
return bath_xs, bath_ys, bath_depths
504+
505+
506+
507+
508+
509+
410510
def getSoilType(x, y, centroid, latlong_crs, custom_crs, soil_file):
411511
"""Function to return the name of the soil below a specific x/y coordinate by creating shapely polygons based on the shapefile data.
412512
It loops through all polygons in the shapefile and if the x/y position is contained in that polygon, it returns the soil of that polygon."""

0 commit comments

Comments
 (0)