import glob
import numpy as np
import xarray as xr
import matplotlib.pyplot as plt
def get_input_files(file_extension):
files = glob.glob(f"*.{file_extension}")
for i, filename in enumerate(files):
print(f"{i+1}: {filename}")
file_num = int(input(f"Enter the number of the file you want to use: ")) - 1
if file_num >= 0 and file_num < len(files):
print(f"You selected {files[file_num]}")
return files[file_num]
else:
print("Invalid choice. Exiting.")
exit(1)
def read_nc_file(file_path):
try:
ds = xr.open_dataset(file_path)
return ds.depth.values, ds.x.values, ds.y.values
except Exception as e:
print(f"Error reading file {file_path}: {str(e)}")
def read_txt_file(file_path):
try:
with open(file_path, 'r') as file:
lines = file.readlines()
rays = []
ray_data = []
start_reading = False
for line in lines:
if "t x y kx ky" in line: start_reading = True
continue if start_reading and "END" in line: if ray_data: ray = np.array(ray_data)
rays.append(ray)
ray_data = [] start_reading = False
continue
if start_reading:
values = list(map(float, line.strip().split()))
ray_data.append(values)
if ray_data:
ray = np.array(ray_data)
rays.append(ray)
return rays
except Exception as e:
print(f"Error reading file {file_path}: {str(e)}")
def create_plot(depth_arr, x_arr, y_arr, rays):
fig, ax = plt.subplots()
im = ax.imshow(depth_arr, extent=[x_arr[0], x_arr[-1], y_arr[0], y_arr[-1]], origin='lower')
cbar = ax.figure.colorbar(im, ax=ax)
cbar.ax.set_ylabel("Depth [m]", rotation=-90, va="bottom")
num_ticks = 10
x_ticks = np.linspace(x_arr[0], x_arr[-1], num_ticks, dtype=int)
y_ticks = np.linspace(y_arr[0], y_arr[-1], num_ticks, dtype=int)
ax.set_xticks(x_ticks)
ax.set_yticks(y_ticks)
ax.set_xticklabels(x_ticks)
ax.set_yticklabels(y_ticks)
ax.set_xlabel("x [m]")
ax.set_ylabel("y [m]")
ax.set_title("ray paths")
for ray in rays:
t, x, y, kx, ky = ray.T ax.plot(x, y, linewidth=2, color='k')
plt.show()
if __name__ == "__main__":
nc_file = get_input_files('nc')
depth_arr, x_arr, y_arr = read_nc_file(nc_file)
txt_file = get_input_files('txt')
rays = read_txt_file(txt_file)
create_plot(depth_arr, x_arr, y_arr, rays)