Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bring codeclimate up to A #4

Merged
merged 2 commits into from
Jan 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 79 additions & 33 deletions src/cortecs/opac/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,17 +138,17 @@ def load(self, cross_sec_filename, T_filename="", P_filename="", wl_filename="")
# todo: check wl units. They're in meters here.
# temperatures are in K.
# pressures are in Pa, I believe.
wl = np.load(wl_filename)
T = np.load(T_filename)
P = np.load(P_filename)
wl = np.load(wl_filename, allow_pickle=True)
T = np.load(T_filename, allow_pickle=True)
P = np.load(P_filename, allow_pickle=True)
cross_section = np.load(
cross_sec_filename
cross_sec_filename, allow_pickle=True
) # packaged as T x P x wl. todo: check packing
# cross-section units for fitting...? keep the same internally.
species = cross_sec_filename.split("_")[-1].split(".")[0]
try:
self.species_weight = self.species_weight_dict[species]
except:
except KeyError:
raise KeyError(
f"Species {species} read from filename and not found in species_weight_dict. Please add it."
)
Expand Down Expand Up @@ -190,8 +190,8 @@ def load(self, cross_sec_filename, T_filename, wl_filename, species_name):
# todo: check wl units. They're in meters here.
# temperatures are in K.
# pressures are in Pa, I believe.
wl = np.load(wl_filename)
T = np.load(T_filename)
wl = np.load(wl_filename, allow_pickle=True)
T = np.load(T_filename, allow_pickle=True)
data = pickle.load(
open(cross_sec_filename, "rb"), encoding="latin1"
) # packaged as T x P x wl. todo: check packing
Expand Down Expand Up @@ -327,6 +327,24 @@ def get_lams_and_opacities(self, file):
return np.array(wavelengths), np.array(opacities)

def load(self, filename):
"""
Loads file.

Inputs
------
filename : str
name of file to load

Outputs
-------
wl : np.ndarray
wavelengths
T : np.ndarray
temperatures
P : np.ndarray
pressures
"""

wl, opacities = self.get_lams_and_opacities(filename)
T, P = self.get_t_p(filename)

Expand All @@ -337,9 +355,16 @@ def load(self, filename):
def get_empty_species_dict(CIA_file, verbose=False):
"""
returns a species dictioanry given a CIA file
:param CIA_file:
:param verbose:
:return:

Inputs
-------
:CIA_file: (str) path to CIA file. e.g., 'opacCIA/opacCIA.dat'
:verbose: (bool) whether to print out the species that are likely in the file.

Outputs
-------
:species_dict: (dict) dictionary of species.

"""
n_species = get_n_species(CIA_file, verbose=verbose)
if n_species == 8:
Expand Down Expand Up @@ -484,30 +509,17 @@ def write(self, opac, outfile, verbose=False):
# todo: include the different temperature range on which to interpolate.

buffer = " " # there's a set of spaces between each string!
temp = 0.0 # initial value
for i in tqdm(range(len(reference_species)), desc="Writing file"):
# the first line gets different treatment!
if i == 0:
temp = np.min(
interped_temps
) # add the LOWEST temperature in the temperature grid!
new_string += ["{:.12e}".format(temp) + "\n"]
if interped_temps[i] != temp:
temp = interped_temps[i]
new_string += ["{:.12e}".format(temp) + "\n"]
wavelength_string = "{:.12e}".format(interped_wavelengths[i])

line_string = wavelength_string + buffer

for species_key in species_dict_interped.keys():
# again, this works because python dicts are ordered in 3.6+
line_string += (
"{:.12e}".format(
list(species_dict_interped[species_key].values())[i]
)
+ buffer
)

new_string += [line_string + "\n"]
new_string, temp = self.append_line_string(
new_string,
i,
interped_temps,
interped_wavelengths,
species_dict_interped,
buffer,
temp,
)

# todo: check the insert. and can pull wavelength grid.
temp_string = (
Expand All @@ -519,4 +531,38 @@ def write(self, opac, outfile, verbose=False):
f2.writelines(new_string)
f2.close()

def append_line_string(
self,
new_string,
i,
interped_temps,
interped_wavelengths,
species_dict_interped,
buffer,
temp,
):
# the first line gets different treatment!
if i == 0:
temp = np.min(
interped_temps
) # add the LOWEST temperature in the temperature grid!
new_string += ["{:.12e}".format(temp) + "\n"]
if interped_temps[i] != temp:
temp = interped_temps[i]
new_string += ["{:.12e}".format(temp) + "\n"]
wavelength_string = "{:.12e}".format(interped_wavelengths[i])

line_string = wavelength_string + buffer

for species_key in species_dict_interped.keys():
# again, this works because python dicts are ordered in 3.6+
line_string += (
"{:.12e}".format(list(species_dict_interped[species_key].values())[i])
+ buffer
)

new_string += [line_string + "\n"]

return new_string, temp

# todo: maybe the loader objects should also take an opac object. for parallel structure : )
Loading