def create_chemistry(config): try: chemistry = config.pop('chemistry_type').lower() except KeyError: log.error('No chemistry defined in input') raise KeyError if chemistry in ('ace','equilibrium'): return create_ace(config) elif chemistry in ('file', ): from taurex.chemistry import ChemistryFile return create_klass(config, ChemistryFile) elif chemistry in ('custom',): from taurex.chemistry import Chemistry config['chemistry_type'] = 'custom' config, klass = determine_klass(config, 'chemistry_type', None, Chemistry) obj = klass(**config) return obj elif chemistry in ('taurex', 'complex', 'defined', 'free'): from taurex.data.profiles.chemistry import TaurexChemistry gases = [] config_key=[] for key, value in config.items(): if isinstance(value, dict): log.debug('FOUND GAS {} {}'.format(key, value)) config_key.append(key) gas_type = value.pop('gas_type').lower() klass = gas_factory(gas_type) gases.append(klass(molecule_name=key, **value)) for k in config_key: del config[k] log.debug('leftover keys {}'.format(config)) obj = TaurexChemistry(**config) for g in gases: obj.addGas(g) return obj else: raise ValueError('Unknown chemistry type {}'.format(chemistry))
def test_taurex_chemistry(self): from taurex.data.profiles.chemistry import TaurexChemistry, ConstantGas from taurex.cache import OpacityCache molecules = ['H2O', 'CH4'] mix_ratios = [1e-2, 1e-8] molecule_classes = [ConstantGas, ConstantGas] with patch.object(OpacityCache, "find_list_of_molecules") as mock_my_method: mock_my_method.return_value = molecules chemistry = TaurexChemistry(fill_gases=['H2', 'N2'], ratio=0.145) for mol, mix, klass in zip(molecules, mix_ratios, molecule_classes): chemistry.addGas(klass(mol, mix)) chemistry.initialize_chemistry(100, None, None, None) file_path = self.gen_valid_hdf5_output(chemistry, 'Test') with patch.object(OpacityCache, "find_list_of_molecules") as mock_my_method: mock_my_method.return_value = molecules with h5py.File(file_path, 'r') as f: loaded = load_chemistry_from_hdf5(f['Test']) loaded.initialize_chemistry(100, None, None, None) self.assertTrue(set(chemistry._fill_gases) == set(loaded._fill_gases)) self.assertTrue(set(chemistry._fill_ratio) == set(loaded._fill_ratio)) np.testing.assert_equal(chemistry.activeGasMixProfile, loaded.activeGasMixProfile)
def test_directimage(self): from taurex.util.hdf5 import load_model_from_hdf5 from taurex.model import DirectImageModel from taurex.data.profiles.chemistry import TaurexChemistry, ConstantGas from taurex.contributions import AbsorptionContribution, RayleighContribution from taurex.cache import OpacityCache with patch.object(OpacityCache, "find_list_of_molecules") as mock_my_method: mock_my_method.return_value = ['H2O'] chem = TaurexChemistry() chem.addGas(ConstantGas()) tm = DirectImageModel(chemistry=chem, ngauss=10) tm.add_contribution(AbsorptionContribution()) tm.add_contribution(RayleighContribution()) tm.build() tm.initialize_profiles() wngrid = np.linspace(100, 400) tm.star.initialize(wngrid) with patch.object(DirectImageModel, "model") as mock_my_method: mock_my_method = None file_path = self.gen_valid_hdf5_output(tm, 'Test') with h5py.File(file_path, 'r') as f: loaded = load_model_from_hdf5(f['Test']['ModelParameters']) loaded.build() loaded.initialize_profiles() wngrid = np.linspace(100, 400) self.assertTrue(isinstance(loaded, DirectImageModel)) np.testing.assert_array_equal(loaded.densityProfile, tm.densityProfile) truth_contrib = [type(c) for c in tm.contribution_list] loaded_contrib = [type(c) for c in loaded.contribution_list] self.assertTrue(set(truth_contrib) == set(loaded_contrib)) self.assertEqual(tm._ngauss, loaded._ngauss)
def _setup_defaults(self, nlayers, atm_min_pressure, atm_max_pressure): if self._pressure_profile is None: from taurex.data.profiles.pressure import SimplePressureProfile self.info('No pressure profile defined, using simple pressure ' 'profile with') self.info('parameters nlayers: %s, atm_pressure_range=(%s,%s)', nlayers, atm_min_pressure, atm_max_pressure) self._pressure_profile = \ SimplePressureProfile(nlayers, atm_min_pressure, atm_max_pressure) if self._planet is None: from taurex.data import Planet self.warning('No planet defined, using Jupiter as planet') self._planet = Planet() if self._temperature_profile is None: from taurex.data.profiles.temperature import Isothermal self.warning('No temeprature profile defined using default ' 'Isothermal profile with T=1500 K') self._temperature_profile = Isothermal() if self._chemistry is None: from taurex.data.profiles.chemistry import TaurexChemistry, \ ConstantGas tc = TaurexChemistry() self.warning('No gas profile set, using constant profile with H2O ' 'and CH4') tc.addGas(ConstantGas('H2O', mix_ratio=1e-5)) tc.addGas(ConstantGas('CH4', mix_ratio=1e-6)) self._chemistry = tc if self._star is None: from taurex.data.stellar import BlackbodyStar self.warning('No star, using the Sun') self._star = BlackbodyStar()
def _compute_inital_mu(self): from taurex.data.profiles.chemistry import TaurexChemistry, ConstantGas tc = TaurexChemistry() tc.addGas(ConstantGas('H2O')) self._inital_mu = tc