Hello. In this article, I'll share some of the stumbling blocks when working with binary data in Python.
First, prepare a function that opens binary data. (numpy read)
def open_grd(gpv_file):
with open(gpv_file, 'rb') as ifile:
data = np.fromfile(ifile, dtype='>f', sep = '')
return data
In order to plot the data returned here, the data is formatted into a grid system. As a prerequisite, it is considered that the data is stored according to the following conditions.
data_info = {
'nx' : 150,
'ny' : 100,
'data_path' : './your_data.grd'
}
data = open_grd(data_info['data_path']).reshape(
data_info['ny'], data_info['nx'], order='C' #! or 'F'
)
If you do this, you may or may not be able to do it well, even though the data array is the same, depending on whether it is distributed data or data created by Fortran. As a solution, I confirmed that the conversion order should be specified by the reshape at the end of the last sentence.
ndarray.The argument of the conversion order of reshape is order by default.='C'It has become.
Therefore, in binary data created by yourself with Fortran etc., it is necessary to set ```order ='F'`` `.
I hope I can help people who are in trouble in the same way.
Thank you for watching until the end.
## reference
-How to use reshape to convert the shape of NumPy array ndarray
https://note.nkmk.me/python-numpy-reshape-usage/
Recommended Posts