@@ -41,9 +41,8 @@ def check_file_inputs(files):
41
41
list: sorted list of files that so indeed exist
42
42
43
43
"""
44
- print ( type ( files ))
44
+
45
45
if isinstance (files , str ): # Probably need to glob
46
- print (type (files ))
47
46
if "*" in files : # Definitely need to glob
48
47
files2read = np .sort (glob (files ))
49
48
elif os .path .isfile (files ):
@@ -76,8 +75,8 @@ def check_file_inputs(files):
76
75
77
76
78
77
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 )
81
80
82
81
Inputs
83
82
------
@@ -118,10 +117,63 @@ def cube2np(files2read):
118
117
# each line is n_y long. Convert it to a python list & retain it
119
118
l = f .readline ().strip ().replace (' ' ,',' ).split (',' )
120
119
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
122
174
123
175
# remove 0th dimension if we only are reading one file
124
176
if len (files2read ) == 1 :
125
177
out = out [0 ]
126
178
127
- return np .array (out )
179
+ return np .array (out )
0 commit comments