For the code I am writing, I need to convert a .dat file with a list of numbers and a header into a numpy array. I must then reshape each data array into an 89x125 rectangle. I am having trouble converting the file to each variable that corresponds with the set of values. Any help would be helpful I find using numpy very confusing.
First lines of .dat file:
title="velocity" variables = "X/C", "Y/C",
"U","V","Omega","Lamta",zone i=125, j=89, DATAPACKING=POINT
-0.925333, 2.625333, -0.007452, 0.249024,-2.348774e-01,3.685912e-07
-0.925333, 2.604000, -0.008238, 0.244324,-2.009359e-02,1.858388e-06
-0.925333, 2.582667, -0.003536, 0.198775,1.353259e+00,3.546745e-05
-0.925333, 2.561333, -0.001177, 0.209091,6.279517e-01,2.377224e-05
-0.925333, 2.540000, -0.013361, 0.157628,2.788983e+00,2.688667e-05
-0.925333, 2.518667, -0.016351, 0.159980,3.093468e+00,8.040523e-06
-0.925333, 2.497333, -0.004509, 0.220179,2.561251e+00,9.985430e-06
-0.925333, 2.476000, 0.000511, 0.213207,1.077752e+00,3.425355e-06
-0.925333, 2.454667, -0.007348, 0.196955,2.366343e+00,4.243688e-05
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
Uc = 0.16 #m/s
diaPC = 38 #mm
diaOT = 76 #mm
sDis = 4*38 #mm
PIV = 500 #FPS
fname = 'Velocity0241.txt'
dtype1 = np.dtype([('X/C', 'f4'), ('Y/C', 'f4'), ('U', 'f4'), ('V', 'f4')])
data = np.loadtxt(fname, dtype=dtype1, skiprows=2, usecols=(0,1,2,3))
I always get this error:
ValueError Traceback (most recent call last)
<ipython-input-59-f4aaca3e57b3> in <module>
11 fname = 'Velocity0241.txt'
12 dtype1 = np.dtype([('X/C', 'f4'), ('Y/C', 'f4'), ('U', 'f4'), ('V', 'f4')])
---> 13 data = np.loadtxt(fname, dtype=dtype1, skiprows=2, usecols=(0,1,2,3))
~\anaconda3\lib\site-packages\numpy\lib\npyio.py in loadtxt(fname, dtype, comments, delimiter, converters, skiprows, usecols, unpack, ndmin, encoding, max_rows)
1157 # converting the data
1158 X = None
-> 1159 for x in read_data(_loadtxt_chunksize):
1160 if X is None:
1161 X = np.array(x, dtype)
~\anaconda3\lib\site-packages\numpy\lib\npyio.py in read_data(chunk_size)
1085
1086 # Convert each value according to its column and store
-> 1087 items = [conv(val) for (conv, val) in zip(converters, vals)]
1088
1089 # Then pack it according to the dtype's nesting
~\anaconda3\lib\site-packages\numpy\lib\npyio.py in <listcomp>(.0)
1085
1086 # Convert each value according to its column and store
-> 1087 items = [conv(val) for (conv, val) in zip(converters, vals)]
1088
1089 # Then pack it according to the dtype's nesting
~\anaconda3\lib\site-packages\numpy\lib\npyio.py in floatconv(x)
792 if '0x' in x:
793 return float.fromhex(x)
--> 794 return float(x)
795
796 typ = dtype.type
ValueError: could not convert string to float: '-0.925333,'