How to extract and save water depth data from a 2dm file?
Show older comments
Do you know about a program called 'Surface-water Modeling System (SMS)'? I am doing homework to create a 'homework_depth.dat' file using the 'homework.2dm' file created by the SMS program to meet the conditions described later. Currently, I created the code as below, but the depth data is not recorded in the 'homework_depth.dat' file. It seems that depth data extraction is not working properly with this code alone. How should I modify the code below to make it work properly? I am uploading the work I have done so far.
The conditions are as follows:
Deepest: obc (far left of grid in .2dm file)
Shallowest: Freshwater input boundary (far right of grid in .2dm file)
Deepest water depth: 50m
Shallowest water depth: 30m
Constant decrease only in the X-axis direction
Scatter interval: 100m each on the x-axis, 50m each on the y-axis
%% Code
clear all; clc; close all;
pwd;
addpath('./01__Data')
conf.base = fullfile(pwd, '01__Data', 'make_input');
filePath = fullfile(conf.base, 'homework.2dm');
outputFilePath = fullfile(pwd, '01__Data', 'homework_depth.dat');
fileID = fopen(filePath, 'r');
data = textscan(fileID, '%f %f %f', 'HeaderLines', 1);
fclose(fileID);
depth = data{3};
minDepth = 30;
maxDepth = 50;
xGrid = data{1};
yGrid = data{2};
depth(xGrid == min(xGrid)) = maxDepth;
depth(xGrid == max(xGrid)) = minDepth;
xInterval = 100;
yInterval = 50;
[~, index] = sort(xGrid, 'descend');
depthSorted = depth(index);
fileID_dat = fopen(outputFilePath, 'w');
for i = 1:length(depthSorted)
fprintf(fileID_dat, '%f\n', depthSorted(i));
end
fclose(fileID_dat);
Accepted Answer
More Answers (1)
Kyoung Moon Lee
on 9 Nov 2023
Edited: Kyoung Moon Lee
on 9 Nov 2023
use meshgrid
In my opinion, next time don't post the original homework file as is.
% enther lon, lat, depth
x = [];
y = [];
z = [];
% make grid
[x y] = meshgird(x,y);
% save file
writematrix([x(:),y(:),z(:)],'homework_depth.dat')
Categories
Find more on Functions in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!