Skip to content

Commit e0db896

Browse files
committed
FEAT: Add func to read armadillo mats
1 parent 58e2f21 commit e0db896

File tree

1 file changed

+58
-6
lines changed

1 file changed

+58
-6
lines changed

srcPython/read_armadillo.py

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,8 @@ def check_file_inputs(files):
4141
list: sorted list of files that so indeed exist
4242
4343
"""
44-
print(type(files))
44+
4545
if isinstance(files, str): # Probably need to glob
46-
print(type(files))
4746
if "*" in files: # Definitely need to glob
4847
files2read = np.sort(glob(files))
4948
elif os.path.isfile(files):
@@ -76,8 +75,8 @@ def check_file_inputs(files):
7675

7776

7877
def cube2np(files2read):
79-
""" Read armadillo cubes from .txt files, automatically globs and/or input.
80-
return np array of shape (nFiles, n_x, n_y, n_x)
78+
""" Read armadillo cubes from .txt files, automatically globs input.
79+
return np array of shape (nFiles, n_x, n_y, n_z)
8180
8281
Inputs
8382
------
@@ -118,10 +117,63 @@ def cube2np(files2read):
118117
# each line is n_y long. Convert it to a python list & retain it
119118
l = f.readline().strip().replace(' ',',').split(',')
120119
ls[i,:, j] = l # n_y
121-
out.append(ls) # speed not a huge issue, work with lists
120+
out.append(ls.T) # speed not a huge issue, work with lists
121+
122+
# remove 0th dimension if we only are reading one file
123+
if len(files2read) == 1:
124+
out = out[0]
125+
126+
return np.array(out)
127+
128+
129+
130+
131+
def mat2np(files2read):
132+
""" Read armadillo matrices from .txt files, automatically globs input.
133+
return np array of shape (nFiles, n_x, n_y)
134+
135+
Inputs
136+
------
137+
files (str or list-like): either path to files or list of files. If it's a str,
138+
the pattern is globbed & sorted, or the directory's .txt files are sorted.
139+
If it's list-like, the list is sorted.
140+
141+
Outputs
142+
-------
143+
np.array of shape (nFiles, n_x, n_y) & dtype float. If we are only reading
144+
one file, return shape is just (n_x, n_y)
145+
146+
Usage
147+
-----
148+
149+
lons = mat2np("../run/geolon_*.txt")
150+
lons = mat2np(np.sort(glob.glob("../run/geolon_*.txt")))
151+
152+
"""
153+
154+
# Sanitize input
155+
files2read = check_file_inputs(files2read)
156+
157+
out = [] # output holder
158+
for thisf in files2read:
159+
with open(thisf, 'r') as f:
160+
_ = f.readline() # first line is a header, not needed
161+
shape = f.readline().strip() # next line holds the shape of the cube
162+
shape = shape.split(' ')
163+
if len(shape) != 2:
164+
raise ValueError(
165+
f"File ({thisf}) does not appear to be an armadillo matrix.\n"
166+
f"Found shape: {shape}")
167+
shape = np.array(shape, dtype=int) # convert shape to np array of int's
168+
ls = np.zeros(shape) # holder for this file's outputs, dtype is float
169+
for i in range(int(shape[0])): # n_x
170+
# each line is n_y long. Convert it to a python list & retain it
171+
l = f.readline().strip().replace(' ',',').split(',')
172+
ls[i,:] = l # n_y
173+
out.append(ls.T) # speed not a huge issue, work with lists
122174

123175
# remove 0th dimension if we only are reading one file
124176
if len(files2read) == 1:
125177
out = out[0]
126178

127-
return np.array(out)
179+
return np.array(out)

0 commit comments

Comments
 (0)