Skip to content

Commit

Permalink
Remove log file output and add docstrings to init
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrus89 committed Apr 2, 2024
1 parent 14de549 commit d692111
Showing 1 changed file with 23 additions and 25 deletions.
48 changes: 23 additions & 25 deletions compass/ocean/tests/tides/init/calculate_wave_drag.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@ class CalculateWaveDrag(Step):
Attributes
----------
bathy_file : str
File name for the blended RTopo/GEBCO pixel file
mesh_file : str
File name for the culled mesh from the mesh test case
bouy_file : str
File name for the WOA bouyancy data
output_file : str
File name for the output file containing wave drag information
"""

def __init__(self, test_case, mesh):
Expand All @@ -27,6 +38,9 @@ def __init__(self, test_case, mesh):
test_case : compass.ocean.tests.tides.init.Init
The test case this step belongs to
mesh : compass.ocean.tests.tides.mesh.Mesh
The test case the produces the mesh for this case
"""

super().__init__(test_case=test_case, name='wave_drag',
Expand All @@ -43,12 +57,12 @@ def __init__(self, test_case, mesh):

mesh_path = mesh.steps['cull_mesh'].path
self.add_input_file(
filename='mesh.nc',
filename=self.mesh_file,
work_dir_target=f'{mesh_path}/culled_mesh.nc')

bathy_path = mesh.steps['pixel'].path
self.add_input_file(
filename='bathy.nc',
filename=self.bathy_file,
work_dir_target=f'{bathy_path}/'
'RTopo_2_0_4_GEBCO_v2023_30sec_pixel.nc')

Expand All @@ -57,7 +71,7 @@ def __init__(self, test_case, mesh):

def interpolate_data_to_grid(self, grid_file, data_file, var):
"""
Interpolate a variable from a data file to an the MPAS grid
"""

# Open files
Expand All @@ -68,28 +82,16 @@ def interpolate_data_to_grid(self, grid_file, data_file, var):
lon_data = data_nc.variables['longitude'][:]
lat_data = data_nc.variables['latitude'][:]
nsnaps = 1
print(np.amax(lon_data), 'WOA2013 max lon data',
np.amin(lon_data), 'min lon data')
print(np.amax(lat_data), 'WOA2013 max lat data',
np.amin(lat_data), 'WOA2013 min lat data')

# Get grid from grid file
lon_grid = np.mod(grid_nc.variables['lonEdge'][:] + np.pi,
2.0 * np.pi) - np.pi
lon_grid = lon_grid * 180.0 / np.pi
lat_grid = grid_nc.variables['latEdge'][:] * 180.0 / np.pi
print(np.amax(lon_grid), 'max longitude in MPAS mesh',
np.amin(lon_grid), 'min lon mesh')
print(np.amax(lat_grid), 'max lat in MPAS mesh',
np.amin(lat_grid), 'min lat mesh')

grid_points = np.column_stack((lon_grid, lat_grid))
print(np.shape(grid_points))
nEdges = lon_grid.size
interp_data = np.zeros((nsnaps, nEdges))
print(interp_data.shape, 'interp')
print(np.amin(lon_grid), np.amax(lon_grid))
print(np.amin(lat_grid), np.amax(lat_grid))

# Interpolate timesnaps
print(f'Interpolating {var}')
Expand All @@ -110,7 +112,7 @@ def interpolate_data_to_grid(self, grid_file, data_file, var):

def run(self):
"""
Run the step to calculate wave drag information
"""

# Open Datasets
Expand Down Expand Up @@ -144,7 +146,6 @@ def run(self):
del xmesh, ymesh, R

for tile in range(indx.size - 1):
print('Tile', tile, '/', indx.size - 2)

head = indx[tile + 0] + 1
tail = indx[tile + 1] + 1
Expand Down Expand Up @@ -173,14 +174,12 @@ def run(self):
ylat_nn = [[] for _ in range(nEdges)]
print("Making lists")
for i in range(np.size(ymid)):
print('xlon', i, '/', np.size(xmid) - 1)
for j in range(np.size(xmid)):
ylat_nn, xlon_nn = self.make_nn_lists(ylat_nn, xlon_nn,
near, i, j)

print("Calculating stats")
for edge in range(nEdges):
print(edge, '/', nEdges)
bed_slope_edge, sd, xGradEdge, yGradEdge = self.calc_stats(
xlon_nn, ylat_nn, near, bed_slope,
edge, xmid, ymid, elev, xgrad, ygrad)
Expand All @@ -203,7 +202,7 @@ def run(self):
xGradEdges = self.fix_nans(xGradEdges, md)
while (np.sum(np.isnan(yGradEdges)) > 0):
print("yGradEdges", np.sum(np.isnan(stddev)))
# yGradEdge = self.fix_nans(yGradEdges, md)
yGradEdge = self.fix_nans(yGradEdges, md)

# Remove any 0s from the bottom Depth
# bottomDepthEdges[bottomDepthEdges==0.0] = 0.01
Expand All @@ -228,11 +227,10 @@ def run(self):

def fix_nans(self, data, mesh_nc):
"""
Replace NaN values with average of surrounding (non-NaN) values
"""
if (np.sum(np.isnan(data))):
edgesOnEdge = mesh_nc.edgesOnEdge.data
# nEdges = mesh_nc.nEdges.data[-1]+1

nanEdges = np.isnan(data)
for edge in range(mesh_nc.nEdges.data[-1]):
Expand All @@ -247,7 +245,7 @@ def fix_nans(self, data, mesh_nc):

def make_nn_lists(self, ylat_nn, xlon_nn, near, i, j):
"""
Create the nearest neighbor lists
"""
ylat_nn[near[i, j]].append(i)
xlon_nn[near[i, j]].append(j)
Expand All @@ -256,7 +254,7 @@ def make_nn_lists(self, ylat_nn, xlon_nn, near, i, j):
def calc_stats(self, xlon_nn, ylat_nn, near, bed_slope,
edge, xlon, ylat, elev, xgrad, ygrad):
"""
Calculate the mean bathymetry slopes/gradients and stadard deviation
"""

# Prepare for calculating area integral
Expand Down Expand Up @@ -302,7 +300,7 @@ def calc_stats(self, xlon_nn, ylat_nn, near, bed_slope,

def polyfit2d(self, X, Y, Z):
"""
Calculate linear least squares fit for standard deviation calculation
"""
# X, Y = np.meshgrid(x, y, copy=False)
# X = X.flatten()
Expand Down

0 comments on commit d692111

Please sign in to comment.