Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# Add global quanta attributes, based on the HITRAN class
df = parse_global_quanta(df, mol)
# Remove non numerical attributes
if drop_non_numeric:
if 'branch' in df:
replace_PQR_with_m101(df)
df = drop_object_format_columns(df, verbose=verbose)
# cached file mode but cached file doesn't exist yet (else we had returned)
if cache:
if verbose:
print('Generating cached file: {0}'.format(fcache))
try:
save_to_hdf(df, fcache, metadata={}, version=radis.__version__,
key='df', overwrite=True, verbose=verbose)
except:
if verbose:
print(sys.exc_info())
print('An error occured in cache file generation. Lookup access rights')
pass
return df
# ... if file doesnt exist.
# If file is deprecated, regenerate it unless 'force' was used
from radis.misc.utils import getProjectRoot
from radis.misc.basics import make_folders
make_folders(join(getProjectRoot(), 'db'), molecule.upper())
filename = '{0}_iso{1}_levels.h5'.format(molecule.lower(), isotope)
cachefile = join(getProjectRoot(), 'db', molecule.upper(), filename)
self.cachefile = cachefile
# If return, return after cachefile generated (used for tests)
if use_cached == 'return':
return
self.df = load_h5_cache_file(cachefile, use_cached, metadata=metadata,
current_version=radis.__version__,
last_compatible_version=OLDEST_COMPATIBLE_VERSION,
verbose=verbose)
# Get levels
if molecule in HITRAN_CLASS1+HITRAN_CLASS2+HITRAN_CLASS3:
nb_vib_modes = 1
elif molecule in HITRAN_CLASS5+HITRAN_CLASS6:
nb_vib_modes = 3
else:
raise NotImplementedError
self.nb_vib_modes = nb_vib_modes
# Build energy levels if needed
if self.df is None:
# Build energy levels
if verbose:
return
df = load_h5_cache_file(cachefile, use_cached, metadata=metadata,
current_version=radis.__version__,
last_compatible_version=OLDEST_COMPATIBLE_VERSION,
verbose=verbose)
if df is None: # Read normal file
df = pd.read_csv(energy_levels, comment='#', delim_whitespace=True)
df = self._add_degeneracies(df)
df = self._add_levels(df)
self.df = df # Store
if use_cached and not exists(cachefile):
save_to_hdf(self.df, cachefile, metadata=metadata, version=radis.__version__,
key='df', overwrite=True, verbose=verbose)
attrs = dict(hf.attrs)
except OSError:
attrs = {}
finally:
hf.close()
# Raise an error if version is not found
try:
file_version = attrs.pop('version')
except KeyError:
raise DeprecatedFileError('File {0} has been generated in a deprecated '.format(file) +
'version. Delete it to regenerate it on next run')
# Get current version
if current_version is None:
current_version = radis.__version__
# If file version is anterior to a major change
# ... Update here versions afterwhich Deprecated cache file is not safe
# ... (example: a key name was changed)
if file_version < last_compatible_version:
raise DeprecatedFileError('File {0} has been generated in a deprecated '.format(file) +
'version ({0}). Oldest compatible version is {1}. '.format(
file_version, last_compatible_version) +
'Delete the file to regenerate it on next run')
# If file version is outdated: Warning, but no error
if current_version > file_version:
warn(DeprecationWarning('File {0} has been generated in '.format(file) +
'a deprecated version ({0}) compared to current ({1})'.format(
file_version, current_version) +
'. Delete it to regenerate it on next run'))
# %% Start reading the full file
df = parse_binary_file(fname, columns, count)
# Remove non numerical attributes
if drop_non_numeric:
replace_PQR_with_m101(df)
df = drop_object_format_columns(df, verbose=verbose)
# cached file mode but cached file doesn't exist yet (else we had returned)
if cache:
if verbose:
print('Generating cached file: {0}'.format(fcache))
try:
save_to_hdf(df, fcache, metadata={}, version=radis.__version__,
key='df', overwrite=True, verbose=verbose)
except:
if verbose:
print('An error occured in cache file generation. Lookup access rights')
pass
return df
ElecState.get_fullname()))
if not use_cached:
print('Set use_cached to True next time not to recompute levels every ' +
'time')
if molecule in HITRAN_CLASS1:
self.build_energy_levels_class1()
elif molecule in HITRAN_CLASS5: # CO2
self.build_energy_levels_class5(calc_Evib_per_mode=calc_Evib_per_mode,
calc_Evib_harmonic_anharmonic=calc_Evib_harmonic_anharmonic,
group_energy_modes_in_2T_model=group_energy_modes)
else:
raise NotImplementedError
# save files if use_cached
if use_cached and not exists(cachefile):
save_to_hdf(self.df, cachefile, metadata=metadata, version=radis.__version__,
key='df', overwrite=True, verbose=verbose)
# Add extra columns (note that this is not saved to disk)
self._add_extra()
return
'A': np.float64,
'airbrd': np.float64,
'selbrd': np.float64,
'El': np.float64,
'Tdpair': np.float64,
'Pshft': np.float64,
}
for c, typ in cast_type.items():
df[c] = df[c].astype(typ)
# cached file mode but cached file doesn't exist yet (else we had returned)
if cache:
if verbose:
print('Generating cached file: {0}'.format(fcache))
try:
save_to_hdf(df, fcache, metadata=metadata, version=radis.__version__,
key='df', overwrite=True, verbose=verbose)
except:
if verbose:
print(sys.exc_info())
print('An error occured in cache file generation. Lookup access rights')
pass
return df
if verbose >= 2:
_warn_if_object_columns(df, fname)
# Update metadata format
metadata = _h5_compatible(metadata)
# Overwrite file
if exists(fname) and not overwrite:
raise ValueError('File exist: {0}'.format(fname))
hf = h5py.File(fname, 'w')
try:
# Start by adding version
if version is None:
version = radis.__version__
hf.attrs['version'] = version
# Add metadata
for k, v in metadata.items():
hf.attrs[k] = v
except:
raise
finally:
hf.close()
# now export dataframe
df.to_hdf(fname, key, format='fixed', mode='a',
complevel=1, complib='blosc')
# df.to_hdf(fname, 'df', format='fixed', mode='a') # would be 10-20% faster, but take 2x more space
import radis
from warnings import warn
from os.path import exists
from radis import OLDEST_COMPATIBLE_VERSION
from radis.misc.basics import compare_dict, is_float
from radis.misc.printer import printr, printm
import pandas as pd
class DeprecatedFileError(DeprecationWarning):
pass
'''str: forces to regenerate cache files that were created in a previous version'''
# Just make sure LAST_BACKWARD_COMPATIBLE_VERSION is valid
assert radis.__version__ >= OLDEST_COMPATIBLE_VERSION
# Utils
def load_h5_cache_file(cachefile, use_cached, metadata, current_version,
last_compatible_version=OLDEST_COMPATIBLE_VERSION,
verbose=True):
''' Function to load a h5 cache file
Parameters
----------
cachefile: str
cache file path
use_cached: str
use cache file if value is not ``False``: