# Python 3 # Author: Jonathan Goodman, goodman@cims.nyu.edu # Fall 2024 # for the class Scientific Computing # https://math.nyu.edu/~goodman/teaching/ScientificComputing2024/ScientificComputing.html # Illustrate writing and reading numerical data to and from files # This file: DataAnalysis.py, # Reads time series data described in DataInfo.py # The datafile and model parameter file names are in DataInfo.py import numpy as np import array import DataInfo as df # Information about the data file m = df.m # time series length n = df.n # number of time series DataFileName = df.DataFileName # binary data file names ModelFileName = df.ModelFileName with open(DataFileName, 'rb') as DF: # 'rb' is for "read binary" allData = array.array('d') # the whole file allData.fromstring(DF.read()) # read the whole file T_vals = np.array( allData[0: m ] ) # values 0, 1 ,...,m-1 f_vals = np.array( allData[m:(2*m)]) # values m,m+1,...,2m for j in range(m): line = "j is {j:4d}, t_j is {t_j:10.5f}, f(t_j) is {f:10.5f}" line = line.format(j=j, t_j = T_vals[j], f = f_vals[j]) print(line) with open(ModelFileName, 'rb') as MF: # 'rb' is for "read binary" allData = array.array('d') # the whole file allData.fromstring(MF.read()) # read the whole file lam = np.array( allData[0: n ] ) # values 0, 1 ,...,m-1 a = np.array( allData[n:(2*n)]) # values m,m+1,...,2m for k in range(n): line = "k is {k:4d}, lambda is {lam:10.5f}, a is {a:10.5f}" line = line.format(k=k, lam = lam[k], a = a[k]) print(line)