Azzera filtri
Azzera filtri

How to extract and save water depth data from a 2dm file?

7 visualizzazioni (ultimi 30 giorni)
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);

Risposta accettata

Angelo Yeo
Angelo Yeo il 10 Nov 2023
Modificato: Angelo Yeo il 10 Nov 2023
You may have an issue with importing data. "fopen" and "textscan" are low-level functions, and it may be hard to get them to use properly.
Why don't you try readtable? It offers numerous options and can potentially import the data as per your requirements. Give the code below a shot to see if it accomplishes the desired data import:
%% Set up the Import Options and import the data
opts = delimitedTextImportOptions("NumVariables", 6);
% Specify range and delimiter
opts.DataLines = [3, Inf];
opts.Delimiter = " ";
% Specify column names and types
opts.VariableNames = ["MESH2D", "VarName2", "VarName3", "VarName4", "VarName5", "Var6"];
opts.SelectedVariableNames = ["MESH2D", "VarName2", "VarName3", "VarName4", "VarName5"];
opts.VariableTypes = ["string", "double", "double", "double", "double", "string"];
% Specify file level properties
opts.ExtraColumnsRule = "ignore";
opts.EmptyLineRule = "read";
opts.ConsecutiveDelimitersRule = "join";
opts.LeadingDelimitersRule = "ignore";
% Specify variable properties
opts = setvaropts(opts, ["MESH2D", "Var6"], "WhitespaceRule", "preserve");
opts = setvaropts(opts, ["MESH2D", "Var6"], "EmptyFieldRule", "auto");
% Import the data
homework = readtable("homework.txt", opts); % You may change it to 2dm file you work with.
head(homework)
MESH2D VarName2 VarName3 VarName4 VarName5 ______ ________ ________ ________ ________ "E3T" 1 2 11 12 "E3T" 2 11 1 10 "E3T" 3 1 11 2 "E3T" 4 2 12 3 "E3T" 5 3 12 13 "E3T" 6 13 4 3 "E3T" 7 14 4 13 "E3T" 8 14 5 4
My last comment is: utilizing the "Import Data" feature in MATLAB is worth mentioning as it holds significant power.

Più risposte (1)

Kyoung Moon Lee
Kyoung Moon Lee il 9 Nov 2023
Modificato: Kyoung Moon Lee il 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')

Categorie

Scopri di più su Data Import and Export in Help Center e File Exchange

Prodotti


Release

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by