|
# -*- coding: utf-8 -*- |
|
|
|
# modules to import |
|
import numpy |
|
|
|
|
|
# local functions |
|
|
|
def read_int32(file_stream): |
|
return numpy.fromfile(file= file_stream, dtype= numpy.int32, count=1)[0] |
|
|
|
|
|
def read_real64(file_stream): |
|
return numpy.fromfile(file= file_stream, dtype= numpy.float64, count=1)[0] |
|
|
|
|
|
class TestData: |
|
|
|
class Shape: |
|
|
|
def __init__(self, file_stream, order, major): |
|
self.row = read_int32(file_stream= file_stream) |
|
self.col = read_int32(file_stream= file_stream) |
|
self.order = order |
|
self.major = major |
|
pass |
|
|
|
|
|
def get_shape(self): |
|
if self.major == 'col': |
|
return self.get_shape_major_col() |
|
elif self.major == 'row': |
|
return self.get_shape_major_row() |
|
else: |
|
pass |
|
|
|
|
|
def get_shape_major_col(self): |
|
return (self.col, self.row) |
|
|
|
|
|
def get_shape_major_row(self): |
|
return (self.row, self.col) |
|
|
|
|
|
def __init__(self, file_path, order, major): |
|
|
|
with open(file_path, 'rb') as self.file_stream: |
|
|
|
# STEP.01 |
|
# get the test data shape |
|
self.shape = TestData.Shape(file_stream= self.file_stream, order= order, major= major) |
|
|
|
# STEP.02 |
|
# prepare the numpy.array object to store the test data |
|
self.array = numpy.zeros( shape= self.shape.get_shape() , dtype= numpy.float64, order= order ) |
|
|
|
# STEP.03 |
|
# read out the test data |
|
if major == 'col': |
|
|
|
for itr_row in range(self.shape.row): |
|
for itr_col in range(self.shape.col): |
|
self.array[itr_col, itr_row] = read_real64(file_stream= self.file_stream) |
|
|
|
elif major == 'row': |
|
|
|
for itr_col in range(self.shape.col): |
|
for itr_row in range(self.shape.row): |
|
self.array[itr_row, itr_col] = read_real64(file_stream= self.file_stream) |
|
|
|
else: |
|
pass |
|
|
|
# STEP.END |
|
pass |
|
|
|
|
|
def show(self): |
|
print() |
|
print( 'order: ', self.shape.order ) |
|
print( 'major: ', self.shape.major ) |
|
print( test_data ) |
|
print( type(test_data) ) |
|
print( test_data.array ) |
|
pass |
|
|
|
|
|
# main process is as follows |
|
|
|
|
|
if __name__ == "__main__": |
|
|
|
test_data = TestData(file_path= './binary.dat', order= 'C', major= 'col'); test_data.show() |
|
test_data = TestData(file_path= './binary.dat', order= 'C', major= 'row'); test_data.show() |
|
test_data = TestData(file_path= './binary.dat', order= 'F', major= 'col'); test_data.show() |
|
test_data = TestData(file_path= './binary.dat', order= 'F', major= 'row'); test_data.show() |
|
|
|
pass |
|
|
|
|
|
# EOF |