フローデータを読み込み、最終プロットのためにデータをグリッドファイルに配布しようとしています。私は処理のためのMATLABコードを持っており、正常に動作します。私はこのコードをPythonに移植する必要がありますが、私は初心者です。デバッグ時に常にクラッシュする、誰かが私が間違っていることを教えてもらえますか?前もって感謝します!バイナリデータを読み込んでグリッドファイルPythonでデータを配布する
データファイル(合計で100メガバイト程度):
https://www.dropbox.com/sh/3gtsmatq56pm0gc/AADUnNLjdrELjvdUy4wtDiiBa?dl=0
MATLABコード
%% Read Grid grid file is used as a guide for the positon where data is put
fid = fopen('FLOW_phys_GRID_1.xyz', 'r');
a = fread(fid, 3, 'int');
Nx = a(1); % number of points in x direction
Ny = a(2); % number of points in y direction
Nz = a(3); % numebr of points in z direction
xx = fread(fid, Nx*Ny*Nz, 'float');
yy = fread(fid, Nx*Ny*Nz, 'float');
xx = reshape(xx, [Nx, Ny]);
yy = reshape(yy, [Nx, Ny]);
fclose(fid);
x = squeeze(xx(:,1));
y = squeeze(yy(1,:));
%% Read Data
fid = fopen('FLOW_phys.raw', 'r'); %flow data in binary format
a = fread(fid, 3, 'int');
Nx = a(1); % number of points in x direction
Ny = a(2); % number of points in y direction
Nz = a(3); % number of points in z direction
Ma = fread(fid, 1, 'float');
some_num = fread(fid, 1, 'float');
Re = fread(fid, 1, 'float');
time = fread(fid, 1, 'float');
xx1 = fread(fid, 2*Nx*Ny*Nz, 'float');
xx1 = reshape(xx1, [Nx, 2*Ny, Nz]);
fclose(fid);
[XX, YY] = meshgrid(x, y);
% plot (squeeze(xx1(2,:,1)));
h = pcolor(XX, YY, squeeze(xx1)');
set(h, 'EdgeColor', 'none');
colorbar
Pythonコード:
import struct
import numpy
import matplotlib
unpackformat_int = '<i'
unpackformat_flo = '<f'
fid = open('FLOW_phys_GRID_1.xyz', 'r+')
Nx = struct.unpack(unpackformat_int,fid.read(4))[0]
Ny = struct.unpack(unpackformat_int,fid.read(4))[0]
Nz = struct.unpack(unpackformat_int,fid.read(4))[0]
aa = Nx*Ny*Nz
xx = struct.unpack('i'*aa, fid.read(aa*4))[0]
yy = struct.unpack('i'*aa, fid.read(aa*4))[0]
xx = xx.reshape([Nx, Ny])
yy = yy.reshape([Nx, Ny])
fid.close()
fid = open('FLOW_phys.raw', 'r+')
Nx = struct.unpack(unpackformat_int,fid.read(4))[0]
Ny = struct.unpack(unpackformat_int,fid.read(4))[0]
Nz = struct.unpack(unpackformat_int,fid.read(4))[0]
Ma = struct.unpack(unpackformat_flo, fid.read(4))[0]
some = struct.unpack(unpackformat_flo, fid.read(4))[0]
Re = struct.unpack(unpackformat_flo, fid.read(4))[0]
time = struct.unpack(unpackformat_flo, fid.read(4))[0]
bb = Nx*Ny*Nz
xx1 = struct.unpack('f'*bb, fid.read(bb*4))[0]
xx2 = struct.unpack('f'*bb, fid.read(bb*4))[0]
xx1 = xx1.reshape([Nx, Ny, Nz])
xx2 = xx2.reshape ([Nx, Ny, Nz])
fid.close()
[XX, YY] = numpy.meshgrid(xx, yy)
matplotlib.plot(XX,YY,xx2)
ようこそスタックオーバーフロー。現時点では、あなたが読んでいるファイルの構造の例を提供していないので、この質問に答えることは可能ではないと思います。何とか抜粋を出すことができますか? Pythonコードのセミコロンは、あなたがそれらのポイントで苦労していると思うようになります(それらは必要ないので、Matlabから来ていると思います)。また、もっと冗長である必要があります。 – roganjosh
@roganjosh ! MATLABはエンジニアリング用の便利なツールです。しかし、私は特定のプロジェクトのPythonスクリプトを実行する必要があります。 – Max
ようこそスタックオーバーフロー。 [mcve]をチェックしてください。実際には避けられない限り、100 MBのファイルをダウンロードするのは理想的ではありません。 – roadrunner66