def fill_range_table_by_wave(wave_angstroms, distance_m): _lambda = wave_angstroms _e = round(ir_util.angstroms_to_ev(array=_lambda), 5) _v = round(3956. / np.sqrt(81.787 / (_e * 1000.)), 2) _tof = round(ir_util.ev_to_s(array=_e, source_to_detector_m=distance_m, offset_us=0) * 1e6, 4) _class = classify_neutron(_e) return {energy_name: _e, wave_name: _lambda, speed_name: _v, tof_name: _tof, class_name: _class}
def _load_beam_shape(relative_path_to_beam_shape): # Load beam shape from static df = pd.read_csv(relative_path_to_beam_shape, sep='\t', skiprows=0) df.columns = ['wavelength_A', 'flux'] # Get rid of crazy data df.drop(df[df.wavelength_A < 0].index, inplace=True) df.drop(df[df.flux <= 0].index, inplace=True) df.reset_index(drop=True, inplace=True) # Convert wavelength to energy energy_array = ir_util.angstroms_to_ev(df['wavelength_A']) df.insert(1, 'energy_eV', round(energy_array, 6)) # df.insert(1, 'energy_eV', energy_array) return df
def load_beam_shape(relative_path_to_beam_shape): # _path_to_beam_shape = 'static/instrument_file/beam_shape_cg1d.txt' # Load beam shape from static df = pd.read_csv(relative_path_to_beam_shape, sep='\t', skiprows=0) df.columns = ['wavelength_A', 'flux'] # Get rid of crazy data df.drop(df[df.wavelength_A < 0].index, inplace=True) df.drop(df[df.flux <= 0].index, inplace=True) df.reset_index(drop=True, inplace=True) # Convert wavelength to energy energy_list = ir_util.angstroms_to_ev(df['wavelength_A']) df.insert(1, 'energy_eV', energy_list) return df
def calculate_transmission(sample_tb_df, iso_tb_df, iso_changed, beamline, band_min, band_max, band_type, database): _main_path = os.path.abspath(os.path.dirname(__file__)) _path_to_beam_shape = { 'imaging': 'static/instrument_file/beam_flux_cg1d.txt', 'imaging_crop': 'static/instrument_file/beam_flux_cg1d_crop.txt', 'snap': 'static/instrument_file/beam_flux_snap.txt', # 'venus': 'static/instrument_file/beam_flux_venus.txt', } df_flux_raw = _load_beam_shape(_path_to_beam_shape[beamline]) if beamline in ['imaging', 'imaging_crop']: e_min = df_flux_raw['energy_eV'].min() e_max = df_flux_raw['energy_eV'].max() else: if band_type == 'lambda': e_min = round(ir_util.angstroms_to_ev(band_max), 6) e_max = round(ir_util.angstroms_to_ev(band_min), 6) else: # band_type == 'energy' e_min = band_min e_max = band_max e_step = (e_max - e_min) / (100 - 1) __o_reso = Resonance(energy_min=e_min, energy_max=e_max, energy_step=e_step, database=database) _o_reso = unpack_sample_tb_df_and_add_layer(o_reso=__o_reso, sample_tb_df=sample_tb_df) o_reso = unpack_iso_tb_df_and_update(o_reso=_o_reso, iso_tb_df=iso_tb_df, iso_changed=iso_changed) o_stack = o_reso.stack # interpolate with the beam shape energy energy = o_reso.total_signal['energy_eV'].round( 6) # !!!need to fix ImagingReso energy_eV columns interp_type = 'cubic' interp_flux_function = interp1d(x=df_flux_raw['energy_eV'], y=df_flux_raw['flux'], kind=interp_type) flux_interp = interp_flux_function(energy) df_flux_interp = pd.DataFrame() df_flux_interp['energy_eV'] = energy df_flux_interp['flux'] = flux_interp df_flux = df_flux_interp[:] trans_tag = 'transmission' mu_tag = 'mu_per_cm' o_signal = o_reso.stack_signal _total_trans = _calculate_transmission( flux_df=df_flux, trans_array=o_reso.total_signal[trans_tag]) for each_layer in o_stack.keys(): _current_layer_thickness = o_stack[each_layer]['thickness']['value'] if len(o_stack.keys()) == 1: _current_layer_trans = _total_trans else: _current_layer_trans = _calculate_transmission( flux_df=df_flux, trans_array=o_signal[each_layer][trans_tag]) o_stack[each_layer][trans_tag] = _current_layer_trans o_stack[each_layer][mu_tag] = _transmission_to_mu_per_cm( transmission=_current_layer_trans, thickness=_current_layer_thickness) for each_ele in o_stack[each_layer]['elements']: _current_ele_trans = _calculate_transmission( flux_df=df_flux, trans_array=o_signal[each_layer][each_ele][trans_tag]) o_stack[each_layer][each_ele][trans_tag] = _current_ele_trans o_stack[each_layer][each_ele][mu_tag] = _transmission_to_mu_per_cm( transmission=_current_ele_trans, thickness=_current_layer_thickness) return _total_trans, o_stack
def error(n_submit, database, sample_tb_rows, iso_tb_rows, range_tb_rows, iso_changed): if n_submit is not None: # Convert all number str to numeric and keep rest invalid input sample_tb_dict = force_dict_to_numeric(input_dict_list=sample_tb_rows) sample_tb_df = pd.DataFrame(sample_tb_dict) # Test sample input format test_passed_list, output_div_list = validate_sample_input( sample_df=sample_tb_df, sample_schema=sample_dict_schema, database=database) # Test density required or not if all(test_passed_list): test_passed_list, output_div_list = validate_density_input( sample_tb_df=sample_tb_df, test_passed_list=test_passed_list, output_div_list=output_div_list) # Test iso input format and sum if all(test_passed_list): if len(iso_changed) == 1: iso_tb_dict = force_dict_to_numeric( input_dict_list=iso_tb_rows) iso_tb_df = pd.DataFrame(iso_tb_dict) else: iso_tb_df = form_iso_table(sample_df=sample_tb_df, database=database) test_passed_list, output_div_list = validate_iso_input( iso_df=iso_tb_df, iso_schema=iso_dict_schema, test_passed_list=test_passed_list, output_div_list=output_div_list, database=database) # Test range table if all(test_passed_list): range_tb_dict = force_dict_to_numeric( input_dict_list=range_tb_rows) range_tb_df = pd.DataFrame(range_tb_dict) test_passed_list, output_div_list = validate_energy_input( range_tb_df=range_tb_df, test_passed_list=test_passed_list, output_div_list=output_div_list) # Test energy range for bonded H cross-sections if all(test_passed_list): for each_chem in sample_tb_dict[constants.chem_name]: if each_chem in ir_util.h_bond_list: for each_row in range_tb_rows: if each_row[constants.wave_name] > 5.35: energy_min = ir_util.angstroms_to_ev(5.35) test_passed_list.append(False) output_div_list.append( html. P(u"INPUT ERROR: {}: ['Only wavelengths <= 5.35 \u212B are currently supported supported for bonded H cross-sections']" .format(str(constants.wave_name)))) output_div_list.append( html. P(u"INPUT ERROR: {}: ['Only wavelengths >= {} \u212B are currently supported supported for bonded H cross-sections']" .format(str(constants.energy_name), energy_min))) # Return result if all(test_passed_list): return True else: return output_div_list else: return None
def error(n_submit, database, sample_tb_rows, iso_tb_rows, iso_changed, beamline, band_min, band_max, band_type): if n_submit is not None: # Convert all number str to numeric and keep rest invalid input sample_tb_dict = force_dict_to_numeric(input_dict_list=sample_tb_rows) sample_tb_df = pd.DataFrame(sample_tb_dict) # Test sample input format test_passed_list, output_div_list = validate_sample_input( sample_df=sample_tb_df, sample_schema=sample_dict_schema, database=database) # Test density required or not if all(test_passed_list): test_passed_list, output_div_list = validate_density_input( sample_tb_df=sample_tb_df, test_passed_list=test_passed_list, output_div_list=output_div_list) # Test iso input format and sum if all(test_passed_list): if len(iso_changed) == 1: iso_tb_dict = force_dict_to_numeric( input_dict_list=iso_tb_rows) iso_tb_df = pd.DataFrame(iso_tb_dict) else: iso_tb_df = form_iso_table(sample_df=sample_tb_df, database=database) test_passed_list, output_div_list = validate_iso_input( iso_df=iso_tb_df, iso_schema=iso_dict_schema, test_passed_list=test_passed_list, output_div_list=output_div_list, database=database) # Test band width input if all(test_passed_list): test_passed_list, output_div_list = validate_band_width_input( beamline=beamline, band_width=(band_min, band_max), band_type=band_type, test_passed_list=test_passed_list, output_div_list=output_div_list) # Test energy range for bonded H cross-sections if all(test_passed_list): for each_chem in sample_tb_dict[constants.chem_name]: if each_chem in ir_util.h_bond_list: if beamline == 'imaging': test_passed_list.append(False) output_div_list.append( html. P(u"SPECTRUM ERROR: ['Only wavelengths <= 5.35 \u212B are currently supported for " u"bonded H cross-sections in '{}'']".format( each_chem))) output_div_list.append( html. P(u"Please use spectrum: ['IMAGING (CG-1D) \u2264 5.35 \u212B, HFIR']" )) elif beamline == 'imaging_crop': test_passed_list.append(True) else: if band_type == 'lambda': if band_max > 5.35: test_passed_list.append(False) output_div_list.append( html. P(u"BAND WIDTH ERROR: ['Only wavelengths <= 5.35 \u212B are currently " u"supported supported for bonded H cross-sections in '{}'']" .format(each_chem))) if band_type == 'energy': wave_max = ir_util.ev_to_angstroms(band_min) energy_min = ir_util.angstroms_to_ev(5.35) if wave_max > 5.35: test_passed_list.append(False) output_div_list.append( html. P(u"BAND WIDTH ERROR: ['Only energies >= {} eV are currently supported " u"supported for bonded H cross-sections in '{}'']" .format(energy_min, each_chem))) # Return result if all(test_passed_list): return True else: return output_div_list else: return None
def store_bragg_df_in_json( n_submit, test_passed, cif_uploads, cif_names, temperature_K, distance_m, delay_us, band_min, band_max, band_step, ): if test_passed: error_div_list = [] xs_dict = {} wavelengths_A = np.arange(band_min, band_max, band_step) if cif_uploads is not None: for each_index, each_content in enumerate(cif_uploads): try: print("'{}', reading .cif file...".format( cif_names[each_index])) _cif_struc = parse_cif_upload(content=each_content) _name_only = cif_names[each_index].split('.')[0] print("'{}', calculating cross-sections...".format( cif_names[each_index])) xscalculator = xscalc.XSCalculator(_cif_struc, temperature_K, max_diffraction_index=4) xs_dict[_name_only + ' (total)'] = xscalculator.xs(wavelengths_A) xs_dict[_name_only + ' (abs)'] = xscalculator.xs_abs(wavelengths_A) xs_dict[_name_only + ' (coh el)'] = xscalculator.xs_coh_el( wavelengths_A) xs_dict[_name_only + ' (inc el)'] = xscalculator.xs_inc_el( wavelengths_A) xs_dict[_name_only + ' (coh inel)'] = xscalculator.xs_coh_inel( wavelengths_A) xs_dict[_name_only + ' (inc inel)'] = xscalculator.xs_inc_inel( wavelengths_A) print("Calculation done.") except AttributeError as error_msg1: print(str(error_msg1)) error1 = "ERROR: '{}', ".format( cif_names[each_index] ) + str(error_msg1).split( '.' )[0] + '. The .cif format is not compatible, please reformat following ICSD database.' error_div_list.append(error1) except ValueError as error_msg2: error2 = "ERROR: '{}', ".format( cif_names[each_index]) + str(error_msg2).split( '.')[0] + '.' error_div_list.append(error2) if len(error_div_list) == 0: df_y = pd.DataFrame.from_dict(xs_dict) df_x = pd.DataFrame() df_x[constants.energy_name] = ir_util.angstroms_to_ev( wavelengths_A) df_x = fill_df_x_types(df=df_x, distance_m=distance_m, delay_us=delay_us) datasets = { 'x': df_x.to_json(orient='split', date_format='iso'), 'y': df_y.to_json(orient='split', date_format='iso'), } return json.dumps(datasets), True else: return None, error_div_list else: return None, False else: return None, False