def restart(): try: shutil.rmtree(HAPI) except OSError: pass os.makedirs(HAPI) db_begin(HAPI)
def import_hitran_and_populate_particles_table(): start_time = time.time() ''' #change file to loadable version with open('/home/toma/Desktop/molecule_properties.txt', 'r') as f: outfile = open('/home/toma/Desktop/molecule_properties (copy).txt', 'w') for line in f: data = line.split() data.pop(1) for i in data: outfile.write("%s " % i) outfile.write('\n') outfile.close() f.close() ''' version_name = 'HITRAN_2016' reference_link = r'https://www.sciencedirect.com/science/article/pii/S0022407317301073?via%3Dihub' #everything is in string though to be noticed #length of lists are 124 mol_ids, iso_ids, iso_names, iso_abundances, iso_masses, mol_names = \ np.loadtxt('/home/toma/Desktop/molecule_properties (copy).txt', dtype='str', skiprows=1, usecols=(1, 2, 3, 4, 5, 6), unpack=True) for i in range(len(mol_ids)): particle_property_query = "INSERT INTO particles VALUES('%s', '%s', '%s', '%s', '%s', null);" % (mol_names[i], iso_names[i], \ iso_abundances[i], iso_masses[i], 1) #this one is temporary #insert each molecule's properties into particles table sql_order(particle_property_query) #then, fetch all the data from HITRAN using HAPI hapi.db_begin('data') #becasue cannot choose inifinity as upper limit, use a giant number instead #gpp is upper state degeneracy hapi.fetch(mol_names[i], int(mol_ids[i]), int(iso_ids[i]), 0, 1e9, Parameters=['nu', 'a', 'gamma_air', 'n_air', 'delta_air', \ 'elower', 'gpp', 'gamma_H2', 'n_H2', 'delta_H2', 'gamma_He', 'n_He', 'delta_He']) #open the file and use insert_hitran.py to insert all parameters into transitions table filename = '/home/toma/Desktop/linelists-database/data/{}.data'.format( mol_names[i]) insert_hitran.insert_hitran(filename, version_name, i + 1, reference_link) #delete the files since the files are named by HAPI using mol_name instead of iso_name #s.t. python wont get confused in the for loop header_filename = '/home/toma/Desktop/linelists-database/data/{}.header'.format( mol_names[i]) os.remove(filename) os.remove(header_filename) print("Finished in %s seconds" % (time.time() - start_time))
def calc_hapi(): """ Calc spectrum under HAPI """ clean_after_run = not exists(HAPIdb) and False try: db_begin(HAPIdb) if not molecule in tableList( ): # only if data not downloaded already fetch( molecule, mol_id, iso, wmin - broadening_max_width / 2, wmax + broadening_max_width / 2, ) # HAPI doesnt correct for side effects # Calculate with HAPI nu, coef = absorptionCoefficient_Voigt( SourceTables="CO2", Environment={ "T": T, "p": p / 1.01325, }, # K # atm WavenumberStep=dnu, HITRAN_units=False, GammaL="gamma_self", ) nu, trans = transmittanceSpectrum( nu, coef, Environment={ "l": L, }, ) # cm s_hapi = Spectrum.from_array( nu, trans, "transmittance_noslit", "cm-1", "1", conditions={"Tgas": T}, name="HAPI", ) except: raise finally: if clean_after_run: shutil.rmtree(HAPIdb) return s_hapi
def generate_availability_(): Config.data_folder = "__temp_data" hapi.db_begin(Config.data_folder) records = dict() for molecule_id in range(0, 100): molecule = MoleculeMeta(molecule_id) if not molecule.is_populated(): continue records[molecule.id] = check_availability(molecule) with open(f"res/parameters/availability.json", "w+") as f: f.write(json.dumps(records, indent=2, sort_keys=True)) print(records) sys.exit(0)
def test_broadening_vs_hapi(rtol=1e-2, verbose=True, plot=False, *args, **kwargs): """ Test broadening against HAPI and tabulated data We're looking at CO(0->1) line 'R1' at 2150.86 cm-1 """ from hapi import absorptionCoefficient_Voigt, db_begin, fetch, tableList if plot: # Make sure matplotlib is interactive so that test are not stuck in pytest plt.ion() setup_test_line_databases() # add HITRAN-CO-TEST in ~/.radis if not there # Conditions T = 3000 p = 0.0001 wstep = 0.001 wmin = 2150 # cm-1 wmax = 2152 # cm-1 broadening_max_width = 10 # cm-1 # %% HITRAN calculation # ----------- # Generate HAPI database locally hapi_data_path = join(dirname(__file__), __file__.replace(".py", "_HAPIdata")) db_begin(hapi_data_path) if not "CO" in tableList(): # only if data not downloaded already fetch("CO", 5, 1, wmin - broadening_max_width / 2, wmax + broadening_max_width / 2) # HAPI doesnt correct for side effects # Calculate with HAPI nu, coef = absorptionCoefficient_Voigt( SourceTables="CO", Environment={ "T": T, "p": p / 1.01325, }, # K # atm WavenumberStep=wstep, HITRAN_units=False, ) s_hapi = Spectrum.from_array(nu, coef, "abscoeff", "cm-1", "cm-1", conditions={"Tgas": T}, name="HAPI") # %% Calculate with RADIS # ---------- sf = SpectrumFactory( wavenum_min=wmin, wavenum_max=wmax, mole_fraction=1, path_length=1, # doesnt change anything wstep=wstep, pressure=p, broadening_max_width=broadening_max_width, isotope=[1], warnings={ "MissingSelfBroadeningWarning": "ignore", "NegativeEnergiesWarning": "ignore", "HighTemperatureWarning": "ignore", "GaussianBroadeningWarning": "ignore", }, ) # 0.2) sf.load_databank(path=join(hapi_data_path, "CO.data"), format="hitran", parfuncfmt="hapi") # s = pl.non_eq_spectrum(Tvib=T, Trot=T, Ttrans=T) s = sf.eq_spectrum(Tgas=T, name="RADIS") if plot: # plot broadening of line of largest linestrength sf.plot_broadening(i=sf.df1.S.idxmax()) # Plot and compare res = abs(get_residual_integral(s, s_hapi, "abscoeff")) if plot: plot_diff( s, s_hapi, var="abscoeff", title="{0} bar, {1} K (residual {2:.2g}%)".format(p, T, res * 100), show_points=False, ) plt.xlim((wmin, wmax)) if verbose: printm("residual:", res) assert res < rtol
s.name = "RADIS ({0:.1f}s)".format(s.conditions["calculation_time"]) # Print our HWHM for comparison (a posteriori) print(("HWHM max {0:.2f} cm-1".format(sf.df1.hwhm_voigt.max()))) print(( "WavenumberWingHW/HWHM", int(sf.params.broadening_max_width / (sf.df1.hwhm_voigt.max())), )) assert (int(sf.params.broadening_max_width / (sf.df1.hwhm_voigt.max()))) == benchmark_line_brd_ratio # %% Run HAPI print("Now calculating with HAPI") # Generate HAPI database locally db_begin(join(dirname(__file__), __file__.replace(".py", "_HAPIdata"))) if not molecule in tableList(): # only if data not downloaded already mol_iso_ids = [k for k in ISO_ID if ISO_ID[k][-1] == molecule] mol_iso_ids = [k for k in mol_iso_ids if ISO_ID[k][1] in isotopes] fetch_by_ids(molecule, mol_iso_ids, wavenum_min, wavenum_max) # Calculate with HAPI def calc_hapi(): nu, coef = absorptionCoefficient_Voigt( SourceTables=molecule, Environment={ "T": T, "p": pressure_bar / 1.01315, }, # K # atm GammaL="gamma_self", WavenumberStep=dnu,
plt.ylabel("Absorption Coefficient") plt.subplot(2, 2, 3) plt.plot(wave_numbers, datasets["NO"]["absorption_coefficient"], color="g") plt.title(f"NO Absorption") plt.xlabel("Wavenumber") plt.ylabel("Absorption Coefficient") plt.subplot(2, 2, 4) plt.plot(wave_numbers, mixture["absorption_coefficient"], color="k") plt.title(f"{mix[0]}%CO2-{mix[1]}%CH4-{mix[2]}%NO, Absorption") plt.xlabel("Wavenumber") plt.ylabel("Absorption Coefficient") plt.tight_layout() plt.savefig(f"plots/mixture_absorption.png") plt.clf() if __name__ == "__main__": db_begin(HAPI) for molecule, spec in MOLECULES.items(): properties(molecule, spec) folder = f"{HEAT}/plots/{molecule}" try: os.makedirs(folder) except OSError: pass sticks(molecule, folder) lineshapes(folder) absorptions(molecule, spec, folder) spectrums(molecule, spec, folder) hires(molecule, spec, folder)
from lib.ReadComputeFunc import ReadData import matplotlib.pyplot as plt Molecule = "CO" TempValue = 1000.0 P_Value = 0.1 #0.00001 #TempValue = 1000.0 #P_Value = 10.0 OmegaWingValue = 500.0 OmegaRangeValue = [2086.32193, 2093.410992] WaveNumber = np.arange(OmegaRangeValue[0], OmegaRangeValue[1] + 0.01, 0.01) Env = {'T': TempValue, 'p': P_Value} #StartTime = time.time() hapi.db_begin('TestData') nu_hapi_Doppler, abs_hapi_Doppler = hapi.absorptionCoefficient_Doppler( SourceTables=Molecule, OmegaGrid=WaveNumber, HITRAN_units=True, Environment=Env, GammaL='gamma_self', OmegaWing=OmegaWingValue, OmegaWingHW=0.0, LineShift=False) #, OmegaStep=0.01) nu_hapi_Voigt, abs_hapi_Voigt = hapi.absorptionCoefficient_Voigt( SourceTables=Molecule, OmegaGrid=WaveNumber, HITRAN_units=True, Environment=Env, GammaL='gamma_air',
html.H2([ u'ИС моделирования спектров\n коэфициентов поглощения смеси газов' ]), dcc.Link([ html.Img(src='/assets/help.svg'), ], href='/about', className='about-link'), ], style={'background': '#33b5ff'}, ), html.Div(id='page-content'), dcc.ConfirmDialog(id='confirm', ), ], ) db_begin('data') @app.callback(Output('page-content', 'children'), [Input('url', 'pathname')]) def display_page(pathname): if pathname == '/': return main elif pathname == '/about': return about else: return '404' if __name__ == '__main__': app.run_server(debug=True, dev_tools_ui=True)
D_startWavelength = control['delta start wavelength'] D_endWavelength = control['delta end wavelength'] D_minTH = control['delta min tangent height'] D_maxTH = control['delta max tangent height'] D_w1_step = control['delta w1 step'] D_n_nO2 = control['delta number of loosen O2 layers'] if control['if A band']: S_startWavelength = control['sigma start wavelength'] S_endWavelength = control['sigma end wavelength'] S_minTH = control['sigma min tangent height'] S_maxTH = control['sigma max tangent height'] S_w1_step = control['sigma w1 step'] S_n_nO2 = control['sigma number of loosen O2 layers'] # load hitran database db_begin(control['hitran database path']) if not os.path.exists( os.path.join( control['hitran database path'], 'O2_{:.1f}-{:.1f}.data'.format( D_startWavelength, D_endWavelength))): fetch('O2_{:.1f}-{:.1f}'.format(D_startWavelength, D_endWavelength), 7, 1, 1e7 / D_endWavelength, 1e7 / D_startWavelength) if control['if A band']: if not os.path.exists( os.path.join( control['hitran database path'], 'O2_{:.1f}-{:.1f}.data'.format(S_startWavelength, S_endWavelength))): fetch('O2_{:.1f}-{:.1f}'.format(S_startWavelength, S_endWavelength), 7, 1, 1e7 / S_endWavelength, 1e7 / S_startWavelength)
def load_hitran(self, v0, vf, threshhold=1e-28, hit_path='../inputs/HITRAN/'): """ Load hitran data file into dictionary Relies on hapi.py from hitran.org # To download new hitran data and get table summary: fetch_by_ids('H2O',[1,2],9000,20000,ParameterGroups=['Voigt_Air'])) # first two isotopologues fetch_by_ids('O2',[36,37],9000,20000,ParameterGroups=['Voigt_Air'])) # first two isotopologues tableList() describeTable('H2O') """ # Define parameters to include pnames = ['nu', 'sw', 'gamma_air', 'n_air', 'delta_air', 'a'] hitparams = { } # dictionary of hitran parameters, keys by molecule then pnames istr = { } # dictionary of indices of lines that are strong enough to fit iwk = {} # Load subset of hitran data already downloaded self.subhitpath = hit_path + 'v0_%s_vf_%s/' % (int(v0), int(vf)) # Load subfile if exists else make directory to store file if os.path.exists(self.subhitpath): # Load pre downloaded sub table pass else: os.system('mkdir ' + self.subhitpath) db_begin(hit_path) table = tableList() for i, molecule in enumerate(self.species): if molecule in table: subhitname = '%s_v0_%s_vf_%s.par' % (molecule, int(v0), int(vf)) subhitfile = self.subhitpath + subhitname # Save sub table into list select(molecule, Conditions=('AND', ('between', 'nu', v0 - 5.0, vf + 5.0), ('>=', 'sw', threshhold)), DestinationTableName=subhitname, File=subhitfile) # db_commit() # dont think i need this anymore else: # Download so can work offline ..no idea where this stores shit and i htink it fails so just d/l it yourself print 'Downloading Hitran data for ' + molecule fetch_by_ids(molecule, [1, 2, 3], 9000, 20000, ParameterGroups=['Voigt_Air' ]) # first two isotopologues db_begin(self.subhitpath) table = tableList() # with table loaded, Loop through molecules again and save parameters into dics for i, molecule in enumerate(self.species): subhitname = '%s_v0_%s_vf_%s.par' % (molecule, int(v0), int(vf)) # Get columns need for Voigt parameters hitparams[molecule] = {} istr[molecule] = {} iwk[molecule] = {} for parameter in pnames: hitparams[molecule][parameter] = getColumn( subhitname.strip('.par'), parameter) istr[molecule] = np.where( np.array(hitparams[molecule]['sw']) > self.fit_threshholds[i] )[0] iwk[molecule] = np.where((np.array(hitparams[molecule]['sw']) < self.fit_threshholds[i])\ & (np.array(hitparams[molecule]['sw']) > 0.1*self.fit_threshholds[i]))[0] self.hitran_params = hitparams # save hitran parameters self.istr = istr self.iwk = iwk
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Wed Nov 1 11:54:59 2017 @author: grosskc """ import numpy as np import hapi as ht # import pylab as plt ht.db_begin('data') # Set up calculation parameters nu1 = 400 nu2 = 7100 dnu = 0.0025 wnW = 350 nX = round((nu2 - nu1) / dnu) + 1 X = np.linspace(nu1, nu2, nX) T1 = 275 T2 = 320 dT = 5.0 nT = round((T2 - T1) / dT) + 1 T = np.linspace(T1, T2, nT) P1 = 0.85 P2 = 1.05 dP = 0.05 nP = round((P2 - P1) / dP) + 1 P = np.linspace(P1, P2, nP) molec = [1, 2]
######################## import numpy as np import os,sys,copy import hapi from spectro import wn2freq,wl2wn from calculator import * ######################## url_jpl = 'http://spec.jpl.nasa.gov' dir = 'JPL/' dir_hitran = 'HITRAN' file_catdir = dir + 'catdir.cat' file_molecules = dir + 'c%06i.cat' ######################## # prepare for using HITRAN database hapi.db_begin(dir_hitran) hitran_db_table = hapi.getTableList() def read_catdir(): format = 'I6,X, A13, I6, 7F7.4, I2' f = open(file_catdir,mode='r') flines = f.readlines() out = {} for fl in flines: tag = np.int(fl[:7]) namefactors = fl[7:13+7].split() if len(namefactors) > 1: for i in xrange(len(namefactors)-1): namefactors[i+1] = ' ' + namefactors[i+1] name = ''.join(namefactors) others = fl[13+7:].split()
import hapi as hp import matplotlib.pyplot as plt hp.db_begin("data") #hp.fetch("H2O",1,1,3400,4100) hp.select('H2O', ParameterNames=('nu', 'sw'), Conditions=('between', 'nu', 4000, 4100)) nu, coef = hp.absorptionCoefficient_Voigt(((1, 1), ), 'H2O', OmegaStep=1, HITRAN_units=False, GammaL='gamma_self', Environment={ 'p': 1, 'T': 296. }) plt.plot(nu, coef) plt.show()