Skip to content

Commit

Permalink
added read/save methods for TransferFunction
Browse files Browse the repository at this point in the history
  • Loading branch information
apetri committed May 17, 2016
1 parent 598e2cf commit c76b832
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions lenstools/simulations/camb.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import StringIO
import cPickle as pkl

import numpy as np
from scipy import interpolate
Expand Down Expand Up @@ -381,6 +382,42 @@ def __call__(self,z,k):
#Use interpolator to compute the transfer function
return self._interpolated[z](k.to((u.Mpc)**-1).value)

#I/O
def save(self,filename):

"""
Pickle the TransferFunction instance
:param filename: name of the file to save the instance to
:type filename: str.
"""

with open(filename,"w") as fp:
pkl.dump(self,fp)

@classmethod
def read(cls,filename):

"""
Load a previously pickled TransferFunction instance
:param filename: name of the file from which the instance should be read
:type filename: str.
:rtype: :py:class:`TransferFunction`
"""

with open(filename,"r") as fp:
tfr = pkl.load(fp)

if isinstance(tfr,cls):
return tfr
else:
raise TypeError("Pickled instance is not of type {0}".format(cls.__name__))


##############################################################################################################################

class CAMBTransferFunction(TransferFunction):
Expand Down

0 comments on commit c76b832

Please sign in to comment.