[英]How to convert a unformatted fortran file(modflow output ) to numpy array
I have a modflow output file with extension hds. 我有一个带有hds扩展名的modflow输出文件。 Google drive link for a file . Google驱动器文件链接。 Its a unformatted fortran file. 它是未格式化的fortran文件。 I need to convert it to numpy array, I have tried : 我需要将其转换为numpy数组,我尝试过:
floattype = 'f4'
a = np.fromfile("lake_example.hds", np.dtype([('kstp','i4'),('kper','i4'),('pertim',floattype),('totim',floattype),('text','a16'),('ncol','i4'),('nrow','i4'),('ilay','i4')]))
print a
print a.shape
github link for code: https://github.com/Kirubaharan/hydrology/blob/master/gw_tut.py github代码链接: https : //github.com/Kirubaharan/hydrology/blob/master/gw_tut.py
I am a trying a tutorial from this link . 我正在尝试通过此链接进行教程。 Since I am on linux I cannot use flopy's method to get the output array from the file. 由于我在Linux上,因此无法使用flopy的方法从文件获取输出数组。 So I am trying to use np.fromfile, But I am having problem in getting the output. 因此,我尝试使用np.fromfile,但是在获取输出时遇到问题。
My output is like this now: 我的输出现在是这样的:
[ (44, 1, 1.401298464324817e-45, 1.0, '\x00\x00\x80? ', 1145128264, 11, 11)
(1, 44, 6.782284567332115e-43, 100.0, '\x00\x00\xc8B\x00\x00\xc8B\x00\x00\xc8B\x00\x00\xc8B', 1120403456, 1120403456, 1120403456)
(1120403456, 1120403456, 100.0, 100.0, '\x0c\xbf\xc7B\x18~\xc7B=@\xc7B\xce\x0e\xc7B', 1120336356, 1120341710, 1120354365)
(1120370200, 1120386828, 100.0, 100.0, '\x18~\xc7B\x0e\xf9\xc6B\xf0s\xc6B\xaa\x00\xc6B', 1120258308, 1120272554, 1120302064)
(1120336142, 1120370200, 100.0, 100.0, '=@\xc7B\xf0s\xc6B\xf8\x94\xc5B\x91\xb3\xc4B', 1120149448, 1120187281, 1120244984)
(1120302064, 1120354365, 100.0, 100.0, '\xce\x0e\xc7B\xaa\x00\xc6B\x91\xb3\xc4B\xac\xff\xc2B', 1119940155, 1120075692, 1120187281)
(1120272554, 1120341710, 100.0, 100.0, '\xe4\xf9\xc6B\x04\xc9\xc5B\xc8\x1f\xc4B;\xee\xc0B', 1119092736, 1119940155, 1120149448)
(1120258308, 1120336356, 100.0, 100.0, '\xce\x0e\xc7B\xaa\x00\xc6B\x91\xb3\xc4B\xac\xff\xc2B', 1119940155, 1120075692, 1120187281)
(1120272554, 1120341710, 100.0, 100.0, '=@\xc7B\xf0s\xc6B\xf8\x94\xc5B\x91\xb3\xc4B', 1120149448, 1120187281, 1120244984)
(1120302064, 1120354365, 100.0, 100.0, '\x18~\xc7B\x0e\xf9\xc6B\xf0s\xc6B\xaa\x00\xc6B', 1120258308, 1120272554, 1120302064)
(1120336142, 1120370200, 100.0, 100.0, '\x0c\xbf\xc7B\x18~\xc7B=@\xc7B\xce\x0e\xc7B', 1120336356, 1120341710, 1120354365)
I have included only few lines of output. 我只包含了几行输出。
For header information you can refer their source code: https://github.com/modflowpy/flopy/blob/master/flopy/utils/binaryfile.py#L30g 有关标题信息,您可以参考其源代码: https : //github.com/modflowpy/flopy/blob/master/flopy/utils/binaryfile.py#L30g
Your code doesn't match the structure of your data file: 您的代码与数据文件的结构不匹配:
00000000 2c 00 00 00 01 00 00 00 01 00 00 00 00 00 80 3f |,..............?|
00000010 00 00 80 3f 20 20 20 20 20 20 20 20 20 20 20 20 |...? |
00000020 48 45 41 44 0b 00 00 00 0b 00 00 00 01 00 00 00 |HEAD............|
00000030 2c 00 00 00 e4 01 00 00 00 00 c8 42 00 00 c8 42 |,..........B...B|
00000040 00 00 c8 42 00 00 c8 42 00 00 c8 42 00 00 c8 42 |...B...B...B...B|
00000050 00 00 c8 42 00 00 c8 42 00 00 c8 42 00 00 c8 42 |...B...B...B...B|
Each data block has it's own 56 bytes header consisting of: 3 integers (i4), 2 floating point values (f4), 16 characters, and again 5 integers (i4): 每个数据块都有自己的56个字节的标头,其中包括:3个整数(i4),2个浮点值(f4),16个字符以及5个整数(i4):
44 1 1
1.0 1.0
HEAD
11 11 1 44 484
Then the data block follows (11x11 floating point values): 然后跟随数据块(11x11浮点值):
100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0
100.0 99.87313842773438 99.74627685546875 99.6254653930664 ...
I'm not sure whether this could be imported directly into a numpy array. 我不确定是否可以将其直接导入到numpy数组中。
The following sample code will loop over the whole file and extracts header and data for each block: 以下示例代码将遍历整个文件,并提取每个块的标题和数据:
#!/usr/bin/python
import struct
import numpy as np
infile = open("lake_example.hds","rb")
blockdata = []
while infile.read(1):
infile.seek(-1,1)
data = infile.read(56)
n = struct.unpack('<3i4', data[0:12])
# print n[0], n[1], n[2]
n = struct.unpack('<2f4', data[12:20])
# print n[0], n[1]
# print data[20:36]
n = struct.unpack('<5i4', data[36:56])
# print n[0], n[1], n[2], n[3], n[4]
ncol = n[0]
nrow = n[1]
a = np.fromfile(infile,dtype='f4',count=ncol*nrow).reshape((ncol,nrow))
blockdata.append(a)
data = infile.read(4)
n = struct.unpack('<i4', data)
# print n[0]
for block in blockdata:
print block
Most likely you also will need some of the information from the block headers (see print statements). 您很可能还需要块标题中的一些信息(请参阅打印语句)。
See also the 'flopy.utils.binaryfile Module': http://modflowpy.github.io/flopydoc/binaryfile.html 另请参见“ flopy.utils.binaryfile模块”: http ://modflowpy.github.io/flopydoc/binaryfile.html
Look at Flopy-3 Tutorial 2 (Unconfined Transient Flow Model), in the plotting section: 在绘图部分中查看Flopy-3教程2(无限制瞬态流模型):
From: http://modflowpy.github.io/flopydoc/tutorial2.html 来自: http : //modflowpy.github.io/flopydoc/tutorial2.html
A 'headobj' is first defined: 首先定义一个“ headobj”:
headobj = bf.HeadFile(modelname+'.hds')
and the heads are extracted as follows: 头部提取如下:
head = headobj.get_data(totim=time)
Runs on Debian 在Debian上运行
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.