Oct-19-2022, 11:34 AM
I have the following code:
All the data is added to a Numpy array of 1 dimension.
I originally envisaged a structure such as:
How might I alter this and is there a more efficient way of collecting/storing the data?
import matplotlib.pyplot as plt
import matplotlib.cbook as cbook
import numpy as np
import math
SIGMA = 5.67051e-5 # Stefan-Boltzmann Constant
PI = math.pi # Pi
M_SUN = 1.989e33 # Solar mass (g)
L_SUN = 3.826e33 # Solar luminosity (ergs/s)
R_SUN = 6.9599e10 # Solar radius (cm)
YEAR = 3.15570e7 # AU year (s)
sa_sun = 4 * PI * math.pow(R_SUN, 2) # Solar surface area
t_sun = L_SUN / (sa_sun * SIGMA) # Solar surface temperature
# Converts Fortran output (xd+/-y) into float
def convert(oldValue):
if oldValue.find('D') != -1:
newValue = float(oldValue.replace('D', 'e'))
else:
if oldValue.find('+') != -1:
mantissa = float(oldValue[0: oldValue.find('+')])
exponent = int(oldValue[oldValue.find('+'):])
else:
mantissa = float(oldValue[0: oldValue.find('-')])
exponent = int(oldValue[oldValue.find('-'):])
newValue = mantissa * (math.pow(10, exponent))
return newValue
# Reads in data from STELCOR file
def readData(datafile):
data = np.array([])
file = open(datafile)
for line in file:
if (line[1:6] == "hydro") or (line[1:6] == "HYDRO"):
time = convert(line[21:35])
delta_t = convert(line[36:50])
mass = convert(line[51:65])
radius = convert(line[66:80])
lum_core = convert(line[81:95])
lum_tot = convert(line[96:110])
flux = convert(line[111:125])
ratio = float(line[125:137])
data = np.append(data, [time, delta_t, mass, radius, lum_core, lum_tot, flux, ratio], axis = 0)
file.close()
return data
if __name__ == "__main__":
stellarData = np.array([])
stellarData = readData("dummymain.lst")
#CM_Graph(?,?)I am adding the data from a textfile that contains thousands of lines, similar to this:Output:hydro output: 1 1.05200D+09 1.05200D+09 9.94376D+31 3.66754D+10 7.52265D+31 7.52265D+31 4.99722-235 0.0499938
hydro output: 2 2.10400D+09 1.05200D+09 9.94376D+31 3.66754D+10 7.52265D+31 7.52265D+31 2.88583-105 0.0499938
hydro output: 3 3.15600D+09 1.05200D+09 9.94376D+31 3.66754D+10 7.52265D+31 7.52265D+31 3.81557D-62 0.0499938
hydro output: 4 4.20800D+09 1.05200D+09 9.94376D+31 3.66754D+10 7.52265D+31 7.52265D+31 1.19575D-40 0.0499938
hydro output: 5 5.26000D+09 1.05200D+09 9.94376D+31 3.66754D+10 7.52265D+31 7.52265D+31 8.64733D-28 0.0499938
hydro output: 6 6.31200D+09 1.05200D+09 9.94376D+31 3.66754D+10 7.52265D+31 7.52265D+31 3.04933D-19 0.0499938
hydro output: 7 7.36400D+09 1.05200D+09 9.94376D+31 3.66754D+10 7.52265D+31 7.52265D+31 3.72610D-13 0.0499938
hydro output: 8 8.41600D+09 1.05200D+09 9.94376D+31 3.66754D+10 7.52265D+31 7.52265D+31 1.32716D-08 0.0499938
hydro output: 9 9.46800D+09 1.05200D+09 9.94376D+31 3.66754D+10 7.52265D+31 7.52265D+31 4.49291D-05 0.0499938
hydro output: 10 1.05200D+10 1.05200D+09 9.94376D+31 3.66754D+10 7.52265D+31 7.52265D+31 2.93595D-02 0.0499938The system strips out the initial text, ignores the incremental step (integer) value and populates an array with the converted float data.All the data is added to a Numpy array of 1 dimension.
I originally envisaged a structure such as:
Output:[[1052000000.0, 1052000000.0, 9.94376e+31, 36675400000.0, 7.52265e+31, 7.52265e+31, 4.9972200000000005e-235, 0.0499938], [...], ...]However, I get these all as a single element in each array position@Output:[1052000000.0, 1052000000.0, 9.94376e+31, 36675400000.0, 7.52265e+31, 7.52265e+31, 4.9972200000000005e-235, 0.0499938]I later want to reference all rows of data, but only the data in a specific position in that row (all data in column 3, for example).How might I alter this and is there a more efficient way of collecting/storing the data?
