instruments.py | instruments.py | |||
---|---|---|---|---|
# -*- coding: utf-8 -*- | # -*- coding: utf-8 -*- | |||
import logging | import logging, os | |||
import configparser | ||||
logging.basicConfig(level=logging.INFO) | logging.basicConfig(level=logging.INFO) | |||
logger = logging.getLogger('GalPaK: Instrument: ') | import numpy as np | |||
from .spread_functions import * | from .spread_functions import PointSpreadFunction, LineSpreadFunction, Gaus sianLineSpreadFunction, GaussianPointSpreadFunction, MoffatPointSpreadFunct ion | |||
from . import convolution | from . import convolution | |||
#Python3 compatibility | ||||
try: | ||||
basestring | ||||
except NameError: | ||||
basestring = str | ||||
logger = logging.getLogger('GalPaK: Instrument ') | ||||
def _read_psf(file_config=None): | ||||
""" | ||||
set PSF from config file | ||||
""" | ||||
psf = None | ||||
if file_config is not None: | ||||
if os.path.isfile(file_config): | ||||
config = configparser.RawConfigParser() | ||||
config.read(file_config) | ||||
else: | ||||
raise ValueError("PSF Config file %s not present! " % (file_con | ||||
fig)) | ||||
else: | ||||
raise ValueError("PSF Config file not defined") | ||||
config = config['PSF'] | ||||
psf_keys={} | ||||
for k in ['type', 'r0', 'C', 'amp', 'fwhm', 'alpha', 'beta', 'ba', 'pa' | ||||
, 'wvl_um', 'mode']: | ||||
if k in config.keys(): | ||||
psf_keys[k] = k | ||||
elif 'psf_'+k in config.keys(): | ||||
psf_keys[k] = 'psf_'+k | ||||
if 'type' in psf_keys: | ||||
psf_type = config[psf_keys['type']].lower() | ||||
logger.info("CONFIG: PSF: type {%s} found " % (psf_type)) | ||||
else: | ||||
logger.error("CONFIG: PSF: type not specified") | ||||
if psf_type == 'moffat': | ||||
psf = MoffatPointSpreadFunction | ||||
elif psf_type == 'gaussian': | ||||
psf = GaussianPointSpreadFunction | ||||
elif psf_type == 'custom': | ||||
psf = ImagePointSpreadFunction | ||||
elif psf_type == 'maoppy': | ||||
psf = MAOPPYPointSpreadFunction | ||||
else: | ||||
#@fixme add other options | ||||
raise NotImplementedError("Currently only PSF type = `moffat` and ` | ||||
gaussian` supported") | ||||
args = {} | ||||
import inspect | ||||
var_args = inspect.getfullargspec(psf).args | ||||
for k in var_args[1:]: | ||||
if k in list(config.keys()): | ||||
try: | ||||
args[k] = eval(config[k].split()[0]) | ||||
except: | ||||
args[k] = config[k] | ||||
return psf(**args) | ||||
def _read_lsf(file_config=None): | ||||
""" | ||||
sets LSF from config LSF: lsf_fwhm | ||||
:return: | ||||
""" | ||||
lsf = None | ||||
if file_config is not None: | ||||
if os.path.isfile(file_config): | ||||
config =configparser.RawConfigParser() | ||||
config.read(file_config) | ||||
else: | ||||
raise ValueError("LSF Config file %s not present! " % (file_con | ||||
fig)) | ||||
else: | ||||
raise ValueError("LSF Config file not defined") | ||||
config = config['LSF'] | ||||
lsf_keys={} | ||||
for k in ['type', 'fwhm']: | ||||
if k in list(config.keys()): | ||||
lsf_keys[k] = k | ||||
elif 'lsf_'+k in config.keys(): | ||||
lsf_keys[k] = 'lsf_'+k | ||||
if 'type' in lsf_keys: | ||||
lsf_type = config[lsf_keys['type']].lower() | ||||
else: | ||||
lsf_type = None | ||||
logger.warning("CONFIG: LSF: type not specified") | ||||
if lsf_type == 'gaussian': | ||||
lsf = GaussianLineSpreadFunction( | ||||
fwhm=float(config[lsf_keys['fwhm']].split()[0]) | ||||
) # lsf_fwhm, | ||||
elif lsf_type is None: | ||||
lsf = None | ||||
else: | ||||
#@fixme | ||||
raise NotImplementedError | ||||
return lsf | ||||
def _read_instrument(file_config): | ||||
if file_config is not None: | ||||
if os.path.isfile(file_config): | ||||
config = configparser.RawConfigParser() | ||||
config.read(file_config) | ||||
else: | ||||
raise ValueError("Instrument Config file %s not present! " % (f | ||||
ile_config)) | ||||
else: | ||||
raise ValueError("Instrument Config not defined") | ||||
psf = _read_psf(file_config) | ||||
try: | ||||
lsf = _read_lsf(file_config) | ||||
except: | ||||
lsf = None | ||||
logger.warning("CONFIG: LSF not found in config file") | ||||
if 'INSTRUMENT' in config.sections(): | ||||
config = config['INSTRUMENT'] | ||||
myinstr = config['type'].lower() | ||||
logger.info("CONFIG: INSTRUMENT: type {%s} found " % (myinstr)) | ||||
if 'pixscale' in list(config.keys()): | ||||
scale = float(config['pixscale'].split()[0]) # | ||||
else: | ||||
scale = None | ||||
if 'muse' == myinstr: | ||||
instrument = MUSE(psf=psf, lsf=lsf) | ||||
elif 'musewfm' == myinstr: | ||||
instrument = MUSEWFM(psf=psf, lsf=lsf) | ||||
elif 'musenfm' == myinstr: | ||||
instrument = MUSENFM(psf=psf, lsf=lsf) | ||||
elif 'alma' in myinstr: | ||||
instrument = ALMA(psf=psf, lsf=lsf, pixscale=scale) | ||||
elif 'sinfok250' in myinstr: | ||||
instrument = SINFOK250(psf=psf, lsf=lsf) | ||||
elif 'sinfok100' in myinstr: | ||||
instrument = SINFOK100(psf=psf, lsf=lsf) | ||||
elif 'sinfoj250' in myinstr: | ||||
instrument = SINFOJ250(psf=psf, lsf=lsf) | ||||
elif 'sinfoj100' in myinstr: | ||||
instrument = SINFOJ100(psf=psf, lsf=lsf) | ||||
elif 'harmoni' in myinstr: | ||||
instrument = HARMONI(psf=psf, lsf=lsf, pixscale=scale) | ||||
elif 'kmos' in myinstr: | ||||
instrument = KMOS(psf=psf, lsf=lsf) | ||||
elif 'osiris' in myinstr: | ||||
instrument = OSIRIS(psf=psf, lsf=lsf) | ||||
elif 'generic' in myinstr: | ||||
instrument = Generic(psf=psf, lsf=lsf, default_spaxel_size=scal | ||||
e) | ||||
else: | ||||
# @fixme:generalize | ||||
raise NotImplementedError | ||||
else: | ||||
#default | ||||
logger.info("CONFIG: INSTRUMENT not present. Will use MUSE as defau | ||||
lt") | ||||
instrument = MUSE(psf=psf, lsf=lsf) | ||||
return instrument | ||||
class Instrument(object): | class Instrument(object): | |||
""" | """ | |||
This is a generic instrument class to use or extend. | This is a generic instrument class to use or extend. | |||
psf: None|PointSpreadFunction | psf: None|PointSpreadFunction | |||
The 2D Point Spread Function instance to use in the convolution, or None (the default). | The 2D Point Spread Function instance to use in the convolution, or None (the default). | |||
When this parameter is None, the instrument's PSF will not be appli ed to the cube. | When this parameter is None, the instrument's PSF will not be appli ed to the cube. | |||
lsf: None|LineSpreadFunction | lsf: None|LineSpreadFunction | |||
The 1D Line Spread Function instance to use in the convolution, or None (the default). | The 1D Line Spread Function instance to use in the convolution, or None (the default). | |||
Will be used in the convolution to spread the PSF 2D image through a third axis, z. | Will be used in the convolution to spread the PSF 2D image through a third axis, z. | |||
skipping to change at line 55 | skipping to change at line 219 | |||
if (self.xy_step is None) and (self.cube_default_xy_step is not Non e): | if (self.xy_step is None) and (self.cube_default_xy_step is not Non e): | |||
self.xy_step = self.cube_default_xy_step | self.xy_step = self.cube_default_xy_step | |||
if (self.z_step is not None) and (self.z_central is not None): | if (self.z_step is not None) and (self.z_central is not None): | |||
self.z_step_kms = 3e5 * self.z_step / self.z_central | self.z_step_kms = 3e5 * self.z_step / self.z_central | |||
else: | else: | |||
self.z_step_kms = None | self.z_step_kms = None | |||
if psf: | if psf: | |||
if not isinstance(psf, PointSpreadFunction): | if isinstance(psf, basestring): | |||
raise ValueError("PSF should be an instance of galpak.Point | psf = _read_psf(psf) | |||
SpreadFunction") | elif not isinstance(psf, PointSpreadFunction): | |||
raise ValueError("PSF should be an instance of galpak.Point | ||||
SpreadFunction or a string") | ||||
self.psf = psf | self.psf = psf | |||
if lsf: | if lsf: | |||
if not isinstance(lsf, LineSpreadFunction): | if not isinstance(lsf, LineSpreadFunction): | |||
raise ValueError("LSF should be an instance of galpak.LineS preadFunction") | raise ValueError("LSF should be an instance of galpak.LineS preadFunction") | |||
self.lsf = lsf | self.lsf = lsf | |||
self.lsf.z_cunit = self.z_cunit | self.lsf.z_cunit = self.z_cunit | |||
def use_pixelsize_from_cube(self, cube): | def use_pixelsize_from_cube(self, cube): | |||
""" | """ | |||
End of changes. 5 change blocks. | ||||
6 lines changed or deleted | 179 lines changed or added | |||
This html diff was produced by rfcdiff 1.41. The latest version is available from http://tools.ietf.org/tools/rfcdiff/ |