# 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: modelFit.py, # Fit a linear combination of oscillators to data from a data file import numpy as np import matplotlib.pyplot as plt import array # to read and write binary files import sys # to read command line arguments import importlib # for importing of the info file runName = sys.argv[1] # this is the argument given on the command line infoImportFile = "info_"+runName # name to be used for importing # Import the info file for the run with this name # This has the effect of: import infoImportFile as info, except that # you don't know the name of the file to import in advance. info = importlib.import_module(infoImportFile) n = info.n # Get the data for this example DataFileName = "data_"+runName # binary data filename with open(DataFileName, 'rb') as DF: # 'rb' is for "read binary" cpD = array.array('d') # for "core python Data" cpD.fromstring(DF.read()) # Fix this to run on your system times = cpD[0:n] # The first numbers are the times xvals = cpD[n:(2*n)] fig,ax = plt.subplots() ax.plot(times, xvals, "o") ax.grid() titleString = "raw data from " + runName ax.set_title(titleString) ax.set_ylabel("x") ax.set_xlabel("t") plotFileName = runName + "RawData.pdf" plt.savefig(plotFileName)