def weight_opacities(self, vol_mix_ratio, vol_mix_ratio2, mass, opac): """ weights opacities """ print("\nWeighting by mass mixing ratio...") for t in range(self.final_nt): for p in range(self.final_np): mass_mix_ratio = vol_mix_ratio[ p + self.final_np * t] * vol_mix_ratio2[ p + self.final_np * t] * mass / self.mu[p + self.final_np * t] for x in range(self.nx): tls.percent_counter(t, self.final_nt, p, self.final_np, x, self.nx) for y in range(self.ny): weighted_opac = mass_mix_ratio * opac[ y + self.ny * x + self.ny * self.nx * p + self.ny * self.nx * self.final_np * t] self.combined_opacities[ y + self.ny * x + self.ny * self.nx * p + self.ny * self.nx * self.final_np * t] += weighted_opac
def big_loop(self, param): """ loops through the molecules and connects the other methods - aka the big guy """ for m in range(len(self.mol_list)): self.press_cgs = [] self.lamda_delim = [] self.lamda_mid = [] self.d_lamda = [] self.numin_mol_list = [] self.numax_mol_list = [] self.temp_mol_list = [] # list with all the opacities for each y value self.ky = [] self.init_mol(m) percentage = 0 if self.check_if_exist(param, m): # skip to next molecule continue for t in range(len(self.temp_list)): for p in range(len(self.press_exp_list)): print('t:', t, 'p:', p) tls.percent_counter(t, len(self.temp_list), p, len(self.press_exp_list)) if self.press_exp_list[p] >= 0: char = 'p' else: char = 'n' for n in range(len(self.numin_mol_list)): self.coeffs = [] self.ystart = [] self.read_chebyshev(m,p,t,char,n) self.calc_opac() if p == 0 and t == 0: self.cons_check() self.fill_rearr() self.conversion() self.write_tell(param, m)
def calc_h_minus(self, conti, param): """ calculates the H- continuum opacity """ print("\nCalculating H- ...") total_opac = [] # pressure and temperature dependent bound-free and free-free opacities for t in range(self.final_nt): for p in range(self.final_np): tls.percent_counter(t, self.final_nt, p, self.final_np) for x in range(self.nx): cross_bf_h_min = conti.bf_cross_sect_h_min( self.k_x[x], self.final_temp[t], self.final_press[p], self.n_e[p + self.final_np * t]) / (pc.M_H * pc.AMU) cross_ff_h_min = conti.ff_cross_sect_h_min( self.k_x[x], self.final_temp[t], self.final_press[p], self.n_e[p + self.final_np * t]) / (pc.M_H * pc.AMU) for y in range(self.ny): total_opac.append(cross_bf_h_min + cross_ff_h_min) # write to h5 file to allow for quick visualization later if param.format == 'ktable': ending = "_opac_ip.h5" elif param.format == 'sampling': ending = "_opac_ip_sampling.h5" self.write_h5(param, "H-" + ending, total_opac) # add to mol list self.molname_list.append("H-".encode('utf8')) print("\nH- calculation complete!") return total_opac
def big_loop(self, param): for m in range(len(self.species_name)): # get molecular parameter ranges files = os.listdir(self.species_path[m]) file_list = [f for f in files if "Out_" in f] temp_list = [] press_list = [] numin_list = [] numax_list = [] press_exp_list = [] species_nr = 'not provided' for f in file_list: if 'Opacity_Atoms' in self.species_path[m]: # numin_list.append(int(f[9:14])) # taking hardcoded wavenumber limits (see below), because some molecules have lower boundaries # numax_list.append(int(f[15:20])) temp_list.append(int(f[21:26])) if species_nr == 'not provided': species_nr = int(f[4:8]) elif 'Opacity2' in self.species_path[m]: # numin_list.append(int(f[7:12])) # numax_list.append(int(f[13:18])) temp_list.append(int(f[19:24])) if species_nr == 'not provided': species_nr = int(f[4:6]) elif 'Opacity3' in self.species_path[m]: # numin_list.append(int(f[4:9])) # numax_list.append(int(f[10:15])) temp_list.append(int(f[16:21])) # hardcoded wavenumber limits -- improve this at some point in future numin_list = npy.arange(0, 30000, 1000) numax_list = npy.arange(1000, 31000, 1000) # delete duplicate entries in the lists and sort in ascending order # numin_list = list(set(numin_list)) # numax_list = list(set(numax_list)) temp_list = list(set(temp_list)) numin_list.sort() numax_list.sort() temp_list.sort() temp_min = min(temp_list) temp_max = max(temp_list) numin = min(numin_list) numax = max(numax_list) # hardcoded pressures -- improve this at some point in future if 'Opacity_Atoms' not in self.species_path[m]: press_list_p1 = [10**p for p in npy.arange(0, 10, 1)] press_list_p2 = [ 10**p for p in npy.arange(0.33333333, 9.33333333, 1) ] press_list_p3 = [ 10**p for p in npy.arange(0.66666666, 9.66666666, 1) ] press_list = npy.append( press_list_p1, npy.append(press_list_p2, press_list_p3)) press_list.sort() press_exp_list = [ 'n600', 'n566', 'n533', 'n500', 'n466', 'n433', 'n400', 'n366', 'n333', 'n300', 'n266', 'n233', 'n200', 'n166', 'n133', 'n100', 'n066', 'n033', 'p000', 'p033', 'p066', 'p100', 'p133', 'p166', 'p200', 'p233', 'p266', 'p300' ] elif 'Opacity_Atoms' in self.species_path[m]: press_list = [1e-2] press_exp_list = ['n800'] # some user feedback to check whether all is fine print("\n--- working on ---") print("molecule or atom: ", self.species_name[m]) print("species number: ", species_nr) print("wavenumber range: ", numin, numax) print("temperature range: ", temp_min, temp_max) print("number of wavelength bins:", len(self.rt_nu), "\n") opac_array = [] # read files for t in range(len(temp_list)): for p in range(len(press_exp_list)): tls.percent_counter(t, len(temp_list), p, len(press_exp_list)) opac_array_temp = [] for n in range(len(numin_list)): for i in range(len(self.rt_nu)): if self.rt_nu[i] < numin_list[n]: continue elif numin_list[n] <= self.rt_nu[i] < numax_list[n]: try: if 'Opacity_Atoms' in self.species_path[m]: content = npy.fromfile( self.species_path[m] + "Out_{:04d}_{:05d}_{:05d}_{:05d}_". format(species_nr, numin_list[n], numax_list[n], temp_list[t]) + press_exp_list[p] + ".bin", npy.float32, -1, "") elif 'Opacity3' in self.species_path[m]: content = npy.fromfile( self.species_path[m] + "Out_{:05d}_{:05d}_{:05d}_".format( numin_list[n], numax_list[n], temp_list[t]) + press_exp_list[p] + ".bin", npy.float32, -1, "") elif 'Opacity2' in self.species_path[m]: content = npy.fromfile( self.species_path[m] + "Out_{:02d}_{:05d}_{:05d}_{:05d}_". format(species_nr, numin_list[n], numax_list[n], temp_list[t]) + press_exp_list[p] + ".bin", npy.float32, -1, "") index = round(self.rt_nu[i] * 100) - int( numin_list[n] * 100) opac_array_temp.append(content[index]) except IOError: opac_array_temp.append(1e-15) elif self.rt_nu[i] >= numax_list[n]: break # reverse array opac_array_temp.reverse() opac_array.extend(opac_array_temp) try: # create directory if necessary os.makedirs(param.resampling_path) except OSError: if not os.path.isdir(param.resampling_path): raise # save to hdf5 with h5py.File( param.resampling_path + self.species_name[m] + "_opac_sampling.h5", "w") as f: f.create_dataset("pressures", data=press_list) f.create_dataset("temperatures", data=temp_list) f.create_dataset("wavelengths", data=self.rt_lamda) f.create_dataset("opacities", data=opac_array) print("\nSuccessfully completed -->", self.species_name[m], "<-- !\n---------------------")
def weight_opacities(self, param, species, vol_mix_ratio, mass, kpoints, cia='no'): """ weights opacities """ try: with h5py.File(param.resampling_path + species + ".h5", "r") as weightfile: self.mixed_opacities = [ k for k in weightfile["weighted kpoints"][:] ] print( "\nWeighted file " + param.resampling_path + species + ".h5 already exists. Reading in weighted opacities for this molecule..." ) if len(self.mixed_opacities ) == self.nt * self.np * self.nx * self.ny: print("Dimensions test passed\n") else: print("Dimensions test failed. Aborting...") raise SystemExit() except OSError: if cia == 'yes': vol_mix_ratio2 = npy.concatenate( (self.fastchem_data_low['H2'], self.fastchem_data_high['H2'])) print("\nWeighting by mass mixing ratio...") for t in range(self.nt): for p in range(self.np): mass_mix_ratio = vol_mix_ratio[ p + self.np * t] * mass / self.mu[p + self.np * t] if param.special_abundance == 'pure_H2O': mass_mix_ratio = 1 self.mu[p + self.np * t] = 18.0153 elif param.special_abundance == 'pure_CO2': mass_mix_ratio = 1 self.mu[p + self.np * t] = 44.01 if param.special_abundance == 'venus' and species == 'H2O_weighted': mass_mix_ratio = 1.227e-5 self.mu[p + self.np * t] = 44.01 if param.special_abundance == 'venus' and species == 'CO2_weighted': mass_mix_ratio = 1 self.mu[p + self.np * t] = 44.01 if cia == 'yes': mass_mix_ratio *= vol_mix_ratio2[p + self.np * t] for x in range(self.nx): tls.percent_counter(t, self.nt, p, self.np, x, self.nx) for y in range(self.ny): mixed = mass_mix_ratio * kpoints[ y + self.ny * x + self.ny * self.nx * p + self.ny * self.nx * self.np * t] self.mixed_opacities.append(mixed) with h5py.File(param.resampling_path + species + ".h5", "w") as weightfile: weightfile.create_dataset("weighted kpoints", data=self.mixed_opacities)
def tabulate_rayleigh_cross_section(self, ray, param, f_h2o): """ tabulates the rayleigh cross sections for various species """ print("Tabulating Rayleigh cross-sections...") try: with h5py.File(param.final_path + "Rayleigh.h5", "r") as ray_file: self.weighted_cross_ray_table = [ o for o in ray_file["weighted Rayleigh cross-sections"][:] ] self.pure_cross_ray_h2_table = [ o for o in ray_file["pure H2 Rayleigh cross-sections"][:] ] self.pure_cross_ray_he_table = [ o for o in ray_file["pure He Rayleigh cross-sections"][:] ] self.pure_cross_ray_h_table = [ o for o in ray_file["pure H Rayleigh cross-sections"][:] ] self.pure_cross_ray_co2_table = [ o for o in ray_file["pure CO2 Rayleigh cross-sections"][:] ] self.pure_cross_ray_h2o_table = [ o for o in ray_file["pure H2O Rayleigh cross-sections"][:] ] print( "File " + param.final_path + "Rayleigh.h5 already exists. Reading of Rayleigh cross-sections successful." ) except OSError or KeyError: # obtain relevant volume mixing ratios f_h2 = npy.concatenate( (self.fastchem_data_low['H2'], self.fastchem_data_high['H2'])) f_he = npy.concatenate( (self.fastchem_data_low['He'], self.fastchem_data_high['He'])) f_co2 = npy.concatenate((self.fastchem_data_low['C1O2'], self.fastchem_data_high['C1O2'])) f_h = npy.concatenate( (self.fastchem_data_low['H'], self.fastchem_data_high['H'])) if param.special_abundance == 'pure_H2O': f_h2o = [1 for i in range(len(f_h2o))] f_co2 = [0 for i in range(len(f_co2))] f_h2 = [0 for i in range(len(f_h2))] f_h = [0 for i in range(len(f_h))] f_he = [0 for i in range(len(f_he))] elif param.special_abundance == 'pure_CO2': f_h2o = [0 for i in range(len(f_h2o))] f_co2 = [1 for i in range(len(f_co2))] f_h2 = [0 for i in range(len(f_h2))] f_h = [0 for i in range(len(f_h))] f_he = [0 for i in range(len(f_he))] elif param.special_abundance == 'venus': f_h2o = [3e-5 for i in range(len(f_h2o))] f_co2 = [1 for i in range(len(f_co2))] f_h2 = [0 for i in range(len(f_h2))] f_h = [0 for i in range(len(f_h))] f_he = [0 for i in range(len(f_he))] for t in range(self.nt): for p in range(self.np): tls.percent_counter(t, self.nt, p, self.np) for x in range(self.nx): cross_ray_h2o = ray.cross_sect( self.k_x[x], ray.index_h2o(self.k_x[x], self.chem_press[p], self.chem_temp[t], self.mu[p + self.np * t]), ray.n_ref_h2o(self.chem_press[p], self.chem_temp[t]), ray.King_h2o, lamda_limit=2.5e-4) cross_ray_h2 = ray.cross_sect( self.k_x[x], ray.index_h2(self.k_x[x]), ray.n_ref_h2, ray.King_h2) cross_ray_he = ray.cross_sect( self.k_x[x], ray.index_he(self.k_x[x]), ray.n_ref_he, ray.King_he) cross_ray_co2 = ray.cross_sect( self.k_x[x], ray.index_co2(self.k_x[x]), ray.n_ref_co2, ray.King_co2(self.k_x[x])) cross_ray_h = ray.cross_sect_h(self.k_x[x]) self.pure_cross_ray_h2o_table.append(cross_ray_h2o) # the following cross-sections depend only on the wavelength if t == 0 and p == 0: self.pure_cross_ray_h2_table.append(cross_ray_h2) self.pure_cross_ray_he_table.append(cross_ray_he) self.pure_cross_ray_co2_table.append(cross_ray_co2) self.pure_cross_ray_h_table.append(cross_ray_h) # mix everything according to their volume (number) mixing ratios mix = f_h2[p + self.np * t] * cross_ray_h2 \ + f_he[p + self.np * t] * cross_ray_he \ + f_h2o[p + self.np * t] * cross_ray_h2o \ + f_co2[p + self.np * t] * cross_ray_co2 \ + f_h[p + self.np * t] * cross_ray_h self.weighted_cross_ray_table.append(mix) try: os.makedirs(param.final_path) except OSError: if not os.path.isdir(param.final_path): raise with h5py.File(param.final_path + "Rayleigh.h5", "w") as ray_file: ray_file.create_dataset("pressures", data=self.chem_press) ray_file.create_dataset("temperatures", data=self.chem_temp) ray_file.create_dataset("wavelengths", data=self.k_x) ray_file.create_dataset("weighted Rayleigh cross-sections", data=self.weighted_cross_ray_table) ray_file.create_dataset("pure H2 Rayleigh cross-sections", data=self.pure_cross_ray_h2_table) ray_file.create_dataset("pure He Rayleigh cross-sections", data=self.pure_cross_ray_he_table) ray_file.create_dataset("pure H2O Rayleigh cross-sections", data=self.pure_cross_ray_h2o_table) ray_file.create_dataset("pure CO2 Rayleigh cross-sections", data=self.pure_cross_ray_co2_table) ray_file.create_dataset("pure H Rayleigh cross-sections", data=self.pure_cross_ray_h_table) ray_file.create_dataset("FastChem path", data=param.fastchem_path) ray.success()
def interpolate_to_new_grid(self, temp_old, temp_new, press_old, press_new, k_old): print("opacity temperature grid:\n", temp_old[:3], "...", temp_old[-3:]) print("final table temperature grid:\n", temp_new[:3], "...", temp_new[-3:]) print("opacity pressure grid:\n", press_old[:3], "...", press_old[-3:]) print("final table pressure grid:\n", press_new[:3], "...", press_new[-3:]) print("ny:", self.ny, "nx:", self.nx, "np:", self.np, "nt:", self.nt) k_new = npy.zeros(self.ny * self.nx * self.np * self.nt) old_nt = len(temp_old) old_np = len(press_old) print("old np:", old_np) print("old nt:", old_nt) for i in range(self.nt): for j in range(self.np): tls.percent_counter(i, self.nt, j, self.np) reduced_t = 0 reduced_p = 0 try: t_left = max([ t for t in range(len(temp_old)) if temp_old[t] <= temp_new[i] ]) except ValueError: t_left = 0 reduced_t = 1 try: p_left = max([ p for p in range(len(press_old)) if press_old[p] <= press_new[j] ]) except ValueError: p_left = 0 reduced_p = 1 if t_left == len(temp_old) - 1: reduced_t = 1 if p_left == len(press_old) - 1: reduced_p = 1 if reduced_p == 1 and reduced_t == 1: for y in range(self.ny): for x in range(self.nx): k_new[y + self.ny * x + self.ny * self.nx * j + self.ny * self.nx * self.np * i] = \ k_old[y + self.ny*x + self.ny*self.nx*p_left + self.ny*self.nx*old_np*t_left] elif reduced_p != 1 and reduced_t == 1: p_right = p_left + 1 for y in range(self.ny): for x in range(self.nx): k_new[y + self.ny*x + self.ny*self.nx*j + self.ny*self.nx*self.np*i] = \ (k_old[y + self.ny*x + self.ny*self.nx*p_right + self.ny*self.nx*old_np*t_left] * (npy.log10(press_new[j]) - npy.log10(press_old[p_left])) \ + k_old[y + self.ny*x + self.ny*self.nx*p_left + self.ny*self.nx*old_np*t_left] * (npy.log10(press_old[p_right]) - npy.log10(press_new[j])) \ ) / (npy.log10(press_old[p_right]) - npy.log10(press_old[p_left])) elif reduced_p == 1 and reduced_t != 1: t_right = t_left + 1 for y in range(self.ny): for x in range(self.nx): k_new[y + self.ny * x + self.ny * self.nx * j + self.ny * self.nx * self.np * i] = \ (k_old[y + self.ny * x + self.ny * self.nx * p_left + self.ny * self.nx * old_np * t_right] * (temp_new[i] - temp_old[t_left]) \ + k_old[y + self.ny * x + self.ny * self.nx * p_left + self.ny * self.nx * old_np * t_left] * (temp_old[t_right] - temp_new[i]) \ ) / (temp_old[t_right] - temp_old[t_left]) elif reduced_p != 1 and reduced_t != 1: p_right = p_left + 1 t_right = t_left + 1 for y in range(self.ny): for x in range(self.nx): try: k_new[y + self.ny * x + self.ny * self.nx * j + self.ny * self.nx * self.np * i] = \ ( k_old[y + self.ny * x + self.ny * self.nx * p_right + self.ny * self.nx * old_np * t_right] * (temp_new[i] - temp_old[t_left]) * (npy.log10(press_new[j]) - npy.log10(press_old[p_left])) \ + k_old[y + self.ny * x + self.ny * self.nx * p_left + self.ny * self.nx * old_np * t_right] * (temp_new[i] - temp_old[t_left]) * (npy.log10(press_old[p_right]) - npy.log10(press_new[j]))\ + k_old[y + self.ny * x + self.ny * self.nx * p_right + self.ny * self.nx * old_np * t_left] * (temp_old[t_right] - temp_new[i]) * (npy.log10(press_new[j]) - npy.log10(press_old[p_left])) \ + k_old[y + self.ny * x + self.ny * self.nx * p_left + self.ny * self.nx * old_np * t_left] * (temp_old[t_right] - temp_new[i]) * (npy.log10(press_old[p_right]) - npy.log10(press_new[j])) \ ) / ((temp_old[t_right] - temp_old[t_left]) * (npy.log10(press_old[p_right]) - npy.log10(press_old[p_left]))) except IndexError: print("IndexError at:", y, x, j, i, p_left, p_right, t_left, t_right, len(k_old), len(temp_new), len(press_new)) return k_new
def include_rayleigh_cross_section(self, ray, species, vol_mix_ratio): """ tabulates the rayleigh cross sections for various species """ print("\nCalculating scattering cross sections ...") # this is kind of hidden -- TODO move to a better location at some point # references for the scattering data: # H2: Cox 2000 # He: Sneep & Ubachs 2005, Thalman et al. 2014 # H: Lee & Kim 2004 # H2O: Murphy 1977, Wagner & Kretzschmar 2008 # CO: Sneep & Ubachs 2005 # CO2: Sneep & Ubachs 2005, Thalman et al. 2014 # O2: Sneep & Ubachs 2005, Thalman et al. 2014 # N2: Sneep & Ubachs 2005, Thalman et al. 2014 list_of_implemented_scattering_species = [ 'H', 'H2', 'He', 'H2O', 'CO2', 'CO', 'O2', 'N2' ] if species.name in list_of_implemented_scattering_species: for t in range(self.final_nt): for p in range(self.final_np): tls.percent_counter(t, self.final_nt, p, self.final_np) for x in range(self.nx): cross_section = 0 if species.name != 'H': if species.name == "H2O": index = ray.index_h2o( self.k_x[x], self.final_press[p], self.final_temp[t], self.mu[p + self.final_np * t]) n_ref = ray.n_ref_h2o(self.final_press[p], self.final_temp[t]) King = ray.King_h2o lamda_limit = 2.5e-4 elif species.name == "CO2": index = ray.index_co2(self.k_x[x]) n_ref = ray.n_ref_co2 King = ray.King_co2(self.k_x[x]) lamda_limit = self.k_x[-1] elif species.name == "H2": index = ray.index_h2(self.k_x[x]) n_ref = ray.n_ref_h2 King = ray.King_h2 lamda_limit = self.k_x[-1] elif species.name == "He": index = ray.index_he(self.k_x[x]) n_ref = ray.n_ref_he King = ray.King_he lamda_limit = self.k_x[-1] elif species.name == "N2": index = ray.index_n2(self.k_x[x]) n_ref = ray.n_ref_n2 King = ray.King_n2(self.k_x[x]) lamda_limit = self.k_x[-1] elif species.name == "O2": index = ray.index_o2(self.k_x[x]) n_ref = ray.n_ref_o2 King = ray.King_o2(self.k_x[x]) lamda_limit = self.k_x[-1] elif species.name == "CO": index = ray.index_co(self.k_x[x]) n_ref = ray.n_ref_co King = ray.King_co lamda_limit = self.k_x[-1] cross_section = ray.cross_sect( self.k_x[x], index, n_ref, King, lamda_limit) elif species.name == "H": cross_section = ray.cross_sect_h(self.k_x[x]) self.combined_cross_sections[ x + self.nx * p + self.nx * self.final_np * t] += vol_mix_ratio[ p + self.final_np * t] * cross_section else: print( "WARNING WARNING WARNING: Rayleigh scattering cross sections for species", species.name, "not found. Please double-check! Continuing without those... ")
def interpolate_opacity_to_final_grid(self, k_press, k_temp, kpoints): temp_old = k_temp press_old = k_press temp_new = self.final_temp press_new = self.final_press k_old = kpoints print("opacity temperature grid:\n", temp_old[:3], "...", temp_old[-3:]) print("final table temperature grid:\n", temp_new[:3], "...", temp_new[-3:]) print("opacity pressure grid:\n", press_old[:3], "...", press_old[-3:]) print("final table pressure grid:\n", press_new[:3], "...", press_new[-3:]) print("ny:", self.ny, "nx:", self.nx, "np:", self.final_np, "nt:", self.final_nt) k_new = npy.zeros(self.ny * self.nx * self.final_np * self.final_nt) old_nt = len(temp_old) old_np = len(press_old) print("old np:", old_np) print("old nt:", old_nt) for i in range(self.final_nt): for j in range(self.final_np): tls.percent_counter(i, self.final_nt, j, self.final_np) reduced_t = 0 reduced_p = 0 try: t_left = max([ t for t in range(len(temp_old)) if temp_old[t] <= temp_new[i] ]) except ValueError: t_left = 0 reduced_t = 1 try: p_left = max([ p for p in range(len(press_old)) if press_old[p] <= press_new[j] ]) except ValueError: p_left = 0 reduced_p = 1 if t_left == len(temp_old) - 1: reduced_t = 1 if p_left == len(press_old) - 1: reduced_p = 1 if reduced_p == 1 and reduced_t == 1: for y in range(self.ny): for x in range(self.nx): k_new[y + self.ny * x + self.ny * self.nx * j + self.ny * self.nx * self.final_np * i] = \ k_old[y + self.ny*x + self.ny*self.nx*p_left + self.ny*self.nx*old_np*t_left] elif reduced_p != 1 and reduced_t == 1: p_right = p_left + 1 for y in range(self.ny): for x in range(self.nx): k_new[y + self.ny*x + self.ny*self.nx*j + self.ny*self.nx*self.final_np*i] = \ (k_old[y + self.ny*x + self.ny*self.nx*p_right + self.ny*self.nx*old_np*t_left] * (npy.log10(press_new[j]) - npy.log10(press_old[p_left])) \ + k_old[y + self.ny*x + self.ny*self.nx*p_left + self.ny*self.nx*old_np*t_left] * (npy.log10(press_old[p_right]) - npy.log10(press_new[j])) \ ) / (npy.log10(press_old[p_right]) - npy.log10(press_old[p_left])) elif reduced_p == 1 and reduced_t != 1: t_right = t_left + 1 for y in range(self.ny): for x in range(self.nx): k_new[y + self.ny * x + self.ny * self.nx * j + self.ny * self.nx * self.final_np * i] = \ (k_old[y + self.ny * x + self.ny * self.nx * p_left + self.ny * self.nx * old_np * t_right] * (temp_new[i] - temp_old[t_left]) \ + k_old[y + self.ny * x + self.ny * self.nx * p_left + self.ny * self.nx * old_np * t_left] * (temp_old[t_right] - temp_new[i]) \ ) / (temp_old[t_right] - temp_old[t_left]) elif reduced_p != 1 and reduced_t != 1: p_right = p_left + 1 t_right = t_left + 1 for y in range(self.ny): for x in range(self.nx): k_new[y + self.ny * x + self.ny * self.nx * j + self.ny * self.nx * self.final_np * i] = \ ( k_old[y + self.ny * x + self.ny * self.nx * p_right + self.ny * self.nx * old_np * t_right] * (temp_new[i] - temp_old[t_left]) * (npy.log10(press_new[j]) - npy.log10(press_old[p_left])) \ + k_old[y + self.ny * x + self.ny * self.nx * p_left + self.ny * self.nx * old_np * t_right] * (temp_new[i] - temp_old[t_left]) * (npy.log10(press_old[p_right]) - npy.log10(press_new[j]))\ + k_old[y + self.ny * x + self.ny * self.nx * p_right + self.ny * self.nx * old_np * t_left] * (temp_old[t_right] - temp_new[i]) * (npy.log10(press_new[j]) - npy.log10(press_old[p_left])) \ + k_old[y + self.ny * x + self.ny * self.nx * p_left + self.ny * self.nx * old_np * t_left] * (temp_old[t_right] - temp_new[i]) * (npy.log10(press_old[p_right]) - npy.log10(press_new[j])) \ ) / ((temp_old[t_right] - temp_old[t_left]) * (npy.log10(press_old[p_right]) - npy.log10(press_old[p_left]))) if npy.isnan(k_new[y + self.ny * x + self.ny * self.nx * j + self.ny * self.nx * self.final_np * i]): print("NaN-Error at entry with indices:", y, x, j, i) raise SystemExit() return k_new
def big_loop(self, param): for m in range(len(self.species_name)): if not self.species_path[m].endswith("/"): self.species_path[m] += "/" # get molecular parameter ranges files = os.listdir(self.species_path[m]) file_list = [ f for f in files if ("Out_" in f) and ("_cbin" not in f) ] if param.heliosk_format == 'binary': file_list = [f for f in file_list if ".bin" in f] elif param.heliosk_format == 'text': file_list = [f for f in file_list if ".dat" in f] # check that format correct if file_list == []: print( "No files with the correct format found in the chosen directory. Please double-check that the Helios-K format is correct." ) print("Aborting...") raise SystemExit() # determine filename structure with one example file in the directory file_name = None example_file = file_list[0] nr_underscore = example_file.count("_") indices = [] for i in range(len(example_file)): if not example_file.find("_", i) in indices: indices.append(example_file.find("_", i)) indices.pop(-1) # remove "-1" entry if nr_underscore > 4: # Aha! This is the case with species number start_file_name = indices[0] + 1 end_file_name = indices[-4] file_name = example_file[start_file_name:end_file_name] start_numin = indices[-4] + 1 end_numin = indices[-3] start_numax = indices[-3] + 1 end_numax = indices[-2] start_temp = indices[-2] + 1 end_temp = indices[-1] start_press = indices[-1] + 1 end_press = indices[-1] + 5 temp_list = [] numin_list = [] numax_list = [] press_exp_list = [] for f in file_list: numin_list.append(int(f[start_numin:end_numin])) numax_list.append(int(f[start_numax:end_numax])) temp_list.append(int(f[start_temp:end_temp])) press_exp_list.append(f[start_press:end_press]) # delete duplicate entries in the lists and sort in ascending order temp_list = list(set(temp_list)) temp_list.sort() numin_list = list(set(numin_list)) numin_list.sort() numax_list = list(set(numax_list)) numax_list.sort() press_list = [ self.press_dict[press_exp_list[p]] for p in range(len(press_exp_list)) ] press_list = list(set(press_list)) press_list.sort() # getting back the press_exp list in ascending order press_exp_list_ordered = [] for p in press_list: for k in self.press_dict.keys(): if self.press_dict[k] == p: press_exp_list_ordered.append(k) break temp_min = min(temp_list) temp_max = max(temp_list) numin = min(numin_list) numax = max(numax_list) press_min = min(press_list) press_max = max(press_list) # some user feedback to check whether all is fine print("\n--- working on ---") print("molecule or atom: ", self.species_name[m]) if not file_name is None: print("Files named as:", file_name) else: print("Files not specifically named.") print("wavenumber range: ", numin, numax) print("temperature range: ", temp_min, temp_max) print("pressure range: {:g} {:g}".format(press_min, press_max)) print("number of wavelength bins:", len(self.rt_nu), "\n") opac_array = [] # read files for t in range(len(temp_list)): for p in range(len(press_exp_list_ordered)): opac_array_temp = [] for n in range(len(numin_list)): exist = 1 tls.percent_counter(t, len(temp_list), p, len(press_exp_list_ordered)) # opening correct file if param.heliosk_format == "binary": if file_name is None: file = self.species_path[ m] + "Out_{:05d}_{:05d}_{:05d}_".format( numin_list[n], numax_list[n], temp_list[t] ) + press_exp_list_ordered[p] + ".bin" else: file = self.species_path[ m] + "Out_{}_{:05d}_{:05d}_{:05d}_".format( file_name, numin_list[n], numax_list[n], temp_list[t] ) + press_exp_list_ordered[p] + ".bin" try: content = npy.fromfile(file, npy.float32, -1, "") except IOError: print( "WARNING: File '" + file + "' not found. Using value 1e-15 for opacity in this regime." ) exist = 0 elif param.heliosk_format == "text": if file_name is None: file = self.species_path[ m] + "Out_{:05d}_{:05d}_{:05d}_".format( numin_list[n], numax_list[n], temp_list[t] ) + press_exp_list_ordered[p] + ".dat" else: file = self.species_path[ m] + "Out_{}_{:05d}_{:05d}_{:05d}_".format( file_name, numin_list[n], numax_list[n], temp_list[t] ) + press_exp_list_ordered[p] + ".dat" try: content = self.read_dat_file(file) except IOError: print( "WARNING: File '" + file + "' not found. Using value 1e-15 for opacity in this regime." ) exist = 0 # read out the entries that match with the chosen wavenumber grid values for i in range(len(self.rt_nu)): if self.rt_nu[i] < numin_list[n]: continue elif numin_list[n] <= self.rt_nu[i] < numax_list[n]: if exist == 1: index = round(self.rt_nu[i] * 100) - int( numin_list[n] * 100 ) # WARNING: Assumes a HELIOS-K resolution of 0.01 cm^-1 opac_array_temp.append( float(content[index])) elif exist == 0: opac_array_temp.append(1e-15) elif self.rt_nu[i] >= numax_list[n]: break # filling up opacity array to match the length of the wavenumber array # this is necessary if opacity data wavenumber grid ends earlier than the input wavenumber grid while len(opac_array_temp) < len(self.rt_nu): opac_array_temp.append(1e-15) # reverse array opac_array_temp.reverse() opac_array.extend(opac_array_temp) try: # create directory if necessary os.makedirs(param.resampling_path) except OSError: if not os.path.isdir(param.resampling_path): raise # save to hdf5 with h5py.File( param.resampling_path + self.species_name[m] + "_opac_sampling.h5", "w") as f: f.create_dataset("pressures", data=press_list) f.create_dataset("temperatures", data=temp_list) f.create_dataset("wavelengths", data=self.rt_lamda) f.create_dataset("opacities", data=opac_array) print("\nSuccessfully completed -->", self.species_name[m], "<-- !\n---------------------")