Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Added capability of reading in linked list binary data (not the age bin data yet, though)
  • Loading branch information
daminton committed Jul 18, 2022
1 parent 722c9a1 commit 62318b1
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions python/ctem/ctem/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import matplotlib.pyplot as plt
import re
from tempfile import mkstemp
from scipy.io import FortranFile

# Set pixel scaling common for image writing, at 1 pixel/ array element
dpi = 72.0
Expand Down Expand Up @@ -263,16 +264,37 @@ def read_user_input(user):
return user


def read_unformatted_binary(filename, gridsize):
def read_unformatted_binary(filename, gridsize, kind='DP'):
# Read unformatted binary files created by Fortran
# For use with surface ejecta and surface dem data files
dt = np.float
if kind == 'DP':
dt = np.dtype('f8')
elif kind == 'SP':
dt = np.dtype('f4')
elif kind == 'I4B':
dt = np.dtype('<i4')
elif kind == 'I8B':
dt = np.dtypye('<i8')
data = np.fromfile(filename, dtype=dt)
data.shape = (gridsize, gridsize)

return data


def read_linked_list_binary(filename, stackname, gridsize):
stack = read_unformatted_binary(stackname,gridsize,kind='I4B')
data = np.empty((gridsize,gridsize),dtype="object")
with FortranFile(filename, 'r') as f:
for i in np.arange(gridsize):
for j in np.arange(gridsize):
datastack = []
for s in np.arange(stack[j, i]):
d = f.read_reals(np.float64)
datastack.append(d)
data[j, i] = d
return data


def real2float(realstr):
"""
Converts a Fortran-generated ASCII string of a real value into a np float type. Handles cases where double precision
Expand Down

0 comments on commit 62318b1

Please sign in to comment.