Skip to content

Commit

Permalink
Fix for reading in old mesaplot v1 pickle files.
Browse files Browse the repository at this point in the history
Add notes that people should not depend on this working between versions
and for long term storage, history and profile files should be kept.
  • Loading branch information
rjfarmer committed Oct 9, 2023
1 parent 45180cc commit 7b82093
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ m=mp.MESA()

Now m contains all the useful stuff


### History files
````python
m.loadHistory()
Expand Down
34 changes: 22 additions & 12 deletions mesaPlot/file_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,8 @@ def __getitem__(self, key):
if key in self.data.dtype.names:
return self.data[key]
if key in self.head.dtype.names:
return self.head[key][0]
return np.atleast_1d(self.head[key])[0]


try:
return self.__dict__[key]
Expand Down Expand Up @@ -254,26 +255,35 @@ def _loadPickle(self, pickname, filename):
# Get checksum
filehash = _hash(filename)
try:
pickhash = pickle.load(f)
except:
version = pickle.load(f)
except Exception:
raise ValueError("Pickle file corrupted please delete it and try again")
if len(str(pickhash)) == len(str(_PICKLE_VERSION)):
if int(pickhash) != int(_PICKLE_VERSION):
# Not a hash but a version number/ or wrong version number:
return False

try:
# For mesaplot 1.* this is a hash, while 2.* is a version number
if len(str(version)) > 12:
# Version 1.0 hash
pickhash = version
else:
if int(version) != int(_PICKLE_VERSION):
raise ValueError("Pickle file not recongised please delete it and try again")

pickhash = pickle.load(f)
except TypeError:
return False

if (
os.path.exists(filename) and pickhash == filehash
) or not os.path.exists(filename):
# Data has not changed
# Get Data
self.data = pickle.load(f)
self.head = pickle.load(f)
data = pickle.load(f)

try:
head = pickle.load(f)
self.head = head
self.data = data
except EOFError: # Handle 1.* verisons of pickle files
self.data = data.data
self.head = data.head

self._loaded = True
return True
return False
Expand Down

0 comments on commit 7b82093

Please sign in to comment.