Commit c49cb3bd authored by Wouter Kessels's avatar Wouter Kessels

Two nicely separate functions for storing temp data and final data

parent 498165e1
This diff is collapsed.
......@@ -64,7 +64,7 @@ def pipeline(bodies, settings):
# Save output data to temporary file
print("Saving...\n")
temp_name_file, temp_name_folder = save_results(bodies, info, settings, overwrite=False, name=None, folder=temp_folder)
temp_file = save_temp(bodies, info, settings, folder=temp_folder)
print("Temporary data saved\n\n")
# Execution time
......@@ -74,7 +74,7 @@ def pipeline(bodies, settings):
else:
print("Time elapsed: {} minutes, for {} bodies (no asteroids)".format(round((end-start)/60, 1), len(bodies)))
return bodies, info, temp_name_file, temp_name_folder
return bodies, info, temp_file, temp_folder
......@@ -116,20 +116,9 @@ def save_init(bodies, info, settings, folder):
def save_progress(positions, velocities, time, bodies, settings):
data = {
'positions' : positions,
'velocities' : velocities,
'time' : time,
'bodies' : bodies,
'settings' : settings
}
def save_results(bodies, info, settings, overwrite, name=None, folder=None, temp_name=None, description="temporary"):
"""Save the acquired data to a file, temporary or lasting.
def save_temp(bodies, info, settings, folder):
"""
Save the acquired data to a temporary file.
Parameters
----------
......@@ -139,33 +128,12 @@ def save_results(bodies, info, settings, overwrite, name=None, folder=None, temp
Attribute information from ensemble of bodies
settings: objects.Settings class object
Settings for the interacting system
overwrite: bool
False: save data to temporary file.
True: delete temporary file defined by the 'name' field and save data to lasting file
name: str
File path and name to save the data to in case of overwrite
temp_name: str
File path and name of the temporary file to overwrite in case of overwrite
description: str
Description of the saved data
folder: string
Path to the folder of the specific run
"""
# TODO: build in check whether name already exists in directory
if not overwrite:
name = folder + "/temporary.pkl"
description = 'temporary'
elif overwrite:
# Check whether new file path is unique and temp_file path is specified
if not os.path.isfile(name):
if temp_name == None:
raise IOError("Specify what temporary file to replace")
else:
os.remove(name)
else:
raise ValueError("File path {} already exists. Define a unique file name".format(name))
name = folder + "/temporary.pkl"
description = 'temporary'
data = {
'description': description,
'bodies': bodies,
......@@ -176,7 +144,49 @@ def save_results(bodies, info, settings, overwrite, name=None, folder=None, temp
out_file = open(name, "wb")
pickle.dump(data, out_file)
out_file.close()
return name, folder
return name
def save_results(source, name, description, rm_temp_name):
"""Save the acquired data from a temporary file to a lasting file.
Parameters
----------
source : string
Either path to temporary pickle file or to temporary directory from which to extract the data
name : string
Name of the new file
description : string
Description of the stored data
rm_temp_name : string
Path to the temporary file to replace
"""
# Check whether source is given as file or as directory
if source[-13:] == 'temporary.pkl':
folder = source[:-13]
elif source[-18:] == 'initialization.pkl':
folder = source[:-18]
else:
folder = source
source = folder + 'temporary.pkl'
# TODO: implement a way to always include the initial positions of all bodies (including the asteroids)
# Check whether new file path is unique
if os.path.isfile(name):
raise ValueError("File path {} already exists. Define a unique file name".format(name))
in_file = open(source, 'rb')
data = pickle.load(in_file)
data['description'] = description
in_file.close()
out_file = open(name, "wb")
pickle.dump(data, out_file)
out_file.close()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment