| galpak3d_utils.py | galpak3d_utils.py | |||
|---|---|---|---|---|
| import os | import os | |||
| import configparser | import configparser | |||
| from .spread_functions import * | ||||
| from .model_sersic3d import ModelSersic | from .model_sersic3d import ModelSersic | |||
| from .instruments import * | DiskModel = DefaultModel = ModelSersic | |||
| import logging | import logging | |||
| logging.basicConfig(level=logging.INFO) | logging.basicConfig(level=logging.INFO) | |||
| logger = logging.getLogger('GalPaK: Utils') | logger = logging.getLogger('GalPaK: Utils') | |||
| #Python3 compatibility | ||||
| try: | ||||
| basestring | ||||
| except NameError: | ||||
| basestring = str | ||||
| def _save_to_file(filename, contents, clobber): | def _save_to_file(filename, contents, clobber): | |||
| if not clobber and os.path.isfile(filename): | if not clobber and os.path.isfile(filename): | |||
| raise IOError("The file {} already exists. Specify overwrite=True t o overwrite it.".format(filename)) | raise IOError("The file {} already exists. Specify overwrite=True t o overwrite it.".format(filename)) | |||
| with open(filename, 'w') as f: | with open(filename, 'w') as f: | |||
| f.write(contents) | f.write(contents) | |||
| def _read_file(filename): | #def _read_file(filename): | |||
| with open(filename, 'r') as f: | # with open(filename, 'r') as f: | |||
| contents = f.read() | # contents = f.read() | |||
| return contents | # return contents | |||
| 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.getargspec(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 | ||||
| def _read_model(file_config): | def _read_model(file_config): | |||
| """ | """ | |||
| sets model from config MODEL | sets model from config MODEL | |||
| :return: | :return: | |||
| """ | """ | |||
| model = None | model = None | |||
| if file_config is not None: | if file_config is not None: | |||
| if os.path.isfile(file_config): | if os.path.isfile(file_config): | |||
| logger.info("Reading model {:s}".format(file_config)) | logger.info("Reading model {:s}".format(file_config)) | |||
| config = configparser.RawConfigParser() | config = configparser.RawConfigParser() | |||
| config.read(file_config) | config.read(file_config) | |||
| else: | else: | |||
| raise ValueError("Model Config file %s not present" % (file_con fig)) | raise ValueError("Model Config file %s not present" % (file_con fig)) | |||
| else: | else: | |||
| raise ValueErrror("Model Config file not defined") | raise ValueError("Model Config file not defined") | |||
| if config.has_section('MODEL'): | if config.has_section('MODEL'): | |||
| config = config['MODEL'] | config = config['MODEL'] | |||
| else: | else: | |||
| logger.warning("CONFIG file has no MODEL section") | logger.warning("CONFIG file has no MODEL section") | |||
| if 'type' in list(config.keys()): | if 'type' in list(config.keys()): | |||
| model_type = config['type'].lower() | model_type = config['type'].lower() | |||
| else: | else: | |||
| logger.error("CONFIG: Model: type not specified") | logger.error("CONFIG: Model: type not specified") | |||
| skipping to change at line 215 | skipping to change at line 65 | |||
| args={} | args={} | |||
| #try: | #try: | |||
| # redshift = float(config['redshift']) | # redshift = float(config['redshift']) | |||
| #except: | #except: | |||
| # redshift = None | # redshift = None | |||
| #args['redshift']=redshift | #args['redshift']=redshift | |||
| if 'default' in model_type: | if 'default' in model_type: | |||
| model = DefaultModel | model = DefaultModel | |||
| elif 'sersic2d' in model_type: | ||||
| model = ModelSersic2D | ||||
| elif 'sersic' in model_type: | elif 'sersic' in model_type: | |||
| model = ModelSersic | model = ModelSersic | |||
| if 'rotation_curve' in config.keys(): | if 'rotation_curve' in config.keys(): | |||
| args['rotation_curve']=config['rotation_curve'] | args['rotation_curve']=config['rotation_curve'] | |||
| elif 'decomp' in model_type: | elif 'disk' in model_type: | |||
| model = ModelDecomp | model = DiskModel | |||
| else: | else: | |||
| raise ValueError("Model type invalid. Must be ModelSersic2D or Mode lSersic or ModelDecomp") | raise ValueError("Model type invalid. Must be DefaultModel or Model Sersic or DiskModel") | |||
| #args parameters | #args parameters | |||
| import inspect | import inspect | |||
| var_args = inspect.getargspec(model).args | var_args = inspect.getfullargspec(model).args | |||
| for k in var_args[1:]: | for k in var_args[1:]: | |||
| if k.lower() in list(config.keys()): | if k.lower() in list(config.keys()): | |||
| try: | try: | |||
| args[k]=eval(config[k]) | args[k]=eval(config[k]) | |||
| except: | except: | |||
| args[k]=config[k] | args[k]=config[k] | |||
| return model(**args) | return model(**args) | |||
| End of changes. 9 change blocks. | ||||
| 175 lines changed or deleted | 16 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/ | ||||