读取hydrus的result中Balance.out的文件

Balance.out的文件是每天的各个变量都是一行,若读取它的话,这就要求读取文件特殊的行。用python程序实现起来非常容易。

# Python的标准库linecache模块非常适合这个任务,读取Balance.out的特殊的行
import linecache
import numpy as np
I_0=0; # 0 表示Top Flux,1表示Bot Flux。
idx1 = np.linspace(29+I_0, 1499+I_0, 99, endpoint=True)
the_line = linecache.getline(r'C:\Users\zxxdd\Documents\Soil_Water\Balance.out', 16+I_0)
filename = r'C:\Users\zxxdd\Documents\Soil_Water\write_data.txt'
with open(filename,'w') as f: # 如果filename不存在会自动创建, 'w'表示写数据,写之前会清空文件中的原有数据!
f.write(the_line)

for i in range(len(idx1)):
the_line = linecache.getline(r'C:\Users\zxxdd\Documents\Soil_Water\Balance.out', int(idx1[i]))
print(the_line)
with open(filename,'a') as f: # 如果filename不存在会自动创建, 'w'表示写数据,写之前会清空文件中的原有数据!
f.write(the_line)

matlab的程序如下:

%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5
clc
% 把Balance.out复制到你的当前的文件夹下,先运行python程序C:\Users\zxxdd\PycharmProjects\AI\Read_BALANCE.py
% python生成write_data.txt,然后再用matlab的本程序读取write_data.txt来生成SimVal
%% 将Balance.out文件的所有数据读出来,让它成为可处理的矩阵SimVal
fileID = fopen('write_data.txt','r');
formatSpec = '%s %s %s %f '; % % 即4列数据
data_A = textscan(fileID,formatSpec); 
SimVal = data_A{1,4};
fclose(fileID);

Leave a Reply

Your email address will not be published. Required fields are marked *