aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAaron LI <aaronly.me@outlook.com>2016-10-18 13:30:23 +0800
committerAaron LI <aaronly.me@outlook.com>2016-10-18 13:30:23 +0800
commit44af51600415d60c527500e138c9f7ef016e2010 (patch)
tree7666721be2a175be7ba73cf7e24317a540b41658
parentafd29a0f91a9092aff302e0d2e85cd34947e7651 (diff)
downloadfg21sim-44af51600415d60c527500e138c9f7ef016e2010.tar.bz2
galactic/synchrotron.py: Add preocess(), postprocess, _make_filepath()
* Add the "preprocess()" and "postprocess()" for a better interface; * Add "_make_filepath()" to compose the output filename; * Reorder some methods for consistency.
-rw-r--r--fg21sim/galactic/synchrotron.py116
1 files changed, 75 insertions, 41 deletions
diff --git a/fg21sim/galactic/synchrotron.py b/fg21sim/galactic/synchrotron.py
index a5e8b57..77c0be4 100644
--- a/fg21sim/galactic/synchrotron.py
+++ b/fg21sim/galactic/synchrotron.py
@@ -39,37 +39,32 @@ class Synchrotron:
----------
???
"""
+ # Component name
+ name = "Galactic free-free"
+
def __init__(self, configs):
self.configs = configs
self._set_configs()
- self._load_template()
- self._load_indexmap()
def _set_configs(self):
"""Load the configs and set the corresponding class attributes."""
- self.template_path = self.configs.get_path(
- "galactic/synchrotron/template")
- self.template_freq = self.configs.getn(
- "galactic/synchrotron/template_freq")
+ comp = "galactic/freefree"
+ self.template_path = self.configs.get_path(comp+"/template")
+ self.template_freq = self.configs.getn(comp+"/template_freq")
self.template_unit = au.Unit(
- self.configs.getn("galactic/synchrotron/template_unit"))
- self.indexmap_path = self.configs.get_path(
- "galactic/synchrotron/indexmap")
- self.smallscales = self.configs.getn(
- "galactic/synchrotron/add_smallscales")
+ self.configs.getn(comp+"/template_unit"))
+ self.indexmap_path = self.configs.get_path(comp+"/indexmap")
+ self.smallscales = self.configs.getn(comp+"/add_smallscales")
+ self.prefix = self.configs.getn(comp+"/prefix")
+ self.save = self.configs.getn(comp+"/save")
+ self.output_dir = self.configs.get_path(comp+"/output_dir")
# output
- self.prefix = self.configs.getn("galactic/synchrotron/prefix")
- self.save = self.configs.getn("galactic/synchrotron/save")
- self.output_dir = self.configs.get_path(
- "galactic/synchrotron/output_dir")
self.filename_pattern = self.configs.getn("output/filename_pattern")
self.use_float = self.configs.getn("output/use_float")
self.clobber = self.configs.getn("output/clobber")
- # common
self.nside = self.configs.getn("common/nside")
self.lmin = self.configs.getn("common/lmin")
self.lmax = self.configs.getn("common/lmax")
- # unit of the frequency
self.freq_unit = au.Unit(self.configs.getn("frequency/unit"))
#
logger.info("Loaded and setup configurations")
@@ -141,27 +136,22 @@ class Synchrotron:
self.template += self.hpmap_smallscales
logger.info("Added small-scale fluctuations")
- def _transform_frequency(self, frequency):
- """Transform the template map to the requested frequency,
- according to the spectral model and using an spectral index map.
+ def _make_filepath(self, **kwargs):
+ """Make the path of output file according to the filename pattern
+ and output directory loaded from configurations.
"""
- hpmap_f = (self.template *
- (frequency / self.template_freq) ** self.indexmap)
- return hpmap_f
-
- def simulate(self, frequencies):
- """Simulate the synchrotron map at the specified frequencies."""
- self._add_smallscales()
- #
- hpmaps = []
- for f in np.array(frequencies, ndmin=1):
- logger.info("Simulating synchrotron map at {0} ({1}) ...".format(
- f, self.freq_unit))
- hpmap_f = self._transform_frequency(f)
- hpmaps.append(hpmap_f)
- if self.save:
- self.output(hpmap_f, f)
- return hpmaps
+ data = {
+ "prefix": self.prefix,
+ }
+ data.update(kwargs)
+ filename = self.filename_pattern.format(**data)
+ filetype = self.configs.getn("output/filetype")
+ if filetype == "fits":
+ filename += ".fits"
+ else:
+ raise NotImplementedError("unsupported filetype: %s" % filetype)
+ filepath = os.path.join(self.output_dir, filename)
+ return filepath
def _make_header(self):
"""Make the header with detail information (e.g., parameters and
@@ -190,10 +180,7 @@ class Synchrotron:
os.mkdir(self.output_dir)
logger.info("Created output dir: {0}".format(self.output_dir))
#
- filename = self.filename_pattern.format(prefix=self.prefix,
- frequency=frequency)
- filename += ".fits"
- filepath = os.path.join(self.output_dir, filename)
+ filepath = self._make_filepath(frequency=frequency)
if not hasattr(self, "header"):
self._make_header()
header = self.header.copy()
@@ -207,3 +194,50 @@ class Synchrotron:
write_fits_healpix(filepath, hpmap, header=header,
clobber=self.clobber)
logger.info("Write simulated map to file: {0}".format(filepath))
+
+ def preprocess(self):
+ """Perform the preparation procedures for the final simulations.
+
+ Attributes
+ ----------
+ _preprocessed : bool
+ This attribute presents and is ``True`` after the preparation
+ procedures are performed, which indicates that it is ready to
+ do the final simulations.
+ """
+ if hasattr(self, "_preprocessed") and self._preprocessed:
+ return
+ #
+ logger.info("{name}: preprocessing ...".format(name=self.name))
+ self._load_template()
+ self._load_indexmap()
+ self._add_smallscales()
+ #
+ self._preprocessed = True
+
+ def simulate_frequency(self, frequency):
+ """Transform the template map to the requested frequency,
+ according to the spectral model and using an spectral index map.
+ """
+ self.preprocess()
+ #
+ logger.info("Simulating {name} map at {freq} ({unit}) ...".format(
+ name=self.name, freq=frequency, unit=self.freq_unit))
+ hpmap_f = (self.template *
+ (frequency / self.template_freq) ** self.indexmap)
+ #
+ if self.save:
+ self.output(hpmap_f, frequency)
+ return hpmap_f
+
+ def simulate(self, frequencies):
+ """Simulate the synchrotron map at the specified frequencies."""
+ hpmaps = []
+ for f in np.array(frequencies, ndmin=1):
+ hpmap_f = self.simulate_frequency(f)
+ hpmaps.append(hpmap_f)
+ return hpmaps
+
+ def postprocess(self):
+ """Perform the post-simulation operations before the end."""
+ pass