A story stuck with handling Python binary data

Hello. In this article, I'll share some of the stumbling blocks when working with binary data in Python.

Template statement when dealing with binary data

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

Favorite part

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

A story stuck with handling Python binary data
[Python3] A story stuck with time zone conversion
A story about a python beginner stuck with No module named'http.server'
Add a Python data source with Redash
Try working with binary data in Python
Data analysis with python 2
Handling yaml with python
Folium: Visualize data on a map with Python
Binary search with python
Binary search with Python3
Data analysis with Python
Extract data from a web page with Python
A story about making 3D space recognition with Python
Get financial data with python (then a little tinkering)
A story about making Hanon-like sheet music with Python
A story about trying a (Golang +) Python monorepo with Bazel
A server that echoes data POSTed with flask / python
Sample data created with python
Get Youtube data with python
Stumble story with Python array
Make a fortune with Python
Create a directory with python
A little stuck with chainer
Read json data with python
A memo that reads data from dashDB with Python & Spark
Notes on handling large amounts of data with python + pandas
A story about an amateur making a breakout with python (kivy) ②
The story of rubyist struggling with python :: Dict data with pycall
A story about an amateur making a breakout with python (kivy) ①
[Python] What is a with statement?
Write a binary search in Python
Solve ABC163 A ~ C with Python
Operate a receipt printer with python
A python graphing manual with Matplotlib.
Python Application: Data Handling Part 3: Data Format
Solve ABC166 A ~ D with Python
Create a virtual environment with Python!
[Python] Get economic data with DataReader
I made a fortune with Python.
Create a binary file in Python
Building a virtual environment with Python 3
Solve ABC168 A ~ C with Python
Python data structures learned with chemoinformatics
Make a recommender system with python
[Small story] Get timestamp with Python
[Python] Generate a password with Slackbot
Solve ABC162 A ~ C with Python
Solve ABC167 A ~ C with Python
Solve ABC158 A ~ C with Python
Easy data visualization with Python seaborn.
Let's make a graph with python! !!
Process Pubmed .xml data with python
Data analysis starting with python (data visualization 1)
[Python] Inherit a class with class variables
I made a daemon with Python
Handling regular expressions with PHP / Python
Data analysis starting with python (data visualization 2)
Write a batch script with Python3.5 ~
Python application: Data cleansing # 2: Data cleansing with DataFrame
[Python, Selenium, PhantomJS] A story when scraping a website with lazy load
A story stuck with the installation of the machine learning library JAX